Implement --shm-path option for UST sessions (per-uid channels)
[lttng-tools.git] / src / common / utils.c
index b640b950e7e3d88ac95a966d60e76acaa1a88121..9a53330004931408df017fb4b2ebbd87ad31a61e 100644 (file)
@@ -18,6 +18,7 @@
  */
 
 #define _GNU_SOURCE
+#define _LGPL_SOURCE
 #include <assert.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <inttypes.h>
 #include <grp.h>
 #include <pwd.h>
+#include <sys/file.h>
 
 #include <common/common.h>
 #include <common/runas.h>
+#include <common/compat/getenv.h>
 
 #include "utils.h"
 #include "defaults.h"
@@ -89,6 +92,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 +149,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 +226,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 +246,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);
@@ -460,14 +478,58 @@ int utils_create_pid_file(pid_t pid, const char *filepath)
        ret = fprintf(fp, "%d\n", pid);
        if (ret < 0) {
                PERROR("fprintf pid file");
+               goto error;
        }
 
-       fclose(fp);
+       if (fclose(fp)) {
+               PERROR("fclose");
+       }
        DBG("Pid %d written in file %s", pid, filepath);
+       ret = 0;
 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) {
+               ERR("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.
  *
@@ -824,11 +886,11 @@ char *utils_get_home_dir(void)
        char *val = NULL;
        struct passwd *pwd;
 
-       val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR);
+       val = lttng_secure_getenv(DEFAULT_LTTNG_HOME_ENV_VAR);
        if (val != NULL) {
                goto end;
        }
-       val = getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR);
+       val = lttng_secure_getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR);
        if (val != NULL) {
                goto end;
        }
@@ -888,12 +950,22 @@ 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)
 {
-       return getenv(DEFAULT_LTTNG_KMOD_PROBES);
+       return lttng_secure_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 lttng_secure_getenv(DEFAULT_LTTNG_EXTRA_KMOD_PROBES);
 }
 
 /*
This page took 0.0252 seconds and 4 git commands to generate.