2003-05-29 21:02:09 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2003-11-26 04:44:56 +08:00
|
|
|
#include <ngx_event.h>
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
/*
|
|
|
|
* On Linux up to 2.4.21 sendfile() (syscall #187) works with 32-bit
|
2004-02-09 15:46:43 +08:00
|
|
|
* offsets only and the including <sys/sendfile.h> breaks the compiling
|
|
|
|
* if off_t is 64 bit wide. So we use own sendfile() definition where offset
|
2003-12-01 04:03:18 +08:00
|
|
|
* parameter is int32_t and use sendfile() with the file parts below 2G.
|
2003-11-28 16:40:40 +08:00
|
|
|
*
|
|
|
|
* Linux 2.4.21 has a new sendfile64() syscall #239.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_chain_t *ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in,
|
|
|
|
off_t limit)
|
2003-05-29 21:02:09 +08:00
|
|
|
{
|
2003-11-26 04:44:56 +08:00
|
|
|
int rc;
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *prev;
|
2004-06-21 23:59:32 +08:00
|
|
|
off_t fprev, send, sprev, aligned;
|
|
|
|
size_t fsize;
|
|
|
|
ssize_t size, sent;
|
|
|
|
ngx_uint_t eintr, complete;
|
2003-05-29 21:02:09 +08:00
|
|
|
struct iovec *iov;
|
|
|
|
ngx_err_t err;
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_buf_t *file;
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_array_t header;
|
|
|
|
ngx_event_t *wev;
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_chain_t *cl;
|
2003-11-28 03:01:37 +08:00
|
|
|
#if (HAVE_SENDFILE64)
|
|
|
|
off_t offset;
|
|
|
|
#else
|
|
|
|
int32_t offset;
|
|
|
|
#endif
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
wev = c->write;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (!wev->ready) {
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
send = 0;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
2003-11-26 04:44:56 +08:00
|
|
|
file = NULL;
|
|
|
|
fsize = 0;
|
|
|
|
eintr = 0;
|
2004-06-21 23:59:32 +08:00
|
|
|
complete = 0;
|
|
|
|
sprev = send;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_init_array(header, c->pool, 10, sizeof(struct iovec),
|
|
|
|
NGX_CHAIN_ERROR);
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
iov = NULL;
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
/* create the iovec and coalesce the neighbouring bufs */
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for (cl = in;
|
|
|
|
cl && header.nelts < IOV_MAX && send < limit;
|
|
|
|
cl = cl->next)
|
|
|
|
{
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2003-11-26 04:44:56 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (!ngx_buf_in_memory_only(cl->buf)) {
|
2003-11-26 04:44:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
size = cl->buf->last - cl->buf->pos;
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (prev == cl->buf->pos) {
|
2004-06-21 23:59:32 +08:00
|
|
|
iov->iov_len += size;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_test_null(iov, ngx_push_array(&header), NGX_CHAIN_ERROR);
|
2004-06-01 14:04:46 +08:00
|
|
|
iov->iov_base = (void *) cl->buf->pos;
|
2004-06-21 23:59:32 +08:00
|
|
|
iov->iov_len = size;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
prev = cl->buf->pos + size;
|
|
|
|
send += size;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set TCP_CORK if there is a header before a file */
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (c->tcp_nopush == NGX_TCP_NOPUSH_UNSET
|
2003-11-26 04:44:56 +08:00
|
|
|
&& header.nelts != 0
|
|
|
|
&& cl
|
2004-06-01 14:04:46 +08:00
|
|
|
&& cl->buf->in_file)
|
2003-11-26 04:44:56 +08:00
|
|
|
{
|
2004-02-09 15:46:43 +08:00
|
|
|
if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
|
|
|
|
err = ngx_errno;
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
/*
|
|
|
|
* there is a tiny chance to be interrupted, however
|
|
|
|
* we continue a processing without the TCP_CORK
|
|
|
|
*/
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
if (err != NGX_EINTR) {
|
|
|
|
wev->error = 1;
|
|
|
|
ngx_connection_error(c, err, ngx_tcp_nopush_n " failed");
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
2004-06-01 14:04:46 +08:00
|
|
|
c->tcp_nopush = NGX_TCP_NOPUSH_SET;
|
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"tcp_nopush");
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
/* get the file buf */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (header.nelts == 0 && cl && cl->buf->in_file && send < limit) {
|
2004-06-01 14:04:46 +08:00
|
|
|
file = cl->buf;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
/* coalesce the neighbouring file bufs */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
do {
|
|
|
|
size = (size_t) (cl->buf->file_last - cl->buf->file_pos);
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
|
|
|
|
aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
|
|
|
|
& ~(ngx_pagesize - 1);
|
|
|
|
|
|
|
|
if (aligned <= cl->buf->file_last) {
|
|
|
|
size = aligned - cl->buf->file_pos;
|
|
|
|
}
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
fsize += size;
|
|
|
|
send += size;
|
|
|
|
fprev = cl->buf->file_pos + size;
|
2003-11-26 04:44:56 +08:00
|
|
|
cl = cl->next;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
} while (cl
|
|
|
|
&& cl->buf->in_file
|
|
|
|
&& send < limit
|
|
|
|
&& file->file->fd == cl->buf->file->fd
|
|
|
|
&& fprev == cl->buf->file_pos);
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (file) {
|
2003-11-28 03:01:37 +08:00
|
|
|
#if (HAVE_SENDFILE64)
|
2003-11-26 23:42:18 +08:00
|
|
|
offset = file->file_pos;
|
2003-11-28 03:01:37 +08:00
|
|
|
#else
|
|
|
|
offset = (int32_t) file->file_pos;
|
|
|
|
#endif
|
2003-11-26 23:42:18 +08:00
|
|
|
rc = sendfile(c->fd, file->file->fd, &offset, fsize);
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (rc == -1) {
|
|
|
|
err = ngx_errno;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
|
|
"sendfile() is not ready");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
2004-02-09 15:46:43 +08:00
|
|
|
wev->error = 1;
|
|
|
|
ngx_connection_error(c, err, "sendfile() failed");
|
2003-11-26 04:44:56 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
sent = rc > 0 ? rc : 0;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"sendfile: %d, @" OFF_T_FMT " %d:%d",
|
|
|
|
rc, file->file_pos, sent, fsize);
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
|
|
|
rc = writev(c->fd, header.elts, header.nelts);
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (rc == -1) {
|
|
|
|
err = ngx_errno;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
|
|
"writev() not ready");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
2004-02-09 15:46:43 +08:00
|
|
|
wev->error = 1;
|
|
|
|
ngx_connection_error(c, err, "writev() failed");
|
|
|
|
return NGX_CHAIN_ERROR;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
sent = rc > 0 ? rc : 0;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %d", sent);
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (send - sprev == sent) {
|
|
|
|
complete = 1;
|
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
c->sent += sent;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
for (cl = in; cl; cl = cl->next) {
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2003-11-26 04:44:56 +08:00
|
|
|
continue;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (sent == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
size = ngx_buf_size(cl->buf);
|
2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
if (sent >= size) {
|
|
|
|
sent -= size;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory(cl->buf)) {
|
|
|
|
cl->buf->pos = cl->buf->last;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (cl->buf->in_file) {
|
|
|
|
cl->buf->file_pos = cl->buf->file_last;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory(cl->buf)) {
|
|
|
|
cl->buf->pos += sent;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (cl->buf->in_file) {
|
|
|
|
cl->buf->file_pos += sent;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
break;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (eintr) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (!complete) {
|
|
|
|
wev->ready = 0;
|
|
|
|
return cl;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (send >= limit || cl == NULL) {
|
|
|
|
return cl;
|
|
|
|
}
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
in = cl;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|