4
* A daemon that periodically logs sensor information to syslog.
6
* Copyright (c) 1999-2002 Merlin Hughes <merlin@merlin.org>
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.
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.
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,
33
#include <sys/types.h>
39
static int logOpened = 0;
41
static volatile sig_atomic_t done = 0;
42
static volatile sig_atomic_t reload = 0;
44
#define LOG_BUFFER 4096
48
void sensorLog(int priority, const char *fmt, ...)
50
static char buffer[1 + LOG_BUFFER];
53
vsnprintf(buffer, LOG_BUFFER, fmt, ap);
54
buffer[LOG_BUFFER] = '\0';
56
if (sensord_args.debug || (priority < LOG_DEBUG)) {
58
syslog(priority, "%s", buffer);
60
fprintf(stderr, "%s\n", buffer);
66
static void signalHandler(int sig)
78
static int sensord(void)
81
int scanValue = 0, logValue = 0;
83
* First RRD update at next RRD timeslot to prevent failures due
84
* one timeslot updated twice on restart for example.
86
int rrdValue = sensord_args.rrdTime - time(NULL) %
89
sensorLog(LOG_INFO, "sensord started");
93
ret = reloadLib(sensord_args.cfgFile);
95
sensorLog(LOG_NOTICE, "configuration reload"
99
if (sensord_args.scanTime && (scanValue <= 0)) {
100
if ((ret = scanChips()))
101
sensorLog(LOG_NOTICE,
102
"sensor scan error (%d)", ret);
103
scanValue += sensord_args.scanTime;
105
if (sensord_args.logTime && (logValue <= 0)) {
106
if ((ret = readChips()))
107
sensorLog(LOG_NOTICE,
108
"sensor read error (%d)", ret);
109
logValue += sensord_args.logTime;
111
if (sensord_args.rrdTime && sensord_args.rrdFile &&
113
if ((ret = rrdUpdate()))
114
sensorLog(LOG_NOTICE,
115
"rrd update error (%d)", ret);
117
* The amount of time to wait is computed using the
118
* same method as in RRD instead of simply adding the
121
rrdValue = sensord_args.rrdTime - time(NULL) %
122
sensord_args.rrdTime;
125
int a = sensord_args.logTime ? logValue : INT_MAX;
126
int b = sensord_args.scanTime ? scanValue : INT_MAX;
127
int c = (sensord_args.rrdTime && sensord_args.rrdFile)
128
? rrdValue : INT_MAX;
129
int sleepTime = (a < b) ? ((a < c) ? a : c) :
132
scanValue -= sleepTime;
133
logValue -= sleepTime;
134
rrdValue -= sleepTime;
138
sensorLog(LOG_INFO, "sensord stopped");
143
static void openLog(void)
145
openlog("sensord", 0, sensord_args.syslogFacility);
149
static void install_sighandler(void)
151
struct sigaction new;
154
memset(&new, 0, sizeof(struct sigaction));
155
new.sa_handler = signalHandler;
156
sigemptyset(&new.sa_mask);
157
new.sa_flags = SA_RESTART;
159
ret = sigaction(SIGTERM, &new, NULL);
161
fprintf(stderr, "Could not set sighandler for SIGTERM: %s\n",
166
ret = sigaction(SIGHUP, &new, NULL);
168
fprintf(stderr, "Could not set sighandler for SIGHUP: %s\n",
174
static void daemonize(void)
177
struct stat fileStat;
180
if (chdir("/") < 0) {
185
if (!(stat(sensord_args.pidFile, &fileStat)) &&
186
((!S_ISREG(fileStat.st_mode)) || (fileStat.st_size > 11))) {
188
"Error: PID file `%s' already exists and looks suspicious.\n",
189
sensord_args.pidFile);
193
if (!(file = fopen(sensord_args.pidFile, "w"))) {
194
fprintf(stderr, "fopen(\"%s\"): %s\n", sensord_args.pidFile,
199
install_sighandler();
201
if ((pid = fork()) == -1) {
204
} else if (pid != 0) {
205
fprintf(file, "%d\n", pid);
222
close(STDOUT_FILENO);
223
close(STDERR_FILENO);
226
static void undaemonize(void)
228
unlink(sensord_args.pidFile);
232
int main(int argc, char **argv)
236
if (parseArgs(argc, argv) ||
237
parseChips(argc, argv))
240
if (loadLib(sensord_args.cfgFile)) {
245
if (!sensord_args.doCGI)
248
if (sensord_args.rrdFile) {
256
if (sensord_args.doCGI) {