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>
Thu, 24 Sep 2015 02:00:49 +0000 (22:00 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
 Conflicts:
src/bin/lttng-sessiond/ust-registry.c
src/common/runas.c
src/common/ust-consumer/ust-consumer.c

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/common/index/index.c
src/common/runas.c
src/common/utils.c

index ea760000c39febfd11856963b4a4a4485ae31b5e..3c08041b0110f340aaec10daf7fdd1c9e44c9413 100644 (file)
@@ -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;
                        }
index c5a3130bf152ba779dd854ae482278dfe1e245ac..c76ca29e56736ac07263dc39147a1d4b559e5521 100644 (file)
@@ -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;
                        }
index 19283d439087c7a155656314803b0472e093b0a7..a66645d959fd98f6ee40511fe635d863398f9ebb 100644 (file)
@@ -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;
                        }
index c1fe2f2a517748fedad70a04f40d17dea96bc4db..9d262184c237041aafd0e0d08c04daf7b15c0526 100644 (file)
@@ -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;
                        }
index fee57d205cbc3d2b5d86224563ca96bf2080a8cf..6410a1eed692df9a62d15bdf7765b4e40d16d001 100644 (file)
@@ -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;
                        }
index 18705a39f86e8e5e6f44c2816cdc201258961402..96073d75fbe104a332e54b2114d1d978de6e44ca 100644 (file)
@@ -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;
                }
index c1d07b3a5e7e16f759540603ceb10efeea14fe79..3521bfbdd07fe628322418b52734ce6c80f793c1 100644 (file)
@@ -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;
 }
index 3e0058067fcd2b73bc42fe767c0e022b96b03b39..7a09259ebc6b8de4657f804146bb795c35ac023e 100644 (file)
@@ -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;
This page took 0.035835 seconds and 4 git commands to generate.