-
Notifications
You must be signed in to change notification settings - Fork 67
/
init.c
343 lines (285 loc) · 9.13 KB
/
init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
init.c - Part of libsensors, a Linux library for reading sensor data.
Copyright (c) 1998, 1999 Frodo Looijaard <[email protected]>
Copyright (C) 2007-2013 Jean Delvare <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301 USA.
*/
/*** This file modified by Arm on Jan 23, 2013 to cast alphasort to supress a warning as it's prototype is different on android. ***/
/*** This file modified by Arm on Aug 14, 2015 to comment out _BSD_SOURCE as it causes warnings with glibc 2.20 and doesn't appear to be needed. ***/
/* #define _BSD_SOURCE */
#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <unistd.h>
#include "sensors.h"
#include "data.h"
#include "error.h"
#include "access.h"
#include "conf.h"
#include "sysfs.h"
#include "scanner.h"
#include "init.h"
#define DEFAULT_CONFIG_FILE ETCDIR "/sensors3.conf"
#define ALT_CONFIG_FILE ETCDIR "/sensors.conf"
#define DEFAULT_CONFIG_DIR ETCDIR "/sensors.d"
/* Wrapper around sensors_yyparse(), which clears the locale so that
the decimal numbers are always parsed properly. */
static int sensors_parse(void)
{
int res;
char *locale;
/* Remember the current locale and clear it */
locale = setlocale(LC_ALL, NULL);
if (locale) {
locale = strdup(locale);
if (!locale)
sensors_fatal_error(__func__, "Out of memory");
setlocale(LC_ALL, "C");
}
res = sensors_yyparse();
/* Restore the old locale */
if (locale) {
setlocale(LC_ALL, locale);
free(locale);
}
return res;
}
static void free_bus(sensors_bus *bus)
{
free(bus->adapter);
}
static void free_config_busses(void)
{
int i;
for (i = 0; i < sensors_config_busses_count; i++)
free_bus(&sensors_config_busses[i]);
free(sensors_config_busses);
sensors_config_busses = NULL;
sensors_config_busses_count = sensors_config_busses_max = 0;
}
static int parse_config(FILE *input, const char *name)
{
int err;
char *name_copy;
if (name) {
/* Record configuration file name for error reporting */
name_copy = strdup(name);
if (!name_copy)
sensors_fatal_error(__func__, "Out of memory");
sensors_add_config_files(&name_copy);
} else
name_copy = NULL;
if (sensors_scanner_init(input, name_copy)) {
err = -SENSORS_ERR_PARSE;
goto exit_cleanup;
}
err = sensors_parse();
sensors_scanner_exit();
if (err) {
err = -SENSORS_ERR_PARSE;
goto exit_cleanup;
}
err = sensors_substitute_busses();
exit_cleanup:
free_config_busses();
return err;
}
static int config_file_filter(const struct dirent *entry)
{
return entry->d_name[0] != '.'; /* Skip hidden files */
}
static int add_config_from_dir(const char *dir)
{
int count, res, i;
struct dirent **namelist;
count = scandir(dir, &namelist, config_file_filter, (int (*)(const struct dirent **, const struct dirent **))alphasort);
if (count < 0) {
/* Do not return an error if directory does not exist */
if (errno == ENOENT)
return 0;
sensors_parse_error_wfn(strerror(errno), NULL, 0);
return -SENSORS_ERR_PARSE;
}
for (res = 0, i = 0; !res && i < count; i++) {
int len;
char path[PATH_MAX];
FILE *input;
struct stat st;
len = snprintf(path, sizeof(path), "%s/%s", dir,
namelist[i]->d_name);
if (len < 0 || len >= (int)sizeof(path)) {
res = -SENSORS_ERR_PARSE;
continue;
}
/* Only accept regular files */
if (stat(path, &st) < 0 || !S_ISREG(st.st_mode))
continue;
input = fopen(path, "r");
if (input) {
res = parse_config(input, path);
fclose(input);
} else {
res = -SENSORS_ERR_PARSE;
sensors_parse_error_wfn(strerror(errno), path, 0);
}
}
/* Free memory allocated by scandir() */
for (i = 0; i < count; i++)
free(namelist[i]);
free(namelist);
return res;
}
/* Ideally, initialization and configuraton file loading should be exposed
separately, to make it possible to load several configuration files. */
int sensors_init(FILE *input)
{
int res;
if (!sensors_init_sysfs())
return -SENSORS_ERR_KERNEL;
if ((res = sensors_read_sysfs_bus()) ||
(res = sensors_read_sysfs_chips()))
goto exit_cleanup;
if (input) {
res = parse_config(input, NULL);
if (res)
goto exit_cleanup;
} else {
const char* name;
/* No configuration provided, use default */
input = fopen(name = DEFAULT_CONFIG_FILE, "r");
if (!input && errno == ENOENT)
input = fopen(name = ALT_CONFIG_FILE, "r");
if (input) {
res = parse_config(input, name);
fclose(input);
if (res)
goto exit_cleanup;
} else if (errno != ENOENT) {
sensors_parse_error_wfn(strerror(errno), name, 0);
res = -SENSORS_ERR_PARSE;
goto exit_cleanup;
}
/* Also check for files in default directory */
res = add_config_from_dir(DEFAULT_CONFIG_DIR);
if (res)
goto exit_cleanup;
}
return 0;
exit_cleanup:
sensors_cleanup();
return res;
}
static void free_chip_name(sensors_chip_name *name)
{
free(name->prefix);
free(name->path);
}
static void free_chip_features(sensors_chip_features *features)
{
int i;
for (i = 0; i < features->subfeature_count; i++)
free(features->subfeature[i].name);
free(features->subfeature);
for (i = 0; i < features->feature_count; i++)
free(features->feature[i].name);
free(features->feature);
}
static void free_label(sensors_label *label)
{
free(label->name);
free(label->value);
}
void sensors_free_expr(sensors_expr *expr)
{
if (expr->kind == sensors_kind_var)
free(expr->data.var);
else if (expr->kind == sensors_kind_sub) {
if (expr->data.subexpr.sub1)
sensors_free_expr(expr->data.subexpr.sub1);
if (expr->data.subexpr.sub2)
sensors_free_expr(expr->data.subexpr.sub2);
}
free(expr);
}
static void free_set(sensors_set *set)
{
free(set->name);
sensors_free_expr(set->value);
}
static void free_compute(sensors_compute *compute)
{
free(compute->name);
sensors_free_expr(compute->from_proc);
sensors_free_expr(compute->to_proc);
}
static void free_ignore(sensors_ignore *ignore)
{
free(ignore->name);
}
static void free_chip(sensors_chip *chip)
{
int i;
for (i = 0; i < chip->chips.fits_count; i++)
free_chip_name(&chip->chips.fits[i]);
free(chip->chips.fits);
chip->chips.fits_count = chip->chips.fits_max = 0;
for (i = 0; i < chip->labels_count; i++)
free_label(&chip->labels[i]);
free(chip->labels);
chip->labels_count = chip->labels_max = 0;
for (i = 0; i < chip->sets_count; i++)
free_set(&chip->sets[i]);
free(chip->sets);
chip->sets_count = chip->sets_max = 0;
for (i = 0; i < chip->computes_count; i++)
free_compute(&chip->computes[i]);
free(chip->computes);
chip->computes_count = chip->computes_max = 0;
for (i = 0; i < chip->ignores_count; i++)
free_ignore(&chip->ignores[i]);
free(chip->ignores);
chip->ignores_count = chip->ignores_max = 0;
}
void sensors_cleanup(void)
{
int i;
for (i = 0; i < sensors_proc_chips_count; i++) {
free_chip_name(&sensors_proc_chips[i].chip);
free_chip_features(&sensors_proc_chips[i]);
}
free(sensors_proc_chips);
sensors_proc_chips = NULL;
sensors_proc_chips_count = sensors_proc_chips_max = 0;
for (i = 0; i < sensors_config_chips_count; i++)
free_chip(&sensors_config_chips[i]);
free(sensors_config_chips);
sensors_config_chips = NULL;
sensors_config_chips_count = sensors_config_chips_max = 0;
sensors_config_chips_subst = 0;
for (i = 0; i < sensors_proc_bus_count; i++)
free_bus(&sensors_proc_bus[i]);
free(sensors_proc_bus);
sensors_proc_bus = NULL;
sensors_proc_bus_count = sensors_proc_bus_max = 0;
for (i = 0; i < sensors_config_files_count; i++)
free(sensors_config_files[i]);
free(sensors_config_files);
sensors_config_files = NULL;
sensors_config_files_count = sensors_config_files_max = 0;
}