diff options
author | Igor Sysoev <igor@sysoev.ru> | 2017-03-09 18:03:27 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2017-03-09 18:03:27 +0300 |
commit | 6f2c9acd1841ca20a1388b34aef64e9f00459090 (patch) | |
tree | c0b9c1063ec464027d1ca29a793f6c0b7a6878d5 /src/nxt_process.c | |
parent | 5745e4826427155e29c1d520fe77811a0f570689 (diff) | |
download | unit-6f2c9acd1841ca20a1388b34aef64e9f00459090.tar.gz unit-6f2c9acd1841ca20a1388b34aef64e9f00459090.tar.bz2 |
Processes refactoring.
The cycle has been renamed to the runtime.
Diffstat (limited to 'src/nxt_process.c')
-rw-r--r-- | src/nxt_process.c | 182 |
1 files changed, 127 insertions, 55 deletions
diff --git a/src/nxt_process.c b/src/nxt_process.c index b79e4388..d24be580 100644 --- a/src/nxt_process.c +++ b/src/nxt_process.c @@ -5,9 +5,11 @@ */ #include <nxt_main.h> +#include <nxt_master_process.h> -static nxt_int_t nxt_user_groups_get(nxt_user_cred_t *uc); +static void nxt_process_start(nxt_task_t *task, nxt_process_init_t *process); +static nxt_int_t nxt_user_groups_get(nxt_task_t *task, nxt_user_cred_t *uc); /* A cached process pid. */ @@ -18,20 +20,17 @@ nxt_pid_t nxt_ppid; nxt_pid_t -nxt_process_create(nxt_process_start_t start, void *data, const char *name) +nxt_process_create(nxt_task_t *task, nxt_process_init_t *process) { - nxt_pid_t pid; - nxt_thread_t *thr; - - thr = nxt_thread(); + nxt_pid_t pid; pid = fork(); switch (pid) { case -1: - nxt_log_alert(thr->log, "fork() failed while creating \"%s\" %E", - name, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "fork() failed while creating \"%s\" %E", + process->name, nxt_errno); break; case 0: @@ -39,14 +38,14 @@ nxt_process_create(nxt_process_start_t start, void *data, const char *name) nxt_pid = getpid(); /* Clean inherited cached thread tid. */ - thr->tid = 0; + task->thread->tid = 0; - start(data); + nxt_process_start(task, process); break; default: /* A parent. */ - nxt_log_debug(thr->log, "fork(): %PI", pid); + nxt_debug(task, "fork(\"%s\"): %PI", process->name, pid); break; } @@ -54,6 +53,73 @@ nxt_process_create(nxt_process_start_t start, void *data, const char *name) } +static void +nxt_process_start(nxt_task_t *task, nxt_process_init_t *process) +{ + nxt_int_t ret; + nxt_thread_t *thread; + nxt_runtime_t *rt; + nxt_event_engine_t *engine; + const nxt_event_interface_t *interface; + + nxt_log(task, NXT_LOG_INFO, "%s started", process->name); + + nxt_process_title(task, "nginext: %s", process->name); + + nxt_random_init(&nxt_random_data); + + if (process->user_cred != NULL && getuid() == 0) { + /* Super-user. */ + + ret = nxt_user_cred_set(task, process->user_cred); + if (ret != NXT_OK) { + goto fail; + } + } + + thread = task->thread; + rt = thread->runtime; + + rt->type = process->type; + + engine = thread->engine; + + /* Update inherited master process event engine and signals processing. */ + engine->signals->sigev = process->signals; + + interface = nxt_service_get(rt->services, "engine", rt->engine); + if (interface == NULL) { + goto fail; + } + + if (nxt_event_engine_change(engine, interface, rt->batch) != NXT_OK) { + goto fail; + } + + nxt_port_read_close(process->master_port); + nxt_port_write_enable(task, process->master_port); + + /* A worker process port. */ + nxt_port_create(thread, process->port, process->port_handlers); + + ret = nxt_runtime_thread_pool_create(thread, rt, rt->auxiliary_threads, + 60000 * 1000000LL); + if (ret != NXT_OK) { + goto fail; + } + + ret = process->start(task, rt); + + if (ret == NXT_OK) { + return; + } + +fail: + + exit(1); +} + + #if (NXT_HAVE_POSIX_SPAWN) /* @@ -76,14 +142,15 @@ nxt_process_create(nxt_process_start_t start, void *data, const char *name) */ nxt_pid_t -nxt_process_execute(char *name, char **argv, char **envp) +nxt_process_execute(nxt_task_t *task, char *name, char **argv, char **envp) { nxt_pid_t pid; - nxt_thread_log_debug("posix_spawn(\"%s\")", name); + nxt_debug(task, "posix_spawn(\"%s\")", name); if (posix_spawn(&pid, name, NULL, NULL, argv, envp) != 0) { - nxt_thread_log_alert("posix_spawn(\"%s\") failed %E", name, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "posix_spawn(\"%s\") failed %E", + name, nxt_errno); return -1; } @@ -93,7 +160,7 @@ nxt_process_execute(char *name, char **argv, char **envp) #else nxt_pid_t -nxt_process_execute(char *name, char **argv, char **envp) +nxt_process_execute(nxt_task_t *task, char *name, char **argv, char **envp) { nxt_pid_t pid; @@ -109,24 +176,26 @@ nxt_process_execute(char *name, char **argv, char **envp) switch (pid) { case -1: - nxt_thread_log_alert("vfork() failed while executing \"%s\" %E", - name, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "vfork() failed while executing \"%s\" %E", + name, nxt_errno); break; case 0: /* A child. */ - nxt_thread_log_debug("execve(\"%s\")", name); + nxt_debug(task, "execve(\"%s\")", name); (void) execve(name, argv, envp); - nxt_thread_log_alert("execve(\"%s\") failed %E", name, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "execve(\"%s\") failed %E", + name, nxt_errno); exit(1); + nxt_unreachable(); break; default: /* A parent. */ - nxt_thread_log_debug("vfork(): %PI", pid); + nxt_debug(task, "vfork(): %PI", pid); break; } @@ -137,14 +206,11 @@ nxt_process_execute(char *name, char **argv, char **envp) nxt_int_t -nxt_process_daemon(void) +nxt_process_daemon(nxt_task_t *task) { nxt_fd_t fd; nxt_pid_t pid; const char *msg; - nxt_thread_t *thr; - - thr = nxt_thread(); /* * fork() followed by a parent process's exit() detaches a child process @@ -166,7 +232,7 @@ nxt_process_daemon(void) default: /* A parent. */ - nxt_log_debug(thr->log, "fork(): %PI", pid); + nxt_debug(task, "fork(): %PI", pid); exit(0); nxt_unreachable(); } @@ -174,14 +240,14 @@ nxt_process_daemon(void) nxt_pid = getpid(); /* Clean inherited cached thread tid. */ - thr->tid = 0; + task->thread->tid = 0; - nxt_log_debug(thr->log, "daemon"); + nxt_debug(task, "daemon"); /* Detach from controlling terminal. */ if (setsid() == -1) { - nxt_log_emerg(thr->log, "setsid() failed %E", nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "setsid() failed %E", nxt_errno); return NXT_ERROR; } @@ -217,7 +283,7 @@ nxt_process_daemon(void) fail: - nxt_log_emerg(thr->log, msg, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, msg, nxt_errno); return NXT_ERROR; } @@ -236,7 +302,7 @@ nxt_nanosleep(nxt_nsec_t ns) nxt_int_t -nxt_user_cred_get(nxt_user_cred_t *uc, const char *group) +nxt_user_cred_get(nxt_task_t *task, nxt_user_cred_t *uc, const char *group) { struct group *grp; struct passwd *pwd; @@ -244,7 +310,8 @@ nxt_user_cred_get(nxt_user_cred_t *uc, const char *group) pwd = getpwnam(uc->user); if (nxt_slow_path(pwd == NULL)) { - nxt_thread_log_emerg("getpwnam(%s) failed %E", uc->user, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "getpwnam(%s) failed %E", + uc->user, nxt_errno); return NXT_ERROR; } @@ -255,7 +322,8 @@ nxt_user_cred_get(nxt_user_cred_t *uc, const char *group) grp = getgrnam(group); if (nxt_slow_path(grp == NULL)) { - nxt_thread_log_emerg("getgrnam(%s) failed %E", group, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "getgrnam(%s) failed %E", + group, nxt_errno); return NXT_ERROR; } @@ -263,7 +331,7 @@ nxt_user_cred_get(nxt_user_cred_t *uc, const char *group) } if (getuid() == 0) { - return nxt_user_groups_get(uc); + return nxt_user_groups_get(task, uc); } return NXT_OK; @@ -297,7 +365,7 @@ nxt_user_cred_get(nxt_user_cred_t *uc, const char *group) */ static nxt_int_t -nxt_user_groups_get(nxt_user_cred_t *uc) +nxt_user_groups_get(nxt_task_t *task, nxt_user_cred_t *uc) { int nsaved, ngroups; nxt_int_t ret; @@ -306,11 +374,11 @@ nxt_user_groups_get(nxt_user_cred_t *uc) nsaved = getgroups(0, NULL); if (nsaved == -1) { - nxt_thread_log_emerg("getgroups(0, NULL) failed %E", nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "getgroups(0, NULL) failed %E", nxt_errno); return NXT_ERROR; } - nxt_thread_log_debug("getgroups(0, NULL): %d", nsaved); + nxt_debug(task, "getgroups(0, NULL): %d", nsaved); if (nsaved > NGROUPS_MAX) { /* MacOSX case. */ @@ -328,14 +396,15 @@ nxt_user_groups_get(nxt_user_cred_t *uc) nsaved = getgroups(nsaved, saved); if (nsaved == -1) { - nxt_thread_log_emerg("getgroups(%d) failed %E", nsaved, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "getgroups(%d) failed %E", + nsaved, nxt_errno); goto fail; } - nxt_thread_log_debug("getgroups(): %d", nsaved); + nxt_debug(task, "getgroups(): %d", nsaved); if (initgroups(uc->user, uc->base_gid) != 0) { - nxt_thread_log_emerg("initgroups(%s, %d) failed", + nxt_log(task, NXT_LOG_CRIT, "initgroups(%s, %d) failed", uc->user, uc->base_gid); goto restore; } @@ -343,11 +412,11 @@ nxt_user_groups_get(nxt_user_cred_t *uc) ngroups = getgroups(0, NULL); if (ngroups == -1) { - nxt_thread_log_emerg("getgroups(0, NULL) failed %E", nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "getgroups(0, NULL) failed %E", nxt_errno); goto restore; } - nxt_thread_log_debug("getgroups(0, NULL): %d", ngroups); + nxt_debug(task, "getgroups(0, NULL): %d", ngroups); uc->gids = nxt_malloc(ngroups * sizeof(nxt_gid_t)); @@ -358,7 +427,8 @@ nxt_user_groups_get(nxt_user_cred_t *uc) ngroups = getgroups(ngroups, uc->gids); if (ngroups == -1) { - nxt_thread_log_emerg("getgroups(%d) failed %E", ngroups, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "getgroups(%d) failed %E", + ngroups, nxt_errno); goto restore; } @@ -377,9 +447,9 @@ nxt_user_groups_get(nxt_user_cred_t *uc) p = nxt_sprintf(p, end, "%uL:", (uint64_t) uc->gids[i]); } - nxt_thread_log_debug("user \"%s\" cred: uid:%uL base gid:%uL, gids:%*s", - uc->user, (uint64_t) uc->uid, - (uint64_t) uc->base_gid, p - msg, msg); + nxt_debug(task, "user \"%s\" cred: uid:%uL base gid:%uL, gids:%*s", + uc->user, (uint64_t) uc->uid, (uint64_t) uc->base_gid, + p - msg, msg); } #endif @@ -388,7 +458,8 @@ nxt_user_groups_get(nxt_user_cred_t *uc) restore: if (setgroups(nsaved, saved) != 0) { - nxt_thread_log_emerg("setgroups(%d) failed %E", nsaved, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "setgroups(%d) failed %E", + nsaved, nxt_errno); ret = NXT_ERROR; } @@ -401,34 +472,35 @@ fail: nxt_int_t -nxt_user_cred_set(nxt_user_cred_t *uc) +nxt_user_cred_set(nxt_task_t *task, nxt_user_cred_t *uc) { - nxt_thread_log_debug("user cred set: \"%s\" uid:%uL base gid:%uL", - uc->user, (uint64_t) uc->uid, uc->base_gid); + nxt_debug(task, "user cred set: \"%s\" uid:%uL base gid:%uL", + uc->user, (uint64_t) uc->uid, uc->base_gid); if (setgid(uc->base_gid) != 0) { - nxt_thread_log_emerg("setgid(%d) failed %E", uc->base_gid, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "setgid(%d) failed %E", + uc->base_gid, nxt_errno); return NXT_ERROR; } if (uc->gids != NULL) { if (setgroups(uc->ngroups, uc->gids) != 0) { - nxt_thread_log_emerg("setgroups(%i) failed %E", - uc->ngroups, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "setgroups(%i) failed %E", + uc->ngroups, nxt_errno); return NXT_ERROR; } } else { /* MacOSX fallback. */ if (initgroups(uc->user, uc->base_gid) != 0) { - nxt_thread_log_emerg("initgroups(%s, %d) failed", - uc->user, uc->base_gid); + nxt_log(task, NXT_LOG_CRIT, "initgroups(%s, %d) failed", + uc->user, uc->base_gid); return NXT_ERROR; } } if (setuid(uc->uid) != 0) { - nxt_thread_log_emerg("setuid(%d) failed %E", uc->uid, nxt_errno); + nxt_log(task, NXT_LOG_CRIT, "setuid(%d) failed %E", uc->uid, nxt_errno); return NXT_ERROR; } |