Some basic rules must be followed when writing daemon to prevent unnecessary interactions. The following describes these rules first, and then provides a function daemonize compiled according to these rules.
(1)The first thing to do is to call umask to set the block word created in file mode to 0.Creating a blocked word in the inherited file mode may reject certain permissions. For example, if the daemon wants to create a group of readable and written files, and the inherited file mode creates a blocked word, the two permissions may be blocked, therefore, the Group read and write requests cannot work.
(2)Call fork and then exit the parent process ).First, if the daemon is started as a simple shell command, if the parent process is terminated, the shell determines that the command has been executed (no control terminal is available). Second, the child process inherits the process group ID of the parent process, but with a new process ID, this ensures that the sub-process is not the leader process of a process group. This is a prerequisite for the setsid call that will be performed below.
(3)Call setsid to create a new session.Run the three operations listed in http://www.cnblogs.com/nufangrensheng/p/3513400.htmlto make the call process: (a) the first process of the new session, (B) the leader process of a new process group, and (c) no control terminal.
In System V-based systems, it is recommended that you call fork again and terminate the parent process. The second child process continues to run as a daemon. This ensures that the daemon is not the first process of the session, so according to System V rules (see the http://www.cnblogs.com/nufangrensheng/p/3513443.html) can prevent it from getting control terminal. Another way to avoid obtaining control terminals is to specify O_NOCTTY whenever a terminal device is opened.
(4)Change the current working directory to the root directory.The current working directory inherited from the parent process may be in a mounted file system. Because the daemon usually exists until the system is started again, if the current working directory of the daemon is in a mounted file system, the file system cannot be detached. This is inconsistent with the original intention of mounting a file system.
In addition, some daemon may change the current working directory to a specified location to do their work, for example, the working directory of a row-Type printer is often changed to the spool directory.
(5)Disable unnecessary file descriptors.This makes the daemon no longer hold certain file descriptors inherited from its parent process (the parent process may be a shell process or another process ). You can use the delimiter (http://www.cnblogs.com/nufangrensheng/p/3509262.html) to determine the maximum file descriptor value and close all descriptors until that value.
(6)Some daemon enables/dev/null to have file descriptors 0, 1, and 2. In this way, any library routine that tries to read standard input, write standard output, and standard error will not produce any effect.Because the daemon is not associated with the terminal device, the output of the daemon cannot be displayed on the terminal device, and the input cannot be accepted from the interactive user. Even if the daemon is started from an interactive session, because the daemon runs in the background, the termination of the logon session does not affect the daemon process. If other users log on to the same terminal device, we will not see the output of the daemon on. Users cannot expect that their input on the terminal will be read by the daemon.
Instance
The program listing 13-1 is a function that can be called by a program that wants to initialize as a daemon process.
Program list 13-1 initialize a daemon
#include <syslog.h><fcntl.h><sys/resource.h> * (getrlimit(RLIMIT_NOFILE, &rl) < ((pid = fork()) < (pid != ) =&= (sigaction(SIGHUP, &sa, NULL) < ((pid = fork()) < ( pid != ) (chdir() < (rl.rlim_max == (i = ; i < rl.rlim_max; i++= open(= dup(= dup((fd0 != || fd1 != || fd2 !=
This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.