Replace strerror to strerror_r 91/103891/5
authorKichan Kwon <k_c.kwon@samsung.com>
Sat, 10 Dec 2016 06:10:02 +0000 (15:10 +0900)
committerKwon <k_c.kwon@samsung.com>
Mon, 12 Dec 2016 10:25:28 +0000 (02:25 -0800)
- strerror is not thread-safe
- Size of buffer is decided to usually used

Change-Id: Icef5a5605e4347d65a634a0088445e07721b23d8
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
12 files changed:
src/common/cgroup.c
src/common/const.h
src/common/procfs.c
src/common/trace.h
src/heart/heart-memory.c
src/mem-stress/mem-stress.c
src/proc-stat/proc-main.c
src/proc-stat/proc-monitor.c
src/resourced/init.c
src/swap/swap.c
src/vip-agent/vip-process.c
src/vip-agent/vip-release-agent.c

index 20702c7..59a7f62 100644 (file)
@@ -69,7 +69,7 @@ resourced_ret_c cgroup_write_pid_fullpath(const char *cgroup_full_path,
        const int pid)
 {
        int ret;
-       char buf[256];
+       char buf[ERR_BUF_SIZE];
 
        if (pid <=0) {
                _E("try to write empty pid to %s", cgroup_full_path);
@@ -81,7 +81,7 @@ resourced_ret_c cgroup_write_pid_fullpath(const char *cgroup_full_path,
 
        ret_value_msg_if(ret < 0, RESOURCED_ERROR_FAIL,
                "Failed place all pid to cgroup %s, error %s",
-                       cgroup_full_path, strerror_r(errno, buf, sizeof(buf)));
+                       cgroup_full_path, strerror_r(errno, buf, ERR_BUF_SIZE));
        return RESOURCED_ERROR_NONE;
 }
 
index d4c9359..bb5e586 100644 (file)
@@ -47,6 +47,8 @@
 
 #define TIME_TO_SAFE_DATA 1 /* one second */
 
+#define ERR_BUF_SIZE 256
+
 /*
  * @desc reserved classid enums
  * internal structure, we don't provide it externally
index 62cf400..72f79b9 100644 (file)
@@ -299,6 +299,7 @@ int proc_get_zram_usage(pid_t pid, unsigned int *usage)
        struct meminfo mi;
        static unsigned int swap_total = 0;
        unsigned int proc_swap_usage, zram_usage;
+       char buf[ERR_BUF_SIZE];
 
        /* Read total swap size just once and cache it */
        if (!swap_total) {
@@ -306,7 +307,7 @@ int proc_get_zram_usage(pid_t pid, unsigned int *usage)
                if (ret < 0) {
                        _E("Failed to get %s: %s",
                           meminfo_id_to_string(MEMINFO_ID_SWAP_TOTAL),
-                          strerror(-ret));
+                          strerror_r(-ret, buf, ERR_BUF_SIZE));
                        return RESOURCED_ERROR_FAIL;
                }
                swap_total = mi.value[MEMINFO_ID_SWAP_TOTAL];
@@ -360,13 +361,13 @@ unsigned int proc_get_mem_available(void)
 {
        struct meminfo mi;
        int ret;
-       char buf[256];
+       char buf[ERR_BUF_SIZE];
 
        ret = proc_get_meminfo(&mi, MEMINFO_MASK_MEM_AVAILABLE);
        if (ret < 0) {
                _E("Failed to get %s: %s",
                                meminfo_id_to_string(MEMINFO_ID_MEM_AVAILABLE),
-                               strerror_r(-ret, buf, sizeof(buf)));
+                               strerror_r(-ret, buf, ERR_BUF_SIZE));
                return 0;
        }
 
@@ -377,13 +378,13 @@ unsigned int proc_get_swap_free(void)
 {
        struct meminfo mi;
        int ret;
-       char error_buf[256];
+       char buf[ERR_BUF_SIZE];
 
        ret = proc_get_meminfo(&mi, MEMINFO_MASK_SWAP_FREE);
        if (ret < 0) {
                _E("Failed to get %s: %s",
                   meminfo_id_to_string(MEMINFO_ID_SWAP_FREE),
-                  strerror_r(-ret, error_buf, sizeof(error_buf)));
+                  strerror_r(-ret, buf, ERR_BUF_SIZE));
                return 0;
        }
 
@@ -497,7 +498,7 @@ int proc_get_status(pid_t pid, char *buf, int len)
 int proc_sys_node_trigger(enum sys_node_id sys_node_id)
 {
        FILE *fp = NULL;
-       char error_buf[256];
+       char buf[ERR_BUF_SIZE];
 
        if (sys_node_id >= ARRAY_SIZE(sys_node_tables)) {
                _E("sys_node_id[%d] is out of range.\n", sys_node_id);
@@ -513,7 +514,7 @@ int proc_sys_node_trigger(enum sys_node_id sys_node_id)
        if (fp == NULL) {
                _E("Failed to open: %s: %s\n",
                        sys_node_tables[sys_node_id].path,
-                       strerror_r(errno, error_buf, sizeof(error_buf)));
+                       strerror_r(errno, buf, ERR_BUF_SIZE));
                sys_node_tables[sys_node_id].valid = 0;
                return RESOURCED_ERROR_FAIL;
        }
index 825e1c1..6ba5790 100644 (file)
@@ -27,6 +27,7 @@
 #define _SYSTEM_RESOURCE_TRACE_H_
 
 #include "config.h"
+#include "const.h"
 #include <dlog.h>
 #include <errno.h>
 #include <signal.h>
@@ -89,8 +90,8 @@ extern enum log_type logtype;
 }
 
 #define TRACE_RET_ERRCODE(type, error_code) do { \
-       char buf[256]; \
-       _##type("errno %d, errmsg %s", error_code, strerror_r(-error_code, buf, sizeof(buf))); \
+       char buf[ERR_BUF_SIZE]; \
+       _##type("errno %d, errmsg %s", error_code, strerror_r(-error_code, buf, ERR_BUF_SIZE)); \
 } while (0)
 
 #define DTRACE_RET_ERRCODE(error_code) TRACE_RET_ERRCODE(D, error_code)
@@ -98,9 +99,9 @@ extern enum log_type logtype;
 #define ETRACE_RET_ERRCODE(error_code) TRACE_RET_ERRCODE(E, error_code)
 
 #define TRACE_RET_ERRCODE_MSG(type, error_code, fmt, arg...) do { \
-       char buf[256]; \
+       char buf[ERR_BUF_SIZE]; \
        _##type(fmt, ##arg); \
-       _##type("errno %d, errmsg %s", error_code, strerror_r(-error_code, buf, sizeof(buf))); \
+       _##type("errno %d, errmsg %s", error_code, strerror_r(-error_code, buf, ERR_BUF_SIZE)); \
 } while (0)
 
 #define DTRACE_RET_ERRCODE_MSG(error_code, fmt, arg...) \
index 115cb63..8f52a95 100644 (file)
@@ -1099,7 +1099,7 @@ static int heart_memory_write(char *appid, char *pkgid, struct proc_status *p_da
        _cleanup_free_ char *info = NULL;
        unsigned int pss = 0, uss = 0;
        int ret;
-       char error_buf[256];
+       char error_buf[ERR_BUF_SIZE];
 
        /* For write to data crud during period */
        /* write memory usage in proc_list */
@@ -1112,7 +1112,7 @@ static int heart_memory_write(char *appid, char *pkgid, struct proc_status *p_da
        if (ret < 0) {
                _E("Failed to get PID(%d) smaps: %s",
                   p_data->pid,
-                  strerror_r(-ret, error_buf, sizeof(error_buf)));
+                  strerror_r(-ret, error_buf, ERR_BUF_SIZE));
                return ret;
        }
 
index f4bd2a0..106364e 100644 (file)
@@ -105,7 +105,7 @@ static int mem_stress_run_loop(void)
 {
        void *mem = NULL;
        int r;
-       char buf[256];
+       char buf[ERR_BUF_SIZE];
 
        fprintf(stdout, "Memory stress size is: %zu", arg_size);
 
@@ -114,7 +114,7 @@ static int mem_stress_run_loop(void)
 
        r = mem_stress_allocate_memory(&mem, arg_size);
        if (r < 0) {
-               fprintf(stderr, "Failed to allocate memory: %s", strerror_r(-r, buf, sizeof(buf)));
+               fprintf(stderr, "Failed to allocate memory: %s", strerror_r(-r, buf, ERR_BUF_SIZE));
                return r;
        }
 
@@ -123,7 +123,7 @@ static int mem_stress_run_loop(void)
 
        r = mem_stress_free_memory(mem, arg_size);
        if (r < 0) {
-               fprintf(stderr, "Failed to free memory: %s", strerror_r(-r, buf, sizeof(buf)));
+               fprintf(stderr, "Failed to free memory: %s", strerror_r(-r, buf, ERR_BUF_SIZE));
                return r;
        }
 
index 6e0e021..edcad6b 100644 (file)
@@ -89,6 +89,7 @@ GSList *proc_app_list_open(void)
 void proc_app_list_close(void)
 {
        int err;
+       char buf[ERR_BUF_SIZE];
 
        app_list.ref--;
 
@@ -100,7 +101,7 @@ void proc_app_list_close(void)
        err = pthread_mutex_unlock(&app_list.lock);
        if (err)
                _E("Failed to unlock app list mutex: %s",
-                  strerror(err));
+                  strerror_r(err, buf, ERR_BUF_SIZE));
 }
 
 static bool is_ui_app(enum application_type type)
@@ -329,6 +330,7 @@ static void proc_app_info_add_child_pid(struct proc_app_info *pai, pid_t pid)
 {
        _cleanup_free_ char *runtime_app_info_path = NULL;
        int ret;
+       char buf[ERR_BUF_SIZE];
 
        assert(pai);
        assert(pid > 0);
@@ -341,14 +343,15 @@ static void proc_app_info_add_child_pid(struct proc_app_info *pai, pid_t pid)
 
        runtime_app_info_path = proc_get_runtime_app_info_path(pai);
        if (!runtime_app_info_path) {
-               _E("Failed to get runtime appinfo path: %s", strerror(ENOMEM));
+               _E("Failed to get runtime appinfo path: %s",
+                               strerror_r(ENOMEM, buf, ERR_BUF_SIZE));
                return;
        }
 
        ret = proc_runtime_app_info_write_procs(runtime_app_info_path, pai->childs);
        if (ret < 0)
                _E("Failed to write appinfo children '%s': %s",
-                  runtime_app_info_path, strerror(-ret));
+                  runtime_app_info_path, strerror_r(-ret, buf, ERR_BUF_SIZE));
 }
 
 void proc_set_process_memory_state(struct proc_app_info *pai,
@@ -651,13 +654,14 @@ static void proc_app_list_add_app_info(struct proc_app_info *pai)
 {
        _cleanup_app_list_close_ GSList *proc_app_list = NULL;
        int ret;
+       char buf[ERR_BUF_SIZE];
 
        if (pai->main_pid <= 0)
                return;
 
        ret = proc_runtime_write_app_info(pai);
        if (ret < 0)
-               _E("Failed to add runtime app info: %s", strerror(ret));
+               _E("Failed to add runtime app info: %s", strerror_r(-ret, buf, ERR_BUF_SIZE));
 
        proc_app_list = proc_app_list_open();
        app_list.list = g_slist_prepend(proc_app_list, pai);
@@ -669,11 +673,12 @@ static GSList *proc_app_list_remove_app_info(struct proc_app_info *pai)
 {
        _cleanup_app_list_close_ GSList *proc_app_list = NULL;
        int ret;
+       char buf[ERR_BUF_SIZE];
 
        ret = proc_runtime_remove_app_info(pai);
        if (ret < 0)
                _E("Failed to remove appinfo '%s': %s",
-                  pai->appid, strerror(-ret));
+                  pai->appid, strerror_r(-ret, buf, ERR_BUF_SIZE));
 
        proc_app_list = proc_app_list_open();
        app_list.list = g_slist_remove(proc_app_list, pai);
@@ -784,6 +789,7 @@ static void proc_app_info_remove_child_pid(struct proc_app_info *pai, pid_t pid)
 {
        _cleanup_free_ char *runtime_app_info_path = NULL;
        int ret;
+       char buf[ERR_BUF_SIZE];
 
        assert(pai);
        assert(pid >= 0);
@@ -793,7 +799,8 @@ static void proc_app_info_remove_child_pid(struct proc_app_info *pai, pid_t pid)
 
        runtime_app_info_path = proc_get_runtime_app_info_path(pai);
        if (!runtime_app_info_path) {
-               _E("Failed to get runtime appinfo path: %s", strerror(ENOMEM));
+               _E("Failed to get runtime appinfo path: %s",
+                               strerror_r(ENOMEM, buf, ERR_BUF_SIZE));
                return;
        }
 
@@ -810,7 +817,7 @@ static void proc_app_info_remove_child_pid(struct proc_app_info *pai, pid_t pid)
        ret = proc_runtime_app_info_write_procs(runtime_app_info_path, pai->childs);
        if (ret < 0)
                _E("Failed to write appinfo children '%s': %s",
-                  runtime_app_info_path, strerror(-ret));
+                  runtime_app_info_path, strerror_r(-ret, buf, ERR_BUF_SIZE));
 
        return;
 
@@ -821,7 +828,7 @@ remove_all:
        ret = proc_runtime_app_info_remove_procs(runtime_app_info_path);
        if (ret < 0)
                _E("Failed to remove appinfo children '%s': %s",
-                  runtime_app_info_path, strerror(-ret));
+                  runtime_app_info_path, strerror_r(-ret, buf, ERR_BUF_SIZE));
 
        return;
 }
@@ -1239,6 +1246,7 @@ static void proc_restore_runtime_app_list(void)
        _cleanup_closedir_ DIR *dp = NULL;
        struct dirent dentry;
        struct dirent *dir;
+       char buf[ERR_BUF_SIZE];
 
        dp = opendir(RUNTIME_APP_INFO_DIR);
        if (!dp)
@@ -1263,15 +1271,17 @@ static void proc_restore_runtime_app_list(void)
                ret = proc_restore_runtime_app_info(path);
                if (ret < 0)
                        _E("Failed to restore runtime appinfo '%s': %s",
-                          dir->d_name, strerror(-ret));
+                          dir->d_name, strerror_r(-ret, buf, ERR_BUF_SIZE));
        }
 }
 
 static int resourced_proc_init(void* data)
 {
+       char buf[ERR_BUF_SIZE];
        int ret = mkdir(RUNTIME_APP_INFO_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
        if (ret < 0 && errno != EEXIST) {
-               _E("Failed to create directory %s: %s", RUNTIME_APP_INFO_DIR, strerror(-ret));
+               _E("Failed to create directory %s: %s", RUNTIME_APP_INFO_DIR,
+                               strerror_r(-ret, buf, ERR_BUF_SIZE));
                return RESOURCED_ERROR_FAIL;
        }
 
index adb04e2..be4c679 100755 (executable)
@@ -93,7 +93,7 @@ static DBusMessage *edbus_get_meminfo(E_DBus_Object *obj, DBusMessage *msg)
        DBusMessage *reply;
        struct meminfo mi;
        int r;
-       char error_buf[256];
+       char error_buf[ERR_BUF_SIZE];
 
        reply = dbus_message_new_method_return(msg);
 
@@ -106,7 +106,7 @@ static DBusMessage *edbus_get_meminfo(E_DBus_Object *obj, DBusMessage *msg)
                             MEMINFO_MASK_SWAP_FREE);
        if (r < 0) {
                _E("Failed to get meminfo: %s",
-                               strerror_r(-r, error_buf, sizeof(error_buf)));
+                               strerror_r(-r, error_buf, ERR_BUF_SIZE));
                return reply;
        }
 
index b312ace..8572c3d 100644 (file)
@@ -66,20 +66,21 @@ static int resourced_write_state(void)
 {
        pid_t pid = getpid(), old_pid = 0;
        int ret;
+       char buf[ERR_BUF_SIZE];
 
        assert(pid >= 0);
 
        ret = mkdir(RUNTIME_RESOURCED_DIR, S_IRWXU | S_IRGRP | S_IXGRP);
        if (ret < 0 && errno != EEXIST)
                _E("Failed to create directory %s: %s",
-                               RUNTIME_RESOURCED_DIR, strerror(-ret));
+                               RUNTIME_RESOURCED_DIR, strerror_r(-ret, buf, ERR_BUF_SIZE));
 
        if (access(RUNTIME_RESOURCED_PID_PATH, F_OK) < 0)
                goto write_pid;
 
        ret = fread_uint(RUNTIME_RESOURCED_PID_PATH, (u_int32_t*)&old_pid);
        if (ret < 0) {
-               _E("Failed to read old resourced pid: %s", strerror(-ret));
+               _E("Failed to read old resourced pid: %s", strerror_r(-ret, buf, ERR_BUF_SIZE));
                return ret;
        }
 
index e40251c..def6d7e 100644 (file)
@@ -164,7 +164,7 @@ static pid_t swap_change_state(enum swap_state state)
        int status;
        pid_t child_pid;
        pid_t pid = fork();
-       char error_buf[256];
+       char error_buf[ERR_BUF_SIZE];
 
        if (pid < 0) {
                _E("failed to fork");
@@ -186,7 +186,7 @@ static pid_t swap_change_state(enum swap_state state)
        child_pid = waitpid(pid, &status, 0);
        if (child_pid < 0) {
                _E("can't wait for a pid %d %d %s", pid, status,
-                               strerror_r(errno, error_buf, sizeof(error_buf)));
+                               strerror_r(errno, error_buf, ERR_BUF_SIZE));
                return child_pid;
        }
 
index 13b39fb..42b6adc 100644 (file)
@@ -93,7 +93,7 @@ static int vip_create_sub_cgroup(const char *name, pid_t pid)
        _cleanup_free_ char *cgroup_name = NULL;
        bool already;
        int r;
-       char buf[256];
+       char buf[ERR_BUF_SIZE];
 
        assert(name);
        assert(pid);
@@ -119,7 +119,7 @@ static int vip_create_sub_cgroup(const char *name, pid_t pid)
        r = cgroup_write_node_uint32(cgroup_name, TASK_FILE_NAME, pid);
        if (r < 0) {
                _E("failed to write pid '%d' to '%s': %s",
-                  pid, cgroup_name, strerror_r(-r, buf, sizeof(buf)));
+                  pid, cgroup_name, strerror_r(-r, buf, ERR_BUF_SIZE));
                return r;
        }
 
@@ -204,7 +204,7 @@ static int resourced_vip_process_init(void *data)
 {
        _cleanup_close_ int checkfd = -1;
        int r;
-       char buf[256];
+       char buf[ERR_BUF_SIZE];
 
        r = access(CHECK_RELEASE_PROGRESS, F_OK);
        if (r == 0) {
@@ -215,7 +215,7 @@ static int resourced_vip_process_init(void *data)
 
        r = config_parse(VIP_CONF_FILE, vip_load_config, NULL);
        if (r < 0) {
-               _E("failed to parse vip config file: %s", strerror_r(-r, buf, sizeof(buf)));
+               _E("failed to parse vip config file: %s", strerror_r(-r, buf, ERR_BUF_SIZE));
                return RESOURCED_ERROR_FAIL;
        }
 
@@ -238,7 +238,7 @@ static int resourced_vip_process_init(void *data)
 
                r = cgroup_set_release_agent("vip", "/usr/bin/vip-release-agent");
                if (r < 0) {
-                       _E("failed to set vip release agent: %s", strerror_r(-r, buf, sizeof(buf)));
+                       _E("failed to set vip release agent: %s", strerror_r(-r, buf, ERR_BUF_SIZE));
                        return RESOURCED_ERROR_FAIL;
                }
        }
index 017172d..6ded760 100644 (file)
@@ -30,7 +30,7 @@ static int run_exec(char **argv)
 {
        int status = 0, times = 0, wpid;
        pid_t pid = 0;
-       char buf[256];
+       char buf[ERR_BUF_SIZE];
 
        if (argv == NULL)
                return -3;
@@ -43,7 +43,7 @@ static int run_exec(char **argv)
        if (pid == 0) {
                setpgid(0, 0);
                if (execv(argv[0], argv) == -1) {
-                       _E("Error execv: %s.\n", strerror_r(errno, buf, sizeof(buf)));
+                       _E("Error execv: %s.\n", strerror_r(errno, buf, ERR_BUF_SIZE));
                        _exit(-1);
                }
                _exit(1);