Files
Vehicle Researcher 6adb63b915 openpilot v0.11.1 release
date: 2026-06-04T09:49:56
master commit: c0ab3550eca2e9daf197c46b7e4b24aa9637cf2e
2026-06-04 09:50:05 -07:00

80 lines
2.2 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
__attribute__((constructor))
void preload_init() {
Py_Initialize();
PyRun_SimpleString("print('hello from c'); import extra.dsp.hook");
}
#define _GNU_SOURCE // Must be defined before any includes for RTLD_NEXT
#include <stdio.h>
#include <dlfcn.h>
#include <Python.h> // Include Python header
//#include <sys/ioctl.h>
// Define the original ioctl function pointer
static int (*real_ioctl)(int fd, unsigned long request, void *arg) = NULL;
// Our custom ioctl hook
int ioctl(int fd, unsigned long request, void *arg) {
// Initialize the real ioctl function pointer on first call
if (!real_ioctl) {
real_ioctl = dlsym(RTLD_NEXT, "ioctl");
if (!real_ioctl) {
fprintf(stderr, "Error: Could not find real ioctl\n");
return -1;
}
}
// Log the call
//printf("Hooked ioctl: tid=%d fd=%d, request=0x%lx, arg=%p\n", gettid(), fd, request, arg);
// Call a Python function from extra.dsp.hook
PyObject *pName, *pModule, *pFunc, *pArgs, *pValue;
PyGILState_STATE gstate;
// Ensure the GIL is held (required for Python calls in multi-threaded apps)
//gstate = PyGILState_Ensure();
// Import the module
pName = PyUnicode_FromString("extra.dsp.hook");
pModule = PyImport_Import(pName);
Py_DECREF(pName);
// Call the original ioctl
int ret = real_ioctl(fd, request, arg);
if (pModule != NULL) {
// Get the function (assume its called "handle_ioctl")
pFunc = PyObject_GetAttrString(pModule, "handle_ioctl");
if (pFunc && PyCallable_Check(pFunc)) {
// Create arguments tuple (fd, request, arg, ret)
pArgs = PyTuple_Pack(4,
PyLong_FromLong(fd),
PyLong_FromUnsignedLong(request),
PyLong_FromVoidPtr(arg),
PyLong_FromLong(ret));
pValue = PyObject_CallObject(pFunc, pArgs);
Py_DECREF(pArgs);
if (pValue != NULL) {
Py_DECREF(pValue);
} else {
PyErr_Print(); // Print Python error if call fails
}
Py_DECREF(pFunc);
} else {
if (PyErr_Occurred()) PyErr_Print();
fprintf(stderr, "Cannot find function 'handle_ioctl'\n");
}
Py_DECREF(pModule);
} else {
PyErr_Print();
fprintf(stderr, "Failed to load 'extra.dsp.hook'\n");
}
// Release the GIL
//PyGILState_Release(gstate);
return ret;
}