X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=e7dccb7b7490cb1c1392461304e70ea7cbf5d390;hp=815965bc4ba25256de830f0794cf357f2150db70;hb=ffb0b851ac47de854df910d226dfd150b32e8bfe;hpb=5983a922b5e591a0fd90800e482e1ab8b89a4281 diff --git a/src/common/utils.c b/src/common/utils.c index 815965bc4..e7dccb7b7 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -18,6 +18,7 @@ */ #define _GNU_SOURCE +#define _LGPL_SOURCE #include #include #include @@ -30,6 +31,7 @@ #include #include #include +#include #include #include @@ -89,6 +91,10 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) /* Cut the part we will be trying to resolve */ cut_path = strndup(path, next - path); + if (cut_path == NULL) { + PERROR("strndup"); + goto error; + } /* Try to resolve this part */ try_path = realpath((char *)cut_path, NULL); @@ -142,6 +148,10 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size) * path are pointers for the same memory space */ cut_path = strdup(prev); + if (cut_path == NULL) { + PERROR("strdup"); + goto error; + } /* Concatenate the strings */ snprintf(resolved_path, size, "%s%s", try_path_prev, cut_path); @@ -215,7 +225,10 @@ char *utils_expand_path(const char *path) /* We prepare the start_path not containing it */ start_path = strndup(absolute_path, next - absolute_path); - + if (!start_path) { + PERROR("strndup"); + goto error; + } /* And we concatenate it with the part after this string */ snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 2); @@ -232,6 +245,10 @@ char *utils_expand_path(const char *path) /* Then we prepare the start_path not containing it */ start_path = strndup(absolute_path, previous - absolute_path); + if (!start_path) { + PERROR("strndup"); + goto error; + } /* And we concatenate it with the part after the '/../' */ snprintf(absolute_path, PATH_MAX, "%s%s", start_path, next + 4); @@ -468,6 +485,46 @@ error: return ret; } +/* + * Create lock file to the given path and filename. + * Returns the associated file descriptor, -1 on error. + */ +LTTNG_HIDDEN +int utils_create_lock_file(const char *filepath) +{ + int ret; + int fd; + + assert(filepath); + + fd = open(filepath, O_CREAT, + O_WRONLY | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (fd < 0) { + PERROR("open lock file %s", filepath); + ret = -1; + goto error; + } + + /* + * Attempt to lock the file. If this fails, there is + * already a process using the same lock file running + * and we should exit. + */ + ret = flock(fd, LOCK_EX | LOCK_NB); + if (ret) { + WARN("Could not get lock file %s, another instance is running.", + filepath); + if (close(fd)) { + PERROR("close lock file"); + } + fd = ret; + goto error; + } + +error: + return fd; +} + /* * Recursively create directory using the given path and mode. * @@ -822,11 +879,28 @@ LTTNG_HIDDEN char *utils_get_home_dir(void) { char *val = NULL; + struct passwd *pwd; + val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR); if (val != NULL) { - return val; + goto end; + } + val = getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); + if (val != NULL) { + goto end; } - return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); + + /* Fallback on the password file entry. */ + pwd = getpwuid(getuid()); + if (!pwd) { + goto end; + } + val = pwd->pw_dir; + + DBG3("Home directory is '%s'", val); + +end: + return val; } /** @@ -869,6 +943,26 @@ end: return home_dir; } +/* + * Obtain the value of LTTNG_KMOD_PROBES environment variable, if exists. + * Otherwise returns NULL. + */ +LTTNG_HIDDEN +char *utils_get_kmod_probes_list(void) +{ + return getenv(DEFAULT_LTTNG_KMOD_PROBES); +} + +/* + * Obtain the value of LTTNG_EXTRA_KMOD_PROBES environment variable, if + * exists. Otherwise returns NULL. + */ +LTTNG_HIDDEN +char *utils_get_extra_kmod_probes_list(void) +{ + return getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES); +} + /* * With the given format, fill dst with the time of len maximum siz. * @@ -889,7 +983,7 @@ size_t utils_get_current_time_str(const char *format, char *dst, size_t len) timeinfo = localtime(&rawtime); ret = strftime(dst, len, format, timeinfo); if (ret == 0) { - ERR("Unable to strftime with format %s at dst %p of len %lu", format, + ERR("Unable to strftime with format %s at dst %p of len %zu", format, dst, len); }