Mercurial > hg > nginx-vendor-0-6
comparison src/os/unix/ngx_posix_init.c @ 88:e916a291e9aa NGINX_0_1_44
nginx 0.1.44
*) Feature: the IMAP/POP3 proxy supports SSL.
*) Feature: the "proxy_timeout" directive of the ngx_imap_proxy_module.
*) Feature: the "userid_mark" directive.
*) Feature: the $remote_user variable value is determined independently
of authorization use.
author | Igor Sysoev <http://sysoev.ru> |
---|---|
date | Tue, 06 Sep 2005 00:00:00 +0400 |
parents | 6ae11d59d10e |
children | 71c46860eb55 |
comparison
equal
deleted
inserted
replaced
87:5b7ec80c3c40 | 88:e916a291e9aa |
---|---|
4 */ | 4 */ |
5 | 5 |
6 | 6 |
7 #include <ngx_config.h> | 7 #include <ngx_config.h> |
8 #include <ngx_core.h> | 8 #include <ngx_core.h> |
9 #include <nginx.h> | |
9 | 10 |
10 | 11 |
11 ngx_int_t ngx_ncpu; | 12 ngx_int_t ngx_ncpu; |
12 ngx_int_t ngx_max_sockets; | 13 ngx_int_t ngx_max_sockets; |
13 ngx_uint_t ngx_inherited_nonblocking; | 14 ngx_uint_t ngx_inherited_nonblocking; |
15 | 16 |
16 | 17 |
17 struct rlimit rlmt; | 18 struct rlimit rlmt; |
18 | 19 |
19 | 20 |
20 #if (NGX_POSIX_IO) | |
21 | |
22 ngx_os_io_t ngx_os_io = { | 21 ngx_os_io_t ngx_os_io = { |
23 ngx_unix_recv, | 22 ngx_unix_recv, |
24 ngx_readv_chain, | 23 ngx_readv_chain, |
25 NULL, | 24 NULL, |
26 ngx_writev_chain, | 25 ngx_writev_chain, |
27 0 | 26 0 |
28 }; | 27 }; |
29 | 28 |
30 | 29 |
31 ngx_int_t ngx_os_init(ngx_log_t *log) | 30 ngx_int_t |
31 ngx_os_init(ngx_log_t *log) | |
32 { | 32 { |
33 return ngx_posix_init(log); | 33 ngx_log_error(NGX_LOG_NOTICE, log, 0, NGINX_VER); |
34 } | |
35 | 34 |
36 | 35 #if (NGX_HAVE_OS_SPECIFIC_INIT) |
37 void ngx_os_status(ngx_log_t *log) | 36 if (ngx_os_specific_init(log) != NGX_OK) { |
38 { | 37 return NGX_ERROR; |
39 ngx_posix_status(log); | 38 } |
40 } | |
41 | |
42 | |
43 #endif | 39 #endif |
44 | |
45 | |
46 void ngx_signal_handler(int signo); | |
47 | |
48 | |
49 typedef struct { | |
50 int signo; | |
51 char *signame; | |
52 void (*handler)(int signo); | |
53 } ngx_signal_t; | |
54 | |
55 | |
56 ngx_signal_t signals[] = { | |
57 { ngx_signal_value(NGX_RECONFIGURE_SIGNAL), | |
58 "SIG" ngx_value(NGX_RECONFIGURE_SIGNAL), | |
59 ngx_signal_handler }, | |
60 | |
61 { ngx_signal_value(NGX_REOPEN_SIGNAL), | |
62 "SIG" ngx_value(NGX_REOPEN_SIGNAL), | |
63 ngx_signal_handler }, | |
64 | |
65 { ngx_signal_value(NGX_NOACCEPT_SIGNAL), | |
66 "SIG" ngx_value(NGX_NOACCEPT_SIGNAL), | |
67 ngx_signal_handler }, | |
68 | |
69 { ngx_signal_value(NGX_TERMINATE_SIGNAL), | |
70 "SIG" ngx_value(NGX_TERMINATE_SIGNAL), | |
71 ngx_signal_handler }, | |
72 | |
73 { ngx_signal_value(NGX_SHUTDOWN_SIGNAL), | |
74 "SIG" ngx_value(NGX_SHUTDOWN_SIGNAL), | |
75 ngx_signal_handler }, | |
76 | |
77 { ngx_signal_value(NGX_CHANGEBIN_SIGNAL), | |
78 "SIG" ngx_value(NGX_CHANGEBIN_SIGNAL), | |
79 ngx_signal_handler }, | |
80 | |
81 { SIGALRM, "SIGALRM", ngx_signal_handler }, | |
82 | |
83 { SIGINT, "SIGINT", ngx_signal_handler }, | |
84 | |
85 { SIGIO, "SIGIO", ngx_signal_handler }, | |
86 | |
87 { SIGCHLD, "SIGCHLD", ngx_signal_handler }, | |
88 | |
89 { SIGPIPE, "SIGPIPE, SIG_IGN", SIG_IGN }, | |
90 | |
91 { 0, NULL, NULL } | |
92 }; | |
93 | |
94 | |
95 ngx_int_t ngx_posix_init(ngx_log_t *log) | |
96 { | |
97 ngx_signal_t *sig; | |
98 struct sigaction sa; | |
99 | 40 |
100 ngx_init_setproctitle(log); | 41 ngx_init_setproctitle(log); |
101 | 42 |
102 ngx_pagesize = getpagesize(); | 43 ngx_pagesize = getpagesize(); |
103 | 44 |
104 if (ngx_ncpu == 0) { | 45 if (ngx_ncpu == 0) { |
105 ngx_ncpu = 1; | 46 ngx_ncpu = 1; |
106 } | 47 } |
107 | 48 |
108 for (sig = signals; sig->signo != 0; sig++) { | |
109 ngx_memzero(&sa, sizeof(struct sigaction)); | |
110 sa.sa_handler = sig->handler; | |
111 sigemptyset(&sa.sa_mask); | |
112 if (sigaction(sig->signo, &sa, NULL) == -1) { | |
113 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, | |
114 "sigaction(%s) failed", sig->signame); | |
115 return NGX_ERROR; | |
116 } | |
117 } | |
118 | |
119 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) { | 49 if (getrlimit(RLIMIT_NOFILE, &rlmt) == -1) { |
120 ngx_log_error(NGX_LOG_ALERT, log, errno, | 50 ngx_log_error(NGX_LOG_ALERT, log, errno, |
121 "getrlimit(RLIMIT_NOFILE) failed)"); | 51 "getrlimit(RLIMIT_NOFILE) failed)"); |
122 return NGX_ERROR; | 52 return NGX_ERROR; |
123 } | 53 } |
124 | 54 |
125 ngx_max_sockets = rlmt.rlim_cur; | 55 ngx_max_sockets = rlmt.rlim_cur; |
126 | 56 |
127 #if (NGX_HAVE_INHERITED_NONBLOCK) | 57 #if (NGX_HAVE_INHERITED_NONBLOCK) |
128 ngx_inherited_nonblocking = 1; | 58 ngx_inherited_nonblocking = 1; |
129 #else | 59 #else |
132 | 62 |
133 return NGX_OK; | 63 return NGX_OK; |
134 } | 64 } |
135 | 65 |
136 | 66 |
137 void ngx_posix_status(ngx_log_t *log) | 67 void |
68 ngx_os_status(ngx_log_t *log) | |
138 { | 69 { |
70 #if (NGX_HAVE_OS_SPECIFIC_INIT) | |
71 ngx_os_specific_status(log); | |
72 #endif | |
73 | |
139 ngx_log_error(NGX_LOG_NOTICE, log, 0, | 74 ngx_log_error(NGX_LOG_NOTICE, log, 0, |
140 "getrlimit(RLIMIT_NOFILE): %r:%r", | 75 "getrlimit(RLIMIT_NOFILE): %r:%r", |
141 rlmt.rlim_cur, rlmt.rlim_max); | 76 rlmt.rlim_cur, rlmt.rlim_max); |
142 } | 77 } |
143 | 78 |
144 | 79 |
145 void ngx_signal_handler(int signo) | 80 ngx_int_t |
146 { | 81 ngx_posix_post_conf_init(ngx_log_t *log) |
147 char *action; | |
148 struct timeval tv; | |
149 ngx_int_t ignore; | |
150 ngx_err_t err; | |
151 ngx_signal_t *sig; | |
152 | |
153 ignore = 0; | |
154 | |
155 err = ngx_errno; | |
156 | |
157 for (sig = signals; sig->signo != 0; sig++) { | |
158 if (sig->signo == signo) { | |
159 break; | |
160 } | |
161 } | |
162 | |
163 ngx_gettimeofday(&tv); | |
164 ngx_time_update(tv.tv_sec); | |
165 | |
166 action = ""; | |
167 | |
168 switch (ngx_process) { | |
169 | |
170 case NGX_PROCESS_MASTER: | |
171 case NGX_PROCESS_SINGLE: | |
172 switch (signo) { | |
173 | |
174 case ngx_signal_value(NGX_SHUTDOWN_SIGNAL): | |
175 ngx_quit = 1; | |
176 action = ", shutting down"; | |
177 break; | |
178 | |
179 case ngx_signal_value(NGX_TERMINATE_SIGNAL): | |
180 case SIGINT: | |
181 ngx_terminate = 1; | |
182 action = ", exiting"; | |
183 break; | |
184 | |
185 case ngx_signal_value(NGX_NOACCEPT_SIGNAL): | |
186 ngx_noaccept = 1; | |
187 action = ", stop accepting connections"; | |
188 break; | |
189 | |
190 case ngx_signal_value(NGX_RECONFIGURE_SIGNAL): | |
191 ngx_reconfigure = 1; | |
192 action = ", reconfiguring"; | |
193 break; | |
194 | |
195 case ngx_signal_value(NGX_REOPEN_SIGNAL): | |
196 ngx_reopen = 1; | |
197 action = ", reopening logs"; | |
198 break; | |
199 | |
200 case ngx_signal_value(NGX_CHANGEBIN_SIGNAL): | |
201 if (getppid() > 1 || ngx_new_binary > 0) { | |
202 | |
203 /* | |
204 * Ignore the signal in the new binary if its parent is | |
205 * not the init process, i.e. the old binary's process | |
206 * is still running. Or ingore the signal in the old binary's | |
207 * process if the new binary's process is already running. | |
208 */ | |
209 | |
210 action = ", ignoring"; | |
211 ignore = 1; | |
212 break; | |
213 } | |
214 | |
215 ngx_change_binary = 1; | |
216 action = ", changing binary"; | |
217 break; | |
218 | |
219 case SIGALRM: | |
220 if (!ngx_terminate) { | |
221 ngx_timer = 1; | |
222 action = ", shutting down old worker processes"; | |
223 } | |
224 | |
225 break; | |
226 | |
227 case SIGIO: | |
228 ngx_sigio = 1; | |
229 break; | |
230 | |
231 case SIGCHLD: | |
232 ngx_reap = 1; | |
233 break; | |
234 } | |
235 | |
236 break; | |
237 | |
238 case NGX_PROCESS_WORKER: | |
239 switch (signo) { | |
240 | |
241 case ngx_signal_value(NGX_NOACCEPT_SIGNAL): | |
242 ngx_debug_quit = 1; | |
243 case ngx_signal_value(NGX_SHUTDOWN_SIGNAL): | |
244 ngx_quit = 1; | |
245 action = ", shutting down"; | |
246 break; | |
247 | |
248 case ngx_signal_value(NGX_TERMINATE_SIGNAL): | |
249 case SIGINT: | |
250 ngx_terminate = 1; | |
251 action = ", exiting"; | |
252 break; | |
253 | |
254 case ngx_signal_value(NGX_REOPEN_SIGNAL): | |
255 ngx_reopen = 1; | |
256 action = ", reopening logs"; | |
257 break; | |
258 | |
259 case ngx_signal_value(NGX_RECONFIGURE_SIGNAL): | |
260 case ngx_signal_value(NGX_CHANGEBIN_SIGNAL): | |
261 case SIGIO: | |
262 action = ", ignoring"; | |
263 break; | |
264 } | |
265 | |
266 break; | |
267 } | |
268 | |
269 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, | |
270 "signal %d (%s) received%s", signo, sig->signame, action); | |
271 | |
272 if (ignore) { | |
273 ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, 0, | |
274 "the changing binary signal is ignored: " | |
275 "you should shutdown or terminate " | |
276 "before either old or new binary's process"); | |
277 } | |
278 | |
279 if (signo == SIGCHLD) { | |
280 ngx_process_get_status(); | |
281 } | |
282 | |
283 ngx_set_errno(err); | |
284 } | |
285 | |
286 | |
287 ngx_int_t ngx_posix_post_conf_init(ngx_log_t *log) | |
288 { | 82 { |
289 ngx_fd_t pp[2]; | 83 ngx_fd_t pp[2]; |
290 | 84 |
291 if (pipe(pp) == -1) { | 85 if (pipe(pp) == -1) { |
292 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed"); | 86 ngx_log_error(NGX_LOG_EMERG, log, ngx_errno, "pipe() failed"); |