Skip to content

Instantly share code, notes, and snippets.

@peo3
Created Apr 21, 2011
Embed
What would you like to do?
An extension module to call eventfd(2) in python
/*
* Usage in python:
*
* import linux
* efd = linux.eventfd(0, 0)
* ...
* ret = struct.unpack('Q', os.read(efd, 8))
* ...
* linux.close(efd)
*/
#include <Python.h>
#include <unistd.h>
#include <sys/eventfd.h>
static PyObject *
linux_eventfd(PyObject *self, PyObject *args)
{
int efd, initval, flags;
if (!PyArg_ParseTuple(args, "ii", &initval, &flags))
return NULL;
efd = eventfd(initval, flags);
if (efd == -1) {
return PyErr_SetFromErrno(PyExc_OSError);
} else {
return Py_BuildValue("i", efd);
}
}
static PyObject *
linux_close(PyObject *self, PyObject *args)
{
int ret, fd;
if (!PyArg_ParseTuple(args, "i", &fd))
return NULL;
ret = close(fd);
if (ret == -1) {
return PyErr_SetFromErrno(PyExc_OSError);
} else {
return Py_BuildValue("i", ret);
}
}
static PyMethodDef LinuxSyscalls[] = {
{"eventfd", linux_eventfd, METH_VARARGS,
"Execute eventfd syscall."},
{"close", linux_close, METH_VARARGS,
"Execute close syscall."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initlinux(void)
{
(void) Py_InitModule("linux", LinuxSyscalls);
}
/*
int
main(int argc, char *argv[])
{
Py_SetProgramName(argv[0]);
Py_Initialize();
initlinux();
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment