From nobody Wed Dec 27 06:08:10 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 4T0LmB39nMz55Bgw; Wed, 27 Dec 2023 06:08:10 +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 4T0LmB2bjJz3JHS; Wed, 27 Dec 2023 06:08:10 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1703657290; 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=CA7k9UsblQUi/sBgIh0bg8gjF6TmX4ac9Bh2NcRK+8g=; b=qA2K+Yevtb4voB2NnLpUqudXgLI+1FrOv26jwDrsejqQAYnjipsW8vbDRZLmQLShCWagQc t3HNTgL+YYx77xSshWwoMBDa6pzae6/jowd3pmJlSalQXbzCX2kqugmkZONmxUCjzeNWd/ cmLGNsV9Nue3VGnWbME3ErftzxcRFf7AQPaBghmWNX3rk78lKPJsin3iBZ+pWIQIaJ+/z0 7CBqlEf7eFzdPYLyVIOA/59C10HX3bTdomH4XuHihqo7Db24e+8PVXucqLOmsIvpaHYbLA zem0reBed0xDOvfLKRBaPGVskT/yVmjTogE9oiLSFxFVsuja2xv3JK5BdGqz0A== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1703657290; a=rsa-sha256; cv=none; b=q7cLhYRDD1h1cW7qjKnOXw6BPRxtbSqyX7zsW5sqV+Vg97YHh+aIm4DSaLx67pvzD0QOUj 0WGoG1nVF96WrL6/JdPO/iesvIMAudCQyFK04v1TShEXbNcbHj+SMBUSlv7bGepR9Z1UpI c+Wi/n3qKYrlwBfjoRwwM8vUVZHusU7DV9V+Lhe7NTFSGCIHLyGqOZ3cLYKFuKwhkQySRb AhrxTQXMMOqTqrkwm7BjF9Mt4KL7VPyKaahIsWvOkyEwpkei1mkmO9w+Vgdp2oivcXvjzO hJXFZrGJzGeKavcrqdQKSXftMny20/an8u+bt/2uXOVEopIaSaBFPURcN25BKg== 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=1703657290; 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=CA7k9UsblQUi/sBgIh0bg8gjF6TmX4ac9Bh2NcRK+8g=; b=u3BjhE9U/Hpt745/vMnuxvXnMdFVN2YQ5UyWvURJ5Y1Pfv4FvCOewe4yDk46xQ399QXtsJ tFYj3AFM+8TM/RYLpFVMgwWKU49iU7S++c24RRbV2VxC2QP/EdSH0WcBO67Rg5rHeSbCQR 6vAjAcoN8tqT6Afq8PQ0KnVy1Q4E5WPJFMWzzHxQsXor0mI70DHZ1AsfIosPSKa3ff8qlz VJZv3fFsni3Td9hxEfmjuN3d3dyst6Ts5fi8dzXWeGHl3jzXVEH3JHTvdGtTcNU3IJdB6L RDxcqz3KyZeAogCLubxXXU7eDwUNItRC3KCOvwNi2OjdH6DIbIn/kBITBSCYxQ== 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 4T0LmB1jk3zC0S; Wed, 27 Dec 2023 06:08:10 +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 3BR68AUd024933; Wed, 27 Dec 2023 06:08:10 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BR68AOs024930; Wed, 27 Dec 2023 06:08:10 GMT (envelope-from git) Date: Wed, 27 Dec 2023 06:08:10 GMT Message-Id: <202312270608.3BR68AOs024930@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: 24fd3e969ebe - main - daemon: move buffer into daemon_state 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: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 24fd3e969ebe2ed44bc04f5ccc4db21cf02184d5 Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=24fd3e969ebe2ed44bc04f5ccc4db21cf02184d5 commit 24fd3e969ebe2ed44bc04f5ccc4db21cf02184d5 Author: Ihor Antonov AuthorDate: 2023-12-27 06:07:25 +0000 Commit: Kyle Evans CommitDate: 2023-12-27 06:07:25 +0000 daemon: move buffer into daemon_state There is no reason for a buffer in listen_child() to be a static function variable. The buffer and its position are parts of the daemon state and should live together with the rest of the state variables. Reviewed by: cperciva, kevans --- usr.sbin/daemon/daemon.c | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/usr.sbin/daemon/daemon.c b/usr.sbin/daemon/daemon.c index ed6239b9fb93..e9c4bf05504c 100644 --- a/usr.sbin/daemon/daemon.c +++ b/usr.sbin/daemon/daemon.c @@ -66,6 +66,8 @@ enum daemon_mode { }; struct daemon_state { + unsigned char buf[LBUF_SIZE]; + size_t pos; int pipe_fd[2]; char **argv; const char *child_pidfile; @@ -584,38 +586,36 @@ restrict_process(const char *user) static bool listen_child(struct daemon_state *state) { - static unsigned char buf[LBUF_SIZE]; - static size_t bytes_read = 0; ssize_t rv; assert(state != NULL); - assert(bytes_read < LBUF_SIZE - 1); + assert(state->pos < LBUF_SIZE - 1); - rv = read(state->pipe_fd[0], buf + bytes_read, LBUF_SIZE - bytes_read - 1); + rv = read(state->pipe_fd[0], state->buf + state->pos, LBUF_SIZE - state->pos - 1); if (rv > 0) { unsigned char *cp; - bytes_read += rv; - assert(bytes_read <= LBUF_SIZE - 1); + state->pos += rv; + assert(state->pos <= LBUF_SIZE - 1); /* Always NUL-terminate just in case. */ - buf[LBUF_SIZE - 1] = '\0'; + state->buf[LBUF_SIZE - 1] = '\0'; /* * Chomp line by line until we run out of buffer. * This does not take NUL characters into account. */ - while ((cp = memchr(buf, '\n', bytes_read)) != NULL) { - size_t bytes_line = cp - buf + 1; - assert(bytes_line <= bytes_read); - do_output(buf, bytes_line, state); - bytes_read -= bytes_line; - memmove(buf, cp + 1, bytes_read); + while ((cp = memchr(state->buf, '\n', state->pos)) != NULL) { + size_t bytes_line = cp - state->buf + 1; + assert(bytes_line <= state->pos); + do_output(state->buf, bytes_line, state); + state->pos -= bytes_line; + memmove(state->buf, cp + 1, state->pos); } /* Wait until the buffer is full. */ - if (bytes_read < LBUF_SIZE - 1) { + if (state->pos < LBUF_SIZE - 1) { return true; } - do_output(buf, bytes_read, state); - bytes_read = 0; + do_output(state->buf, state->pos, state); + state->pos = 0; return true; } else if (rv == -1) { /* EINTR should trigger another read. */ @@ -627,9 +627,9 @@ listen_child(struct daemon_state *state) } } /* Upon EOF, we have to flush what's left of the buffer. */ - if (bytes_read > 0) { - do_output(buf, bytes_read, state); - bytes_read = 0; + if (state->pos > 0) { + do_output(state->buf, state->pos, state); + state->pos = 0; } return false; } @@ -685,6 +685,8 @@ static void daemon_state_init(struct daemon_state *state) { *state = (struct daemon_state) { + .buf = {0}, + .pos = 0, .pipe_fd = { -1, -1 }, .argv = NULL, .parent_pidfh = NULL,