Fix: sessiond: occasional badfd error on repeated SIGTERM
[lttng-tools.git] / src / common / utils.c
index 893122905bd44ca75b9fa3836cdf9098e142dfff..69d0ab16ef7a26cb542b8ad6c6f96b95e1141fd5 100644 (file)
 #include <common/compat/getenv.h>
 #include <common/compat/string.h>
 #include <common/compat/dirent.h>
+#include <common/dynamic-buffer.h>
 #include <lttng/constant.h>
 
 #include "utils.h"
 #include "defaults.h"
+#include "time.h"
 
 /*
  * Return a partial realpath(3) of the path even if the full path does not
@@ -430,6 +432,7 @@ void utils_close_pipe(int *src)
                if (ret) {
                        PERROR("close pipe");
                }
+               src[i] = -1;
        }
 }
 
@@ -528,7 +531,7 @@ int utils_create_lock_file(const char *filepath)
                S_IRGRP | S_IWGRP);
        if (fd < 0) {
                PERROR("open lock file %s", filepath);
-               ret = -1;
+               fd = -1;
                goto error;
        }
 
@@ -1202,26 +1205,6 @@ 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 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);
-}
-
 /*
  * With the given format, fill dst with the time of len maximum siz.
  *
@@ -1250,24 +1233,77 @@ size_t utils_get_current_time_str(const char *format, char *dst, size_t len)
 }
 
 /*
- * Return the group ID matching name, else 0 if it cannot be found.
+ * Return 0 on success and set *gid to the group_ID matching the passed name.
+ * Else -1 if it cannot be found or an error occurred.
  */
 LTTNG_HIDDEN
-gid_t utils_get_group_id(const char *name)
+int utils_get_group_id(const char *name, bool warn, gid_t *gid)
 {
-       struct group *grp;
+       static volatile int warn_once;
+       int ret;
+       long sys_len;
+       size_t len;
+       struct group grp;
+       struct group *result;
+       struct lttng_dynamic_buffer buffer;
+
+       /* Get the system limit, if it exists. */
+       sys_len = sysconf(_SC_GETGR_R_SIZE_MAX);
+       if (sys_len == -1) {
+               len = 1024;
+       } else {
+               len = (size_t) sys_len;
+       }
+
+       lttng_dynamic_buffer_init(&buffer);
+       ret = lttng_dynamic_buffer_set_size(&buffer, len);
+       if (ret) {
+               ERR("Failed to allocate group info buffer");
+               ret = -1;
+               goto error;
+       }
 
-       grp = getgrnam(name);
-       if (!grp) {
-               static volatile int warn_once;
+       while ((ret = getgrnam_r(name, &grp, buffer.data, buffer.size, &result)) == ERANGE) {
+               const size_t new_len = 2 * buffer.size;
 
-               if (!warn_once) {
-                       WARN("No tracing group detected");
-                       warn_once = 1;
+               /* Buffer is not big enough, increase its size. */
+               if (new_len < buffer.size) {
+                       ERR("Group info buffer size overflow");
+                       ret = -1;
+                       goto error;
                }
-               return 0;
+
+               ret = lttng_dynamic_buffer_set_size(&buffer, new_len);
+               if (ret) {
+                       ERR("Failed to grow group info buffer to %zu bytes",
+                                       new_len);
+                       ret = -1;
+                       goto error;
+               }
+       }
+       if (ret) {
+               PERROR("Failed to get group file entry for group name \"%s\"",
+                               name);
+               ret = -1;
+               goto error;
+       }
+
+       /* Group not found. */
+       if (!result) {
+               ret = -1;
+               goto error;
        }
-       return grp->gr_gid;
+
+       *gid = result->gr_gid;
+       ret = 0;
+
+error:
+       if (ret && warn && !warn_once) {
+               WARN("No tracing group detected");
+               warn_once = 1;
+       }
+       lttng_dynamic_buffer_reset(&buffer);
+       return ret;
 }
 
 /*
@@ -1402,15 +1438,17 @@ LTTNG_HIDDEN
 int utils_truncate_stream_file(int fd, off_t length)
 {
        int ret;
+       off_t lseek_ret;
 
        ret = ftruncate(fd, length);
        if (ret < 0) {
                PERROR("ftruncate");
                goto end;
        }
-       ret = lseek(fd, length, SEEK_SET);
-       if (ret < 0) {
+       lseek_ret = lseek(fd, length, SEEK_SET);
+       if (lseek_ret < 0) {
                PERROR("lseek");
+               ret = -1;
                goto end;
        }
 end:
@@ -1458,3 +1496,38 @@ int utils_show_help(int section, const char *page_name,
 end:
        return ret;
 }
+
+LTTNG_HIDDEN
+int timespec_to_ms(struct timespec ts, unsigned long *ms)
+{
+       unsigned long res, remain_ms;
+
+       if (ts.tv_sec > ULONG_MAX / MSEC_PER_SEC) {
+               errno = EOVERFLOW;
+               return -1;      /* multiplication overflow */
+       }
+       res = ts.tv_sec * MSEC_PER_SEC;
+       remain_ms = ULONG_MAX - res;
+       if (ts.tv_nsec / NSEC_PER_MSEC > remain_ms) {
+               errno = EOVERFLOW;
+               return -1;      /* addition overflow */
+       }
+       res += ts.tv_nsec / NSEC_PER_MSEC;
+       *ms = res;
+       return 0;
+}
+
+LTTNG_HIDDEN
+struct timespec timespec_abs_diff(struct timespec t1, struct timespec t2)
+{
+       uint64_t ts1 = (uint64_t) t1.tv_sec * (uint64_t) NSEC_PER_SEC +
+                       (uint64_t) t1.tv_nsec;
+       uint64_t ts2 = (uint64_t) t2.tv_sec * (uint64_t) NSEC_PER_SEC +
+                       (uint64_t) t2.tv_nsec;
+       uint64_t diff = max(ts1, ts2) - min(ts1, ts2);
+       struct timespec res;
+
+       res.tv_sec = diff / (uint64_t) NSEC_PER_SEC;
+       res.tv_nsec = diff % (uint64_t) NSEC_PER_SEC;
+       return res;
+}
This page took 0.025422 seconds and 4 git commands to generate.