sessiond: Add --extra-kmod-probes option
[lttng-tools.git] / src / common / utils.c
index 815965bc4ba25256de830f0794cf357f2150db70..1d07cb31cc01511aedb781124de609277410075d 100644 (file)
@@ -30,6 +30,7 @@
 #include <inttypes.h>
 #include <grp.h>
 #include <pwd.h>
+#include <sys/file.h>
 
 #include <common/common.h>
 #include <common/runas.h>
@@ -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.
  *
@@ -822,11 +861,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 +925,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 +965,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);
        }
 
This page took 0.02483 seconds and 4 git commands to generate.