Commit d500b090 authored by Kirk Webb's avatar Kirk Webb

Some important changes to slothd

1) Removed the ten iterations limit from main daemon loop I had set during
testing a while back (duh).  This was why slothd was mysteriously disappearing
after half a day.

2) Updated the makefile to produce static slothd binaries

3) Added a locking mechanism to avoid accidentally starting two slothd's
simulatneously.
parent 64c78cae
......@@ -26,7 +26,7 @@ fbsd: isfbsd slothd
$(CP) slothd fbsd/slothd
slothd: slothd.o slothd.h
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ slothd.o $(LIBS)
$(CC) $(CFLAGS) $(LDFLAGS) -static -o $@ slothd.o $(LIBS)
sdcollectd: sdcollectd.o sdcollectd.h
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ sdcollectd.o $(SDLIBS)
......
......@@ -3,16 +3,6 @@
SLOTHD_PACKET *pkt;
SLOTHD_OPTS *opts;
void siginthandler(int signum) {
int status;
while (wait(&status) != -1);
printf("exiting.\n");
exit(0);
}
void lerror(const char* msgstr) {
if (msgstr) {
syslog(LOG_ERR, "%s: %m", msgstr);
......@@ -34,6 +24,27 @@ void lnotice(const char *msgstr) {
}
}
void sigunkhandler(int signum) {
int status;
char message[50];
sprintf(message, "Unhandled signal: %d. Exiting.", signum);
lerror(message);
unlink(PIDFILE);
while (wait(&status) != -1);
exit(signum);
}
void siginthandler(int signum) {
int status;
unlink(PIDFILE);
while (wait(&status) != -1);
lnotice("exiting.");
exit(0);
}
void usage(void) {
printf("Usage:\tslothd -h\n");
......@@ -50,7 +61,9 @@ void usage(void) {
int main(int argc, char **argv) {
int exitcode = -1, i;
int exitcode = -1;
int pfd;
char pidbuf[10];
static SLOTHD_OPTS mopts;
static SLOTHD_PACKET mpkt;
......@@ -60,6 +73,15 @@ int main(int argc, char **argv) {
opts = &mopts;
pkt = &mpkt;
/* Try to get lock. If can't, then bail out. */
if ((pfd = open(PIDFILE, O_EXCL | O_CREAT | O_RDWR)) < 0) {
exit(1);
}
fchmod(pfd, S_IRUSR | S_IRGRP | S_IROTH);
sprintf(pidbuf, "%d", getpid());
write(pfd, pidbuf, sizeof(pidbuf));
close(pfd);
if (parse_args(argc, argv) < 0) {
fprintf(stderr, "Error processing arguments.\n");
}
......@@ -69,7 +91,8 @@ int main(int argc, char **argv) {
}
else {
exitcode = 0;
for (i = 0; i < 10; ++i) {
lnotice("Slothd started");
for (;;) {
if (!opts->first) {
sleep(mopts.interval);
}
......@@ -160,8 +183,14 @@ int init_slothd(void) {
/* Setup signals */
signal(SIGTERM, siginthandler);
signal(SIGINT, siginthandler);
signal(SIGQUIT, siginthandler);
signal(SIGHUP, SIG_IGN);
signal(SIGPIPE, SIG_IGN);
signal(SIGBUS, sigunkhandler);
signal(SIGSEGV, sigunkhandler);
signal(SIGFPE, sigunkhandler);
signal(SIGILL, sigunkhandler);
signal(SIGSYS, sigunkhandler);
/* Setup logging facil. */
openlog("slothd", LOG_NDELAY, LOG_DAEMON);
......@@ -184,6 +213,7 @@ int init_slothd(void) {
snprintf(bufstr, MAXDEVLEN, "/dev/%s", dptr->d_name);
opts->ttys[opts->numttys] = strdup(bufstr);
opts->numttys++;
bzero(bufstr, sizeof(bufstr));
}
}
}
......
......@@ -34,13 +34,14 @@
#define SLOTHD_PATH_ENV "/bin:/usr/bin:/sbin:/usr/sbin:/etc/testbed"
#define UTMP_PATH "/var/run/utmp"
#define WTMP_PATH "/var/log/wtmp"
#define PIDFILE "/tmp/.sdpid"
#define MACADDRLEN 18
#define MAXNUMIFACES 10
#define MAXIFNAMELEN 10
#define LINEBUFLEN 256
#define MAXTTYS 2000
#define MAXDEVLEN 50
#define MIN_INTVL 5
#define MIN_INTVL 1
#define DEF_INTVL 3600 /* 1 hour */
#define SLOTHD_DEF_SERV "boss"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment