comparison src/core/nginx.c @ 222:99df0edb63ed

nginx-0.0.1-2004-01-09-00:02:06 import
author Igor Sysoev <igor@sysoev.ru>
date Thu, 08 Jan 2004 21:02:06 +0000
parents 401154e21826
children 5d168ebc1ecc
comparison
equal deleted inserted replaced
221:401154e21826 222:99df0edb63ed
8 typedef struct { 8 typedef struct {
9 ngx_str_t user; 9 ngx_str_t user;
10 int daemon; 10 int daemon;
11 int master; 11 int master;
12 ngx_str_t pid; 12 ngx_str_t pid;
13 ngx_str_t newpid;
13 } ngx_core_conf_t; 14 } ngx_core_conf_t;
14 15
15 16
16 typedef struct { 17 typedef struct {
17 ngx_file_t pid; 18 ngx_file_t pid;
72 uid_t user; 73 uid_t user;
73 74
74 u_int ngx_connection_counter; 75 u_int ngx_connection_counter;
75 76
76 ngx_int_t ngx_process; 77 ngx_int_t ngx_process;
77 78 ngx_int_t ngx_inherited;
78
79 ngx_int_t ngx_reap; 79 ngx_int_t ngx_reap;
80 ngx_int_t ngx_terminate; 80 ngx_int_t ngx_terminate;
81 ngx_int_t ngx_quit; 81 ngx_int_t ngx_quit;
82 ngx_int_t ngx_pause;
82 ngx_int_t ngx_reconfigure; 83 ngx_int_t ngx_reconfigure;
83 ngx_int_t ngx_reopen; 84 ngx_int_t ngx_reopen;
84 ngx_int_t ngx_change_binary; 85 ngx_int_t ngx_change_binary;
85 86
86 87
189 } 190 }
190 191
191 if (ccf->pid.len == 0) { 192 if (ccf->pid.len == 0) {
192 ccf->pid.len = sizeof(NGINX_PID) - 1; 193 ccf->pid.len = sizeof(NGINX_PID) - 1;
193 ccf->pid.data = NGINX_PID; 194 ccf->pid.data = NGINX_PID;
195 ccf->newpid.len = sizeof(NGINX_NEW_PID) - 1;
196 ccf->newpid.data = NGINX_NEW_PID;
194 } 197 }
195 198
196 len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid()); 199 len = ngx_snprintf(pid, /* STUB */ 10, PID_T_FMT, ngx_getpid());
197 ngx_memzero(&ctx.pid, sizeof(ngx_file_t)); 200 ngx_memzero(&ctx.pid, sizeof(ngx_file_t));
198 ctx.pid.name = ccf->pid; 201 ctx.pid.name = ngx_inherited ? ccf->newpid : ccf->pid;
199 202
200 ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR, 203 ctx.pid.fd = ngx_open_file(ctx.pid.name.data, NGX_FILE_RDWR,
201 NGX_FILE_CREATE_OR_OPEN); 204 NGX_FILE_CREATE_OR_OPEN);
202 205
203 if (ctx.pid.fd == NGX_INVALID_FILE) { 206 if (ctx.pid.fd == NGX_INVALID_FILE) {
227 230
228 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx) 231 static void ngx_master_process_cycle(ngx_cycle_t *cycle, ngx_master_ctx_t *ctx)
229 { 232 {
230 ngx_msec_t delay; 233 ngx_msec_t delay;
231 struct timeval tv; 234 struct timeval tv;
232 ngx_int_t i; 235 ngx_uint_t i, live;
233 sigset_t set, wset; 236 sigset_t set, wset;
234 237
235 delay = 1000; 238 delay = 1000;
236 239
237 sigemptyset(&set); 240 sigemptyset(&set);
238 sigaddset(&set, SIGCHLD); 241 sigaddset(&set, SIGCHLD);
239 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL)); 242 sigaddset(&set, ngx_signal_value(NGX_RECONFIGURE_SIGNAL));
240 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL)); 243 sigaddset(&set, ngx_signal_value(NGX_REOPEN_SIGNAL));
241 sigaddset(&set, ngx_signal_value(NGX_INTERRUPT_SIGNAL)); 244 sigaddset(&set, ngx_signal_value(NGX_PAUSE_SIGNAL));
242 sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL)); 245 sigaddset(&set, ngx_signal_value(NGX_TERMINATE_SIGNAL));
243 sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); 246 sigaddset(&set, ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
244 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL)); 247 sigaddset(&set, ngx_signal_value(NGX_CHANGEBIN_SIGNAL));
245 248
246 sigemptyset(&wset); 249 sigemptyset(&wset);
288 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0, 291 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
289 "worker cycle"); 292 "worker cycle");
290 293
291 ngx_process_events(cycle->log); 294 ngx_process_events(cycle->log);
292 295
293 } else if (ngx_process == NGX_PROCESS_MASTER_QUIT) { 296 } else if (ngx_process == NGX_PROCESS_QUITING
297 || ngx_process == NGX_PROCESS_PAUSED)
298 {
299 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
300 "quit cycle");
301
294 if (delay < 10000) { 302 if (delay < 10000) {
295 delay *= 2; 303 delay *= 2;
296 } 304 }
297 305
298 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) { 306 if (sigprocmask(SIG_UNBLOCK, &set, NULL) == -1) {
299 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 307 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
300 "sigprocmask() failed"); 308 "sigprocmask() failed");
301 continue; 309 continue;
302 } 310 }
303 311
304 ngx_msleep(delay); 312 if (ngx_reap == 0) {
305 313 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
306 ngx_gettimeofday(&tv); 314 "sleep %d", delay / 1000);
307 ngx_time_update(tv.tv_sec); 315
316 ngx_msleep(delay);
317
318 ngx_gettimeofday(&tv);
319 ngx_time_update(tv.tv_sec);
320
321 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, cycle->log, 0,
322 "wake up");
323 }
308 324
309 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) { 325 if (sigprocmask(SIG_BLOCK, &set, NULL) == -1) {
310 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 326 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
311 "sigprocmask() failed"); 327 "sigprocmask() failed");
312 } 328 }
313 } 329
314 330 if (ngx_reap) {
315 if (ngx_quit || ngx_terminate) { 331 ngx_reap = 0;
316 #if !(WIN32) 332
317 if (ngx_delete_file(ctx->pid.name.data) == NGX_FILE_ERROR) { 333 live = 0;
318 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 334 for (i = 0; i < ngx_last_process; i++) {
319 ngx_delete_file_n " \"%s\" failed", 335 if (ngx_processes[i].exiting
320 ctx->pid.name.data); 336 && !ngx_processes[i].exited)
337 {
338 live = 1;
339 continue;
340 }
341
342 if (i != --ngx_last_process) {
343 ngx_processes[i--] =
344 ngx_processes[ngx_last_process];
345 }
346 }
347
348 if (live == 0 && ngx_process == NGX_PROCESS_QUITING) {
349 if (ngx_delete_file(ctx->pid.name.data)
350 == NGX_FILE_ERROR)
351 {
352 ngx_log_error(NGX_LOG_ALERT, cycle->log,
353 ngx_errno,
354 ngx_delete_file_n
355 " \"%s\" failed",
356 ctx->pid.name.data);
357 }
358
359 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");
360 exit(0);
361 }
321 } 362 }
322 #endif 363 }
323 364
324 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exiting"); 365 if (ngx_terminate) {
325 366 ngx_signal_processes(cycle,
326 if (ngx_process == NGX_PROCESS_MASTER) { 367 ngx_signal_value(NGX_TERMINATE_SIGNAL));
327 ngx_signal_processes(cycle, 368 ngx_process = NGX_PROCESS_QUITING;
369 }
370
371 if (ngx_quit) {
372 ngx_signal_processes(cycle,
328 ngx_signal_value(NGX_SHUTDOWN_SIGNAL)); 373 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
329 374 ngx_process = NGX_PROCESS_QUITING;
330 /* TODO: wait workers */ 375 }
331 376
332 ngx_msleep(1000); 377 if (ngx_pause || ngx_process != NGX_PROCESS_PAUSED) {
333 378 ngx_signal_processes(cycle,
334 ngx_gettimeofday(&tv); 379 ngx_signal_value(NGX_SHUTDOWN_SIGNAL));
335 ngx_time_update(tv.tv_sec); 380 ngx_process = NGX_PROCESS_PAUSED;
336 }
337
338 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "exit");
339 exit(0);
340 } 381 }
341 382
342 if (ngx_reap) { 383 if (ngx_reap) {
343 ngx_reap = 0; 384 ngx_reap = 0;
344 ngx_respawn_processes(cycle); 385 ngx_respawn_processes(cycle);
347 if (ngx_change_binary) { 388 if (ngx_change_binary) {
348 ngx_change_binary = 0; 389 ngx_change_binary = 0;
349 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, 390 ngx_log_error(NGX_LOG_INFO, cycle->log, 0,
350 "changing binary"); 391 "changing binary");
351 ngx_exec_new_binary(cycle, ctx->argv); 392 ngx_exec_new_binary(cycle, ctx->argv);
352
353 /* TODO: quit workers */
354 } 393 }
355 394
356 if (ngx_reconfigure) { 395 if (ngx_reconfigure) {
357 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring"); 396 ngx_log_error(NGX_LOG_INFO, cycle->log, 0, "reconfiguring");
358 break; 397 break;
365 ngx_reopen = 0; 404 ngx_reopen = 0;
366 } 405 }
367 406
368 } 407 }
369 408
370 cycle = ngx_init_cycle(cycle); 409 if (ngx_pause) {
371 if (cycle == NULL) { 410 ngx_pause = 0;
372 cycle = (ngx_cycle_t *) ngx_cycle; 411 ngx_process = NGX_PROCESS_MASTER;
373 continue; 412
413 } else {
414 cycle = ngx_init_cycle(cycle);
415 if (cycle == NULL) {
416 cycle = (ngx_cycle_t *) ngx_cycle;
417 continue;
418 }
419
420 ngx_cycle = cycle;
374 } 421 }
375 422
376 ngx_cycle = cycle;
377 ngx_reconfigure = 0; 423 ngx_reconfigure = 0;
378 break; 424 break;
379 } 425 }
380 } 426 }
381 } 427 }
386 sigset_t set; 432 sigset_t set;
387 ngx_int_t i; 433 ngx_int_t i;
388 ngx_listening_t *ls; 434 ngx_listening_t *ls;
389 435
390 ngx_process = NGX_PROCESS_WORKER; 436 ngx_process = NGX_PROCESS_WORKER;
437 ngx_last_process = 0;
391 438
392 if (user) { 439 if (user) {
393 if (setuid(user) == -1) { 440 if (setuid(user) == -1) {
394 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno, 441 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
395 "setuid() failed"); 442 "setuid() failed");
500 547
501 ls->fd = s; 548 ls->fd = s;
502 } 549 }
503 } 550 }
504 551
552 ngx_inherited = 1;
553
505 return ngx_set_inherited_sockets(cycle); 554 return ngx_set_inherited_sockets(cycle);
506 } 555 }
507 556
508 return NGX_OK; 557 return NGX_OK;
509 } 558 }