From df5b86c84d896eb2d74a8757c234492c1d1fc3be Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Wed, 19 Aug 2015 14:13:48 -0700 Subject: [PATCH] Fix: streamline ret/errno of run_as() MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Mathieu Desnoyers Signed-off-by: Jérémie Galarneau --- src/bin/lttng-sessiond/consumer.c | 2 +- src/bin/lttng-sessiond/kernel-consumer.c | 2 +- src/bin/lttng-sessiond/main.c | 2 +- src/bin/lttng-sessiond/ust-app.c | 2 +- src/bin/lttng-sessiond/ust-consumer.c | 2 +- src/bin/lttng-sessiond/ust-registry.c | 2 - src/common/index/index.c | 2 +- src/common/runas.c | 77 ++++++++++++------------ src/common/ust-consumer/ust-consumer.c | 2 - src/common/utils.c | 4 -- 10 files changed, 46 insertions(+), 51 deletions(-) diff --git a/src/bin/lttng-sessiond/consumer.c b/src/bin/lttng-sessiond/consumer.c index 6eb84d6ae..b9764f8a5 100644 --- a/src/bin/lttng-sessiond/consumer.c +++ b/src/bin/lttng-sessiond/consumer.c @@ -1353,7 +1353,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 6cda088d9..83771f2d7 100644 --- a/src/bin/lttng-sessiond/kernel-consumer.c +++ b/src/bin/lttng-sessiond/kernel-consumer.c @@ -57,7 +57,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 59faae853..c5c7d84b1 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -2851,7 +2851,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 01204beb7..96ba2f4bc 100644 --- a/src/bin/lttng-sessiond/ust-app.c +++ b/src/bin/lttng-sessiond/ust-app.c @@ -4085,7 +4085,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 ba464d9b7..27ebf7d23 100644 --- a/src/bin/lttng-sessiond/ust-consumer.c +++ b/src/bin/lttng-sessiond/ust-consumer.c @@ -73,7 +73,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/bin/lttng-sessiond/ust-registry.c b/src/bin/lttng-sessiond/ust-registry.c index 2ba41ed45..35911e322 100644 --- a/src/bin/lttng-sessiond/ust-registry.c +++ b/src/bin/lttng-sessiond/ust-registry.c @@ -593,7 +593,6 @@ int ust_registry_session_init(struct ust_registry_session **sessionp, S_IRWXU | S_IRWXG, euid, egid); if (ret) { - errno = -ret; PERROR("run_as_mkdir_recursive"); goto error; } @@ -604,7 +603,6 @@ int ust_registry_session_init(struct ust_registry_session **sessionp, O_WRONLY | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR, euid, egid); if (ret < 0) { - errno = -ret; PERROR("Opening metadata file"); goto error; } diff --git a/src/common/index/index.c b/src/common/index/index.c index 46f8bcb1f..c193ced27 100644 --- a/src/common/index/index.c +++ b/src/common/index/index.c @@ -53,7 +53,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 36be188fa..8dda20916 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -87,6 +87,11 @@ struct run_as_recursive_rmdir_data { const char *path; }; +struct run_as_ret { + int ret; + int _errno; +}; + #ifdef VALGRIND static int use_clone(void) @@ -124,50 +129,33 @@ 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 int _recursive_rmdir(void *_data) { - int ret; struct run_as_recursive_rmdir_data *data = _data; - ret = utils_recursive_rmdir(data->path); - if (ret < 0) { - ret = -errno; - } - - return ret; + return utils_recursive_rmdir(data->path); } static @@ -176,7 +164,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 @@ -189,7 +177,6 @@ int child_run_as(void *_data) ret = setegid(data->gid); if (ret < 0) { PERROR("setegid"); - sendret = -1; goto write_return; } } @@ -197,7 +184,6 @@ int child_run_as(void *_data) ret = seteuid(data->uid); if (ret < 0) { PERROR("seteuid"); - sendret = -1; goto write_return; } } @@ -205,9 +191,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)) { @@ -228,23 +216,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; @@ -257,8 +248,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; } /* @@ -268,14 +260,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; } /* @@ -284,26 +278,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; } /* @@ -314,12 +315,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/ust-consumer/ust-consumer.c b/src/common/ust-consumer/ust-consumer.c index df12f6b87..819817d14 100644 --- a/src/common/ust-consumer/ust-consumer.c +++ b/src/common/ust-consumer/ust-consumer.c @@ -504,7 +504,6 @@ error_open: closeret = run_as_unlink(shm_path, channel->uid, channel->gid); if (closeret) { - errno = -closeret; PERROR("unlink %s", shm_path); } } @@ -1835,7 +1834,6 @@ void lttng_ustconsumer_del_channel(struct lttng_consumer_channel *chan) } ret = run_as_unlink(shm_path, chan->uid, chan->gid); if (ret) { - errno = -ret; PERROR("unlink %s", shm_path); } } diff --git a/src/common/utils.c b/src/common/utils.c index 337713eb7..d7811bd7a 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -763,10 +763,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; -- 2.34.1