Commit graph

101 commits

Author SHA1 Message Date
Mathieu 1e2be096d4 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:	f907027b49 ("daemon: set supervise_enabled during [..]")
(cherry picked from commit bbc6e6c5ec)
2024-04-17 00:50:03 -05:00
Konstantin Belousov 1d2a587ef9 daemon(8): handle case of waitpid() returning without exited child
PR:	277764

(cherry picked from commit 8eaa6be80d)
2024-03-27 10:27:52 +02:00
Dag-Erling Smørgrav 70bf48a72a 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

(cherry picked from commit cec8e6ba64)
2023-12-13 17:21:13 +01:00
Kyle Evans 0147143008 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:	8935a39932 ("daemon: use kqueue for all events")

(cherry picked from commit 494e7dfdbe)
2023-12-13 17:21:09 +01:00
Ihor Antonov b64f569e5c 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

(cherry picked from commit 8935a39932)
2023-12-13 17:21:03 +01:00
Mateusz Piotrowski 6ed98c185a daemon.8: Document -u sets HOME, USER, and SHELL
Fixes:	6b3ad1d737 When -u option is used also set USER, HOME and SHELL
MFC after:	3 days

(cherry picked from commit c1207678f7)
2023-09-22 17:07:28 +02:00
Mateusz Piotrowski 53512a06ab daemon.8: Sort options in DESCRIPTION
MFC after:	3 days

(cherry picked from commit ee23e1e496)
2023-09-22 17:07:28 +02:00
Mateusz Piotrowski 488064449b daemon.8: Use Cm where appropriate
MFC after:	3 days

(cherry picked from commit a5bc8e8a58)
2023-09-22 17:07:28 +02:00
Mateusz Piotrowski ab77ddea33 daemon.8: Use Dv for signal names
MFC after:	3 days

(cherry picked from commit 735637f7d6)
2023-09-22 17:07:28 +02:00
Warner Losh b144e70a33 Remove $FreeBSD$: two-line nroff pattern
Remove /^\.\\"\n\.\\"\s*\$FreeBSD\$$\n/

Similar commit in main:
(cherry picked from commit fa9896e082)
2023-08-23 11:43:31 -06:00
Warner Losh 023fc80ee3 Remove $FreeBSD$: one-line sh pattern
Remove /^\s*#[#!]?\s*\$FreeBSD\$.*$\n/

Similar commit in main:
(cherry picked from commit d0b2dbfa0e)
2023-08-23 11:43:30 -06:00
Warner Losh 3d497e17eb Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/

Similar commit in main:
(cherry picked from commit 1d386b48a5)
2023-08-23 11:43:26 -06:00
Warner Losh caa41f6417 spdx: The BSD-2-Clause-FreeBSD identifier is obsolete, drop -FreeBSD
The SPDX folks have obsoleted the BSD-2-Clause-FreeBSD identifier. Catch
up to that fact and revert to their recommended match of BSD-2-Clause.

Discussed with:		pfg
MFC After:		3 days
Sponsored by:		Netflix

(cherry picked from commit 4d846d260e)
2023-07-25 09:13:49 -06:00
Kyle Evans 535fc5f75e daemon: reformat longopts
Use a single tab instead of eight spaces, these aren't line
continuations.

(cherry picked from commit b84aaf143c)
2023-04-09 17:49:52 -05:00
Ihor Antonov 81f89a76e8 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

(cherry picked from commit 4c41f4a0d6)
2023-04-09 17:49:52 -05:00
Ihor Antonov 7ad1bebb0f daemon: move signal setup into a function
No functional change intended.

Reviewed by:	kevans

(cherry picked from commit 9ee1faeeba)
2023-04-09 17:49:52 -05:00
Kyle Evans 2a69943c7b daemon: kill off some stray blank lines
Overlooked in review; mea culpa.

Reported by:	jrtc27

(cherry picked from commit 6b49a630f4)
2023-04-09 17:49:51 -05:00
Ihor Antonov 980ad64271 daemon: remove unnecessary memset in daemon_state_init()
(cherry picked from commit 8117ea0a41)
2023-04-09 17:49:51 -05:00
Ihor Antonov edc3bffde2 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@.

(cherry picked from commit cf6356fd47)
2023-04-09 17:49:51 -05:00
Ihor Antonov 7362081fad 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

(cherry picked from commit 298a392ec3)
2023-04-09 17:49:51 -05:00
Ihor Antonov 799d67ec40 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

(cherry picked from commit f907027b49)
2023-03-17 16:01:03 -05:00
Ihor Antonov ec3a7d2828 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

(cherry picked from commit e781739084)
2023-03-17 16:01:03 -05:00
Ihor Antonov 3f0b048a24 daemon: add braces to while loop
Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/672

(cherry picked from commit d6c398d882)
2023-03-17 16:01:03 -05:00
Ihor Antonov bc9d2ee0a7 daemon: simplify if/else chain
Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/672

(cherry picked from commit cd1e6e70d0)
2023-03-17 16:01:02 -05:00
Ihor Antonov bb2e206cae daemon: change type of listen_child() to C99 bool
Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/672

(cherry picked from commit bc43a9a715)
2023-03-17 16:01:02 -05:00
Ihor Antonov 204306b053 daemon: flatten and simplify fork() logic
Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/672

(cherry picked from commit 75f61ca920)
2023-03-17 16:01:02 -05:00
Ihor Antonov 05578dd521 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

(cherry picked from commit 39ea4280e4)
2023-03-17 16:01:02 -05:00
Ihor Antonov 8eb1e5dd77 daemon: more human-friendly variable names
Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/669

(cherry picked from commit 129ec8f480)
2023-03-17 16:01:02 -05:00
Ihor Antonov d8b8a0316e 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

(cherry picked from commit 6f0636728b)
2023-03-17 16:01:02 -05:00
Ihor Antonov b71a65870f 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

(cherry picked from commit e70444c622)
2023-03-17 16:01:02 -05:00
Ihor Antonov 5e46ebc5d0 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

(cherry picked from commit 97022e90c3)
2023-03-17 16:01:02 -05:00
Ihor Antonov d394c80f49 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

(cherry picked from commit f2f9d31d9f)
2023-03-17 16:01:01 -05:00
Ihor Antonov 6f71a68456 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

(cherry picked from commit 203df05b69)
2023-03-17 16:01:01 -05:00
Ihor Antonov 52f302f136 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

(cherry picked from commit 259ed21d21)
2023-03-17 16:01:01 -05:00
Ihor Antonov 93871c6cff daemon: fix double init of pid variable
Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/669

(cherry picked from commit 91b921c7d4)
2023-03-17 16:01:01 -05:00
Ihor Antonov cc123f0dba 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

(cherry picked from commit 84866cefdf)
2023-03-17 16:01:01 -05:00
Ihor Antonov d24c97ebfb 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

(cherry picked from commit e745dc2287)
2023-03-17 16:01:01 -05:00
Ihor Antonov 488cf4bb6f daemon: use braces with 'if' consistently
Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/669

(cherry picked from commit 6b4ef4b16a)
2023-03-17 16:01:01 -05:00
Ihor Antonov 282e4cf32d 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

(cherry picked from commit 0a402ad2e6)
2023-03-17 16:01:01 -05:00
Maxim Sobolev dc96fb0723 daemon(8): when -u option is used set USER, HOME and SHELL variables.
This is consistent with what other uid-morphing utilities
do, i.e. jexec(1), su(1) etc.

Reviewed by:    gbe
Differential Revision:  https://reviews.freebsd.org/D36148

(cherry picked from commit 6b3ad1d737)
2022-09-08 04:42:56 -07:00
Alan Somers d94aa742b5 daemon: add some basic tests
Sponsored by:	Axcient
Differential Revision:	https://reviews.freebsd.org/D29316

(cherry picked from commit 3b57d80c7a)
2021-06-03 20:48:47 -06:00
Mateusz Piotrowski 54c743e5b2 Fix a typo
"and" is not a flag.

MFC after:	3 days
2021-01-14 19:12:55 +01:00
Maxim Sobolev dffc6929bf Fix a typo in the 366098.
Reported by:	0mp
MFC after:	2 weeks
		(along with 366098)
2020-09-24 19:12:03 +00:00
Maxim Sobolev 4cd407ec93 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
2020-09-24 02:44:58 +00:00
Ian Lepore b6fa976de2 Bump .Dd for earlier update (should have been part of r353024). 2019-10-02 23:19:34 +00:00
Ian Lepore 0ec0f10d66 Clarify how the -f option for daemon(8) interacts with other options
related to redirecting stdout and stderr.
2019-10-02 23:06:17 +00:00
Conrad Meyer 09a3675d96 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
2019-06-04 16:07:01 +00:00
Michael Gmelin 4f6714d53b Correct contradictory information on default syslog logging priority.
MFC after:	1 week
2019-03-25 21:14:51 +00:00
Mateusz Piotrowski a641e44515 Cross-reference nohup(1) and daemon(8).
Reviewed by:	bcr
Approved by:	krion (mentor, implicit), mat (mentor, implicit)
Differential Revision:	https://reviews.freebsd.org/D17920
2018-11-09 13:47:06 +00:00
Mateusz Piotrowski 26e19acaeb Add a missing "Ar" macro to the description of the -R flag in the manpage.
Approved by:	bjk (doc committer), krion (mentor)
Differential Revision:	https://reviews.freebsd.org/D16520
2018-08-01 12:18:52 +00:00