Files
lm-sensors/prog/sensord/sensord.c
T
Merlin Hughes ae68e2238c Made a few changes to nonstandard sensord:
. added support for via686a and other new chips
. added support for alarms and beeps
. updated to log through syslog instead of a custom logfile
. updated so when run as "sensors" (ln -s sensord sensors) it
  mimics the existing sensors command except for formatting
. modularized a bit
Yet to do:
. complete support for alarm checking without sensor logging
. signal alarms through syslog level ALERT
. add final support for multivalued alarms
. check up on some questions about existing sensors command
. update manual page inc. notes on syslog usage
. test more fully
-- merlin


git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@828 7894878c-1315-0410-8ee3-d5d059ff63e0
2000-07-02 22:49:34 +00:00

157 lines
3.0 KiB
C

/*
* sensord
*
* A daemon that periodically logs sensor information to syslog.
*
* Copyright (c) 1999-2000 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 <signal.h>
#include <syslog.h>
#include <unistd.h>
#include "sensord.h"
static int logOpened = 0;
static volatile sig_atomic_t done = 0;
#define LOG_BUFFER 4096
#include <stdarg.h>
void
sensorLog
(int priority, const char *fmt, ...) {
static char buffer[1 + LOG_BUFFER];
va_list ap;
va_start (ap, fmt);
vsnprintf (buffer, LOG_BUFFER, fmt, ap);
buffer[LOG_BUFFER] = '\0';
va_end (ap);
if (logOpened) {
syslog (priority, "%s", buffer);
} else if (priority != LOG_DEBUG) {
fprintf (stderr, "%s\n", buffer);
fflush (stderr);
}
}
static void
signalHandler
(int sig) {
signal (sig, signalHandler);
switch (sig) {
case SIGTERM:
done = 1;
break;
}
}
static int
sensord
(void) {
int ret = 0;
sensorLog (LOG_INFO, "sensord started");
while (!done && (ret == 0)) {
if (ret == 0)
ret = reloadLib ();
if (ret == 0)
ret = readChips ();
if (!done && (ret == 0))
sleep (sleepTime);
}
sensorLog (LOG_INFO, "sensord %s", ret ? "failed" : "stopped");
return ret;
}
static void
daemonize
(void) {
int pid;
openlog ("sensord", 0, syslogFacility);
logOpened = 1;
if (chdir ("/") < 0) {
perror ("chdir()");
exit (EXIT_FAILURE);
}
/* I should use sigaction but... */
if (signal (SIGTERM, signalHandler) == SIG_ERR) {
perror ("signal(SIGTERM)");
exit (EXIT_FAILURE);
}
if ((pid = fork ()) == -1) {
perror ("fork()");
exit (EXIT_FAILURE);
} else if (pid != 0) {
exit (EXIT_SUCCESS);
}
if (setsid () < 0) {
perror ("setsid()");
exit (EXIT_FAILURE);
}
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
}
static void
undaemonize
(void) {
closelog ();
}
int
main
(int argc, char **argv) {
int ret;
if (parseArgs (argc, argv) ||
parseChips (argc, argv))
exit (EXIT_FAILURE);
if (initLib () ||
loadLib ())
exit (EXIT_FAILURE);
if (isDaemon) {
daemonize ();
ret = sensord ();
undaemonize ();
} else {
ret = readChips ();
}
if (unloadLib ())
exit (EXIT_FAILURE);
return ret;
}