Commit 3145c690 authored by Kirk Webb's avatar Kirk Webb

Added "-o" option to slothd to run once, send an update, and exit.

Also changed PID file from /var/run/slothd to /var/run/slothd.pid
parent e317e5c2
......@@ -78,7 +78,7 @@ int main(int argc, char **argv) {
}
else {
if (init_slothd() < 0) {
lerror("Problem initializing, bailing out");
lerror("Problem initializing, bailing out.");
}
else {
exitcode = 0;
......@@ -88,12 +88,12 @@ int main(int argc, char **argv) {
if (!opts->first) {
sleep(mopts.interval);
}
get_load();
get_min_tty_idle();
get_packet_counts();
send_pkt();
do_update();
if (opts->once) {
break;
}
if (opts->first) {
sleep(mopts.interval);
sleep(mopts.interval);
}
}
}
......@@ -101,6 +101,12 @@ int main(int argc, char **argv) {
return exitcode;
}
void do_update (void) {
get_load();
get_min_tty_idle();
get_packet_counts();
send_pkt();
}
int parse_args(int argc, char **argv) {
......@@ -113,10 +119,15 @@ int parse_args(int argc, char **argv) {
opts->port = SLOTHD_DEF_PORT;
opts->servname = SLOTHD_DEF_SERV;
opts->first = 1;
opts->once = 0;
while ((ch = getopt(argc, argv, "ai:dp:s:hf")) != -1) {
while ((ch = getopt(argc, argv, "ai:dp:s:hfo")) != -1) {
switch (ch) {
case 'o': /* run once */
opts->once = 1;
break;
case 'i':
if ((opts->interval = atoi(optarg)) < MIN_INTVL) {
lwarn("Warning! Interval set too low, defaulting.");
......@@ -241,22 +252,22 @@ int init_slothd(void) {
return -1;
}
/* Daemonize, unless in debug mode. */
if (!opts->debug && daemon(0,0) < 0) {
lerror("Couldn't daemonize");
return -1;
}
/* Try to get lock. If can't, then bail out. */
if ((pfd = open(PIDFILE, O_EXCL | O_CREAT | O_RDWR)) < 0) {
lerror("Can't create lock file, quiting.");
exit(1);
/* Daemonize, unless in debug, or once-only mode. */
if (!opts->debug && !opts->once) {
if (daemon(0,0) < 0) {
lerror("Couldn't daemonize");
return -1;
}
/* Try to get lock. If can't, then bail out. */
if ((pfd = open(PIDFILE, O_EXCL | O_CREAT | O_RDWR)) < 0) {
lerror("Can't create lock file.");
return -1;
}
fchmod(pfd, S_IRUSR | S_IRGRP | S_IROTH);
sprintf(pidbuf, "%d", getpid());
write(pfd, pidbuf, strlen(pidbuf));
close(pfd);
}
fchmod(pfd, S_IRUSR | S_IRGRP | S_IROTH);
sprintf(pidbuf, "%d", getpid());
write(pfd, pidbuf, strlen(pidbuf));
close(pfd);
return 0;
}
......
......@@ -34,7 +34,7 @@
#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 "/var/run/slothd"
#define PIDFILE "/var/run/slothd.pid"
#define MACADDRLEN 18
#define MAXNUMIFACES 10
#define MAXIFNAMELEN 10
......@@ -77,6 +77,7 @@ typedef struct {
u_char debug;
u_char actterms;
u_char first;
u_char once;
char *servname;
u_short port;
char *ttys[MAXTTYS];
......@@ -84,6 +85,7 @@ typedef struct {
int parse_args(int, char**);
int init_slothd(void);
void do_update(void);
void get_min_tty_idle(void);
void utmp_enum_terms(void);
......
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