Fix: utils: unhandled close return value
[lttng-tools.git] / src / common / utils.c
index 6c34dd56200dfd8a46be50e6a1a0461695cd667e..8b2e0063c28d8bbc5570dc319c9121ea04d8af44 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include "common/macros.h"
+#include <stdint.h>
 #define _LGPL_SOURCE
 #include <assert.h>
 #include <ctype.h>
@@ -223,30 +224,43 @@ end:
 LTTNG_HIDDEN
 int utils_create_pid_file(pid_t pid, const char *filepath)
 {
-       int ret;
-       FILE *fp;
+       int ret, fd = -1;
+       FILE *fp = NULL;
 
        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;
+               if (close(fd)) {
+                       PERROR("Failed to close `%s` file descriptor while handling fdopen error", filepath);
+               }
+
                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;
 }
 
@@ -1062,7 +1076,7 @@ end:
 }
 
 static
-int read_proc_meminfo_field(const char *field, size_t *value)
+int read_proc_meminfo_field(const char *field, uint64_t *value)
 {
        int ret;
        FILE *proc_meminfo;
@@ -1080,10 +1094,10 @@ int read_proc_meminfo_field(const char *field, size_t *value)
         * field.
         */
        while (!feof(proc_meminfo)) {
-               unsigned long value_kb;
+               uint64_t value_kb;
 
                ret = fscanf(proc_meminfo,
-                               "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %lu kB\n",
+                               "%" MAX_NAME_LEN_SCANF_IS_A_BROKEN_API "s %" SCNu64 " kB\n",
                                name, &value_kb);
                if (ret == EOF) {
                        /*
@@ -1100,7 +1114,12 @@ int read_proc_meminfo_field(const char *field, size_t *value)
                         * This number is displayed in kilo-bytes. Return the
                         * number of bytes.
                         */
-                       *value = ((size_t) value_kb) * 1024;
+                       if (value_kb > UINT64_MAX / 1024) {
+                               ERR("Overflow on kb to bytes conversion");
+                               break;
+                       }
+
+                       *value = value_kb * 1024;
                        ret = 0;
                        goto found;
                }
@@ -1120,7 +1139,7 @@ fopen_error:
  * a best guess.
  */
 LTTNG_HIDDEN
-int utils_get_memory_available(size_t *value)
+int utils_get_memory_available(uint64_t *value)
 {
        return read_proc_meminfo_field(PROC_MEMINFO_MEMAVAILABLE_LINE, value);
 }
@@ -1130,7 +1149,7 @@ int utils_get_memory_available(size_t *value)
  * the information in `/proc/meminfo`.
  */
 LTTNG_HIDDEN
-int utils_get_memory_total(size_t *value)
+int utils_get_memory_total(uint64_t *value)
 {
        return read_proc_meminfo_field(PROC_MEMINFO_MEMTOTAL_LINE, value);
 }
This page took 0.024966 seconds and 4 git commands to generate.