diff --git a/src/daemonize.c b/src/daemonize.c index d135fb9..58ad5bc 100644 --- a/src/daemonize.c +++ b/src/daemonize.c @@ -48,7 +48,7 @@ void close_lock_file() remove(LOCK_FILE); } -void daemonize(){ +void kamathvasudev_daemonize(){ int pid; int i; char str[10]; @@ -85,3 +85,34 @@ void daemonize(){ atexit(close_lock_file); } + +void daemonize() +{ + pid_t pid; + char str[10]; + + /* + * use unistd.h function + * - do change to root directory + * - close std streams + */ + daemon(1, 0); + + /* + * Double fork method + */ + pid = fork(); + if(pid < 0) exit(1); + if(pid > 0) exit(0); + + umask(022); + + lfp = open(LOCK_FILE,O_RDWR|O_CREAT,0640); + if(lfp < 0) { log_signal_message("daemond is allready running !\n");exit(1); } + if(lockf(lfp,F_TLOCK,0) < 0) { log_signal_message("daemond is allready running\n");exit(1); } + + sprintf(str,"%d\n",getpid()); + write(lfp,str,strlen(str)); + + atexit(close_lock_file); +} diff --git a/tmp/daemond.log b/tmp/daemond.log index e38c268..93d2d45 100644 --- a/tmp/daemond.log +++ b/tmp/daemond.log @@ -1 +1,2 @@ process 11096 signal 15 trapped +process 16565 signal 15 trapped