From nobody Tue Dec 10 23:38:06 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 4Y7FXZ64sMz5g2bn; Tue, 10 Dec 2024 23:38:06 +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 4Y7FXZ45jSz4lk6; Tue, 10 Dec 2024 23:38:06 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733873886; 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=X3/tOjJZUXYE5u4wRa4iSuU+XgsSrnV2w2y+BjzXROo=; b=ZY21EITw9Tctnc5ScfWy5h+GDq5/TcDyFXoAxA79B7O2UzhAn/72xch0Xr5r8cYo6KciQM W89Wyi4+eXjCcoVJRvBCu8P0Wa3314EzcCzJOxb4olRV23Mkng1idu/j2Mj6lR4Hm6/j3F SZSkAe2YBKzSe3tNe/LdiMWeXva/u7UjPlYG58DC0WWabfc4n42yRDkF1RuSWdCeUpIEEx mPJzjufv23irLjKaaIKTYidfKC+EAOeLSgPZ9XQg7s6jGEZ+FJzjkjv7GMvjpfZxvYzJ/H ehIIRFWfZ68iPizhEePvSLWqsoyC6XCX5MTp7Lnr6CaX6eTHpmylkLuZEJILBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733873886; 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=X3/tOjJZUXYE5u4wRa4iSuU+XgsSrnV2w2y+BjzXROo=; b=bkeRejs/SQzu3MehSoyxKRrCpEghCmhxGJLAwTn9V4Kzz9+O/dDpL9P0YgKc6AGncXQY7P no18KbW2Zk7UoB7KX283/oIap3Ifni67tTNEyrf+nqiHRiAzgTTCVPy9YHHxicTmWohAyG GDM24Ag2ZZVj6FCSmLfhhNpS6ABOgXe6fVOOsh3hHZOliPI6O2u9s16dvLlt/N7/G0gi2e kvwaCu0gOlKUeEy7v2hkrkwesRClQFpMvodGnAAJw7a1jWO79HDQSIFuvywye7A4P8Brlt nkWOpzQgyeckiicRM+8nRyVED3AB6SlcJDhegTlJdlERy3f2mYQZ11Cy1x8K5Q== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733873886; a=rsa-sha256; cv=none; b=Atrl/dCXWSlIljTKxje3YN7TgPpMiToYZlfyNiqSOu1yaodpyExAt5p6QiLmsy0LFQajLF mauoW+aNGYvIY7kRBtUlW06kJDwbKws1PYgV1+9YRW8OqvAiviIeEw72LE54vy8g8YR59g V5+lM5oQcd+idErlbYrIrYQDtQq8l8W/0q+HQoIhUYZcuIlski+wMGhJPcRH0QZUDG3cSH FNyrQL/TMdREvKuV7OrNPUpid9H0/hJX4zfM952HNmWAVmdl/1MQ40Vztd9PAFBTtAuv98 D7JH1ewd7czL9mOlbaOS32bvP7Oqskqb5njUEt9nTJ55jSskhDcaOzjOSnbXKA== 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 4Y7FXZ27gszkTC; Tue, 10 Dec 2024 23:38:06 +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 4BANc6HP020184; Tue, 10 Dec 2024 23:38:06 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4BANc6qL020181; Tue, 10 Dec 2024 23:38:06 GMT (envelope-from git) Date: Tue, 10 Dec 2024 23:38:06 GMT Message-Id: <202412102338.4BANc6qL020181@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: 067e3791d615 - stable/13 - 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/13 X-Git-Reftype: branch X-Git-Commit: 067e3791d6151973eaae70ab767ce5f53fbef3f6 Auto-Submitted: auto-generated The branch stable/13 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=067e3791d6151973eaae70ab767ce5f53fbef3f6 commit 067e3791d6151973eaae70ab767ce5f53fbef3f6 Author: Kyle Evans AuthorDate: 2024-11-19 19:51:26 +0000 Commit: Kyle Evans CommitDate: 2024-12-10 23:02:36 +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" ] || \