Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 1 | |
| 2 | #include <nginx.h> |
| 3 | |
| 4 | #include <ngx_config.h> |
Igor Sysoev | c181784 | 2002-12-27 16:22:50 +0000 | [diff] [blame] | 5 | |
Igor Sysoev | b262063 | 2003-01-10 06:09:20 +0000 | [diff] [blame] | 6 | #include <ngx_core.h> |
Igor Sysoev | 79a8048 | 2003-05-14 17:13:13 +0000 | [diff] [blame^] | 7 | #include <ngx_os_init.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 8 | #include <ngx_string.h> |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 9 | #include <ngx_errno.h> |
| 10 | #include <ngx_time.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 11 | #include <ngx_log.h> |
| 12 | #include <ngx_alloc.h> |
Igor Sysoev | e0af1b8 | 2002-08-16 15:27:03 +0000 | [diff] [blame] | 13 | #include <ngx_array.h> |
| 14 | #include <ngx_socket.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 15 | #include <ngx_server.h> |
| 16 | #include <ngx_connection.h> |
| 17 | #include <ngx_listen.h> |
Igor Sysoev | c181784 | 2002-12-27 16:22:50 +0000 | [diff] [blame] | 18 | #include <ngx_conf_file.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 19 | |
Igor Sysoev | e0af1b8 | 2002-08-16 15:27:03 +0000 | [diff] [blame] | 20 | /* STUB */ |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 21 | #include <ngx_http.h> |
Igor Sysoev | e0af1b8 | 2002-08-16 15:27:03 +0000 | [diff] [blame] | 22 | /* */ |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 23 | |
| 24 | |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 25 | static void ngx_set_signals(ngx_log_t *log); |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 26 | static void ngx_open_listening_sockets(ngx_log_t *log); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 27 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 28 | |
| 29 | /* STUB */ |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 30 | int ngx_max_conn = 512; |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 31 | u_int ngx_sendfile_flags; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 32 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 33 | ngx_server_t ngx_server; |
| 34 | /* */ |
| 35 | |
| 36 | ngx_log_t ngx_log; |
| 37 | ngx_pool_t *ngx_pool; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 38 | |
| 39 | |
Igor Sysoev | e2a3154 | 2003-04-08 15:40:10 +0000 | [diff] [blame] | 40 | int ngx_max_module; |
| 41 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 42 | int ngx_connection_counter; |
| 43 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 44 | ngx_array_t ngx_listening_sockets; |
Igor Sysoev | e0af1b8 | 2002-08-16 15:27:03 +0000 | [diff] [blame] | 45 | |
| 46 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 47 | int main(int argc, char *const *argv) |
| 48 | { |
Igor Sysoev | b262063 | 2003-01-10 06:09:20 +0000 | [diff] [blame] | 49 | int i; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 50 | ngx_str_t conf_file; |
| 51 | ngx_conf_t conf; |
| 52 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 53 | /* STUB */ |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 54 | ngx_log.log_level = NGX_LOG_DEBUG; |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 55 | |
Igor Sysoev | 79a8048 | 2003-05-14 17:13:13 +0000 | [diff] [blame^] | 56 | if (ngx_os_init(&ngx_log) == NGX_ERROR) { |
| 57 | exit(1); |
| 58 | } |
| 59 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 60 | ngx_pool = ngx_create_pool(16 * 1024, &ngx_log); |
| 61 | /* */ |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 62 | |
Igor Sysoev | 2a2d2b5 | 2003-01-30 18:21:39 +0000 | [diff] [blame] | 63 | #if (WIN32) |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 64 | |
Igor Sysoev | 7300977 | 2003-02-06 17:21:13 +0000 | [diff] [blame] | 65 | if (ngx_init_sockets(&ngx_log) == NGX_ERROR) { |
| 66 | exit(1); |
| 67 | } |
| 68 | |
| 69 | #else |
| 70 | |
| 71 | ngx_set_signals(&ngx_log); |
| 72 | |
| 73 | #endif |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 74 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 75 | ngx_init_array(ngx_listening_sockets, ngx_pool, 10, sizeof(ngx_listen_t), |
| 76 | 1); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 77 | |
Igor Sysoev | e2a3154 | 2003-04-08 15:40:10 +0000 | [diff] [blame] | 78 | ngx_max_module = 0; |
| 79 | for (i = 0; ngx_modules[i]; i++) { |
| 80 | ngx_modules[i]->index = ngx_max_module++; |
| 81 | } |
| 82 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 83 | ngx_memzero(&conf, sizeof(ngx_conf_t)); |
| 84 | ngx_test_null(conf.args, |
| 85 | ngx_create_array(ngx_pool, 10, sizeof(ngx_str_t)), 1); |
| 86 | conf.pool = ngx_pool; |
| 87 | conf.log = &ngx_log; |
Igor Sysoev | 79a8048 | 2003-05-14 17:13:13 +0000 | [diff] [blame^] | 88 | conf.module_type = NGX_CORE_MODULE_TYPE; |
| 89 | conf.cmd_type = NGX_MAIN_CONF; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 90 | |
| 91 | conf_file.len = sizeof("nginx.conf") - 1; |
| 92 | conf_file.data = "nginx.conf"; |
| 93 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 94 | if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) { |
Igor Sysoev | b262063 | 2003-01-10 06:09:20 +0000 | [diff] [blame] | 95 | return 1; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 96 | } |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 97 | |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 98 | ngx_init_temp_number(); |
| 99 | |
Igor Sysoev | b262063 | 2003-01-10 06:09:20 +0000 | [diff] [blame] | 100 | for (i = 0; ngx_modules[i]; i++) { |
| 101 | if (ngx_modules[i]->init_module) { |
| 102 | if (ngx_modules[i]->init_module(ngx_pool) == NGX_ERROR) { |
| 103 | return 1; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 108 | ngx_open_listening_sockets(&ngx_log); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 109 | |
Igor Sysoev | e0af1b8 | 2002-08-16 15:27:03 +0000 | [diff] [blame] | 110 | /* TODO: daemon */ |
| 111 | |
| 112 | /* TODO: fork */ |
| 113 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 114 | ngx_pre_thread(&ngx_listening_sockets, ngx_pool, &ngx_log); |
Igor Sysoev | e0af1b8 | 2002-08-16 15:27:03 +0000 | [diff] [blame] | 115 | |
| 116 | /* TODO: threads */ |
| 117 | |
| 118 | /* STUB */ |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 119 | ngx_worker(&ngx_log); |
Igor Sysoev | a58e3ca | 2002-09-02 14:48:24 +0000 | [diff] [blame] | 120 | |
| 121 | return 0; |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 124 | #if !(WIN32) |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 125 | static void ngx_set_signals(ngx_log_t *log) |
| 126 | { |
| 127 | struct sigaction sa; |
| 128 | |
| 129 | ngx_memzero(&sa, sizeof(struct sigaction)); |
| 130 | sa.sa_handler = SIG_IGN; |
| 131 | sigemptyset(&sa.sa_mask); |
| 132 | if (sigaction(SIGPIPE, &sa, NULL) == -1) { |
| 133 | ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, |
| 134 | "sigaction(SIGPIPE, SIG_IGN) failed"); |
| 135 | exit(1); |
| 136 | } |
| 137 | } |
Igor Sysoev | 42feecb | 2002-12-15 06:25:09 +0000 | [diff] [blame] | 138 | #endif |
Igor Sysoev | b086905 | 2002-12-10 18:05:12 +0000 | [diff] [blame] | 139 | |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 140 | static void ngx_open_listening_sockets(ngx_log_t *log) |
| 141 | { |
| 142 | int times, failed, reuseaddr, i; |
| 143 | ngx_err_t err; |
| 144 | ngx_socket_t s; |
| 145 | ngx_listen_t *ls; |
| 146 | |
| 147 | reuseaddr = 1; |
| 148 | |
| 149 | for (times = 10; times; times--) { |
| 150 | failed = 0; |
| 151 | |
| 152 | /* for each listening socket */ |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 153 | ls = (ngx_listen_t *) ngx_listening_sockets.elts; |
| 154 | for (i = 0; i < ngx_listening_sockets.nelts; i++) { |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 155 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 156 | if (ls[i].bound) |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 157 | continue; |
| 158 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 159 | if (ls[i].inherited) { |
| 160 | |
| 161 | /* TODO: close on exit */ |
| 162 | /* TODO: nonblocking */ |
| 163 | /* TODO: deferred accept */ |
| 164 | |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 165 | ls[i].bound = 1; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 166 | continue; |
| 167 | } |
| 168 | |
| 169 | s = ngx_socket(ls[i].family, ls[i].type, ls[i].protocol, |
| 170 | ls[i].flags); |
| 171 | if (s == -1) { |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 172 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame] | 173 | ngx_socket_n " %s falied", ls[i].addr_text.data); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 174 | exit(1); |
| 175 | } |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 176 | |
| 177 | if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 178 | (const void *) &reuseaddr, sizeof(int)) == -1) { |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 179 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 180 | "setsockopt(SO_REUSEADDR) %s failed", |
Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame] | 181 | ls[i].addr_text.data); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 182 | exit(1); |
| 183 | } |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 184 | |
| 185 | /* TODO: close on exit */ |
| 186 | |
| 187 | if (ls[i].nonblocking) { |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 188 | if (ngx_nonblocking(s) == -1) { |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 189 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
| 190 | ngx_nonblocking_n " %s failed", |
Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame] | 191 | ls[i].addr_text.data); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 192 | exit(1); |
| 193 | } |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 196 | if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) { |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 197 | err = ngx_socket_errno; |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 198 | ngx_log_error(NGX_LOG_EMERG, log, err, |
Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame] | 199 | "bind() to %s failed", ls[i].addr_text.data); |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 200 | |
| 201 | if (err != NGX_EADDRINUSE) |
| 202 | exit(1); |
| 203 | |
| 204 | if (ngx_close_socket(s) == -1) |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 205 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 206 | ngx_close_socket_n " %s failed", |
Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame] | 207 | ls[i].addr_text.data); |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 208 | |
| 209 | failed = 1; |
| 210 | continue; |
| 211 | } |
| 212 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 213 | if (listen(s, ls[i].backlog) == -1) { |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 214 | ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno, |
Igor Sysoev | 8809257 | 2002-12-19 07:08:55 +0000 | [diff] [blame] | 215 | "listen() to %s failed", ls[i].addr_text.data); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 216 | exit(1); |
| 217 | } |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 218 | |
| 219 | /* TODO: deferred accept */ |
| 220 | |
| 221 | ls[i].fd = s; |
Igor Sysoev | 4e9393a | 2003-01-09 05:36:00 +0000 | [diff] [blame] | 222 | ls[i].bound = 1; |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | if (!failed) |
| 226 | break; |
| 227 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 228 | ngx_log_error(NGX_LOG_NOTICE, log, 0, |
| 229 | "try again to bind() after 500ms"); |
Igor Sysoev | 2b54238 | 2002-08-20 14:48:28 +0000 | [diff] [blame] | 230 | ngx_msleep(500); |
| 231 | } |
| 232 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 233 | if (failed) { |
| 234 | ngx_log_error(NGX_LOG_EMERG, log, 0, "can not bind(), exiting"); |
| 235 | exit(1); |
| 236 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 237 | } |