summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_linux_sendfile.c
blob: 0e772ffb33b65c917cb0118c62ff3898a6830186 (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

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

#include <nxt_main.h>


/*
 * sendfile() has been introduced in Linux 2.2.
 * It supported 32-bit offsets only.
 *
 * Linux 2.4.21 has introduced sendfile64().  However, even on 64-bit
 * platforms it returns EINVAL if the count argument is more than 2G-1 bytes.
 * In Linux 2.6.17 sendfile() has been internally changed to splice()
 * and this limitation has gone.
 */

#ifdef NXT_TEST_BUILD_LINUX_SENDFILE

#define MSG_NOSIGNAL      0x4000
#define MSG_MORE          0x8000

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

static ssize_t nxt_sys_sendfile(int out_fd, int in_fd, off_t *offset,
    size_t count)
{
    return -1;
}

#else
#define nxt_sys_sendfile  sendfile
#endif


static ssize_t nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size,
    nxt_uint_t flags);
static ssize_t nxt_linux_sendmsg(nxt_event_conn_t *c,
    nxt_sendbuf_coalesce_t *sb, nxt_uint_t niov, nxt_uint_t flags);


ssize_t
nxt_linux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b,
    size_t limit)
{
    size_t                  size;
    ssize_t                 n;
    nxt_buf_t               *fb;
    nxt_err_t               err;
    nxt_off_t               offset;
    nxt_uint_t              niov, flags;
    struct iovec            iov[NXT_IOBUF_MAX];
    nxt_sendbuf_coalesce_t  sb;

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

    niov = nxt_sendbuf_mem_coalesce(c->socket.task, &sb);

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

    fb = (sb.buf != NULL && nxt_buf_is_file(sb.buf)) ? sb.buf : NULL;

    if (niov != 0) {

        flags = MSG_NOSIGNAL;

        if (fb != NULL) {
            /*
             * The Linux-specific MSG_MORE flag is cheaper
             * than additional setsockopt(TCP_CORK) syscall.
             */
            flags |= MSG_MORE;
        }

        if (niov == 1) {
            /*
             * Disposal of surplus kernel msghdr
             * and iovec copy-in operations.
             */
            return nxt_linux_send(c, iov->iov_base, iov->iov_len, flags);
        }

        return nxt_linux_sendmsg(c, &sb, niov, flags);
    }

    size = nxt_sendbuf_file_coalesce(&sb);

    nxt_debug(c->socket.task, "sendfile(%d, %FD, @%O, %uz)",
              c->socket.fd, fb->file->fd, fb->file_pos, size);

    offset = fb->file_pos;

    n = nxt_sys_sendfile(c->socket.fd, fb->file->fd, &offset, size);

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

    nxt_debug(c->socket.task, "sendfile(): %z", n);

    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(c->socket.task, nxt_socket_error_level(err),
                    "sendfile(%d, %FD, %O, %uz) failed %E \"%FN\"",
                    c->socket.fd, fb->file->fd, fb->file_pos, size,
                    err, fb->file->name);

            return NXT_ERROR;
        }

        nxt_debug(c->socket.task, "sendfile() %E", err);

        return 0;
    }

    if (n < (ssize_t) size) {
        c->socket.write_ready = 0;
    }

    return n;
}


static ssize_t
nxt_linux_send(nxt_event_conn_t *c, void *buf, size_t size, nxt_uint_t flags)
{
    ssize_t    n;
    nxt_err_t  err;

    n = send(c->socket.fd, buf, size, flags);

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

    nxt_debug(c->socket.task, "send(%d, %p, %uz, 0x%uXi): %z",
              c->socket.fd, buf, size, flags, n);

    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(c->socket.task, nxt_socket_error_level(err),
                    "send(%d, %p, %uz, 0x%uXi) failed %E",
                    c->socket.fd, buf, size, flags, err);

            return NXT_ERROR;
        }

        nxt_debug(c->socket.task, "send() %E", err);

        return 0;
    }

    if (n < (ssize_t) size) {
        c->socket.write_ready = 0;
    }

    return n;
}


static ssize_t
nxt_linux_sendmsg(nxt_event_conn_t *c, nxt_sendbuf_coalesce_t *sb,
    nxt_uint_t niov, nxt_uint_t flags)
{
    ssize_t        n;
    nxt_err_t      err;
    struct msghdr  msg;

    msg.msg_name = NULL;
    msg.msg_namelen = 0;
    msg.msg_iov = sb->iobuf;
    msg.msg_iovlen = niov;
    msg.msg_control = NULL;
    msg.msg_controllen = 0;
    msg.msg_flags = 0;

    n = sendmsg(c->socket.fd, &msg, flags);

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

    nxt_debug(c->socket.task, "sendmsg(%d, %ui, 0x%uXi): %z",
              c->socket.fd, niov, flags, n);

    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(c->socket.task, nxt_socket_error_level(err),
                    "sendmsg(%d, %ui, 0x%uXi) failed %E",
                    c->socket.fd, niov, flags, err);

            return NXT_ERROR;
        }

        nxt_debug(c->socket.task, "sendmsg() %E", err);

        return 0;
    }

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

    return n;
}