History log of /freebsd-current/usr.sbin/daemon/daemon.c
Revision Date Author Comments
# 7618c9e1 25-Apr-2024 Juraj Lutter <otis@FreeBSD.org>

daemon: Add -C (--restart-count) option

Add a new option (-C, --restart-count) to specify the maximum
number of times that the controlled process is restarted if
restart (-r) is restarted.

Reviewed by: kevans
Differential Revision: https://reviews.freebsd.org/D44944


# bbc6e6c5 13-Apr-2024 Mathieu <sigsys@gmail.com>

daemon: fix -R to enable supervision mode

If we're doing restarts, then we must supervise -- the 'R' case simply
got missed.

PR: 278342
Fixes: f907027b49d ("daemon: set supervise_enabled during [..]")


# f7a10a77 12-Apr-2024 Collin Funk <collin.funk1@gmail.com>

daemon: Prefer sys/cdefs.h __unreachable over the builtin

The __builtin_unreachable macro provided by Clang and GCC is a hint to
the compiler used for optimization. The programs work fine even if the
compiler doesn't support it. The sys/cdefs.h has had __unreachable for
9 years (commit 732b31de5d9244bd1cc98192e09ee1881e9f55e9). It expands
to the builtin if it is available. In the rare case that it is
unsupported it expands to a null statement so compilation does not
fail.

Signed-off-by: Collin Funk <collin.funk1@gmail.com>
Reviewed by: imp, freebsd@igalic.co
Pull Request: https://github.com/freebsd/freebsd-src/pull/1117


# 8eaa6be8 18-Mar-2024 Konstantin Belousov <kib@FreeBSD.org>

daemon(8): handle case of waitpid() returning without exited child

Not checking for either WIFEXITED(status) or zero result results in
never finishing the loop.

PR: 277764
Reviewed by: kevans (previous version)
Discussed with: Daniel Tameling
Sponsored by: The FreeBSD Foundation
MFC after: 1 week
Differential revision: https://reviews.freebsd.org/D44401


# e0645579 27-Dec-2023 Kyle Evans <kevans@FreeBSD.org>

daemon: fix recent style regressions

Re-wrap the read(2) line, and make the listen_child loop more explicit
in intent.

Reported by: kib
Fixes: 6ac7c9f06ae9 ("daemon: remove redundant parameter from [...]")


# 407e3790 26-Dec-2023 Ihor Antonov <ihor@antonovs.family>

daemon: separate pipe_fd[2] into pipe_rd and pipe_wr

This improves code readability and prevents mixing up read and write
ends of the pipe.

Reviewed by: cperciva, kevans
Requested by: kevans


# 5745a584 26-Dec-2023 Ihor Antonov <ihor@antonovs.family>

daemon: replace memchr with memrchr

Looping over lines in the buffer is not needed.
Same effect can be achieved by looking for the last new line.
If found the buffer is guaranteed to have one or more complete lines.
All complete lines are flushed at once with no looping.

Reviewed by: cperciva, kevans


# 24fd3e96 26-Dec-2023 Ihor Antonov <ihor@antonovs.family>

daemon: move buffer into daemon_state

There is no reason for a buffer in listen_child()
to be a static function variable. The buffer and
its position are parts of the daemon state and should
live together with the rest of the state variables.

Reviewed by: cperciva, kevans


# 6ac7c9f0 26-Dec-2023 Ihor Antonov <ihor@antonovs.family>

daemon: remove redundant parameter from listen_child()

state already contains pipe fd

Reviewed by: cperciva, kevans


# a6f795cc 26-Dec-2023 Ihor Antonov <ihor@antonovs.family>

daemon: fix clang-tidy warnings

Fixed narrowing conversions:
- strtol replaced with strtonum with range check
- read returns ssize_t
- kevent.data explicitly cast to int before passing into strerror

While we we're here:
- Defined and documented maximum restart delay.
- Fixed typo in a comment.
- Remove unused includes

Reviewed by: cperciva, kevans


# 4d65a7c6 24-Nov-2023 Warner Losh <imp@FreeBSD.org>

usr.sbin: Automated cleanup of cdefs and other formatting

Apply the following automated changes to try to eliminate
no-longer-needed sys/cdefs.h includes as well as now-empty
blank lines in a row.

Remove /^#if.*\n#endif.*\n#include\s+<sys/cdefs.h>.*\n/
Remove /\n+#include\s+<sys/cdefs.h>.*\n+#if.*\n#endif.*\n+/
Remove /\n+#if.*\n#endif.*\n+/
Remove /^#if.*\n#endif.*\n/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/types.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/param.h>/
Remove /\n+#include\s+<sys/cdefs.h>\n#include\s+<sys/capsicum.h>/

Sponsored by: Netflix


# cec8e6ba 06-Oct-2023 Dag-Erling Smørgrav <des@FreeBSD.org>

daemon: Disable stdio buffering.

The daemon utility already does its own buffering and retransmits its
child's output line by line. There's no need for stdio to add its own
buffering on top of this.

MFC after: 1 week
Sponsored by: Modirum MDPay
Reviewed by: allanjude
Differential Revision: https://reviews.freebsd.org/D42111


# 1d386b48 16-Aug-2023 Warner Losh <imp@FreeBSD.org>

Remove $FreeBSD$: one-line .c pattern

Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/


# 494e7dfd 05-May-2023 Kyle Evans <kevans@FreeBSD.org>

daemon: EINTR from kevent(2) is not a fatal error

Simply resume waiting for events rather than exiting if we took a signal
here.

This at least fixes running programs under daemon(8) in the face of
suspend/resume, which I suspect hits us with a spurious EINTR rather
than a signal anyways.

Reported and tested by: manu
Fixes: 8935a3993219b ("daemon: use kqueue for all events")


# 8935a399 13-Apr-2023 Ihor Antonov <ihor@antonovs.family>

daemon: use kqueue for all events

Refactor daemon to use kqueue/kevent instead of signals.

This changes allows to simplify the code in several ways:
- the execution flow is now linear, no async events.
- several variables became redundant and got removed.
- all event handling is now concentrated inside of the event loop, which
makes code reading and comprehension easier.
- new kqueuex(2) call is used for CLOEXEC, but maintained closing the
kq fd prior to execve() to ease later MFC

No UX/API changes are intended.

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/701


# b84aaf14 22-Mar-2023 Kyle Evans <kevans@FreeBSD.org>

daemon: reformat longopts

Use a single tab instead of eight spaces, these aren't line
continuations.


# 4c41f4a0 22-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: decouple init logic from main loop

main() func contained both initialization and main loop logic.
This made certain operations like restarting problematic and
required dirty hacks in form of goto jumps.

This commit moves the main loop logic into daemon_eventloop(),
cleans up main, and makes restart logic clear: daemon_mainloop()
is run in a loop with a restart condition checked at the end.

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/699


# 9ee1faee 20-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: move signal setup into a function

No functional change intended.

Reviewed by: kevans


# 6b49a630 18-Mar-2023 Kyle Evans <kevans@FreeBSD.org>

daemon: kill off some stray blank lines

Overlooked in review; mea culpa.

Reported by: jrtc27


# 8117ea0a 17-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: remove unnecessary memset in daemon_state_init()

Pull Request: https://github.com/freebsd/freebsd-src/pull/694


# cf6356fd 17-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: repace goto exit with daemon_terminate()

Start breaking down big main()
Remove goto exit label and replace it with a function that does cleanup.

Comment re-worded by kevans@.

Pull Request: https://github.com/freebsd/freebsd-src/pull/694


# 298a392e 12-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: move variables into struct daemon_state

The fact that most of the daemon's state is stored on the stack
of the main() makes it hard to split the logic smaller chunks.
Which in turn leads to huge main func that does a a lot of things.
struct log_params existed because some variables need to be passed
into other functions together.

This change renames struct log_params into daemon_state
and moves the rest of the variables into it. This is a necessary
preparation step for further refactroing.

Reviewed by: imp
Pull Request: https://github.com/freebsd/freebsd-src/pull/687


# f907027b 02-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: set supervise_enabled during argument processing

Now when supervsion mode has it's own variable there is really no
reason to set it separately from the rest of the variables. Move
initialization of supervise_enabled var to the argument processing
switch loop, where it belongs.

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/672


# e7817390 02-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: decouple restart variable

The 'restart' variable was responsible for enablement of restart
behavior and for restart delay. While it may seem convenient it
leads to cluttering the exit/restart logic

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/672


# d6c398d8 02-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: add braces to while loop

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/672


# cd1e6e70 02-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: simplify if/else chain

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/672


# bc43a9a7 02-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: change type of listen_child() to C99 bool

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/672


# 75f61ca9 02-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: flatten and simplify fork() logic

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/672


# 39ea4280 02-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: style changes

This is not a functional change.
- Clean up whitespace (spaces where there should be tabs)
- Break up lines that are longer than 80

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/672


# 129ec8f4 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: more human-friendly variable names

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 6f063672 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: move syslog facility and syslog tag into log_params

Since struct log_params already contains logging-related
varaiables, including syslog-related, move remaining
syslog-related variables into struct log_params as well

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# e70444c6 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: deduplicate log_params varaibles

While we're here, sort log_params by size.

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 97022e90 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: make log_reopen variable a bool

Following style(9) and C99 recommendation use bool instead of
int for boolean operations.

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# f2f9d31d 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: make dosyslog variable a bool and give it a better name

Following style(9) and C99 recommendation use bool instead of
int for boolean operations. Also give the variable a more descriptive
name that follows boolean naming convention.

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 203df05b 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: add supervision_enabled var

explicitly name a bunch of boolean checks that enable
supervison mode and improve comments

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 259ed21d 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: initialize struct sigaction at declaration site

This improves readability by uncluttering the code

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 91b921c7 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: fix double init of pid variable

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 84866cef 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: initialize mask_orig with sigemptyset()

consolidation of variable declarations and initializations in previous
commit allowed me to detect that one of the signal masks is not properly
initialized with sigemptyset (as man 3 sigsetops demands)

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# e745dc22 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: consolidate variable declarations and initializaions

- improve readability by breaking apart single-line multi-variable declarations
- initialize simple variables at declaration site
- move other top-level variable initializations closer declarations
to avoid potential UB and unclutter the use-site.

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 6b4ef4b1 01-Mar-2023 Ihor Antonov <ihor@antonovs.family>

daemon: use braces with 'if' consistently

Reviewed by: kevans
Pull Request: https://github.com/freebsd/freebsd-src/pull/669


# 0a402ad2 09-Feb-2023 Ihor Antonov <ihor@antonovs.family>

daemon: add long_opts

Long options improve readability of scripts, makes code comprehension
easier. This patch adds long options while preserving the existing CLI
interface.

Also --help/-h option is added.

Reviewed by: allanjude, pauamma (both earlier versions), kevans
Differential Revision: https://reviews.freebsd.org/D38244


# 6b3ad1d7 10-Aug-2022 Maxim Sobolev <sobomax@FreeBSD.org>

When -u option is used also set USER, HOME and SHELL variables.
This is consistent with what other uid-morphing utilities
do, i.e. jexec(1), su(1) etc.

MFC after: 2 weeks
Reviewed by: gbe
Differential Revision: https://reviews.freebsd.org/D36148


# 4cd407ec 23-Sep-2020 Maxim Sobolev <sobomax@FreeBSD.org>

dd a new option (-H) to daemon(8) to catch SIGHUP and re-open output_file file when
received.

The default system log rotation mechanism (newsyslog(8)) requires ability to send
signal to a daemon in order to properly complete rotation of the logs in an "atomic"
manner without having to making a copy and truncating original file. Unfortunately
our built-in mechanism to convert "dumb" programs into daemons has no way to handle
this rotation properly. This change adds this ability, to be enabled by supplying -H
option in addition to the -o option.

Reviewed by: markj, rpokala (manpages)
MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D26526


# 09a3675d 04-Jun-2019 Conrad Meyer <cem@FreeBSD.org>

daemon(8): Don't block SIGTERM during restart delay

I believe this was introduced in the original '-r' commit, r231911 (2012).
At the time, the scope was limited to a 1 second sleep. r332518 (2018)
added '-R', which increased the potential duration of the affected interval
(from 1 to N seconds) by permitting arbitrary restart intervals.

Instead, handle SIGTERM normally during restart-sleep, when the monitored
process is not running, and shut down promptly.

(I noticed this behavior when debugging a child process that exited quickly
under the 'daemon -r -R 30' environment. 'kill <daemonpid>' had no
immediate effect and the monitor process slept until the next restart
attempt. This was annoying.)

Reviewed by: allanjude, imp, markj
Differential Revision: https://reviews.freebsd.org/D20509


# 37820b87 15-Apr-2018 Ian Lepore <ian@FreeBSD.org>

Add an option to daemon(8) to specify a delay between restarts of a
supervised program. The existing -r option has a hard-coded delay of one
second. This change adds a -R option which takes a delay in seconds. This
can be used to prevent log spam and rapid restarts, similar to init(8)'s
behavior of adding a delay between rapid restarts when it's supervising a
program.


# 1de7b4b8 27-Nov-2017 Pedro F. Giffuni <pfg@FreeBSD.org>

various: general adoption of SPDX licensing ID tags.

Mainly focus on files that use BSD 2-Clause license, however the tool I
was using misidentified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

No functional change intended.


# 53d49b37 22-Oct-2016 Jilles Tjoelker <jilles@FreeBSD.org>

daemon: Allow logging daemon stdout/stderr to file or syslog.

There are various new options, documented in the man page, to send the
daemon's standard output and/or standard error to a file or to syslog.

Submitted by: ank at iki.fi
Reviewed by: wblock (man page only)
Relnotes: yes
Differential Revision: https://reviews.freebsd.org/D7993


# 64778f37 03-Mar-2016 Maxim Konovalov <maxim@FreeBSD.org>

o -t comman line option added to the usage().


# 112bfcf5 02-Mar-2016 Conrad Meyer <cem@FreeBSD.org>

daemon(8): Add -t option to set process title

The default process title is taken from the argv[0] value (any
particular hardlink name). Add a -t option to override the default.

PR: 205016
Submitted by: Yuri <yuri@rawbw.com>
No objection from: freebsd-current@
Sponsored by: EMC / Isilon Storage Division


# ceda7f06 06-Apr-2014 Jaakko Heinonen <jh@FreeBSD.org>

Fork a child process and wait until the process terminates when the -P
option is specified. This behavior is documented on the manual page.

PR: bin/187265
Submitted by: Kimo R
MFC after: 2 weeks


# 9da0ef13 19-Sep-2013 Mikolaj Golub <trociny@FreeBSD.org>

1. Properly clean pid files in the case of the error.
2. Write the supervisor pid before the restart loop, so we don't
uselessly rewrite it after every child restart.
3. Remove duplicate ppfh and pfh initialization.

Approved by: re (glebius)
MFC after: 2 weeks


# 32b17786 13-Sep-2013 John-Mark Gurney <jmg@FreeBSD.org>

add support for writing the pid of the daemon program to a pid file so
that daemon can be used w/ rc.subr and ports can use the additional
functionality, such as keeping the ldap daemon up and running, and have
the proper program to signal to exit..

PR: bin/181341
Submitted by: feld
Approved by: re (glebius)


# 7c0360c4 02-Jan-2013 Marius Strobl <marius@FreeBSD.org>

Remove bogus '-' from getopt(3) string hit when porting daemon(8) to
GNU/Linux *duck*.

MFC after: 1 week


# 76462499 04-Jun-2012 Mikolaj Golub <trociny@FreeBSD.org>

Document -r option in SYNOPSIS and usage statement.

Submitted by: Andrey Zonov <andrey zonov.org>
MFC after: 3 days


# c60d51f9 04-Jun-2012 Mikolaj Golub <trociny@FreeBSD.org>

On a child exit, call waitpid(2) to clean up the process table.

Submitted by: Andrey Zonov <andrey zonov.org>
MFC after: 1 week


# 53c49998 19-Feb-2012 Mikolaj Golub <trociny@FreeBSD.org>

If permitted protect the supervisor against pageout kill.

Suggested by: Andrey Zonov <andrey zonov org>
MFC after: 2 weeks


# b6193c24 19-Feb-2012 Mikolaj Golub <trociny@FreeBSD.org>

Add -r option to restart the program if it has been terminated.

Suggested by: Andrey Zonov <andrey zonov org>
MFC after: 2 weeks


# 195fc497 19-Feb-2012 Mikolaj Golub <trociny@FreeBSD.org>

If the supervising process receives SIGTERM, forward it to the spawned
process. Normally it will cause the child to exit followed by the
termination of the supervisor after removing the pidfile.

This looks like desirable behavior, because termination of a
supervisor usually supposes termination of its charge. Also it will
fix the issue with stale pid files after reboot due to init kills a
supervisor before its child exits.

MFC after: 2 weeks


# 2ad43027 19-Feb-2012 Mikolaj Golub <trociny@FreeBSD.org>

The pidfile_open(3) is going to be fixed to set close-on-exec in order
not to leak the descriptor after exec(3). This raises the issue for
daemon(3) of the pidfile lock to be lost when the child process
executes.

To solve this and also to have the pidfile cleaned up when the program
exits, if a pidfile is specified, spawn a child to exec the command
and wait in the parent keeping the pidfile locked until the child
process exits and remove the file.

Reported by: Andrey Zonov <andrey zonov org>
Suggested by: pjd
Reviewed by: pjd
MFC after: 2 weeks


# 704523b0 25-Jan-2012 Guy Helmer <ghelmer@FreeBSD.org>

Revert 229667: After some discussion of this change, it seems it is
better to leave the pidfile open where it was. Add a note to the
man page describing pidfile strategies to use if the daemon is to
be run as a user other than root.


# 074db536 05-Jan-2012 Guy Helmer <ghelmer@FreeBSD.org>

Allow daemon(8) to run pidfile_open() before relenquishing privileges
so pid files can be written in /var/run when started as root.

I do not expect this to cause any security issues, but if anyone objects
it could be easily reverted.

PR: bin/159568
MFC after: 4 weeks


# a7d5f7eb 19-Oct-2010 Jamie Gritton <jamie@FreeBSD.org>

A new jail(8) with a configuration file, to replace the work currently done
by /etc/rc.d/jail.


# fe0506d7 09-Mar-2010 Marcel Moolenaar <marcel@FreeBSD.org>

Create the altix project branch. The altix project will add support
for the SGI Altix 350 to FreeBSD/ia64. The hardware used for porting
is a two-module system, consisting of a base compute module and a
CPU expansion module. SGI's NUMAFlex architecture can be an excellent
platform to test CPU affinity and NUMA-aware features in FreeBSD.


# d7f03759 19-Oct-2008 Ulf Lilleengen <lulf@FreeBSD.org>

- Import the HEAD csup code which is the basis for the cvsmode work.


# 12d5df2b 19-Apr-2007 Peter Wemm <peter@FreeBSD.org>

Unbreak rev 1.7's getopt usage. The -f switch does not take an argument.
The stray ':' was from the defunct '-g' group arg.


# e6d4b388 18-Mar-2007 Tom Rhodes <trhodes@FreeBSD.org>

Improve previous commit by using setusercontext(3) and removing the group
option. Bump doc date for manual page changes.

Reviewed by: rwatson, ru, will (older version)


# 15543aad 09-Mar-2007 Tom Rhodes <trhodes@FreeBSD.org>

Initialize *pfh to NULL to quiet a gcc warning, not part of my original
commit because it was not part of the new code.


# 12d7249e 09-Mar-2007 Tom Rhodes <trhodes@FreeBSD.org>

Add support for dropping privileges to a specified user and/or group.

PR: 108523
Submitted by: Dmitri Alenitchev <dmitri@dworlds.ru> (original version)
Reviewed by: mpp (first reply to PR)


# c6262cb6 24-Aug-2005 Pawel Jakub Dawidek <pjd@FreeBSD.org>

Teach daemon(8) how to use pidfile(3).


# 846be7bd 02-Jun-2004 Poul-Henning Kamp <phk@FreeBSD.org>

Give daemon(8) the ability to create a pid-file. Since the target program
does not know anything about the pid-file and we don't keep a babysitting
process for the task, the pid-file will linger.

Submitted by: mi
PR: 56398


# 54ede02d 05-Jul-2003 Philippe Charnier <charnier@FreeBSD.org>

add FBSDID


# bd06a3ec 29-Aug-2001 Mike Barcroft <mike@FreeBSD.org>

Add a new utility that runs an executable detached from the
controlling terminal.

Discussed on: -hackers
Obtained from: BSD/OS