Fix: auto load session in the auto/ directory
authorDavid Goulet <dgoulet@efficios.com>
Thu, 26 Jun 2014 19:41:42 +0000 (15:41 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Wed, 9 Jul 2014 17:39:38 +0000 (13:39 -0400)
The session daemon will now automatically load sessions from the auto/
directory in the session config path. Both system config path and home
config path are used. The session is only loaded if the UID of the
session file matches the UID of the session daemon (or root).

For that, a autoload parameter has been added to the
config_load_session() function to indicate that we want to auto load
sessions.

Fixes #812

Signed-off-by: David Goulet <dgoulet@efficios.com>
src/bin/lttng-sessiond/load-session-thread.c
src/bin/lttng/commands/load.c
src/common/config/config.c
src/common/config/config.h
src/common/defaults.h
src/lib/lttng-ctl/load.c

index 845948e7cd559363395efd82e05c283145b7e21c..38fa1f616104175c2f31fd56807764a3a36b3b37 100644 (file)
@@ -95,7 +95,8 @@ void *thread_load_session(void *data)
                goto end;
        }
 
-       ret = config_load_session(info->path, NULL, 0);
+       /* Override existing session and autoload also. */
+       ret = config_load_session(info->path, NULL, 1, 1);
        if (ret) {
                ERR("Session load failed: %s", error_get_str(ret));
        }
index e732baa80e9f1200fc4e1994f94533d77c718b6e..20d984389e01d080f5e30ef5cb4ccee8b26348b5 100644 (file)
@@ -102,7 +102,7 @@ int cmd_load(int argc, const char **argv)
                }
        }
 
-       ret = config_load_session(opt_input_path, session_name, opt_force);
+       ret = config_load_session(opt_input_path, session_name, opt_force, 0);
        if (ret) {
                ERR("%s", lttng_strerror(ret));
                ret = -ret;
index c9777d63f673728313c9f1d0fb0d8cc783e6af21..b9c4021087bfbecd5af5aea619f162e92fc31d5a 100644 (file)
@@ -2504,9 +2504,43 @@ end:
        return ret;
 }
 
+/*
+ * Validate that the given path's credentials and the current process have the
+ * same UID. If so, return 0 else return 1 if it does NOT match.
+ */
+static int validate_path_creds(const char *path)
+{
+       int ret, uid = getuid();
+       struct stat buf;
+
+       assert(path);
+
+       if (uid != 0) {
+               goto valid;
+       }
+
+       ret = stat(path, &buf);
+       if (ret < 0) {
+               if (errno != ENOENT) {
+                       PERROR("stat");
+               }
+               ret = -LTTNG_ERR_INVALID;
+               goto valid;
+       }
+
+       if (buf.st_uid != uid) {
+               goto invalid;
+       }
+
+valid:
+       return 0;
+invalid:
+       return 1;
+}
+
 LTTNG_HIDDEN
 int config_load_session(const char *path, const char *session_name,
-               int override)
+               int override, unsigned int autoload)
 {
        int ret;
        struct session_config_validation_ctx validation_ctx = { 0 };
@@ -2517,34 +2551,56 @@ int config_load_session(const char *path, const char *session_name,
        }
 
        if (!path) {
+               char *home_path;
+               const char *sys_path;
+
+               /*
+                * Try system session configuration path. Ignore error here so we can
+                * continue loading the home sessions. The above call should print an
+                * error to inform the user.
+                */
+               if (autoload) {
+                       sys_path = DEFAULT_SESSION_SYSTEM_CONFIGPATH "/"
+                               DEFAULT_SESSION_CONFIG_AUTOLOAD;
+               } else {
+                       sys_path = DEFAULT_SESSION_HOME_CONFIGPATH;
+               }
+
+               ret = validate_path_creds(sys_path);
+               if (!ret && autoload) {
+                       (void) load_session_from_path(sys_path, session_name,
+                                       &validation_ctx, override);
+               }
+
                /* Try home path */
-               char *home_path = utils_get_home_dir();
+               home_path = utils_get_home_dir();
                if (home_path) {
-                       char *path;
+                       char path[PATH_MAX];
 
-                       ret = asprintf(&path, DEFAULT_SESSION_HOME_CONFIGPATH,
-                               home_path);
+                       if (autoload) {
+                               ret = snprintf(path, sizeof(path),
+                                               DEFAULT_SESSION_HOME_CONFIGPATH "/"
+                                               DEFAULT_SESSION_CONFIG_AUTOLOAD, home_path);
+                       } else {
+                               ret = snprintf(path, sizeof(path),
+                                               DEFAULT_SESSION_HOME_CONFIGPATH, home_path);
+                       }
                        if (ret < 0) {
                                goto end;
                        }
 
+                       ret = validate_path_creds(path);
+                       if (ret && autoload) {
+                               ret = 0;
+                               goto end;
+                       }
+
                        ret = load_session_from_path(path, session_name,
                                &validation_ctx, override);
                        if (!ret || (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
                                /* Session found or an error occured */
-                               free(path);
                                goto end;
                        }
-
-                       free(path);
-               }
-
-               /* Try system session configuration path */
-               ret = load_session_from_path(DEFAULT_SESSION_SYSTEM_CONFIGPATH,
-                       session_name, &validation_ctx, override);
-               if (!ret || (ret && ret != -LTTNG_ERR_LOAD_SESSION_NOENT)) {
-                       /* Session found or an error occured */
-                       goto end;
                }
        } else {
                ret = access(path, F_OK);
index a11eec9c74a3ac6722ecfc899c2291b15693e41a..9a5671ab0c138cb9d7d76d95225f1af524810696 100644 (file)
@@ -199,12 +199,13 @@ int config_writer_write_element_string(struct config_writer *writer,
  * sessions from path if NULL.
  *
  * override Override current session configuration if it exists.
+ * autoload Tell to load the auto session(s).
  *
  * Returns zero if the session could be loaded successfully. Returns
  * a negative LTTNG_ERR code on error.
  */
 LTTNG_HIDDEN
 int config_load_session(const char *path, const char *session_name,
-               int override);
+               int override, unsigned int autoload);
 
 #endif /* _CONFIG_H */
index b49bf4b33cd6fb5b0fbeb7c13f1101391157d7f9..8107d973ba2358e6ae13017876aa3b2fbf5ffa78 100644 (file)
 
 /* Default session configuration file path */
 #define DEFAULT_SESSION_PATH                    "sessions"
+/* Auto load session in that directory. */
+#define DEFAULT_SESSION_CONFIG_AUTOLOAD         "auto"
 #define DEFAULT_SESSION_HOME_CONFIGPATH         DEFAULT_LTTNG_HOME_RUNDIR "/" \
        DEFAULT_SESSION_PATH
 #define DEFAULT_SESSION_SYSTEM_CONFIGPATH       DEFAULT_SYSTEM_CONFIGPATH "/" \
index 3811981ef007f6c906ff326fc5b27ddc2d09a093..21619a6839c83106b46692c6f19ddd9ee900b9e4 100644 (file)
@@ -166,7 +166,7 @@ int lttng_load_session(struct lttng_load_session_attr *attr)
        }
 
        ret = config_load_session(attr->input_url, attr->session_name,
-                       attr->overwrite);
+                       attr->overwrite, 0);
 
 end:
        return ret;
This page took 0.041604 seconds and 4 git commands to generate.