Commit | Line | Data |
---|---|---|
4942dacd FT |
1 | /* |
2 | ashd - A Sane HTTP Daemon | |
3 | Copyright (C) 2008 Fredrik Tolf <fredrik@dolda2000.com> | |
4 | ||
5 | This program is free software: you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation, either version 3 of the License, or | |
8 | (at your option) any later version. | |
9 | ||
10 | This program is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 | */ | |
18 | ||
19 | #include <Python.h> | |
20 | #include <errno.h> | |
21 | ||
e8b7861a FT |
22 | #include <ashd/utils.h> |
23 | #include <ashd/proc.h> | |
4942dacd FT |
24 | |
25 | static PyObject *p_recvfd(PyObject *self, PyObject *args) | |
26 | { | |
27 | int fd, ret; | |
28 | char *data; | |
29 | size_t dlen; | |
30 | PyObject *ro; | |
31 | ||
32 | fd = 0; | |
33 | if(!PyArg_ParseTuple(args, "|i", &fd)) | |
34 | return(NULL); | |
00b6649e FT |
35 | while(1) { |
36 | Py_BEGIN_ALLOW_THREADS; | |
37 | ret = recvfd(fd, &data, &dlen); | |
38 | Py_END_ALLOW_THREADS; | |
39 | if(ret < 0) { | |
40 | if(errno == 0) | |
41 | return(Py_BuildValue("OO", Py_None, Py_None)); | |
42 | if(errno == EINTR) { | |
43 | if(PyErr_CheckSignals()) | |
44 | return(NULL); | |
45 | continue; | |
46 | } | |
47 | PyErr_SetFromErrno(PyExc_OSError); | |
48 | return(NULL); | |
49 | } | |
50 | ro = Py_BuildValue("Ni", PyString_FromStringAndSize(data, dlen), ret); | |
51 | free(data); | |
52 | return(ro); | |
4942dacd | 53 | } |
4942dacd FT |
54 | } |
55 | ||
56 | static PyObject *p_sendfd(PyObject *self, PyObject *args) | |
57 | { | |
b9f22456 | 58 | int sock, fd, ret; |
173e0e9e | 59 | PyObject *data; |
4942dacd | 60 | |
173e0e9e | 61 | if(!PyArg_ParseTuple(args, "iiO", &sock, &fd, &data)) |
4942dacd | 62 | return(NULL); |
173e0e9e FT |
63 | if(!PyString_Check(data)) { |
64 | PyErr_SetString(PyExc_TypeError, "datagram must be a string"); | |
65 | return(NULL); | |
66 | } | |
00b6649e FT |
67 | while(1) { |
68 | Py_BEGIN_ALLOW_THREADS; | |
69 | ret = sendfd(sock, fd, PyString_AsString(data), PyString_Size(data)); | |
70 | Py_END_ALLOW_THREADS; | |
71 | if(ret < 0) { | |
72 | if(errno == EINTR) { | |
73 | if(PyErr_CheckSignals()) | |
74 | return(NULL); | |
75 | continue; | |
76 | } | |
77 | PyErr_SetFromErrno(PyExc_OSError); | |
78 | return(NULL); | |
79 | } | |
80 | Py_RETURN_NONE; | |
4942dacd | 81 | } |
4942dacd FT |
82 | } |
83 | ||
84 | static PyMethodDef methods[] = { | |
85 | {"recvfd", p_recvfd, METH_VARARGS, "Receive a datagram and a file descriptor"}, | |
86 | {"sendfd", p_sendfd, METH_VARARGS, "Send a datagram and a file descriptor"}, | |
87 | {NULL, NULL, 0, NULL} | |
88 | }; | |
89 | ||
173e0e9e | 90 | PyMODINIT_FUNC inithtlib(void) |
4942dacd | 91 | { |
173e0e9e | 92 | Py_InitModule("ashd.htlib", methods); |
4942dacd | 93 | } |