Accept uid and gid parameters in utils_mkdir()/utils_mkdir_recursive()
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 3 Sep 2015 19:09:00 +0000 (15:09 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 24 Sep 2015 01:34:11 +0000 (21:34 -0400)
utils_mkdir* utils may now be use in immediate or "run_as" mode.

This is done since some of the code shared between daemons calls
run_as directly, which doesn't support negative uid/gid (which we use
to mean "run as current user").

Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
src/bin/lttng-relayd/main.c
src/common/runas.c
src/common/utils.c
src/common/utils.h

index a0d82b0cb6534b7b688957459478e07236831b70..e69ddd7d2cf9f7595a4e81d5324243c781aa58be 100644 (file)
@@ -1221,7 +1221,8 @@ int relay_add_stream(struct lttcomm_relayd_hdr *recv_hdr,
        lttng_ht_node_init_u64(&stream->node, stream->stream_handle);
        pthread_mutex_init(&stream->lock, NULL);
 
-       ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG);
+       ret = utils_mkdir_recursive(stream->path_name, S_IRWXU | S_IRWXG,
+                       -1, -1);
        if (ret < 0) {
                ERR("relay creating output directory");
                goto err_free_stream;
@@ -2744,7 +2745,8 @@ int main(int argc, char **argv)
                        goto exit;
                }
 
-               ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG);
+               ret = utils_mkdir_recursive(opt_output_path, S_IRWXU | S_IRWXG,
+                               -1, -1);
                if (ret < 0) {
                        ERR("Unable to create %s", opt_output_path);
                        goto exit;
index 5ab42717a9058b4a5083516aa142a3c1efda226d..a8fe47e8eafe7b2f91fb7ea0a1bb205c02c50117 100644 (file)
@@ -91,6 +91,9 @@ int use_clone(void)
 }
 #endif
 
+LTTNG_HIDDEN
+int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode);
+
 /*
  * Create recursively directory using the FULL path.
  */
@@ -104,7 +107,8 @@ int _mkdir_recursive(void *_data)
        path = data->path;
        mode = data->mode;
 
-       return utils_mkdir_recursive(path, mode);
+       /* Safe to call as we have transitioned to the requested uid/gid. */
+       return _utils_mkdir_recursive_unsafe(path, mode);
 }
 
 static
index 203739c320707e55a3a3903cec27540c97339131..9b26656c65662a43360f8489114b839fea754e57 100644 (file)
@@ -523,12 +523,42 @@ error:
 }
 
 /*
- * Recursively create directory using the given path and mode.
+ * Create directory using the given path and mode.
  *
  * On success, return 0 else a negative error code.
  */
 LTTNG_HIDDEN
-int utils_mkdir_recursive(const char *path, mode_t mode)
+int utils_mkdir(const char *path, mode_t mode, int uid, int gid)
+{
+       int ret;
+
+       if (uid < 0 || gid < 0) {
+               ret = mkdir(path, mode);
+       } else {
+               ret = run_as_mkdir(path, mode, uid, gid);
+       }
+       if (ret < 0) {
+               if (errno != EEXIST) {
+                       PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL",
+                                       uid, gid);
+               } else {
+                       ret = 0;
+               }
+       }
+
+       return ret;
+}
+
+/*
+ * Internal version of mkdir_recursive. Runs as the current user.
+ * Don't call directly; use utils_mkdir_recursive().
+ *
+ * This function is ominously marked as "unsafe" since it should only
+ * be called by a caller that has transitioned to the uid and gid under which
+ * the directory creation should occur.
+ */
+LTTNG_HIDDEN
+int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode)
 {
        char *p, tmp[PATH_MAX];
        size_t len;
@@ -573,7 +603,7 @@ int utils_mkdir_recursive(const char *path, mode_t mode)
        ret = mkdir(tmp, mode);
        if (ret < 0) {
                if (errno != EEXIST) {
-                       PERROR("mkdir recursive last piece");
+                       PERROR("mkdir recursive last element");
                        ret = -errno;
                } else {
                        ret = 0;
@@ -584,8 +614,34 @@ error:
        return ret;
 }
 
+/*
+ * Recursively create directory using the given path and mode, under the
+ * provided uid and gid.
+ *
+ * On success, return 0 else a negative error code.
+ */
+LTTNG_HIDDEN
+int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid)
+{
+       int ret;
+
+       if (uid < 0 || gid < 0) {
+               /* Run as current user. */
+               ret = _utils_mkdir_recursive_unsafe(path, mode);
+       } else {
+               ret = run_as_mkdir_recursive(path, mode, uid, gid);
+       }
+       if (ret < 0) {
+               PERROR("mkdir %s, uid %d, gid %d", path ? path : "NULL",
+                               uid, gid);
+       }
+
+       return ret;
+}
+
 /*
  * Create the stream tracefile on disk.
+ * path is the output parameter. It needs to be PATH_MAX len.
  *
  * Return 0 on success or else a negative value.
  */
index 537fe0f9e6cf1017cafbe7cbd43fa5f054291a3e..90dbf175ace68a6e41074a5d3e8d4169c6bd2914 100644 (file)
@@ -37,7 +37,8 @@ void utils_close_pipe(int *src);
 char *utils_strdupdelim(const char *begin, const char *end);
 int utils_set_fd_cloexec(int fd);
 int utils_create_pid_file(pid_t pid, const char *filepath);
-int utils_mkdir_recursive(const char *path, mode_t mode);
+int utils_mkdir(const char *path, mode_t mode, int uid, int gid);
+int utils_mkdir_recursive(const char *path, mode_t mode, int uid, int gid);
 int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size,
                uint64_t count, int uid, int gid, char *suffix);
 int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
This page took 0.029516 seconds and 4 git commands to generate.