root/lm-sensors/branches/lm-sensors-3.0.0/lib/init.c @ 4990

Revision 4990, 4.8 KB (checked in by khali, 6 years ago)

Use /etc/sensors3.conf as the default configuration file. If it can't
be found, fallback to /etc/sensors.conf. This allows for an old
libsensors and a new libsensors to be installed in parallel, and each
one has its own configuration file.

One important change here is that the default configuration file will
be installed as /etc/sensors3.conf by "make install".

  • 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    Copyright (C) 2007        Jean Delvare <khali@linux-fr.org>
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include <stdlib.h>
22#include <stdio.h>
23#include <errno.h>
24#include "sensors.h"
25#include "data.h"
26#include "error.h"
27#include "access.h"
28#include "conf.h"
29#include "sysfs.h"
30#include "scanner.h"
31#include "init.h"
32
33#define DEFAULT_CONFIG_FILE     ETCDIR "/sensors3.conf"
34#define ALT_CONFIG_FILE         ETCDIR "/sensors.conf"
35
36int sensors_init(FILE *input)
37{
38        int res;
39
40        if (!sensors_init_sysfs())
41                return -SENSORS_ERR_KERNEL;
42        if ((res = sensors_read_sysfs_bus()) ||
43            (res = sensors_read_sysfs_chips()))
44                goto exit_cleanup;
45
46        res = -SENSORS_ERR_PARSE;
47        if (input) {
48                if (sensors_scanner_init(input) ||
49                    sensors_yyparse())
50                        goto exit_cleanup;
51        } else {
52                /* No configuration provided, use default */
53                input = fopen(DEFAULT_CONFIG_FILE, "r");
54                if (!input && errno == ENOENT)
55                        input = fopen(ALT_CONFIG_FILE, "r");
56                if (input) {
57                        if (sensors_scanner_init(input) ||
58                            sensors_yyparse()) {
59                                fclose(input);
60                                goto exit_cleanup;
61                        }
62                        fclose(input);
63                }
64        }
65
66        if ((res = sensors_substitute_busses()))
67                goto exit_cleanup;
68        return 0;
69
70exit_cleanup:
71        sensors_cleanup();
72        return res;
73}
74
75static void free_chip_name(sensors_chip_name *name)
76{
77        free(name->prefix);
78        free(name->path);
79}
80
81static void free_chip_features(sensors_chip_features *features)
82{
83        int i;
84
85        for (i = 0; i < features->subfeature_count; i++)
86                free(features->subfeature[i].name);
87        free(features->subfeature);
88        for (i = 0; i < features->feature_count; i++)
89                free(features->feature[i].name);
90        free(features->feature);
91}
92
93static void free_bus(sensors_bus *bus)
94{
95        free(bus->adapter);
96}
97
98static void free_label(sensors_label *label)
99{
100        free(label->name);
101        free(label->value);
102}
103
104void free_expr(sensors_expr *expr)
105{
106        if (expr->kind == sensors_kind_var)
107                free(expr->data.var);
108        else if (expr->kind == sensors_kind_sub) {
109                if (expr->data.subexpr.sub1)
110                        free_expr(expr->data.subexpr.sub1);
111                if (expr->data.subexpr.sub2)
112                        free_expr(expr->data.subexpr.sub2);
113        }
114        free(expr);
115}
116
117static void free_set(sensors_set *set)
118{
119        free(set->name);
120        free_expr(set->value);
121}
122
123static void free_compute(sensors_compute *compute)
124{
125        free(compute->name);
126        free_expr(compute->from_proc);
127        free_expr(compute->to_proc);
128}
129
130static void free_ignore(sensors_ignore *ignore)
131{
132        free(ignore->name);
133}
134
135static void free_chip(sensors_chip *chip)
136{
137        int i;
138
139        for (i = 0; i < chip->chips.fits_count; i++)
140                free_chip_name(&chip->chips.fits[i]);
141        free(chip->chips.fits);
142        chip->chips.fits_count = chip->chips.fits_max = 0;
143
144        for (i = 0; i < chip->labels_count; i++)
145                free_label(&chip->labels[i]);
146        free(chip->labels);
147        chip->labels_count = chip->labels_max = 0;
148
149        for (i = 0; i < chip->sets_count; i++)
150                free_set(&chip->sets[i]);
151        free(chip->sets);
152        chip->sets_count = chip->sets_max = 0;
153
154        for (i = 0; i < chip->computes_count; i++)
155                free_compute(&chip->computes[i]);
156        free(chip->computes);
157        chip->computes_count = chip->computes_max = 0;
158
159        for (i = 0; i < chip->ignores_count; i++)
160                free_ignore(&chip->ignores[i]);
161        free(chip->ignores);
162        chip->ignores_count = chip->ignores_max = 0;
163}
164
165void sensors_cleanup(void)
166{
167        int i;
168
169        sensors_scanner_exit();
170
171        for (i = 0; i < sensors_proc_chips_count; i++) {
172                free_chip_name(&sensors_proc_chips[i].chip);
173                free_chip_features(&sensors_proc_chips[i]);
174        }
175        free(sensors_proc_chips);
176        sensors_proc_chips = NULL;
177        sensors_proc_chips_count = sensors_proc_chips_max = 0;
178
179        for (i = 0; i < sensors_config_busses_count; i++)
180                free_bus(&sensors_config_busses[i]);
181        free(sensors_config_busses);
182        sensors_config_busses = NULL;
183        sensors_config_busses_count = sensors_config_busses_max = 0;
184
185        for (i = 0; i < sensors_config_chips_count; i++)
186                free_chip(&sensors_config_chips[i]);
187        free(sensors_config_chips);
188        sensors_config_chips = NULL;
189        sensors_config_chips_count = sensors_config_chips_max = 0;
190
191        for (i = 0; i < sensors_proc_bus_count; i++)
192                free_bus(&sensors_proc_bus[i]);
193        free(sensors_proc_bus);
194        sensors_proc_bus = NULL;
195        sensors_proc_bus_count = sensors_proc_bus_max = 0;
196}
Note: See TracBrowser for help on using the browser.