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, 3 Sep 2015 20:26:24 +0000 (16:26 -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 8e1879f50ae3875dd90581c1fe3a43465d8057af..92d466df7bc34ff2ba73161f37944dd9147b0cb5 100644 (file)
@@ -1285,7 +1285,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);
 
        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;
        if (ret < 0) {
                ERR("relay creating output directory");
                goto err_free_stream;
@@ -2828,7 +2829,8 @@ int main(int argc, char **argv)
                        goto exit_options;
                }
 
                        goto exit_options;
                }
 
-               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);
                        retval = -1;
                if (ret < 0) {
                        ERR("Unable to create %s", opt_output_path);
                        retval = -1;
index 9d9091638a5a9910c8cec2b6810357ca087e4499..36be188fafb54036688930689d1d23781cbe000d 100644 (file)
@@ -101,6 +101,9 @@ int use_clone(void)
 }
 #endif
 
 }
 #endif
 
+LTTNG_HIDDEN
+int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode);
+
 /*
  * Create recursively directory using the FULL path.
  */
 /*
  * Create recursively directory using the FULL path.
  */
@@ -114,7 +117,8 @@ int _mkdir_recursive(void *_data)
        path = data->path;
        mode = data->mode;
 
        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
 }
 
 static
index 766f224c7e167118d30dbd99c6b32595b4fe99e4..db2ed8e7d11dee928a6f96be4167a94669f015c6 100644 (file)
@@ -532,12 +532,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
  *
  * 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;
 {
        char *p, tmp[PATH_MAX];
        size_t len;
@@ -582,7 +612,7 @@ int utils_mkdir_recursive(const char *path, mode_t mode)
        ret = mkdir(tmp, mode);
        if (ret < 0) {
                if (errno != EEXIST) {
        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;
                        ret = -errno;
                } else {
                        ret = 0;
@@ -593,8 +623,34 @@ error:
        return ret;
 }
 
        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.
 /*
  * 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.
  */
  *
  * Return 0 on success or else a negative value.
  */
index 05914cc37d694b28e8846adba3587df9526e1623..e5af6fadd3d0479c5b2f93badcf2b98c9c1a20c1 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);
 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,
 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.029289 seconds and 4 git commands to generate.