1、源码分析(daemon.c)

ngx_int_t ngx_daemon(ngx_log_t *log)
{
  int  fd;

  //用fork创建守护进程
  switch (fork()) {
  //fork返回-1创建失败
  case -1:  
    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "fork() failed");
    return NGX_ERROR;
  //子进程返回
  case 0:
    break;
  //父进程返回
  default:
    //父进程退出
    exit(0);
  }

  ngx_pid = ngx_getpid();
  //建立新的会话,然后子进程称为会话组长
  if (setsid() == -1) {
    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "setsid() failed");
    return NGX_ERROR;
  }
  //重设文件创建掩模
  umask(0);

  /*重定向标准输入、输出到/dev/null(传说中的黑洞)*/
  fd = open("/dev/null", O_RDWR);
  if (fd == -1) {
    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno,"open(\"/dev/null\") failed");
    return NGX_ERROR;
  }
  //输入重定向到fd,即从/dev/null输入
  if (dup2(fd, STDIN_FILENO) == -1) {
    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDIN) failed");
    return NGX_ERROR;
  }
  //输出重定向到fd,即所有输出到/dev/null
  if (dup2(fd, STDOUT_FILENO) == -1) {
    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDOUT) failed");
    return NGX_ERROR;
  }
//标准出错不处理
#if 0
  if (dup2(fd, STDERR_FILENO) == -1) {
    ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "dup2(STDERR) failed");
    return NGX_ERROR;
  }
#endif

  if (fd > STDERR_FILENO) {
    if (close(fd) == -1) {
      ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "close() failed");
      return NGX_ERROR;
    }
  }
  return NGX_OK;
}

2、dup2函数


NAME
    #include <unistd.h>
    int dup(int oldfd);
    int dup2(int oldfd, int newfd);
    int dup3(int oldfd, int newfd, int flags);

dup() uses the lowest-numbered unused descriptor for the new descriptor.
dup2() makes newfd be the copy of oldfd, closing newfd first if necessary, but note
   the following:

   *  If oldfd is not a valid file descriptor, then the call fails, and newfd  is  not
      closed.

   *  If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then
      dup2() does nothing, and returns newfd.

   After a successful return from one of these system calls,  the  old  and  new  file
   descriptors may be used interchangeably.  They refer to the same open file descrip-
   tion (see open(2)) and thus share file offset and file status flags;  for  example,
   if  the  file  offset  is modified by using lseek(2) on one of the descriptors, the
   offset is also changed for the other.

   The two descriptors do not share file descriptor flags  (the  close-on-exec  flag).
   The  close-on-exec  flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is
   off.
 dup3() is the same as dup2(), except that:

   *  The caller can force the close-on-exec flag to be set for the new file  descrip-
      tor  by  specifying O_CLOEXEC in flags.  See the description of the same flag in
      open(2) for reasons why this may be useful.

   *  If oldfd equals newfd, then dup3() fails with the error EINVAL.
RETURN VALUE
   On success, these system  calls  return  the  new  descriptor.   On  error,  -1  is
   returned, and errno is set appropriately.

3、daemon函数

NAME
   daemon - run in the background
SYNOPSIS
   #include <unistd.h>
   int daemon(int nochdir, int noclose);
DESCRIPTION
   The  daemon()  function  is for programs wishing to detach themselves from the con-
   trolling terminal and run in the background as system daemons.

   If nochdir is zero, daemon() changes the process’s current working directory to the
   root directory ("/"); otherwise,

   If noclose is zero, daemon() redirects standard input, standard output and standard
   error to /dev/null; otherwise, no changes are made to these file descriptors.
RETURN VALUE
   (This function forks, and if the fork(2) succeeds, the parent  calls  _exit(2),  so
   that further errors are seen by the child only.)  On success daemon() returns zero.
   If an error occurs, daemon() returns -1 and sets errno to any of the errors  speci-
   fied for the fork(2) and setsid(2).
NOTES
   The glibc implementation can also return -1 when /dev/null  exists  but  is  not  a
   character  device  with  the  expected major and minor numbers.  In this case errno
   need not be set.