Fix: streamline ret/errno of run_as()
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 19 Aug 2015 21:13:48 +0000 (14:13 -0700)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Fri, 4 Sep 2015 22:43:42 +0000 (18:43 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-sessiond/consumer.c
src/bin/lttng-sessiond/kernel-consumer.c
src/bin/lttng-sessiond/main.c
src/bin/lttng-sessiond/ust-app.c
src/bin/lttng-sessiond/ust-consumer.c
src/bin/lttng-sessiond/ust-registry.c
src/common/index/index.c
src/common/runas.c
src/common/ust-consumer/ust-consumer.c
src/common/utils.c

index 6eb84d6aee403318df005dad3393c7c24f9e19b7..b9764f8a52bc7cfe73076a3ec0f28f19bc4b99bc 100644 (file)
@@ -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) {
                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;
                        }
                                ERR("Trace directory creation error");
                                goto error;
                        }
index 6cda088d9e5e7600174476d29962adf2bd532189..83771f2d75c8896d142a705066410c50023acaf4 100644 (file)
@@ -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) {
                /* 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;
                        }
                                ERR("Trace directory creation error");
                                goto error;
                        }
index 59faae853fc389cfc946a9d700e4b8feb2451db5..c5c7d84b1f5d7bb0d434fff7f187665ca9041cc7 100644 (file)
@@ -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) {
                                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;
                        }
                                ERR("Trace directory creation error");
                                goto error;
                        }
index 01204beb7df8f7e945a5e9468db37954c430e154..96ba2f4bcad0649afe1cfcec93e2f6c1f13002df 100644 (file)
@@ -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) {
                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;
                        }
                                ERR("Trace directory creation error");
                                goto error_unlock;
                        }
index ba464d9b7d90c8b1a318c527eefaf7f5032b2b02..27ebf7d23270a6ad4a5a1e13f3eb971573a2455a 100644 (file)
@@ -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) {
                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;
                        }
                                ERR("Trace directory creation error");
                                goto error;
                        }
index 2ba41ed45dffbb91d272c4561dcf040290f6ed58..35911e322f49175a29e58f0784b32c966ca7f7db 100644 (file)
@@ -593,7 +593,6 @@ int ust_registry_session_init(struct ust_registry_session **sessionp,
                        S_IRWXU | S_IRWXG,
                        euid, egid);
                if (ret) {
                        S_IRWXU | S_IRWXG,
                        euid, egid);
                if (ret) {
-                       errno = -ret;
                        PERROR("run_as_mkdir_recursive");
                        goto error;
                }
                        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) {
                        O_WRONLY | O_CREAT | O_EXCL,
                        S_IRUSR | S_IWUSR, euid, egid);
                if (ret < 0) {
-                       errno = -ret;
                        PERROR("Opening metadata file");
                        goto error;
                }
                        PERROR("Opening metadata file");
                        goto error;
                }
index 46f8bcb1f02bfcb185782d65bb5e77fa8c4720cc..c193ced27568f94f5764dd7672e8b440aaf104a7 100644 (file)
@@ -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) {
        /* 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;
                }
                        PERROR("Index trace directory creation error");
                        goto error;
                }
index 36be188fafb54036688930689d1d23781cbe000d..8dda20916033dbce5a6d810ea94b234b3117f63a 100644 (file)
@@ -87,6 +87,11 @@ struct run_as_recursive_rmdir_data {
        const char *path;
 };
 
        const char *path;
 };
 
+struct run_as_ret {
+       int ret;
+       int _errno;
+};
+
 #ifdef VALGRIND
 static
 int use_clone(void)
 #ifdef VALGRIND
 static
 int use_clone(void)
@@ -124,50 +129,33 @@ int _mkdir_recursive(void *_data)
 static
 int _mkdir(void *_data)
 {
 static
 int _mkdir(void *_data)
 {
-       int ret;
        struct run_as_mkdir_data *data = _data;
 
        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;
 }
 
 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)
 {
        return open(data->path, data->flags, data->mode);
 }
 
 static
 int _unlink(void *_data)
 {
-       int ret;
        struct run_as_unlink_data *data = _data;
 
        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)
 {
 }
 
 static
 int _recursive_rmdir(void *_data)
 {
-       int ret;
        struct run_as_recursive_rmdir_data *data = _data;
 
        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
 }
 
 static
@@ -176,7 +164,7 @@ int child_run_as(void *_data)
        int ret;
        struct run_as_data *data = _data;
        ssize_t writelen;
        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
 
        /*
         * 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");
                ret = setegid(data->gid);
                if (ret < 0) {
                        PERROR("setegid");
-                       sendret = -1;
                        goto write_return;
                }
        }
                        goto write_return;
                }
        }
@@ -197,7 +184,6 @@ int child_run_as(void *_data)
                ret = seteuid(data->uid);
                if (ret < 0) {
                        PERROR("seteuid");
                ret = seteuid(data->uid);
                if (ret < 0) {
                        PERROR("seteuid");
-                       sendret = -1;
                        goto write_return;
                }
        }
                        goto write_return;
                }
        }
@@ -205,9 +191,11 @@ int child_run_as(void *_data)
         * Also set umask to 0 for mkdir executable bit.
         */
        umask(0);
         * Also set umask to 0 for mkdir executable bit.
         */
        umask(0);
-       sendret = (*data->cmd)(data->data);
+       ret = (*data->cmd)(data->data);
 
 write_return:
 
 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)) {
        /* 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;
        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()) {
 
        /*
         * 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());
                        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) {
                }
        }
 
        ret = pipe(retval_pipe);
        if (ret < 0) {
+               recvret.ret = -1;
+               recvret._errno = errno;
                PERROR("pipe");
                PERROR("pipe");
-               retval = ret;
                goto end;
        }
        run_as_data.data = data;
                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) {
                MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
                -1, 0);
        if (child_stack == MAP_FAILED) {
+               recvret.ret = -1;
+               recvret._errno = ENOMEM;
                PERROR("mmap");
                PERROR("mmap");
-               retval = -ENOMEM;
                goto close_pipe;
        }
        /*
                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) {
        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");
                PERROR("clone");
-               retval = pid;
                goto unmap_stack;
        }
        /* receive return value */
                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) {
         */
        pid = waitpid(pid, &status, 0);
        if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
+               recvret.ret = -1;
+               recvret._errno = errno;
                PERROR("wait");
                PERROR("wait");
-               retval = -1;
        }
 unmap_stack:
        ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
        if (ret < 0) {
        }
 unmap_stack:
        ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
        if (ret < 0) {
+               recvret.ret = -1;
+               recvret._errno = errno;
                PERROR("munmap");
                PERROR("munmap");
-               retval = ret;
        }
 close_pipe:
        ret = close(retval_pipe[0]);
        if (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) {
                PERROR("close");
        }
        ret = close(retval_pipe[1]);
        if (ret) {
+               recvret.ret = -1;
+               recvret._errno = errno;
                PERROR("close");
        }
 end:
                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)
 {
 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);
        mode_t old_mask;
 
        old_mask = umask(0);
        ret = cmd(data);
+       saved_errno = errno;
        umask(old_mask);
        umask(old_mask);
+       errno = saved_errno;
 
        return ret;
 }
 
        return ret;
 }
index df12f6b8746de71a4a1ef952359abe2158268733..819817d149922d5b2a6b02eb42230a3e35f21fe6 100644 (file)
@@ -504,7 +504,6 @@ error_open:
                        closeret = run_as_unlink(shm_path,
                                        channel->uid, channel->gid);
                        if (closeret) {
                        closeret = run_as_unlink(shm_path,
                                        channel->uid, channel->gid);
                        if (closeret) {
-                               errno = -closeret;
                                PERROR("unlink %s", shm_path);
                        }
                }
                                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) {
                        }
                        ret = run_as_unlink(shm_path, chan->uid, chan->gid);
                        if (ret) {
-                               errno = -ret;
                                PERROR("unlink %s", shm_path);
                        }
                }
                                PERROR("unlink %s", shm_path);
                        }
                }
index 337713eb78ab39493bb745c994d7d43df9400d0f..d7811bd7a6fd566e992c505a1d81770aa1790b18 100644 (file)
@@ -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);
                ret = unlink(path);
        } else {
                ret = run_as_unlink(path, uid, gid);
-               if (ret < 0) {
-                       errno = -ret;
-                       ret = -1;
-               }
        }
        if (ret < 0) {
                goto error;
        }
        if (ret < 0) {
                goto error;
This page took 0.036366 seconds and 4 git commands to generate.