2003-11-20 00:26:41 +08:00
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2004-04-05 04:32:09 +08:00
|
|
|
#include <ngx_event.h>
|
2003-11-20 00:26:41 +08:00
|
|
|
|
|
|
|
|
2004-03-05 00:34:23 +08:00
|
|
|
static void ngx_execute_proc(ngx_cycle_t *cycle, void *data);
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-06-19 00:22:16 +08:00
|
|
|
ngx_int_t ngx_process_slot;
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_socket_t ngx_channel;
|
2004-06-19 00:22:16 +08:00
|
|
|
ngx_int_t ngx_last_process;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_process_t ngx_processes[NGX_MAX_PROCESSES];
|
2003-11-20 00:26:41 +08:00
|
|
|
|
|
|
|
|
2004-01-14 00:43:23 +08:00
|
|
|
ngx_pid_t ngx_spawn_process(ngx_cycle_t *cycle,
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_spawn_proc_pt proc, void *data,
|
|
|
|
char *name, ngx_int_t respawn)
|
2003-11-20 00:26:41 +08:00
|
|
|
{
|
2004-06-15 15:55:11 +08:00
|
|
|
u_long on;
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_pid_t pid;
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_int_t s;
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2004-06-15 15:55:11 +08:00
|
|
|
s = respawn >= 0 ? respawn : ngx_last_process;
|
|
|
|
|
|
|
|
|
2004-06-23 13:54:27 +08:00
|
|
|
if (respawn != NGX_PROCESS_DETACHED) {
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2004-06-23 13:54:27 +08:00
|
|
|
/* Solaris 9 still has no AF_LOCAL */
|
2004-01-06 04:55:48 +08:00
|
|
|
|
2004-06-23 13:54:27 +08:00
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, ngx_processes[s].channel) == -1)
|
|
|
|
{
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"socketpair() failed while spawning \"%s\"", name);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-06-19 00:22:16 +08:00
|
|
|
|
2004-06-23 13:54:27 +08:00
|
|
|
if (ngx_nonblocking(ngx_processes[s].channel[0]) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
ngx_nonblocking_n " failed while spawning \"%s\"",
|
|
|
|
name);
|
|
|
|
ngx_close_channel(ngx_processes[s].channel, cycle->log);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-06-19 00:22:16 +08:00
|
|
|
|
2004-06-23 13:54:27 +08:00
|
|
|
if (ngx_nonblocking(ngx_processes[s].channel[1]) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
ngx_nonblocking_n " failed while spawning \"%s\"",
|
|
|
|
name);
|
|
|
|
ngx_close_channel(ngx_processes[s].channel, cycle->log);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2004-06-15 15:55:11 +08:00
|
|
|
|
2004-06-23 13:54:27 +08:00
|
|
|
on = 1;
|
|
|
|
if (ioctl(ngx_processes[s].channel[0], FIOASYNC, &on) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"ioctl(FIOASYNC) failed while spawning \"%s\"", name);
|
|
|
|
ngx_close_channel(ngx_processes[s].channel, cycle->log);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fcntl(ngx_processes[s].channel[0], F_SETOWN, ngx_pid) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"fcntl(F_SETOWN) failed while spawning \"%s\"", name);
|
|
|
|
ngx_close_channel(ngx_processes[s].channel, cycle->log);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_channel = ngx_processes[s].channel[1];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_processes[s].channel[0] = -1;
|
|
|
|
ngx_processes[s].channel[1] = -1;
|
2004-06-15 15:55:11 +08:00
|
|
|
}
|
|
|
|
|
2004-06-19 00:22:16 +08:00
|
|
|
ngx_process_slot = s;
|
2004-06-15 15:55:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
|
2003-11-20 00:26:41 +08:00
|
|
|
switch (pid) {
|
2004-06-15 15:55:11 +08:00
|
|
|
|
2003-11-20 00:26:41 +08:00
|
|
|
case -1:
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"fork() failed while spawning \"%s\"", name);
|
2004-06-23 13:54:27 +08:00
|
|
|
ngx_close_channel(ngx_processes[s].channel, cycle->log);
|
2003-11-20 00:26:41 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
case 0:
|
2004-02-04 00:43:54 +08:00
|
|
|
ngx_pid = ngx_getpid();
|
2004-01-06 04:55:48 +08:00
|
|
|
proc(cycle, data);
|
2003-11-20 00:26:41 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2003-11-26 04:44:56 +08:00
|
|
|
break;
|
2003-11-20 00:26:41 +08:00
|
|
|
}
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_CORE, cycle->log, 0,
|
|
|
|
"spawn %s: " PID_T_FMT, name, pid);
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_processes[s].pid = pid;
|
|
|
|
ngx_processes[s].exited = 0;
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
if (respawn >= 0) {
|
2004-01-14 00:43:23 +08:00
|
|
|
return pid;
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_processes[s].proc = proc;
|
|
|
|
ngx_processes[s].data = data;
|
|
|
|
ngx_processes[s].name = name;
|
|
|
|
ngx_processes[s].exiting = 0;
|
2004-04-16 13:14:16 +08:00
|
|
|
|
|
|
|
switch (respawn) {
|
|
|
|
|
|
|
|
case NGX_PROCESS_RESPAWN:
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_processes[s].respawn = 1;
|
|
|
|
ngx_processes[s].just_respawn = 0;
|
|
|
|
ngx_processes[s].detached = 0;
|
2004-04-16 13:14:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NGX_PROCESS_JUST_RESPAWN:
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_processes[s].respawn = 1;
|
|
|
|
ngx_processes[s].just_respawn = 1;
|
|
|
|
ngx_processes[s].detached = 0;
|
2004-04-16 13:14:16 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NGX_PROCESS_DETACHED:
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_processes[s].respawn = 0;
|
|
|
|
ngx_processes[s].just_respawn = 0;
|
|
|
|
ngx_processes[s].detached = 1;
|
2004-04-16 13:14:16 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
ngx_last_process++;
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2004-01-14 00:43:23 +08:00
|
|
|
return pid;
|
2003-11-20 00:26:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-05 00:34:23 +08:00
|
|
|
ngx_pid_t ngx_execute(ngx_cycle_t *cycle, ngx_exec_ctx_t *ctx)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
2004-03-05 00:34:23 +08:00
|
|
|
return ngx_spawn_process(cycle, ngx_execute_proc, ctx, ctx->name,
|
2004-01-14 05:33:59 +08:00
|
|
|
NGX_PROCESS_DETACHED);
|
2004-01-06 04:55:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-05 00:34:23 +08:00
|
|
|
static void ngx_execute_proc(ngx_cycle_t *cycle, void *data)
|
2004-01-06 04:55:48 +08:00
|
|
|
{
|
|
|
|
ngx_exec_ctx_t *ctx = data;
|
|
|
|
|
|
|
|
if (execve(ctx->path, ctx->argv, ctx->envp) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
|
|
|
|
"execve() failed while executing %s \"%s\"",
|
|
|
|
ctx->name, ctx->path);
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ngx_process_get_status()
|
|
|
|
{
|
|
|
|
int status;
|
|
|
|
char *process;
|
|
|
|
ngx_pid_t pid;
|
|
|
|
ngx_err_t err;
|
2004-06-15 15:55:11 +08:00
|
|
|
ngx_int_t i;
|
|
|
|
ngx_uint_t one;
|
2004-01-06 04:55:48 +08:00
|
|
|
struct timeval tv;
|
2003-11-20 00:26:41 +08:00
|
|
|
one = 0;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
pid = waitpid(-1, &status, WNOHANG);
|
|
|
|
|
|
|
|
if (pid == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
err = ngx_errno;
|
|
|
|
|
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err == NGX_ECHILD && one) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, errno,
|
|
|
|
"waitpid() failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2004-04-05 04:32:09 +08:00
|
|
|
|
|
|
|
if (ngx_accept_mutex_ptr) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* unlock the accept mutex if the abnormally exited process
|
|
|
|
* held it
|
|
|
|
*/
|
|
|
|
|
|
|
|
ngx_atomic_cmp_set(ngx_accept_mutex_ptr, pid, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-20 00:26:41 +08:00
|
|
|
one = 1;
|
2004-01-06 04:55:48 +08:00
|
|
|
process = "";
|
2003-11-20 00:26:41 +08:00
|
|
|
|
2004-01-06 04:55:48 +08:00
|
|
|
for (i = 0; i < ngx_last_process; i++) {
|
|
|
|
if (ngx_processes[i].pid == pid) {
|
|
|
|
ngx_processes[i].status = status;
|
2004-01-09 05:02:06 +08:00
|
|
|
ngx_processes[i].exited = 1;
|
2004-01-06 04:55:48 +08:00
|
|
|
process = ngx_processes[i].name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == ngx_last_process) {
|
|
|
|
process = "unknown process";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WTERMSIG(status)) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
|
|
|
|
"%s " PID_T_FMT " exited on signal %d%s",
|
|
|
|
process, pid, WTERMSIG(status),
|
|
|
|
WCOREDUMP(status) ? " (core dumped)" : "");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_log_error(NGX_LOG_INFO, ngx_cycle->log, 0,
|
|
|
|
"%s " PID_T_FMT " exited with code %d",
|
|
|
|
process, pid, WEXITSTATUS(status));
|
|
|
|
}
|
2004-01-21 04:40:08 +08:00
|
|
|
|
|
|
|
if (WEXITSTATUS(status) == 2 && ngx_processes[i].respawn) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
|
|
|
|
"%s " PID_T_FMT
|
|
|
|
" exited with fatal code %d and could not respawn",
|
|
|
|
process, pid, WEXITSTATUS(status));
|
|
|
|
ngx_processes[i].respawn = 0;
|
|
|
|
}
|
2003-11-20 00:26:41 +08:00
|
|
|
}
|
|
|
|
}
|
2004-06-23 13:54:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
void ngx_close_channel(ngx_fd_t *fd, ngx_log_t *log)
|
|
|
|
{
|
|
|
|
if (close(fd[0]) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close() failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (close(fd[1]) == -1) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, "close() failed");
|
|
|
|
}
|
|
|
|
}
|