Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 1 | |
| 2 | #include <ngx_config.h> |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 3 | #include <ngx_core.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 4 | #include <ngx_event.h> |
Igor Sysoev | 7578ec9 | 2003-06-02 15:24:30 +0000 | [diff] [blame] | 5 | #include <nginx.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 6 | |
| 7 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 8 | typedef struct { |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 9 | int flag; |
| 10 | u_char *name; |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 11 | } ngx_accept_log_ctx_t; |
| 12 | |
| 13 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 14 | static size_t ngx_accept_log_error(void *data, char *buf, size_t len); |
| 15 | |
| 16 | |
Igor Sysoev | 205dc14 | 2004-04-01 06:21:13 +0000 | [diff] [blame^] | 17 | ngx_atomic_t *ngx_accept_mutex_ptr; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 18 | ngx_atomic_t *ngx_accept_mutex; |
Igor Sysoev | 205dc14 | 2004-04-01 06:21:13 +0000 | [diff] [blame^] | 19 | ngx_uint_t ngx_accept_mutex_held; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 20 | |
| 21 | |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 22 | void ngx_event_accept(ngx_event_t *ev) |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 23 | { |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 24 | ngx_uint_t instance, rinstance, winstance, accepted; |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 25 | socklen_t len; |
| 26 | struct sockaddr *sa; |
| 27 | ngx_err_t err; |
| 28 | ngx_log_t *log; |
| 29 | ngx_pool_t *pool; |
| 30 | ngx_socket_t s; |
| 31 | ngx_event_t *rev, *wev; |
| 32 | ngx_connection_t *c, *ls; |
| 33 | ngx_event_conf_t *ecf; |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 34 | ngx_accept_log_ctx_t *ctx; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 35 | |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 36 | ecf = ngx_event_get_conf(ngx_cycle->conf_ctx, ngx_event_core_module); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 37 | |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 38 | if (ngx_event_flags & (NGX_USE_EDGE_EVENT|NGX_USE_SIGIO_EVENT)) { |
| 39 | ev->available = 1; |
| 40 | |
| 41 | } else if (!(ngx_event_flags & NGX_HAVE_KQUEUE_EVENT)) { |
| 42 | ev->available = ecf->multi_accept; |
| 43 | } |
| 44 | |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 45 | ls = ev->data; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 46 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 47 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
| 48 | "accept on %s, ready: %d", |
| 49 | ls->listening->addr_text.data, ev->available); |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 50 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 51 | ev->ready = 0; |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 52 | accepted = 0; |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 53 | pool = NULL; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 54 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 55 | do { |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 56 | |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 57 | if (pool == NULL) { |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 58 | |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 59 | /* |
| 60 | * Create the pool before accept() to avoid copy the sockaddr. |
| 61 | * Although accept() can fail it's an uncommon case |
| 62 | * and besides the pool can be got from the free pool list |
| 63 | */ |
| 64 | |
| 65 | if (!(pool = ngx_create_pool(ls->listening->pool_size, ev->log))) { |
| 66 | return; |
| 67 | } |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 68 | } |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 69 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 70 | if (!(sa = ngx_palloc(pool, ls->listening->socklen))) { |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 71 | ngx_destroy_pool(pool); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 72 | return; |
| 73 | } |
| 74 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 75 | if (!(log = ngx_palloc(pool, sizeof(ngx_log_t)))) { |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 76 | ngx_destroy_pool(pool); |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 77 | return; |
| 78 | } |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 79 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 80 | ngx_memcpy(log, ls->log, sizeof(ngx_log_t)); |
| 81 | pool->log = log; |
| 82 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 83 | if (!(ctx = ngx_palloc(pool, sizeof(ngx_accept_log_ctx_t)))) { |
| 84 | ngx_destroy_pool(pool); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | /* -1 disable logging the connection number */ |
| 89 | ctx->flag = -1; |
| 90 | ctx->name = ls->listening->addr_text.data; |
| 91 | |
| 92 | log->data = ctx; |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 93 | log->handler = ngx_accept_log_error; |
| 94 | |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 95 | len = ls->listening->socklen; |
| 96 | |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 97 | s = accept(ls->fd, sa, &len); |
| 98 | if (s == -1) { |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 99 | err = ngx_socket_errno; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 100 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 101 | if (err == NGX_EAGAIN) { |
Igor Sysoev | 9139cd2 | 2004-02-17 17:53:12 +0000 | [diff] [blame] | 102 | if (!(ngx_event_flags |
| 103 | & (NGX_USE_EDGE_EVENT|NGX_USE_SIGIO_EVENT))) |
| 104 | { |
| 105 | ngx_log_error(NGX_LOG_NOTICE, log, err, |
| 106 | "EAGAIN after %d accepted connection(s)", |
| 107 | accepted); |
| 108 | } |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 109 | |
| 110 | ngx_destroy_pool(pool); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 111 | return; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 112 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 113 | |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 114 | ngx_log_error(NGX_LOG_ALERT, ev->log, err, |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 115 | "accept() on %s failed", |
| 116 | ls->listening->addr_text.data); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 117 | |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 118 | if (err == NGX_ECONNABORTED) { |
| 119 | if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { |
| 120 | ev->available--; |
| 121 | } |
| 122 | |
| 123 | if (ev->available) { |
| 124 | /* reuse the previously allocated pool */ |
| 125 | continue; |
| 126 | } |
| 127 | } |
| 128 | |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 129 | ngx_destroy_pool(pool); |
| 130 | return; |
| 131 | } |
| 132 | |
Igor Sysoev | 72f2e36 | 2003-07-22 19:53:10 +0000 | [diff] [blame] | 133 | /* disable warning: Win32 SOCKET is u_int while UNIX socket is int */ |
Igor Sysoev | 890fc96 | 2003-07-20 21:15:59 +0000 | [diff] [blame] | 134 | |
Igor Sysoev | aa3436c | 2003-05-30 14:27:59 +0000 | [diff] [blame] | 135 | if ((unsigned) s >= (unsigned) ecf->connections) { |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 136 | |
| 137 | ngx_log_error(NGX_LOG_ALERT, ev->log, 0, |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 138 | "accept() on %s returned socket #%d while " |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 139 | "only %d connections was configured, " |
Igor Sysoev | bb6ec8c | 2003-11-20 07:05:50 +0000 | [diff] [blame] | 140 | "closing the connection", |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 141 | ls->listening->addr_text.data, s, ecf->connections); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 142 | |
| 143 | if (ngx_close_socket(s) == -1) { |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 144 | ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, |
| 145 | ngx_close_socket_n "failed"); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Igor Sysoev | bb6ec8c | 2003-11-20 07:05:50 +0000 | [diff] [blame] | 148 | /* TODO: disable temporary accept() event */ |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 149 | |
| 150 | ngx_destroy_pool(pool); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 151 | return; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 152 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 153 | |
Igor Sysoev | 7b6062a | 2004-02-12 20:57:10 +0000 | [diff] [blame] | 154 | /* set a blocking mode for aio and non-blocking mode for the others */ |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 155 | |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 156 | if (ngx_inherited_nonblocking) { |
| 157 | if ((ngx_event_flags & NGX_USE_AIO_EVENT)) { |
| 158 | if (ngx_blocking(s) == -1) { |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 159 | ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, |
| 160 | ngx_blocking_n " failed"); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 161 | |
| 162 | if (ngx_close_socket(s) == -1) { |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 163 | ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, |
| 164 | ngx_close_socket_n " failed"); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | ngx_destroy_pool(pool); |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 168 | return; |
| 169 | } |
| 170 | } |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 171 | |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 172 | } else { |
Igor Sysoev | 7af6b16 | 2004-02-09 07:46:43 +0000 | [diff] [blame] | 173 | if (!(ngx_event_flags & NGX_USE_AIO_EVENT)) { |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 174 | if (ngx_nonblocking(s) == -1) { |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 175 | ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, |
| 176 | ngx_nonblocking_n " failed"); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 177 | |
| 178 | if (ngx_close_socket(s) == -1) { |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 179 | ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno, |
| 180 | ngx_close_socket_n " failed"); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | ngx_destroy_pool(pool); |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 184 | return; |
| 185 | } |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 188 | |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 189 | #if (WIN32) |
| 190 | /* |
| 191 | * Winsock assignes a socket number divisible by 4 |
| 192 | * so to find a connection we divide a socket number by 4. |
| 193 | */ |
| 194 | |
| 195 | if (s % 4) { |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 196 | ngx_log_error(NGX_LOG_EMERG, ev->log, 0, |
| 197 | "accept() on %s returned socket #%d, " |
| 198 | "not divisible by 4", |
| 199 | ls->listening->addr_text.data, s); |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 200 | exit(1); |
| 201 | } |
| 202 | |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 203 | c = &ngx_cycle->connections[s / 4]; |
| 204 | rev = &ngx_cycle->read_events[s / 4]; |
| 205 | wev = &ngx_cycle->write_events[s / 4]; |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 206 | #else |
Igor Sysoev | 9d63952 | 2003-07-07 06:11:50 +0000 | [diff] [blame] | 207 | c = &ngx_cycle->connections[s]; |
| 208 | rev = &ngx_cycle->read_events[s]; |
| 209 | wev = &ngx_cycle->write_events[s]; |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 210 | #endif |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 211 | |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 212 | instance = rev->instance; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 213 | rinstance = rev->returned_instance; |
| 214 | winstance = wev->returned_instance; |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 215 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 216 | ngx_memzero(rev, sizeof(ngx_event_t)); |
| 217 | ngx_memzero(wev, sizeof(ngx_event_t)); |
| 218 | ngx_memzero(c, sizeof(ngx_connection_t)); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 219 | |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 220 | c->pool = pool; |
| 221 | |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 222 | c->listening = ls->listening; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 223 | c->sockaddr = sa; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 224 | c->socklen = len; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 225 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 226 | rev->instance = (u_char) !instance; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 227 | rev->returned_instance = (u_char) rinstance; |
| 228 | |
Igor Sysoev | 10a543a | 2004-03-16 07:10:12 +0000 | [diff] [blame] | 229 | wev->instance = (u_char) !instance; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 230 | wev->returned_instance = (u_char) winstance; |
Igor Sysoev | 6b863e3 | 2003-05-12 15:52:24 +0000 | [diff] [blame] | 231 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 232 | rev->index = NGX_INVALID_INDEX; |
| 233 | wev->index = NGX_INVALID_INDEX; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 234 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 235 | rev->data = c; |
| 236 | wev->data = c; |
| 237 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 238 | c->read = rev; |
| 239 | c->write = wev; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 240 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 241 | c->fd = s; |
| 242 | c->unexpected_eof = 1; |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 243 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 244 | wev->write = 1; |
Igor Sysoev | b5faed2 | 2003-10-29 08:30:44 +0000 | [diff] [blame] | 245 | wev->ready = 1; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 246 | |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 247 | if (ngx_event_flags |
| 248 | & (NGX_USE_AIO_EVENT|NGX_USE_EDGE_EVENT|NGX_USE_SIGIO_EVENT)) |
| 249 | { |
| 250 | /* aio, iocp, sigio, epoll */ |
Igor Sysoev | b5faed2 | 2003-10-29 08:30:44 +0000 | [diff] [blame] | 251 | rev->ready = 1; |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 252 | } |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 253 | |
Igor Sysoev | 10318a2 | 2004-01-29 21:45:01 +0000 | [diff] [blame] | 254 | if (ev->deferred_accept) { |
| 255 | rev->ready = 1; |
| 256 | } |
| 257 | |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 258 | c->ctx = ls->ctx; |
| 259 | c->servers = ls->servers; |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 260 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 261 | c->log = log; |
| 262 | rev->log = log; |
| 263 | wev->log = log; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 264 | |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 265 | /* |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 266 | * TODO: MT: - atomic increment (x86: lock xadd) |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 267 | * or protection by critical section or light mutex |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 268 | * |
| 269 | * TODO: MP: - allocated in a shared memory |
| 270 | * - atomic increment (x86: lock xadd) |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 271 | * or protection by critical section or light mutex |
Igor Sysoev | 5f80078 | 2003-12-08 20:48:12 +0000 | [diff] [blame] | 272 | */ |
Igor Sysoev | e89c458 | 2003-12-19 08:15:11 +0000 | [diff] [blame] | 273 | |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 274 | c->number = ngx_atomic_inc(&ngx_connection_counter); |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 275 | |
Igor Sysoev | d8e1f07 | 2004-01-22 06:47:28 +0000 | [diff] [blame] | 276 | ngx_log_debug2(NGX_LOG_DEBUG_EVENT, ev->log, 0, |
Igor Sysoev | b54698b | 2004-02-23 20:57:12 +0000 | [diff] [blame] | 277 | "accept: fd:%d c:%d", s, c->number); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 278 | |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 279 | if (ngx_add_conn) { |
| 280 | if (ngx_add_conn(c) == NGX_ERROR) { |
| 281 | if (ngx_close_socket(s) == -1) { |
| 282 | ngx_log_error(NGX_LOG_ALERT, ev->log, ngx_socket_errno, |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 283 | ngx_close_socket_n " failed"); |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 284 | } |
Igor Sysoev | 6a644c6 | 2003-03-04 06:33:48 +0000 | [diff] [blame] | 285 | |
Igor Sysoev | 1393325 | 2003-05-29 13:02:09 +0000 | [diff] [blame] | 286 | ngx_destroy_pool(pool); |
Igor Sysoev | d581fd5 | 2003-05-13 16:02:32 +0000 | [diff] [blame] | 287 | return; |
Igor Sysoev | 6a644c6 | 2003-03-04 06:33:48 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Igor Sysoev | a4b16df | 2004-02-02 21:19:52 +0000 | [diff] [blame] | 291 | pool = NULL; |
| 292 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 293 | log->data = NULL; |
| 294 | log->handler = NULL; |
| 295 | |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 296 | ls->listening->handler(c); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 297 | |
Igor Sysoev | a983011 | 2003-05-19 16:39:14 +0000 | [diff] [blame] | 298 | if (ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) { |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 299 | ev->available--; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 300 | } |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 301 | |
| 302 | accepted++; |
Igor Sysoev | 86de4cb | 2003-01-30 07:28:09 +0000 | [diff] [blame] | 303 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 304 | } while (ev->available); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 305 | } |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 306 | |
| 307 | |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 308 | ngx_int_t ngx_trylock_accept_mutex(ngx_cycle_t *cycle) |
| 309 | { |
| 310 | if (*ngx_accept_mutex == 0 && ngx_atomic_cmp_set(ngx_accept_mutex, 0, 1)) { |
| 311 | |
| 312 | ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, |
| 313 | "accept mutex locked"); |
| 314 | |
Igor Sysoev | 205dc14 | 2004-04-01 06:21:13 +0000 | [diff] [blame^] | 315 | if (!ngx_accept_mutex_held) { |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 316 | if (ngx_enable_accept_events(cycle) == NGX_ERROR) { |
Igor Sysoev | 205dc14 | 2004-04-01 06:21:13 +0000 | [diff] [blame^] | 317 | *ngx_accept_mutex = 0; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 318 | return NGX_ERROR; |
| 319 | } |
| 320 | |
Igor Sysoev | 205dc14 | 2004-04-01 06:21:13 +0000 | [diff] [blame^] | 321 | ngx_accept_mutex_held = 1; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | return NGX_OK; |
| 325 | } |
| 326 | |
Igor Sysoev | 205dc14 | 2004-04-01 06:21:13 +0000 | [diff] [blame^] | 327 | if (ngx_accept_mutex_held) { |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 328 | if (ngx_disable_accept_events(cycle) == NGX_ERROR) { |
| 329 | return NGX_ERROR; |
| 330 | } |
| 331 | |
Igor Sysoev | 205dc14 | 2004-04-01 06:21:13 +0000 | [diff] [blame^] | 332 | ngx_accept_mutex_held = 0; |
Igor Sysoev | 709405b | 2004-03-31 15:26:46 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | return NGX_OK; |
| 336 | } |
| 337 | |
| 338 | |
| 339 | ngx_int_t ngx_enable_accept_events(ngx_cycle_t *cycle) |
| 340 | { |
| 341 | ngx_uint_t i; |
| 342 | ngx_listening_t *s; |
| 343 | |
| 344 | s = cycle->listening.elts; |
| 345 | for (i = 0; i < cycle->listening.nelts; i++) { |
| 346 | |
| 347 | /* |
| 348 | * we do not need to handle the Winsock sockets here (divde a socket |
| 349 | * number by 4) because this function would never called |
| 350 | * in the Winsock environment |
| 351 | */ |
| 352 | |
| 353 | if (ngx_event_flags & NGX_USE_SIGIO_EVENT) { |
| 354 | if (ngx_add_conn(&cycle->connections[s[i].fd]) == NGX_ERROR) { |
| 355 | return NGX_ERROR; |
| 356 | } |
| 357 | |
| 358 | } else { |
| 359 | if (ngx_add_event(&cycle->read_events[s[i].fd], NGX_READ_EVENT, 0) |
| 360 | == NGX_ERROR) |
| 361 | { |
| 362 | return NGX_ERROR; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return NGX_OK; |
| 368 | } |
| 369 | |
| 370 | |
| 371 | ngx_int_t ngx_disable_accept_events(ngx_cycle_t *cycle) |
| 372 | { |
| 373 | ngx_uint_t i; |
| 374 | ngx_listening_t *s; |
| 375 | |
| 376 | s = cycle->listening.elts; |
| 377 | for (i = 0; i < cycle->listening.nelts; i++) { |
| 378 | |
| 379 | /* |
| 380 | * we do not need to handle the Winsock sockets here (divde a socket |
| 381 | * number by 4) because this function would never called |
| 382 | * in the Winsock environment |
| 383 | */ |
| 384 | |
| 385 | if (ngx_event_flags & NGX_USE_SIGIO_EVENT) { |
| 386 | if (ngx_del_conn(&cycle->connections[s[i].fd], NGX_DISABLE_EVENT) |
| 387 | == NGX_ERROR) |
| 388 | { |
| 389 | return NGX_ERROR; |
| 390 | } |
| 391 | |
| 392 | } else { |
| 393 | if (ngx_del_event(&cycle->read_events[s[i].fd], NGX_READ_EVENT, |
| 394 | NGX_DISABLE_EVENT) == NGX_ERROR) |
| 395 | { |
| 396 | return NGX_ERROR; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return NGX_OK; |
| 402 | } |
| 403 | |
| 404 | |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 405 | static size_t ngx_accept_log_error(void *data, char *buf, size_t len) |
| 406 | { |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 407 | ngx_accept_log_ctx_t *ctx = data; |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 408 | |
Igor Sysoev | 54498db | 2004-02-11 17:08:49 +0000 | [diff] [blame] | 409 | return ngx_snprintf(buf, len, " while accept() on %s", ctx->name); |
Igor Sysoev | 160d774 | 2003-11-19 16:26:41 +0000 | [diff] [blame] | 410 | } |