From nobody Thu Sep 28 15:52:34 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 4RxJ032RzGz4tyQx; Thu, 28 Sep 2023 15:52:35 +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 4RxJ030lkPz4KgC; Thu, 28 Sep 2023 15:52:35 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1695916355; 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=gx66RsGiBI+lLnIrrgGjvWw8O3jpF78eaBTd6FohqGQ=; b=lUsU/XVEprW9Qz2bxSGBVc5WgFJhYhN6TWFsv8qWvMaqMmKCTBhgLyVqY3z3kvzhYyMz7h S2Rr3WO1SamW+phSpRAjhaF6ynaYYrBqg8bwzwprg8Tjkl8W044gOz0DF3RROu0clrEo3c jASo1o1PT4afahRr48WI3e09B1Ehou53FR0PqtcNNO5+IHCkroyV/wPfjllhOylI2rDBMy ry7wF4d4VbFHaOv1+PfHqZZ1M1yDCWUUwz6itqjmoRslMUfUuwFTBISiADW9+vwnNnNxLy N1GEF+fAL56TCWIwj7muoqJvPYn0u9bvMapEmSsHOSOCRNMqdNAVIJBgxO0QLQ== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1695916355; a=rsa-sha256; cv=none; b=qfZf45autHad1ol6DH2DYf1GRrae5cGyjO99kLBkrfBbqKJsVdTirKF09X/agDgKPhm8yY q2sNE1QhIMyugpZJmY0Lyo/fauY8w0ggOAQtUl13M32ORDQfzFzFSCFdFNu0MIpZulUEb1 jA1i5uT8Iwbtkvf49A+D9G3D4r5PPJhI8gZggTOJh0cAibQVNmtAMc27QaFTXD8VC74L9e x2jUqg7VzceLOYCXVOWnqoTGcr7Zet0QSNFyaYbuZ5bGr3ExVGUcqmmxDe5I2/5XguALR1 6POLx8iSvZkEncusZJ0YWRrinoJddHnf6K6WmvjH77X+OkLFsjQZEXsBxKe1lw== 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=1695916355; 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=gx66RsGiBI+lLnIrrgGjvWw8O3jpF78eaBTd6FohqGQ=; b=Z+xEdNVCsyBs8Ge5gfL0lAy3HkXjHIThvu7oWZd8fwvVWpnQda6SBRstBXVwBERPAWFxql y1DZBC2pI3anGIYlO4MKRjZNn8gf9GlgGVYXQXsG5x6Xdh9tzjWD48rHzTIrA06idKlclz BMd2ylQW4wz0XcjpAmfHFaMWi+QFPzCiWFXus5TgPQM11kYnQ9MTQBIZU801X0RBzcZC6A jLDE9paVhFo+dHE8b7tF0cMD8QzpUrWh4rThH42q3N+7DFjA5AAlCgYShtKW5LOhM6qrEs tnaenjMIx2i9Rod2U7AKA8lpoYmt4LWwU9ZBgUQVxSKvAXMeTfEAhqkc+8QQbQ== 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 4RxJ026zJnzD68; Thu, 28 Sep 2023 15:52:34 +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 38SFqYBI022028; Thu, 28 Sep 2023 15:52:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 38SFqYHt022025; Thu, 28 Sep 2023 15:52:34 GMT (envelope-from git) Date: Thu, 28 Sep 2023 15:52:34 GMT Message-Id: <202309281552.38SFqYHt022025@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: fe233250d6fd - main - syslogd: Do not reap child processes 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: fe233250d6fd0d4b915c89244f2b1f79cb45284e Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=fe233250d6fd0d4b915c89244f2b1f79cb45284e commit fe233250d6fd0d4b915c89244f2b1f79cb45284e Author: Jake Freeland AuthorDate: 2023-09-01 02:50:19 +0000 Commit: Mark Johnston CommitDate: 2023-09-28 15:51:53 +0000 syslogd: Do not reap child processes Use the SA_NOCLDWAIT sigaction() flag to disable the creation of zombie processes. This leaves no reason to wait() on children, so remove the reaping routine. Previously, deadq entries were removed in reapchild(). Move deadq removal into markit() after SIGKILL is sent. Reviewed by: markj MFC after: 3 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D41415 --- usr.sbin/syslogd/syslogd.c | 101 +++++---------------------------------------- 1 file changed, 11 insertions(+), 90 deletions(-) diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index fff45372fda8..c20928d48495 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -441,7 +441,6 @@ static void cfline(const char *, const char *, const char *, const char *); static const char *cvthname(struct sockaddr *); static void deadq_enter(int); static void deadq_remove(struct deadq_entry *); -static bool deadq_removebypid(pid_t); static int decode(const char *, const CODE *); static void die(int) __dead2; static void dofsync(void); @@ -453,7 +452,6 @@ static void init(bool); static void logerror(const char *); static void logmsg(int, const struct logtime *, const char *, const char *, const char *, const char *, const char *, const char *, int); -static void log_deadchild(pid_t, int, const char *); static void markit(void); static struct socklist *socksetup(struct addrinfo *, const char *, mode_t); static int socklist_recv_file(struct socklist *); @@ -466,7 +464,6 @@ static int prop_filter_compile(struct prop_filter *pfilter, static void parsemsg(const char *, char *); static void printsys(char *); static int p_open(const char *, pid_t *); -static void reapchild(int); static const char *ttymsg_check(struct iovec *, int, char *, int); static void usage(void); static bool validate(struct sockaddr *, const char *); @@ -598,6 +595,7 @@ addfile(int fd) int main(int argc, char *argv[]) { + struct sigaction act = { }; sigset_t sigset = { }; struct kevent ev; struct socklist *sl; @@ -846,6 +844,14 @@ main(int argc, char *argv[]) } (void)alarm(TIMERINTVL); + /* Do not create zombie processes. */ + act.sa_flags = SA_NOCLDWAIT; + if (sigaction(SIGCHLD, &act, NULL) == -1) { + warn("failed to apply signal handler"); + pidfile_remove(pfh); + exit(1); + } + /* tuck my process id away */ pidfile_write(pfh); @@ -884,9 +890,6 @@ main(int argc, char *argv[]) case SIGALRM: markit(); break; - case SIGCHLD: - reapchild(ev.ident); - break; } break; } @@ -2256,38 +2259,6 @@ ttymsg_check(struct iovec *iov, int iovcnt, char *line, int tmout) return (ttymsg(iov, iovcnt, line, tmout)); } -static void -reapchild(int signo __unused) -{ - int status; - pid_t pid, pipe_pid; - struct filed *f; - - while ((pid = wait3(&status, WNOHANG, (struct rusage *)NULL)) > 0) { - /* First, look if it's a process from the dead queue. */ - if (deadq_removebypid(pid)) - continue; - - /* Now, look in list of active processes. */ - STAILQ_FOREACH(f, &fhead, next) { - if (f->f_type == F_PIPE) { - if (pdgetpid(f->fu_pipe_pd, &pipe_pid) == -1) { - dprintf("Failed to query PID: %s\n", - strerror(errno)); - continue; - } - if (pipe_pid != pid) - continue; - /* Do not enter into deadq. */ - f->fu_pipe_pd = -1; - close_filed(f); - log_deadchild(pid, status, f->fu_pipe_pname); - break; - } - } - } -} - /* * Return a printable representation of a host address. */ @@ -3228,19 +3199,11 @@ markit(void) switch (dq->dq_timeout) { case 0: /* Already signalled once, try harder now. */ - if (pdkill(dq->dq_procdesc, SIGKILL) != 0) - (void)deadq_remove(dq); + (void)pdkill(dq->dq_procdesc, SIGKILL); + (void)deadq_remove(dq); break; case 1: - /* - * Timed out on dead queue, send terminate - * signal. Note that we leave the removal - * from the dead queue to reapchild(), which - * will also log the event (unless the process - * didn't even really exist, in case we simply - * drop it from the dead queue). - */ if (pdkill(dq->dq_procdesc, SIGTERM) != 0) (void)deadq_remove(dq); else @@ -3715,48 +3678,6 @@ deadq_remove(struct deadq_entry *dq) free(dq); } -static bool -deadq_removebypid(pid_t pid) -{ - struct deadq_entry *dq; - pid_t dq_pid; - - TAILQ_FOREACH(dq, &deadq_head, dq_entries) { - if (pdgetpid(dq->dq_procdesc, &dq_pid) == -1) { - dprintf("Failed to query PID: %s\n", strerror(errno)); - continue; - } - if (dq_pid == pid) { - deadq_remove(dq); - return (true); - } - } - return (false); -} - -static void -log_deadchild(pid_t pid, int status, const char *name) -{ - int code; - char buf[256]; - const char *reason; - - errno = 0; /* Keep strerror() stuff out of logerror messages. */ - if (WIFSIGNALED(status)) { - reason = "due to signal"; - code = WTERMSIG(status); - } else { - reason = "with status"; - code = WEXITSTATUS(status); - if (code == 0) - return; - } - (void)snprintf(buf, sizeof buf, - "Logging subprocess %d (%s) exited %s %d.", - pid, name, reason, code); - logerror(buf); -} - static struct socklist * socksetup(struct addrinfo *ai, const char *name, mode_t mode) {