Debian Codegraph @

psutil/arch/windows/proc_utils.c

raw
1 /*
2 * Copyright (c) 2009, Jay Loden, 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 * Helper process functions.
7 */
8
9 #include <Python.h>
10 #include <windows.h>
11 #include <Psapi.h> // EnumProcesses
12
13 #include "../../arch/all/init.h"
14
15
16 DWORD *
17 psutil_get_pids(DWORD *numberOfReturnedPIDs) {
18 // Win32 SDK says the only way to know if our process array
19 // wasn't large enough is to check the returned size and make
20 // sure that it doesn't match the size of the array.
21 // If it does we allocate a larger array and try again
22
23 // Stores the actual array
24 DWORD *procArray = NULL;
25 DWORD procArrayByteSz;
26 int procArraySz = 0;
27
28 // Stores the byte size of the returned array from enumprocesses
29 DWORD enumReturnSz = 0;
30
31 do {
32 procArraySz += 1024;
33 if (procArray != NULL)
34 free(procArray);
35 procArrayByteSz = procArraySz * sizeof(DWORD);
36 procArray = malloc(procArrayByteSz);
37 if (procArray == NULL) {
38 PyErr_NoMemory();
39 return NULL;
40 }
41 if (! EnumProcesses(procArray, procArrayByteSz, &enumReturnSz)) {
42 free(procArray);
43 PyErr_SetFromWindowsErr(0);
44 return NULL;
45 }
46 } while (enumReturnSz == procArraySz * sizeof(DWORD));
47
48 // The number of elements is the returned size / size of each element
49 *numberOfReturnedPIDs = enumReturnSz / sizeof(DWORD);
50
51 return procArray;
52 }
53
54
55 // Return 1 if PID exists, 0 if not, -1 on error.
56 int
57 psutil_pid_in_pids(DWORD pid) {
58 DWORD *proclist = NULL;
59 DWORD numberOfReturnedPIDs;
60 DWORD i;
61
62 proclist = psutil_get_pids(&numberOfReturnedPIDs);
63 if (proclist == NULL) {
64 psutil_debug("psutil_get_pids() failed");
65 return -1;
66 }
67 for (i = 0; i < numberOfReturnedPIDs; i++) {
68 if (proclist[i] == pid) {
69 free(proclist);
70 return 1;
71 }
72 }
73 free(proclist);
74 return 0;
75 }
76
77
78 // Given a process handle checks whether it's actually running. If it
79 // does return the handle, else return NULL with Python exception set.
80 // This is needed because OpenProcess API sucks.
81 HANDLE
82 psutil_check_phandle(HANDLE hProcess, DWORD pid, int check_exit_code) {
83 DWORD exitCode;
84
85 if (hProcess == NULL) {
86 if (GetLastError() == ERROR_INVALID_PARAMETER) {
87 // Yeah, this is the actual error code in case of
88 // "no such process".
89 NoSuchProcess("OpenProcess -> ERROR_INVALID_PARAMETER");
90 return NULL;
91 }
92 if (GetLastError() == ERROR_SUCCESS) {
93 // Yeah, it's this bad.
94 // https://github.com/giampaolo/psutil/issues/1877
95 if (psutil_pid_in_pids(pid) == 1) {
96 psutil_debug("OpenProcess -> ERROR_SUCCESS turned into AD");
97 AccessDenied("OpenProcess -> ERROR_SUCCESS");
98 }
99 else {
100 psutil_debug("OpenProcess -> ERROR_SUCCESS turned into NSP");
101 NoSuchProcess("OpenProcess -> ERROR_SUCCESS");
102 }
103 return NULL;
104 }
105 psutil_PyErr_SetFromOSErrnoWithSyscall("OpenProcess");
106 return NULL;
107 }
108
109 if (check_exit_code == 0)
110 return hProcess;
111
112 if (GetExitCodeProcess(hProcess, &exitCode)) {
113 // XXX - maybe STILL_ACTIVE is not fully reliable as per:
114 // http://stackoverflow.com/questions/1591342/#comment47830782_1591379
115 if (exitCode == STILL_ACTIVE) {
116 return hProcess;
117 }
118 if (psutil_pid_in_pids(pid) == 1) {
119 return hProcess;
120 }
121 CloseHandle(hProcess);
122 NoSuchProcess("GetExitCodeProcess != STILL_ACTIVE");
123 return NULL;
124 }
125
126 if (GetLastError() == ERROR_ACCESS_DENIED) {
127 psutil_debug("GetExitCodeProcess -> ERROR_ACCESS_DENIED (ignored)");
128 SetLastError(0);
129 return hProcess;
130 }
131 psutil_PyErr_SetFromOSErrnoWithSyscall("GetExitCodeProcess");
132 CloseHandle(hProcess);
133 return NULL;
134 }
135
136
137 // A wrapper around OpenProcess setting NSP exception if process no
138 // longer exists. *pid* is the process PID, *access* is the first
139 // argument to OpenProcess.
140 // Return a process handle or NULL with exception set.
141 HANDLE
142 psutil_handle_from_pid(DWORD pid, DWORD access) {
143 HANDLE hProcess;
144
145 if (pid == 0) {
146 // otherwise we'd get NoSuchProcess
147 return AccessDenied("automatically set for PID 0");
148 }
149
150 hProcess = OpenProcess(access, FALSE, pid);
151
152 if ((hProcess == NULL) && (GetLastError() == ERROR_ACCESS_DENIED)) {
153 psutil_PyErr_SetFromOSErrnoWithSyscall("OpenProcess");
154 return NULL;
155 }
156
157 hProcess = psutil_check_phandle(hProcess, pid, 1);
158 return hProcess;
159 }
160
161
162 // Check for PID existence. Return 1 if pid exists, 0 if not, -1 on error.
163 int
164 psutil_pid_is_running(DWORD pid) {
165 HANDLE hProcess;
166
167 // Special case for PID 0 System Idle Process
168 if (pid == 0)
169 return 1;
170 if (pid < 0)
171 return 0;
172
173 hProcess = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
174
175 if (hProcess != NULL) {
176 hProcess = psutil_check_phandle(hProcess, pid, 1);
177 if (hProcess != NULL) {
178 CloseHandle(hProcess);
179 return 1;
180 }
181 CloseHandle(hProcess);
182 }
183
184 PyErr_Clear();
185 return psutil_pid_in_pids(pid);
186 }