From owner-dev-commits-src-main@freebsd.org Sat Jan 16 08:38:44 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id D4A524D865A; Sat, 16 Jan 2021 08:38:44 +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 4DHs045ZvNz3JpT; Sat, 16 Jan 2021 08:38:44 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 A3F4E4037; Sat, 16 Jan 2021 08:38:44 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 10G8ciHP031608; Sat, 16 Jan 2021 08:38:44 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10G8ci4b031607; Sat, 16 Jan 2021 08:38:44 GMT (envelope-from git) Date: Sat, 16 Jan 2021 08:38:44 GMT Message-Id: <202101160838.10G8ci4b031607@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mateusz Guzik Subject: git: fe258f23ef36 - main - Save on getpid in setproctitle by supporting -1 as curproc. MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mjg X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: fe258f23ef3612f8230c4ed82f1efa511cc0524f Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 16 Jan 2021 08:38:44 -0000 The branch main has been updated by mjg: URL: https://cgit.FreeBSD.org/src/commit/?id=fe258f23ef3612f8230c4ed82f1efa511cc0524f commit fe258f23ef3612f8230c4ed82f1efa511cc0524f Author: Mateusz Guzik AuthorDate: 2021-01-16 08:26:17 +0000 Commit: Mateusz Guzik CommitDate: 2021-01-16 08:36:54 +0000 Save on getpid in setproctitle by supporting -1 as curproc. --- lib/libc/gen/setproctitle.c | 28 ++++++++++++++++++++++++---- sys/kern/kern_proc.c | 6 +++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/libc/gen/setproctitle.c b/lib/libc/gen/setproctitle.c index b975fd72b649..991af38565cd 100644 --- a/lib/libc/gen/setproctitle.c +++ b/lib/libc/gen/setproctitle.c @@ -184,8 +184,18 @@ setproctitle_fast(const char *fmt, ...) oid[0] = CTL_KERN; oid[1] = KERN_PROC; oid[2] = KERN_PROC_ARGS; - oid[3] = getpid(); - sysctl(oid, 4, 0, 0, "", 0); + oid[3] = -1; + if (sysctl(oid, 4, 0, 0, "", 0) != 0) { + /* + * Temporary compat for kernels which don't support + * passing -1. + */ + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = getpid(); + sysctl(oid, 4, 0, 0, "", 0); + } fast_update = 1; } } @@ -206,8 +216,18 @@ setproctitle(const char *fmt, ...) oid[0] = CTL_KERN; oid[1] = KERN_PROC; oid[2] = KERN_PROC_ARGS; - oid[3] = getpid(); - sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1); + oid[3] = -1; + if (sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1) != 0) { + /* + * Temporary compat for kernels which don't support + * passing -1. + */ + oid[0] = CTL_KERN; + oid[1] = KERN_PROC; + oid[2] = KERN_PROC_ARGS; + oid[3] = getpid(); + sysctl(oid, 4, 0, 0, buf, strlen(buf) + 1); + } fast_update = 0; } } diff --git a/sys/kern/kern_proc.c b/sys/kern/kern_proc.c index 269705205fbc..ae80ba9ed5ed 100644 --- a/sys/kern/kern_proc.c +++ b/sys/kern/kern_proc.c @@ -2088,12 +2088,16 @@ sysctl_kern_proc_args(SYSCTL_HANDLER_ARGS) if (namelen != 1) return (EINVAL); + p = curproc; pid = (pid_t)name[0]; + if (pid == -1) { + pid = p->p_pid; + } + /* * If the query is for this process and it is single-threaded, there * is nobody to modify pargs, thus we can just read. */ - p = curproc; if (pid == p->p_pid && p->p_numthreads == 1 && req->newptr == NULL && (pa = p->p_args) != NULL) return (SYSCTL_OUT(req, pa->ar_args, pa->ar_length));