bjwf:
老玉米来看看,偶那个用文件来保证daemon只起一个实例的办法是标准的unix做法.
[阅读: 299] 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
试图在互联网上划出国界的举动是愚蠢的!!!
把人当成猪羊圈养,草料永远都不够!把人当人待,人有无限的创造力! “人口减少一半,人们两倍富有”的观念为“一种非常糟糕的自甘堕落—这背后的潜台词是,似乎中国人是封闭围栏中的动物,对周围那些自己赖以生存的资源,除了咀嚼,别无办法。”
我的像册:
http://picasaweb.google.com/bjwf2000/
我的主页:
http://bjwf2000.googlepages.com/