Is changing parent process necessary when daemoniz

2019-09-08 14:43发布

问题:

I am reading about daemonizing a process at https://en.wikipedia.org/wiki/Daemon_%28computing%29#Creation

In a strictly technical sense, a Unix-like system process is a daemon when its parent process terminates and the daemon is assigned the init process (process number 1) as its parent process and has no controlling terminal. However, more commonly, a daemon may be any background process, whether a child of the init process or not.

On a Unix-like system, the common method for a process to become a daemon, when the process is started from the command line or from a startup script such as an init script or a SystemStarter script, involves:

  1. Dissociating from the controlling tty
  2. Becoming a session leader
  3. Becoming a process group leader
  4. Executing as a background task by forking and exiting (once or twice). This is required sometimes for the process to become a session leader. It also allows the parent process to continue its normal execution.
  5. Setting the root directory (/) as the current working directory so that the process does not keep any directory in use that may be on a mounted file system (allowing it to be unmounted).
  6. Changing the umask to 0 to allow open(), creat(), and other operating system calls to provide their own permission masks and not to depend on the umask of the caller
  7. Closing all inherited files at the time of execution that are left open by the parent process, including file descriptors 0, 1 and 2 for the standard streams (stdin, stdout and stderr). Required files will be opened later.
  8. Using a logfile, the console, or /dev/null as stdin, stdout, and stderr

If the process is started by a super-server daemon, such as inetd, launchd, or systemd, the super-server daemon will perform those functions for the process[5][6][7] (except for old-style daemons not converted to run under systemd and specified as Type=forking[7] and "multi-threaded" datagram servers under inetd[5]).

  1. Is there a step there that changes the parent process of a process to be daemonized? It seems to me none of the steps does that?

    Is changing parent process necessary when daemonize a process?

  2. After changing the parent process of a process (a process not necessarily to be daemonized), can the process be associated to the controlling tty of the new parent process? (The purpose of the question is to see whether "keeping a process disassociated from the the controlling tty of the new parent process" is a necessary condition of "changing the parent process of the process".)

See my related question https://unix.stackexchange.com/questions/266565/daemonize-a-process-in-shell

Thanks.

回答1:

The parent of a Unix process can't be changed by the process itself. The typical method of creating a daemon involves a fork call (which creates the process that will become the daemon). The initial process then exits, and the newly-orphaned child process will be inherited by the init process which becomes it's new parent. That's handled in step 4. The only thing init will do is wait for all it's children to exit. init doesn't have a controlling TTY, so once inherited by init the daemon can't become associated with a controlling TTY anymore. The main reason to become disassociated is to prevent signals generated from the TTY (hangups and control-C's etc.) from getting to the daemon.

There are two ways daemons are usually run:

  1. From a shell script. The script runs the daemon's executable with the & operator at the end of the command to put the daemon into the background, possibly with I/O redirection to set the daemon's stdin, stdout and/or stderr, and then exits leaving the daemon without a parent. Running an executable from the shell involves the shell doing a fork followed by an exec in the child process of the executable to be run.

  2. The daemon program has an option to daemonize itself. When run with that option it does a fork followed in the child process by an exec of itself with an appropriate set of arguments. The parent will normally exit after the fork since the work it's been asked to do is done. If it doesn't, the child process needs an extra fork to give it a parent that can exit. NB: this is why so many programs that normally run as daemons can be run directly without becoming a daemon, the "become a daemon" option causes the child process to close stdin/stdout/stderr and then just exec it's own executable without the "become a daemon" option.



回答2:

I would suggest to use daemon(3). See also credentials(7)

Your list does not mention explicitly setsid(2).

MUSL libc has a legacy/daemon.c which forks twice and do setsid



标签: linux daemon