From nobody Tue Nov 19 19:51:41 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 4XtFW12J41z5fNDP; Tue, 19 Nov 2024 19:51:41 +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 4XtFW11phGz4cj2; Tue, 19 Nov 2024 19:51:41 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732045901; 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=4hmwkLnUQvoYt1omZcK80mKSEqF4WkyrJoR6aQ3V0vA=; b=dRBlt8qxzBkRXqkE3LcpV7S9BpNPZ4LKDUR64PAvCctl79FG4UpPUdZH1r7d0ibHQqZcdd bh1nz994QJ5KntE5ZA9s3Qm98bskjfQqblFTu9oWdl6ka+Wtlzflv61C+cQGaKNsUu+cDw u79nnQq31qDejn0D5rBvqrJmCpbrHfzhvvD+ms2vvUOYVn8a5DjRWlI9boeNcN0HJzo6m2 Yd8eCv8YOKsMiWYkRjylp2lHxQeU7QRtQ4wolvKXOR3yO9r01t3eaUxkAKtVLQ7I7AnMd1 THqcbI+NeiNym1JTFcni2B5tJ9IJ5ZzDMCa96L/n8ThNUPL676vafqG0PbLbfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732045901; 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=4hmwkLnUQvoYt1omZcK80mKSEqF4WkyrJoR6aQ3V0vA=; b=i7S5Mn3iq6tl4lMMB4ryBJ5x7BQ4ULjCq2T/ZefOytcwn4OnPqy6OqyjxRkHCQ5ATURVWC BDWMraFiOp/JBn8h27gWYg34LuOsaZm14+RSfWv+U6k+lc7ZqrxkCb94QrDotaDV47YKK6 EU8bKlRQvwx6jUA30pXz4hJ4vKDTiTvCu2fTfPlHLnJzFRds7kvuTc9H/G6OFy7CmaOSle ASO+U8aCcVbRh4JayJ1nmqR7m9h3SwV2R3Oy2iBgAaV1ouy8PEofR1zcwApShaY4NZZrtw GeLo/42JSyg91ilzrkp+Qx3EoNGKLf9NdTsd3fzN95JwKU1tBCDJ51DEU56vsw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732045901; a=rsa-sha256; cv=none; b=yqwQ8UZwFfFDqDPFSYw2QAivfs+7AsPBz4VgP3XkwhytT7Y15pAuL9YzrSeg/fyPTDPq8a EW1FaOWEZSTwD4n9wagNeP91Rw/dpLBtlN4qlsiuG9BHe3ozvi9381z7xQ4qxkQ5zYELfj hU4EunLr7hfmnMBLmtn5J0Y3bwn1EsyWzQDwl4Eh3aeVNG6XeYatnmNdOYZ5588ZxHsGWS YJ2P49dHsI/DoZdNp0HsX0mnD7c9+UbYufHjYoCTcNKmRE740N/rL59JXqnLrIb7t9i0M+ 1LojkkqkvI6hoscb3c+/IuGx909GgOd6oc1E1enrXPhoLQ4UvARVmhzE6a0qbQ== 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 4XtFW11PRlzPBj; Tue, 19 Nov 2024 19:51:41 +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 4AJJpf0L035529; Tue, 19 Nov 2024 19:51:41 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4AJJpfDw035526; Tue, 19 Nov 2024 19:51:41 GMT (envelope-from git) Date: Tue, 19 Nov 2024 19:51:41 GMT Message-Id: <202411191951.4AJJpfDw035526@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: aa8722cc18e4 - main - 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/main X-Git-Reftype: branch X-Git-Commit: aa8722cc18e4620ea5e7b0a4917207ed1508393d Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=aa8722cc18e4620ea5e7b0a4917207ed1508393d commit aa8722cc18e4620ea5e7b0a4917207ed1508393d Author: Kyle Evans AuthorDate: 2024-11-19 19:51:26 +0000 Commit: Kyle Evans CommitDate: 2024-11-19 19:51:26 +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 Differential Revision: https://reviews.freebsd.org/D47003 --- 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 7214faf8bde3..a21346086dd1 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -114,6 +114,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:C:h"; static const struct option longopts[] = { @@ -526,6 +528,15 @@ daemon_eventloop(struct daemon_state *state) close(kq); close(state->pipe_rd); state->pipe_rd = -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 @@ -823,3 +834,22 @@ daemon_set_child_pipe(struct daemon_state *state) /* The child gets dup'd pipes. */ close(state->pipe_rd); } + +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" ] || \