From feb0f3e5900ff58202ea0c3f227de3c1b8291952 Mon Sep 17 00:00:00 2001 From: Amit Margalit Date: Thu, 13 Jun 2013 12:35:13 +0300 Subject: [PATCH] Using LTTNG_HOME environment variable if exists, with fallback to HOME --- doc/man/lttng.1 | 4 ++++ src/bin/lttng-relayd/utils.c | 10 +--------- src/bin/lttng-sessiond/main.c | 2 +- src/bin/lttng-sessiond/utils.c | 10 ---------- src/bin/lttng/commands/create.c | 2 +- src/bin/lttng/conf.c | 13 +++---------- src/bin/lttng/conf.h | 1 - src/bin/lttng/utils.c | 3 ++- src/common/defaults.h | 2 ++ src/common/utils.c | 15 +++++++++++++++ src/common/utils.h | 1 + src/lib/lttng-ctl/lttng-ctl.c | 5 +++-- 12 files changed, 33 insertions(+), 35 deletions(-) diff --git a/doc/man/lttng.1 b/doc/man/lttng.1 index f472d389d..26fd44a69 100644 --- a/doc/man/lttng.1 +++ b/doc/man/lttng.1 @@ -214,6 +214,10 @@ automatically created having this form: 'auto-yyyymmdd-hhmmss'. If no \fB\-o, \-\-output\fP is specified, the traces will be written in $HOME/lttng-traces. + +The $HOME environment variable can be overridden by defining the environment +variable LTTNG_HOME. This is useful when the user running the commands has +a non-writeable home directory. .fi .B OPTIONS: diff --git a/src/bin/lttng-relayd/utils.c b/src/bin/lttng-relayd/utils.c index ad13d3277..392fab6a1 100644 --- a/src/bin/lttng-relayd/utils.c +++ b/src/bin/lttng-relayd/utils.c @@ -29,14 +29,6 @@ #include "lttng-relayd.h" #include "utils.h" -/* - * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer. - */ -static char *get_default_path(void) -{ - return getenv("HOME"); -} - static char *create_output_path_auto(char *path_name) { int ret; @@ -44,7 +36,7 @@ static char *create_output_path_auto(char *path_name) char *alloc_path = NULL; char *default_path; - default_path = get_default_path(); + default_path = utils_get_home_dir(); if (default_path == NULL) { ERR("Home path not found.\n \ Please specify an output path using -o, --output PATH"); diff --git a/src/bin/lttng-sessiond/main.c b/src/bin/lttng-sessiond/main.c index 55751e85b..f5a4db4dc 100644 --- a/src/bin/lttng-sessiond/main.c +++ b/src/bin/lttng-sessiond/main.c @@ -4256,7 +4256,7 @@ int main(int argc, char **argv) DBG2("Kernel consumer cmd path: %s", kconsumer_data.cmd_unix_sock_path); } else { - home_path = get_home_dir(); + home_path = utils_get_home_dir(); if (home_path == NULL) { /* TODO: Add --socket PATH option */ ERR("Can't get HOME directory for sockets creation."); diff --git a/src/bin/lttng-sessiond/utils.c b/src/bin/lttng-sessiond/utils.c index 358e407e9..85a1f0203 100644 --- a/src/bin/lttng-sessiond/utils.c +++ b/src/bin/lttng-sessiond/utils.c @@ -49,16 +49,6 @@ int notify_thread_pipe(int wpipe) return ret; } -/* - * Return pointer to home directory path using the env variable HOME. - * - * No home, NULL is returned. - */ -const char *get_home_dir(void) -{ - return ((const char *) getenv("HOME")); -} - void ht_cleanup_push(struct lttng_ht *ht) { int ret; diff --git a/src/bin/lttng/commands/create.c b/src/bin/lttng/commands/create.c index 805cdb1d2..ddd2983e2 100644 --- a/src/bin/lttng/commands/create.c +++ b/src/bin/lttng/commands/create.c @@ -250,7 +250,7 @@ static int create_session(void) print_str_url = url; } else { /* Auto output path */ - alloc_path = config_get_default_path(); + alloc_path = utils_get_home_dir(); if (alloc_path == NULL) { ERR("HOME path not found.\n \ Please specify an output path using -o, --output PATH"); diff --git a/src/bin/lttng/conf.c b/src/bin/lttng/conf.c index b6632fcb1..5a0da9d33 100644 --- a/src/bin/lttng/conf.c +++ b/src/bin/lttng/conf.c @@ -25,6 +25,7 @@ #include #include +#include #include "conf.h" @@ -122,14 +123,6 @@ end: return ret; } -/* - * Returns the HOME directory path. Caller MUST NOT free(3) the return pointer. - */ -char *config_get_default_path(void) -{ - return getenv("HOME"); -} - /* * Destroys directory config and file config. */ @@ -161,7 +154,7 @@ end: */ void config_destroy_default(void) { - char *path = config_get_default_path(); + char *path = utils_get_home_dir(); if (path == NULL) { return; } @@ -277,7 +270,7 @@ int config_init(char *session_name) int ret; char *path; - path = config_get_default_path(); + path = utils_get_home_dir(); if (path == NULL) { ret = -1; goto error; diff --git a/src/bin/lttng/conf.h b/src/bin/lttng/conf.h index 2cb04b0c7..3bed59c2a 100644 --- a/src/bin/lttng/conf.h +++ b/src/bin/lttng/conf.h @@ -25,7 +25,6 @@ void config_destroy_default(void); int config_exists(const char *path); int config_init(char *path); int config_add_session_name(char *path, char *name); -char *config_get_default_path(void); /* Must free() the return pointer */ char *config_read_session_name(char *path); diff --git a/src/bin/lttng/utils.c b/src/bin/lttng/utils.c index 94c4527d9..6041655dd 100644 --- a/src/bin/lttng/utils.c +++ b/src/bin/lttng/utils.c @@ -21,6 +21,7 @@ #include #include +#include #include "conf.h" #include "utils.h" @@ -36,7 +37,7 @@ char *get_session_name(void) char *path, *session_name = NULL; /* Get path to config file */ - path = config_get_default_path(); + path = utils_get_home_dir(); if (path == NULL) { goto error; } diff --git a/src/common/defaults.h b/src/common/defaults.h index 66bb972fd..c04063488 100644 --- a/src/common/defaults.h +++ b/src/common/defaults.h @@ -77,6 +77,8 @@ #define DEFAULT_USTCONSUMERD32_ERR_SOCK_PATH DEFAULT_USTCONSUMERD32_PATH "/error" /* Default lttng run directory */ +#define DEFAULT_LTTNG_HOME_ENV_VAR "LTTNG_HOME" +#define DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR "HOME" #define DEFAULT_LTTNG_RUNDIR "/var/run/lttng" #define DEFAULT_LTTNG_HOME_RUNDIR "%s/.lttng" #define DEFAULT_LTTNG_SESSIOND_PIDFILE "lttng-sessiond.pid" diff --git a/src/common/utils.c b/src/common/utils.c index 3e659a1c8..f253e797f 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -32,6 +32,7 @@ #include #include "utils.h" +#include "defaults.h" /* * Return the realpath(3) of the path even if the last directory token does not @@ -580,3 +581,17 @@ int utils_get_count_order_u32(uint32_t x) return fls_u32(x - 1); } + +/** + * Obtain the value of LTTNG_HOME environment variable, if exists. + * Otherwise returns the value of HOME. + */ +char *utils_get_home_dir(void) +{ + char *val = NULL; + val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR); + if (val != NULL) { + return val; + } + return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); +} diff --git a/src/common/utils.h b/src/common/utils.h index 083acef93..9e6fb37f0 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -40,5 +40,6 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size, uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count); int utils_parse_size_suffix(char *str, uint64_t *size); int utils_get_count_order_u32(uint32_t x); +char *utils_get_home_dir(void); #endif /* _COMMON_UTILS_H */ diff --git a/src/lib/lttng-ctl/lttng-ctl.c b/src/lib/lttng-ctl/lttng-ctl.c index 6c8ad411a..1b9f3a8ab 100644 --- a/src/lib/lttng-ctl/lttng-ctl.c +++ b/src/lib/lttng-ctl/lttng-ctl.c @@ -32,6 +32,7 @@ #include #include #include +#include #include #include "filter/filter-ast.h" @@ -485,7 +486,7 @@ static int set_session_daemon_path(void) * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) */ ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path), - DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME")); + DEFAULT_HOME_CLIENT_UNIX_SOCK, utils_get_home_dir()); if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) { goto error; } @@ -1543,7 +1544,7 @@ static int set_health_socket_path(void) * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small; * With GNU C >= 2.1, snprintf returns the required size (excluding closing null) */ - home = getenv("HOME"); + home = utils_get_home_dir(); if (home == NULL) { /* Fallback in /tmp .. */ home = "/tmp"; -- 2.34.1