diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-01-17 20:00:00 +0300 |
commit | 16cbf3c076a0aca6d47adaf3f719493674cf2363 (patch) | |
tree | e6530480020f62a2bdbf249988ec3e2a751d3927 /src/nxt_hpux_sendfile.c | |
download | unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.gz unit-16cbf3c076a0aca6d47adaf3f719493674cf2363.tar.bz2 |
Initial version.
Diffstat (limited to 'src/nxt_hpux_sendfile.c')
-rw-r--r-- | src/nxt_hpux_sendfile.c | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/nxt_hpux_sendfile.c b/src/nxt_hpux_sendfile.c new file mode 100644 index 00000000..a105b684 --- /dev/null +++ b/src/nxt_hpux_sendfile.c @@ -0,0 +1,138 @@ + +/* + * Copyright (C) Igor Sysoev + * Copyright (C) NGINX, Inc. + */ + +#include <nxt_main.h> + + +#ifdef NXT_TEST_BUILD_HPUX_SENDFILE + +ssize_t nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, + size_t limit); + +static ssize_t nxt_sys_sendfile(int s, int fd, off_t offset, size_t nbytes, + const struct iovec *hdtrl, int flags) +{ + return -1; +} + +#else + +/* sendfile() is not declared if _XOPEN_SOURCE_EXTENDED is defined. */ + +sbsize_t sendfile(int s, int fd, off_t offset, bsize_t nbytes, + const struct iovec *hdtrl, int flags); + +#define nxt_sys_sendfile sendfile + +#endif + + +ssize_t +nxt_hpux_event_conn_io_sendfile(nxt_event_conn_t *c, nxt_buf_t *b, size_t limit) +{ + size_t file_size; + ssize_t n; + nxt_buf_t *fb; + nxt_err_t err; + nxt_uint_t nhd, ntr; + struct iovec iov[NXT_IOBUF_MAX], *hdtrl; + 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; + + nhd = nxt_sendbuf_mem_coalesce(&sb); + + if (nhd == 0 && sb.sync) { + return 0; + } + + if (nhd > 1 || sb.buf == NULL || !nxt_buf_is_file(sb.buf)) { + return nxt_event_conn_io_writev(c, iov, nhd); + } + + fb = sb.buf; + + file_size = nxt_sendbuf_file_coalesce(&sb); + + if (file_size == 0) { + return nxt_event_conn_io_writev(c, iov, nhd); + } + + sb.iobuf = &iov[1]; + sb.nmax = 1; + + ntr = nxt_sendbuf_mem_coalesce(&sb); + + /* + * Disposal of surplus kernel operations + * if there are no headers and trailers. + */ + + if (nhd == 0) { + hdtrl = NULL; + iov[0].iov_base = NULL; + iov[0].iov_len = 0; + + } else { + hdtrl = iov; + } + + if (ntr == 0) { + iov[1].iov_base = NULL; + iov[1].iov_len = 0; + + } else { + hdtrl = iov; + } + + nxt_log_debug(c->socket.log, "sendfile(%d, %FD, @%O, %uz) hd:%ui tr:%ui", + c->socket.fd, fb->file->fd, fb->file_pos, file_size, + nhd, ntr); + + n = nxt_sys_sendfile(c->socket.fd, fb->file->fd, fb->file_pos, + file_size, hdtrl, 0); + + err = (n == -1) ? nxt_errno : 0; + + nxt_log_debug(c->socket.log, "sendfile(): %uz", 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_error(nxt_socket_error_level(err, c->socket.log_error), + c->socket.log, "sendfile(%d, %FD, @%O, %uz) failed " + "%E \"%FN\" hd:%ui tr:%ui", c->socket.fd, + fb->file->fd, fb->file_pos, file_size, + err, &fb->file->name, nhd, ntr); + + return NXT_ERROR; + } + + nxt_log_debug(c->socket.log, "sendfile() %E", err); + + return 0; + } + + if (n < (ssize_t) sb.size) { + c->socket.write_ready = 0; + } + + return n; +} |