From 2bdce36ecbae3ff709a8c698fb5264d358444246 Mon Sep 17 00:00:00 2001 From: Alexander Wenzel Date: Fri, 4 Nov 2011 23:43:28 +0100 Subject: Added daemonise and signal handlers to dlt-system. --- src/system/dlt-system.c | 172 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 151 insertions(+), 21 deletions(-) (limited to 'src/system/dlt-system.c') diff --git a/src/system/dlt-system.c b/src/system/dlt-system.c index 3189b61..fc45e1e 100755 --- a/src/system/dlt-system.c +++ b/src/system/dlt-system.c @@ -78,6 +78,16 @@ #include #include +#include +#include +#include +#include + +#include +#include +#include +#include + #include "dlt_common.h" #include "dlt_user.h" @@ -93,12 +103,16 @@ DLT_DECLARE_CONTEXT(processesContext); DLT_DECLARE_CONTEXT(filetransferContext); DltContext logFileContext[DLT_SYSTEM_LOG_FILE_MAX]; +DltSystemOptions options; +DltSystemRuntime runtime; + void dlt_system_init_options(DltSystemOptions *options) { int num; strncpy(options->ConfigurationFile,DEFAULT_CONFIGURATION_FILE,sizeof(options->ConfigurationFile)); strncpy(options->ApplicationId,DEFAULT_APPLICATION_ID,sizeof(options->ApplicationId)); + options->daemonise = 0; /* Syslog Adapter */ options->SyslogEnable = 0; @@ -141,10 +155,15 @@ int dlt_system_parse_options(DltSystemOptions *options,int argc, char* argv[]) int opt; char version[255]; - while ((opt = getopt(argc, argv, "c:h")) != -1) + while ((opt = getopt(argc, argv, "c:hd")) != -1) { switch (opt) { + case 'd': + { + options->daemonise = 1; + break; + } case 'c': { strncpy(options->ConfigurationFile,optarg,sizeof(options->ConfigurationFile)); @@ -158,6 +177,7 @@ int dlt_system_parse_options(DltSystemOptions *options,int argc, char* argv[]) printf("System information manager and forwarder to DLT daemon.\n"); printf("%s \n", version); printf("Options:\n"); + printf("-d - Daemonize\n"); printf("-c filename - Set configuration file (default: /etc/dlt-system.conf)\n"); printf("-h - This help\n"); return -1; @@ -354,6 +374,124 @@ int dlt_system_parse_configuration(DltSystemOptions *options) return 0; } +void dlt_system_daemonize() +{ + int i,lfp,bytes_written,ret; + + /* Daemonize */ + i=fork(); + if (i<0) + { + exit(-1); /* fork error */ + } + + if (i>0) + { + exit(0); /* parent exits */ + } + /* child (daemon) continues */ + + /* Process independency */ + + /* obtain a new process group */ + if (setsid()==-1) + { + exit(-1); /* fork error */ + } + + /* Close descriptors */ + for (i=getdtablesize();i>=0;--i) + { + close(i); /* close all descriptors */ + } + + /* Open standard descriptors stdin, stdout, stderr */ + i=open("/dev/null",O_RDWR); /* open stdin */ + ret=dup(i); /* stdout */ + ret=dup(i); /* stderr */ + + /* Set umask */ + //umask(DLT_DAEMON_UMASK); + + /* Change to known directory */ + //ret=chdir(DLT_USER_DIR); + + /* Ensure single copy of daemon; + run only one instance at a time */ +#if 0 + lfp=open(DLT_DAEMON_LOCK_FILE,O_RDWR|O_CREAT,DLT_DAEMON_LOCK_FILE_PERM); + if (lfp<0) + { + dlt_log(LOG_CRIT, "can't open lock file, exiting DLT daemon\n"); + exit(-1); /* can not open */ + } + if (lockf(lfp,F_TLOCK,0)<0) + { + dlt_log(LOG_CRIT, "can't lock lock file, exiting DLT daemon\n"); + exit(-1); /* can not lock */ + } + /* only first instance continues */ + + sprintf(str,"%d\n",getpid()); + bytes_written=write(lfp,str,strlen(str)); /* record pid to lockfile */ + +#endif + /* Catch signals */ + signal(SIGCHLD,SIG_IGN); /* ignore child */ + signal(SIGTSTP,SIG_IGN); /* ignore tty signals */ + signal(SIGTTOU,SIG_IGN); + signal(SIGTTIN,SIG_IGN); + +} /* dlt_system_daemonize() */ + +void dlt_system_cleanup() +{ + int num; + + if(options.SyslogEnable) + DLT_UNREGISTER_CONTEXT(syslogContext); + + if(options.FiletransferEnable) + DLT_UNREGISTER_CONTEXT(filetransferContext); + + if(options.LogFileEnable) { + for(num=0;num