Fix: agent port file is o+w when launching as root
[lttng-tools.git] / src / common / utils.cpp
index 5e116a9ab09513baa6809ed06671954c37156372..6c0fd261c7b8a4033d5eb35cc7247af509ddfca3 100644 (file)
@@ -6,36 +6,33 @@
  *
  */
 
-#include "common/macros.h"
-#include <stdint.h>
 #define _LGPL_SOURCE
 #include <ctype.h>
 #include <fcntl.h>
+#include <grp.h>
+#include <inttypes.h>
 #include <limits.h>
+#include <pwd.h>
 #include <stdlib.h>
+#include <sys/file.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <unistd.h>
-#include <inttypes.h>
-#include <grp.h>
-#include <pwd.h>
-#include <sys/file.h>
-#include <unistd.h>
 
-#include <common/common.h>
-#include <common/readwrite.h>
-#include <common/runas.h>
-#include <common/compat/getenv.h>
-#include <common/compat/string.h>
-#include <common/compat/dirent.h>
-#include <common/compat/directory-handle.h>
-#include <common/dynamic-buffer.h>
-#include <common/string-utils/format.h>
+#include <common/common.hpp>
+#include <common/compat/directory-handle.hpp>
+#include <common/compat/dirent.hpp>
+#include <common/compat/getenv.hpp>
+#include <common/compat/string.hpp>
+#include <common/dynamic-buffer.hpp>
+#include <common/readwrite.hpp>
+#include <common/runas.hpp>
+#include <common/string-utils/format.hpp>
 #include <lttng/constant.h>
 
-#include "utils.h"
-#include "defaults.h"
-#include "time.h"
+#include "defaults.hpp"
+#include "time.hpp"
+#include "utils.hpp"
 
 #define PROC_MEMINFO_PATH               "/proc/meminfo"
 #define PROC_MEMINFO_MEMAVAILABLE_LINE  "MemAvailable:"
@@ -174,9 +171,8 @@ void utils_close_pipe(int *src)
  */
 char *utils_strdupdelim(const char *begin, const char *end)
 {
-       char *str;
+       char *str = zmalloc<char>(end - begin + 1);
 
-       str = (char *) zmalloc(end - begin + 1);
        if (str == NULL) {
                PERROR("zmalloc strdupdelim");
                goto error;
@@ -216,30 +212,40 @@ end:
  */
 int utils_create_pid_file(pid_t pid, const char *filepath)
 {
-       int ret;
-       FILE *fp;
+       int ret, fd = -1;
+       FILE *fp = NULL;
 
        LTTNG_ASSERT(filepath);
 
-       fp = fopen(filepath, "w");
+       fd = open(filepath, O_CREAT | O_WRONLY, S_IRUSR |S_IWUSR | S_IRGRP | S_IROTH);
+       if (fd < 0) {
+               PERROR("open file %s", filepath);
+               ret = -1;
+               goto error;
+       }
+
+       fp = fdopen(fd, "w");
        if (fp == NULL) {
-               PERROR("open pid file %s", filepath);
+               PERROR("fdopen file %s", filepath);
                ret = -1;
+               close(fd);
                goto error;
        }
 
        ret = fprintf(fp, "%d\n", (int) pid);
        if (ret < 0) {
-               PERROR("fprintf pid file");
+               PERROR("fprintf file %s", filepath);
+               ret = -1;
                goto error;
        }
 
-       if (fclose(fp)) {
-               PERROR("fclose");
-       }
-       DBG("Pid %d written in file %s", (int) pid, filepath);
+       DBG("'%d' written in file %s", (int) pid, filepath);
        ret = 0;
+
 error:
+       if (fp && fclose(fp)) {
+               PERROR("fclose file %s", filepath);
+       }
        return ret;
 }
 
@@ -787,7 +793,7 @@ char *utils_get_user_home_dir(uid_t uid)
                goto end;
        }
 retry:
-       buf = (char *) zmalloc(buflen);
+       buf = zmalloc<char>(buflen);
        if (!buf) {
                goto end;
        }
@@ -937,7 +943,7 @@ char *utils_generate_optstring(const struct option *long_options,
                string_len += long_options[i].has_arg ? 1 : 0;
        }
 
-       optstring = (char *) zmalloc(string_len);
+       optstring = zmalloc<char>(string_len);
        if (!optstring) {
                goto end;
        }
@@ -1162,7 +1168,7 @@ enum lttng_error_code utils_user_id_from_name(const char *user_name, uid_t *uid)
                buflen = FALLBACK_USER_BUFLEN;
        }
 
-       buf = (char *) zmalloc(buflen);
+       buf = zmalloc<char>(buflen);
        if (!buf) {
                ret_val = LTTNG_ERR_NOMEM;
                goto end;
@@ -1176,7 +1182,7 @@ enum lttng_error_code utils_user_id_from_name(const char *user_name, uid_t *uid)
                case ERANGE:
                        buflen *= 2;
                        free(buf);
-                       buf = (char *) zmalloc(buflen);
+                       buf = zmalloc<char>(buflen);
                        if (!buf) {
                                ret_val = LTTNG_ERR_NOMEM;
                                goto end;
@@ -1227,7 +1233,7 @@ enum lttng_error_code utils_group_id_from_name(
                buflen = FALLBACK_GROUP_BUFLEN;
        }
 
-       buf = (char *) zmalloc(buflen);
+       buf = zmalloc<char>(buflen);
        if (!buf) {
                ret_val = LTTNG_ERR_NOMEM;
                goto end;
@@ -1241,7 +1247,7 @@ enum lttng_error_code utils_group_id_from_name(
                case ERANGE:
                        buflen *= 2;
                        free(buf);
-                       buf = (char *) zmalloc(buflen);
+                       buf = zmalloc<char>(buflen);
                        if (!buf) {
                                ret_val = LTTNG_ERR_NOMEM;
                                goto end;
This page took 0.034683 seconds and 4 git commands to generate.