X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Frunas.c;h=1105f207fb83d3b4ef1734159fbd7a7e0e0a3f04;hp=3a1de0a3acdd2b3613d715d48f404c3a60eb9eeb;hb=5b5bb8c82d70cf65707d5561e0640bf2ae8c2e6e;hpb=749b7a0c92f961c87318691bf44efc250d29ce2a diff --git a/src/common/runas.c b/src/common/runas.c index 3a1de0a3a..1105f207f 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -31,10 +31,12 @@ #include #include #include +#include #include #include #include +#include #include #include "runas.h" @@ -322,12 +324,12 @@ 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); - if (ret) { - errno = ret; - ret = -1; - PERROR("pthread_setname_np"); - return EXIT_FAILURE; + ret = lttng_prctl(PR_SET_NAME, + (unsigned long) DEFAULT_RUN_AS_WORKER_NAME, 0, 0, 0); + if (ret && ret != -ENOSYS) { + /* Don't fail as this is not essential. */ + PERROR("prctl PR_SET_NAME"); + ret = 0; } sendret.ret = 0; @@ -527,6 +529,73 @@ 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; + + DBG("Resetting run_as worker signal handlers to default"); + for (sig = 1; sig <= 31; sig++) { + (void) signal(sig, SIG_DFL); + } + return 0; +} + +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 +635,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. */ @@ -580,6 +653,7 @@ int run_as_create_worker(char *procname) ret = -1; } worker->sockpair[1] = -1; + LOG(ret ? PRINT_ERR : PRINT_DBG, "run_as worker exiting (ret = %d)", ret); exit(ret ? EXIT_FAILURE : EXIT_SUCCESS); } else { /* Parent */ @@ -628,22 +702,42 @@ LTTNG_HIDDEN void run_as_destroy_worker(void) { struct run_as_worker *worker = global_worker; - int status; - pid_t pid; + DBG("Destroying run_as worker"); pthread_mutex_lock(&worker_lock); if (!worker) { goto end; } /* Close unix socket */ + DBG("Closing run_as worker socket"); if (lttcomm_close_unix_sock(worker->sockpair[0])) { PERROR("close"); } 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;