summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_solaris_sendfilev.c
blob: 1b46d0995bf88839c93436b579b0aacbf91d430e (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

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

#include <nxt_main.h>


/*
 * sendfilev() has been introduced in Solaris 8 (7/01).
 * According to sendfilev(3EXT) it can write to:
 *
 *   a file descriptor to a regular file or to a AF_NCA, AF_INET, or
 *   AF_INET6 family type SOCK_STREAM socket that is open for writing.
 */

ssize_t nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b,
    size_t limit);
static size_t nxt_solaris_buf_coalesce(nxt_buf_t *b, sendfilevec_t *sfv,
    int32_t *nsfv, nxt_bool_t *sync, size_t limit);


ssize_t
nxt_solaris_event_conn_io_sendfilev(nxt_event_conn_t *c, nxt_buf_t *b,
    size_t limit)
{
    size_t         sent;
    ssize_t        n;
    int32_t        nsfv;
    nxt_err_t      err;
    nxt_off_t      size;
    nxt_bool_t     sync;
    sendfilevec_t  sfv[NXT_IOBUF_MAX];

    if (c->sendfile == 0) {
        /* AF_UNIX does not support sendfilev(). */
        return nxt_event_conn_io_sendbuf(c, b, limit);
    }

    sync = 0;

    size = nxt_solaris_buf_coalesce(b, sfv, &nsfv, &sync, limit);

    nxt_debug(c->socket.task, "sendfilev(%d, %D)", c->socket.fd, nsfv);

    if (nsfv == 0 && sync) {
        return 0;
    }

    sent = 0;
    n = sendfilev(c->socket.fd, sfv, nsfv, &sent);

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

    nxt_debug(c->socket.task, "sendfilev(): %d sent:%uz", 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(c->socket.task, nxt_socket_error_level(err),
                    "sendfilev(%d, %D) failed %E", c->socket.fd, nsfv, err);

            return NXT_ERROR;
        }

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

        return sent;
    }

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

    return sent;
}


static size_t
nxt_solaris_buf_coalesce(nxt_buf_t *b, sendfilevec_t *sfv, int32_t *nsfv,
    nxt_bool_t *sync, size_t limit)
{
    size_t     size, total;
    nxt_fd_t   fd, last_fd;
    nxt_int_t  i;
    nxt_off_t  pos, last_pos;

    i = -1;
    last_fd = -1;
    last_pos = 0;
    total = 0;

    for (total = 0; b != NULL && total < limit; b = b->next) {

        if (nxt_buf_is_file(b)) {

            fd = b->file->fd;
            pos = b->file_pos;
            size = b->file_end - pos;

            if (size == 0) {
                continue;
            }

            if (total + size > limit) {
                size = limit - total;
            }

        } else if (nxt_buf_is_mem(b)) {

            fd = SFV_FD_SELF;
            pos = (uintptr_t) b->mem.pos;
            size = b->mem.free - b->mem.pos;

            if (size == 0) {
                continue;
            }

            if (total + size > limit) {
                size = limit - total;
            }

        } else {
            *sync = 1;
            continue;
        }

        if (size == 0) {
            break;
        }

        if (fd != last_fd || pos != last_pos) {

            if (++i >= NXT_IOBUF_MAX) {
                goto done;
            }

            sfv[i].sfv_fd = fd;
            sfv[i].sfv_flag = 0;
            sfv[i].sfv_off = pos;
            sfv[i].sfv_len = size;

        } else {
            sfv[i].sfv_len += size;
        }

        total += size;
        last_pos = pos + size;
        last_fd = fd;
    }

    i++;

done:

    *nsfv = i;

    return total;
}