Backport: Add --working-directory options to lttng-relayd
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Wed, 16 May 2018 22:24:01 +0000 (18:24 -0400)
committerJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Fri, 21 Sep 2018 04:00:52 +0000 (00:00 -0400)
Base version

Signed-off-by: Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
src/bin/lttng-relayd/main.c
src/common/utils.c
src/common/utils.h

index 43498017dd0b41cbbafe908dbb69e1287c57d066..7f0b413bcbb789b83422255aad252e71c3b8e0d5 100644 (file)
@@ -83,7 +83,7 @@ enum relay_connection_status {
 };
 
 /* command line options */
-char *opt_output_path;
+char *opt_output_path, *opt_working_directory;
 static int opt_daemon, opt_background;
 
 /*
@@ -169,6 +169,7 @@ static struct option long_options[] = {
        { "verbose", 0, 0, 'v', },
        { "config", 1, 0, 'f' },
        { "version", 0, 0, 'V' },
+       { "working-directory", 1, 0, 'w', },
        { NULL, 0, 0, 0, },
 };
 
@@ -279,6 +280,20 @@ static int set_option(int opt, const char *arg, const char *optname)
                        }
                }
                break;
+       case 'w':
+               if (lttng_is_setuid_setgid()) {
+                       WARN("Getting '%s' argument from setuid/setgid binary refused for security reasons.",
+                               "-w, --working-directory");
+               } else {
+                       ret = asprintf(&opt_working_directory, "%s", arg);
+                       if (ret < 0) {
+                               ret = -errno;
+                               PERROR("asprintf working_directory");
+                               goto end;
+                       }
+               }
+               break;
+
        case 'v':
                /* Verbose level can increase using multiple -v */
                if (arg) {
@@ -3191,6 +3206,15 @@ int main(int argc, char **argv)
                }
        }
 
+
+       if (opt_working_directory) {
+               ret = utils_change_working_dir(opt_working_directory);
+               if (ret) {
+                       ERR("Changing working directory");
+                       goto exit_options;
+               }
+       }
+
        /* Initialize thread health monitoring */
        health_relayd = health_app_create(NR_HEALTH_RELAYD_TYPES);
        if (!health_relayd) {
index 25d510854f972438085c98a8b6f41fd06001bf47..d8a78091a4076fd90bb9d9956286dd1dfb9b8f2b 100644 (file)
@@ -1452,3 +1452,33 @@ int utils_show_man_page(int section, const char *page_name)
                section_string, page_name, NULL);
        return ret;
 }
+
+LTTNG_HIDDEN
+int utils_change_working_dir(const char *path)
+{
+       int ret;
+
+       assert(path);
+
+       ret = chdir(path);
+       if (ret) {
+               PERROR("Failed to change working directory: %s", path);
+               goto end;
+       }
+
+       /* Check for write access */
+       if (access(path, W_OK)) {
+               if (errno == EACCES) {
+                       /*
+                        * Do not treat this as an error since the permission
+                        * might change in the lifetime of the process
+                        */
+                       DBG("Working directory is not writable: %s", path);
+               } else {
+                       PERROR("access");
+               }
+       }
+
+end:
+       return ret;
+}
index 0daf5b98df70d7add96b6a7fda2fac53e85ad681..4c7884b765429f650fc3f7bb0d91651997e67467 100644 (file)
@@ -61,5 +61,6 @@ int utils_create_lock_file(const char *filepath);
 int utils_recursive_rmdir(const char *path);
 int utils_truncate_stream_file(int fd, off_t length);
 int utils_show_man_page(int section, const char *page_name);
+int utils_change_working_dir(const char *path);
 
 #endif /* _COMMON_UTILS_H */
This page took 0.029277 seconds and 5 git commands to generate.