From: Mathieu Desnoyers Date: Wed, 19 Aug 2015 21:13:48 +0000 (-0700) Subject: Fix: streamline ret/errno of run_as() X-Git-Tag: v2.6.1~49 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=4ffbac14c6208ca9789f6507c0d9e54b2483c90b;p=lttng-tools.git Fix: streamline ret/errno of run_as() Signed-off-by: Mathieu Desnoyers Signed-off-by: Jérémie Galarneau Conflicts: src/bin/lttng-sessiond/ust-registry.c src/common/runas.c src/common/ust-consumer/ust-consumer.c --- diff --git a/src/bin/lttng-sessiond/consumer.c b/src/bin/lttng-sessiond/consumer.c index ea760000c..3c08041b0 100644 --- a/src/bin/lttng-sessiond/consumer.c +++ b/src/bin/lttng-sessiond/consumer.c @@ -1342,7 +1342,7 @@ int consumer_snapshot_channel(struct consumer_socket *socket, uint64_t key, ret = run_as_mkdir_recursive(msg.u.snapshot_channel.pathname, S_IRWXU | S_IRWXG, uid, gid); if (ret < 0) { - if (ret != -EEXIST) { + if (errno != EEXIST) { ERR("Trace directory creation error"); goto error; } diff --git a/src/bin/lttng-sessiond/kernel-consumer.c b/src/bin/lttng-sessiond/kernel-consumer.c index c5a3130bf..c76ca29e5 100644 --- a/src/bin/lttng-sessiond/kernel-consumer.c +++ b/src/bin/lttng-sessiond/kernel-consumer.c @@ -56,7 +56,7 @@ static char *create_channel_path(struct consumer_output *consumer, /* Create directory */ ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, uid, gid); if (ret < 0) { - if (ret != -EEXIST) { + if (errno != EEXIST) { ERR("Trace directory creation error"); goto error; } diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 19283d439..a66645d95 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -2757,7 +2757,7 @@ static int create_kernel_session(struct ltt_session *session) session->kernel_session->consumer->dst.trace_path, S_IRWXU | S_IRWXG, session->uid, session->gid); if (ret < 0) { - if (ret != -EEXIST) { + if (errno != EEXIST) { ERR("Trace directory creation error"); goto error; } diff --git a/src/bin/lttng-sessiond/ust-app.c b/src/bin/lttng-sessiond/ust-app.c index c1fe2f2a5..9d262184c 100644 --- a/src/bin/lttng-sessiond/ust-app.c +++ b/src/bin/lttng-sessiond/ust-app.c @@ -3963,7 +3963,7 @@ int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app) ret = run_as_mkdir_recursive(usess->consumer->dst.trace_path, S_IRWXU | S_IRWXG, ua_sess->euid, ua_sess->egid); if (ret < 0) { - if (ret != -EEXIST) { + if (errno != EEXIST) { ERR("Trace directory creation error"); goto error_unlock; } diff --git a/src/bin/lttng-sessiond/ust-consumer.c b/src/bin/lttng-sessiond/ust-consumer.c index fee57d205..6410a1eed 100644 --- a/src/bin/lttng-sessiond/ust-consumer.c +++ b/src/bin/lttng-sessiond/ust-consumer.c @@ -72,7 +72,7 @@ static char *setup_trace_path(struct consumer_output *consumer, ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, ua_sess->euid, ua_sess->egid); if (ret < 0) { - if (ret != -EEXIST) { + if (errno != EEXIST) { ERR("Trace directory creation error"); goto error; } diff --git a/src/common/index/index.c b/src/common/index/index.c index 18705a39f..96073d75f 100644 --- a/src/common/index/index.c +++ b/src/common/index/index.c @@ -52,7 +52,7 @@ int index_create_file(char *path_name, char *stream_name, int uid, int gid, /* Create index directory if necessary. */ ret = utils_mkdir(fullpath, S_IRWXU | S_IRWXG, uid, gid); if (ret < 0) { - if (ret != -EEXIST) { + if (errno != EEXIST) { PERROR("Index trace directory creation error"); goto error; } diff --git a/src/common/runas.c b/src/common/runas.c index c1d07b3a5..3521bfbdd 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -81,6 +81,11 @@ struct run_as_unlink_data { const char *path; }; +struct run_as_ret { + int ret; + int _errno; +}; + #ifdef VALGRIND static int use_clone(void) @@ -118,36 +123,25 @@ int _mkdir_recursive(void *_data) static int _mkdir(void *_data) { - int ret; struct run_as_mkdir_data *data = _data; - ret = mkdir(data->path, data->mode); - if (ret < 0) { - ret = -errno; - } - - return ret; + return mkdir(data->path, data->mode); } static int _open(void *_data) { struct run_as_open_data *data = _data; + return open(data->path, data->flags, data->mode); } static int _unlink(void *_data) { - int ret; struct run_as_unlink_data *data = _data; - ret = unlink(data->path); - if (ret < 0) { - ret = -errno; - } - - return ret; + return unlink(data->path); } static @@ -156,7 +150,7 @@ int child_run_as(void *_data) int ret; struct run_as_data *data = _data; ssize_t writelen; - int sendret; + struct run_as_ret sendret; /* * Child: it is safe to drop egid and euid while sharing the @@ -169,7 +163,6 @@ int child_run_as(void *_data) ret = setegid(data->gid); if (ret < 0) { PERROR("setegid"); - sendret = -1; goto write_return; } } @@ -177,7 +170,6 @@ int child_run_as(void *_data) ret = seteuid(data->uid); if (ret < 0) { PERROR("seteuid"); - sendret = -1; goto write_return; } } @@ -185,9 +177,11 @@ int child_run_as(void *_data) * Also set umask to 0 for mkdir executable bit. */ umask(0); - sendret = (*data->cmd)(data->data); + ret = (*data->cmd)(data->data); write_return: + sendret.ret = ret; + sendret._errno = errno; /* send back return value */ writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret)); if (writelen < sizeof(sendret)) { @@ -208,23 +202,26 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) pid_t pid; int retval_pipe[2]; void *child_stack; - int retval; + struct run_as_ret recvret; /* * If we are non-root, we can only deal with our own uid. */ if (geteuid() != 0) { if (uid != geteuid()) { + recvret.ret = -1; + recvret._errno = EPERM; ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)", uid, geteuid()); - return -EPERM; + goto end; } } ret = pipe(retval_pipe); if (ret < 0) { + recvret.ret = -1; + recvret._errno = errno; PERROR("pipe"); - retval = ret; goto end; } run_as_data.data = data; @@ -237,8 +234,9 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK, -1, 0); if (child_stack == MAP_FAILED) { + recvret.ret = -1; + recvret._errno = ENOMEM; PERROR("mmap"); - retval = -ENOMEM; goto close_pipe; } /* @@ -248,14 +246,16 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2), &run_as_data); if (pid < 0) { + recvret.ret = -1; + recvret._errno = errno; PERROR("clone"); - retval = pid; goto unmap_stack; } /* receive return value */ - readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval)); - if (readlen < sizeof(retval)) { - ret = -1; + readlen = lttng_read(retval_pipe[0], &recvret, sizeof(recvret)); + if (readlen < sizeof(recvret)) { + recvret.ret = -1; + recvret._errno = errno; } /* @@ -264,26 +264,33 @@ int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) */ pid = waitpid(pid, &status, 0); if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) { + recvret.ret = -1; + recvret._errno = errno; PERROR("wait"); - retval = -1; } unmap_stack: ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE); if (ret < 0) { + recvret.ret = -1; + recvret._errno = errno; PERROR("munmap"); - retval = ret; } close_pipe: ret = close(retval_pipe[0]); if (ret) { + recvret.ret = -1; + recvret._errno = errno; PERROR("close"); } ret = close(retval_pipe[1]); if (ret) { + recvret.ret = -1; + recvret._errno = errno; PERROR("close"); } end: - return retval; + errno = recvret._errno; + return recvret.ret; } /* @@ -294,12 +301,14 @@ end: static int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid) { - int ret; + int ret, saved_errno; mode_t old_mask; old_mask = umask(0); ret = cmd(data); + saved_errno = errno; umask(old_mask); + errno = saved_errno; return ret; } diff --git a/src/common/utils.c b/src/common/utils.c index 3e0058067..7a09259eb 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -754,10 +754,6 @@ int utils_unlink_stream_file(const char *path_name, char *file_name, uint64_t si ret = unlink(path); } else { ret = run_as_unlink(path, uid, gid); - if (ret < 0) { - errno = -ret; - ret = -1; - } } if (ret < 0) { goto error;