use unistd.h daemon function

This commit is contained in:
2020-07-15 16:09:29 +02:00
parent 92cb9bb3fa
commit 75ca7ce46e
2 changed files with 33 additions and 1 deletions
+32 -1
View File
@@ -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);
}
+1
View File
@@ -1 +1,2 @@
process 11096 signal 15 trapped
process 16565 signal 15 trapped