Ticket #514: ngx_process_cycle.c

File ngx_process_cycle.c, 27.4 KB (added by Nos Apatka, 12 years ago)

modified source code

Line 
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
6
7
8#include <ngx_config.h>
9#include <ngx_core.h>
10#include <ngx_event.h>
11#include <nginx.h>
12
13
14static void ngx_process_init(ngx_cycle_t *cycle);
15static void ngx_console_init(ngx_cycle_t *cycle);
16static int __stdcall ngx_console_handler(u_long type);
17static ngx_int_t ngx_create_signal_events(ngx_cycle_t *cycle);
18static ngx_int_t ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t type);
19static void ngx_reopen_worker_processes(ngx_cycle_t *cycle);
20static void ngx_quit_worker_processes(ngx_cycle_t *cycle, ngx_uint_t old);
21static void ngx_terminate_worker_processes(ngx_cycle_t *cycle);
22static ngx_uint_t ngx_reap_worker(ngx_cycle_t *cycle, HANDLE h);
23static void ngx_master_process_exit(ngx_cycle_t *cycle);
24static void ngx_worker_process_cycle(ngx_cycle_t *cycle, char *mevn);
25static void ngx_worker_process_exit(ngx_cycle_t *cycle);
26static ngx_thread_value_t __stdcall ngx_worker_thread(void *data);
27static ngx_thread_value_t __stdcall ngx_cache_manager_thread(void *data);
28static void ngx_cache_manager_process_handler(void);
29static ngx_thread_value_t __stdcall ngx_cache_loader_thread(void *data);
30
31
32ngx_uint_t ngx_process;
33ngx_pid_t ngx_pid;
34ngx_uint_t ngx_threaded;
35
36ngx_uint_t ngx_inherited;
37ngx_pid_t ngx_new_binary;
38
39sig_atomic_t ngx_terminate;
40sig_atomic_t ngx_quit;
41sig_atomic_t ngx_reopen;
42sig_atomic_t ngx_reconfigure;
43ngx_uint_t ngx_exiting;
44
45
46HANDLE ngx_master_process_event;
47char ngx_master_process_event_name[NGX_PROCESS_SYNC_NAME];
48
49static HANDLE ngx_stop_event;
50static char ngx_stop_event_name[NGX_PROCESS_SYNC_NAME];
51static HANDLE ngx_quit_event;
52static char ngx_quit_event_name[NGX_PROCESS_SYNC_NAME];
53static HANDLE ngx_reopen_event;
54static char ngx_reopen_event_name[NGX_PROCESS_SYNC_NAME];
55static HANDLE ngx_reload_event;
56static char ngx_reload_event_name[NGX_PROCESS_SYNC_NAME];
57
58HANDLE ngx_cache_manager_mutex;
59char ngx_cache_manager_mutex_name[NGX_PROCESS_SYNC_NAME];
60HANDLE ngx_cache_manager_event;
61
62
63void
64ngx_master_process_cycle(ngx_cycle_t *cycle)
65{
66 u_long nev, ev, timeout;
67 ngx_err_t err;
68 ngx_int_t n;
69 ngx_msec_t timer;
70 ngx_uint_t live;
71 HANDLE events[MAXIMUM_WAIT_OBJECTS];
72
73 ngx_process_init(cycle);
74
75 ngx_sprintf((u_char *) ngx_master_process_event_name,
76 "ngx_master_%s%Z", ngx_unique);
77
78 if (ngx_process == NGX_PROCESS_WORKER) {
79 ngx_worker_process_cycle(cycle, ngx_master_process_event_name);
80 return;
81 }
82
83 ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "master started");
84
85 ngx_console_init(cycle);
86
87 SetEnvironmentVariable("ngx_unique", ngx_unique);
88
89 ngx_master_process_event = CreateEvent(NULL, 1, 0,
90 ngx_master_process_event_name);
91 if (ngx_master_process_event == NULL) {
92 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
93 "CreateEvent(\"%s\") failed",
94 ngx_master_process_event_name);
95 exit(2);
96 }
97
98 if (ngx_create_signal_events(cycle) != NGX_OK) {
99 exit(2);
100 }
101
102 ngx_sprintf((u_char *) ngx_cache_manager_mutex_name,
103 "ngx_cache_manager_mutex_%s%Z", ngx_unique);
104
105 ngx_cache_manager_mutex = CreateMutex(NULL, 0,
106 ngx_cache_manager_mutex_name);
107 if (ngx_cache_manager_mutex == NULL) {
108 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
109 "CreateMutex(\"%s\") failed", ngx_cache_manager_mutex_name);
110 exit(2);
111 }
112
113
114 events[0] = ngx_stop_event;
115 events[1] = ngx_quit_event;
116 events[2] = ngx_reopen_event;
117 events[3] = ngx_reload_event;
118
119 ngx_close_listening_sockets(cycle);
120
121 if (ngx_start_worker_processes(cycle, NGX_PROCESS_RESPAWN) == 0) {
122 exit(2);
123 }
124
125 timer = 0;
126 timeout = INFINITE;
127
128 for ( ;; ) {
129
130 nev = 4;
131 for (n = 0; n < ngx_last_process; n++) {
132 if (ngx_processes[n].handle) {
133 events[nev++] = ngx_processes[n].handle;
134 }
135 }
136
137 if (timer) {
138 timeout = timer > ngx_current_msec ? timer - ngx_current_msec : 0;
139 }
140
141 ev = WaitForMultipleObjects(nev, events, 0, timeout);
142
143 err = ngx_errno;
144 ngx_time_update();
145
146 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
147 "master WaitForMultipleObjects: %ul", ev);
148
149 if (ev == WAIT_OBJECT_0) {
150 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
151
152 if (ResetEvent(ngx_stop_event) == 0) {
153 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
154 "ResetEvent(\"%s\") failed", ngx_stop_event_name);
155 }
156
157 if (timer == 0) {
158 timer = ngx_current_msec + 5000;
159 }
160
161 ngx_terminate = 1;
162 ngx_quit_worker_processes(cycle, 0);
163
164 continue;
165 }
166
167 if (ev == WAIT_OBJECT_0 + 1) {
168 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "shutting down");
169
170 if (ResetEvent(ngx_quit_event) == 0) {
171 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
172 "ResetEvent(\"%s\") failed", ngx_quit_event_name);
173 }
174
175 ngx_quit = 1;
176 ngx_quit_worker_processes(cycle, 0);
177
178 continue;
179 }
180
181 if (ev == WAIT_OBJECT_0 + 2) {
182 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reopening logs");
183
184 if (ResetEvent(ngx_reopen_event) == 0) {
185 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
186 "ResetEvent(\"%s\") failed",
187 ngx_reopen_event_name);
188 }
189
190 ngx_reopen_files(cycle, -1);
191 ngx_reopen_worker_processes(cycle);
192
193 continue;
194 }
195
196 if (ev == WAIT_OBJECT_0 + 3) {
197 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "reconfiguring");
198
199 if (ResetEvent(ngx_reload_event) == 0) {
200 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
201 "ResetEvent(\"%s\") failed",
202 ngx_reload_event_name);
203 }
204
205 cycle = ngx_init_cycle(cycle);
206 if (cycle == NULL) {
207 cycle = (ngx_cycle_t *) ngx_cycle;
208 continue;
209 }
210
211 ngx_cycle = cycle;
212
213 ngx_close_listening_sockets(cycle);
214
215 if (ngx_start_worker_processes(cycle, NGX_PROCESS_JUST_RESPAWN)) {
216 ngx_quit_worker_processes(cycle, 1);
217 }
218
219 continue;
220 }
221
222 if (ev > WAIT_OBJECT_0 + 3 && ev < WAIT_OBJECT_0 + nev) {
223
224 ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "reap worker");
225
226 live = ngx_reap_worker(cycle, events[ev]);
227
228 if (!live && (ngx_terminate || ngx_quit)) {
229 ngx_master_process_exit(cycle);
230 }
231
232 continue;
233 }
234
235 if (ev == WAIT_TIMEOUT) {
236 ngx_terminate_worker_processes(cycle);
237
238 ngx_master_process_exit(cycle);
239 }
240
241 if (ev == WAIT_FAILED) {
242 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
243 "WaitForMultipleObjects() failed");
244
245 continue;
246 }
247
248 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
249 "WaitForMultipleObjects() returned unexpected value %ul", ev);
250 }
251}
252
253
254static void
255ngx_process_init(ngx_cycle_t *cycle)
256{
257 ngx_err_t err;
258 ngx_core_conf_t *ccf;
259
260 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
261
262 if (ngx_init_threads(ngx_threads_n, ccf->thread_stack_size, cycle)
263 != NGX_OK)
264 {
265 /* fatal */
266 exit(2);
267 }
268
269 err = ngx_thread_key_create(&ngx_core_tls_key);
270 if (err != 0) {
271 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
272 ngx_thread_key_create_n " failed");
273 /* fatal */
274 exit(2);
275 }
276}
277
278
279static void
280ngx_console_init(ngx_cycle_t *cycle)
281{
282 ngx_core_conf_t *ccf;
283
284 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
285
286 if (ccf->daemon) {
287 if (FreeConsole() == 0) {
288 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
289 "FreeConsole() failed");
290 }
291
292 return;
293 }
294
295 if (SetConsoleCtrlHandler(ngx_console_handler, 1) == 0) {
296 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
297 "SetConsoleCtrlHandler() failed");
298 }
299}
300
301
302static int __stdcall
303ngx_console_handler(u_long type)
304{
305 char *msg;
306
307 switch (type) {
308
309 case CTRL_C_EVENT:
310 msg = "Ctrl-C pressed, exiting";
311 break;
312
313 case CTRL_BREAK_EVENT:
314 msg = "Ctrl-Break pressed, exiting";
315 break;
316
317 case CTRL_CLOSE_EVENT:
318 msg = "console closing, exiting";
319 break;
320
321 case CTRL_LOGOFF_EVENT:
322 msg = "user logs off, exiting";
323 break;
324
325 default:
326 return 0;
327 }
328
329 ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0, msg);
330
331 if (ngx_stop_event == NULL) {
332 return 1;
333 }
334
335 if (SetEvent(ngx_stop_event) == 0) {
336 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
337 "SetEvent(\"%s\") failed", ngx_stop_event_name);
338 }
339
340 return 1;
341}
342
343
344static ngx_int_t
345ngx_create_signal_events(ngx_cycle_t *cycle)
346{
347 ngx_sprintf((u_char *) ngx_stop_event_name,
348 "Global\\ngx_stop_%s%Z", ngx_unique);
349
350 ngx_stop_event = CreateEvent(NULL, 1, 0, ngx_stop_event_name);
351 if (ngx_stop_event == NULL) {
352 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
353 "CreateEvent(\"%s\") failed", ngx_stop_event_name);
354 return NGX_ERROR;
355 }
356
357
358 ngx_sprintf((u_char *) ngx_quit_event_name,
359 "Global\\ngx_quit_%s%Z", ngx_unique);
360
361 ngx_quit_event = CreateEvent(NULL, 1, 0, ngx_quit_event_name);
362 if (ngx_quit_event == NULL) {
363 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
364 "CreateEvent(\"%s\") failed", ngx_quit_event_name);
365 return NGX_ERROR;
366 }
367
368
369 ngx_sprintf((u_char *) ngx_reopen_event_name,
370 "Global\\ngx_reopen_%s%Z", ngx_unique);
371
372 ngx_reopen_event = CreateEvent(NULL, 1, 0, ngx_reopen_event_name);
373 if (ngx_reopen_event == NULL) {
374 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
375 "CreateEvent(\"%s\") failed", ngx_reopen_event_name);
376 return NGX_ERROR;
377 }
378
379
380 ngx_sprintf((u_char *) ngx_reload_event_name,
381 "Global\\ngx_reload_%s%Z", ngx_unique);
382
383 ngx_reload_event = CreateEvent(NULL, 1, 0, ngx_reload_event_name);
384 if (ngx_reload_event == NULL) {
385 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
386 "CreateEvent(\"%s\") failed", ngx_reload_event_name);
387 return NGX_ERROR;
388 }
389
390 return NGX_OK;
391}
392
393
394static ngx_int_t
395ngx_start_worker_processes(ngx_cycle_t *cycle, ngx_int_t type)
396{
397 ngx_int_t n;
398 ngx_core_conf_t *ccf;
399
400 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "start worker processes");
401
402 ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module);
403
404 for (n = 0; n < ccf->worker_processes; n++) {
405 if (ngx_spawn_process(cycle, "worker", type) == NGX_INVALID_PID) {
406 break;
407 }
408 }
409
410 return n;
411}
412
413
414static void
415ngx_reopen_worker_processes(ngx_cycle_t *cycle)
416{
417 ngx_int_t n;
418
419 for (n = 0; n < ngx_last_process; n++) {
420
421 if (ngx_processes[n].handle == NULL) {
422 continue;
423 }
424
425 if (SetEvent(ngx_processes[n].reopen) == 0) {
426 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
427 "SetEvent(\"%s\") failed",
428 ngx_processes[n].reopen_event);
429 }
430 }
431}
432
433
434static void
435ngx_quit_worker_processes(ngx_cycle_t *cycle, ngx_uint_t old)
436{
437 ngx_int_t n;
438
439 for (n = 0; n < ngx_last_process; n++) {
440
441 ngx_log_debug5(NGX_LOG_DEBUG_CORE, cycle->log, 0,
442 "process: %d %P %p e:%d j:%d",
443 n,
444 ngx_processes[n].pid,
445 ngx_processes[n].handle,
446 ngx_processes[n].exiting,
447 ngx_processes[n].just_spawn);
448
449 if (old && ngx_processes[n].just_spawn) {
450 ngx_processes[n].just_spawn = 0;
451 continue;
452 }
453
454 if (ngx_processes[n].handle == NULL) {
455 continue;
456 }
457
458 if (SetEvent(ngx_processes[n].quit) == 0) {
459 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
460 "SetEvent(\"%s\") failed",
461 ngx_processes[n].quit_event);
462 }
463
464 ngx_processes[n].exiting = 1;
465 }
466}
467
468
469static void
470ngx_terminate_worker_processes(ngx_cycle_t *cycle)
471{
472 ngx_int_t n;
473
474 for (n = 0; n < ngx_last_process; n++) {
475
476 if (ngx_processes[n].handle == NULL) {
477 continue;
478 }
479
480 if (TerminateProcess(ngx_processes[n].handle, 0) == 0) {
481 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
482 "TerminateProcess(\"%p\") failed",
483 ngx_processes[n].handle);
484 }
485
486 ngx_processes[n].exiting = 1;
487
488 ngx_close_handle(ngx_processes[n].reopen);
489 ngx_close_handle(ngx_processes[n].quit);
490 ngx_close_handle(ngx_processes[n].term);
491 ngx_close_handle(ngx_processes[n].handle);
492 }
493}
494
495
496static ngx_uint_t
497ngx_reap_worker(ngx_cycle_t *cycle, HANDLE h)
498{
499 u_long code;
500 ngx_int_t n;
501
502 for (n = 0; n < ngx_last_process; n++) {
503
504 if (ngx_processes[n].handle != h) {
505 continue;
506 }
507
508 if (GetExitCodeProcess(h, &code) == 0) {
509 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
510 "GetExitCodeProcess(%P) failed",
511 ngx_processes[n].pid);
512 }
513
514 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0,
515 "%s process %P exited with code %Xul",
516 ngx_processes[n].name, ngx_processes[n].pid, code);
517
518 ngx_close_handle(ngx_processes[n].reopen);
519 ngx_close_handle(ngx_processes[n].quit);
520 ngx_close_handle(ngx_processes[n].term);
521 ngx_close_handle(h);
522
523 ngx_processes[n].handle = NULL;
524 ngx_processes[n].term = NULL;
525 ngx_processes[n].quit = NULL;
526 ngx_processes[n].reopen = NULL;
527
528 if (!ngx_processes[n].exiting && !ngx_terminate && !ngx_quit) {
529
530 if (ngx_spawn_process(cycle, ngx_processes[n].name, n)
531 == NGX_INVALID_PID)
532 {
533 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
534 "could not respawn %s", ngx_processes[n].name);
535
536 if (n == ngx_last_process - 1) {
537 ngx_last_process--;
538 }
539 }
540 }
541
542 goto found;
543 }
544
545 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, "unknown process handle %p", h);
546
547found:
548
549 for (n = 0; n < ngx_last_process; n++) {
550
551 ngx_log_debug5(NGX_LOG_DEBUG_CORE, cycle->log, 0,
552 "process: %d %P %p e:%d j:%d",
553 n,
554 ngx_processes[n].pid,
555 ngx_processes[n].handle,
556 ngx_processes[n].exiting,
557 ngx_processes[n].just_spawn);
558
559 if (ngx_processes[n].handle) {
560 return 1;
561 }
562 }
563
564 return 0;
565}
566
567
568static void
569ngx_master_process_exit(ngx_cycle_t *cycle)
570{
571 ngx_uint_t i;
572
573 ngx_delete_pidfile(cycle);
574
575 ngx_close_handle(ngx_cache_manager_mutex);
576 ngx_close_handle(ngx_stop_event);
577 ngx_close_handle(ngx_quit_event);
578 ngx_close_handle(ngx_reopen_event);
579 ngx_close_handle(ngx_reload_event);
580 ngx_close_handle(ngx_master_process_event);
581
582 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");
583
584 for (i = 0; ngx_modules[i]; i++) {
585 if (ngx_modules[i]->exit_master) {
586 ngx_modules[i]->exit_master(cycle);
587 }
588 }
589
590 ngx_destroy_pool(cycle->pool);
591
592 exit(0);
593}
594
595
596static void
597ngx_worker_process_cycle(ngx_cycle_t *cycle, char *mevn)
598{
599 char wtevn[NGX_PROCESS_SYNC_NAME];
600 char wqevn[NGX_PROCESS_SYNC_NAME];
601 char wroevn[NGX_PROCESS_SYNC_NAME];
602 HANDLE mev, events[3];
603 u_long nev, ev;
604 ngx_err_t err;
605 ngx_tid_t wtid, cmtid, cltid;
606 ngx_log_t *log;
607
608 log = cycle->log;
609
610 ngx_log_debug0(NGX_LOG_DEBUG_CORE, log, 0, "worker started");
611
612 ngx_sprintf((u_char *) wtevn, "ngx_worker_term_%ul%Z", ngx_pid);
613 events[0] = CreateEvent(NULL, 1, 0, wtevn);
614 if (events[0] == NULL) {
615 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
616 "CreateEvent(\"%s\") failed", wtevn);
617 goto failed;
618 }
619
620 ngx_sprintf((u_char *) wqevn, "ngx_worker_quit_%ul%Z", ngx_pid);
621 events[1] = CreateEvent(NULL, 1, 0, wqevn);
622 if (events[1] == NULL) {
623 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
624 "CreateEvent(\"%s\") failed", wqevn);
625 goto failed;
626 }
627
628 ngx_sprintf((u_char *) wroevn, "ngx_worker_reopen_%ul%Z", ngx_pid);
629 events[2] = CreateEvent(NULL, 1, 0, wroevn);
630 if (events[2] == NULL) {
631 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
632 "CreateEvent(\"%s\") failed", wroevn);
633 goto failed;
634 }
635
636 mev = OpenEvent(EVENT_MODIFY_STATE, 0, mevn);
637 if (mev == NULL) {
638 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
639 "OpenEvent(\"%s\") failed", mevn);
640 goto failed;
641 }
642
643 if (SetEvent(mev) == 0) {
644 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
645 "SetEvent(\"%s\") failed", mevn);
646 goto failed;
647 }
648
649
650 ngx_sprintf((u_char *) ngx_cache_manager_mutex_name,
651 "ngx_cache_manager_mutex_%s%Z", ngx_unique);
652
653 ngx_cache_manager_mutex = OpenMutex(SYNCHRONIZE, 0,
654 ngx_cache_manager_mutex_name);
655 if (ngx_cache_manager_mutex == NULL) {
656 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
657 "OpenMutex(\"%s\") failed", ngx_cache_manager_mutex_name);
658 goto failed;
659 }
660
661 ngx_cache_manager_event = CreateEvent(NULL, 1, 0, NULL);
662 if (ngx_cache_manager_event == NULL) {
663 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
664 "CreateEvent(\"ngx_cache_manager_event\") failed");
665 goto failed;
666 }
667
668
669 if (ngx_create_thread(&wtid, ngx_worker_thread, NULL, log) != 0) {
670 goto failed;
671 }
672
673 if (ngx_create_thread(&cmtid, ngx_cache_manager_thread, NULL, log) != 0) {
674 goto failed;
675 }
676
677 if (ngx_create_thread(&cltid, ngx_cache_loader_thread, NULL, log) != 0) {
678 goto failed;
679 }
680
681 for ( ;; ) {
682 ev = WaitForMultipleObjects(3, events, 0, INFINITE);
683
684 err = ngx_errno;
685 ngx_time_update();
686
687 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
688 "worker WaitForMultipleObjects: %ul", ev);
689
690 if (ev == WAIT_OBJECT_0) {
691 ngx_terminate = 1;
692 ngx_log_error(NGX_LOG_NOTICE, log, 0, "exiting");
693
694 if (ResetEvent(events[0]) == 0) {
695 ngx_log_error(NGX_LOG_ALERT, log, 0,
696 "ResetEvent(\"%s\") failed", wtevn);
697 }
698
699 break;
700 }
701
702 if (ev == WAIT_OBJECT_0 + 1) {
703 ngx_quit = 1;
704 ngx_log_error(NGX_LOG_NOTICE, log, 0, "gracefully shutting down");
705 break;
706 }
707
708 if (ev == WAIT_OBJECT_0 + 2) {
709 ngx_reopen = 1;
710 ngx_log_error(NGX_LOG_NOTICE, log, 0, "reopening logs");
711
712 if (ResetEvent(events[2]) == 0) {
713 ngx_log_error(NGX_LOG_ALERT, log, 0,
714 "ResetEvent(\"%s\") failed", wroevn);
715 }
716
717 continue;
718 }
719
720 if (ev == WAIT_FAILED) {
721 ngx_log_error(NGX_LOG_ALERT, log, err,
722 "WaitForMultipleObjects() failed");
723
724 goto failed;
725 }
726 }
727
728 /* wait threads */
729
730 if (SetEvent(ngx_cache_manager_event) == 0) {
731 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
732 "SetEvent(\"ngx_cache_manager_event\") failed");
733 }
734
735 events[1] = wtid;
736 events[2] = cmtid;
737
738 nev = 3;
739
740 for ( ;; ) {
741 ev = WaitForMultipleObjects(nev, events, 0, INFINITE);
742
743 err = ngx_errno;
744 ngx_time_update();
745
746 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
747 "worker exit WaitForMultipleObjects: %ul", ev);
748
749 if (ev == WAIT_OBJECT_0) {
750 break;
751 }
752
753 if (ev == WAIT_OBJECT_0 + 1) {
754 if (nev == 2) {
755 break;
756 }
757
758 events[1] = events[2];
759 nev = 2;
760 continue;
761 }
762
763 if (ev == WAIT_OBJECT_0 + 2) {
764 nev = 2;
765 continue;
766 }
767
768 if (ev == WAIT_FAILED) {
769 ngx_log_error(NGX_LOG_ALERT, log, err,
770 "WaitForMultipleObjects() failed");
771 break;
772 }
773 }
774
775 ngx_close_handle(ngx_cache_manager_event);
776 ngx_close_handle(events[0]);
777 ngx_close_handle(events[1]);
778 ngx_close_handle(events[2]);
779 ngx_close_handle(mev);
780
781 ngx_worker_process_exit(cycle);
782
783failed:
784
785 exit(2);
786}
787
788
789static ngx_thread_value_t __stdcall
790ngx_worker_thread(void *data)
791{
792 ngx_int_t n;
793 ngx_uint_t i;
794 ngx_cycle_t *cycle;
795 ngx_connection_t *c;
796
797 cycle = (ngx_cycle_t *) ngx_cycle;
798
799 for (n = 0; ngx_modules[n]; n++) {
800 if (ngx_modules[n]->init_process) {
801 if (ngx_modules[n]->init_process(cycle) == NGX_ERROR) {
802 /* fatal */
803 exit(2);
804 }
805 }
806 }
807
808 while (!ngx_quit) {
809
810 if (ngx_exiting) {
811
812 c = cycle->connections;
813
814 for (i = 0; i < cycle->connection_n; i++) {
815
816 /* THREAD: lock */
817
818 if (c[i].fd != (ngx_socket_t) -1 && c[i].idle) {
819 c[i].close = 1;
820 c[i].read->handler(c[i].read);
821 }
822 }
823
824 if (ngx_event_timer_rbtree.root
825 == ngx_event_timer_rbtree.sentinel)
826 {
827 ngx_quit = 1;
828 break;
829 }
830 }
831
832 ngx_log_debug0(NGX_LOG_DEBUG_CORE, cycle->log, 0, "worker cycle");
833
834 ngx_process_events_and_timers(cycle);
835
836 if (ngx_terminate) {
837 return 0;
838 }
839
840 if (ngx_quit) {
841 ngx_quit = 0;
842
843 if (!ngx_exiting) {
844 ngx_close_listening_sockets(cycle);
845 ngx_exiting = 1;
846 }
847 }
848
849 if (ngx_reopen) {
850 ngx_reopen = 0;
851 ngx_reopen_files(cycle, -1);
852 }
853 }
854
855 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
856
857 return 0;
858}
859
860
861static void
862ngx_worker_process_exit(ngx_cycle_t *cycle)
863{
864 ngx_uint_t i;
865 ngx_connection_t *c;
866
867 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exit");
868
869 for (i = 0; ngx_modules[i]; i++) {
870 if (ngx_modules[i]->exit_process) {
871 ngx_modules[i]->exit_process(cycle);
872 }
873 }
874
875 if (ngx_exiting) {
876 c = cycle->connections;
877 for (i = 0; i < cycle->connection_n; i++) {
878 if (c[i].fd != (ngx_socket_t) -1
879 && c[i].read
880 && !c[i].read->accept
881 && !c[i].read->channel
882 && !c[i].read->resolver)
883 {
884 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
885 "open socket #%d left in connection %ui",
886 c[i].fd, i);
887 }
888 }
889 }
890
891 ngx_destroy_pool(cycle->pool);
892
893 exit(0);
894}
895
896
897static ngx_thread_value_t __stdcall
898ngx_cache_manager_thread(void *data)
899{
900 u_long ev;
901 HANDLE events[2];
902 ngx_err_t err;
903 ngx_cycle_t *cycle;
904
905 cycle = (ngx_cycle_t *) ngx_cycle;
906
907 events[0] = ngx_cache_manager_event;
908 events[1] = ngx_cache_manager_mutex;
909
910 for ( ;; ) {
911 ev = WaitForMultipleObjects(2, events, 0, INFINITE);
912
913 err = ngx_errno;
914 ngx_time_update();
915
916 ngx_log_debug1(NGX_LOG_DEBUG_CORE, cycle->log, 0,
917 "cache manager WaitForMultipleObjects: %ul", ev);
918
919 if (ev == WAIT_FAILED) {
920 ngx_log_error(NGX_LOG_ALERT, cycle->log, err,
921 "WaitForMultipleObjects() failed");
922 }
923
924 /*
925 * ev == WAIT_OBJECT_0
926 * ev == WAIT_OBJECT_0 + 1
927 * ev == WAIT_ABANDONED_0 + 1
928 */
929
930 if (ngx_terminate || ngx_quit) {
931 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
932 return 0;
933 }
934
935 break;
936 }
937
938 for ( ;; ) {
939
940 if (ngx_terminate || ngx_quit) {
941 ngx_log_error(NGX_LOG_NOTICE, cycle->log, 0, "exiting");
942 break;
943 }
944
945 ngx_cache_manager_process_handler();
946 }
947
948 if (ReleaseMutex(ngx_cache_manager_mutex) == 0) {
949 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
950 "ReleaseMutex() failed");
951 }
952
953 return 0;
954}
955
956
957static void
958ngx_cache_manager_process_handler(void)
959{
960 u_long ev;
961 time_t next, n;
962 ngx_uint_t i;
963 ngx_path_t **path;
964
965 next = 60 * 60;
966
967 path = ngx_cycle->paths.elts;
968 for (i = 0; i < ngx_cycle->paths.nelts; i++) {
969
970 if (path[i]->manager) {
971 n = path[i]->manager(path[i]->data);
972
973 next = (n <= next) ? n : next;
974
975 ngx_time_update();
976 }
977 }
978
979 if (next == 0) {
980 next = 1;
981 }
982
983 ev = WaitForSingleObject(ngx_cache_manager_event, (u_long) next * 1000);
984
985 if (ev != WAIT_TIMEOUT) {
986
987 ngx_time_update();
988
989 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
990 "cache manager WaitForSingleObject: %ul", ev);
991 }
992}
993
994
995static ngx_thread_value_t __stdcall
996ngx_cache_loader_thread(void *data)
997{
998 ngx_uint_t i;
999 ngx_path_t **path;
1000 ngx_cycle_t *cycle;
1001
1002 ngx_msleep(60000);
1003
1004 cycle = (ngx_cycle_t *) ngx_cycle;
1005
1006 path = cycle->paths.elts;
1007 for (i = 0; i < cycle->paths.nelts; i++) {
1008
1009 if (ngx_terminate || ngx_quit) {
1010 break;
1011 }
1012
1013 if (path[i]->loader) {
1014 path[i]->loader(path[i]->data);
1015 ngx_time_update();
1016 }
1017 }
1018
1019 return 0;
1020}
1021
1022
1023void
1024ngx_single_process_cycle(ngx_cycle_t *cycle)
1025{
1026 ngx_tid_t tid;
1027
1028 ngx_process_init(cycle);
1029
1030 ngx_console_init(cycle);
1031
1032 if (ngx_create_signal_events(cycle) != NGX_OK) {
1033 exit(2);
1034 }
1035
1036 if (ngx_create_thread(&tid, ngx_worker_thread, NULL, cycle->log) != 0) {
1037 /* fatal */
1038 exit(2);
1039 }
1040
1041 /* STUB */
1042 WaitForSingleObject(ngx_stop_event, INFINITE);
1043}
1044
1045
1046ngx_int_t
1047ngx_os_signal_process(ngx_cycle_t *cycle, char *sig, ngx_int_t pid)
1048{
1049 HANDLE ev;
1050 ngx_int_t rc;
1051 char evn[NGX_PROCESS_SYNC_NAME];
1052
1053 ngx_sprintf((u_char *) evn, "Global\\ngx_%s_%ul%Z", sig, pid);
1054
1055 ev = OpenEvent(EVENT_MODIFY_STATE, 0, evn);
1056 if (ev == NULL) {
1057 ngx_log_error(NGX_LOG_ERR, cycle->log, ngx_errno,
1058 "OpenEvent(\"%s\") failed", evn);
1059 return 1;
1060 }
1061
1062 if (SetEvent(ev) == 0) {
1063 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1064 "SetEvent(\"%s\") failed", evn);
1065 rc = 1;
1066
1067 } else {
1068 rc = 0;
1069 }
1070
1071 ngx_close_handle(ev);
1072
1073 return rc;
1074}
1075
1076
1077void
1078ngx_close_handle(HANDLE h)
1079{
1080 if (CloseHandle(h) == 0) {
1081 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
1082 "CloseHandle(%p) failed", h);
1083 }
1084}