git: fe258f23ef36 - main - Save on getpid in setproctitle by supporting -1 as curproc.

Mateusz Guzik mjg at FreeBSD.org
Sat Jan 16 08:38:44 UTC 2021


The branch main has been updated by mjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=fe258f23ef3612f8230c4ed82f1efa511cc0524f

commit fe258f23ef3612f8230c4ed82f1efa511cc0524f
Author:     Mateusz Guzik <mjg at FreeBSD.org>
AuthorDate: 2021-01-16 08:26:17 +0000
Commit:     Mateusz Guzik <mjg at FreeBSD.org>
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));


More information about the dev-commits-src-all mailing list