Fix: make lttng expand path for trace output opt
authorDavid Goulet <dgoulet@efficios.com>
Thu, 5 Apr 2012 15:39:59 +0000 (11:39 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Thu, 5 Apr 2012 15:58:01 +0000 (11:58 -0400)
lttng create --output was passing the path string to the session daemon
and thus, for relative path like './mytraces', it was created in the
current directory of the session daemon.

Now lttng command line uses the realpath(3) of the --output string and
denies creation if multiple level of directory does not exist (Ex:
/tmp/foo/bar/chap, if foo/ does not exist, it is refused).

Directory creation still occurs on the session daemon side.

Reported-by: Ettore Del Negro <ettore@ettoredelnegro.me>
Signed-off-by: David Goulet <dgoulet@efficios.com>
src/bin/lttng/commands/create.c
src/bin/lttng/utils.c
src/bin/lttng/utils.h

index b1f3e8a777a0420ca4f5de66c7c9032118780015..2778ef553cdd2a24c27567f780be0b1c950678c6 100644 (file)
@@ -115,7 +115,11 @@ static int create_session()
                        goto error;
                }
        } else {
-               traces_path = opt_output_path;
+               traces_path = expand_full_path(opt_output_path);
+               if (traces_path == NULL) {
+                       ret = CMD_ERROR;
+                       goto error;
+               }
        }
 
        ret = lttng_create_session(session_name, traces_path);
index b982bd5936149ab7667e64176a9f0c499dfd0207..8ae984836595d1271b0a38a8809e2e5f5e0d88b9 100644 (file)
 #define _GNU_SOURCE
 #include <stdlib.h>
 #include <ctype.h>
+#include <limits.h>
 
 #include <common/error.h>
 
 #include "conf.h"
 #include "utils.h"
 
+/*
+ * Return the realpath(3) of the path even if the last directory token does not
+ * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
+ * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
+ * fails if the end point directory does not exist.
+ */
+char *expand_full_path(const char *path)
+{
+       const char *end_path = path;
+       char *next, *cut_path, *expanded_path;
+
+       /* Find last token delimited by '/' */
+       while ((next = strpbrk(end_path + 1, "/"))) {
+               end_path = next;
+       }
+
+       /* Cut last token from original path */
+       cut_path = strndup(path, end_path - path);
+
+       expanded_path = malloc(PATH_MAX);
+       if (expanded_path == NULL) {
+               goto error;
+       }
+
+       expanded_path = realpath((char *)cut_path, expanded_path);
+       if (expanded_path == NULL) {
+               switch (errno) {
+               case ENOENT:
+                       ERR("%s: No such file or directory", cut_path);
+                       break;
+               default:
+                       perror("realpath");
+                       break;
+               }
+               goto error;
+       }
+
+       /* Add end part to expanded path */
+       strcat(expanded_path, end_path);
+
+       free(cut_path);
+       return expanded_path;
+
+error:
+       free(cut_path);
+       return NULL;
+}
+
 /*
  *  get_session_name
  *
index 9c51b67e341a73b461e698e9ca52d57677a35270..a430b8bdf3e9577ebf5bbcb1e176f519cc9c50fc 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <popt.h>
 
+char *expand_full_path(const char *path);
 char *get_config_file_path(void);
 char *get_session_name(void);
 int set_session_name(char *name);
This page took 0.037568 seconds and 4 git commands to generate.