Debian Codegraph @

psutil/arch/posix/proc.c

raw
1 /*
2 * Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 */
6
7
8 #include <signal.h>
9 #include <sys/resource.h>
10
11 #include "../../arch/all/init.h"
12
13 // Check if PID exists. Return values:
14 // 1: exists
15 // 0: does not exist
16 // -1: error (Python exception is set)
17 int
18 psutil_pid_exists(pid_t pid) {
19 int ret;
20
21 // No negative PID exists, plus -1 is an alias for sending signal
22 // too all processes except system ones. Not what we want.
23 if (pid < 0)
24 return 0;
25
26 // As per "man 2 kill" PID 0 is an alias for sending the signal to
27 // every process in the process group of the calling process.
28 // Not what we want. Some platforms have PID 0, some do not.
29 // We decide that at runtime.
30 if (pid == 0) {
31 #if defined(PSUTIL_LINUX) || defined(PSUTIL_FREEBSD)
32 return 0;
33 #else
34 return 1;
35 #endif
36 }
37
38 ret = kill(pid , 0);
39 if (ret == 0)
40 return 1;
41 else {
42 if (errno == ESRCH) {
43 // ESRCH == No such process
44 return 0;
45 }
46 else if (errno == EPERM) {
47 // EPERM clearly indicates there's a process to deny
48 // access to.
49 return 1;
50 }
51 else {
52 // According to "man 2 kill" possible error values are
53 // (EINVAL, EPERM, ESRCH) therefore we should never get
54 // here. If we do let's be explicit in considering this
55 // an error.
56 PyErr_SetFromErrno(PyExc_OSError);
57 return -1;
58 }
59 }
60 }
61
62
63 // Utility used for those syscalls which do not return a meaningful
64 // error that we can translate into an exception which makes sense. As
65 // such, we'll have to guess. On UNIX, if errno is set, we return that
66 // one (OSError). Else, if PID does not exist we assume the syscall
67 // failed because of that so we raise NoSuchProcess. If none of this is
68 // true we giveup and raise RuntimeError(msg). This will always set a
69 // Python exception and return NULL.
70 void
71 psutil_raise_for_pid(pid_t pid, char *syscall) {
72 if (errno != 0)
73 psutil_PyErr_SetFromOSErrnoWithSyscall(syscall);
74 else if (psutil_pid_exists(pid) == 0)
75 NoSuchProcess(syscall);
76 else
77 PyErr_Format(PyExc_RuntimeError, "%s syscall failed", syscall);
78 }
79
80
81 // Get PID priority.
82 PyObject *
83 psutil_posix_getpriority(PyObject *self, PyObject *args) {
84 pid_t pid;
85 int priority;
86 errno = 0;
87
88 if (! PyArg_ParseTuple(args, _Py_PARSE_PID, &pid))
89 return NULL;
90
91 #ifdef PSUTIL_OSX
92 priority = getpriority(PRIO_PROCESS, (id_t)pid);
93 #else
94 priority = getpriority(PRIO_PROCESS, pid);
95 #endif
96 if (errno != 0)
97 return PyErr_SetFromErrno(PyExc_OSError);
98 return Py_BuildValue("i", priority);
99 }
100
101
102 // Set PID priority.
103 PyObject *
104 psutil_posix_setpriority(PyObject *self, PyObject *args) {
105 pid_t pid;
106 int priority;
107 int retval;
108
109 if (! PyArg_ParseTuple(args, _Py_PARSE_PID "i", &pid, &priority))
110 return NULL;
111
112 #ifdef PSUTIL_OSX
113 retval = setpriority(PRIO_PROCESS, (id_t)pid, priority);
114 #else
115 retval = setpriority(PRIO_PROCESS, pid, priority);
116 #endif
117 if (retval == -1)
118 return PyErr_SetFromErrno(PyExc_OSError);
119 Py_RETURN_NONE;
120 }