summaryrefslogtreecommitdiff
path: root/sendfilemodule.c
blob: 7e70dcd2de89702ce6f1071fda98e20da4846112 (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * $Id$
 */

/*
 * py-sendfile
 *
 * A Python module interface to sendfile(2)
 *
 * Original author:
 *     Copyright (C) 2005 Ben Woolley <user tautolog at gmail>
 *
 * The AIX support code is:
 *     Copyright (C) 2008,2009 Niklas Edmundsson <nikke@acc.umu.se>
 *
 * Currently maintained by Giampaolo Rodola'
 *     Copyright (C) 2011 <g.rodola@gmail.com>
 *
 *
 *  The MIT License
 *
 *  Copyright (c) <2011> <Ben Woolley, Niklas Edmundsson, Giampaolo Rodola'>
 *
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
*/

#include <Python.h>
#include <stdlib.h>

int unsupported = 0;

/* --- begin FreeBSD / Dragonfly / OSX --- */
#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(__APPLE__)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/uio.h>

// needed on OSX - Python < 2.6
#if defined(__APPLE__) && defined(_POSIX_C_SOURCE)
struct sf_hdtr {
    struct iovec *headers;
    int hdr_cnt;
    struct iovec *trailers;
    int trl_cnt;
};
#endif

static PyObject *
method_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
{
    int fd;
    int sock;
    int flags = 0;
    long o,n;
    char * head = NULL;
    size_t head_len = 0;
    char * tail = NULL;
    size_t tail_len = 0;
    struct sf_hdtr hdtr;
    static char *keywords[] = {"out", "in", "offset", "count", "header",
                               "trailer", "flags", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwdict,
                                     "iill|s#s#i:sendfile", keywords,
                                     &fd, &sock, &o, &n, &head, &head_len,
                                     &tail, &tail_len, &flags))
        return NULL;

    off_t offset = o;
    size_t nbytes = n;
    off_t sent;
    int ret;
#ifdef __APPLE__
    sent = nbytes;
#endif
    if (head_len != 0 || tail_len != 0) {
        struct iovec ivh = {head, head_len};
        struct iovec ivt = {tail, tail_len};
        hdtr.headers = &ivh;
        hdtr.hdr_cnt = 1;
        hdtr.trailers = &ivt;
        hdtr.trl_cnt = 1;
        Py_BEGIN_ALLOW_THREADS
#ifdef __APPLE__
        sent += head_len;
        ret = sendfile(sock, fd, offset, &sent, &hdtr, flags);
#else
        ret = sendfile(sock, fd, offset, nbytes, &hdtr, &sent, flags);
#endif
        Py_END_ALLOW_THREADS
    }
    else {
        Py_BEGIN_ALLOW_THREADS
#ifdef __APPLE__
        ret = sendfile(sock, fd, offset, &sent, NULL, flags);
#else
        ret = sendfile(sock, fd, offset, nbytes, NULL, &sent, flags);
#endif
        Py_END_ALLOW_THREADS
    }

    if (ret < 0) {
        if ((errno == EAGAIN) || (errno == EBUSY)) {
            if (sent != 0) {
                // some data has been sent
                goto done;
            }
            else {
                // no data has been sent; upper application is supposed
                // to retry on EAGAIN or EBUSY
                PyErr_SetFromErrno(PyExc_OSError);
                return NULL;
            }
        }
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }

    goto done;

done:
    #if !defined(HAVE_LARGEFILE_SUPPORT)
        return Py_BuildValue("l", sent);
    #else
        return Py_BuildValue("L", sent);
    #endif
}
/* --- end OSX / FreeBSD / Dragonfly --- */

/* --- begin AIX --- */
#elif defined(_AIX)
#include <sys/socket.h>

static PyObject *
method_sendfile(PyObject *self, PyObject *args)
{
    int out_fd, in_fd;
    off_t offset;
    size_t count;
    char *hdr=NULL, *trail=NULL;
    int hdrsize, trailsize;
    ssize_t sts=0;
    struct sf_parms sf_iobuf;
    int rc;

    if (!PyArg_ParseTuple(args, "iiLk|s#s#",
                          &out_fd, &in_fd, &offset, &count, &hdr, &hdrsize,
			  &trail, &trailsize))
        return NULL;

    if(hdr != NULL) {
        sf_iobuf.header_data = hdr;
        sf_iobuf.header_length = hdrsize;
    }
    else {
        sf_iobuf.header_data = NULL;
        sf_iobuf.header_length = 0;
    }
    if(trail != NULL) {
        sf_iobuf.trailer_data = trail;
        sf_iobuf.trailer_length = trailsize;
    }
    else {
	sf_iobuf.trailer_data = NULL;
	sf_iobuf.trailer_length = 0;
    }
    sf_iobuf.file_descriptor = in_fd;
    sf_iobuf.file_offset = offset;
    sf_iobuf.file_bytes = count;

    Py_BEGIN_ALLOW_THREADS;
    do {
	    sf_iobuf.bytes_sent = 0; /* Really needed? */
            rc = send_file(&out_fd, &sf_iobuf, SF_DONT_CACHE);
	    sts += sf_iobuf.bytes_sent;
    } while( rc == 1 || (rc == -1 && errno == EINTR) );
    Py_END_ALLOW_THREADS;

    offset = sf_iobuf.file_offset;

    if (rc == -1) {
        PyErr_SetFromErrno(PyExc_OSError);
        return NULL;
    }
    else {
    #if !defined(HAVE_LARGEFILE_SUPPORT)
        return Py_BuildValue("l", sts);
    #else
        return Py_BuildValue("L", sts);
    #endif
    }
}
/* --- end AIX --- */

/* --- start Linux --- */
#elif defined (__linux__)
#include <sys/sendfile.h>
#include <sys/socket.h>

// these are taken from linux/socket.h, for some reason including
// that file doesn't work here.
#define SOL_TCP 6
#define TCP_CORK 3

static PyObject *
method_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
{
    int out_fd, in_fd;
    off_t offset;
    size_t count;
    char * head = NULL;
    size_t head_len = 0;
    char * tail = NULL;
    size_t tail_len = 0;
    int orig_cork = 1;
    int orig_cork_len = sizeof(int);
    int flags;
    int ret;
    ssize_t sent_h = 0;
    ssize_t sent_f = 0;
    ssize_t sent_t = 0;

    static char *keywords[] = {"out", "in", "offset", "count", "header",
                               "trailer", "flags", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwdict,
                                     "iiLK|s#s#i:sendfile", keywords,
                                     &out_fd, &in_fd, &offset, &count,
                                     &head, &head_len,
                                     &tail, &tail_len, &flags))
        return NULL;

    if (head_len != 0 || tail_len != 0) {
        int cork = 1;
        // first, fetch the original setting
        Py_BEGIN_ALLOW_THREADS
        ret = getsockopt(out_fd, SOL_TCP, TCP_CORK,
                         (void*)&orig_cork, &orig_cork_len);
        Py_END_ALLOW_THREADS
        if (ret == -1)
            return PyErr_SetFromErrno(PyExc_OSError);
        Py_BEGIN_ALLOW_THREADS
        ret = setsockopt(out_fd, SOL_TCP, TCP_CORK, (void*)&cork, sizeof(cork));
        Py_END_ALLOW_THREADS
        if (ret == -1)
            return PyErr_SetFromErrno(PyExc_OSError);
    }

    // send header
    if (head_len != 0) {
        Py_BEGIN_ALLOW_THREADS
        sent_h = send(out_fd, head, head_len, 0);
        Py_END_ALLOW_THREADS
        if (sent_h < 0)
            return PyErr_SetFromErrno(PyExc_OSError);
        else if (sent_h == 0) {
            // A return value of 0 is supposed to be interpreted as EOF
            // being reached. We do not want that and raise EAGAIN instead.
            errno = EAGAIN;
            return PyErr_SetFromErrno(PyExc_OSError);
        }
        else if (sent_h < head_len)
            goto done;
    }

    // send file
    Py_BEGIN_ALLOW_THREADS;
    sent_f = sendfile(out_fd, in_fd, &offset, count);
    Py_END_ALLOW_THREADS;
    if (sent_f == -1)
        return PyErr_SetFromErrno(PyExc_OSError);

    // send trailer
    if (tail_len != 0) {
        Py_BEGIN_ALLOW_THREADS
        sent_t = send(out_fd, tail, tail_len, 0);
        Py_END_ALLOW_THREADS
        if (sent_t < 0)
           return PyErr_SetFromErrno(PyExc_OSError);
        else if (sent_t == 0) {
            // A return value of 0 is supposed to be interpreted as EOF
            // being reached. We do not want that and raise EAGAIN instead.
            errno = EAGAIN;
            return PyErr_SetFromErrno(PyExc_OSError);
        }
    }

    goto done;

done:
#if !defined(HAVE_LARGEFILE_SUPPORT)
    return Py_BuildValue("l", sent_h + sent_f + sent_t);
#else
    return Py_BuildValue("L", sent_h + sent_f + sent_t);
#endif
}
/* --- end Linux --- */

/* --- begin SUN OS --- */
#elif defined(__sun)
#include <sys/sendfile.h>

static PyObject *
method_sendfile(PyObject *self, PyObject *args, PyObject *kwdict)
{
    int out_fd;
    int in_fd;
    off_t offset;
    size_t nbytes;
    ssize_t sent;

    if (!PyArg_ParseTuple(args, "iill", &out_fd, &in_fd, &offset, &nbytes))
        return NULL;
    sent = sendfile(out_fd, in_fd, &offset, nbytes);
    if (sent == -1)
        return PyErr_SetFromErrno(PyExc_OSError);
    return Py_BuildValue("i", sent);
}
#else
/* --- end SUN OS --- */

/* --- being not supported --- */
static PyObject *
method_sendfile(PyObject *self, PyObject *args)
{
    PyErr_SetString(PyExc_NotImplementedError, "platform not supported");
    return NULL;
}
#endif
/* --- end not supported --- */


static PyMethodDef
SendfileMethods[] =
{
    {"sendfile",  method_sendfile,  METH_VARARGS | METH_KEYWORDS,
"sendfile(out, in, offset, nbytes)\n"
"sendfile(out, in, offset, nbytes, header=None, trailer=None, flags=0)\n"
"\n"
"Copy *nbytes* bytes from file descriptor *in* to file descriptor *out*\n"
"starting from *offset* and return the number of bytes sent.\n"
"\n"
"The first function notation is supported by all platforms.\n"
"\n"
"On Linux, if *offset* is given as `None`, the bytes are read from the\n"
"current position of *in* and the position of *in* is updated. It returns\n"
"the same as above with offset being `None`.\n"
"\n"
"The second case may be used on Mac OS X and FreeBSD where *header* and\n"
"*trailer* are arbitrary sequences of buffers that are written before and\n"
"after the data from *in* is written. It returns the same as the first case.\n"
"\n"
"On Mac OS X and FreeBSD, a value of 0 for *nbytes* specifies to send until\n"
"the end of *in* is reached.\n"
"\n"
"On Solaris, *out* may be the file descriptor of a regular file or the file\n"
"descriptor of a socket. On all other platforms, *out* must be the file\n"
"descriptor of an open socket.\n"
"\n"
    },
    {NULL, NULL, 0, NULL}        /* Sentinel */
};

struct module_state {
    PyObject *error;
};

#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif

#if PY_MAJOR_VERSION >= 3
static int
sendfile_traverse(PyObject *m, visitproc visit, void *arg) {
    Py_VISIT(GETSTATE(m)->error);
    return 0;
}

static int
sendfile_clear(PyObject *m) {
    Py_CLEAR(GETSTATE(m)->error);
    return 0;
}

static struct PyModuleDef
moduledef = {
        PyModuleDef_HEAD_INIT,
        "sendfile",
        NULL,
        sizeof(struct module_state),
        SendfileMethods,
        NULL,
        sendfile_traverse,
        sendfile_clear,
        NULL
};

#define INITERROR return NULL

PyObject *
PyInit_sendfile(void)

#else
#define INITERROR return

void initsendfile(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
    PyObject *module = PyModule_Create(&moduledef);
#else
    PyObject *module = Py_InitModule("sendfile", SendfileMethods);
#endif
    // constants
#ifdef SF_NODISKIO
    PyModule_AddIntConstant(module, "SF_NODISKIO", SF_NODISKIO);
#endif
#ifdef SF_MNOWAIT
    PyModule_AddIntConstant(module, "SF_MNOWAIT", SF_MNOWAIT);
#endif
#ifdef SF_SYNC
    PyModule_AddIntConstant(module, "SF_SYNC", SF_SYNC);
#endif

    if (module == NULL) {
        INITERROR;
    }

    if (unsupported == 1) {
        PyErr_SetString(PyExc_NotImplementedError, "platform not supported");
    }

#if PY_MAJOR_VERSION >= 3
    return module;
#endif
}