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