comparison src/os/unix/ngx_posix_cycle.c @ 228:8aa1bc7d06ba

nginx-0.0.1-2004-01-15-11:12:45 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 15 Jan 2004 08:12:45 +0000
parents
children
comparison
equal deleted inserted replaced
227:2ba3477070ac 228:8aa1bc7d06ba
1
2
3 void ngx_posix_master_cycle(ngx_cycle_t *cycle)
4 {
5 static ngx_int_t sent;
6 static ngx_msec_t delay = 125;
7
8 if (ngx_process == NGX_PROCESS_MASTER) {
9 if (sent) {
10 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
11 "sent signal cycle");
12
13 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
14 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
15 "sigprocmask() failed");
16 continue;
17 }
18
19 /*
20 * there is very big chance that the pending signals
21 * would be delivered right on the sigprocmask() return
22 */
23
24 if (!ngx_signal) {
25
26 if (delay < 15000) {
27 delay *= 2;
28 }
29
30 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
31 "msleep %d", delay);
32
33 ngx_msleep(delay);
34
35 ngx_gettimeofday(&tv);
36 ngx_time_update(tv.tv_sec);
37
38 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
39 "wake up");
40 }
41
42 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
43 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
44 "sigprocmask() failed");
45 }
46
47 ngx_signal = 0;
48
49 } else {
50 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
51 "sigsuspend");
52
53 sigsuspend(&wset);
54
55 ngx_gettimeofday(&tv);
56 ngx_time_update(tv.tv_sec);
57
58 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
59 "wake up");
60 }
61
62 } else { /* NGX_PROCESS_SINGLE */
63 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
64 "worker cycle");
65
66 ngx_process_events(cycle->log);
67 }
68
69 if (ngx_reap) {
70 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
71 "reap childs");
72
73 live = 0;
74 for (i = 0; i < ngx_last_process; i++) {
75
76 ngx_log_debug6(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
77 "child: " PID_T_FMT
78 " s:%d e:%d t:%d d:%d r:%d",
79 ngx_processes[i].pid,
80 ngx_processes[i].signal,
81 ngx_processes[i].exiting,
82 ngx_processes[i].exited,
83 ngx_processes[i].detached,
84 ngx_processes[i].respawn);
85
86 if (ngx_processes[i].exited) {
87
88 if (ngx_processes[i].respawn
89 && !ngx_processes[i].exiting
90 && !ngx_terminate
91 && !ngx_quit)
92 {
93 if (ngx_spawn_process(cycle,
94 ngx_processes[i].proc,
95 ngx_processes[i].data,
96 ngx_processes[i].name, i)
97 == NGX_ERROR)
98 {
99 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
100 "can not respawn %s",
101 ngx_processes[i].name);
102 }
103
104 continue;
105 }
106
107 if (ngx_processes[i].pid == ngx_new_binary) {
108 ngx_new_binary = 0;
109 }
110
111 if (i != --ngx_last_process) {
112 ngx_processes[i--] =
113 ngx_processes[ngx_last_process];
114 }
115
116 } else if (!ngx_processes[i].detached
117 && (ngx_terminate || ngx_quit))
118 {
119 live = 1;
120
121 } else if (ngx_processes[i].exiting) {
122 live = 1;
123 }
124 }
125
126 if (!live) {
127 if (ngx_terminate || ngx_quit) {
128
129 if (ngx_inherited && getppid() > 1) {
130 name = ctx->pid.name.data;
131
132 } else {
133 name = ctx->name;
134 }
135
136 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
137 ngx_log_error(NGX_LOG_ALERT, cycle->log,
138 ngx_errno,
139 ngx_delete_file_n
140 " \"%s\" failed", name);
141 }
142
143 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");
144 exit(0);
145
146 } else {
147 sent = 0;
148 }
149 }
150 }
151
152 if (ngx_terminate) {
153 if (delay > 10000) {
154 signo = SIGKILL;
155 } else {
156 signo = ngx_signal_value(NGX_TERMINATE_SIGNAL);
157 }
158
159 } else if (ngx_quit) {
160 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
161
162 } else {
163
164 if (ngx_noaccept) {
165 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
166 }
167
168 if (ngx_change_binary) {
169 ngx_change_binary = 0;
170 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "changing binary");
171 ngx_new_binary = ngx_exec_new_binary(cycle, ctx->argv);
172 }
173
174 if (ngx_reconfigure) {
175 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
176 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring");
177 }
178
179 if (ngx_reopen) {
180 /* STUB */
181 signo = ngx_signal_value(NGX_SHUTDOWN_SIGNAL);
182
183 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reopening logs");
184 ngx_reopen_files(cycle);
185 }
186 }
187
188 if (signo) {
189 for (i = 0; i < ngx_last_process; i++) {
190
191 if (!ngx_processes[i].detached) {
192 ngx_processes[i].signal = signo;
193
194 ngx_log_debug2(NGX_LOG_DEBUG_EVENT,
195 cycle->log, 0,
196 "signal " PID_T_FMT " %d",
197 ngx_processes[i].pid, signo);
198 }
199 }
200
201 delay = 125;
202 signo = 0;
203 }
204
205 for (i = 0; i < ngx_last_process; i++) {
206
207 if (ngx_processes[i].signal == 0) {
208 continue;
209 }
210
211 if (ccf->kqueue_signal != 1) {
212 sent = 1;
213 }
214
215 ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
216 "kill (" PID_T_FMT ", %d)" ,
217 ngx_processes[i].pid,
218 ngx_processes[i].signal);
219
220 if (kill(ngx_processes[i].pid, ngx_processes[i].signal) == -1) {
221 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
222 "kill(%d, %d) failed",
223 ngx_processes[i].pid, ngx_processes[i].signal);
224 continue;
225 }
226
227 if (ngx_processes[i].signal != ngx_signal_value(NGX_REOPEN_SIGNAL)) {
228 ngx_processes[i].exiting = 1;
229 }
230 }
231 }