diff --git a/TODO b/TODO index b9f7df28e..28a2f4230 100644 --- a/TODO +++ b/TODO @@ -14,7 +14,10 @@ HIGHER PRIORITY * #371: CPU temperature (apparently OSX and Linux only; on Linux it requires lm-sensors lib). - * #269: expose network ifaces RX/TW queues. + * #269: expose network ifaces RX/TW queues. This should probably go into + net_if_stats(). Figure out on what platforms this is supported: + Linux: yes + Others: ? * Process.threads(): thread names; patch for OSX available at: https://code.google.com/p/plcrashreporter/issues/detail?id=65 @@ -85,10 +88,18 @@ DEBATABLE Also, we can probably reimplement wait_pid() on POSIX which is currently implemented as a busy-loop. - * Certain systems (XXX figure out which ones exactly) provide CPU times about - process children. On those systems Process.cpu_times() might return - a (user, system, user_children, system_children) ntuple. - Also, os.times() provides 'elapsed' times as well. + * Certain systems provide CPU times about process children. On those systems + Process.cpu_times() might return a (user, system, user_children, + system_children) ntuple. + * Linux: /proc/{PID}/stat + * Solaris: pr_cutime and pr_cstime + * FreeBSD: none + * OSX: none + * Windows: none + + * ...also, os.times() provides 'elapsed' times as well. + + * ...also Linux provides guest_time and cguest_time. * Enrich exception classes hierarchy on Python >= 3.3 / post PEP-3151 so that: - NoSuchProcess inherits from ProcessLookupError diff --git a/psutil/_psutil_bsd.c b/psutil/_psutil_bsd.c index 65115649d..7b6e56173 100644 --- a/psutil/_psutil_bsd.c +++ b/psutil/_psutil_bsd.c @@ -99,12 +99,10 @@ psutil_kinfo_proc(const pid_t pid, struct kinfo_proc *proc) */ void psutil_raise_ad_or_nsp(long pid) { - if (psutil_pid_exists(pid) == 0) { + if (psutil_pid_exists(pid) == 0) NoSuchProcess(); - } - else { + else AccessDenied(); - } } @@ -121,9 +119,8 @@ psutil_pids(PyObject *self, PyObject *args) PyObject *retlist = PyList_New(0); PyObject *pid = NULL; - if (retlist == NULL) { + if (retlist == NULL) return NULL; - } if (psutil_get_proc_list(&proclist, &num_processes) != 0) { PyErr_SetString(PyExc_RuntimeError, "failed to retrieve process list."); @@ -149,9 +146,8 @@ psutil_pids(PyObject *self, PyObject *args) error: Py_XDECREF(pid); Py_DECREF(retlist); - if (orig_address != NULL) { + if (orig_address != NULL) free(orig_address); - } return NULL; } @@ -184,12 +180,10 @@ psutil_proc_name(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("s", kp.ki_comm); } @@ -208,9 +202,8 @@ psutil_proc_exe(PyObject *self, PyObject *args) int mib[4]; size_t size; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } mib[0] = CTL_KERN; mib[1] = KERN_PROC; @@ -224,12 +217,10 @@ psutil_proc_exe(PyObject *self, PyObject *args) return NULL; } if (size == 0 || strlen(pathname) == 0) { - if (psutil_pid_exists(pid) == 0) { + if (psutil_pid_exists(pid) == 0) return NoSuchProcess(); - } - else { + else strcpy(pathname, ""); - } } return Py_BuildValue("s", pathname); } @@ -244,18 +235,16 @@ psutil_proc_cmdline(PyObject *self, PyObject *args) long pid; PyObject *arglist = NULL; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } // get the commandline, defined in arch/bsd/process_info.c arglist = psutil_get_arg_list(pid); // psutil_get_arg_list() returns NULL only if psutil_cmd_args // failed with ESRCH (no process with that PID) - if (NULL == arglist) { + if (NULL == arglist) return PyErr_SetFromErrno(PyExc_OSError); - } return Py_BuildValue("N", arglist); } @@ -268,12 +257,10 @@ psutil_proc_ppid(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("l", (long)kp.ki_ppid); } @@ -286,12 +273,10 @@ psutil_proc_status(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("i", (int)kp.ki_stat); } @@ -305,12 +290,10 @@ psutil_proc_uids(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("lll", (long)kp.ki_ruid, (long)kp.ki_uid, @@ -327,12 +310,10 @@ psutil_proc_gids(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("lll", (long)kp.ki_rgid, (long)kp.ki_groups[0], @@ -349,12 +330,10 @@ psutil_proc_tty_nr(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("i", kp.ki_tdev); } @@ -367,12 +346,10 @@ psutil_proc_num_ctx_switches(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("(ll)", kp.ki_rusage.ru_nvcsw, kp.ki_rusage.ru_nivcsw); @@ -387,12 +364,10 @@ psutil_proc_num_threads(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("l", (long)kp.ki_numthreads); } @@ -473,9 +448,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) error: Py_XDECREF(pyTuple); Py_DECREF(retList); - if (kip != NULL) { + if (kip != NULL) free(kip); - } return NULL; } @@ -489,12 +463,10 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) long pid; double user_t, sys_t; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } // convert from microseconds to seconds user_t = TV2DOUBLE(kp.ki_rusage.ru_utime); sys_t = TV2DOUBLE(kp.ki_rusage.ru_stime); @@ -517,13 +489,10 @@ psutil_cpu_count_logical(PyObject *self, PyObject *args) mib[1] = HW_NCPU; len = sizeof(ncpu); - if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) { - // mimic os.cpu_count() - Py_RETURN_NONE; - } - else { + if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) + Py_RETURN_NONE; // mimic os.cpu_count() + else return Py_BuildValue("i", ncpu); - } } @@ -570,12 +539,10 @@ psutil_proc_create_time(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("d", TV2DOUBLE(kp.ki_start)); } @@ -589,12 +556,10 @@ psutil_proc_io_counters(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } // there's apparently no way to determine bytes count, hence return -1. return Py_BuildValue("(llll)", kp.ki_rusage.ru_inblock, @@ -612,12 +577,10 @@ psutil_proc_memory_info(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("(lllll)", ptoa(kp.ki_rssize), // rss (long)kp.ki_size, // vms @@ -890,9 +853,8 @@ psutil_proc_cwd(PyObject *self, PyObject *args) * (lsof can't do that it either). Since this happens even * as root we return an empty string instead of AccessDenied. */ - if (path == NULL) { + if (path == NULL) path = Py_BuildValue("s", ""); - } free(freep); return path; @@ -939,7 +901,8 @@ psutil_sockaddr_port(int family, struct sockaddr_storage *ss) if (family == AF_INET) { sin = (struct sockaddr_in *)ss; return (sin->sin_port); - } else { + } + else { sin6 = (struct sockaddr_in6 *)ss; return (sin6->sin6_port); } @@ -954,7 +917,8 @@ psutil_sockaddr_addr(int family, struct sockaddr_storage *ss) if (family == AF_INET) { sin = (struct sockaddr_in *)ss; return (&sin->sin_addr); - } else { + } + else { sin6 = (struct sockaddr_in6 *)ss; return (&sin6->sin6_addr); } @@ -1052,12 +1016,10 @@ psutil_proc_connections(PyObject *self, PyObject *args) PyObject *_family = NULL; PyObject *_type = NULL; - if (retList == NULL) { + if (retList == NULL) return NULL; - } - if (! PyArg_ParseTuple(args, "lOO", &pid, &af_filter, &type_filter)) { + if (! PyArg_ParseTuple(args, "lOO", &pid, &af_filter, &type_filter)) goto error; - } if (!PySequence_Check(af_filter) || !PySequence_Check(type_filter)) { PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence"); goto error; @@ -1085,22 +1047,18 @@ psutil_proc_connections(PyObject *self, PyObject *args) raddr = NULL; kif = &freep[i]; - if (kif->kf_type == KF_TYPE_SOCKET) - { + if (kif->kf_type == KF_TYPE_SOCKET) { // apply filters _family = PyLong_FromLong((long)kif->kf_sock_domain); inseq = PySequence_Contains(af_filter, _family); Py_DECREF(_family); - if (inseq == 0) { + if (inseq == 0) continue; - } _type = PyLong_FromLong((long)kif->kf_sock_type); inseq = PySequence_Contains(type_filter, _type); Py_DECREF(_type); - if (inseq == 0) { + if (inseq == 0) continue; - } - // IPv4 / IPv6 socket if ((kif->kf_sock_domain == AF_INET) || (kif->kf_sock_domain == AF_INET6)) { @@ -1134,12 +1092,10 @@ psutil_proc_connections(PyObject *self, PyObject *args) laddr = Py_BuildValue("(si)", lip, lport); if (!laddr) goto error; - if (rport != 0) { + if (rport != 0) raddr = Py_BuildValue("(si)", rip, rport); - } - else { + else raddr = Py_BuildValue("()"); - } if (!raddr) goto error; tuple = Py_BuildValue("(iiiNNi)", @@ -1296,15 +1252,12 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) PyObject *pytuple = NULL; PyObject *retlist = PyList_New(0); - if (retlist == NULL) { + if (retlist == NULL) return NULL; - } - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) goto error; - } - if (psutil_kinfo_proc(pid, &kp) == -1) { + if (psutil_kinfo_proc(pid, &kp) == -1) goto error; - } freep = kinfo_getvmmap(pid, &cnt); if (freep == NULL) { @@ -1555,9 +1508,8 @@ psutil_net_io_counters(PyObject *self, PyObject *args) // http://lists.freebsd.org/pipermail/freebsd-current/ // 2011-October/028752.html // 'ifconfig -a' doesn't show them, nor do we. - if (strncmp(ifc_name, "usbus", 5) == 0) { + if (strncmp(ifc_name, "usbus", 5) == 0) continue; - } py_ifc_info = Py_BuildValue("(kkkkkkki)", if2m->ifm_data.ifi_obytes, @@ -1648,9 +1600,8 @@ psutil_disk_io_counters(PyObject *self, PyObject *args) Py_DECREF(py_disk_info); } - if (stats.dinfo->mem_ptr) { + if (stats.dinfo->mem_ptr) free(stats.dinfo->mem_ptr); - } free(stats.dinfo); return py_retdict; @@ -1788,9 +1739,8 @@ psutil_get_pid_from_sock(int sock_hash) if (xf->xf_data == NULL) continue; hash = (int)((uintptr_t)xf->xf_data % HASHSIZE); - if (sock_hash == hash) { + if (sock_hash == hash) return xf->xf_pid; - } } return -1; } @@ -1834,10 +1784,8 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) do { for (;;) { buf = realloc(buf, bufsize); - if (buf == NULL) { - // XXX - continue; - } + if (buf == NULL) + continue; // XXX len = bufsize; if (sysctlbyname(varname, buf, &len, NULL, 0) == 0) break; @@ -1915,12 +1863,10 @@ int psutil_gather_inet(int proto, PyObject *py_retlist) laddr = Py_BuildValue("(si)", lip, lport); if (!laddr) goto error; - if (rport != 0) { + if (rport != 0) raddr = Py_BuildValue("(si)", rip, rport); - } - else { + else raddr = Py_BuildValue("()"); - } if (!raddr) goto error; tuple = Py_BuildValue("(iiiNNii)", -1, family, type, laddr, raddr, @@ -2008,9 +1954,8 @@ int psutil_gather_unix(int proto, PyObject *py_retlist) if (xug >= exug) break; xup = (struct xunpcb *)xug; - if (xup->xu_len != sizeof *xup) { + if (xup->xu_len != sizeof *xup) goto error; - } hash = (int)((uintptr_t) xup->xu_socket.xso_so % HASHSIZE); pid = psutil_get_pid_from_sock(hash); @@ -2089,10 +2034,8 @@ psutil_proc_cpu_affinity_get(PyObject* self, PyObject* args) PyObject* py_retlist; PyObject* py_cpu_num; - if (!PyArg_ParseTuple(args, "i", &pid)) { + if (!PyArg_ParseTuple(args, "i", &pid)) return NULL; - } - ret = cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_PID, pid, sizeof(mask), &mask); if (ret != 0) { @@ -2155,9 +2098,8 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) #else long value = PyInt_AsLong(item); #endif - if (value == -1 && PyErr_Occurred()) { + if (value == -1 && PyErr_Occurred()) goto error; - } CPU_SET(value, &cpu_set); } @@ -2346,9 +2288,8 @@ void init_psutil_bsd(void) PyModule_AddIntConstant(module, "TCPS_TIME_WAIT", TCPS_TIME_WAIT); PyModule_AddIntConstant(module, "PSUTIL_CONN_NONE", PSUTIL_CONN_NONE); - if (module == NULL) { + if (module == NULL) INITERROR; - } #if PY_MAJOR_VERSION >= 3 return module; #endif diff --git a/psutil/_psutil_linux.c b/psutil/_psutil_linux.c index 1eee2a9d4..0bc6642c6 100644 --- a/psutil/_psutil_linux.c +++ b/psutil/_psutil_linux.c @@ -79,13 +79,11 @@ psutil_proc_ioprio_get(PyObject *self, PyObject *args) { long pid; int ioprio, ioclass, iodata; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } ioprio = ioprio_get(IOPRIO_WHO_PROCESS, pid); - if (ioprio == -1) { + if (ioprio == -1) return PyErr_SetFromErrno(PyExc_OSError); - } ioclass = IOPRIO_PRIO_CLASS(ioprio); iodata = IOPRIO_PRIO_DATA(ioprio); return Py_BuildValue("ii", ioclass, iodata); @@ -104,14 +102,12 @@ psutil_proc_ioprio_set(PyObject *self, PyObject *args) int ioprio, ioclass, iodata; int retval; - if (! PyArg_ParseTuple(args, "lii", &pid, &ioclass, &iodata)) { + if (! PyArg_ParseTuple(args, "lii", &pid, &ioclass, &iodata)) return NULL; - } ioprio = IOPRIO_PRIO_VALUE(ioclass, iodata); retval = ioprio_set(IOPRIO_WHO_PROCESS, pid, ioprio); - if (retval == -1) { + if (retval == -1) return PyErr_SetFromErrno(PyExc_OSError); - } Py_RETURN_NONE; } #endif @@ -133,9 +129,8 @@ psutil_linux_prlimit(PyObject *self, PyObject *args) PyObject *soft = NULL; PyObject *hard = NULL; - if (! PyArg_ParseTuple(args, "li|OO", &pid, &resource, &soft, &hard)) { + if (! PyArg_ParseTuple(args, "li|OO", &pid, &resource, &soft, &hard)) return NULL; - } // get if (soft == NULL && hard == NULL) { @@ -238,10 +233,9 @@ static PyObject * psutil_linux_sysinfo(PyObject *self, PyObject *args) { struct sysinfo info; - if (sysinfo(&info) != 0) { - return PyErr_SetFromErrno(PyExc_OSError); - } + if (sysinfo(&info) != 0) + return PyErr_SetFromErrno(PyExc_OSError); // note: boot time might also be determined from here return Py_BuildValue( "(KKKKKK)", @@ -271,10 +265,8 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) cpu_set_t *mask = NULL; PyObject *res = NULL; - if (!PyArg_ParseTuple(args, "i", &pid)) { + if (!PyArg_ParseTuple(args, "i", &pid)) return NULL; - } - ncpus = NCPUS_START; while (1) { setsize = CPU_ALLOC_SIZE(ncpus); @@ -341,14 +333,11 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) PyObject* py_retlist; PyObject *py_cpu_num; - if (!PyArg_ParseTuple(args, "i", &pid)) { + if (!PyArg_ParseTuple(args, "i", &pid)) return NULL; - } - CPU_ZERO(&cpuset); - if (sched_getaffinity(pid, len, &cpuset) < 0) { + if (sched_getaffinity(pid, len, &cpuset) < 0) return PyErr_SetFromErrno(PyExc_OSError); - } py_retlist = PyList_New(0); if (py_retlist == NULL) @@ -386,9 +375,8 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) PyObject *py_cpu_set; PyObject *py_cpu_seq = NULL; - if (!PyArg_ParseTuple(args, "lO", &pid, &py_cpu_set)) { - goto error; - } + if (!PyArg_ParseTuple(args, "lO", &pid, &py_cpu_set)) + return NULL; if (!PySequence_Check(py_cpu_set)) { PyErr_Format(PyExc_TypeError, "sequence argument expected, got %s", @@ -397,9 +385,8 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) } py_cpu_seq = PySequence_Fast(py_cpu_set, "expected a sequence or integer"); - if (!py_cpu_seq) { + if (!py_cpu_seq) goto error; - } seq_len = PySequence_Fast_GET_SIZE(py_cpu_seq); CPU_ZERO(&cpu_set); for (i = 0; i < seq_len; i++) { @@ -409,9 +396,8 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) #else long value = PyInt_AsLong(item); #endif - if (value == -1 && PyErr_Occurred()) { + if (value == -1 && PyErr_Occurred()) goto error; - } CPU_SET(value, &cpu_set); } @@ -425,10 +411,8 @@ psutil_proc_cpu_affinity_set(PyObject *self, PyObject *args) Py_RETURN_NONE; error: - if (py_cpu_seq != NULL) { + if (py_cpu_seq != NULL) Py_DECREF(py_cpu_seq); - } - return NULL; } @@ -691,9 +675,8 @@ void init_psutil_linux(void) PyModule_AddIntConstant(module, "DUPLEX_FULL", DUPLEX_FULL); PyModule_AddIntConstant(module, "DUPLEX_UNKNOWN", DUPLEX_UNKNOWN); - if (module == NULL) { + if (module == NULL) INITERROR; - } #if PY_MAJOR_VERSION >= 3 return module; #endif diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 2e2d57d15..3ebf8ff27 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -120,12 +120,10 @@ psutil_proc_name(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_get_kinfo_proc(pid, &kp) == -1) { + if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("s", kp.kp_proc.p_comm); } @@ -139,9 +137,8 @@ psutil_proc_cwd(PyObject *self, PyObject *args) long pid; struct proc_vnodepathinfo pathinfo; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } if (! psutil_proc_pidinfo(pid, PROC_PIDVNODEPATHINFO, &pathinfo, sizeof(pathinfo))) @@ -162,17 +159,14 @@ psutil_proc_exe(PyObject *self, PyObject *args) char buf[PATH_MAX]; int ret; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } ret = proc_pidpath(pid, &buf, sizeof(buf)); if (ret == 0) { - if (! psutil_pid_exists(pid)) { + if (! psutil_pid_exists(pid)) return NoSuchProcess(); - } - else { + else return AccessDenied(); - } } return Py_BuildValue("s", buf); } @@ -187,9 +181,8 @@ psutil_proc_cmdline(PyObject *self, PyObject *args) long pid; PyObject *arglist = NULL; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } // get the commandline, defined in arch/osx/process_info.c arglist = psutil_get_arg_list(pid); @@ -205,12 +198,10 @@ psutil_proc_ppid(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_get_kinfo_proc(pid, &kp) == -1) { + if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("l", (long)kp.kp_eproc.e_ppid); } @@ -223,12 +214,10 @@ psutil_proc_uids(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_get_kinfo_proc(pid, &kp) == -1) { + if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("lll", (long)kp.kp_eproc.e_pcred.p_ruid, (long)kp.kp_eproc.e_ucred.cr_uid, @@ -244,12 +233,10 @@ psutil_proc_gids(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_get_kinfo_proc(pid, &kp) == -1) { + if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("lll", (long)kp.kp_eproc.e_pcred.p_rgid, (long)kp.kp_eproc.e_ucred.cr_groups[0], @@ -265,12 +252,10 @@ psutil_proc_tty_nr(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_get_kinfo_proc(pid, &kp) == -1) { + if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("i", kp.kp_eproc.e_tdev); } @@ -299,9 +284,8 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) if (py_list == NULL) return NULL; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) goto error; - } err = task_for_pid(mach_task_self(), pid, &task); @@ -324,11 +308,8 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) err = vm_region_recurse_64(task, &address, &size, &depth, (vm_region_info_64_t)&info, &count); - - if (err == KERN_INVALID_ADDRESS) { + if (err == KERN_INVALID_ADDRESS) break; - } - if (info.is_submap) { depth++; } @@ -430,18 +411,14 @@ psutil_cpu_count_logical(PyObject *self, PyObject *args) int mib[2]; int ncpu; size_t len; - mib[0] = CTL_HW; mib[1] = HW_NCPU; len = sizeof(ncpu); - if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) { - // mimic os.cpu_count() - Py_RETURN_NONE; - } - else { + if (sysctl(mib, 2, &ncpu, &len, NULL, 0) == -1) + Py_RETURN_NONE; // mimic os.cpu_count() + else return Py_BuildValue("i", ncpu); - } } @@ -453,11 +430,11 @@ psutil_cpu_count_phys(PyObject *self, PyObject *args) { int num; size_t size = sizeof(int); - if (sysctlbyname("hw.physicalcpu", &num, &size, NULL, 0)) { - // mimic os.cpu_count() - Py_RETURN_NONE; - } - return Py_BuildValue("i", num); + + if (sysctlbyname("hw.physicalcpu", &num, &size, NULL, 0)) + Py_RETURN_NONE; // mimic os.cpu_count() + else + return Py_BuildValue("i", num); } @@ -471,12 +448,11 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) { long pid; struct proc_taskinfo pti; - if (! PyArg_ParseTuple(args, "l", &pid)) { + + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) { + if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) return NULL; - } return Py_BuildValue("(dd)", (float)pti.pti_total_user / 1000000000.0, (float)pti.pti_total_system / 1000000000.0); @@ -492,12 +468,10 @@ psutil_proc_create_time(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_get_kinfo_proc(pid, &kp) == -1) { + if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("d", TV2DOUBLE(kp.kp_proc.p_starttime)); } @@ -510,13 +484,11 @@ psutil_proc_memory_info(PyObject *self, PyObject *args) { long pid; struct proc_taskinfo pti; - if (! PyArg_ParseTuple(args, "l", &pid)) { + + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) { + if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) return NULL; - } - // Note: determining other memory stats on OSX is a mess: // http://www.opensource.apple.com/source/top/top-67/libtop.c?txt // I just give up... @@ -540,12 +512,11 @@ psutil_proc_num_threads(PyObject *self, PyObject *args) { long pid; struct proc_taskinfo pti; - if (! PyArg_ParseTuple(args, "l", &pid)) { + + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) { + if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) return NULL; - } return Py_BuildValue("k", pti.pti_threadnum); } @@ -558,12 +529,11 @@ psutil_proc_num_ctx_switches(PyObject *self, PyObject *args) { long pid; struct proc_taskinfo pti; - if (! PyArg_ParseTuple(args, "l", &pid)) { + + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) { + if (! psutil_proc_pidinfo(pid, PROC_PIDTASKINFO, &pti, sizeof(pti))) return NULL; - } // unvoluntary value seems not to be available; // pti.pti_csw probably refers to the sum of the two (getrusage() // numbers seems to confirm this theory). @@ -583,10 +553,10 @@ psutil_virtual_mem(PyObject *self, PyObject *args) size_t len = sizeof(total); vm_statistics_data_t vm; int pagesize = getpagesize(); - // physical mem mib[0] = CTL_HW; mib[1] = HW_MEMSIZE; + if (sysctl(mib, 2, &total, &len, NULL, 0)) { if (errno != 0) PyErr_SetFromErrno(PyExc_OSError); @@ -596,9 +566,8 @@ psutil_virtual_mem(PyObject *self, PyObject *args) } // vm - if (!psutil_sys_vminfo(&vm)) { + if (!psutil_sys_vminfo(&vm)) return NULL; - } return Py_BuildValue( "KKKKK", @@ -633,9 +602,8 @@ psutil_swap_mem(PyObject *self, PyObject *args) PyErr_Format(PyExc_RuntimeError, "sysctl(VM_SWAPUSAGE) failed"); return NULL; } - if (!psutil_sys_vminfo(&vmstat)) { + if (!psutil_sys_vminfo(&vmstat)) return NULL; - } return Py_BuildValue( "LLLKK", @@ -660,11 +628,10 @@ psutil_cpu_times(PyObject *self, PyObject *args) mach_port_t host_port = mach_host_self(); error = host_statistics(host_port, HOST_CPU_LOAD_INFO, (host_info_t)&r_load, &count); - if (error != KERN_SUCCESS) { + if (error != KERN_SUCCESS) return PyErr_Format(PyExc_RuntimeError, "Error in host_statistics(): %s", mach_error_string(error)); - } mach_port_deallocate(mach_task_self(), host_port); return Py_BuildValue( @@ -724,9 +691,8 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) ret = vm_deallocate(mach_task_self(), (vm_address_t)info_array, info_count * sizeof(int)); - if (ret != KERN_SUCCESS) { + if (ret != KERN_SUCCESS) PyErr_WarnEx(PyExc_RuntimeWarning, "vm_deallocate() failed", 2); - } return py_retlist; error: @@ -735,9 +701,8 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) if (cpu_load_info != NULL) { ret = vm_deallocate(mach_task_self(), (vm_address_t)info_array, info_count * sizeof(int)); - if (ret != KERN_SUCCESS) { + if (ret != KERN_SUCCESS) PyErr_WarnEx(PyExc_RuntimeWarning, "vm_deallocate() failed", 2); - } } return NULL; } @@ -896,12 +861,10 @@ psutil_proc_status(PyObject *self, PyObject *args) { long pid; struct kinfo_proc kp; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (psutil_get_kinfo_proc(pid, &kp) == -1) { + if (psutil_get_kinfo_proc(pid, &kp) == -1) return NULL; - } return Py_BuildValue("i", (int)kp.kp_proc.p_stat); } @@ -930,19 +893,16 @@ psutil_proc_threads(PyObject *self, PyObject *args) return NULL; // the argument passed should be a process id - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) goto error; - } // task_for_pid() requires special privileges err = task_for_pid(mach_task_self(), pid, &task); if (err != KERN_SUCCESS) { - if (! psutil_pid_exists(pid)) { + if (! psutil_pid_exists(pid)) NoSuchProcess(); - } - else { + else AccessDenied(); - } goto error; } @@ -995,9 +955,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) ret = vm_deallocate(task, (vm_address_t)thread_list, thread_count * sizeof(int)); - if (ret != KERN_SUCCESS) { + if (ret != KERN_SUCCESS) PyErr_WarnEx(PyExc_RuntimeWarning, "vm_deallocate() failed", 2); - } mach_port_deallocate(mach_task_self(), task); @@ -1011,9 +970,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) if (thread_list != NULL) { ret = vm_deallocate(task, (vm_address_t)thread_list, thread_count * sizeof(int)); - if (ret != KERN_SUCCESS) { + if (ret != KERN_SUCCESS) PyErr_WarnEx(PyExc_RuntimeWarning, "vm_deallocate() failed", 2); - } } return NULL; } @@ -1044,9 +1002,8 @@ psutil_proc_open_files(PyObject *self, PyObject *args) if (retList == NULL) return NULL; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) goto error; - } pidinfo_result = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); if (pidinfo_result <= 0) { @@ -1123,19 +1080,14 @@ psutil_proc_open_files(PyObject *self, PyObject *args) error: Py_XDECREF(tuple); Py_DECREF(retList); - if (fds_pointer != NULL) { + if (fds_pointer != NULL) free(fds_pointer); - } - if (errno != 0) { + if (errno != 0) return PyErr_SetFromErrno(PyExc_OSError); - } - else if (! psutil_pid_exists(pid)) { + else if (! psutil_pid_exists(pid)) return NoSuchProcess(); - } - else { - // exception has already been set earlier - return NULL; - } + else + return NULL; // exception has already been set earlier } @@ -1171,23 +1123,19 @@ psutil_proc_connections(PyObject *self, PyObject *args) if (retList == NULL) return NULL; - if (! PyArg_ParseTuple(args, "lOO", &pid, &af_filter, &type_filter)) { + if (! PyArg_ParseTuple(args, "lOO", &pid, &af_filter, &type_filter)) goto error; - } if (!PySequence_Check(af_filter) || !PySequence_Check(type_filter)) { PyErr_SetString(PyExc_TypeError, "arg 2 or 3 is not a sequence"); goto error; } - if (pid == 0) { + if (pid == 0) return retList; - } - pidinfo_result = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); - if (pidinfo_result <= 0) { + if (pidinfo_result <= 0) goto error; - } fds_pointer = malloc(pidinfo_result); if (fds_pointer == NULL) { @@ -1197,10 +1145,8 @@ psutil_proc_connections(PyObject *self, PyObject *args) pidinfo_result = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fds_pointer, pidinfo_result); - if (pidinfo_result <= 0) { + if (pidinfo_result <= 0) goto error; - } - iterations = (pidinfo_result / PROC_PIDLISTFD_SIZE); for (i = 0; i < iterations; i++) { @@ -1221,14 +1167,12 @@ psutil_proc_connections(PyObject *self, PyObject *args) // let's assume socket has been closed continue; } - if (errno != 0) { + if (errno != 0) PyErr_SetFromErrno(PyExc_OSError); - } - else { + else PyErr_Format( PyExc_RuntimeError, "proc_pidinfo(PROC_PIDFDVNODEPATHINFO) failed"); - } goto error; } if (nb < sizeof(si)) { @@ -1254,15 +1198,13 @@ psutil_proc_connections(PyObject *self, PyObject *args) _family = PyLong_FromLong((long)family); inseq = PySequence_Contains(af_filter, _family); Py_DECREF(_family); - if (inseq == 0) { + if (inseq == 0) continue; - } _type = PyLong_FromLong((long)type); inseq = PySequence_Contains(type_filter, _type); Py_DECREF(_type); - if (inseq == 0) { + if (inseq == 0) continue; - } if (errno != 0) { PyErr_SetFromErrno(PyExc_OSError); @@ -1301,22 +1243,18 @@ psutil_proc_connections(PyObject *self, PyObject *args) lport = ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_lport); rport = ntohs(si.psi.soi_proto.pri_tcp.tcpsi_ini.insi_fport); - if (type == SOCK_STREAM) { + if (type == SOCK_STREAM) state = (int)si.psi.soi_proto.pri_tcp.tcpsi_state; - } - else { + else state = PSUTIL_CONN_NONE; - } laddr = Py_BuildValue("(si)", lip, lport); if (!laddr) goto error; - if (rport != 0) { + if (rport != 0) raddr = Py_BuildValue("(si)", rip, rport); - } - else { + else raddr = Py_BuildValue("()"); - } if (!raddr) goto error; @@ -1355,19 +1293,15 @@ psutil_proc_connections(PyObject *self, PyObject *args) Py_XDECREF(raddr); Py_DECREF(retList); - if (fds_pointer != NULL) { + if (fds_pointer != NULL) free(fds_pointer); - } - if (errno != 0) { + if (errno != 0) return PyErr_SetFromErrno(PyExc_OSError); - } - else if (! psutil_pid_exists(pid) ) { + else if (! psutil_pid_exists(pid)) return NoSuchProcess(); - } - else { + else return PyErr_Format(PyExc_RuntimeError, "proc_pidinfo(PROC_PIDLISTFDS) failed"); - } } @@ -1382,19 +1316,16 @@ psutil_proc_num_fds(PyObject *self, PyObject *args) int num; struct proc_fdinfo *fds_pointer; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } pidinfo_result = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, NULL, 0); - if (pidinfo_result <= 0) { + if (pidinfo_result <= 0) return PyErr_SetFromErrno(PyExc_OSError); - } fds_pointer = malloc(pidinfo_result); - if (fds_pointer == NULL) { + if (fds_pointer == NULL) return PyErr_NoMemory(); - } pidinfo_result = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, fds_pointer, pidinfo_result); if (pidinfo_result <= 0) { @@ -1869,9 +1800,8 @@ init_psutil_osx(void) PyModule_AddIntConstant(module, "TCPS_TIME_WAIT", TCPS_TIME_WAIT); PyModule_AddIntConstant(module, "PSUTIL_CONN_NONE", PSUTIL_CONN_NONE); - if (module == NULL) { + if (module == NULL) INITERROR; - } #if PY_MAJOR_VERSION >= 3 return module; #endif diff --git a/psutil/_psutil_posix.c b/psutil/_psutil_posix.c index b010864cb..183dab0e1 100644 --- a/psutil/_psutil_posix.c +++ b/psutil/_psutil_posix.c @@ -41,13 +41,12 @@ psutil_posix_getpriority(PyObject *self, PyObject *args) long pid; int priority; errno = 0; - if (! PyArg_ParseTuple(args, "l", &pid)) { + + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } priority = getpriority(PRIO_PROCESS, pid); - if (errno != 0) { + if (errno != 0) return PyErr_SetFromErrno(PyExc_OSError); - } return Py_BuildValue("i", priority); } @@ -61,13 +60,12 @@ psutil_posix_setpriority(PyObject *self, PyObject *args) long pid; int priority; int retval; - if (! PyArg_ParseTuple(args, "li", &pid, &priority)) { + + if (! PyArg_ParseTuple(args, "li", &pid, &priority)) return NULL; - } retval = setpriority(PRIO_PROCESS, pid, priority); - if (retval == -1) { + if (retval == -1) return PyErr_SetFromErrno(PyExc_OSError); - } Py_RETURN_NONE; } @@ -525,9 +523,8 @@ void init_psutil_posix(void) PyModule_AddIntConstant(module, "AF_LINK", AF_LINK); #endif - if (module == NULL) { + if (module == NULL) INITERROR; - } #if PY_MAJOR_VERSION >= 3 return module; #endif diff --git a/psutil/_psutil_sunos.c b/psutil/_psutil_sunos.c index 273961a68..fb80e3efb 100644 --- a/psutil/_psutil_sunos.c +++ b/psutil/_psutil_sunos.c @@ -204,13 +204,11 @@ proc_io_counters(PyObject* self, PyObject* args) char path[100]; prusage_t info; - if (! PyArg_ParseTuple(args, "i", &pid)) { + if (! PyArg_ParseTuple(args, "i", &pid)) return NULL; - } sprintf(path, "/proc/%i/usage", pid); - if (! psutil_file_to_struct(path, (void *)&info, sizeof(info))) { + if (! psutil_file_to_struct(path, (void *)&info, sizeof(info))) return NULL; - } // On Solaris we only have 'pr_ioch' which accounts for bytes read // *and* written. @@ -318,9 +316,8 @@ psutil_swap_mem(PyObject *self, PyObject *args) uint_t sout = 0; kc = kstat_open(); - if (kc == NULL) { + if (kc == NULL) return PyErr_SetFromErrno(PyExc_OSError);; - } k = kc->kc_chain; while (k != NULL) { @@ -572,17 +569,14 @@ psutil_proc_memory_maps(PyObject *self, PyObject *args) PyObject *pytuple = NULL; PyObject *py_retlist = PyList_New(0); - if (py_retlist == NULL) { + if (py_retlist == NULL) return NULL; - } - if (! PyArg_ParseTuple(args, "i", &pid)) { + if (! PyArg_ParseTuple(args, "i", &pid)) goto error; - } sprintf(path, "/proc/%i/status", pid); - if (! psutil_file_to_struct(path, (void *)&status, sizeof(status))) { + if (! psutil_file_to_struct(path, (void *)&status, sizeof(status))) goto error; - } sprintf(path, "/proc/%i/xmap", pid); if (stat(path, &st) == -1) { @@ -718,9 +712,8 @@ psutil_net_io_counters(PyObject *self, PyObject *args) (strcmp(ksp->ks_module, "lo") != 0)) { goto skip; */ - if ((strcmp(ksp->ks_module, "link") != 0)) { + if ((strcmp(ksp->ks_module, "link") != 0)) goto next; - } if (kstat_read(kc, ksp, NULL) == -1) { errno = 0; @@ -946,9 +939,8 @@ psutil_net_connections(PyObject *self, PyObject *args) py_laddr = Py_BuildValue("(si)", lip, lport); if (!py_laddr) goto error; - if (rport != 0) { + if (rport != 0) py_raddr = Py_BuildValue("(si)", rip, rport); - } else { py_raddr = Py_BuildValue("()"); } @@ -960,9 +952,8 @@ psutil_net_connections(PyObject *self, PyObject *args) py_tuple = Py_BuildValue("(iiiNNiI)", -1, AF_INET, SOCK_STREAM, py_laddr, py_raddr, state, processed_pid); - if (!py_tuple) { + if (!py_tuple) goto error; - } if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); @@ -989,12 +980,10 @@ psutil_net_connections(PyObject *self, PyObject *args) py_laddr = Py_BuildValue("(si)", lip, lport); if (!py_laddr) goto error; - if (rport != 0) { + if (rport != 0) py_raddr = Py_BuildValue("(si)", rip, rport); - } - else { + else py_raddr = Py_BuildValue("()"); - } if (!py_raddr) goto error; state = tp6->tcp6ConnEntryInfo.ce_state; @@ -1002,9 +991,8 @@ psutil_net_connections(PyObject *self, PyObject *args) // add item py_tuple = Py_BuildValue("(iiiNNiI)", -1, AF_INET6, SOCK_STREAM, py_laddr, py_raddr, state, processed_pid); - if (!py_tuple) { + if (!py_tuple) goto error; - } if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); @@ -1037,9 +1025,8 @@ psutil_net_connections(PyObject *self, PyObject *args) py_tuple = Py_BuildValue("(iiiNNiI)", -1, AF_INET, SOCK_DGRAM, py_laddr, py_raddr, PSUTIL_CONN_NONE, processed_pid); - if (!py_tuple) { + if (!py_tuple) goto error; - } if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); @@ -1047,7 +1034,9 @@ psutil_net_connections(PyObject *self, PyObject *args) } #if defined(AF_INET6) // UDPv6 - else if (mibhdr->level == MIB2_UDP6 || mibhdr->level == MIB2_UDP6_ENTRY) { + else if (mibhdr->level == MIB2_UDP6 || + mibhdr->level == MIB2_UDP6_ENTRY) + { ude6 = (mib2_udp6Entry_t *)databuf.buf; num_ent = mibhdr->len / sizeof(mib2_udp6Entry_t); for (i = 0; i < num_ent; i++, ude6++) { @@ -1065,9 +1054,8 @@ psutil_net_connections(PyObject *self, PyObject *args) py_tuple = Py_BuildValue("(iiiNNiI)", -1, AF_INET6, SOCK_DGRAM, py_laddr, py_raddr, PSUTIL_CONN_NONE, processed_pid); - if (!py_tuple) { + if (!py_tuple) goto error; - } if (PyList_Append(py_retlist, py_tuple)) goto error; Py_DECREF(py_tuple); @@ -1393,9 +1381,8 @@ void init_psutil_sunos(void) PyModule_AddIntConstant(module, "TCPS_BOUND", TCPS_BOUND); PyModule_AddIntConstant(module, "PSUTIL_CONN_NONE", PSUTIL_CONN_NONE); - if (module == NULL) { + if (module == NULL) INITERROR; - } #if PY_MAJOR_VERSION >= 3 return module; #endif diff --git a/psutil/_psutil_windows.c b/psutil/_psutil_windows.c index 9add15e64..670be24c2 100644 --- a/psutil/_psutil_windows.c +++ b/psutil/_psutil_windows.c @@ -229,14 +229,12 @@ psutil_pid_exists(PyObject *self, PyObject *args) long pid; int status; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } status = psutil_pid_is_running(pid); - if (-1 == status) { + if (-1 == status) return NULL; // exception raised in psutil_pid_is_running() - } return PyBool_FromLong(status); } @@ -253,13 +251,11 @@ psutil_pids(PyObject *self, PyObject *args) PyObject *pid = NULL; PyObject *retlist = PyList_New(0); - if (retlist == NULL) { + if (retlist == NULL) return NULL; - } proclist = psutil_get_pids(&numberOfReturnedPIDs); - if (NULL == proclist) { + if (proclist == NULL) goto error; - } for (i = 0; i < numberOfReturnedPIDs; i++) { pid = Py_BuildValue("I", proclist[i]); @@ -292,12 +288,10 @@ psutil_proc_kill(PyObject *self, PyObject *args) HANDLE hProcess; long pid; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (pid == 0) { + if (pid == 0) return AccessDenied(); - } hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); if (hProcess == NULL) { @@ -335,12 +329,10 @@ psutil_proc_wait(PyObject *self, PyObject *args) long pid; long timeout; - if (! PyArg_ParseTuple(args, "ll", &pid, &timeout)) { + if (! PyArg_ParseTuple(args, "ll", &pid, &timeout)) return NULL; - } - if (pid == 0) { + if (pid == 0) return AccessDenied(); - } hProcess = OpenProcess(SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid); @@ -395,15 +387,12 @@ psutil_proc_cpu_times(PyObject *self, PyObject *args) HANDLE hProcess; FILETIME ftCreate, ftExit, ftKernel, ftUser; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid(pid); - if (hProcess == NULL) { + if (hProcess == NULL) return NULL; - } - if (! GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) { CloseHandle(hProcess); if (GetLastError() == ERROR_ACCESS_DENIED) { @@ -452,20 +441,16 @@ psutil_proc_create_time(PyObject *self, PyObject *args) BOOL ret; FILETIME ftCreate, ftExit, ftKernel, ftUser; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } // special case for PIDs 0 and 4, return system boot time - if (0 == pid || 4 == pid) { + if (0 == pid || 4 == pid) return psutil_boot_time(NULL, NULL); - } hProcess = psutil_handle_from_pid(pid); - if (hProcess == NULL) { + if (hProcess == NULL) return NULL; - } - if (! GetProcessTimes(hProcess, &ftCreate, &ftExit, &ftKernel, &ftUser)) { CloseHandle(hProcess); if (GetLastError() == ERROR_ACCESS_DENIED) { @@ -487,9 +472,8 @@ psutil_proc_create_time(PyObject *self, PyObject *args) ret = GetExitCodeProcess(hProcess, &exitCode); CloseHandle(hProcess); if (ret != 0) { - if (exitCode != STILL_ACTIVE) { + if (exitCode != STILL_ACTIVE) return NoSuchProcess(); - } } else { // Ignore access denied as it means the process is still alive. @@ -523,13 +507,10 @@ psutil_cpu_count_logical(PyObject *self, PyObject *args) system_info.dwNumberOfProcessors = 0; GetSystemInfo(&system_info); - if (system_info.dwNumberOfProcessors == 0) { - // mimic os.cpu_count() - Py_RETURN_NONE; - } - else { + if (system_info.dwNumberOfProcessors == 0) + Py_RETURN_NONE; // mimic os.cpu_count() + else return Py_BuildValue("I", system_info.dwNumberOfProcessors); - } } @@ -605,20 +586,16 @@ psutil_proc_cmdline(PyObject *self, PyObject *args) { int pid_return; PyObject *arglist; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if ((pid == 0) || (pid == 4)) { + if ((pid == 0) || (pid == 4)) return Py_BuildValue("[]"); - } pid_return = psutil_pid_is_running(pid); - if (pid_return == 0) { + if (pid_return == 0) return NoSuchProcess(); - } - if (pid_return == -1) { + if (pid_return == -1) return NULL; - } // XXX the assumptio below probably needs to go away @@ -644,14 +621,12 @@ psutil_proc_exe(PyObject *self, PyObject *args) { long pid; HANDLE hProcess; wchar_t exe[MAX_PATH]; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid_waccess(pid, PROCESS_QUERY_INFORMATION); - if (NULL == hProcess) { + if (NULL == hProcess) return NULL; - } if (GetProcessImageFileNameW(hProcess, exe, MAX_PATH) == 0) { CloseHandle(hProcess); PyErr_SetFromWindowsErr(0); @@ -718,18 +693,15 @@ psutil_proc_memory_info(PyObject *self, PyObject *args) #endif SIZE_T private = 0; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid(pid); - if (NULL == hProcess) { + if (NULL == hProcess) return NULL; - } if (! GetProcessMemoryInfo(hProcess, (PPROCESS_MEMORY_COUNTERS)&cnt, - sizeof(cnt)) ) - { + sizeof(cnt))) { CloseHandle(hProcess); return PyErr_SetFromWindowsErr(0); } @@ -792,12 +764,10 @@ psutil_proc_memory_info_2(PyObject *self, PyObject *args) unsigned int m1, m2, m3, m4, m5, m6, m7, m8; #endif - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } - if (! psutil_get_proc_info(pid, &process, &buffer)) { + if (! psutil_get_proc_info(pid, &process, &buffer)) return NULL; - } #if (_WIN32_WINNT >= 0x0501) // Windows XP with SP2 private = process->PrivatePageCount; @@ -840,10 +810,8 @@ psutil_virtual_mem(PyObject *self, PyObject *args) MEMORYSTATUSEX memInfo; memInfo.dwLength = sizeof(MEMORYSTATUSEX); - if (! GlobalMemoryStatusEx(&memInfo) ) { + if (! GlobalMemoryStatusEx(&memInfo)) return PyErr_SetFromWindowsErr(0); - } - return Py_BuildValue("(LLLLLL)", memInfo.ullTotalPhys, // total memInfo.ullAvailPhys, // avail @@ -865,9 +833,8 @@ psutil_cpu_times(PyObject *self, PyObject *args) float idle, kernel, user, system; FILETIME idle_time, kernel_time, user_time; - if (!GetSystemTimes(&idle_time, &kernel_time, &user_time)) { + if (!GetSystemTimes(&idle_time, &kernel_time, &user_time)) return PyErr_SetFromWindowsErr(0); - } idle = (float)((HI_T * idle_time.dwHighDateTime) + \ (LO_T * idle_time.dwLowDateTime)); @@ -968,12 +935,10 @@ psutil_per_cpu_times(PyObject *self, PyObject *args) error: Py_XDECREF(arg); Py_DECREF(retlist); - if (sppi) { + if (sppi) free(sppi); - } - if (hNtDll) { + if (hNtDll) FreeLibrary(hNtDll); - } PyErr_SetFromWindowsErr(0); return NULL; } @@ -996,14 +961,12 @@ psutil_proc_cwd(PyObject *self, PyObject *args) PyObject *cwd_from_wchar = NULL; PyObject *cwd = NULL; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } processHandle = psutil_handle_from_pid(pid); - if (processHandle == NULL) { + if (processHandle == NULL) return NULL; - } pebAddress = psutil_get_peb_address(processHandle); @@ -1192,13 +1155,11 @@ psutil_proc_suspend(PyObject *self, PyObject *args) { long pid; int suspend = 1; - if (! PyArg_ParseTuple(args, "l", &pid)) { - return NULL; - } - if (! psutil_proc_suspend_or_resume(pid, suspend)) { + if (! PyArg_ParseTuple(args, "l", &pid)) + return NULL; + if (! psutil_proc_suspend_or_resume(pid, suspend)) return NULL; - } Py_RETURN_NONE; } @@ -1208,13 +1169,11 @@ psutil_proc_resume(PyObject *self, PyObject *args) { long pid; int suspend = 0; - if (! PyArg_ParseTuple(args, "l", &pid)) { - return NULL; - } - if (! psutil_proc_suspend_or_resume(pid, suspend)) { + if (! PyArg_ParseTuple(args, "l", &pid)) + return NULL; + if (! psutil_proc_suspend_or_resume(pid, suspend)) return NULL; - } Py_RETURN_NONE; } @@ -1232,12 +1191,10 @@ psutil_proc_threads(PyObject *self, PyObject *args) PyObject *pyTuple = NULL; HANDLE hThreadSnap = NULL; - if (retList == NULL) { + if (retList == NULL) return NULL; - } - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) goto error; - } if (pid == 0) { // raise AD instead of returning 0 as procexp is able to // retrieve useful information somehow @@ -1250,9 +1207,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) NoSuchProcess(); goto error; } - if (pid_return == -1) { + if (pid_return == -1) goto error; - } hThreadSnap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0); if (hThreadSnap == INVALID_HANDLE_VALUE) { @@ -1270,10 +1226,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) // Walk the thread snapshot to find all threads of the process. // If the thread belongs to the process, increase the counter. - do - { - if (te32.th32OwnerProcessID == pid) - { + do { + if (te32.th32OwnerProcessID == pid) { pyTuple = NULL; hThread = NULL; hThread = OpenThread(THREAD_QUERY_INFORMATION, @@ -1324,9 +1278,8 @@ psutil_proc_threads(PyObject *self, PyObject *args) Py_DECREF(retList); if (hThread != NULL) CloseHandle(hThread); - if (hThreadSnap != NULL) { + if (hThreadSnap != NULL) CloseHandle(hThreadSnap); - } return NULL; } @@ -1339,20 +1292,16 @@ psutil_proc_open_files(PyObject *self, PyObject *args) DWORD access = PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION; PyObject *filesList; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } processHandle = psutil_handle_from_pid_waccess(pid, access); - if (processHandle == NULL) { + if (processHandle == NULL) return NULL; - } - filesList = psutil_get_open_files(pid, processHandle); CloseHandle(processHandle); - if (filesList == NULL) { + if (filesList == NULL) return PyErr_SetFromWindowsErr(0); - } return filesList; } @@ -1369,12 +1318,10 @@ psutil_win32_QueryDosDevice(PyObject *self, PyObject *args) TCHAR d = TEXT('A'); TCHAR szBuff[5]; - if (!PyArg_ParseTuple(args, "s", &lpDevicePath)) { + if (!PyArg_ParseTuple(args, "s", &lpDevicePath)) return NULL; - } - while (d <= TEXT('Z')) - { + while (d <= TEXT('Z')) { TCHAR szDeviceName[3] = {d, TEXT(':'), TEXT('\0')}; TCHAR szTarget[512] = {0}; if (QueryDosDevice(szDeviceName, szTarget, 511) != 0) { @@ -1408,15 +1355,13 @@ psutil_proc_username(PyObject *self, PyObject *args) PTSTR fullName; PyObject *returnObject; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } processHandle = psutil_handle_from_pid_waccess( pid, PROCESS_QUERY_INFORMATION); - if (processHandle == NULL) { + if (processHandle == NULL) return NULL; - } if (!OpenProcessToken(processHandle, TOKEN_QUERY, &tokenHandle)) { CloseHandle(processHandle); @@ -1429,9 +1374,8 @@ psutil_proc_username(PyObject *self, PyObject *args) bufferSize = 0x100; user = malloc(bufferSize); - if (user == NULL) { + if (user == NULL) return PyErr_NoMemory(); - } if (!GetTokenInformation(tokenHandle, TokenUser, user, bufferSize, &bufferSize)) @@ -1963,9 +1907,8 @@ psutil_proc_priority_get(PyObject *self, PyObject *args) long pid; DWORD priority; HANDLE hProcess; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid(pid); if (hProcess == NULL) { @@ -2028,9 +1971,8 @@ psutil_proc_io_priority_get(PyObject *self, PyObject *args) (_NtQueryInformationProcess)GetProcAddress( GetModuleHandleA("ntdll.dll"), "NtQueryInformationProcess"); - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid(pid); if (hProcess == NULL) { return NULL; @@ -2099,9 +2041,8 @@ psutil_proc_io_counters(PyObject *self, PyObject *args) HANDLE hProcess; IO_COUNTERS IoCounters; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid(pid); if (NULL == hProcess) { return NULL; @@ -2130,9 +2071,8 @@ psutil_proc_cpu_affinity_get(PyObject *self, PyObject *args) DWORD_PTR proc_mask; DWORD_PTR system_mask; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid(pid); if (hProcess == NULL) { return NULL; @@ -2198,9 +2138,8 @@ psutil_proc_is_suspended(PyObject *self, PyObject *args) PSYSTEM_PROCESS_INFORMATION process; PVOID buffer; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } if (! psutil_get_proc_info(pid, &process, &buffer)) { return NULL; } @@ -2737,9 +2676,8 @@ psutil_proc_num_handles(PyObject *self, PyObject *args) HANDLE hProcess; DWORD handleCount; - if (! PyArg_ParseTuple(args, "l", &pid)) { + if (! PyArg_ParseTuple(args, "l", &pid)) return NULL; - } hProcess = psutil_handle_from_pid(pid); if (NULL == hProcess) { return NULL; diff --git a/psutil/arch/bsd/process_info.c b/psutil/arch/bsd/process_info.c index ea7cba0da..4d7392406 100644 --- a/psutil/arch/bsd/process_info.c +++ b/psutil/arch/bsd/process_info.c @@ -128,9 +128,8 @@ char mib[3] = pid; // call with a null buffer first to determine if we need a buffer - if (sysctl(mib, 4, NULL, &size, NULL, 0) == -1) { + if (sysctl(mib, 4, NULL, &size, NULL, 0) == -1) return NULL; - } path = malloc(size); if (path == NULL) { @@ -213,14 +212,11 @@ psutil_get_arg_list(long pid) PyObject *retlist = Py_BuildValue("[]"); PyObject *item = NULL; - if (pid < 0) { + if (pid < 0) return retlist; - } - argstr = psutil_get_cmd_args(pid, &argsize); - if (argstr == NULL) { + if (argstr == NULL) goto error; - } // args are returned as a flattened string with \0 separators between // arguments add each string to the list then step forward to the next @@ -256,16 +252,13 @@ int psutil_pid_exists(long pid) { int kill_ret; - if (pid < 0) { - return 0; - } + if (pid < 0) + return 0; // if kill returns success of permission denied we know it's a valid PID kill_ret = kill(pid , 0); - if ((0 == kill_ret) || (EPERM == errno)) { + if ((0 == kill_ret) || (EPERM == errno)) return 1; - } - // otherwise return 0 for PID not found return 0; } diff --git a/psutil/arch/osx/process_info.c b/psutil/arch/osx/process_info.c index be8092efe..b6dd5bb93 100644 --- a/psutil/arch/osx/process_info.c +++ b/psutil/arch/osx/process_info.c @@ -32,15 +32,12 @@ psutil_pid_exists(long pid) int kill_ret; // save some time if it's an invalid PID - if (pid < 0) { + if (pid < 0) return 0; - } - // if kill returns success of permission denied we know it's a valid PID kill_ret = kill(pid , 0); - if ( (0 == kill_ret) || (EPERM == errno) ) { + if ( (0 == kill_ret) || (EPERM == errno)) return 1; - } // otherwise return 0 for PID not found return 0; @@ -85,34 +82,29 @@ psutil_get_proc_list(kinfo_proc **procList, size_t *procCount) */ while (lim-- > 0) { size = 0; - if (sysctl((int *)mib3, 3, NULL, &size, NULL, 0) == -1) { + if (sysctl((int *)mib3, 3, NULL, &size, NULL, 0) == -1) return errno; - } - size2 = size + (size >> 3); // add some if (size2 > size) { ptr = malloc(size2); - if (ptr == NULL) { + if (ptr == NULL) ptr = malloc(size); - } else { + else size = size2; - } } else { ptr = malloc(size); } - if (ptr == NULL) { + if (ptr == NULL) return ENOMEM; - } if (sysctl((int *)mib3, 3, ptr, &size, NULL, 0) == -1) { err = errno; free(ptr); - if (err != ENOMEM) { + if (err != ENOMEM) return err; - } - - } else { + } + else { *procList = (kinfo_proc *)ptr; *procCount = size / sizeof(kinfo_proc); return 0; @@ -130,9 +122,8 @@ psutil_get_argmax() int mib[] = { CTL_KERN, KERN_ARGMAX }; size_t size = sizeof(argmax); - if (sysctl(mib, 2, &argmax, &size, NULL, 0) == 0) { + if (sysctl(mib, 2, &argmax, &size, NULL, 0) == 0) return argmax; - } return 0; } @@ -153,9 +144,8 @@ psutil_get_arg_list(long pid) PyObject *arglist = NULL; // special case for PID 0 (kernel_task) where cmdline cannot be fetched - if (pid == 0) { + if (pid == 0) return Py_BuildValue("[]"); - } // read argmax and allocate memory for argument space. argmax = psutil_get_argmax(); @@ -177,11 +167,10 @@ psutil_get_arg_list(long pid) if (sysctl(mib, 3, procargs, &argmax, NULL, 0) < 0) { if (EINVAL == errno) { // EINVAL == access denied OR nonexistent PID - if ( psutil_pid_exists(pid) ) { + if (psutil_pid_exists(pid)) AccessDenied(); - } else { + else NoSuchProcess(); - } } goto error; } @@ -201,9 +190,8 @@ psutil_get_arg_list(long pid) // skip ahead to the first argument for (; arg_ptr < arg_end; arg_ptr++) { - if (*arg_ptr != '\0') { + if (*arg_ptr != '\0') break; - } } // iterate through arguments diff --git a/psutil/arch/windows/process_info.c b/psutil/arch/windows/process_info.c index 8298b16c5..ad272bff9 100644 --- a/psutil/arch/windows/process_info.c +++ b/psutil/arch/windows/process_info.c @@ -38,12 +38,10 @@ psutil_handle_from_pid_waccess(DWORD pid, DWORD dwDesiredAccess) hProcess = OpenProcess(dwDesiredAccess, FALSE, pid); if (hProcess == NULL) { - if (GetLastError() == ERROR_INVALID_PARAMETER) { + if (GetLastError() == ERROR_INVALID_PARAMETER) NoSuchProcess(); - } - else { + else PyErr_SetFromWindowsErr(0); - } return NULL; } @@ -129,13 +127,10 @@ psutil_pid_is_running(DWORD pid) DWORD exitCode; // Special case for PID 0 System Idle Process - if (pid == 0) { + if (pid == 0) return 1; - } - - if (pid < 0) { + if (pid < 0) return 0; - } hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); @@ -183,10 +178,8 @@ psutil_pid_in_proclist(DWORD pid) DWORD i; proclist = psutil_get_pids(&numberOfReturnedPIDs); - if (NULL == proclist) { + if (proclist == NULL) return -1; - } - for (i = 0; i < numberOfReturnedPIDs; i++) { if (pid == proclist[i]) { free(proclist); @@ -205,13 +198,12 @@ int handlep_is_running(HANDLE hProcess) { DWORD dwCode; - if (NULL == hProcess) { + + if (NULL == hProcess) return 0; - } if (GetExitCodeProcess(hProcess, &dwCode)) { - if (dwCode == STILL_ACTIVE) { + if (dwCode == STILL_ACTIVE) return 1; - } } return 0; } @@ -236,10 +228,8 @@ psutil_get_arg_list(long pid) PyObject *argList = NULL; hProcess = psutil_handle_from_pid(pid); - if (hProcess == NULL) { + if (hProcess == NULL) return NULL; - } - pebAddress = psutil_get_peb_address(hProcess); // get the address of ProcessParameters @@ -419,9 +409,8 @@ psutil_get_proc_info(DWORD pid, PSYSTEM_PROCESS_INFORMATION *retProcess, goto error; } - if (bufferSize <= 0x20000) { + if (bufferSize <= 0x20000) initialBufferSize = bufferSize; - } process = PH_FIRST_PROCESS(buffer); do { diff --git a/psutil/arch/windows/security.c b/psutil/arch/windows/security.c index a837dfe4e..3aabffd0c 100644 --- a/psutil/arch/windows/security.c +++ b/psutil/arch/windows/security.c @@ -18,9 +18,8 @@ HANDLE psutil_token_from_handle(HANDLE hProcess) { HANDLE hToken = NULL; - if (! OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) { + if (! OpenProcessToken(hProcess, TOKEN_QUERY, &hToken)) return PyErr_SetFromWindowsErr(0); - } return hToken; } @@ -50,10 +49,8 @@ psutil_has_system_privilege(HANDLE hProcess) { TOKEN_PRIVILEGES *tp = NULL; HANDLE hToken = psutil_token_from_handle(hProcess); - if (NULL == hToken) { + if (NULL == hToken) return -1; - } - // call GetTokenInformation first to get the buffer size if (! GetTokenInformation(hToken, TokenPrivileges, NULL, 0, &dwSize)) { dwRetval = GetLastError(); @@ -140,14 +137,11 @@ psutil_set_privilege(HANDLE hToken, LPCTSTR Privilege, BOOL bEnablePrivilege) tpPrevious.PrivilegeCount = 1; tpPrevious.Privileges[0].Luid = luid; - if (bEnablePrivilege) { + if (bEnablePrivilege) tpPrevious.Privileges[0].Attributes |= (SE_PRIVILEGE_ENABLED); - } - - else { + else tpPrevious.Privileges[0].Attributes ^= (SE_PRIVILEGE_ENABLED & tpPrevious.Privileges[0].Attributes); - } AdjustTokenPrivileges( hToken, @@ -213,15 +207,12 @@ psutil_unset_se_debug() &hToken) ) { if (GetLastError() == ERROR_NO_TOKEN) { - if (! ImpersonateSelf(SecurityImpersonation)) { + if (! ImpersonateSelf(SecurityImpersonation)) return 0; - } - if (!OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, FALSE, - &hToken) - ) + &hToken)) { return 0; } @@ -229,9 +220,8 @@ psutil_unset_se_debug() } // now disable SeDebug - if (! psutil_set_privilege(hToken, SE_DEBUG_NAME, FALSE)) { + if (! psutil_set_privilege(hToken, SE_DEBUG_NAME, FALSE)) return 0; - } CloseHandle(hToken); return 1;