需要用到的头文件
#include
<
syslog.h
>
#include < fcntl.h >
#include < sys / resource.h >
#include < fcntl.h >
#include < sys / resource.h >
函数框架
void
daemonize(
const
char
*
cmd)
... {
int i, fd0, fd1, fd2;
pid_t pid;
struct rlimit rl; //用于获取系统资源限制信息
struct sigaction sa; //用于改变信号处理函数
/**//*
* Clear file creation mask.
*/
//1. 清除所有文件创建掩码
umask(0);
/**//*
* Get maximum number of file descriptors.
*/
//获得系统允许打开的最大文件符数量
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
err_quit("%s: can't get file limit", cmd);
/**//*
* Become a session leader to lose controlling TTY.
*/
// 2 fork 然后父进程退出
if ((pid = fork()) < 0)
err_quit("%s: can't fork", cmd);
else if (pid != 0) /**//* parent */
exit(0);
//3 成为session leader
setsid();
/**//*
* Ensure future opens won't allocate controlling TTYs.
*/
//忽略SIGHUP信号 目的未知
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGHUP, &sa, NULL) < 0)
err_quit("%s: can't ignore SIGHUP");
if ((pid = fork()) < 0)
err_quit("%s: can't fork", cmd);
else if (pid != 0) /**//* parent */
exit(0);
/**//*
* Change the current working directory to the root so
* we won't prevent file systems from being unmounted.
*/
//4.设置当前工作路径
if (chdir("/") < 0)
err_quit("%s: can't change directory to /");
/**//*
* Close all open file descriptors.
*/
//5.关闭所有文件描述符
if (rl.rlim_max == RLIM_INFINITY)
rl.rlim_max = 1024;
for (i = 0; i < rl.rlim_max; i++)
close(i);
/**//*
* Attach file descriptors 0, 1, and 2 to /dev/null.
*/
// 6.重定向三个标准流
fd0 = open("/dev/null", O_RDWR);
fd1 = dup(0);
fd2 = dup(0);
/**//*
* Initialize the log file.
*/
openlog(cmd, LOG_CONS, LOG_DAEMON);
if (fd0 != 0 || fd1 != 1 || fd2 != 2) ...{
syslog(LOG_ERR, "unexpected file descriptors %d %d %d",
fd0, fd1, fd2);
exit(1);
}
}
... {
int i, fd0, fd1, fd2;
pid_t pid;
struct rlimit rl; //用于获取系统资源限制信息
struct sigaction sa; //用于改变信号处理函数
/**//*
* Clear file creation mask.
*/
//1. 清除所有文件创建掩码
umask(0);
/**//*
* Get maximum number of file descriptors.
*/
//获得系统允许打开的最大文件符数量
if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
err_quit("%s: can't get file limit", cmd);
/**//*
* Become a session leader to lose controlling TTY.
*/
// 2 fork 然后父进程退出
if ((pid = fork()) < 0)
err_quit("%s: can't fork", cmd);
else if (pid != 0) /**//* parent */
exit(0);
//3 成为session leader
setsid();
/**//*
* Ensure future opens won't allocate controlling TTYs.
*/
//忽略SIGHUP信号 目的未知
sa.sa_handler = SIG_IGN;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (sigaction(SIGHUP, &sa, NULL) < 0)
err_quit("%s: can't ignore SIGHUP");
if ((pid = fork()) < 0)
err_quit("%s: can't fork", cmd);
else if (pid != 0) /**//* parent */
exit(0);
/**//*
* Change the current working directory to the root so
* we won't prevent file systems from being unmounted.
*/
//4.设置当前工作路径
if (chdir("/") < 0)
err_quit("%s: can't change directory to /");
/**//*
* Close all open file descriptors.
*/
//5.关闭所有文件描述符
if (rl.rlim_max == RLIM_INFINITY)
rl.rlim_max = 1024;
for (i = 0; i < rl.rlim_max; i++)
close(i);
/**//*
* Attach file descriptors 0, 1, and 2 to /dev/null.
*/
// 6.重定向三个标准流
fd0 = open("/dev/null", O_RDWR);
fd1 = dup(0);
fd2 = dup(0);
/**//*
* Initialize the log file.
*/
openlog(cmd, LOG_CONS, LOG_DAEMON);
if (fd0 != 0 || fd1 != 1 || fd2 != 2) ...{
syslog(LOG_ERR, "unexpected file descriptors %d %d %d",
fd0, fd1, fd2);
exit(1);
}
}