From nobody Thu Sep 28 15:52:48 2023 X-Original-To: dev-commits-src-main@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 4RxJ0J5kTrz4txtw; Thu, 28 Sep 2023 15:52:48 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4RxJ0J59RKz4LjY; Thu, 28 Sep 2023 15:52:48 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695916368; 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=0XmEKu5FiD9EuoBaZd4HYdgU9ckMvE3Gab6rewi4+q4=; b=Ko1Br65Lyilse3m/ItIXos3q/Wni9wXijxg2Ar0HCwdfIZE3iPtlg4YdbkvbV9ep90bbYC eIqGfvG+uXixz5ajQIHWejBLp5Ywz+Lvlrqgx6DqlHulSAXnDUBgFq6LpJY9ql5Srn9VQ9 uB8vgSZ6KQVnIsX1hEBURtlMCCHBj0xEVintOjbcEz24t1Gr2mqFuHfOmtu56V5pJFLwaH KrbhPaTWVxy8ihdb0xgN+95QCfK1TBXsC4U785NypBsacjQTuyw7ZX+NjipjPmnJYSDaJy VUvz6llB8SzNslzDA2RBXpYXpN2eaQxO+AsirW1h+uDHtVTgw+jalDXahdeJcw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695916368; a=rsa-sha256; cv=none; b=qmi1Qjr18kvmHYUbNi78QgYXxber+3bvMF2XqtFqVFhxRXfWeBRSm81Dob9KPk+AbMVZ9I 9uG7eY18yd/X2jGDefsYNY9IQKegNkJfGb5W4PmXJK2V7WEAZvce2S6yHJxRD+6Gcd07MA L+IXofDKgJm0h3mvRDFQ96h5V8BjG/Nfl48+D3xOBbTwrB0wFDvp2R3VJlTwyE556HT/Ho 0arPNFbOrH4dpwEDIYLREIfd3XRdP/nYq+rm17PuuN86D+ILk9dEW9aEPUIJt6OE8rcdp5 YDoaVcZo3eWWYuKCPXkaO37ci7iGTcT9RoQXxPZWoMlCYBZ6EqnfolRvd9Az5w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695916368; 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=0XmEKu5FiD9EuoBaZd4HYdgU9ckMvE3Gab6rewi4+q4=; b=tItNp2fijsjenwYiCMdtLVrz/E6K0qZdvD9gU+BvGtlmcjhBD8xebOwfGO5Tjm2SFqN6+u Fv0A0PGNurv8PDWylGPK4HOlag9BU4cXs7GM3Vcz4SGoYW8acM+4bHTk5mMmjVVkzM4+ry HCz07EXBL1eK6aj7B39+/UzPa6OuUM76V5cC7yvi8M7QBo+2Qn8lYWYKRlYDGJY/xMEYrk b2w1KprT5T6xmLWQLWJvXTkq2CNf9y+P91Gj9LhMAsyNINUxhPaugYj/pbuAtyHDWemsO1 6hZUyS+tAkCz8HO4jeV/zTRkjla9yx/ti+YZcBCQEC6qAv6CFYaW2k2nSDnaPw== 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 4RxJ0J3rQ6zCqV; Thu, 28 Sep 2023 15:52:48 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 38SFqmVm022589; Thu, 28 Sep 2023 15:52:48 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38SFqm8b022587; Thu, 28 Sep 2023 15:52:48 GMT (envelope-from git) Date: Thu, 28 Sep 2023 15:52:48 GMT Message-Id: <202309281552.38SFqm8b022587@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 8536447a07fa - main - syslogd: Use pipe to communicate with daemon List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-main@freebsd.org X-BeenThere: dev-commits-src-main@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8536447a07fa29073f93af70808db58c760aa6f9 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=8536447a07fa29073f93af70808db58c760aa6f9 commit 8536447a07fa29073f93af70808db58c760aa6f9 Author: Jake Freeland AuthorDate: 2023-09-01 02:51:28 +0000 Commit: Mark Johnston CommitDate: 2023-09-28 15:51:54 +0000 syslogd: Use pipe to communicate with daemon Previously, syslogd's daemon process would signal that it had finished initialization using SIGALRM. In capability mode, signal delivery is not allowed, so use a pipe to indicate that it is ready to accept messages. Reviewed by: markj MFC after: 3 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D41459 --- usr.sbin/syslogd/syslogd.c | 100 ++++++++++++++++++++------------------------- 1 file changed, 44 insertions(+), 56 deletions(-) diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index 3f9633ca8c35..8d2f3c3edf14 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -135,6 +135,7 @@ static char sccsid[] = "@(#)syslogd.c 8.3 (Berkeley) 4/4/94"; #include #include #include +#include #include #include #include @@ -471,7 +472,6 @@ static bool validate(struct sockaddr *, const char *); static void unmapped(struct sockaddr *); static void wallmsg(struct filed *, struct iovec *, const int iovlen); static int waitdaemon(int); -static void timedout(int); static void increase_rcvbuf(int); static void @@ -599,8 +599,8 @@ main(int argc, char *argv[]) struct sigaction act = { }; struct kevent ev; struct socklist *sl; - pid_t ppid = -1, spid; - int ch, kq, s; + pid_t spid; + int ch, kq, ppipe_w = -1, s; char *p; bool bflag = false, pflag = false, Sflag = false; @@ -813,14 +813,9 @@ main(int argc, char *argv[]) (void)strlcpy(bootfile, getbootfile(), sizeof(bootfile)); - if ((!Foreground) && (!Debug)) { - ppid = waitdaemon(30); - if (ppid < 0) { - warn("could not become daemon"); - pidfile_remove(pfh); - exit(1); - } - } else if (Debug) + if (!Foreground && !Debug) + ppipe_w = waitdaemon(30); + else if (Debug) setlinebuf(stdout); kq = kqueue(); @@ -868,9 +863,14 @@ main(int argc, char *argv[]) for (;;) { if (needdofsync) { dofsync(); - if (ppid != -1) { - kill(ppid, SIGALRM); - ppid = -1; + if (ppipe_w != -1) { + /* + * Close our end of the pipe so our + * parent knows that we have finished + * initialization. + */ + (void)close(ppipe_w); + ppipe_w = -1; } } if (kevent(kq, NULL, 0, &ev, 1, NULL) == -1) { @@ -3237,64 +3237,52 @@ markit(void) /* * fork off and become a daemon, but wait for the child to come online * before returning to the parent, or we get disk thrashing at boot etc. - * Set a timer so we don't hang forever if it wedges. */ static int waitdaemon(int maxwait) { - int status; - pid_t pid, childpid; + struct pollfd pollfd; + int events, pipefd[2], status; + pid_t pid; - switch (childpid = fork()) { - case -1: - return (-1); - case 0: - break; - default: - signal(SIGALRM, timedout); - alarm(maxwait); - while ((pid = wait3(&status, 0, NULL)) != -1) { + if (pipe(pipefd) == -1) { + warn("failed to daemonize, pipe"); + die(0); + } + pid = fork(); + if (pid == -1) { + warn("failed to daemonize, fork"); + die(0); + } else if (pid > 0) { + close(pipefd[1]); + pollfd.fd = pipefd[0]; + pollfd.events = POLLHUP; + events = poll(&pollfd, 1, maxwait * 1000); + if (events == -1) + err(1, "failed to daemonize, poll"); + else if (events == 0) + errx(1, "timed out waiting for child"); + if (waitpid(pid, &status, WNOHANG) > 0) { if (WIFEXITED(status)) errx(1, "child pid %d exited with return code %d", - pid, WEXITSTATUS(status)); + pid, WEXITSTATUS(status)); if (WIFSIGNALED(status)) errx(1, "child pid %d exited on signal %d%s", - pid, WTERMSIG(status), - WCOREDUMP(status) ? " (core dumped)" : - ""); - if (pid == childpid) /* it's gone... */ - break; + pid, WTERMSIG(status), + WCOREDUMP(status) ? " (core dumped)" : ""); } exit(0); } - - if (setsid() == -1) - return (-1); - + close(pipefd[0]); + if (setsid() == -1) { + warn("failed to daemonize, setsid"); + die(0); + } (void)chdir("/"); (void)dup2(nulldesc, STDIN_FILENO); (void)dup2(nulldesc, STDOUT_FILENO); (void)dup2(nulldesc, STDERR_FILENO); - return (getppid()); -} - -/* - * We get a SIGALRM from the child when it's running and finished doing it's - * fsync()'s or O_SYNC writes for all the boot messages. - * - * We also get a signal from the kernel if the timer expires, so check to - * see what happened. - */ -static void -timedout(int sig __unused) -{ - int left; - left = alarm(0); - signal(SIGALRM, SIG_DFL); - if (left == 0) - errx(1, "timed out waiting for child"); - else - _exit(0); + return (pipefd[1]); } /*