summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_macosx_sendfile.c
blob: 94a767183e891749065507427bd0b14fb323c5ca (plain) (blame)
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

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


/* sendfile() has been introduced in MacOSX 10.5 (Leopard) */

#ifdef NXT_TEST_BUILD_MACOSX_SENDFILE

ssize_t nxt_macosx_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b,
    size_t limit);

static int nxt_sys_sendfile(int fd, int s, off_t offset, off_t *len,
    struct sf_hdtr *hdtr, int flags)
{
    return -1;
}

#else
#define nxt_sys_sendfile  sendfile
#endif


ssize_t
nxt_macosx_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b,
    size_t limit)
{
    size_t                  hd_size, file_size;
    ssize_t                 n;
    nxt_buf_t               *fb;
    nxt_err_t               err;
    nxt_off_t               sent;
    nxt_uint_t              nhd, ntr;
    struct iovec            hd[NXT_IOBUF_MAX], tr[NXT_IOBUF_MAX];
    struct sf_hdtr          hdtr, *ht;
    nxt_sendbuf_coalesce_t  sb;

    sb.buf = b;
    sb.iobuf = hd;
    sb.nmax = NXT_IOBUF_MAX;
    sb.sync = 0;
    sb.size = 0;
    sb.limit = limit;

    nhd = nxt_sendbuf_mem_coalesce(&sb);

    if (nhd == 0 && sb.sync) {
        return 0;
    }

    if (sb.buf == NULL || !nxt_buf_is_file(sb.buf)) {
        return nxt_event_conn_io_writev(c, hd, nhd);
    }

    hd_size = sb.size;
    fb = sb.buf;

    file_size = nxt_sendbuf_file_coalesce(&sb);

    if (file_size == 0) {
        return nxt_event_conn_io_writev(c, hd, nhd);
    }

    sb.iobuf = tr;

    ntr = nxt_sendbuf_mem_coalesce(&sb);

    /*
     * Disposal of surplus kernel operations if there are no headers
     * and trailers.  Besides sendfile() returns EINVAL if a sf_hdtr's
     * count is 0, but corresponding pointer is not NULL.
     */

    nxt_memzero(&hdtr, sizeof(struct sf_hdtr));
    ht = NULL;

    if (nhd != 0) {
        ht = &hdtr;
        hdtr.headers = hd;
        hdtr.hdr_cnt = nhd;
    }

    if (ntr != 0) {
        ht = &hdtr;
        hdtr.trailers = tr;
        hdtr.trl_cnt = ntr;
    }

    /*
     * MacOSX has the same bug as old FreeBSD (http://bugs.freebsd.org/33771).
     * However this bug has never been fixed and instead of this it has been
     * documented as a feature in MacOSX 10.7 (Lion) sendfile(2):
     *
     *   When a header or trailer is specified, the value of len argument
     *   indicates the maximum number of bytes in the header and/or file
     *   to be sent.  It does not control the trailer; if a trailer exists,
     *   all of it will be sent.
     */
    sent = hd_size + file_size;

    nxt_log_debug(c->socket.log,
                  "sendfile(%FD, %d, @%O, %O) hd:%ui tr:%ui hs:%uz",
                  fb->file->fd, c->socket.fd, fb->file_pos, sent,
                  nhd, ntr, hd_size);

    n = nxt_sys_sendfile(fb->file->fd, c->socket.fd,
                         fb->file_pos, &sent, ht, 0);

    err = (n == -1) ? nxt_errno : 0;

    nxt_log_debug(c->socket.log, "sendfile(): %d sent:%O", n, sent);

    if (n == -1) {
        switch (err) {

        case NXT_EAGAIN:
            c->socket.write_ready = 0;
            break;

        case NXT_EINTR:
            break;

        default:
            c->socket.error = err;
            nxt_log_error(nxt_socket_error_level(err, c->socket.log_error),
                          c->socket.log, "sendfile(%FD, %d, %O, %O) failed "
                          "%E \"%FN\" hd:%ui tr:%ui", fb->file->fd,
                          c->socket.fd, fb->file_pos, sent, err,
                          fb->file->name, nhd, ntr);

            return NXT_ERROR;
        }

        nxt_log_debug(c->socket.log, "sendfile() %E", err);

        return sent;
    }

    if (sent == 0) {
        nxt_log_error(NXT_LOG_ERR, c->socket.log,
                      "file \"%FN\" was truncated while sendfile()",
                      fb->file->name);

        return NXT_ERROR;
    }

    if (sent < (nxt_off_t) sb.size) {
        c->socket.write_ready = 0;
    }

    return sent;
}


#if 0

typedef struct {
    nxt_socket_t  socket;
    nxt_err_t     error;

    uint8_t       write_ready;  /* 1 bit */
    uint8_t       log_error;
} nxt_sendbuf_t;


ssize_t nxt_macosx_sendfile(nxt_thread_t *thr, nxt_sendbuf_t *sb, nxt_buf_t *b,
    size_t limit);
ssize_t nxt_writev(nxt_thread_t *thr, nxt_sendbuf_t *sb, nxt_iobuf_t *iob,
    nxt_uint_t niob);
ssize_t nxt_send(nxt_thread_t *thr, nxt_sendbuf_t *sb, void *buf, size_t size);


ssize_t
nxt_macosx_sendfile(nxt_thread_t *thr, nxt_sendbuf_t *sb, nxt_buf_t *b,
    size_t limit)
{
    size_t                  hd_size, file_size;
    ssize_t                 n;
    nxt_buf_t               *buf;
    nxt_err_t               err;
    nxt_off_t               sent;
    nxt_uint_t              nhd, ntr;
    struct iovec            hd[NXT_IOBUF_MAX], tr[NXT_IOBUF_MAX];
    struct sf_hdtr          hdtr, *ht;
    nxt_sendbuf_coalesce_t  sbc;

    sbc.buf = b;
    sbc.iobuf = hd;
    sbc.nmax = NXT_IOBUF_MAX;
    sbc.sync = 0;
    sbc.size = 0;
    sbc.limit = limit;

    nhd = nxt_sendbuf_mem_coalesce(&sbc);

    if (nhd == 0 && sbc.sync) {
        return 0;
    }

    if (sbc.buf == NULL || !nxt_buf_is_file(sbc.buf)) {
        return nxt_writev(thr, sb, hd, nhd);
    }

    hd_size = sbc.size;
    buf = sbc.buf;

    file_size = nxt_sendbuf_file_coalesce(&sbc);

    if (file_size == 0) {
        return nxt_writev(thr, sb, hd, nhd);
    }

    sbc.iobuf = tr;

    ntr = nxt_sendbuf_mem_coalesce(&sbc);

    /*
     * Disposal of surplus kernel operations if there are no headers
     * and trailers.  Besides sendfile() returns EINVAL if a sf_hdtr's
     * count is 0, but corresponding pointer is not NULL.
     */

    nxt_memzero(&hdtr, sizeof(struct sf_hdtr));
    ht = NULL;

    if (nhd != 0) {
        ht = &hdtr;
        hdtr.headers = hd;
        hdtr.hdr_cnt = nhd;
    }

    if (ntr != 0) {
        ht = &hdtr;
        hdtr.trailers = tr;
        hdtr.trl_cnt = ntr;
    }

    /*
     * MacOSX has the same bug as old FreeBSD (http://bugs.freebsd.org/33771).
     * However this bug has never been fixed and instead of this it has been
     * documented as a feature in MacOSX 10.7 (Lion) sendfile(2):
     *
     *   When a header or trailer is specified, the value of len argument
     *   indicates the maximum number of bytes in the header and/or file
     *   to be sent.  It does not control the trailer; if a trailer exists,
     *   all of it will be sent.
     */
    sent = hd_size + file_size;

    nxt_log_debug(thr->log, "sendfile(%FD, %d, @%O, %O) hd:%ui tr:%ui hs:%uz",
                  buf->file->fd, sb->socket, buf->file_pos, sent,
                  nhd, ntr, hd_size);

    n = nxt_sys_sendfile(buf->file->fd, sb->socket,
                         buf->file_pos, &sent, ht, 0);

    err = (n == -1) ? nxt_errno : 0;

    nxt_log_debug(thr->log, "sendfile(): %d sent:%O", n, sent);

    if (n == -1) {
        switch (err) {

        case NXT_EAGAIN:
            sb->write_ready = 0;
            break;

        case NXT_EINTR:
            break;

        default:
            sb->error = err;
            nxt_log_error(nxt_socket_error_level(err, sb->log_error), thr->log,
                          "sendfile(%FD, %d, %O, %O) failed %E \"%FN\" "
                          "hd:%ui tr:%ui", buf->file->fd, sb->socket,
                          buf->file_pos, sent, err, buf->file->name, nhd, ntr);

            return NXT_ERROR;
        }

        nxt_log_debug(thr->log, "sendfile() %E", err);

        return sent;
    }

    if (sent == 0) {
        nxt_log_error(NXT_LOG_ERR, thr->log,
                      "file \"%FN\" was truncated while sendfile()",
                      buf->file->name);

        return NXT_ERROR;
    }

    if (sent < (nxt_off_t) sbc.size) {
        sb->write_ready = 0;
    }

    return sent;
}


ssize_t
nxt_writev(nxt_thread_t *thr, nxt_sendbuf_t *sb, nxt_iobuf_t *iob,
    nxt_uint_t niob)
{
    ssize_t    n;
    nxt_err_t  err;

    if (niob == 1) {
        /* Disposal of surplus kernel iovec copy-in operation. */
        return nxt_send(thr, sb, iob->iov_base, iob->iov_len);
    }

    for ( ;; ) {
        n = writev(sb->socket, iob, niob);

        err = (n == -1) ? nxt_socket_errno : 0;

        nxt_log_debug(thr->log, "writev(%d, %ui): %d", sb->socket, niob, n);

        if (n > 0) {
            return n;
        }

        /* n == -1 */

        switch (err) {

        case NXT_EAGAIN:
            nxt_log_debug(thr->log, "writev() %E", err);
            sb->write_ready = 0;
            return NXT_AGAIN;

        case NXT_EINTR:
            nxt_log_debug(thr->log, "writev() %E", err);
            continue;

        default:
            sb->error = err;
            nxt_log_error(nxt_socket_error_level(err, sb->log_error), thr->log,
                          "writev(%d, %ui) failed %E", sb->socket, niob, err);
            return NXT_ERROR;
        }
    }
}


ssize_t
nxt_send(nxt_thread_t *thr, nxt_sendbuf_t *sb, void *buf, size_t size)
{
    ssize_t    n;
    nxt_err_t  err;

    for ( ;; ) {
        n = send(sb->socket, buf, size, 0);

        err = (n == -1) ? nxt_socket_errno : 0;

        nxt_log_debug(thr->log, "send(%d, %p, %uz): %z",
                      sb->socket, buf, size, n);

        if (n > 0) {
            return n;
        }

        /* n == -1 */

        switch (err) {

        case NXT_EAGAIN:
            nxt_log_debug(thr->log, "send() %E", err);
            sb->write_ready = 0;
            return NXT_AGAIN;

        case NXT_EINTR:
            nxt_log_debug(thr->log, "send() %E", err);
            continue;

        default:
            sb->error = err;
            nxt_log_error(nxt_socket_error_level(err, sb->log_error), thr->log,
                          "send(%d, %p, %uz) failed %E",
                          sb->socket, buf, size, err);
            return NXT_ERROR;
        }
    }
}

#endif