Cover V11, I02
feb2002.tar

About Listing 1

1. Use fork() to create an exact copy of the parent (line 14) to disconnect from the controlling terminal and be sure it's not executing as a process group leader.

2. In the child process, use setsid() to create a new session (line 40), which automatically makes the child process the leader of the new session.

3. Close STDIN, STDOUT, and STDERR to be sure the daemon won't output anything to the screen or try to read anything from the console (lines 49, 50, and 51). Instead, the program will output information to some files.

4. Change the working directory to / (line 58) to prevent blocking any device that might need to be unmounted. For example, if you have /home on a separate partition and your current directory is /home/myself when you start the program, you won't be able to unmount /home if you skip the chdir("/").

5. Start the infinite loop that will run do_quota_stuff() every two seconds (lines 64 through 74).

We now have a full-featured daemon process that repeats every two seconds until you explicitly tell it to stop by killing it. However, the real functionality is implemented in the do_quota_stuff() function, shown in Listing 2.