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
|
|
|
|
* offsets only and the including <sys/sendfile.h> breaks building
|
|
|
|
* if off_t is 64 bit wide. So we use own sendfile() definition where
|
|
|
|
* offset paramter is int32_t. It allows to use sendfile() with
|
|
|
|
* the file parts below 2G.
|
|
|
|
*
|
|
|
|
* Linux 2.4.21 has a new sendfile64() syscall #239.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
ngx_chain_t *ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in)
|
|
|
|
{
|
2003-11-26 04:44:56 +08:00
|
|
|
int rc;
|
2003-05-29 21:02:09 +08:00
|
|
|
char *prev;
|
2003-11-28 03:01:37 +08:00
|
|
|
off_t fprev;
|
2003-11-26 04:44:56 +08:00
|
|
|
size_t size, fsize, sent;
|
|
|
|
ngx_int_t use_cork, eintr;
|
2003-05-29 21:02:09 +08:00
|
|
|
struct iovec *iov;
|
|
|
|
ngx_err_t err;
|
|
|
|
ngx_hunk_t *file;
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_array_t header;
|
|
|
|
ngx_event_t *wev;
|
2003-11-26 23:42:18 +08:00
|
|
|
ngx_chain_t *cl, *tail;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
file = NULL;
|
|
|
|
fsize = 0;
|
|
|
|
eintr = 0;
|
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;
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
/* create the iovec and coalesce the neighbouring hunks */
|
|
|
|
|
|
|
|
for (cl = in; cl; cl = cl->next) {
|
|
|
|
if (ngx_hunk_special(cl->hunk)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ngx_hunk_in_memory_only(cl->hunk)) {
|
|
|
|
break;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (prev == cl->hunk->pos) {
|
|
|
|
iov->iov_len += cl->hunk->last - cl->hunk->pos;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_test_null(iov, ngx_push_array(&header), NGX_CHAIN_ERROR);
|
2003-11-26 04:44:56 +08:00
|
|
|
iov->iov_base = cl->hunk->pos;
|
|
|
|
iov->iov_len = cl->hunk->last - cl->hunk->pos;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
prev = cl->hunk->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set TCP_CORK if there is a header before a file */
|
|
|
|
|
|
|
|
if (!c->tcp_nopush
|
|
|
|
&& header.nelts != 0
|
|
|
|
&& cl
|
|
|
|
&& cl->hunk->type & NGX_HUNK_FILE)
|
|
|
|
{
|
|
|
|
c->tcp_nopush = 1;
|
|
|
|
|
|
|
|
ngx_log_debug(c->log, "CORK");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno,
|
|
|
|
ngx_tcp_nopush_n " failed");
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (header.nelts == 0 && cl && cl->hunk->type & NGX_HUNK_FILE) {
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
/* get the file hunk */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
file = cl->hunk;
|
|
|
|
fsize = (size_t) (file->file_last - file->file_pos);
|
2003-11-28 03:01:37 +08:00
|
|
|
fprev = file->file_last;
|
2003-11-26 04:44:56 +08:00
|
|
|
cl = cl->next;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
/* coalesce the neighbouring file hunks */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
while (cl && (cl->hunk->type & NGX_HUNK_FILE)) {
|
|
|
|
if (file->file->fd != cl->hunk->file->fd
|
2003-11-28 03:01:37 +08:00
|
|
|
|| fprev != cl->hunk->file_pos)
|
2003-11-26 04:44:56 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
fsize += (size_t) (cl->hunk->file_last - cl->hunk->file_pos);
|
2003-11-28 03:01:37 +08:00
|
|
|
fprev = cl->hunk->file_last;
|
2003-11-26 04:44:56 +08:00
|
|
|
cl = cl->next;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
/*
|
|
|
|
* the tail is the rest of the chain that exceeded
|
|
|
|
* a single sendfile() capability
|
|
|
|
*/
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
tail = cl;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (fsize) {
|
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;
|
|
|
|
if (err == NGX_EAGAIN) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
|
|
"sendfile() EAGAIN");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, err,
|
|
|
|
"sendfile() EINTR");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, c->log, err,
|
|
|
|
"sendfile() failed");
|
|
|
|
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
|
|
|
|
|
|
|
#if (NGX_DEBUG_WRITE_CHAIN)
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_log_debug(c->log, "sendfile: %d, @" OFF_T_FMT " %d:%d" _
|
|
|
|
rc _ file->file_pos _ sent _ fsize);
|
2003-05-29 21:02:09 +08:00
|
|
|
#endif
|
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;
|
|
|
|
if (err == NGX_EAGAIN) {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EAGAIN");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
ngx_log_error(NGX_LOG_INFO, c->log, err, "writev() EINTR");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, c->log, err, "writev() failed");
|
|
|
|
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
|
|
|
|
|
|
|
#if (NGX_DEBUG_WRITE_CHAIN)
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_log_debug(c->log, "writev: %d" _ sent);
|
2003-05-29 21:02:09 +08:00
|
|
|
#endif
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
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
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (ngx_hunk_special(cl->hunk)) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (sent == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = ngx_hunk_size(cl->hunk);
|
|
|
|
|
|
|
|
if (sent >= size) {
|
|
|
|
sent -= size;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
|
|
|
|
cl->hunk->pos = cl->hunk->last;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cl->hunk->type & NGX_HUNK_FILE) {
|
|
|
|
cl->hunk->file_pos = cl->hunk->file_last;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
|
|
|
|
cl->hunk->pos += sent;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_FILE) {
|
|
|
|
cl->hunk->file_pos += sent;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
in = cl;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
/* "tail == in" means that a single sendfile() is complete */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} while ((tail && tail == in) || eintr);
|
|
|
|
|
|
|
|
if (in) {
|
|
|
|
wev->ready = 0;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
return in;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|