Save on getpid in setproctitle by supporting -1 as curproc.

This commit is contained in:
Mateusz Guzik 2021-01-16 09:26:17 +01:00
parent 2968dde3de
commit fe258f23ef
2 changed files with 29 additions and 5 deletions

View File

@ -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;
}
}

View File

@ -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));