diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-24 22:25:26 +0100 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2020-01-24 22:25:26 +0100 |
commit | 0632484d75f685fdbfeeb351054a7349210edde7 (patch) | |
tree | 56a7ea8e107e44497c85241070fc035b6a329859 | |
parent | 98789a5048862bef169073e658a7bf82af031389 (diff) | |
parent | 4bebbe54b373b0c8150e649bdd7ff5e6046f3cd8 (diff) | |
download | psutil-0632484d75f685fdbfeeb351054a7349210edde7.tar.gz |
Merge branch 'win-pid-type' of github.com:giampaolo/psutil into win-pid-type
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | psutil/_psutil_bsd.c | 63 | ||||
-rw-r--r-- | psutil/_psutil_common.c | 2 | ||||
-rw-r--r-- | psutil/_psutil_common.h | 11 | ||||
-rw-r--r-- | psutil/_psutil_posix.c | 4 | ||||
-rw-r--r-- | psutil/_psutil_windows.c | 56 | ||||
-rw-r--r-- | psutil/arch/freebsd/proc_socks.c | 6 | ||||
-rw-r--r-- | psutil/arch/freebsd/specific.c | 22 | ||||
-rw-r--r-- | psutil/arch/freebsd/sys_socks.c | 16 |
9 files changed, 98 insertions, 83 deletions
@@ -15,7 +15,6 @@ DEPS = \ ipaddress \ mock==1.0.1 \ pyperf \ - readline \ requests \ setuptools \ sphinx \ diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index f58cc72f..0f36c0a0 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -142,9 +142,9 @@ psutil_pids(PyObject *self, PyObject *args) { orig_address = proclist; // save so we can free it after we're done for (idx = 0; idx < num_processes; idx++) { #ifdef PSUTIL_FREEBSD - py_pid = Py_BuildValue("i", proclist->ki_pid); + py_pid = PyLong_FromPid(proclist->ki_pid); #elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD) - py_pid = Py_BuildValue("i", proclist->p_pid); + py_pid = PyLong_FromPid(proclist->p_pid); #endif if (!py_pid) goto error; @@ -190,7 +190,7 @@ psutil_boot_time(PyObject *self, PyObject *args) { */ static PyObject * psutil_proc_oneshot_info(PyObject *self, PyObject *args) { - long pid; + pid_t pid; long rss; long vms; long memtext; @@ -201,9 +201,10 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) { long pagesize = sysconf(_SC_PAGESIZE); char str[1000]; PyObject *py_name; + PyObject *py_ppid; PyObject *py_retlist; - if (! PyArg_ParseTuple(args, "l", &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; @@ -265,16 +266,25 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) { oncpu = -1; #endif +#ifdef PSUTIL_FREEBSD + py_ppid = PyLong_FromPid(kp.ki_ppid); +#elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD) + py_ppid = PyLong_FromPid(kp.p_ppid); +#else + py_ppid = Py_BuildfValue(-1); +#endif + if (! py_ppid) + return NULL; + // Return a single big tuple with all process info. py_retlist = Py_BuildValue( #if defined(__FreeBSD_version) && __FreeBSD_version >= 1200031 - "(lillllllLdllllddddlllllbO)", + "(OillllllLdllllddddlllllbO)", #else - "(lillllllidllllddddlllllbO)", + "(OillllllidllllddddlllllbO)", #endif #ifdef PSUTIL_FREEBSD - // - (long)kp.ki_ppid, // (long) ppid + py_ppid, // (pid_t) ppid (int)kp.ki_stat, // (int) status // UIDs (long)kp.ki_ruid, // (long) real uid @@ -307,8 +317,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) { // others oncpu, // (int) the CPU we are on #elif defined(PSUTIL_OPENBSD) || defined(PSUTIL_NETBSD) - // - (long)kp.p_ppid, // (long) ppid + py_ppid, // (pid_t) ppid (int)kp.p_stat, // (int) status // UIDs (long)kp.p_ruid, // (long) real uid @@ -347,6 +356,7 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) { ); Py_DECREF(py_name); + Py_DECREF(py_ppid); return py_retlist; } @@ -356,11 +366,11 @@ psutil_proc_oneshot_info(PyObject *self, PyObject *args) { */ static PyObject * psutil_proc_name(PyObject *self, PyObject *args) { - long pid; + pid_t pid; kinfo_proc kp; char str[1000]; - if (! PyArg_ParseTuple(args, "l", &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; @@ -379,10 +389,10 @@ psutil_proc_name(PyObject *self, PyObject *args) { */ static PyObject * psutil_proc_cmdline(PyObject *self, PyObject *args) { - long pid; + pid_t pid; PyObject *py_retlist = NULL; - if (! PyArg_ParseTuple(args, "l", &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; py_retlist = psutil_get_cmdline(pid); if (py_retlist == NULL) @@ -452,7 +462,7 @@ psutil_cpu_times(PyObject *self, PyObject *args) { #if (defined(__FreeBSD_version) && __FreeBSD_version >= 800000) || PSUTIL_OPENBSD || defined(PSUTIL_NETBSD) static PyObject * psutil_proc_open_files(PyObject *self, PyObject *args) { - long pid; + pid_t pid; int i; int cnt; int regular; @@ -467,7 +477,7 @@ psutil_proc_open_files(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - if (! PyArg_ParseTuple(args, "l", &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) goto error; if (psutil_kinfo_proc(pid, &kipp) == -1) goto error; @@ -794,6 +804,7 @@ psutil_users(PyObject *self, PyObject *args) { PyObject *py_tty = NULL; PyObject *py_hostname = NULL; PyObject *py_tuple = NULL; + PyObject *py_pid = NULL; if (py_retlist == NULL) return NULL; @@ -831,7 +842,7 @@ psutil_users(PyObject *self, PyObject *args) { #ifdef PSUTIL_OPENBSD -1 // process id (set to None later) #else - ut.ut_pid // process id + ut.ut_pid // TODO: use PyLong_FromPid #endif ); if (!py_tuple) { @@ -864,17 +875,21 @@ psutil_users(PyObject *self, PyObject *args) { py_hostname = PyUnicode_DecodeFSDefault(utx->ut_host); if (! py_hostname) goto error; +#ifdef PSUTIL_OPENBSD + py_pid = Py_BuildValue("i", -1); // set to None later +#else + py_pid = PyLong_FromPid(utx->ut_pid); +#endif + if (! py_pid) + goto error; + py_tuple = Py_BuildValue( - "(OOOfi)", + "(OOOfO)", py_username, // username py_tty, // tty py_hostname, // hostname (float)utx->ut_tv.tv_sec, // start time -#ifdef PSUTIL_OPENBSD - -1 // process id (set to None later) -#else - utx->ut_pid // process id -#endif + py_pid // process id ); if (!py_tuple) { @@ -889,6 +904,7 @@ psutil_users(PyObject *self, PyObject *args) { Py_CLEAR(py_tty); Py_CLEAR(py_hostname); Py_CLEAR(py_tuple); + Py_CLEAR(py_pid); } endutxent(); @@ -900,6 +916,7 @@ error: Py_XDECREF(py_tty); Py_XDECREF(py_hostname); Py_XDECREF(py_tuple); + Py_XDECREF(py_pid); Py_DECREF(py_retlist); return NULL; } diff --git a/psutil/_psutil_common.c b/psutil/_psutil_common.c index e433f797..5dfa723f 100644 --- a/psutil/_psutil_common.c +++ b/psutil/_psutil_common.c @@ -170,9 +170,11 @@ Py_PidConverter(PyObject *arg, void *addr) { if ((sizeof(pid_t) == sizeof(int)) || (sizeof(pid_t) == sizeof(long))) { *((pid_t *)addr) = PyLong_AsLong(arg); } +#ifndef PSUTIL_WINDOWS else if (sizeof(pid_t) == sizeof(long long)) { *((pid_t *)addr) = PyLong_AsLongLong(arg); } +#endif else { PyErr_SetString(PyExc_ValueError, "can't get size of pid_t"); } diff --git a/psutil/_psutil_common.h b/psutil/_psutil_common.h index a99b6337..7eb0e8bd 100644 --- a/psutil/_psutil_common.h +++ b/psutil/_psutil_common.h @@ -22,17 +22,6 @@ static const int PSUTIL_CONN_NONE = 128; #if PY_MAJOR_VERSION < 3 PyObject* PyUnicode_DecodeFSDefault(char *s); PyObject* PyUnicode_DecodeFSDefaultAndSize(char *s, Py_ssize_t size); -#endif - -// Python 2 compatibility for PyArg_ParseTuple pid arg type handling. -#if PY_MAJOR_VERSION == 2 - // XXX: not bullet proof (long long case is missing). - #if PSUTIL_SIZEOF_PID_T == 4 - #define _Py_PARSE_PID "i" - #else - #define _Py_PARSE_PID "l" - #endif - PyObject *PyLong_FromPid(pid_t pid); #endif diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index f4ecbd93..792264de 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -135,7 +135,7 @@ psutil_posix_getpriority(PyObject *self, PyObject *args) { int priority; errno = 0; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; #ifdef PSUTIL_OSX @@ -158,7 +158,7 @@ psutil_posix_setpriority(PyObject *self, PyObject *args) { int priority; int retval; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "i", &pid, &priority)) + if (! PyArg_ParseTuple(args, "O&i", Py_PidConverter, &pid, &priority)) return NULL; #ifdef PSUTIL_OSX diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 94508134..1dee658c 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -114,7 +114,7 @@ psutil_pid_exists(PyObject *self, PyObject *args) { pid_t pid; int status; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; status = psutil_pid_is_running(pid); @@ -171,7 +171,7 @@ psutil_proc_kill(PyObject *self, PyObject *args) { HANDLE hProcess; pid_t pid; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; if (pid == 0) return AccessDenied("automatically set for PID 0"); @@ -215,7 +215,7 @@ psutil_proc_wait(PyObject *self, PyObject *args) { pid_t pid; long timeout; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "l", &pid, &timeout)) + if (! PyArg_ParseTuple(args,"O&l", Py_PidConverter, &pid, &timeout)) return NULL; if (pid == 0) return AccessDenied("automatically set for PID 0"); @@ -285,7 +285,7 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) { HANDLE hProcess; FILETIME ftCreate, ftExit, ftKernel, ftUser; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); @@ -337,7 +337,7 @@ psutil_proc_create_time(PyObject *self, PyObject *args) { HANDLE hProcess; FILETIME ftCreate, ftExit, ftKernel, ftUser; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; // special case for PIDs 0 and 4, return system boot time @@ -403,8 +403,8 @@ psutil_proc_cmdline(PyObject *self, PyObject *args, PyObject *kwdict) { PyObject *py_usepeb = Py_True; static char *keywords[] = {"pid", "use_peb", NULL}; - if (!PyArg_ParseTupleAndKeywords(args, kwdict, _Py_PARSE_PID "|O", - keywords, &pid, &py_usepeb)) { + if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O&|O", keywords, + Py_PidConverter, &pid, &py_usepeb)) { return NULL; } if ((pid == 0) || (pid == 4)) @@ -429,7 +429,7 @@ psutil_proc_environ(PyObject *self, PyObject *args) { pid_t pid; int pid_return; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; if ((pid == 0) || (pid == 4)) return Py_BuildValue("s", ""); @@ -454,7 +454,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) { wchar_t exe[MAX_PATH]; unsigned int size = sizeof(exe); - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); @@ -489,7 +489,7 @@ psutil_proc_name(PyObject *self, PyObject *args) { PROCESSENTRY32W pentry; HANDLE hSnapShot; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, pid); if (hSnapShot == INVALID_HANDLE_VALUE) @@ -525,7 +525,7 @@ psutil_proc_memory_info(PyObject *self, PyObject *args) { pid_t pid; PROCESS_MEMORY_COUNTERS_EX cnt; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); @@ -641,7 +641,7 @@ psutil_proc_memory_uss(PyObject *self, PyObject *args) { PMEMORY_WORKING_SET_INFORMATION wsInfo; ULONG_PTR i; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_INFORMATION); if (hProcess == NULL) @@ -709,7 +709,7 @@ psutil_proc_cwd(PyObject *self, PyObject *args) { pid_t pid; int pid_return; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; pid_return = psutil_pid_is_running(pid); @@ -732,7 +732,7 @@ psutil_proc_suspend_or_resume(PyObject *self, PyObject *args) { HANDLE hProcess; PyObject* suspend; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "O", &pid, &suspend)) + if (! PyArg_ParseTuple(args, "O&O", Py_PidConverter, &pid, &suspend)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_SUSPEND_RESUME); @@ -768,7 +768,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) goto error; if (pid == 0) { // raise AD instead of returning 0 as procexp is able to @@ -866,7 +866,7 @@ psutil_proc_open_files(PyObject *self, PyObject *args) { DWORD access = PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION; PyObject *py_retlist; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; processHandle = psutil_handle_from_pid(pid, access); @@ -898,7 +898,7 @@ psutil_proc_username(PyObject *self, PyObject *args) { PyObject *py_domain = NULL; PyObject *py_tuple = NULL; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; processHandle = psutil_handle_from_pid( @@ -1015,7 +1015,7 @@ psutil_proc_priority_get(PyObject *self, PyObject *args) { DWORD priority; HANDLE hProcess; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); @@ -1044,7 +1044,7 @@ psutil_proc_priority_set(PyObject *self, PyObject *args) { HANDLE hProcess; DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "i", &pid, &priority)) + if (! PyArg_ParseTuple(args, "O&i", Py_PidConverter, &pid, &priority)) return NULL; hProcess = psutil_handle_from_pid(pid, access); if (hProcess == NULL) @@ -1072,7 +1072,7 @@ psutil_proc_io_priority_get(PyObject *self, PyObject *args) { DWORD IoPriority; NTSTATUS status; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); @@ -1105,7 +1105,7 @@ psutil_proc_io_priority_set(PyObject *self, PyObject *args) { NTSTATUS status; DWORD access = PROCESS_QUERY_INFORMATION | PROCESS_SET_INFORMATION; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "i", &pid, &prio)) + if (! PyArg_ParseTuple(args, "O&i", Py_PidConverter, &pid, &prio)) return NULL; hProcess = psutil_handle_from_pid(pid, access); @@ -1135,7 +1135,7 @@ psutil_proc_io_counters(PyObject *self, PyObject *args) { HANDLE hProcess; IO_COUNTERS IoCounters; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); if (NULL == hProcess) @@ -1168,7 +1168,7 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) { DWORD_PTR proc_mask; DWORD_PTR system_mask; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); if (hProcess == NULL) { @@ -1200,9 +1200,9 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) { DWORD_PTR mask; #ifdef _WIN64 - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "K", &pid, &mask)) + if (! PyArg_ParseTuple(args, "O&K", Py_PidConverter, &pid, &mask)) #else - if (! PyArg_ParseTuple(args, _Py_PARSE_PID "k", &pid, &mask)) + if (! PyArg_ParseTuple(args, "O&k", Py_PidConverter, &pid, &mask)) #endif { return NULL; @@ -1232,7 +1232,7 @@ psutil_proc_is_suspended(PyObject *self, PyObject *args) { PSYSTEM_PROCESS_INFORMATION process; PVOID buffer; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; if (! psutil_get_proc_info(pid, &process, &buffer)) return NULL; @@ -1391,7 +1391,7 @@ psutil_proc_num_handles(PyObject *self, PyObject *args) { HANDLE hProcess; DWORD handleCount; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) return NULL; hProcess = psutil_handle_from_pid(pid, PROCESS_QUERY_LIMITED_INFORMATION); if (NULL == hProcess) @@ -1449,7 +1449,7 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid)) + if (! PyArg_ParseTuple(args, "O&", Py_PidConverter, &pid)) goto error; hProcess = psutil_handle_from_pid(pid, access); if (NULL == hProcess) diff --git a/psutil/arch/freebsd/proc_socks.c b/psutil/arch/freebsd/proc_socks.c index a458a01e..b40888ee 100644 --- a/psutil/arch/freebsd/proc_socks.c +++ b/psutil/arch/freebsd/proc_socks.c @@ -179,7 +179,7 @@ psutil_search_tcplist(char *buf, struct kinfo_file *kif) { PyObject * psutil_proc_connections(PyObject *self, PyObject *args) { // Return connections opened by process. - long pid; + pid_t pid; int i; int cnt; struct kinfo_file *freep = NULL; @@ -202,8 +202,10 @@ psutil_proc_connections(PyObject *self, PyObject *args) { if (py_retlist == NULL) return NULL; - if (! PyArg_ParseTuple(args, "lOO", &pid, &py_af_filter, &py_type_filter)) + if (! PyArg_ParseTuple(args, "O&OO", Py_PidConverter, &pid, + &py_af_filter, &py_type_filter)) { goto error; + } if (!PySequence_Check(py_af_filter) || !PySequence_Check(py_type_filter)) { PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence"); goto error; diff --git a/psutil/arch/freebsd/specific.c b/psutil/arch/freebsd/specific.c index 90ea81e8..b32d4f19 100644 --- a/psutil/arch/freebsd/specific.c +++ b/psutil/arch/freebsd/specific.c @@ -44,7 +44,7 @@ int -psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc) { +psutil_kinfo_proc(pid_t pid, struct kinfo_proc *proc) { // Fills a kinfo_proc struct based on process pid. int mib[4]; size_t size; @@ -180,7 +180,7 @@ psutil_get_proc_list(struct kinfo_proc **procList, size_t *procCount) { * 1 for insufficient privileges. */ static char -*psutil_get_cmd_args(long pid, size_t *argsize) { +*psutil_get_cmd_args(pid_t pid, size_t *argsize) { int mib[4]; int argmax; size_t size = sizeof(argmax); @@ -222,7 +222,7 @@ static char // returns the command line as a python list object PyObject * -psutil_get_cmdline(long pid) { +psutil_get_cmdline(pid_t pid) { char *argstr = NULL; size_t pos = 0; size_t argsize = 0; @@ -269,7 +269,7 @@ error: */ PyObject * psutil_proc_exe(PyObject *self, PyObject *args) { - long pid; + pid_t pid; char pathname[PATH_MAX]; int error; int mib[4]; @@ -313,7 +313,7 @@ psutil_proc_exe(PyObject *self, PyObject *args) { PyObject * psutil_proc_num_threads(PyObject *self, PyObject *args) { // Return number of threads used by process as a Python integer. - long pid; + pid_t pid; struct kinfo_proc kp; if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; @@ -330,7 +330,7 @@ psutil_proc_threads(PyObject *self, PyObject *args) { // Thanks to Robert N. M. Watson: // http://code.metager.de/source/xref/freebsd/usr.bin/procstat/ // procstat_threads.c - long pid; + pid_t pid; int mib[4]; struct kinfo_proc *kip = NULL; struct kinfo_proc *kipp = NULL; @@ -559,7 +559,7 @@ psutil_swap_mem(PyObject *self, PyObject *args) { #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000 PyObject * psutil_proc_cwd(PyObject *self, PyObject *args) { - long pid; + pid_t pid; struct kinfo_file *freep = NULL; struct kinfo_file *kif; struct kinfo_proc kipp; @@ -610,7 +610,7 @@ error: #if defined(__FreeBSD_version) && __FreeBSD_version >= 800000 PyObject * psutil_proc_num_fds(PyObject *self, PyObject *args) { - long pid; + pid_t pid; int cnt; struct kinfo_file *freep; @@ -769,7 +769,7 @@ PyObject * psutil_proc_memory_maps(PyObject *self, PyObject *args) { // Return a list of tuples for every process memory maps. //'procstat' cmdline utility has been used as an example. - long pid; + pid_t pid; int ptrwidth; int i, cnt; char addr[1000]; @@ -885,7 +885,7 @@ psutil_proc_cpu_affinity_get(PyObject* self, PyObject* args) { // Get process CPU affinity. // Reference: // http://sources.freebsd.org/RELENG_9/src/usr.bin/cpuset/cpuset.c - long pid; + pid_t pid; int ret; int i; cpuset_t mask; @@ -927,7 +927,7 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) { // Set process CPU affinity. // Reference: // http://sources.freebsd.org/RELENG_9/src/usr.bin/cpuset/cpuset.c - long pid; + pid_t pid; int i; int seq_len; int ret; diff --git a/psutil/arch/freebsd/sys_socks.c b/psutil/arch/freebsd/sys_socks.c index e0e2046b..0946f761 100644 --- a/psutil/arch/freebsd/sys_socks.c +++ b/psutil/arch/freebsd/sys_socks.c @@ -91,6 +91,7 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { PyObject *py_tuple = NULL; PyObject *py_laddr = NULL; PyObject *py_raddr = NULL; + PyObject *py_pid = NULL; switch (proto) { case IPPROTO_TCP: @@ -129,7 +130,7 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { } while (xig->xig_gen != exig->xig_gen && retry--); for (;;) { - struct xfile *xf; + struct xfile *xf; int lport, rport, status, family; xig = (struct xinpgen *)(void *)((char *)xig + xig->xig_len); @@ -202,20 +203,24 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) { py_raddr = Py_BuildValue("()"); if (!py_raddr) goto error; + py_pid = PyLong_FromPid(xf->xf_pid); + if (! py_pid) + goto error; py_tuple = Py_BuildValue( - "(iiiNNii)", + "(iiiNNiO)", xf->xf_fd, // fd family, // family type, // type py_laddr, // laddr py_raddr, // raddr status, // status - xf->xf_pid); // pid + py_pid); // pid if (!py_tuple) goto error; if (PyList_Append(py_retlist, py_tuple)) goto error; - Py_DECREF(py_tuple); + Py_CLEAR(py_tuple); + Py_CLEAR(py_pid); } free(buf); @@ -225,6 +230,7 @@ error: Py_XDECREF(py_tuple); Py_XDECREF(py_laddr); Py_XDECREF(py_raddr); + Py_XDECREF(py_pid); free(buf); return 0; } @@ -286,7 +292,7 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) { } while (xug->xug_gen != exug->xug_gen && retry--); for (;;) { - struct xfile *xf; + struct xfile *xf; xug = (struct xunpgen *)(void *)((char *)xug + xug->xug_len); if (xug >= exug) |