#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);
}
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 */
+ /* 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;
}
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])) {
}
free(worker);
global_worker = NULL;
+end:
+ pthread_mutex_unlock(&worker_lock);
}