diff options
author | Kamil Rytarowski <krytarowski@users.noreply.github.com> | 2019-06-11 06:04:16 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2019-06-11 12:04:16 +0800 |
commit | 1087e2418a35cccf6cdad6c2207308a915cf8a1a (patch) | |
tree | 12c2a9934b272d4b2217e92e039dd4781b1a9968 | |
parent | f5df901843097d20246a3171dac3e0ea670f974e (diff) | |
download | psutil-1087e2418a35cccf6cdad6c2207308a915cf8a1a.tar.gz |
NetBSD fixes (#1526)
* Fix psutil_get_cmd_args() for NetBSD
Do not overallocate the buffer prompting for KERN_ARGMAX.
It also fixes the code as KERN_ARGMAX was received into a size_t
type, while kernel returned int. It caused argmax to contain
garbage and allocation was randomly crashing with new jemalloc in
the basesystem.
New code prompts for exact buffer size before allocation and
stores argv[] inside it.
Bug investigated by Leonardo Taccari.
* Stop including sys/user.h for NetBSD
This header was removed from the OS as it was empty.
-rw-r--r-- | psutil/_psutil_bsd.c | 2 | ||||
-rw-r--r-- | psutil/arch/netbsd/specific.c | 28 |
2 files changed, 13 insertions, 17 deletions
diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index efb933fb..0f899ef5 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -30,7 +30,9 @@ #include <sys/types.h> #include <sys/param.h> #include <sys/sysctl.h> +#if !defined(__NetBSD__) #include <sys/user.h> +#endif #include <sys/proc.h> #include <sys/file.h> #include <sys/socket.h> diff --git a/psutil/arch/netbsd/specific.c b/psutil/arch/netbsd/specific.c index cab60d60..195896f2 100644 --- a/psutil/arch/netbsd/specific.c +++ b/psutil/arch/netbsd/specific.c @@ -22,7 +22,6 @@ #include <sys/types.h> #include <sys/param.h> #include <sys/sysctl.h> -#include <sys/user.h> #include <sys/proc.h> #include <sys/swap.h> // for swap_mem #include <signal.h> @@ -313,40 +312,35 @@ psutil_get_proc_list(kinfo_proc **procList, size_t *procCount) { char * psutil_get_cmd_args(pid_t pid, size_t *argsize) { int mib[4]; - ssize_t st; - size_t argmax; - size_t size; - char *procargs = NULL; + int st; + size_t len; + char *procargs; mib[0] = CTL_KERN; - mib[1] = KERN_ARGMAX; + mib[1] = KERN_PROC_ARGS; + mib[2] = pid; + mib[3] = KERN_PROC_ARGV; + len = 0; - size = sizeof(argmax); - st = sysctl(mib, 2, &argmax, &size, NULL, 0); + st = sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0); if (st == -1) { PyErr_SetFromErrno(PyExc_OSError); return NULL; } - procargs = (char *)malloc(argmax); + procargs = (char *)malloc(len); if (procargs == NULL) { PyErr_NoMemory(); return NULL; } - - mib[0] = CTL_KERN; - mib[1] = KERN_PROC_ARGS; - mib[2] = pid; - mib[3] = KERN_PROC_ARGV; - - st = sysctl(mib, 4, procargs, &argmax, NULL, 0); + st = sysctl(mib, __arraycount(mib), procargs, &len, NULL, 0); if (st == -1) { free(procargs); PyErr_SetFromErrno(PyExc_OSError); return NULL; } - *argsize = argmax; + *argsize = len; return procargs; } |