From nobody Tue Dec 10 23:37:58 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Y7FXR18mFz5g34C; Tue, 10 Dec 2024 23:37:59 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y7FXR0KFqz4l2g; Tue, 10 Dec 2024 23:37:59 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733873879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=W8Nh0LRK7owKcgIL6Kq4LS0tPNv0FOHNMJe8cKBT7V8=; b=t9HFHkB8zt1dTX8wTT9Lr4teexUxC2bxPX0mhoDDxyWC6vo6E76OtYBCc+NzWdnrrYpvh2 DFqWKvMsCcQeZyhMLuq4w8Patwh0rkLyNQ37vjxTJ34vVFH3Ywys/zjE02TI4ElcRuWjaO u9TMylkgF/1BgP7d///PeEMSwsPFVSO5k42shrMlSCzu/Uv9mdIGElQWKmiUujT0E62qWb gC/+QKBsr/E7bZIsyBH5tbWSEdEkKvBXl1eRh9QobfPP7RbZp4dP7r5KksZ4ctGIcbTuJX gW1fGcHjDG2+1es5JgcPPqKSKb9VtiFGkuIES6KVLgMFXMCUDp3AtGlBIhFWpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733873879; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=W8Nh0LRK7owKcgIL6Kq4LS0tPNv0FOHNMJe8cKBT7V8=; b=SJjsq1qW4t8nANP9V71CxWsUQi9ec8AeQtSqSjP/fB0RaFcDynAs6HZnc3eW8tcilCzcmm Jn8XlzJZus5Fyd3lsgaA5RfWPheCG5oNNChBszHxoO6uguZuBnPG5i+BxUkYePccv2JPZF kSn8WJoYYCz0aJQzOIRGCSmdHYvpeinVLh+73a8jGnWzwpcQ8OaylgxPznmHmJjGZ+6p/u n+3kaOQLxV+bbigxlFB2l8C9nKZR0VW3OTXuIZaPcCWbNoT0cMaRj9O0wCCuLG4MWGOPiU v5gvor+jLOoPnQaOdT+3OCyrLWbFa32oBAvgAOl5cJU1LFCd0tDJ9XayMmtyNg== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733873879; a=rsa-sha256; cv=none; b=ITv00ls2PmMsqNn2mI5nNVOgSyZ8aH7U2TnKjdYaOxuYGB9eaDSF9yV6M4f1LozQdajJ/X Jv5QTqhE++91NVgEbdoyfETXtjPC+Q7cSohR8oA/Vp2hEbNJAcjVq3DILP9wpYnHvOE68V PnfWeYFIYAwcd7bmUUszTJy31e2dCmFyW5mtvMHgcviqKbVtgRFj56VvkoP+MWBvwejE8K S1jI7tV5c/tOuGzY79qWdR/nPpdmmNaXGC99EJY81x0SwT/bSWiMjMczfs5+A6Wy/bmrIk Pvk9H2GoOMl43t9eb6h2i660gY++OLxvTv4j2RASPTGUFn7pYE+zGelpBCVohg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4Y7FXQ6hr9zkk7; Tue, 10 Dec 2024 23:37:58 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 4BANbwX2019711; Tue, 10 Dec 2024 23:37:58 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BANbwJ5019708; Tue, 10 Dec 2024 23:37:58 GMT (envelope-from git) Date: Tue, 10 Dec 2024 23:37:58 GMT Message-Id: <202412102337.4BANbwJ5019708@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: aee794942130 - stable/14 - daemon: truncate the pidfile when we're waiting to restart child List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: aee794942130e684dd6cfe9c989e8a43c95a3ff3 Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=aee794942130e684dd6cfe9c989e8a43c95a3ff3 commit aee794942130e684dd6cfe9c989e8a43c95a3ff3 Author: Kyle Evans AuthorDate: 2024-11-19 19:51:26 +0000 Commit: Kyle Evans CommitDate: 2024-12-10 22:59:58 +0000 daemon: truncate the pidfile when we're waiting to restart child We need to be able to test some more restart behavior that depends on knowing specifically where we're at (inside the event loop or outside of the event loop). Truncate the pidfile until the process is restarted to give the test a clean marker rather than having to add arbitrary delays and hoping for the best. Reviewed by: des, markj (cherry picked from commit aa8722cc18e4620ea5e7b0a4917207ed1508393d) --- usr.sbin/daemon/daemon.c | 30 ++++++++++++++++++++++++++++++ usr.sbin/daemon/tests/daemon_test.sh | 7 +++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index 52fbfca1dcd2..6d0ce2cc10e6 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -105,6 +105,8 @@ static void daemon_exec(struct daemon_state *); static bool daemon_is_child_dead(struct daemon_state *); static void daemon_set_child_pipe(struct daemon_state *); +static int pidfile_truncate(struct pidfh *); + static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h"; static const struct option longopts[] = { @@ -497,6 +499,15 @@ daemon_eventloop(struct daemon_state *state) close(kq); close(state->pipe_fd[0]); state->pipe_fd[0] = -1; + + /* + * We don't have to truncate the pidfile, but it's easier to test + * daemon(8) behavior in some respects if we do. We won't bother if + * the child won't be restarted. + */ + if (state->child_pidfh != NULL && state->restart_enabled) { + pidfile_truncate(state->child_pidfh); + } } static void @@ -788,3 +799,22 @@ daemon_set_child_pipe(struct daemon_state *state) /* The child gets dup'd pipes. */ close(state->pipe_fd[0]); } + +static int +pidfile_truncate(struct pidfh *pfh) +{ + int pfd = pidfile_fileno(pfh); + + assert(pfd >= 0); + + if (ftruncate(pfd, 0) == -1) + return (-1); + + /* + * pidfile_write(3) will always pwrite(..., 0) today, but let's assume + * it may not always and do a best-effort reset of the position just to + * set a good example. + */ + (void)lseek(pfd, 0, SEEK_SET); + return (0); +} diff --git a/usr.sbin/daemon/tests/daemon_test.sh b/usr.sbin/daemon/tests/daemon_test.sh index 948a9f6a8a57..2ad0803842b7 100644 --- a/usr.sbin/daemon/tests/daemon_test.sh +++ b/usr.sbin/daemon/tests/daemon_test.sh @@ -139,8 +139,11 @@ restart_child_body() { kill $orig_sleep_pid # Wait up to 10s for the daemon to restart the child. for t in `seq 0 0.1 10`; do - new_sleep_pid=`cat sleep.pid` - [ "$orig_sleep_pid" -ne "$new_sleep_pid" ] && break + if [ -s "sleep.pid" ]; then + new_sleep_pid=`cat sleep.pid` + [ "$orig_sleep_pid" -ne "$new_sleep_pid" ] && break + fi + sleep 0.1 done [ "$orig_sleep_pid" -ne "$new_sleep_pid" ] || \