blob: 4a22e581fc766c4eabe9b800cf0089581f9b6eb9 [file] [log] [blame]
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00001
2#include <nginx.h>
3
4#include <ngx_config.h>
Igor Sysoevc1817842002-12-27 16:22:50 +00005
Igor Sysoevb2620632003-01-10 06:09:20 +00006#include <ngx_core.h>
Igor Sysoev79a80482003-05-14 17:13:13 +00007#include <ngx_os_init.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +00008#include <ngx_string.h>
Igor Sysoev2b542382002-08-20 14:48:28 +00009#include <ngx_errno.h>
10#include <ngx_time.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000011#include <ngx_log.h>
12#include <ngx_alloc.h>
Igor Sysoeve0af1b82002-08-16 15:27:03 +000013#include <ngx_array.h>
14#include <ngx_socket.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000015#include <ngx_server.h>
16#include <ngx_connection.h>
17#include <ngx_listen.h>
Igor Sysoevc1817842002-12-27 16:22:50 +000018#include <ngx_conf_file.h>
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000019
Igor Sysoeve0af1b82002-08-16 15:27:03 +000020/* STUB */
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000021#include <ngx_http.h>
Igor Sysoeve0af1b82002-08-16 15:27:03 +000022/* */
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000023
24
Igor Sysoevb0869052002-12-10 18:05:12 +000025static void ngx_set_signals(ngx_log_t *log);
Igor Sysoev2b542382002-08-20 14:48:28 +000026static void ngx_open_listening_sockets(ngx_log_t *log);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000027
Igor Sysoev2b542382002-08-20 14:48:28 +000028
29/* STUB */
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000030int ngx_max_conn = 512;
Igor Sysoev73009772003-02-06 17:21:13 +000031u_int ngx_sendfile_flags;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000032
Igor Sysoev2b542382002-08-20 14:48:28 +000033ngx_server_t ngx_server;
34/* */
35
36ngx_log_t ngx_log;
37ngx_pool_t *ngx_pool;
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000038
39
Igor Sysoeve2a31542003-04-08 15:40:10 +000040int ngx_max_module;
41
Igor Sysoev42feecb2002-12-15 06:25:09 +000042int ngx_connection_counter;
43
Igor Sysoev4e9393a2003-01-09 05:36:00 +000044ngx_array_t ngx_listening_sockets;
Igor Sysoeve0af1b82002-08-16 15:27:03 +000045
46
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000047int main(int argc, char *const *argv)
48{
Igor Sysoevb2620632003-01-10 06:09:20 +000049 int i;
Igor Sysoeva6717c42002-12-23 06:29:22 +000050 ngx_str_t conf_file;
51 ngx_conf_t conf;
52
Igor Sysoev2b542382002-08-20 14:48:28 +000053 /* STUB */
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000054 ngx_log.log_level = NGX_LOG_DEBUG;
Igor Sysoev2b542382002-08-20 14:48:28 +000055
Igor Sysoev79a80482003-05-14 17:13:13 +000056 if (ngx_os_init(&ngx_log) == NGX_ERROR) {
57 exit(1);
58 }
59
Igor Sysoev2b542382002-08-20 14:48:28 +000060 ngx_pool = ngx_create_pool(16 * 1024, &ngx_log);
61 /* */
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000062
Igor Sysoev2a2d2b52003-01-30 18:21:39 +000063#if (WIN32)
Igor Sysoevb0869052002-12-10 18:05:12 +000064
Igor Sysoev73009772003-02-06 17:21:13 +000065 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 Sysoev6de5c2c2002-08-06 16:39:45 +000074
Igor Sysoev4e9393a2003-01-09 05:36:00 +000075 ngx_init_array(ngx_listening_sockets, ngx_pool, 10, sizeof(ngx_listen_t),
76 1);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +000077
Igor Sysoeve2a31542003-04-08 15:40:10 +000078 ngx_max_module = 0;
79 for (i = 0; ngx_modules[i]; i++) {
80 ngx_modules[i]->index = ngx_max_module++;
81 }
82
Igor Sysoeva6717c42002-12-23 06:29:22 +000083 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 Sysoev79a80482003-05-14 17:13:13 +000088 conf.module_type = NGX_CORE_MODULE_TYPE;
89 conf.cmd_type = NGX_MAIN_CONF;
Igor Sysoeva6717c42002-12-23 06:29:22 +000090
91 conf_file.len = sizeof("nginx.conf") - 1;
92 conf_file.data = "nginx.conf";
93
Igor Sysoev4e9393a2003-01-09 05:36:00 +000094 if (ngx_conf_parse(&conf, &conf_file) != NGX_CONF_OK) {
Igor Sysoevb2620632003-01-10 06:09:20 +000095 return 1;
Igor Sysoev4e9393a2003-01-09 05:36:00 +000096 }
Igor Sysoeva6717c42002-12-23 06:29:22 +000097
Igor Sysoev1d8d9ee2003-04-28 15:06:39 +000098 ngx_init_temp_number();
99
Igor Sysoevb2620632003-01-10 06:09:20 +0000100 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 Sysoev2b542382002-08-20 14:48:28 +0000108 ngx_open_listening_sockets(&ngx_log);
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000109
Igor Sysoeve0af1b82002-08-16 15:27:03 +0000110 /* TODO: daemon */
111
112 /* TODO: fork */
113
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000114 ngx_pre_thread(&ngx_listening_sockets, ngx_pool, &ngx_log);
Igor Sysoeve0af1b82002-08-16 15:27:03 +0000115
116 /* TODO: threads */
117
118 /* STUB */
Igor Sysoev2b542382002-08-20 14:48:28 +0000119 ngx_worker(&ngx_log);
Igor Sysoeva58e3ca2002-09-02 14:48:24 +0000120
121 return 0;
Igor Sysoev2b542382002-08-20 14:48:28 +0000122}
123
Igor Sysoev42feecb2002-12-15 06:25:09 +0000124#if !(WIN32)
Igor Sysoevb0869052002-12-10 18:05:12 +0000125static 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 Sysoev42feecb2002-12-15 06:25:09 +0000138#endif
Igor Sysoevb0869052002-12-10 18:05:12 +0000139
Igor Sysoev2b542382002-08-20 14:48:28 +0000140static 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 Sysoev4e9393a2003-01-09 05:36:00 +0000153 ls = (ngx_listen_t *) ngx_listening_sockets.elts;
154 for (i = 0; i < ngx_listening_sockets.nelts; i++) {
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000155
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000156 if (ls[i].bound)
Igor Sysoev2b542382002-08-20 14:48:28 +0000157 continue;
158
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000159 if (ls[i].inherited) {
160
161 /* TODO: close on exit */
162 /* TODO: nonblocking */
163 /* TODO: deferred accept */
164
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000165 ls[i].bound = 1;
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000166 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 Sysoev2b542382002-08-20 14:48:28 +0000172 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
Igor Sysoev88092572002-12-19 07:08:55 +0000173 ngx_socket_n " %s falied", ls[i].addr_text.data);
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000174 exit(1);
175 }
Igor Sysoev2b542382002-08-20 14:48:28 +0000176
177 if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR,
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000178 (const void *) &reuseaddr, sizeof(int)) == -1) {
Igor Sysoev2b542382002-08-20 14:48:28 +0000179 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000180 "setsockopt(SO_REUSEADDR) %s failed",
Igor Sysoev88092572002-12-19 07:08:55 +0000181 ls[i].addr_text.data);
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000182 exit(1);
183 }
Igor Sysoev2b542382002-08-20 14:48:28 +0000184
185 /* TODO: close on exit */
186
187 if (ls[i].nonblocking) {
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000188 if (ngx_nonblocking(s) == -1) {
Igor Sysoev2b542382002-08-20 14:48:28 +0000189 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
190 ngx_nonblocking_n " %s failed",
Igor Sysoev88092572002-12-19 07:08:55 +0000191 ls[i].addr_text.data);
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000192 exit(1);
193 }
Igor Sysoev2b542382002-08-20 14:48:28 +0000194 }
195
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000196 if (bind(s, ls[i].sockaddr, ls[i].socklen) == -1) {
Igor Sysoev2b542382002-08-20 14:48:28 +0000197 err = ngx_socket_errno;
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000198 ngx_log_error(NGX_LOG_EMERG, log, err,
Igor Sysoev88092572002-12-19 07:08:55 +0000199 "bind() to %s failed", ls[i].addr_text.data);
Igor Sysoev2b542382002-08-20 14:48:28 +0000200
201 if (err != NGX_EADDRINUSE)
202 exit(1);
203
204 if (ngx_close_socket(s) == -1)
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000205 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
Igor Sysoev2b542382002-08-20 14:48:28 +0000206 ngx_close_socket_n " %s failed",
Igor Sysoev88092572002-12-19 07:08:55 +0000207 ls[i].addr_text.data);
Igor Sysoev2b542382002-08-20 14:48:28 +0000208
209 failed = 1;
210 continue;
211 }
212
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000213 if (listen(s, ls[i].backlog) == -1) {
Igor Sysoev2b542382002-08-20 14:48:28 +0000214 ngx_log_error(NGX_LOG_EMERG, log, ngx_socket_errno,
Igor Sysoev88092572002-12-19 07:08:55 +0000215 "listen() to %s failed", ls[i].addr_text.data);
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000216 exit(1);
217 }
Igor Sysoev2b542382002-08-20 14:48:28 +0000218
219 /* TODO: deferred accept */
220
221 ls[i].fd = s;
Igor Sysoev4e9393a2003-01-09 05:36:00 +0000222 ls[i].bound = 1;
Igor Sysoev2b542382002-08-20 14:48:28 +0000223 }
224
225 if (!failed)
226 break;
227
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000228 ngx_log_error(NGX_LOG_NOTICE, log, 0,
229 "try again to bind() after 500ms");
Igor Sysoev2b542382002-08-20 14:48:28 +0000230 ngx_msleep(500);
231 }
232
Igor Sysoev0ad17c02002-08-26 15:18:19 +0000233 if (failed) {
234 ngx_log_error(NGX_LOG_EMERG, log, 0, "can not bind(), exiting");
235 exit(1);
236 }
Igor Sysoev6de5c2c2002-08-06 16:39:45 +0000237}