X-Git-Url: http://git.lttng.org./?a=blobdiff_plain;ds=sidebyside;f=src%2Fcommon%2Frunas.c;h=57f7382bd2b6b6f8dd5f07adf4a7dfe90584f58a;hb=de3fb857f034c208c135a10a3cdec2dfe43fbda6;hp=3a1de0a3acdd2b3613d715d48f404c3a60eb9eeb;hpb=9f21bc2c96ab5615e7410539b114484382d46b83;p=lttng-tools.git diff --git a/src/common/runas.c b/src/common/runas.c index 3a1de0a3a..57f7382bd 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include @@ -322,12 +323,11 @@ int run_as_worker(struct run_as_worker *worker) memset(worker->procname, 0, proc_orig_len); strncpy(worker->procname, DEFAULT_RUN_AS_WORKER_NAME, proc_orig_len); - ret = pthread_setname_np(pthread_self(), DEFAULT_RUN_AS_WORKER_NAME); + ret = prctl(PR_SET_NAME, DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0); if (ret) { - errno = ret; - ret = -1; - PERROR("pthread_setname_np"); - return EXIT_FAILURE; + /* Don't fail as this is not essential. */ + PERROR("prctl PR_SET_NAME"); + ret = 0; } sendret.ret = 0; @@ -527,6 +527,81 @@ int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid) return run_as(RUN_AS_RMDIR_RECURSIVE, &data, uid, gid); } +static +int reset_sighandler(void) +{ + int sig, ret = 0; + + for (sig = SIGHUP; sig <= SIGUNUSED; sig++) { + /* Skip unblockable signals. */ + if (sig == SIGKILL || sig == SIGSTOP) { + continue; + } + if (signal(sig, SIG_DFL) == SIG_ERR) { + PERROR("reset signal %d", sig); + ret = -1; + goto end; + } + } +end: + return ret; +} + +static +void worker_sighandler(int sig) +{ + const char *signame; + + /* + * The worker will its parent's signals since they are part of the same + * process group. However, in the case of SIGINT and SIGTERM, we want + * to give the worker a chance to teardown gracefully when its parent + * closes the command socket. + */ + switch (sig) { + case SIGINT: + signame = "SIGINT"; + break; + case SIGTERM: + signame = "SIGTERM"; + break; + default: + signame = "Unknown"; + } + + DBG("run_as worker received signal %s", signame); +} + +static +int set_worker_sighandlers(void) +{ + int ret = 0; + sigset_t sigset; + struct sigaction sa; + + if ((ret = sigemptyset(&sigset)) < 0) { + PERROR("sigemptyset"); + goto end; + } + + sa.sa_handler = worker_sighandler; + sa.sa_mask = sigset; + sa.sa_flags = 0; + if ((ret = sigaction(SIGINT, &sa, NULL)) < 0) { + PERROR("sigaction SIGINT"); + goto end; + } + + if ((ret = sigaction(SIGTERM, &sa, NULL)) < 0) { + PERROR("sigaction SIGTERM"); + goto end; + } + + DBG("run_as signal handler set for SIGTERM and SIGINT"); +end: + return ret; +} + LTTNG_HIDDEN int run_as_create_worker(char *procname) { @@ -566,6 +641,10 @@ int run_as_create_worker(char *procname) } else if (pid == 0) { /* Child */ + reset_sighandler(); + + set_worker_sighandlers(); + /* The child has no use for this lock. */ pthread_mutex_unlock(&worker_lock); /* Just close, no shutdown. */ @@ -628,8 +707,6 @@ LTTNG_HIDDEN void run_as_destroy_worker(void) { struct run_as_worker *worker = global_worker; - int status; - pid_t pid; pthread_mutex_lock(&worker_lock); if (!worker) { @@ -641,9 +718,29 @@ void run_as_destroy_worker(void) } worker->sockpair[0] = -1; /* Wait for worker. */ - pid = waitpid(worker->pid, &status, 0); - if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { - PERROR("wait"); + for (;;) { + int status; + pid_t wait_ret; + + wait_ret = waitpid(worker->pid, &status, 0); + if (wait_ret < 0) { + if (errno == EINTR) { + continue; + } + PERROR("waitpid"); + break; + } + + if (WIFEXITED(status)) { + LOG(WEXITSTATUS(status) == 0 ? PRINT_DBG : PRINT_ERR, + DEFAULT_RUN_AS_WORKER_NAME " terminated with status code %d", + WEXITSTATUS(status)); + break; + } else if (WIFSIGNALED(status)) { + ERR(DEFAULT_RUN_AS_WORKER_NAME " was killed by signal %d", + WTERMSIG(status)); + break; + } } free(worker); global_worker = NULL;