| 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 |
// Global names shared by all platforms. |
| 8 |
|
| 9 |
#include <Python.h> |
| 10 |
|
| 11 |
// We do this so that all .c files have to include only one header |
| 12 |
// (ourselves, init.h). |
| 13 |
|
| 14 |
#if defined(PSUTIL_POSIX) |
| 15 |
#include "../../arch/posix/init.h" |
| 16 |
#endif |
| 17 |
#if defined(PSUTIL_BSD) |
| 18 |
#include "../../arch/bsd/init.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
#if defined(PSUTIL_LINUX) |
| 22 |
#include "../../arch/linux/init.h" |
| 23 |
#elif defined(PSUTIL_WINDOWS) |
| 24 |
#include "../../arch/windows/init.h" |
| 25 |
#elif defined(PSUTIL_OSX) |
| 26 |
#include "../../arch/osx/init.h" |
| 27 |
#elif defined(PSUTIL_FREEBSD) |
| 28 |
#include "../../arch/freebsd/init.h" |
| 29 |
#elif defined(PSUTIL_OPENSBD) |
| 30 |
#include "../../arch/openbsd/init.h" |
| 31 |
#elif defined(PSUTIL_NETBSD) |
| 32 |
#include "../../arch/netbsd/init.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
// print debug messages when set to 1 |
| 36 |
extern int PSUTIL_DEBUG; |
| 37 |
// a signaler for connections without an actual status |
| 38 |
extern int PSUTIL_CONN_NONE; |
| 39 |
|
| 40 |
#ifdef Py_GIL_DISABLED |
| 41 |
extern PyMutex utxent_lock; |
| 42 |
#define UTXENT_MUTEX_LOCK() PyMutex_Lock(&utxent_lock) |
| 43 |
#define UTXENT_MUTEX_UNLOCK() PyMutex_Unlock(&utxent_lock) |
| 44 |
#else |
| 45 |
#define UTXENT_MUTEX_LOCK() |
| 46 |
#define UTXENT_MUTEX_UNLOCK() |
| 47 |
#endif |
| 48 |
|
| 49 |
// Print a debug message on stderr. |
| 50 |
#define psutil_debug(...) do { \ |
| 51 |
if (! PSUTIL_DEBUG) \ |
| 52 |
break; \ |
| 53 |
fprintf(stderr, "psutil-debug [%s:%d]> ", __FILE__, __LINE__); \ |
| 54 |
fprintf(stderr, __VA_ARGS__); \ |
| 55 |
fprintf(stderr, "\n");} while(0) |
| 56 |
|
| 57 |
|
| 58 |
// strncpy() variant which appends a null terminator. |
| 59 |
#define PSUTIL_STRNCPY(dst, src, n) \ |
| 60 |
strncpy(dst, src, n - 1); \ |
| 61 |
dst[n - 1] = '\0' |
| 62 |
|
| 63 |
// ==================================================================== |
| 64 |
// --- Custom exceptions |
| 65 |
// ==================================================================== |
| 66 |
|
| 67 |
PyObject* AccessDenied(const char *msg); |
| 68 |
PyObject* NoSuchProcess(const char *msg); |
| 69 |
PyObject* psutil_PyErr_SetFromOSErrnoWithSyscall(const char *syscall); |
| 70 |
|
| 71 |
// ==================================================================== |
| 72 |
// --- Backward compatibility with missing Python.h APIs |
| 73 |
// ==================================================================== |
| 74 |
|
| 75 |
// --- _Py_PARSE_PID |
| 76 |
|
| 77 |
// SIZEOF_INT|LONG is missing on Linux + PyPy (only?). |
| 78 |
// In this case we guess it from setup.py. It's not 100% bullet proof, |
| 79 |
// If wrong we'll probably get compiler warnings. |
| 80 |
// FWIW on all UNIX platforms I've seen pid_t is defined as an int. |
| 81 |
// _getpid() on Windows also returns an int. |
| 82 |
#if !defined(SIZEOF_INT) |
| 83 |
#define SIZEOF_INT 4 |
| 84 |
#endif |
| 85 |
#if !defined(SIZEOF_LONG) |
| 86 |
#define SIZEOF_LONG 8 |
| 87 |
#endif |
| 88 |
#if !defined(SIZEOF_PID_T) |
| 89 |
#define SIZEOF_PID_T PSUTIL_SIZEOF_PID_T // set as a macro in setup.py |
| 90 |
#endif |
| 91 |
|
| 92 |
// _Py_PARSE_PID was added in Python 3, but since it's private we make |
| 93 |
// sure it's always present. |
| 94 |
#ifndef _Py_PARSE_PID |
| 95 |
#if SIZEOF_PID_T == SIZEOF_INT |
| 96 |
#define _Py_PARSE_PID "i" |
| 97 |
#elif SIZEOF_PID_T == SIZEOF_LONG |
| 98 |
#define _Py_PARSE_PID "l" |
| 99 |
#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG |
| 100 |
#define _Py_PARSE_PID "L" |
| 101 |
#else |
| 102 |
#error "_Py_PARSE_PID: sizeof(pid_t) is neither sizeof(int), " |
| 103 |
"sizeof(long) or sizeof(long long)" |
| 104 |
#endif |
| 105 |
#endif |
| 106 |
|
| 107 |
// PyPy on Windows |
| 108 |
#ifndef PyLong_FromPid |
| 109 |
#if ((SIZEOF_PID_T == SIZEOF_INT) || (SIZEOF_PID_T == SIZEOF_LONG)) |
| 110 |
#define PyLong_FromPid PyLong_FromLong |
| 111 |
#elif defined(SIZEOF_LONG_LONG) && SIZEOF_PID_T == SIZEOF_LONG_LONG |
| 112 |
#define PyLong_FromPid PyLong_FromLongLong |
| 113 |
#else |
| 114 |
#error "PyLong_FromPid: sizeof(pid_t) is neither sizeof(int), " |
| 115 |
"sizeof(long) or sizeof(long long)" |
| 116 |
#endif |
| 117 |
#endif |
| 118 |
|
| 119 |
PyObject* psutil_set_debug(PyObject *self, PyObject *args); |
| 120 |
PyObject* psutil_check_pid_range(PyObject *self, PyObject *args); |
| 121 |
int psutil_setup(void); |