X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fcommon%2Futils.c;h=1d07cb31cc01511aedb781124de609277410075d;hp=b640b950e7e3d88ac95a966d60e76acaa1a88121;hb=c9d424071aa532aa287fa86cfe02edbeded50e7b;hpb=04135dbd279d3d0de6a6527b9b933e59de5cb495 diff --git a/src/common/utils.c b/src/common/utils.c index b640b950e..1d07cb31c 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -468,6 +469,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. * @@ -888,7 +927,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) @@ -896,6 +935,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. *