| 1 |
#include <Python.h> |
| 2 |
#include <stdbool.h> |
| 3 |
#if __APPLE__ |
| 4 |
#include <sys/types.h> |
| 5 |
#endif |
| 6 |
#include <sys/ptrace.h> |
| 7 |
|
| 8 |
#define UNUSED(arg) arg __attribute__((unused)) |
| 9 |
|
| 10 |
char python_ptrace_DOCSTR[] = |
| 11 |
"ptrace(command: int, pid: int, arg1=0, arg2=0, check_errno=False): call ptrace syscall.\r\n" |
| 12 |
"Raise a ValueError on error.\r\n" |
| 13 |
"Returns an unsigned integer.\r\n"; |
| 14 |
|
| 15 |
static bool cpython_cptrace( |
| 16 |
unsigned int request, |
| 17 |
pid_t pid, |
| 18 |
void *arg1, |
| 19 |
void *arg2, |
| 20 |
bool check_errno, |
| 21 |
unsigned long *result) |
| 22 |
{ |
| 23 |
unsigned long ret; |
| 24 |
errno = 0; |
| 25 |
ret = ptrace(request, pid, arg1, arg2); |
| 26 |
if ((long)ret == -1) { |
| 27 |
/** |
| 28 |
* peek operations may returns -1 with errno=0: it's not an error. |
| 29 |
* For other operations, -1 is always an error |
| 30 |
*/ |
| 31 |
if (!check_errno || errno) { |
| 32 |
PyErr_Format( |
| 33 |
PyExc_ValueError, |
| 34 |
"ptrace(request=%u, pid=%i, %p, %p) " |
| 35 |
"error #%i: %s", |
| 36 |
request, pid, arg1, arg2, |
| 37 |
errno, strerror(errno)); |
| 38 |
return false; |
| 39 |
} |
| 40 |
} |
| 41 |
if (result) |
| 42 |
*result = ret; |
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
static PyObject* cpython_ptrace(PyObject* UNUSED(self), PyObject *args, PyObject *keywds) |
| 47 |
{ |
| 48 |
unsigned long result; |
| 49 |
unsigned int request; |
| 50 |
pid_t pid; |
| 51 |
unsigned long arg1 = 0; |
| 52 |
unsigned long arg2 = 0; |
| 53 |
bool check_errno = false; |
| 54 |
PyObject* check_errno_p = NULL; |
| 55 |
static char *kwlist[] = {"request", "pid", "arg1", "arg2", "check_errno", NULL}; |
| 56 |
|
| 57 |
if (!PyArg_ParseTupleAndKeywords(args, keywds, |
| 58 |
"Ii|LLO", kwlist, |
| 59 |
&request, &pid, &arg1, &arg2, &check_errno_p |
| 60 |
)) |
| 61 |
{ |
| 62 |
return NULL; |
| 63 |
} |
| 64 |
|
| 65 |
if (check_errno_p) { |
| 66 |
check_errno = PyObject_IsTrue(check_errno_p); |
| 67 |
} |
| 68 |
|
| 69 |
if (cpython_cptrace(request, pid, (void*)arg1, (void*)arg2, check_errno, &result)) |
| 70 |
return PyLong_FromUnsignedLong(result); |
| 71 |
else |
| 72 |
return NULL; |
| 73 |
} |
| 74 |
|
| 75 |
static PyMethodDef module_methods[] = { |
| 76 |
{"ptrace", (PyCFunction)cpython_ptrace, METH_VARARGS | METH_KEYWORDS, python_ptrace_DOCSTR}, |
| 77 |
{NULL, NULL, 0, NULL} |
| 78 |
}; |
| 79 |
|
| 80 |
PyDoc_STRVAR(module_doc, |
| 81 |
"ptrace module written in C"); |
| 82 |
|
| 83 |
#if PY_MAJOR_VERSION >= 3 |
| 84 |
static struct PyModuleDef module_def = { |
| 85 |
PyModuleDef_HEAD_INIT, |
| 86 |
"cptrace", |
| 87 |
module_doc, |
| 88 |
0, |
| 89 |
module_methods, |
| 90 |
NULL |
| 91 |
}; |
| 92 |
#endif |
| 93 |
|
| 94 |
PyMODINIT_FUNC |
| 95 |
#if PY_MAJOR_VERSION >= 3 |
| 96 |
PyInit_cptrace(void) |
| 97 |
#else |
| 98 |
initcptrace(void) |
| 99 |
#endif |
| 100 |
{ |
| 101 |
#if PY_MAJOR_VERSION >= 3 |
| 102 |
return PyModule_Create(&module_def); |
| 103 |
#else |
| 104 |
(void)Py_InitModule3("cptrace", module_methods, module_doc); |
| 105 |
#endif |
| 106 |
} |
| 107 |
|