#include <fcntl.h>
#include <sched.h>
#include <sys/signal.h>
+#include <assert.h>
#include <common/common.h>
#include <common/utils.h>
ssize_t readlen, writelen;
struct run_as_ret recvret;
- pthread_mutex_lock(&worker_lock);
/*
* If we are non-root, we can only deal with our own uid.
*/
}
end:
- pthread_mutex_unlock(&worker_lock);
errno = recvret._errno;
return recvret.ret;
}
}
static
-int run_as(struct run_as_worker *worker,
- enum run_as_cmd cmd,
- struct run_as_data *data, uid_t uid, gid_t gid)
+int run_as(enum run_as_cmd cmd, struct run_as_data *data, uid_t uid, gid_t gid)
{
int ret;
- if (worker) {
+ if (use_clone()) {
DBG("Using run_as worker");
- ret = run_as_cmd(worker, cmd, data, uid, gid);
+ pthread_mutex_lock(&worker_lock);
+ assert(global_worker);
+ ret = run_as_cmd(global_worker, cmd, data, uid, gid);
+ pthread_mutex_unlock(&worker_lock);
+
} else {
DBG("Using run_as without worker");
ret = run_as_noworker(cmd, data, uid, gid);
LTTNG_HIDDEN
int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
{
- struct run_as_worker *worker = global_worker;
struct run_as_data data;
DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
data.u.mkdir.path[PATH_MAX - 1] = '\0';
data.u.mkdir.mode = mode;
- return run_as(worker, RUN_AS_MKDIR_RECURSIVE, &data, uid, gid);
+ return run_as(RUN_AS_MKDIR_RECURSIVE, &data, uid, gid);
}
LTTNG_HIDDEN
int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
{
- struct run_as_worker *worker = global_worker;
struct run_as_data data;
DBG3("mkdir() %s with mode %d for uid %d and gid %d",
strncpy(data.u.mkdir.path, path, PATH_MAX - 1);
data.u.mkdir.path[PATH_MAX - 1] = '\0';
data.u.mkdir.mode = mode;
- return run_as(worker, RUN_AS_MKDIR, &data, uid, gid);
+ return run_as(RUN_AS_MKDIR, &data, uid, gid);
}
/*
LTTNG_HIDDEN
int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
{
- struct run_as_worker *worker = global_worker;
struct run_as_data data;
DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
data.u.open.path[PATH_MAX - 1] = '\0';
data.u.open.flags = flags;
data.u.open.mode = mode;
- return run_as(worker, RUN_AS_OPEN, &data, uid, gid);
+ return run_as(RUN_AS_OPEN, &data, uid, gid);
}
LTTNG_HIDDEN
int run_as_unlink(const char *path, uid_t uid, gid_t gid)
{
- struct run_as_worker *worker = global_worker;
struct run_as_data data;
DBG3("unlink() %s with for uid %d and gid %d",
path, uid, gid);
strncpy(data.u.unlink.path, path, PATH_MAX - 1);
data.u.unlink.path[PATH_MAX - 1] = '\0';
- return run_as(worker, RUN_AS_UNLINK, &data, uid, gid);
+ return run_as(RUN_AS_UNLINK, &data, uid, gid);
}
LTTNG_HIDDEN
int run_as_rmdir_recursive(const char *path, uid_t uid, gid_t gid)
{
- struct run_as_worker *worker = global_worker;
struct run_as_data data;
DBG3("rmdir_recursive() %s with for uid %d and gid %d",
path, uid, gid);
strncpy(data.u.rmdir_recursive.path, path, PATH_MAX - 1);
data.u.rmdir_recursive.path[PATH_MAX - 1] = '\0';
- return run_as(worker, RUN_AS_RMDIR_RECURSIVE, &data, uid, gid);
+ return run_as(RUN_AS_RMDIR_RECURSIVE, &data, uid, gid);
+}
+
+static
+void reset_sighandler(void)
+{
+ int sig;
+
+ 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);
+ }
+ }
}
LTTNG_HIDDEN
struct run_as_ret recvret;
struct run_as_worker *worker;
+ pthread_mutex_lock(&worker_lock);
+ assert(!global_worker);
if (!use_clone()) {
+ /*
+ * Don't initialize a worker, all run_as tasks will be performed
+ * in the current process.
+ */
ret = 0;
goto end;
}
} else if (pid == 0) {
/* Child */
+ reset_sighandler();
+
+ /* The child has no use for this lock. */
+ pthread_mutex_unlock(&worker_lock);
/* Just close, no shutdown. */
if (close(worker->sockpair[0])) {
PERROR("close");
global_worker = worker;
}
end:
+ pthread_mutex_unlock(&worker_lock);
return ret;
/* Error handling. */
}
error_sock:
free(worker);
+ pthread_mutex_unlock(&worker_lock);
return ret;
}
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) {
- return;
+ goto end;
}
/* Close unix socket */
if (lttcomm_close_unix_sock(worker->sockpair[0])) {
}
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;
+end:
+ pthread_mutex_unlock(&worker_lock);
}