Fix various complaints by SVACE static analysis 52/213352/7
authorMichal Bloch <m.bloch@partner.samsung.com>
Fri, 30 Aug 2019 12:49:26 +0000 (14:49 +0200)
committerBaumann <a.baumann@samsung.com>
Fri, 27 Sep 2019 14:06:41 +0000 (16:06 +0200)
Change-Id: I30253cab444b3592ca57822b3d398bc69bacf2e0
Signed-off-by: Michal Bloch <m.bloch@partner.samsung.com>
21 files changed:
src/common/appinfo-list.c
src/common/cgroup.c
src/common/config-parser.c
src/common/module.c
src/common/smaps.c
src/common/util.c
src/common/util.h
src/cpu/cpu.c
src/heart/decision-memory.c
src/heart/heart-abnormal.c
src/heart/heart-battery.c
src/heart/heart-cpu.c
src/heart/heart-memory.c
src/heart/include/heart.h
src/heart/logging.c
src/memory/memcontrol.c
src/memory/vmpressure-lowmem-handler.c
src/proc-stat/proc-monitor.c
src/swap/swap.c
src/swap/zramswap.c
src/vip-agent/vip-process.c

index 2b2bd27..d7fdedb 100644 (file)
@@ -51,8 +51,8 @@ static struct resourced_appinfo *resourced_appinfo_create(const char *appid,
        appid_len = strlen(appid);
        pkgname_len = strlen(pkgname);
 
-       if (appid_len >= MAX_APPID_LENGTH - 1 ||
-           pkgname_len >= MAX_PKGNAME_LENGTH - 1) {
+       if (appid_len >= sizeof ai->appid - 1 ||
+           pkgname_len >= sizeof ai->pkgname - 1) {
                _E("appid length = %d, pkgname length = %d",
                        appid_len, pkgname_len);
                return NULL;
@@ -65,10 +65,10 @@ static struct resourced_appinfo *resourced_appinfo_create(const char *appid,
        }
 
        /* appid and pkgname are terminated with null */
-       strncpy(ai->appid, appid, appid_len);
-       ai->appid[appid_len] = '\0';
-       strncpy(ai->pkgname, pkgname, pkgname_len);
-       ai->pkgname[pkgname_len] = '\0';
+       strncpy(ai->appid, appid, sizeof ai->appid);
+       ai->appid[sizeof ai->appid - 1] = '\0';
+       strncpy(ai->pkgname, pkgname, sizeof ai->pkgname);
+       ai->pkgname[sizeof ai->pkgname - 1] = '\0';
        ai->ref = 0;
 
        g_rw_lock_writer_lock(&resourced_appinfo_lock);
index cb7279d..43c5f75 100644 (file)
@@ -200,6 +200,9 @@ int cgroup_make_subdir(const char* parentdir, const char* cgroup_name, bool *alr
 
        cgroup_exists = cgroup_is_exists(buf);
        if (!cgroup_exists) {
+               /* FIXME: do something about this mounting, then replace
+                * `is_exists` with checking for -EEXIST in `create`,
+                * as currently this is both duplication AND a race condition */
                if (!strncmp(parentdir, DEFAULT_CGROUP, sizeof(DEFAULT_CGROUP))) {
                        ret = mount("tmpfs", DEFAULT_CGROUP, "tmpfs",
                                        MS_REMOUNT|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_STRICTATIME, "mode=755");
index e1b4aed..0e4e8fa 100644 (file)
@@ -71,7 +71,7 @@ int config_parse(const char *file_name, int cb(struct parse_result *result,
                lineno++;
 
                start = line;
-               start[strcspn(start, NEWLINE)] = '\0';
+               truncate_nl(start);
                start = trim_str(start);
 
                if (*start == COMMENT) {
index bf301dc..1b2111a 100644 (file)
@@ -234,7 +234,6 @@ static struct d_bus_method resourced_module_methods[] = {
 
 int modules_add_methods(void)
 {
-       d_bus_register_methods(RESOURCED_DBUS_OBJECT_PATH, resourced_module_methods_xml,
+       return d_bus_register_methods(RESOURCED_DBUS_OBJECT_PATH, resourced_module_methods_xml,
                resourced_module_methods, ARRAY_SIZE(resourced_module_methods));
-       return 0;
 }
index 960ab20..d4e5064 100644 (file)
@@ -140,10 +140,6 @@ int smaps_get(pid_t pid, struct smaps **maps, enum smap_mask mask)
        if (r < 0)
                return -ENOMEM;
 
-       r = access(path, F_OK);
-       if (r < 0)
-               return -errno;
-
        f = fopen(path, "re");
        if (!f)
                return -errno;
index 5cf8109..ac5a000 100644 (file)
@@ -74,6 +74,8 @@ int exec_cmd(int argc, const char *argv[])
        if (!argv)
                return RESOURCED_ERROR_INVALID_PARAMETER;
 
+       /* execve already checks for existence (duplication + race condition),
+        * but would make it much harder to return a failure to the caller */
        if (access(argv[0], F_OK)) {
                _E("There is no %s", argv[0]);
                return RESOURCED_ERROR_INVALID_PARAMETER;
@@ -99,10 +101,14 @@ int exec_dump_cmd(char *argv[], char *filename)
        if (fork() == 0) {
                fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0644);
                if (fd < 0) {
-                       _E("Failed to create/open file %s", filename);
+                       _E("Failed to create/open file \"%s\": %m", filename);
+                       return RESOURCED_ERROR_FAIL;
+               }
+               if (dup2(fd, 1) < 0) {
+                       _E("Failed to duplicate a file descriptor: %m");
+                       close(fd);
                        return RESOURCED_ERROR_FAIL;
                }
-               dup2(fd, 1);
                status = execvp(argv[0], argv);
                close(fd);
        }
@@ -300,20 +306,18 @@ int strv_attach(char **first, char **second, char ***strv, bool free_second)
        return 0;
 }
 
-void strv_free_full(char **strv)
+void strv_free_full(char ***strv)
 {
        char **s;
 
-       if (!strv)
+       if (!strv || !*strv)
                return;
 
-       FOREACH_STRV(s, strv) {
-               if (s && *s) {
-                       free(*s);
-                       *s = NULL;
-               }
+       FOREACH_STRV(s, *strv) {
+               free(*s);
+               *s = NULL;
        }
 
-       free(strv);
-       strv = NULL;
+       free(*strv);
+       *strv = NULL;
 }
index d2a86e4..24a4583 100644 (file)
@@ -154,5 +154,5 @@ char *strstrip(char *s);
 int str_to_strv(const char *str, char ***strv, const char *seperator);
 size_t sizeof_strv(const char **strv);
 int strv_attach(char **first, char **second, char ***strv, bool free_second);
-void strv_free_full(char **strv);
+void strv_free_full(char ***strv);
 #endif /*_RESOURCED_UTIL_H_*/
index 256c3a9..6188044 100644 (file)
@@ -253,10 +253,11 @@ static int cpu_service_state(void *data)
 static int cpu_widget_state(void *data)
 {
        struct proc_status *ps = (struct proc_status *)data;
-       assert(ps && ps->pai);
+       assert(ps);
+       assert(ps->pai);
 
        _D("widget background: pid = %d, appname = %s", ps->pid, ps->pai->appid);
-       if (ps->pai && CHECK_BIT(ps->pai->flags, PROC_DOWNLOADAPP))
+       if (CHECK_BIT(ps->pai->flags, PROC_DOWNLOADAPP))
                cpu_move_cgroup(ps->pid, CPU_BACKGROUND_GROUP);
        return RESOURCED_ERROR_NONE;
 }
index 5371853..d6296ed 100644 (file)
@@ -247,7 +247,7 @@ int decision_memory_init(void *data)
 {
        _D("decision memory init finished");
 
-       logging_register_listener("memory", decision_memory_updated_cb);
+       logging_register_listener(MEM_NAME, decision_memory_updated_cb);
        decision_module_register(&decision_memory);
 
        return RESOURCED_ERROR_NONE;
@@ -258,8 +258,7 @@ int decision_memory_exit(void *data)
        _D("decision memory finalize");
 
        decision_module_unregister(&decision_memory);
-       logging_unregister_listener(DECISION_MEMORY,
-               decision_memory_updated_cb);
+       logging_unregister_listener(MEM_NAME, decision_memory_updated_cb);
 
        return RESOURCED_ERROR_NONE;
 }
index 2aba308..7096a82 100644 (file)
@@ -74,7 +74,11 @@ void heart_abnormal_fill_array(struct logging_table_form *entry, void *data)
        struct heart_abnormal_table *table = NULL;
        GHashTable *list = (GHashTable *)data;
 
-       sscanf((char *)entry->data, "%*s %*s %u ", &type);
+       if (sscanf((char *)entry->data, "%*s %*s %u ", &type) < 0) {
+               _E("sscanf failed, %m");
+               return;
+       }
+
        if (type >= ABNORMAL_TYPE_MAX) {
                _E("wrong abnormal type, %u", type);
                return;
index 5217cc6..e2e8ad0 100644 (file)
@@ -466,20 +466,19 @@ static int heart_battery_calculate_discharge_rate_level(long spc)
        return BATTERY_DISCHARGE_AVG;
 }
 
-static int heart_battery_save_used_time(char *key, struct battery_used *used)
+static void heart_battery_save_used_time(char *key, struct battery_used *used)
 {
-       if (!key || !used)
-               return RESOURCED_ERROR_FAIL;
+       assert(key);
+       assert(used);
 
        logging_leveldb_putv(key, strlen(key), "%d %d ",
                        used->used_time_sec, used->last_update_time);
-       return RESOURCED_ERROR_NONE;
 };
 
-static int heart_battery_save_status(char *key, struct battery_status *status)
+static void heart_battery_save_status(char *key, struct battery_status *status)
 {
-       if (!key || !status)
-               return RESOURCED_ERROR_FAIL;
+       assert(key);
+       assert(status);
 
        logging_leveldb_putv(key, strlen(key), "%d %ld %ld %ld %ld %d %d ",
                        status->curr_capacity,
@@ -489,17 +488,16 @@ static int heart_battery_save_status(char *key, struct battery_status *status)
                        status->curr_cap_counter[CHARGING],
                        status->curr_charger_status,
                        status->reset_mark);
-
-       return RESOURCED_ERROR_NONE;
 };
 
-static int heart_battery_save_usage(char *key, struct battery_usage *usage, int total_size)
+static void heart_battery_save_usage(char *key, struct battery_usage *usage, int total_size)
 {
        int i, len, num;
        char buf[BATTERY_DATA_MAX] = {0, };
 
-       if (!key || !usage)
-               return RESOURCED_ERROR_FAIL;
+       assert(key);
+       assert(usage);
+
        len = 0;
        num = total_size/sizeof(struct battery_usage);
        for (i = 0; i < num; i++) {
@@ -511,16 +509,16 @@ static int heart_battery_save_usage(char *key, struct battery_usage *usage, int
                                usage[i].cap_counter[CHARGING]);
        }
        logging_leveldb_put(key, strlen(key), buf, len);
-       return RESOURCED_ERROR_NONE;
 };
 
-static int heart_battery_save_prediction(char *key, struct battery_prediction *prediction)
+static void heart_battery_save_prediction(char *key, struct battery_prediction *prediction)
 {
        int i, len;
        char buf[BATTERY_DATA_MAX] = {0, };
 
-       if (!key || !prediction)
-               return RESOURCED_ERROR_FAIL;
+       assert(key);
+       assert(prediction);
+
        len = 0;
        for (i = 0; i < MAX_STRATEGY; i++) {
                len += snprintf(buf + len, BATTERY_DATA_MAX - len, "%ld %ld %ld %ld %ld %ld ",
@@ -532,7 +530,6 @@ static int heart_battery_save_prediction(char *key, struct battery_prediction *p
                                prediction[CHARGING].time_pred_min[i]);
        }
        logging_leveldb_put(key, strlen(key), buf, len);
-       return RESOURCED_ERROR_NONE;
 };
 
 
@@ -836,7 +833,7 @@ static void heart_battery_insert_capacity(GSList **history_list, int capacity,
 
 /* ======================== Serialization/Deserialization ==================== */
 
-static int heart_battery_status_save_to_db(void)
+static void heart_battery_status_save_to_db(void)
 {
        heart_battery_save_used_time(BATTERY_USED_TIME, &batt_used);
        heart_battery_save_status(BATTERY_STATUS, &batt_stat);
@@ -846,20 +843,22 @@ static int heart_battery_status_save_to_db(void)
        heart_battery_save_usage(BATTERY_LEVEL_USAGE, batt_stat.batt_lvl_usage, sizeof(batt_stat.batt_lvl_usage));
 
        heart_battery_save_prediction(BATTERY_PREDICTION, batt_stat.prediction);
-       return RESOURCED_ERROR_NONE;
 }
 
 static int heart_battery_status_read_from_db(void)
 {
-       heart_battery_load_used_time(BATTERY_USED_TIME, &batt_used);
-       heart_battery_load_status(BATTERY_STATUS, &batt_stat);
+       bool ok = true;
 
-       heart_battery_load_usage(BATTERY_RESET_USAGE, batt_stat.batt_reset_usage, sizeof(batt_stat.batt_reset_usage));
-       heart_battery_load_usage(BATTERY_WEEK_DAY_USAGE, batt_stat.week_day_usage, sizeof(batt_stat.week_day_usage));
-       heart_battery_load_usage(BATTERY_LEVEL_USAGE, batt_stat.batt_lvl_usage, sizeof(batt_stat.batt_lvl_usage));
+       ok &= (RESOURCED_ERROR_NONE == heart_battery_load_used_time(BATTERY_USED_TIME, &batt_used));
+       ok &= (RESOURCED_ERROR_NONE == heart_battery_load_status(BATTERY_STATUS, &batt_stat));
 
-       heart_battery_load_prediction(BATTERY_PREDICTION, batt_stat.prediction);
-       return RESOURCED_ERROR_NONE;
+       ok &= (RESOURCED_ERROR_NONE == heart_battery_load_usage(BATTERY_RESET_USAGE, batt_stat.batt_reset_usage, sizeof(batt_stat.batt_reset_usage)));
+       ok &= (RESOURCED_ERROR_NONE == heart_battery_load_usage(BATTERY_WEEK_DAY_USAGE, batt_stat.week_day_usage, sizeof(batt_stat.week_day_usage)));
+       ok &= (RESOURCED_ERROR_NONE == heart_battery_load_usage(BATTERY_LEVEL_USAGE, batt_stat.batt_lvl_usage, sizeof(batt_stat.batt_lvl_usage)));
+
+       ok &= (RESOURCED_ERROR_NONE == heart_battery_load_prediction(BATTERY_PREDICTION, batt_stat.prediction));
+
+       return ok ? RESOURCED_ERROR_NONE : RESOURCED_ERROR_FAIL;
 }
 
 static int heart_battery_capacity_save_to_file(char *filename)
@@ -994,9 +993,7 @@ static void heart_battery_save_to_file(bool force)
            heart_battery_get_file_commit_timestamp() + HEART_BATTERY_SAVE_INTERVAL >= now)
                return;
 
-       ret = heart_battery_status_save_to_db();
-       if (ret)
-               _E("failed to save status db");
+       heart_battery_status_save_to_db();
 
        ret = heart_battery_capacity_save_to_file(HEART_BATTERY_CAPACITY_DATA_FILE);
        if (ret)
@@ -2516,15 +2513,9 @@ static void dbus_get_battery_discharge_rate_level(GDBusMethodInvocation *invocat
 
 static void dbus_battery_save_to_file(GDBusMethodInvocation *invocation, GVariant *params)
 {
-       int ret;
+       heart_battery_status_save_to_db();
 
-       ret = heart_battery_status_save_to_db();
-       if (ret) {
-               _E("save to db failed");
-               D_BUS_REPLY_NULL(invocation)
-               return;
-       }
-       ret = heart_battery_capacity_save_to_file(HEART_BATTERY_CAPACITY_DATA_FILE);
+       int ret = heart_battery_capacity_save_to_file(HEART_BATTERY_CAPACITY_DATA_FILE);
        if (ret) {
                _E("save to file failed");
                D_BUS_REPLY_NULL(invocation)
index 9b4fb4f..62be41f 100644 (file)
@@ -570,7 +570,7 @@ static int heart_cpu_save_to_file(struct heart_cpu_dat_cache *cache)
                                                ci->state);
                        }
                }
-               len += snprintf(buf + len, CPU_DATA_MAX - len, "\n");
+               snprintf(buf + len, CPU_DATA_MAX - len, "\n");
                fputs(buf, fp);
        }
        ret = pthread_mutex_unlock(&heart_cpu_mutex);
@@ -793,8 +793,7 @@ void heart_cpu_update(struct logging_table_form *data, void *user_data)
                ci->utime = utime;
                ci->stime = stime;
                ci->state = state;
-               table->last_pid_info = NULL;
-               table->last_pid_info = g_slist_prepend(table->last_pid_info, ci);
+               table->last_pid_info = g_slist_prepend(NULL, ci);
                table->last_pid = pid;
 
                g_hash_table_insert(cpu_usage_list, (gpointer)table->appid, (gpointer)table);
@@ -1180,8 +1179,11 @@ static int heart_cpu_update_appid(void *data)
        }
 
        info = (char*)data;
-       sscanf(info, "%d "STR_FS(MAX_APPID_LENGTH_M1)" "STR_FS(MAX_APPID_LENGTH_M1),
-                       &pid, old_appid, new_appid);
+       if (sscanf(info, "%d "STR_FS(MAX_APPID_LENGTH_M1)" "STR_FS(MAX_APPID_LENGTH_M1),
+                       &pid, old_appid, new_appid) < 0) {
+               _E("sscanf failed, %m");
+               return RESOURCED_ERROR_FAIL;
+       }
 
        return logging_modify_appid(CPU_NAME, old_appid, new_appid, pid);
 }
index 893266b..80ffa75 100644 (file)
 #include <sqlite3.h>
 #include <time.h>
 
-#define MEM_NAME                                       "memory"
-#define MEM_DATA_MAX                                   1024
-#define MEM_ARRAY_MAX                                  24
-#define MEM_FILE_SEPERATOR                             '@'
-
-#define HEART_MEMORY_SAVE_INTERVAL                     3600  /* 1 hour */
-#define HEART_MEMORY_FILE                              HEART_FILE_PATH"/.memory.dat"
-
 struct heart_memory_info {
        unsigned int max_rss;
        unsigned int avg_rss;
@@ -298,7 +290,7 @@ static int heart_memory_save_to_file(GHashTable *hashtable, char *filename)
                        }
                }
 
-               len += snprintf(buf + len, MEM_DATA_MAX - len, "%c\n", MEM_FILE_SEPERATOR);
+               snprintf(buf + len, MEM_DATA_MAX - len, "%c\n", MEM_FILE_SEPERATOR);
 
                fputs(buf, fp);
        }
index 360cda9..c7347ff 100644 (file)
 #define HEART_USER_FILE_PATH           "%s/data/heart"
 #define HEART_CONF_SECTION                     "HEART"
 
+#define MEM_NAME                                       "memory"
+#define MEM_DATA_MAX                                   1024
+#define MEM_ARRAY_MAX                                  24
+#define MEM_FILE_SEPERATOR                             '@'
+
+#define HEART_MEMORY_SAVE_INTERVAL                     3600  /* 1 hour */
+#define HEART_MEMORY_FILE                              HEART_FILE_PATH"/.memory.dat"
+
 struct heart_module_ops {
        char *name;
        int (*init) (void *data);
index 2935d28..8b09f62 100644 (file)
@@ -163,7 +163,7 @@ static int logging_convert_pid_to_uid(int pid)
        char buf[LOGGING_BUF_MAX];
        char key[20];
        FILE *fp = NULL;
-       int uid = -1;
+       int ret = -1;
 
        if (pid == PID_FOR_ROOT)
                return UID_FOR_ROOT;
@@ -176,15 +176,19 @@ static int logging_convert_pid_to_uid(int pid)
        }
 
        while (fgets(buf, LOGGING_BUF_MAX, fp) != NULL) {
-               sscanf(buf, "%19s %d", key, &uid);
-               if (!strncmp(key, "Uid:", sizeof("Uid:") + 1)) {
-                       fclose(fp);
-                       return uid;
+               int uid;
+               if (sscanf(buf, "%19s %d", key, &uid) < 0) {
+                       _E("sscanf failed: %m");
+                       break;
+               }
+               if (!strncmp(key, "Uid:", sizeof("Uid:"))) {
+                       ret = uid;
+                       break;
                }
        }
 
        fclose(fp);
-       return -1;
+       return ret;
 }
 
 static struct logging_module *logging_find_module(char *name)
@@ -338,16 +342,20 @@ static int logging_update_user_db_list(char *name, enum logging_db_type type, GH
                        return RESOURCED_ERROR_DB_FAILED;
                }
 
+               db_elem = NULL;
                uid = malloc(sizeof(uid_t));
-               assert(uid);
+               if (!uid)
+                       goto oom_failure;
 
                *uid = elem->uid;
 
                db_elem = (struct logging_db*)malloc(sizeof(struct logging_db));
-               assert(db_elem);
+               if (!db_elem)
+                       goto oom_failure;
 
                db_elem->path = strndup(path, strlen(path));
-               assert(db_elem->path);
+               if (!db_elem->path)
+                       goto oom_failure;
 
                db_elem->file = file;
                db_elem->insert_stmt = NULL;
@@ -355,6 +363,15 @@ static int logging_update_user_db_list(char *name, enum logging_db_type type, GH
        }
 
        return RESOURCED_ERROR_NONE;
+
+oom_failure:
+       free(uid);
+       if (db_elem)
+               free(db_elem->path);
+       free(db_elem);
+
+       _E("out of memory!");
+       return RESOURCED_ERROR_OUT_OF_MEMORY;
 }
 
 int logging_module_init(char *name, enum logging_period max_period,
@@ -373,7 +390,10 @@ int logging_module_init(char *name, enum logging_period max_period,
 
        if (!logging_instance) {
                logging_instance = (struct logging_object *)malloc(sizeof(struct logging_object));
-               assert(logging_instance);
+               if (!logging_instance) {
+                       _E("out of memory");
+                       return RESOURCED_ERROR_OUT_OF_MEMORY;
+               }
 
                logging_instance->ref = 0;
                ret = logging_init(NULL);
@@ -393,7 +413,10 @@ int logging_module_init(char *name, enum logging_period max_period,
        }
 
        module = calloc(1, sizeof(struct logging_module));
-       assert(module);
+       if (!module) {
+               _E("out of memory");
+               return RESOURCED_ERROR_OUT_OF_MEMORY;
+       }
 
        /* DB create */
        switch (db_type) {
index 53ea7d0..ef76635 100644 (file)
@@ -174,14 +174,18 @@ int memcg_set_eventfd(const char *memcg, const char *event, char *value)
        snprintf(buf, PATH_MAX, "%s/%s", memcg, MEMCG_EVENTFD_CONTROL);
        cgfd = open(buf, O_WRONLY);
        if (cgfd < 0) {
+               const int saved_errno = errno;
                _E("open event_control failed");
+               errno = saved_errno;
                return RESOURCED_ERROR_FAIL;
        }
 
        snprintf(buf, PATH_MAX, "%s/%s", memcg, event);
        mcgfd = open(buf, O_RDONLY);
        if (mcgfd < 0) {
+               const int saved_errno = errno;
                _E("open memory control failed");
+               errno = saved_errno;
                return RESOURCED_ERROR_FAIL;
        }
 
@@ -191,7 +195,9 @@ int memcg_set_eventfd(const char *memcg, const char *event, char *value)
        sz += 1;
        res = write(cgfd, buf, sz);
        if (res != sz) {
+               int saved_errno = errno;
                _E("write cgfd failed : %d", res);
+               errno = saved_errno;
                return RESOURCED_ERROR_FAIL;
        }
        return evfd;
index 64b4c75..9837d2c 100644 (file)
@@ -432,7 +432,7 @@ int clear_logs(void *data)
        }
 
        len = strlen(dir);
-       if (len <= 0 || len >= BUF_MAX -1) {
+       if (len <= 0 || len >= sizeof fpath - 1) {
                _E("Invalid parameter - Directory path is too short or too long");
                return RESOURCED_ERROR_INVALID_PARAMETER;
        }
@@ -446,17 +446,18 @@ int clear_logs(void *data)
                return RESOURCED_ERROR_NONE;
        }
 
-       strncpy(fpath, dir, len);
+       strncpy(fpath, dir, sizeof fpath);
+       fpath[sizeof fpath - 1] = '\0';
        fname = fpath + len;
        *fname++ = '/';
 
-       len = BUF_MAX - len - 1;
+       len = sizeof fpath - len - 1;
        for (i = 0; i < n; i++) {
                if (i < NUM_RM_LOGS) {
                        if (strlen(namelist[i]->d_name) > len - 1)
                                continue;
                        strncpy(fname, namelist[i]->d_name, len - 1);
-                       fpath[BUF_MAX - 1] = '\0';
+                       fpath[sizeof fpath - 1] = '\0';
                        _D("remove log file %s", fpath);
                        ret = remove(fpath);
                        if (ret < 0)
@@ -1506,9 +1507,10 @@ static void lowmem_move_memcgroup(int pid, int oom_score_adj)
        struct memcg_info *mi;
        int ret, memcg_idx, should_swap = 0;
 
-       if (!pai) {
+       if (!pai)
                return;
-       } else if (oom_score_adj >= OOMADJ_BACKGRD_PERCEPTIBLE &&
+
+       if (oom_score_adj >= OOMADJ_BACKGRD_PERCEPTIBLE &&
                       oom_score_adj < OOMADJ_BACKGRD_UNLOCKED) {
                /* We'd like lowmem a chance to do a bit more with some
                 * long-lived processes such as background-locked or
@@ -1539,9 +1541,9 @@ static void lowmem_move_memcgroup(int pid, int oom_score_adj)
                memcg_idx = MEMCG_BGLOCKED;
                mi = memcg_tree[memcg_idx]->info;
        } else if (oom_score_adj > OOMADJ_BACKGRD_UNLOCKED + OOMADJ_APP_INCREASE) {
-               if (pai && (oom_score_adj != pai->memory.oom_score_adj))
+               if (oom_score_adj != pai->memory.oom_score_adj)
                        proc_set_process_memory_state(pai, pai->memory.memcg_idx,
-                       pai->memory.memcg_info, oom_score_adj);
+                               pai->memory.memcg_info, oom_score_adj);
                return;
        } else if (oom_score_adj >= OOMADJ_INIT) {
                memcg_idx = MEMCG_APPS;
@@ -1698,6 +1700,7 @@ static bool lowmem_press_eventfd_handler(int fd, void *data)
        GSList *iter = NULL;
        enum lmk_type lmk_type = LMK_MEMORY;
 
+       // FIXME: probably shouldn't get ignored
        (void)lowmem_press_eventfd_read(fd);
 
        for (i = 0; i < MEMCG_MAX; i++) {
@@ -1737,28 +1740,29 @@ static bool lowmem_press_eventfd_handler(int fd, void *data)
        return true;
 }
 
-static void lowmem_press_register_eventfd(struct memcg_info *mi)
+static int lowmem_press_register_eventfd(struct memcg_info *mi)
 {
        int evfd;
        const char *name = mi->name;
        static fd_handler_h handler;
 
        if (mi->threshold[LOWMEM_MEDIUM] == LOWMEM_THRES_INIT)
-               return;
+               return 0;
 
        evfd = memcg_set_eventfd(name, MEMCG_EVENTFD_MEMORY_PRESSURE,
                        event_level);
 
-       if (evfd < 0) {
+       if (evfd == RESOURCED_ERROR_FAIL) {
+               int saved_errno = errno;
                _I("fail to register event press fd %s cgroup", name);
-               return;
+               return -saved_errno;
        }
 
        mi->evfd = evfd;
 
        _I("register event fd success for %s cgroup", name);
        add_fd_read_handler(evfd, lowmem_press_eventfd_handler, NULL, NULL, &handler);
-       return;
+       return 0;
 }
 
 static int lowmem_press_setup_eventfd(void)
@@ -1769,7 +1773,9 @@ static int lowmem_press_setup_eventfd(void)
                if (!memcg_tree[i]->use_hierarchy)
                        continue;
 
-               lowmem_press_register_eventfd(memcg_tree[i]->info);
+               const int r = lowmem_press_register_eventfd(memcg_tree[i]->info);
+               if (r < 0)
+                       return RESOURCED_ERROR_FAIL;
        }
        return RESOURCED_ERROR_NONE;
 }
index d7a9cb9..a27d05a 100644 (file)
@@ -116,14 +116,13 @@ static void dbus_get_meminfo(GDBusMethodInvocation *invocation, GVariant *params
 
 static void dbus_reclaim_memory(GDBusMethodInvocation *invocation, GVariant *params)
 {
-       int ret = -1;
-
        _D("reclaiming memory!");
 
-       ret = proc_sys_node_trigger(SYS_VM_SHRINK_MEMORY);
-       ret = proc_sys_node_trigger(SYS_VM_COMPACT_MEMORY);
+       bool ok = true;
+       ok &= (RESOURCED_ERROR_NONE == proc_sys_node_trigger(SYS_VM_SHRINK_MEMORY));
+       ok &= (RESOURCED_ERROR_NONE == proc_sys_node_trigger(SYS_VM_COMPACT_MEMORY));
 
-       g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", ret));
+       g_dbus_method_invocation_return_value(invocation, g_variant_new("(i)", ok ? RESOURCED_ERROR_NONE : RESOURCED_ERROR_FAIL));
 }
 
 static void turn_off_vip_handler()
index 62fb326..eac3946 100644 (file)
@@ -536,8 +536,7 @@ static int swap_move_inactive_to_swap(struct swap_status_msg *msg)
        msg->type = MEMCG_SWAP;
        ret = swap_move_to_cgroup(msg->info, candidates);
 out:
-       if (candidates)
-               g_array_free(candidates, TRUE);
+       g_array_free(candidates, TRUE);
 
        return ret;
 }
@@ -665,6 +664,8 @@ static void swap_activate_in_module(void)
 
        r = cgroup_read_node_int32(LOWMEM_ROOT_CGROUP,
                MEMCG_SWAPNESS_PATH, &current_swappiness);
+       if (r)
+               return;
 
        if (swap_get_state() == SWAP_ON)
                return;
index 4c828f2..a19e8e5 100644 (file)
@@ -206,6 +206,8 @@ static int swap_zram_reclaim(void *data)
                swap_total = proc_get_swap_total();
 
        r = memcg_get_swap_usage(LOWMEM_SWAP_CGROUP, &swap_usage);
+       if (r)
+               return r;
        swapcg_usage_ratio = (float)(swap_usage / (swap_total - swap_available) *100);
        if (swapcg_usage_ratio > SWAPCG_CHECK_RATIO)
                type = LMK_OLDEST;
index 75226ab..a2d87b2 100644 (file)
@@ -269,8 +269,8 @@ static int resourced_vip_process_init(void *data)
 
 static int resourced_vip_process_finalize(void *data)
 {
-       strv_free_full(arg_vip_proc_names);
-       strv_free_full(arg_vip_systemd_services);
+       strv_free_full(&arg_vip_proc_names);
+       strv_free_full(&arg_vip_systemd_services);
 
        vip_process_disable(NULL);
        unregister_notifier(RESOURCED_NOTIFIER_BOOTING_DONE, vip_booting_done);