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 ssize_t
0014 ngx_unix_recv(ngx_connection_t *c, u_char *buf, size_t size)
0015 {
0016     ssize_t       n;
0017     ngx_err_t     err;
0018     ngx_event_t  *rev;
0019 
0020     rev = c->read;
0021 
0022 #if (NGX_HAVE_KQUEUE)
0023 
0024     if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0025         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
0026                        "recv: eof:%d, avail:%d, err:%d",
0027                        rev->pending_eof, rev->available, rev->kq_errno);
0028 
0029         if (rev->available == 0) {
0030             if (rev->pending_eof) {
0031                 rev->ready = 0;
0032                 rev->eof = 1;
0033 
0034                 if (rev->kq_errno) {
0035                     rev->error = 1;
0036                     ngx_set_socket_errno(rev->kq_errno);
0037 
0038                     return ngx_connection_error(c, rev->kq_errno,
0039                                "kevent() reported about an closed connection");
0040                 }
0041 
0042                 return 0;
0043 
0044             } else {
0045                 rev->ready = 0;
0046                 return NGX_AGAIN;
0047             }
0048         }
0049     }
0050 
0051 #endif
0052 
0053 #if (NGX_HAVE_EPOLLRDHUP)
0054 
0055     if (ngx_event_flags & NGX_USE_EPOLL_EVENT) {
0056         ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
0057                        "recv: eof:%d, avail:%d",
0058                        rev->pending_eof, rev->available);
0059 
0060         if (!rev->available && !rev->pending_eof) {
0061             rev->ready = 0;
0062             return NGX_AGAIN;
0063         }
0064     }
0065 
0066 #endif
0067 
0068     do {
0069         n = recv(c->fd, buf, size, 0);
0070 
0071         ngx_log_debug3(NGX_LOG_DEBUG_EVENT, c->log, 0,
0072                        "recv: fd:%d %z of %uz", c->fd, n, size);
0073 
0074         if (n == 0) {
0075             rev->ready = 0;
0076             rev->eof = 1;
0077 
0078 #if (NGX_HAVE_KQUEUE)
0079 
0080             /*
0081              * on FreeBSD recv() may return 0 on closed socket
0082              * even if kqueue reported about available data
0083              */
0084 
0085             if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0086                 rev->available = 0;
0087             }
0088 
0089 #endif
0090 
0091             return 0;
0092         }
0093 
0094         if (n > 0) {
0095 
0096 #if (NGX_HAVE_KQUEUE)
0097 
0098             if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
0099                 rev->available -= n;
0100 
0101                 /*
0102                  * rev->available may be negative here because some additional
0103                  * bytes may be received between kevent() and recv()
0104                  */
0105 
0106                 if (rev->available <= 0) {
0107                     if (!rev->pending_eof) {
0108                         rev->ready = 0;
0109                     }
0110 
0111                     rev->available = 0;
0112                 }
0113 
0114                 return n;
0115             }
0116 
0117 #endif
0118 
0119 #if (NGX_HAVE_EPOLLRDHUP)
0120 
0121             if ((ngx_event_flags & NGX_USE_EPOLL_EVENT)
0122                 && ngx_use_epoll_rdhup)
0123             {
0124                 if ((size_t) n < size) {
0125                     if (!rev->pending_eof) {
0126                         rev->ready = 0;
0127                     }
0128 
0129                     rev->available = 0;
0130                 }
0131 
0132                 return n;
0133             }
0134 
0135 #endif
0136 
0137             if ((size_t) n < size
0138                 && !(ngx_event_flags & NGX_USE_GREEDY_EVENT))
0139             {
0140                 rev->ready = 0;
0141             }
0142 
0143             return n;
0144         }
0145 
0146         err = ngx_socket_errno;
0147 
0148         if (err == NGX_EAGAIN || err == NGX_EINTR) {
0149             ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
0150                            "recv() not ready");
0151             n = NGX_AGAIN;
0152 
0153         } else {
0154             n = ngx_connection_error(c, err, "recv() failed");
0155             break;
0156         }
0157 
0158     } while (err == NGX_EINTR);
0159 
0160     rev->ready = 0;
0161 
0162     if (n == NGX_ERROR) {
0163         rev->error = 1;
0164     }
0165 
0166     return n;
0167 }