File: lib.c

package info (click to toggle)
lm-sensors 1%3A2.10.1-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,104 kB
  • ctags: 10,474
  • sloc: ansic: 61,469; perl: 7,544; sh: 1,491; makefile: 400; lex: 300; yacc: 291
file content (131 lines) | stat: -rw-r--r-- 3,567 bytes parent folder | download | duplicates (4)
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
/*
 * sensord
 *
 * A daemon that periodically logs sensor information to syslog.
 *
 * Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
#include <sys/stat.h>

#include "sensord.h"
#include "lib/error.h"

static const char *sensorsCfgPaths[] = {
  "/etc", "/usr/local/etc", "/usr/lib/sensors", "/usr/local/lib/sensors", "/usr/lib", "/usr/local/lib", NULL
};

#define CFG_PATH_LEN 4096

static char cfgPath[CFG_PATH_LEN + 1];

static time_t cfgLastModified;

int
initLib
(void) {
  cfgPath[CFG_PATH_LEN] = '\0';
  if (!strcmp (sensorsCfgFile, "-")) {
    strncpy (cfgPath, sensorsCfgFile, CFG_PATH_LEN);
  } else if (sensorsCfgFile[0] == '/') {
    strncpy (cfgPath, sensorsCfgFile, CFG_PATH_LEN);
  } else if (strchr (sensorsCfgFile, '/')) {
    char *cwd = getcwd (NULL, 0);
    snprintf (cfgPath, CFG_PATH_LEN, "%s/%s", cwd, sensorsCfgFile);
    free (cwd);
  } else {
    int index0;
    struct stat stats;
    for (index0 = 0; sensorsCfgPaths[index0]; ++ index0) {
      snprintf (cfgPath, CFG_PATH_LEN, "%s/%s", sensorsCfgPaths[index0], sensorsCfgFile);
      if (stat (cfgPath, &stats) == 0)
        break;
    }
    if (!sensorsCfgPaths[index0]) {
      sensorLog (LOG_ERR, "Error locating sensors configuration file: %s", sensorsCfgFile);
      return 9;
    }
  }
  return 0;
}

static int
loadConfig
(int reload) {
  struct stat stats;
  FILE *cfg = NULL;
  int ret = 0;

  if (!strcmp (cfgPath, "-")) {
    if (!reload) {
      if ((ret = sensors_init (stdin))) {
        if (ret == -SENSORS_ERR_PROC)
          sensorLog (LOG_ERR, "Error reading /proc or /sys; modules probably not loaded");
        else
          sensorLog (LOG_ERR, "Error %d loading sensors configuration file: <stdin>", ret);
        ret = 12;
      }
    }
  } else if (stat (cfgPath, &stats) < 0) {
    sensorLog (LOG_ERR, "Error stating sensors configuration file: %s", cfgPath);
    ret = 10;
  } else if (!reload || (difftime (stats.st_mtime, cfgLastModified) > 0.0)) {
    if (reload)
      sensorLog (LOG_INFO, "configuration reloading");
    if (!(cfg = fopen (cfgPath, "r"))) {
      sensorLog (LOG_ERR, "Error opening sensors configuration file: %s", cfgPath);
      ret = 11;
    } else if ((ret = sensors_init (cfg))) {
      if (ret == -SENSORS_ERR_PROC)
        sensorLog (LOG_ERR, "Error reading /proc or /sys; modules probably not loaded");
      else
        sensorLog (LOG_ERR, "Error %d loading sensors configuration file: %s", ret, cfgPath);
      ret = 11;
    } else {
      cfgLastModified = stats.st_mtime;
    }
    if (cfg)
      fclose (cfg);
  }

  return ret;
}

int
loadLib
(void) {
  return loadConfig (0);
}

int
reloadLib
(void) {
  return loadConfig (1);
}

int
unloadLib
(void) {
  sensors_cleanup ();
  return 0;  
}