X-Git-Url: http://git.efficios.com/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=4e78767dfb46d2c18b47670174a21bbbeafbdfb3;hp=bf93386c46147eca75e33c59a13b9f8214f49ba4;hb=6c1c0768320135c6936c371b09731851b508c023;hpb=fbb9748b6dc509542c4a82960da36d7b03a3a66b diff --git a/src/common/utils.c b/src/common/utils.c index bf93386c4..4e78767df 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 @@ -468,6 +470,44 @@ 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); + close(fd); + fd = ret; + goto error; + } + +error: + return fd; +} + /* * Recursively create directory using the given path and mode. * @@ -822,11 +862,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; + } + + /* Fallback on the password file entry. */ + pwd = getpwuid(getuid()); + if (!pwd) { + goto end; } - return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR); + val = pwd->pw_dir; + + DBG3("Home directory is '%s'", val); + +end: + return val; } /** @@ -871,7 +928,7 @@ end: /* * Obtain the value of LTTNG_KMOD_PROBES environment variable, if exists. - * Otherwise returns an empty string. + * Otherwise returns NULL. */ LTTNG_HIDDEN char *utils_get_kmod_probes_list(void) @@ -879,6 +936,16 @@ 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. *