Commit 6b73f9fa authored by 's avatar

-G or --daemon will cause abw to run as daemon


git-svn-id: file:///home/svn/mapi/trunk@1227 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 0f8230ef
......@@ -57,6 +57,7 @@ double round(double x);
flow_t *flow[MAX_FLOWS];
int debug=0;
int daemonize=0;
extern tracked_protocol_t tracked_protocols[];
extern protocol_t protocols[];
......@@ -172,6 +173,15 @@ int main(int argc, char *argv[])
if (debug)
print_conf(&configuration);
if (daemonize) {
printf("Switching to daemon\n");
if (continue_as_daemon()<0) {
fprintf(stderr, "%s: continue_as_daemon() failed\n", __func__);
return -1;
}
}
printf("Continuing as daemon\n");
/*
* Create RRD files
*/
......
......@@ -298,3 +298,53 @@ int timestamp_diff(struct timeval *tv_start, struct timeval *tv_stop) {
return usec;
} /* timestamp_diff() */
int continue_as_daemon(void) {
int nullfd;
printf("Closing stdin, stdout, stderr and going into background.\n");
switch (fork()) {
case 0:
break;
case -1:
fprintf(stderr, "%s: fork() failed, %d - %s\n", __func__, errno,
strerror(errno));
return -1;
break;
default:
_exit(0);
break;
}
if (setsid() == -1) {
fprintf(stderr, "%s: setsid() failed, %d - %s\n", __func__, errno,
strerror(errno));
return -1;
}
setpgrp();
switch (fork()) {
case 0:
break;
case -1:
fprintf(stderr, "%s: fork() failed, %d - %s\n", __func__, errno,
strerror(errno));
return -1;
break;
default:
_exit(0);
break;
}
chdir("/");
nullfd = open("/dev/null", O_RDONLY);
dup2(nullfd, STDIN_FILENO);
close(nullfd);
nullfd = open("/dev/null", O_WRONLY);
dup2(nullfd, STDOUT_FILENO);
dup2(nullfd, STDERR_FILENO);
close(nullfd);
return 0;
}
......@@ -97,4 +97,6 @@ int get_local_hostname(char **hostname);
int timestamp_diff(struct timeval *tv_start, struct timeval *tv_stop);
int continue_as_daemon(void);
#endif
......@@ -49,6 +49,7 @@
#include "abw_conf.h"
extern int debug;
extern int daemonize;
void usage() {
char *progname="abw";
......@@ -60,6 +61,7 @@ void usage() {
fprintf(stderr, " -h print this help message\n");
fprintf(stderr, " -D or --debug[=n] set debug level to n\n");
fprintf(stderr, " (n can be specified with --debug only)\n");
fprintf(stderr, " -G or --daemon run as daemon\n");
fprintf(stderr, " -q or --no_stdout do not print results on stdout\n");
fprintf(stderr, " --stdout_simple print results as numbers only\n");
fprintf(stderr, " --no_measure do not start measurements\n");
......@@ -123,6 +125,10 @@ int read_command_line(int argc, char *argv[], configuration_t *configuration) {
else
debug=1;
break;
case 'G';
case LO_DAEMON:
daemonize=1;
break;
case LO_NO_MEASURE:
configuration->global->no_measure=1;
break;
......
......@@ -49,7 +49,7 @@
* as not followed by ":" and an option with an optional argument as
* followed by "::".
*/
#define ARGS ":n:d:m:r:w:f:i:l:D::hq"
#define ARGS ":n:d:m:r:w:f:i:l:D::hqG"
/* Long option codes */
enum {
......@@ -58,7 +58,7 @@ enum {
LO_SAU_MODE, LO_SAU_THRESHOLD, LO_PAYLOAD_STRING, LO_INTERVAL,
LO_PROTOCOLS,
LO_CONF_FILENAME, LO_NO_MEASURE, LO_NO_STDOUT, LO_STDOUT_SIMPLE,
LO_DEBUG };
LO_DEBUG, LO_DAEMON };
/* Long option names and argument types */
static const struct option long_options[] = {
......@@ -78,6 +78,7 @@ static const struct option long_options[] = {
{ "no_stdout", no_argument, 0, LO_NO_STDOUT },
{ "stdout_simple", no_argument, 0, LO_STDOUT_SIMPLE },
{ "debug", optional_argument, 0, LO_DEBUG },
{ "daemon", no_argument, 0, LO_DAEMON },
{ 0, 0, 0, 0 }
};
......
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