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 #include <Python.h>
8 
9 // ====================================================================
10 // --- Global vars / constants
11 // ====================================================================
12 
13 extern int PSUTIL_TESTING;
14 extern int PSUTIL_DEBUG;
15 // a signaler for connections without an actual status
16 static const int PSUTIL_CONN_NONE = 128;
17 
18 // strncpy() variant which appends a null terminator.
19 #define PSUTIL_STRNCPY(dst, src, n) \
20     strncpy(dst, src, n - 1); \
21     dst[n - 1] = '\0'
22 
23 // ====================================================================
24 // --- Backward compatibility with missing Python.h APIs
25 // ====================================================================
26 
27 #if PY_MAJOR_VERSION < 3
28     // On Python 2 we just return a plain byte string, which is never
29     // supposed to raise decoding errors, see:
30     // https://github.com/giampaolo/psutil/issues/1040
31     #define PyUnicode_DecodeFSDefault          PyString_FromString
32     #define PyUnicode_DecodeFSDefaultAndSize   PyString_FromStringAndSize
33 #endif
34 
35 #if defined(PSUTIL_WINDOWS) && \
36         defined(PYPY_VERSION) && \
37         !defined(PyErr_SetFromWindowsErrWithFilename)
38     PyObject *PyErr_SetFromWindowsErrWithFilename(int ierr,
39                                                   const char *filename);
40 #endif
41 
42 // --- _Py_PARSE_PID
43 
44 // SIZEOF_INT|LONG is missing on Linux + PyPy (only?).
45 // SIZEOF_PID_T is missing on Windows + Python2.
46 // In this case we guess it from setup.py. It's not 100% bullet proof,
47 // If wrong we'll probably get compiler warnings.
48 // FWIW on all UNIX platforms I've seen pid_t is defined as an int.
49 // _getpid() on Windows also returns an int.
50 #if !defined(SIZEOF_INT)
51     #define SIZEOF_INT 4
52 #endif
53 #if !defined(SIZEOF_LONG)
54     #define SIZEOF_LONG 8
55 #endif
56 #if !defined(SIZEOF_PID_T)
57     #define SIZEOF_PID_T PSUTIL_SIZEOF_PID_T  // set as a macro in setup.py
58 #endif
59 
60 // _Py_PARSE_PID is Python 3 only, but since it's private make sure it's
61 // always present.
62 #ifndef _Py_PARSE_PID
63     #if SIZEOF_PID_T == SIZEOF_INT
64         #define _Py_PARSE_PID "i"
65     #elif SIZEOF_PID_T == SIZEOF_LONG
66         #define _Py_PARSE_PID "l"
67     #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
68         #define _Py_PARSE_PID "L"
69     #else
70         #error "_Py_PARSE_PID: sizeof(pid_t) is neither sizeof(int), "
71                "sizeof(long) or sizeof(long long)"
72     #endif
73 #endif
74 
75 // Python 2 or PyPy on Windows
76 #ifndef PyLong_FromPid
77     #if ((SIZEOF_PID_T == SIZEOF_INT) || (SIZEOF_PID_T == SIZEOF_LONG))
78         #if PY_MAJOR_VERSION >= 3
79             #define PyLong_FromPid PyLong_FromLong
80         #else
81             #define PyLong_FromPid PyInt_FromLong
82         #endif
83     #elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG
84         #define PyLong_FromPid PyLong_FromLongLong
85     #else
86         #error "PyLong_FromPid: sizeof(pid_t) is neither sizeof(int), "
87                "sizeof(long) or sizeof(long long)"
88     #endif
89 #endif
90 
91 // ====================================================================
92 // --- Custom exceptions
93 // ====================================================================
94 
95 PyObject* AccessDenied(const char *msg);
96 PyObject* NoSuchProcess(const char *msg);
97 PyObject* PyErr_SetFromOSErrnoWithSyscall(const char *syscall);
98 
99 // ====================================================================
100 // --- Global utils
101 // ====================================================================
102 
103 PyObject* psutil_set_testing(PyObject *self, PyObject *args);
104 void psutil_debug(const char* format, ...);
105 int psutil_setup(void);
106 
107 // ====================================================================
108 // --- BSD
109 // ====================================================================
110 
111 void convert_kvm_err(const char *syscall, char *errbuf);
112 
113 // ====================================================================
114 // --- Windows
115 // ====================================================================
116 
117 #ifdef PSUTIL_WINDOWS
118     #include <windows.h>
119     // make it available to any file which includes this module
120     #include "arch/windows/ntextapi.h"
121 
122     extern int PSUTIL_WINVER;
123     extern SYSTEM_INFO          PSUTIL_SYSTEM_INFO;
124     extern CRITICAL_SECTION     PSUTIL_CRITICAL_SECTION;
125 
126     #define PSUTIL_WINDOWS_VISTA 60
127     #define PSUTIL_WINDOWS_7 61
128     #define PSUTIL_WINDOWS_8 62
129     #define PSUTIL_WINDOWS_8_1 63
130     #define PSUTIL_WINDOWS_10 100
131     #define PSUTIL_WINDOWS_NEW MAXLONG
132 
133     #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
134     #define MALLOC_ZERO(x) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, (x))
135     #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
136 
137     #define _NT_FACILITY_MASK 0xfff
138     #define _NT_FACILITY_SHIFT 16
139     #define _NT_FACILITY(status) \
140         ((((ULONG)(status)) >> _NT_FACILITY_SHIFT) & _NT_FACILITY_MASK)
141 
142     #define NT_NTWIN32(status) (_NT_FACILITY(status) == FACILITY_WIN32)
143     #define WIN32_FROM_NTSTATUS(status) (((ULONG)(status)) & 0xffff)
144 
145     #define LO_T 1e-7
146     #define HI_T 429.4967296
147 
148     #ifndef AF_INET6
149         #define AF_INET6 23
150     #endif
151 
152     int psutil_load_globals();
153     PVOID psutil_GetProcAddress(LPCSTR libname, LPCSTR procname);
154     PVOID psutil_GetProcAddressFromLib(LPCSTR libname, LPCSTR procname);
155     PVOID psutil_SetFromNTStatusErr(NTSTATUS Status, const char *syscall);
156     double psutil_FiletimeToUnixTime(FILETIME ft);
157     double psutil_LargeIntegerToUnixTime(LARGE_INTEGER li);
158 #endif
159