Debugging: Add code to print backtrace for guest on SIGSEGV
[nativeclient.git] / python_extension / python-nacl.c
blob3840c7a9c915ed99d72fdcb4aedfb1bd618d1ee6
2 #include <Python.h>
4 /* TODO: get this from installed headers */
5 #include "../../../glibc/sysdeps/nacl/nacl_syscalls.h"
7 /* TODO: get these from libnacl rather than hardcoding them */
8 static int (*imc_sendmsg)(int fd, const struct NaClImcMsgHdr *msg, int flags) =
9 NACL_SYSCALL_ADDR(NACL_sys_imc_sendmsg);
10 static int (*imc_recvmsg)(int fd, struct NaClImcMsgHdr *msg, int flags) =
11 NACL_SYSCALL_ADDR(NACL_sys_imc_recvmsg);
14 static PyObject *py_imc_sendmsg(PyObject *self, PyObject *args)
16 int fd;
17 char *data;
18 int data_size;
19 struct NaClImcMsgIoVec iov;
20 struct NaClImcMsgHdr msg;
22 if(!PyArg_ParseTuple(args, "is#", &fd, &data, &data_size))
23 return NULL;
24 iov.base = data;
25 iov.length = data_size;
26 msg.iov = &iov;
27 msg.iov_length = 1;
28 msg.descv = NULL;
29 msg.desc_length = 0;
30 imc_sendmsg(fd, &msg, 0);
31 Py_INCREF(Py_None);
32 return Py_None;
35 static PyObject *py_imc_recvmsg(PyObject *self, PyObject *args)
37 int fd;
38 char buffer[1024];
39 struct NaClImcMsgIoVec iov;
40 struct NaClImcMsgHdr msg;
41 int result;
43 if(!PyArg_ParseTuple(args, "i", &fd))
44 return NULL;
45 iov.base = buffer;
46 iov.length = sizeof(buffer);
47 msg.iov = &iov;
48 msg.iov_length = 1;
49 msg.descv = NULL;
50 msg.desc_length = 0;
51 result = imc_recvmsg(fd, &msg, 0);
52 if(result >= 0)
53 return PyString_FromStringAndSize(buffer, result);
54 else {
55 Py_INCREF(Py_None);
56 return Py_None;
60 static PyMethodDef module_methods[] = {
61 { "imc_sendmsg", py_imc_sendmsg, METH_VARARGS,
62 "NaCl imc_sendmsg() syscall: sends a message" },
63 { "imc_recvmsg", py_imc_recvmsg, METH_VARARGS,
64 "NaCl imc_recvmsg() syscall: receives a message" },
65 { NULL, NULL, 0, NULL } /* Sentinel */
68 void initnacl(void)
70 Py_InitModule3("nacl", module_methods, "NaCl syscall wrappers");