From: Jonathan Rajotte Date: Wed, 16 May 2018 22:24:01 +0000 (-0400) Subject: Backport: Add --working-directory options to lttng-relayd X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=commitdiff_plain;h=763755801af9465a7936b1a4cbb613123781f740 Backport: Add --working-directory options to lttng-relayd Base version Signed-off-by: Jonathan Rajotte --- diff --git a/src/bin/lttng-relayd/main.c b/src/bin/lttng-relayd/main.c index 43498017d..7f0b413bc 100644 --- a/src/bin/lttng-relayd/main.c +++ b/src/bin/lttng-relayd/main.c @@ -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) { diff --git a/src/common/utils.c b/src/common/utils.c index 25d510854..d8a78091a 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -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; +} diff --git a/src/common/utils.h b/src/common/utils.h index 0daf5b98d..4c7884b76 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -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 */