bjwf:
老玉米来看看,偶那个用文件来保证daemon只起一个实例的办法是标准的unix做法.
[阅读: 378] 2005-04-14 07:09:43
some rules from <Advanced Programming in the UNIX Environment> and <Internetworing With TCP/IP: Vol3>
1. close inherited file descriptors.
for(i = getdtablesize() -1; i>=0; i--)
close(i);
2. separate with controy terminal.
fd = open("/dev/tty", O_RDWR);
ioctl(fd, TIOCNOTTY, 0);
3. change to a security directory
chdir("/");
4. set umask
umask(027);
5. set a new session , and become session leader
setsid();
6. set process group
setpgrp(0, getpid());
7. open 3 standard file descriptors to /dev/null
fd = open("/dev/null", O_RDWR); /* stdin */
dup(fd); /*stdout*/
dup(fd); /*stderr*/
8. keep only one copy of daemon
use flock(), and the file lock should be keep in daemon's life.
9. record daemon's pid
write pid to lock file is a good idea.
10. ignore all unneeded signals