Skip to content

Instantly share code, notes, and snippets.

@peo3
Created April 21, 2011 11:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save peo3/934279 to your computer and use it in GitHub Desktop.
Save peo3/934279 to your computer and use it in GitHub Desktop.
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