root/lm-sensors/branches/lm-sensors-3.0.0/prog/sensord/lib.c @ 4828

Revision 4828, 3.7 KB (checked in by khali, 7 years ago)

Scan chip features only once at initialization time, rather than on
each update. The chip list and chip features do not change until
sensors_cleanup() is called, so there's no point in doing the
same work over and over again.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/*
2 * sensord
3 *
4 * A daemon that periodically logs sensor information to syslog.
5 *
6 * Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <syslog.h>
27#include <time.h>
28#include <unistd.h>
29#include <sys/stat.h>
30
31#include "sensord.h"
32#include "lib/error.h"
33
34static const char *sensorsCfgPaths[] = {
35  "/etc", "/usr/local/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib", "/usr/local/lib", NULL
36};
37
38#define CFG_PATH_LEN 4096
39
40static char cfgPath[CFG_PATH_LEN + 1];
41
42static time_t cfgLastModified;
43
44int
45initLib
46(void) {
47  cfgPath[CFG_PATH_LEN] = '\0';
48  if (!strcmp (sensorsCfgFile, "-")) {
49    strncpy (cfgPath, sensorsCfgFile, CFG_PATH_LEN);
50  } else if (sensorsCfgFile[0] == '/') {
51    strncpy (cfgPath, sensorsCfgFile, CFG_PATH_LEN);
52  } else if (strchr (sensorsCfgFile, '/')) {
53    char *cwd = getcwd (NULL, 0);
54    snprintf (cfgPath, CFG_PATH_LEN, "%s/%s", cwd, sensorsCfgFile);
55    free (cwd);
56  } else {
57    int index0;
58    struct stat stats;
59    for (index0 = 0; sensorsCfgPaths[index0]; ++ index0) {
60      snprintf (cfgPath, CFG_PATH_LEN, "%s/%s", sensorsCfgPaths[index0], sensorsCfgFile);
61      if (stat (cfgPath, &stats) == 0)
62        break;
63    }
64    if (!sensorsCfgPaths[index0]) {
65      sensorLog (LOG_ERR, "Error locating sensors configuration file: %s", sensorsCfgFile);
66      return 9;
67    }
68  }
69  return 0;
70}
71
72static int
73loadConfig
74(int reload) {
75  struct stat stats;
76  FILE *cfg = NULL;
77  int ret = 0;
78
79  if (!strcmp (cfgPath, "-")) {
80    if (!reload) {
81      if ((ret = sensors_init (stdin))) {
82        if (ret == -SENSORS_ERR_PROC)
83          sensorLog (LOG_ERR, "Error reading /proc or /sys; modules probably not loaded");
84        else
85          sensorLog (LOG_ERR, "Error %d loading sensors configuration file: <stdin>", ret);
86        ret = 12;
87      }
88    }
89  } else if (stat (cfgPath, &stats) < 0) {
90    sensorLog (LOG_ERR, "Error stating sensors configuration file: %s", cfgPath);
91    ret = 10;
92  } else if (!reload || (difftime (stats.st_mtime, cfgLastModified) > 0.0)) {
93    if (reload) {
94      sensorLog (LOG_INFO, "configuration reloading");
95      sensors_cleanup ();
96    }
97    if (!(cfg = fopen (cfgPath, "r"))) {
98      sensorLog (LOG_ERR, "Error opening sensors configuration file: %s", cfgPath);
99      ret = 11;
100    } else if ((ret = sensors_init (cfg))) {
101      if (ret == -SENSORS_ERR_PROC)
102        sensorLog (LOG_ERR, "Error reading /proc or /sys; modules probably not loaded");
103      else
104        sensorLog (LOG_ERR, "Error %d loading sensors configuration file: %s", ret, cfgPath);
105      ret = 11;
106    } else {
107      cfgLastModified = stats.st_mtime;
108    }
109    if (cfg)
110      fclose (cfg);
111  }
112
113  return ret;
114}
115
116int
117loadLib
118(void) {
119  int ret;
120  ret = loadConfig (0);
121  if (!ret)
122    ret = initKnownChips ();
123  return ret;
124}
125
126int
127reloadLib
128(void) {
129  int ret;
130  freeKnownChips ();
131  ret = loadConfig (1);
132  if (!ret)
133    ret = initKnownChips ();
134  return ret;
135}
136
137int
138unloadLib
139(void) {
140  freeKnownChips ();
141  sensors_cleanup ();
142  return 0; 
143}
Note: See TracBrowser for help on using the browser.