The most important feature of a daemon is that it runs in the background. At this point, the TSR of the resident memory program under DOS is similar. Second, the daemon must be isolated from the environment before running. These environments include unclosed file descriptors, control terminals, sessions and process groups, working directories, and file creation masks. These environments are generally inherited by the daemon from the parent process (especially the shell) that executes the daemon. Finally, the daemon startup method has its own special features. It can be started from the startup script/etc/rc. d when the Linux system is started. It can be started by the job planning process crond and executed by the user terminal (usually shell.
In short, apart from these special features, the daemon process is basically no different from the common process. Therefore, writing a daemon actually transforms a common process into a daemon according to the features of the preceding daemon.
In four steps:
1) create a sub-process out of the parent process,
To avoid pending the control terminal, place the daemon in the background for execution. The method is to call fork in the process to terminate the parent process and run daemon in the background of the child process.
2) log on to the session and process group from the control terminal:
A process belongs to a process group. The process group number (GID) is the process ID (PID) of the process leader ). A logon session can contain multiple Process Groups. These process groups share a control terminal. This control terminal is usually the login terminal that creates a process.
Control terminal, logon sessions and process groups are generally inherited from the parent process. Our goal is to get rid of them so that they are not affected. The method is to call setsid () on the basis of to make the process a session leader:
3) prohibit the process from re-opening the control terminal:
Now, the process has become the no-end session leader. But it can re-apply to open a control terminal. You can disable the process from re-opening the control terminal by making the process no longer the session leader:
4) Close the opened file descriptor:
A process inherits the opened file descriptor from the parent process that created it. If you do not close it, system resources will be wasted, and the file system where the process is located will not be able to be detached and unexpected errors will occur.
Static void deamonize ()
{
Int FD;
Int PID;
If (pid = fork ()){
Exit (0);/* parent process, exit it */
} Else if (PID <0 ){
Spd_log (log_error, "error when fork \ n ");
Exit (0);/* Error */
} Else {
If (setsid () <0 ){
Spd_log (log_error, "error when change session ID ");
Exit (0 );
}
}
/* Child proc, for now, we are session leader and process group leader */
If (pid = fork ()){
Exit (0);/* Kill child */
} Else if (PID <0 ){
Spd_log (log_error, "error when fork \ n ");
Exit (0 );
}
/* Child proc, we are no longer the session leader and process group leader */
/* Ignore FD leak */
FD = open ("/dev/null", o_rdonly );
If (FD! = 0 ){
Dup2 (FD, 0 );
Close (FD );
}
FD = open ("/dev/null", o_wronly );
If (FD! = 1 ){
Dup2 (FD, 1 );
Close (FD );
}
FD = open ("/dev/null", o_wronly );
If (FD! = 2 ){
Dup2 (FD, 2 );
Close (FD );
}
}