root/lm-sensors/trunk/lib/init.c @ 3293

Revision 3293, 4.4 KB (checked in by mmh, 8 years ago)

fix 2 leaks found by valgrind - missing some free() during cleanup
(as opposed to ongoing run-time leaks)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2    init.c - Part of libsensors, a Linux library for reading sensor data.
3    Copyright (c) 1998, 1999  Frodo Looijaard <frodol@dds.nl>
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20#include <stdlib.h>
21#include <stdio.h>
22#include "sensors.h"
23#include "data.h"
24#include "proc.h"
25#include "error.h"
26#include "access.h"
27#include "conf.h"
28#include "sysfs.h"
29
30static void free_proc_chips_entry(sensors_proc_chips_entry entry);
31static void free_chip_name(sensors_chip_name name);
32static void free_bus(sensors_bus bus);
33static void free_chip(sensors_chip chip);
34static void free_label(sensors_label label);
35static void free_set(sensors_set set);
36static void free_compute(sensors_compute compute);
37static void free_ignore(sensors_ignore ignore);
38static void free_expr(sensors_expr *expr);
39
40int sensors_init(FILE *input)
41{
42  int res;
43  sensors_cleanup();
44  if (sensors_init_sysfs()) {
45    if ((res = sensors_read_sysfs_chips()) || (res = sensors_read_sysfs_bus()))
46      return res;
47  } else {
48    if ((res = sensors_read_proc_chips()) || (res = sensors_read_proc_bus()))
49      return res;
50  }
51  sensors_yyin = input;
52  if ((res = sensors_yyparse()))
53    return -SENSORS_ERR_PARSE;
54  if ((res = sensors_substitute_busses()));
55    return res;
56  return 0;
57}
58
59void sensors_cleanup(void)
60{
61  int i;
62
63  for (i = 0; i < sensors_proc_chips_count; i++)
64    free_proc_chips_entry(sensors_proc_chips[i]);
65  free(sensors_proc_chips);
66  sensors_proc_chips = NULL;
67  sensors_proc_chips_count = sensors_proc_chips_max = 0;
68 
69  for (i = 0; i < sensors_config_busses_count; i++)
70    free_bus(sensors_config_busses[i]);
71  free(sensors_config_busses);
72  sensors_config_busses = NULL;
73  sensors_config_busses_count = sensors_config_busses_max = 0;
74
75  for (i = 0; i < sensors_config_chips_count; i++)
76    free_chip(sensors_config_chips[i]);
77  free(sensors_config_chips);
78  sensors_config_chips = NULL;
79  sensors_config_chips_count = sensors_config_chips_max = 0;
80
81  for (i = 0; i < sensors_proc_bus_count; i++)
82    free_bus(sensors_proc_bus[i]);
83  free(sensors_proc_bus);
84  sensors_proc_bus = NULL;
85  sensors_proc_bus_count = sensors_proc_bus_max = 0;
86}
87
88void free_proc_chips_entry(sensors_proc_chips_entry entry)
89{
90    free_chip_name(entry.name);
91}
92
93void free_chip_name(sensors_chip_name name)
94{
95  free(name.prefix);
96  free(name.busname);
97}
98
99void free_bus(sensors_bus bus)
100{
101  free(bus.adapter);
102  free(bus.algorithm);
103}
104
105void free_chip(sensors_chip chip)
106{
107  int i;
108
109  for (i = 0; i < chip.chips.fits_count; i++)
110    free_chip_name(chip.chips.fits[i]);
111  free(chip.chips.fits);
112  chip.chips.fits_count = chip.chips.fits_max = 0;
113
114  for (i = 0; i < chip.labels_count; i++)
115    free_label(chip.labels[i]);
116  free(chip.labels);
117  chip.labels_count = chip.labels_max = 0;
118
119  for (i = 0; i < chip.sets_count; i++)
120    free_set(chip.sets[i]);
121  free(chip.sets);
122  chip.sets_count = chip.sets_max = 0;
123
124  for (i = 0; i < chip.computes_count; i++)
125    free_compute(chip.computes[i]);
126  free(chip.computes);
127  chip.computes_count = chip.computes_max = 0;
128
129  for (i = 0; i < chip.ignores_count; i++)
130    free_ignore(chip.ignores[i]);
131  free(chip.ignores);
132  chip.ignores_count = chip.ignores_max = 0;
133}
134
135void free_label(sensors_label label)
136{
137  free(label.name);
138  free(label.value);
139}
140
141void free_set(sensors_set set)
142{
143  free(set.name);
144  free_expr(set.value);
145}
146
147void free_compute(sensors_compute compute)
148{
149  free(compute.name);
150  free_expr(compute.from_proc);
151  free_expr(compute.to_proc);
152}
153
154void free_ignore(sensors_ignore ignore)
155{
156  free(ignore.name);
157}
158
159void free_expr(sensors_expr *expr)
160{
161  if ((expr->kind) == sensors_kind_var)
162    free(expr->data.var);
163  else if ((expr->kind) == sensors_kind_sub) {
164    if (expr->data.subexpr.sub1)
165      free_expr(expr->data.subexpr.sub1);
166    if (expr->data.subexpr.sub2)
167      free_expr(expr->data.subexpr.sub2);
168  }
169  free(expr);
170}
Note: See TracBrowser for help on using the browser.