};
/* command line options */
-char *opt_output_path;
+char *opt_output_path, *opt_working_directory;
static int opt_daemon, opt_background, opt_print_version;
/*
{ "verbose", 0, 0, 'v', },
{ "config", 1, 0, 'f' },
{ "version", 0, 0, 'V' },
+ { "working-directory", 1, 0, 'w', },
{ NULL, 0, 0, 0, },
};
}
}
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 opt_working_directory");
+ goto end;
+ }
+ }
+ break;
+
case 'v':
/* Verbose level can increase using multiple -v */
if (arg) {
if (sessions_ht)
lttng_ht_destroy(sessions_ht);
- /* free the dynamically allocated opt_output_path */
free(opt_output_path);
+ free(opt_working_directory);
/* Close thread quit pipes */
utils_close_pipe(thread_quit_pipe);
}
}
+ if (opt_working_directory) {
+ ret = utils_change_working_directory(opt_working_directory);
+ if (ret) {
+ /* All errors are already logged. */
+ goto exit_options;
+ }
+ }
+
sessiond_trace_chunk_registry = sessiond_trace_chunk_registry_create();
if (!sessiond_trace_chunk_registry) {
ERR("Failed to initialize session daemon trace chunk registry");
{
return read_proc_meminfo_field(PROC_MEMINFO_MEMTOTAL_LINE, value);
}
+
+LTTNG_HIDDEN
+int utils_change_working_directory(const char *path)
+{
+ int ret;
+
+ assert(path);
+
+ DBG("Changing working directory to \"%s\"", path);
+ ret = chdir(path);
+ if (ret) {
+ PERROR("Failed to change working directory to \"%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 \"%s\" is not writable", path);
+ } else {
+ PERROR("Failed to check if working directory \"%s\" is writable",
+ path);
+ }
+ }
+
+end:
+ return ret;
+}
int utils_show_help(int section, const char *page_name, const char *help_msg);
int utils_get_memory_available(size_t *value);
int utils_get_memory_total(size_t *value);
+int utils_change_working_directory(const char *path);
#endif /* _COMMON_UTILS_H */