|
|
|
@ -10,7 +10,7 @@ |
|
|
|
|
|
|
|
#define RUNNING_DIR "/tmp"
|
|
|
|
#define LOCK_FILE "/tmp/daemond.lock"
|
|
|
|
#define LOG_FILE "daemond.log" |
|
|
|
#define LOG_FILE "/tmp/daemond.log" |
|
|
|
|
|
|
|
/*
|
|
|
|
* daemon.c
|
|
|
|
@ -33,35 +33,48 @@ |
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void log_signal_message(char *filename,char *message){
|
|
|
|
FILE *logfile;
|
|
|
|
logfile = fopen(filename,"a");
|
|
|
|
if(!logfile) return;
|
|
|
|
fprintf(logfile,"%s",message);
|
|
|
|
fclose(logfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
int lfp;
|
|
|
|
void close_lock_file()
|
|
|
|
{
|
|
|
|
close(lfp);
|
|
|
|
remove(LOCK_FILE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void daemonize(){
|
|
|
|
int i,lfp;
|
|
|
|
int pid;
|
|
|
|
int i;
|
|
|
|
char str[10];
|
|
|
|
|
|
|
|
if(getppid() == 1)
|
|
|
|
return;
|
|
|
|
pid = getppid(); |
|
|
|
if(pid == 1) return; |
|
|
|
|
|
|
|
i = fork();
|
|
|
|
pid = fork();
|
|
|
|
if(pid < 0) exit(1);
|
|
|
|
if(pid > 0) exit(0);
|
|
|
|
|
|
|
|
if(i < 0)
|
|
|
|
exit(1);
|
|
|
|
if(i > 0)
|
|
|
|
exit(0);
|
|
|
|
setsid();
|
|
|
|
|
|
|
|
for(i = getdtablesize(); i >= 0; --i)
|
|
|
|
close(i);
|
|
|
|
for(i = getdtablesize(); i >= 0; --i) close(i);
|
|
|
|
|
|
|
|
i = open("/dev/null",O_RDWR); |
|
|
|
dup(i); |
|
|
|
dup(i); |
|
|
|
lfp = open("/dev/null",O_RDWR); |
|
|
|
dup(lfp); |
|
|
|
dup(lfp); |
|
|
|
umask(022);
|
|
|
|
|
|
|
|
lfp = open(LOCK_FILE,O_RDWR|O_CREAT,0640);
|
|
|
|
if(lfp < 0)
|
|
|
|
exit(1);
|
|
|
|
if(lockf(lfp,F_TLOCK,0) < 0)
|
|
|
|
exit(1);
|
|
|
|
if(lfp < 0) exit(1);
|
|
|
|
if(lockf(lfp,F_TLOCK,0) < 0) exit(1);
|
|
|
|
|
|
|
|
sprintf(str,"%d\n",getpid());
|
|
|
|
write(lfp,str,strlen(str));
|
|
|
|
}
|
|
|
|
|
|
|
|
atexit(close_lock_file);
|
|
|
|
}
|