aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Guzik <mjg@FreeBSD.org>2021-01-16 08:26:17 +0000
committerMateusz Guzik <mjg@FreeBSD.org>2021-01-16 08:36:54 +0000
commitfe258f23ef3612f8230c4ed82f1efa511cc0524f (patch)
treef364895c667e068e2c6f0bc98299aafe18cb981f
parent2968dde3de6f6274517904add6a0a8e2e3f9662b (diff)
downloadsrc-fe258f23ef3612f8230c4ed82f1efa511cc0524f.tar.gz
src-fe258f23ef3612f8230c4ed82f1efa511cc0524f.zip
Save on getpid in setproctitle by supporting -1 as curproc.
-rw-r--r--lib/libc/gen/setproctitle.c28
-rw-r--r--sys/kern/kern_proc.c6
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));