| 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 | |
|---|
| 34 | static 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 | |
|---|
| 40 | static char cfgPath[CFG_PATH_LEN + 1]; |
|---|
| 41 | |
|---|
| 42 | static time_t cfgLastModified; |
|---|
| 43 | |
|---|
| 44 | int |
|---|
| 45 | initLib |
|---|
| 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 | |
|---|
| 72 | static int |
|---|
| 73 | loadConfig |
|---|
| 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 | |
|---|
| 116 | int |
|---|
| 117 | loadLib |
|---|
| 118 | (void) { |
|---|
| 119 | return loadConfig (0); |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | int |
|---|
| 123 | reloadLib |
|---|
| 124 | (void) { |
|---|
| 125 | return loadConfig (1); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | int |
|---|
| 129 | unloadLib |
|---|
| 130 | (void) { |
|---|
| 131 | sensors_cleanup (); |
|---|
| 132 | return 0; |
|---|
| 133 | } |
|---|