daemon - turns other processes into daemons
Daemon turns other processes into daemons. There are many tasks that need tobe performed to correctly set up a daemon process. This can be tedious.Daemon performs these tasks for other processes. This is useful for writingdaemons in languages other thanC,C++ orPerl (e.g./bin/sh
,Java).
If you want to write daemons in languages that can link againstC functions(e.g.C,C++), seelibslack which contains the core functionality ofdaemon.
Daemon turns other processes into daemons. There are many tasks that need tobe performed to correctly set up a daemon process. This can be tedious.Daemon performs these tasks for other processes.
The preparatory tasks thatdaemon performs for other processes are:
First revoke any setuid or setgid privileges thatdaemon may have beeninstalled with (by system administrators who laugh in the face of danger).
Process command line options.
Change the root directory if the--chroot
option was supplied.
Change the processuid
andgid
if the--user
option was supplied. Onlyroot can use this option. Note that theuid
ofdaemon itself is changed,rather than just changing theuid
of the client process.
Read the system configuration file(s) (/etc/daemon.conf
and/etc/daemon.conf.d/*
by default, or specified by the--config
option)unless the--noconfig
option was supplied. Then read the user's personalconfiguration file(s) (~/.daemonrc
and~/.daemonrc.d/*
), if any. Note:The root directory and the user must be set before access to theconfiguration file(s) can be attempted so neither--chroot
nor--user
options may appear in the configuration file.
OnBSD systems (exceptmacOS), the system configuration file(s) are/usr/local/etc/daemon.conf
and/usr/local/etc/daemon.conf.d/*
bydefault.
OnmacOS, when installed viamacports, the system configuration file(s)are/opt/local/etc/daemon.conf
and/opt/local/etc/daemon.conf.d/*
.
Disable core file generation to prevent leaking potentially sensitiveinformation in daemons that are run byroot (unless the--core
optionwas supplied).
Become a daemon process:
Ifdaemon was not invoked byinit(8) (i.e. pid 1) orinetd(8):
IgnoreSIGHUP
signals in case the current process session leaderterminates while attached to a controlling terminal, causing us toreceive aSIGHUP
signal before we start our own process session below.This can happen whendaemon was invoked interactively via the shellbuiltinexec
. When this initial process terminates below, the terminalemulator that invoked the shell also terminates, sodaemon needs toprotect itself from that.
Background the process to lose process group leadership.
Start a new process session.
Background the process again to lose process session leadership. UnderSVR4 this prevents the process from ever gaining a controllingterminal. This is only necessary underSVR4, but is always done forsimplicity. Note that ignoringSIGHUP
signals earlier means thatwhen the newly created process session leader terminates, then even ifit has a controlling terminal open, the newly backgrounded processwon't receive the correspondingSIGHUP
signal that is sent to allprocesses in the process session's foreground process group, becauseit inherited signal dispositions from the initial process.
Change the current directory to the root directory so as not to hamperumounts.
Clear theumask to enable explicit file creation modes.
Close all open file descriptors. Ifdaemon was invoked byinetd(8),stdin
,stdout
andstderr
are left open, because they are open to asocket.
Openstdin
,stdout
andstderr
to/dev/null
, in case somethingrequires them to be open. Of course, this is not done ifdaemon wasinvoked byinetd(8).
If the--name
option was supplied, create and lock a file containing theprocess id of thedaemon process. The presence of this locked fileprevents two instances of a daemon with the same name from running atthe same time. The default location of the pidfile is/var/run
forroot (/etc
onSolaris,/opt/local/var/run
onmacOS wheninstalled viamacports), and/tmp
for normal users. If the--pidfiles
option was supplied, its argument specifies the directoryin which the pidfile will be placed. If the--pidfile
option wassupplied, its argument specifies the name of the pidfile and thedirectory in which it will be placed.
If the--umask
option was supplied, set theumask to its argument, whichmust be a valid three-digit octal mode. Otherwise, set theumask to022
,to prevent accidentally creating group- or world-writable files.
Set the current directory if the--chdir
option was supplied.
Spawn the client command and wait for it to terminate. The client commandcan be specified as command line arguments, or as the argument of the--command
option. If both the--command
option and command linearguments are present, the client command is the result of appending thecommand line arguments to the argument of the--command
option.
If the--output
,--stdout
and/or--stderr
option were supplied, the client'sstandard output and/or standard error are captured bydaemon, and sent to therespectivesyslog destinations.
When the client terminates,daemon respawns it if the--respawn
optionwas supplied. If the client ran for less than300
seconds (or the value ofthe--acceptable
option), thendaemon sees this as a failure. It willattempt to restart the client up to five times (or the value of the--attempts
option), before waiting for300
seconds (or the value of the--delay
option). This gives the system administrator the chance to correctwhatever is preventing the client from running successfully withoutoverloading system resources. If the--limit
option was supplied,daemonterminates after the specified number of respawn attempt bursts. The defaultis zero, which means never give up, never surrender.
When the client terminates, and the--respawn
option wasn't supplied,daemon terminates as well.
If the--foreground
option was supplied, the client process is run as aforeground process, and is not turned into a daemon at all. Ifdaemon isconnected to a terminal, then the client process will also be connected toit. Ifdaemon is not connected to a terminal, but the client needs to beconnected to a terminal, use the--pty
option.
If the--bind
option was supplied, on systems withsystemd-logind orelogind, the client process will be terminated when the user logs out.
The--stop
option sends aSIGTERM
signal to a currently running nameddaemon, which causes it to terminate its client process (with aSIGTERM
signal), and to then terminate itself.
The--restart
option sends aSIGUSR1
signal to a currently running nameddaemon, which causes it to terminate its client process (with aSIGTERM
signal). If the nameddaemon was started with the--respawn
option, itwill then restart the client. Otherwise, it will terminate itself.
The--signal
option sends a user-specified signal directly to a currentlyrunning nameddaemon's client process.
The--running
option reports whether or not a given nameddaemon processis currently running.
The--list
option reports all the currently running nameddaemon processes.
URL: https://libslack.org/daemonURL: https://raf.org/daemonGIT: https://github.com/raforg/daemonGIT: https://codeberg.org/raforg/daemonDate: 20230824Author: raf <raf@raf.org>