Documentation of live usage and protocol
[lttng-tools.git] / src / common / utils.c
index 38f78a7e8e7d40cd6fcebefd297e5bb240c52aaf..dd9967312aca1be20253df8fd881c485858d0e6f 100644 (file)
@@ -22,8 +22,8 @@
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <unistd.h>
 #include <inttypes.h>
 #include <regex.h>
@@ -32,6 +32,7 @@
 #include <common/runas.h>
 
 #include "utils.h"
+#include "defaults.h"
 
 /*
  * Return the realpath(3) of the path even if the last directory token does not
@@ -141,6 +142,48 @@ error:
        return ret;
 }
 
+/*
+ * Create pipe and set fd flags to FD_CLOEXEC and O_NONBLOCK.
+ *
+ * Make sure the pipe opened by this function are closed at some point. Use
+ * utils_close_pipe(). Using pipe() and fcntl rather than pipe2() to
+ * support OSes other than Linux 2.6.23+.
+ */
+LTTNG_HIDDEN
+int utils_create_pipe_cloexec_nonblock(int *dst)
+{
+       int ret, i;
+
+       if (dst == NULL) {
+               return -1;
+       }
+
+       ret = utils_create_pipe(dst);
+       if (ret < 0) {
+               goto error;
+       }
+
+       for (i = 0; i < 2; i++) {
+               ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
+               if (ret < 0) {
+                       PERROR("fcntl pipe cloexec");
+                       goto error;
+               }
+               /*
+                * Note: we override any flag that could have been
+                * previously set on the fd.
+                */
+               ret = fcntl(dst[i], F_SETFL, O_NONBLOCK);
+               if (ret < 0) {
+                       PERROR("fcntl pipe nonblock");
+                       goto error;
+               }
+       }
+
+error:
+       return ret;
+}
+
 /*
  * Close both read and write side of the pipe.
  */
@@ -248,7 +291,6 @@ LTTNG_HIDDEN
 int utils_mkdir_recursive(const char *path, mode_t mode)
 {
        char *p, tmp[PATH_MAX];
-       struct stat statbuf;
        size_t len;
        int ret;
 
@@ -276,15 +318,12 @@ int utils_mkdir_recursive(const char *path, mode_t mode)
                                ret = -1;
                                goto error;
                        }
-                       ret = stat(tmp, &statbuf);
+                       ret = mkdir(tmp, mode);
                        if (ret < 0) {
-                               ret = mkdir(tmp, mode);
-                               if (ret < 0) {
-                                       if (errno != EEXIST) {
-                                               PERROR("mkdir recursive");
-                                               ret = -errno;
-                                               goto error;
-                                       }
+                               if (errno != EEXIST) {
+                                       PERROR("mkdir recursive");
+                                       ret = -errno;
+                                       goto error;
                                }
                        }
                        *p = '/';
@@ -311,11 +350,12 @@ error:
  * Return 0 on success or else a negative value.
  */
 LTTNG_HIDDEN
-int utils_create_stream_file(char *path_name, char *file_name, uint64_t size,
-               uint64_t count, int uid, int gid)
+int utils_create_stream_file(const char *path_name, char *file_name, uint64_t size,
+               uint64_t count, int uid, int gid, char *suffix)
 {
        int ret, out_fd, flags, mode;
-       char full_path[PATH_MAX], *path_name_id = NULL, *path;
+       char full_path[PATH_MAX], *path_name_suffix = NULL, *path;
+       char *extra = NULL;
 
        assert(path_name);
        assert(file_name);
@@ -327,17 +367,30 @@ int utils_create_stream_file(char *path_name, char *file_name, uint64_t size,
                goto error;
        }
 
+       /* Setup extra string if suffix or/and a count is needed. */
+       if (size > 0 && suffix) {
+               ret = asprintf(&extra, "_%" PRIu64 "%s", count, suffix);
+       } else if (size > 0) {
+               ret = asprintf(&extra, "_%" PRIu64, count);
+       } else if (suffix) {
+               ret = asprintf(&extra, "%s", suffix);
+       }
+       if (ret < 0) {
+               PERROR("Allocating extra string to name");
+               goto error;
+       }
+
        /*
         * If we split the trace in multiple files, we have to add the count at the
         * end of the tracefile name
         */
-       if (size > 0) {
-               ret = asprintf(&path_name_id, "%s_%" PRIu64, full_path, count);
+       if (extra) {
+               ret = asprintf(&path_name_suffix, "%s%s", full_path, extra);
                if (ret < 0) {
-                       PERROR("Allocating path name ID");
-                       goto error;
+                       PERROR("Allocating path name with extra string");
+                       goto error_free_suffix;
                }
-               path = path_name_id;
+               path = path_name_suffix;
        } else {
                path = full_path;
        }
@@ -358,7 +411,9 @@ int utils_create_stream_file(char *path_name, char *file_name, uint64_t size,
        ret = out_fd;
 
 error_open:
-       free(path_name_id);
+       free(path_name_suffix);
+error_free_suffix:
+       free(extra);
 error:
        return ret;
 }
@@ -374,10 +429,14 @@ error:
  */
 LTTNG_HIDDEN
 int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
-               uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count)
+               uint64_t count, int uid, int gid, int out_fd, uint64_t *new_count,
+               int *stream_fd)
 {
        int ret;
 
+       assert(new_count);
+       assert(stream_fd);
+
        ret = close(out_fd);
        if (ret < 0) {
                PERROR("Closing tracefile");
@@ -390,8 +449,16 @@ int utils_rotate_stream_file(char *path_name, char *file_name, uint64_t size,
                (*new_count)++;
        }
 
-       return utils_create_stream_file(path_name, file_name, size, *new_count,
-                       uid, gid);
+       ret = utils_create_stream_file(path_name, file_name, size, *new_count,
+                       uid, gid, 0);
+       if (ret < 0) {
+               goto error;
+       }
+       *stream_fd = ret;
+
+       /* Success. */
+       ret = 0;
+
 error:
        return ret;
 }
@@ -444,6 +511,7 @@ static void regex_print_error(int errcode, regex_t *regex)
  *
  * @return 0 on success, -1 on failure.
  */
+LTTNG_HIDDEN
 int utils_parse_size_suffix(char *str, uint64_t *size)
 {
        regex_t regex;
@@ -584,3 +652,45 @@ int utils_get_count_order_u32(uint32_t x)
 
        return fls_u32(x - 1);
 }
+
+/**
+ * Obtain the value of LTTNG_HOME environment variable, if exists.
+ * Otherwise returns the value of HOME.
+ */
+LTTNG_HIDDEN
+char *utils_get_home_dir(void)
+{
+       char *val = NULL;
+       val = getenv(DEFAULT_LTTNG_HOME_ENV_VAR);
+       if (val != NULL) {
+               return val;
+       }
+       return getenv(DEFAULT_LTTNG_FALLBACK_HOME_ENV_VAR);
+}
+
+/*
+ * With the given format, fill dst with the time of len maximum siz.
+ *
+ * Return amount of bytes set in the buffer or else 0 on error.
+ */
+LTTNG_HIDDEN
+size_t utils_get_current_time_str(const char *format, char *dst, size_t len)
+{
+       size_t ret;
+       time_t rawtime;
+       struct tm *timeinfo;
+
+       assert(format);
+       assert(dst);
+
+       /* Get date and time for session path */
+       time(&rawtime);
+       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,
+                               dst, len);
+       }
+
+       return ret;
+}
This page took 0.026216 seconds and 4 git commands to generate.