add: if (pgid_file!=0){ if ((pid_stream=fopen(pgid_file, "r"))!=NULL){ fscanf(pid_stream, "%d", &p); fclose(pid_stream); if (p==-1){ LOG(L_CRIT, "pgid file %s exists, but doesn't contain a valid" " pgid number\n", pgid_file); goto error; } } pid=getpgid(0); if ((pid_stream=fopen(pgid_file, "w"))==NULL){ LOG(L_WARN, "unable to create pgid file %s: %s\n", pgid_file, strerror(errno)); goto error; }else{ fprintf(pid_stream, "%i\n", (int)pid); fclose(pid_stream); } }
context: int daemonize(char*  name){	FILE *pid_stream;	pid_t pid;	int r, p;	p=-1;	if (chroot_dir&&(chroot(chroot_dir)<0)){		LOG(L_CRIT, "Cannot chroot to %s: %s\n", chroot_dir, strerror(errno));		goto error;	}		if (chdir(working_dir)<0){		LOG(L_CRIT,"cannot chdir to %s: %s\n", working_dir, strerror(errno));		goto error;	}	/* fork to become!= group leader*/	if ((pid=fork())<0){		LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));		goto error;	}else if (pid!=0){		/* parent process => exit*/		exit(0);	}	/* become session leader to drop the ctrl. terminal */	if (setsid()<0){		LOG(L_WARN, "setsid failed: %s\n",strerror(errno));	}else{		own_pgid=1;/* we have our own process group */	}	/* fork again to drop group  leadership */	if ((pid=fork())<0){		LOG(L_CRIT, "Cannot  fork:%s\n", strerror(errno));		goto error;	}else if (pid!=0){		/*parent process => exit */		exit(0);	}	/* added by noh: create a pid file for the main process */	if (pid_file!=0){				if ((pid_stream=fopen(pid_file, "r"))!=NULL){			fscanf(pid_stream, "%d", &p);			fclose(pid_stream);			if (p==-1){				LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"					" pid number\n", pid_file);				goto error;			}			if (kill((pid_t)p, 0)==0 || errno==EPERM){				LOG(L_CRIT, "running process found in the pid file %s\n",					pid_file);				goto error;			}else{				LOG(L_WARN, "pid file contains old pid, replacing pid\n");			}		}		pid=getpid();		if ((pid_stream=fopen(pid_file, "w"))==NULL){			LOG(L_WARN, "unable to create pid file %s: %s\n", 				pid_file, strerror(errno));			goto error;		}else{			fprintf(pid_stream, "%i\n", (int)pid);			fclose(pid_stream);		}	}		/* try to replace stdin, stdout & stderr with /dev/null */	if (freopen("/dev/null", "r", stdin)==0){		LOG(L_ERR, "unable to replace stdin with /dev/null: %s\n",				strerror(errno));		/* continue, leave it open */	};	if (freopen("/dev/null", "w", stdout)==0){		LOG(L_ERR, "unable to replace stdout with /dev/null: %s\n",				strerror(errno));		/* continue, leave it open */	};	/* close stderr only if log_stderr=0 */	if ((!log_stderr) &&(freopen("/dev/null", "w", stderr)==0)){		LOG(L_ERR, "unable to replace stderr with /dev/null: %s\n",				strerror(errno));		/* continue, leave it open */	};		/* close any open file descriptors */	closelog();	for (r=3;r<MAX_FD; r++){			close(r);	}		if (log_stderr==0)		openlog(name, LOG_PID|LOG_CONS, log_facility);		/* LOG_CONS, LOG_PERRROR ? */	return  0;error:	return -1;}