Void DaemonInit (void)
{
// LOG: INF ("[ServerMeeting] service initialization. \ n ");
Int pid;
// If it is a parent process, terminate the parent process and the child process continues
If (pid = fork ())
{
Exit (0 );
}
/* Else if (pid <0) // cannot enter
{
Exit (1); // fork failed, quit
}*/
// It is the first sub-process and continues execution in the background
Setsid (); // The first sub-process becomes the new session leader and process leader
// Separate it from the control terminal
If (pid = fork ())
{
Exit (0); // It is the first sub-process and ends the first sub-process.
}
/* Else if (pid <0)
{
Exit (1); // fork failed, quit
}*/
// Close the opened file descriptor, retain the standard input (0), standard output (1), and standard error output (2)
// You can also redirect these three files through dup2: error = open ("/tmp/error", O_WRONLY | O_CREAT, 0600); dup2 (error, 2 ); close (error); // in = open ("/tmp/in", O_RDONLY | O_CREAT, 0600); if (dup2 (in, 0) =-1) perror ("in"); close (in); // out = open ("/tmp/out", O_WRONLY | O_CREAT, 0600); if (dup2 (out, 1) =-1) perror ("out"); close (out );
For (int fd = 3, fdtablesize = getdtablesize (); fd <fdtablesize; fd ++)
{
Close (fd );
}
// Reset the file to create a mask
Umask (0 );
// Signal Processing
Signal (SIGTTOU, SIG_IGN );
Signal (SIGTTIN, SIG_IGN );
Signal (SIGTSTP, SIG_IGN );
Signal (SIGHUP, SIG_IGN );
Signal (SIGTERM, SIG_IGN );
// Set System Parameters
Struct rlimit l = {0, 0 };
L. rlim_cur = 65535;
L. rlim_max = 65535;
Setrlimit (RLIMIT_NOFILE, & l );
}