In general when you write a
daemon process you...
Become a session leader - fork()
Become a process-group leader - setsid()
Dissociate from controlling terminal - setsid() normally does this too.
chdir to '/' - chdir()
Set file creation mask to 0 - umask(0)
You still need to close unneeded descriptors. Assuming you
haven't opened anything, you can simply do...
for (i = 0; i < 10; i++)
close(i);
As Perderabo says, "The close call will fail if the file is not open,
so you just ignore that error."
At this point, you have initalized your
daemon process.
A great reference book with examples of this is...
Advanced Programming in the UNIX Environment
by W. Richard Stevens