X-Git-Url: https://git.liburcu.org/?a=blobdiff_plain;f=src%2Fcommon%2Futils.cpp;h=6c0fd261c7b8a4033d5eb35cc7247af509ddfca3;hb=de5abcb02431896a1827dff5d3376e1f2e124cd7;hp=2ebaa84bb507c44a39715d74e900366f3486c81b;hpb=4971b7f0243bd3a7a661bcf1cfe95f0f8014b59b;p=lttng-tools.git diff --git a/src/common/utils.cpp b/src/common/utils.cpp index 2ebaa84bb..6c0fd261c 100644 --- a/src/common/utils.cpp +++ b/src/common/utils.cpp @@ -2,39 +2,37 @@ * Copyright (C) 2012 David Goulet * Copyright (C) 2013 Jérémie Galarneau * - * SPDX-License-Identifier: GPL-2.0-only + * SPDX-License-Identifier: LGPL-2.1-only * */ -#include "common/macros.h" #define _LGPL_SOURCE #include #include +#include +#include #include +#include #include +#include #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include -#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:" @@ -173,9 +171,8 @@ void utils_close_pipe(int *src) */ char *utils_strdupdelim(const char *begin, const char *end) { - char *str; + char *str = zmalloc(end - begin + 1); - str = (char *) zmalloc(end - begin + 1); if (str == NULL) { PERROR("zmalloc strdupdelim"); goto error; @@ -215,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; } @@ -786,7 +793,7 @@ char *utils_get_user_home_dir(uid_t uid) goto end; } retry: - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { goto end; } @@ -936,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(string_len); if (!optstring) { goto end; } @@ -1041,7 +1048,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; @@ -1059,10 +1066,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) { /* @@ -1079,7 +1086,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; } @@ -1098,7 +1110,7 @@ fopen_error: * the information in `/proc/meminfo`. The number returned by this function is * a best guess. */ -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); } @@ -1107,7 +1119,7 @@ int utils_get_memory_available(size_t *value) * Returns the total size of the memory on the system in bytes based on the * the information in `/proc/meminfo`. */ -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); } @@ -1156,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(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end; @@ -1170,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(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end; @@ -1221,7 +1233,7 @@ enum lttng_error_code utils_group_id_from_name( buflen = FALLBACK_GROUP_BUFLEN; } - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end; @@ -1235,7 +1247,7 @@ enum lttng_error_code utils_group_id_from_name( case ERANGE: buflen *= 2; free(buf); - buf = (char *) zmalloc(buflen); + buf = zmalloc(buflen); if (!buf) { ret_val = LTTNG_ERR_NOMEM; goto end;