Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_event.h>
0011 
0012 
0013 ngx_chain_t *
0014 ngx_writev_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
0015 {
0016     ssize_t        n, sent;
0017     off_t          send, prev_send;
0018     ngx_chain_t   *cl;
0019     ngx_event_t   *wev;
0020     ngx_iovec_t    vec;
0021     struct iovec   iovs[NGX_IOVS_PREALLOCATE];
0022 
0023     wev = c->write;
0024 
0025     if (!wev->ready) {
0026         return in;
0027     }
0028 
0029 #if (NGX_HAVE_KQUEUE)
0030 
0031     if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
0032         (void) ngx_connection_error(c, wev->kq_errno,
0033                                "kevent() reported about an closed connection");
0034         wev->error = 1;
0035         return NGX_CHAIN_ERROR;
0036     }
0037 
0038 #endif
0039 
0040     /* the maximum limit size is the maximum size_t value - the page size */
0041 
0042     if (limit == 0 || limit > (off_t) (NGX_MAX_SIZE_T_VALUE - ngx_pagesize)) {
0043         limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize;
0044     }
0045 
0046     send = 0;
0047 
0048     vec.iovs = iovs;
0049     vec.nalloc = NGX_IOVS_PREALLOCATE;
0050 
0051     for ( ;; ) {
0052         prev_send = send;
0053 
0054         /* create the iovec and coalesce the neighbouring bufs */
0055 
0056         cl = ngx_output_chain_to_iovec(&vec, in, limit - send, c->log);
0057 
0058         if (cl == NGX_CHAIN_ERROR) {
0059             return NGX_CHAIN_ERROR;
0060         }
0061 
0062         if (cl && cl->buf->in_file) {
0063             ngx_log_error(NGX_LOG_ALERT, c->log, 0,
0064                           "file buf in writev "
0065                           "t:%d r:%d f:%d %p %p-%p %p %O-%O",
0066                           cl->buf->temporary,
0067                           cl->buf->recycled,
0068                           cl->buf->in_file,
0069                           cl->buf->start,
0070                           cl->buf->pos,
0071                           cl->buf->last,
0072                           cl->buf->file,
0073                           cl->buf->file_pos,
0074                           cl->buf->file_last);
0075 
0076             ngx_debug_point();
0077 
0078             return NGX_CHAIN_ERROR;
0079         }
0080 
0081         send += vec.size;
0082 
0083         n = ngx_writev(c, &vec);
0084 
0085         if (n == NGX_ERROR) {
0086             return NGX_CHAIN_ERROR;
0087         }
0088 
0089         sent = (n == NGX_AGAIN) ? 0 : n;
0090 
0091         c->sent += sent;
0092 
0093         in = ngx_chain_update_sent(in, sent);
0094 
0095         if (send - prev_send != sent) {
0096             wev->ready = 0;
0097             return in;
0098         }
0099 
0100         if (send >= limit || in == NULL) {
0101             return in;
0102         }
0103     }
0104 }
0105 
0106 
0107 ngx_chain_t *
0108 ngx_output_chain_to_iovec(ngx_iovec_t *vec, ngx_chain_t *in, size_t limit,
0109     ngx_log_t *log)
0110 {
0111     size_t         total, size;
0112     u_char        *prev;
0113     ngx_uint_t     n;
0114     struct iovec  *iov;
0115 
0116     iov = NULL;
0117     prev = NULL;
0118     total = 0;
0119     n = 0;
0120 
0121     for ( /* void */ ; in && total < limit; in = in->next) {
0122 
0123         if (ngx_buf_special(in->buf)) {
0124             continue;
0125         }
0126 
0127         if (in->buf->in_file) {
0128             break;
0129         }
0130 
0131         if (!ngx_buf_in_memory(in->buf)) {
0132             ngx_log_error(NGX_LOG_ALERT, log, 0,
0133                           "bad buf in output chain "
0134                           "t:%d r:%d f:%d %p %p-%p %p %O-%O",
0135                           in->buf->temporary,
0136                           in->buf->recycled,
0137                           in->buf->in_file,
0138                           in->buf->start,
0139                           in->buf->pos,
0140                           in->buf->last,
0141                           in->buf->file,
0142                           in->buf->file_pos,
0143                           in->buf->file_last);
0144 
0145             ngx_debug_point();
0146 
0147             return NGX_CHAIN_ERROR;
0148         }
0149 
0150         size = in->buf->last - in->buf->pos;
0151 
0152         if (size > limit - total) {
0153             size = limit - total;
0154         }
0155 
0156         if (prev == in->buf->pos) {
0157             iov->iov_len += size;
0158 
0159         } else {
0160             if (n == vec->nalloc) {
0161                 break;
0162             }
0163 
0164             iov = &vec->iovs[n++];
0165 
0166             iov->iov_base = (void *) in->buf->pos;
0167             iov->iov_len = size;
0168         }
0169 
0170         prev = in->buf->pos + size;
0171         total += size;
0172     }
0173 
0174     vec->count = n;
0175     vec->size = total;
0176 
0177     return in;
0178 }
0179 
0180 
0181 ssize_t
0182 ngx_writev(ngx_connection_t *c, ngx_iovec_t *vec)
0183 {
0184     ssize_t    n;
0185     ngx_err_t  err;
0186 
0187 eintr:
0188 
0189     n = writev(c->fd, vec->iovs, vec->count);
0190 
0191     ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
0192                    "writev: %z of %uz", n, vec->size);
0193 
0194     if (n == -1) {
0195         err = ngx_errno;
0196 
0197         switch (err) {
0198         case NGX_EAGAIN:
0199             ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
0200                            "writev() not ready");
0201             return NGX_AGAIN;
0202 
0203         case NGX_EINTR:
0204             ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
0205                            "writev() was interrupted");
0206             goto eintr;
0207 
0208         default:
0209             c->write->error = 1;
0210             ngx_connection_error(c, err, "writev() failed");
0211             return NGX_ERROR;
0212         }
0213     }
0214 
0215     return n;
0216 }