I guess that either technique presented in those links is good enough, but I have to say that there is some room for improvement. Both of these techniques are relying on the parent to not make strange mistakes prior to invoking the daemon. We can do a better job of protecting ourself from a insane or hostile parent. By using two forks separated by a setsid() we can create a process that is guaranteed to not have a controlling terminal
or even have the ability to accidentally acquire one. I kinda like the idea of closing all possible fd's, but using gettablesize sabotages the idea. gettablesize is basicly a getrlimit(RLIMIT_NOFILE) and is returning the soft limit. You can open a file and then set the soft limit lower...the file won't close or anything. And if the total number of open file is low, you can still open new files. Using sysconf() can avoid that.
Here is a link that gets all of this right
1.7 How do I get my program to act like a daemon? This link is from the Usenet comp.unix.programmer FAQ. From the same FAQ, also see
Daemon utility functions. In fact, see the entire FAQ. The whole thing is excellent, despite being a little dated.