Debian Codegraph @

prog/sensord/lib.c

raw
1 /*
2 * sensord
3 *
4 * A daemon that periodically logs sensor information to syslog.
5 *
6 * Copyright (c) 1999-2002 Merlin Hughes <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21 * MA 02110-1301 USA.
22 */
23
24 #include <errno.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <syslog.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <sys/stat.h>
32
33 #include "sensord.h"
34 #include "lib/error.h"
35
36 static int loadConfig(const char *cfgPath, int reload)
37 {
38 int ret;
39 FILE *fp;
40
41 /* Load default configuration. */
42 if (!cfgPath) {
43 if (reload) {
44 sensorLog(LOG_INFO, "configuration reloading");
45 sensors_cleanup();
46 }
47
48 ret = sensors_init(NULL);
49 if (ret) {
50 sensorLog(LOG_ERR, "Error loading default"
51 " configuration file: %s",
52 sensors_strerror(ret));
53 return -1;
54 }
55 return 0;
56 }
57
58 fp = fopen(cfgPath, "r");
59 if (!fp) {
60 sensorLog(LOG_ERR, "Error opening config file %s: %s",
61 strerror(errno));
62 return -1;
63 }
64
65 if (reload) {
66 sensorLog(LOG_INFO, "configuration reloading");
67 sensors_cleanup();
68 }
69 ret = sensors_init(fp);
70 if (ret) {
71 sensorLog(LOG_ERR, "Error loading sensors configuration file"
72 " %s: %s", cfgPath, sensors_strerror(ret));
73 fclose(fp);
74 return -1;
75 }
76 fclose(fp);
77
78 return 0;
79 }
80
81 int loadLib(const char *cfgPath)
82 {
83 int ret;
84 ret = loadConfig(cfgPath, 0);
85 if (!ret)
86 ret = initKnownChips();
87 return ret;
88 }
89
90 int reloadLib(const char *cfgPath)
91 {
92 int ret;
93 freeKnownChips();
94 ret = loadConfig(cfgPath, 1);
95 if (!ret)
96 ret = initKnownChips();
97 return ret;
98 }
99
100 int unloadLib(void)
101 {
102 freeKnownChips();
103 sensors_cleanup();
104 return 0;
105 }