2003-11-27 15:45:22 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-03-01 05:03:02 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-11-27 15:45:22 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
#if (NGX_TEST_BUILD_SOLARIS_SENDFILEV)
|
|
|
|
|
|
|
|
/* Solaris declarations */
|
|
|
|
|
|
|
|
typedef struct sendfilevec {
|
|
|
|
int sfv_fd;
|
|
|
|
u_int sfv_flag;
|
|
|
|
off_t sfv_off;
|
|
|
|
size_t sfv_len;
|
|
|
|
} sendfilevec_t;
|
|
|
|
|
|
|
|
#define SFV_FD_SELF -2
|
|
|
|
|
|
|
|
static ssize_t sendfilev(int fd, const struct sendfilevec *vec,
|
2005-12-06 00:59:05 +08:00
|
|
|
int sfvcnt, size_t *xferred)
|
2004-12-03 02:40:46 +08:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2006-01-11 23:26:57 +08:00
|
|
|
#if (IOV_MAX > 64)
|
|
|
|
#define NGX_SENDFILEVECS 64
|
|
|
|
#else
|
|
|
|
#define NGX_SENDFILEVECS IOV_MAX
|
|
|
|
#endif
|
|
|
|
|
2004-09-17 00:10:13 +08:00
|
|
|
|
|
|
|
|
2005-12-06 00:59:05 +08:00
|
|
|
ngx_chain_t *
|
|
|
|
ngx_solaris_sendfilev_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
2003-11-27 15:45:22 +08:00
|
|
|
{
|
|
|
|
int fd;
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *prev;
|
2004-12-03 02:40:46 +08:00
|
|
|
off_t size, send, prev_send, aligned, fprev;
|
|
|
|
size_t sent;
|
|
|
|
ssize_t n;
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_int_t eintr, complete;
|
2003-11-28 03:01:37 +08:00
|
|
|
ngx_err_t err;
|
2004-09-17 00:10:13 +08:00
|
|
|
sendfilevec_t *sfv, sfvs[NGX_SENDFILEVECS];
|
2003-11-27 15:45:22 +08:00
|
|
|
ngx_array_t vec;
|
|
|
|
ngx_event_t *wev;
|
2005-03-19 20:38:37 +08:00
|
|
|
ngx_chain_t *cl;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
wev = c->write;
|
|
|
|
|
|
|
|
if (!wev->ready) {
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2004-10-11 23:07:03 +08:00
|
|
|
if (!c->sendfile) {
|
|
|
|
return ngx_writev_chain(c, in, limit);
|
|
|
|
}
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
|
|
|
|
/* the maximum limit size is the maximum size_t value - the page size */
|
|
|
|
|
2007-07-30 02:24:53 +08:00
|
|
|
if (limit == 0 || limit > (off_t) (NGX_MAX_SIZE_T_VALUE - ngx_pagesize)) {
|
2004-12-21 20:30:30 +08:00
|
|
|
limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize;
|
2004-12-03 02:40:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
send = 0;
|
|
|
|
complete = 0;
|
|
|
|
|
2004-09-17 00:10:13 +08:00
|
|
|
vec.elts = sfvs;
|
|
|
|
vec.size = sizeof(sendfilevec_t);
|
|
|
|
vec.nalloc = NGX_SENDFILEVECS;
|
|
|
|
vec.pool = c->pool;
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for ( ;; ) {
|
2003-11-27 15:45:22 +08:00
|
|
|
fd = SFV_FD_SELF;
|
|
|
|
prev = NULL;
|
2003-11-28 03:01:37 +08:00
|
|
|
fprev = 0;
|
2003-11-27 15:45:22 +08:00
|
|
|
sfv = NULL;
|
|
|
|
eintr = 0;
|
|
|
|
sent = 0;
|
2004-12-03 02:40:46 +08:00
|
|
|
prev_send = send;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-09-17 00:10:13 +08:00
|
|
|
vec.nelts = 0;
|
2003-11-28 03:01:37 +08:00
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
/* create the sendfilevec and coalesce the neighbouring bufs */
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for (cl = in; cl && vec.nelts < IOV_MAX && send < limit; cl = cl->next)
|
|
|
|
{
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2003-11-27 15:45:22 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory_only(cl->buf)) {
|
2003-11-27 15:45:22 +08:00
|
|
|
fd = SFV_FD_SELF;
|
|
|
|
|
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-12-03 02:40:46 +08:00
|
|
|
sfv->sfv_len += (size_t) size;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
} else {
|
2005-03-19 20:38:37 +08:00
|
|
|
sfv = ngx_array_push(&vec);
|
|
|
|
if (sfv == NULL) {
|
2004-09-17 00:10:13 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2003-11-27 15:45:22 +08:00
|
|
|
sfv->sfv_fd = SFV_FD_SELF;
|
|
|
|
sfv->sfv_flag = 0;
|
2004-06-01 14:04:46 +08:00
|
|
|
sfv->sfv_off = (off_t) (uintptr_t) cl->buf->pos;
|
2004-12-03 02:40:46 +08:00
|
|
|
sfv->sfv_len = (size_t) size;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
prev = cl->buf->pos + (size_t) size;
|
2004-06-21 23:59:32 +08:00
|
|
|
send += size;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
} else {
|
|
|
|
prev = NULL;
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
size = cl->buf->file_last - cl->buf->file_pos;
|
2004-06-21 23:59:32 +08:00
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
|
|
|
|
aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
|
2007-04-24 05:32:13 +08:00
|
|
|
& ~((off_t) ngx_pagesize - 1);
|
2004-06-21 23:59:32 +08:00
|
|
|
|
|
|
|
if (aligned <= cl->buf->file_last) {
|
|
|
|
size = aligned - cl->buf->file_pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (fd == cl->buf->file->fd && fprev == cl->buf->file_pos) {
|
2004-12-03 02:40:46 +08:00
|
|
|
sfv->sfv_len += (size_t) size;
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
} else {
|
2005-03-19 20:38:37 +08:00
|
|
|
sfv = ngx_array_push(&vec);
|
|
|
|
if (sfv == NULL) {
|
2004-09-17 00:10:13 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
fd = cl->buf->file->fd;
|
2003-11-27 15:45:22 +08:00
|
|
|
sfv->sfv_fd = fd;
|
|
|
|
sfv->sfv_flag = 0;
|
2004-06-01 14:04:46 +08:00
|
|
|
sfv->sfv_off = cl->buf->file_pos;
|
2004-12-03 02:40:46 +08:00
|
|
|
sfv->sfv_len = (size_t) size;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
fprev = cl->buf->file_pos + size;
|
|
|
|
send += size;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-28 03:01:37 +08:00
|
|
|
n = sendfilev(c->fd, vec.elts, vec.nelts, &sent);
|
2003-11-27 15:45:22 +08:00
|
|
|
|
|
|
|
if (n == -1) {
|
|
|
|
err = ngx_errno;
|
|
|
|
|
2009-08-30 17:42:29 +08:00
|
|
|
switch (err) {
|
|
|
|
case NGX_EAGAIN:
|
|
|
|
break;
|
2004-02-12 01:08:49 +08:00
|
|
|
|
2009-08-30 17:42:29 +08:00
|
|
|
case NGX_EINTR:
|
|
|
|
eintr = 1;
|
|
|
|
break;
|
2004-02-12 01:08:49 +08:00
|
|
|
|
2009-08-30 17:42:29 +08:00
|
|
|
default:
|
2003-11-27 15:45:22 +08:00
|
|
|
wev->error = 1;
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_connection_error(c, err, "sendfilev() failed");
|
2003-11-27 15:45:22 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
2009-08-30 17:42:29 +08:00
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
|
|
"sendfilev() sent only %uz bytes", sent);
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-02-12 01:08:49 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
2004-11-11 22:07:14 +08:00
|
|
|
"sendfilev: %z %z", n, sent);
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (send - prev_send == (off_t) sent) {
|
2004-06-21 23:59:32 +08:00
|
|
|
complete = 1;
|
|
|
|
}
|
|
|
|
|
2003-11-27 15:45:22 +08:00
|
|
|
c->sent += sent;
|
|
|
|
|
|
|
|
for (cl = in; cl; cl = cl->next) {
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2005-11-15 21:30:52 +08:00
|
|
|
continue;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sent == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
size = ngx_buf_size(cl->buf);
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if ((off_t) sent >= size) {
|
|
|
|
sent = (size_t) ((off_t) sent - size);
|
2003-11-27 15:45:22 +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-27 15:45:22 +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-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory(cl->buf)) {
|
|
|
|
cl->buf->pos += sent;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (cl->buf->in_file) {
|
|
|
|
cl->buf->file_pos += sent;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (eintr) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (!complete) {
|
|
|
|
wev->ready = 0;
|
|
|
|
return cl;
|
|
|
|
}
|
2004-02-18 23:45:21 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (send >= limit || cl == NULL) {
|
|
|
|
return cl;
|
|
|
|
}
|
2003-11-27 15:45:22 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
in = cl;
|
2003-11-27 15:45:22 +08:00
|
|
|
}
|
|
|
|
}
|