1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
/*
* Passing file descriptions with Python. Tested with Linux and FreeBSD.
* Should also work on Solaris. Portability fixes or success stories welcome.
*
* Neil Schemenauer <nas@arctrix.com>
*/
#include "Python.h"
#ifndef __OpenBSD__
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif
#ifndef _XOPEN_SOURCE_EXTENDED
#define _XOPEN_SOURCE_EXTENDED 1 /* Solaris <= 2.7 needs this too */
#endif
#endif /* __OpenBSD__ */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>
#include <stddef.h>
static int
recv_fd(int sockfd)
{
char tmp[CMSG_SPACE(sizeof(int))];
struct cmsghdr *cmsg;
struct iovec iov;
struct msghdr msg;
char ch = '\0';
memset(&msg, 0, sizeof(msg));
iov.iov_base = &ch;
iov.iov_len = 1;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = tmp;
msg.msg_controllen = sizeof(tmp);
if (recvmsg(sockfd, &msg, 0) <= 0)
return -1;
cmsg = CMSG_FIRSTHDR(&msg);
return *(int *) CMSG_DATA(cmsg);
}
static int
send_fd (int sockfd, int fd)
{
char tmp[CMSG_SPACE(sizeof(int))];
struct cmsghdr *cmsg;
struct iovec iov;
struct msghdr msg;
char ch = '\0';
memset(&msg, 0, sizeof(msg));
msg.msg_control = (caddr_t) tmp;
msg.msg_controllen = CMSG_LEN(sizeof(int));
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
*(int *)CMSG_DATA(cmsg) = fd;
iov.iov_base = &ch;
iov.iov_len = 1;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
if (sendmsg(sockfd, &msg, 0) != 1)
return -1;
return 0;
}
static char sendfd_doc [] =
"sendfd(sockfd, fd)";
static PyObject *
passfd_sendfd(PyObject *self, PyObject *args)
{
int sockfd, fd;
if (!PyArg_ParseTuple(args, "ii:sendfd", &sockfd, &fd))
return NULL;
if (send_fd(sockfd, fd) < 0) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
Py_INCREF(Py_None);
return Py_None;
}
static char recvfd_doc [] =
"recvfd(sockfd) -> fd";
static PyObject *
passfd_recvfd(PyObject *self, PyObject *args)
{
int sockfd, fd;
if (!PyArg_ParseTuple(args, "i:revcfd", &sockfd))
return NULL;
if ((fd = recv_fd(sockfd)) < 0) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return PyInt_FromLong((long) fd);
}
static char socketpair_doc [] =
"socketpair(family, type, proto=0) -> (fd, fd)";
static PyObject *
passfd_socketpair(PyObject *self, PyObject *args)
{
int family, type, proto=0;
int fd[2];
if (!PyArg_ParseTuple(args, "ii|i:socketpair", &family, &type, &proto))
return NULL;
if (socketpair(family, type, proto, fd) < 0) {
PyErr_SetFromErrno(PyExc_IOError);
return NULL;
}
return Py_BuildValue("(ii)", (long) fd[0], (long) fd[1]);
}
/* List of functions */
static PyMethodDef passfd_methods[] = {
{"sendfd", passfd_sendfd, METH_VARARGS, sendfd_doc},
{"recvfd", passfd_recvfd, METH_VARARGS, recvfd_doc},
{"socketpair", passfd_socketpair, METH_VARARGS, socketpair_doc},
{NULL, NULL} /* sentinel */
};
DL_EXPORT(void)
initpassfd(void)
{
PyObject *m;
/* Create the module and add the functions and documentation */
m = Py_InitModule3("passfd", passfd_methods, NULL);
}
|