proc-usage : return empty list if there is no launched app 10/188210/4
authorKichan Kwon <k_c.kwon@samsung.com>
Mon, 3 Sep 2018 04:47:12 +0000 (13:47 +0900)
committerKichan Kwon <k_c.kwon@samsung.com>
Mon, 3 Sep 2018 07:08:28 +0000 (16:08 +0900)
- Empty list is different with NULL
- Return NULL if error occurs

Change-Id: I5369187c68f46d1a02f3460fec7fe4514817561a
Signed-off-by: Kichan Kwon <k_c.kwon@samsung.com>
src/proc-usage/proc-usage-application.c

index 303d65796f15e818b112cb0dec8031613195e553..617f15ea1ef4cbadeff6133ffe92a0907115d9ff 100644 (file)
@@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+#include <errno.h>
 #include <gio/gio.h>
 #include <glib.h>
 #include <stdio.h>
 #include "proc-usage-application.h"
 #include "proc-usage-process.h"
 
-GSList *proc_usage_application_get_list(void)
+/**
+ * @param[out] applist  The appid list
+ * @return  The number of apps if success, otherwise a negative error value
+ */
+int proc_usage_application_get_list(GSList **applist)
 {
        int ret;
+       int count = 0;
        pid_t pid, pid_max;
        DIR *dp;
        struct dirent *de;
        char label[64];
-       GSList *applist = NULL;
        struct proc_usage_application *pua;
 
+       if (!applist) {
+               _E("Invalid parameter");
+               return -EINVAL;
+       }
+
+       *applist = NULL;
+
        ret = procfs_get_pid_max(&pid_max);
        if (ret < 0) {
                _E("Failed to get the maximum value of PID");
-               return NULL;
+               return ret;
        }
 
        dp = opendir("/proc");
        if (!dp) {
                _E("Failed to open /proc : %m");
-               return NULL;
+               return -errno;
        }
 
        while ((de = readdir(dp))) {
@@ -78,22 +90,28 @@ GSList *proc_usage_application_get_list(void)
                pua = g_new(struct proc_usage_application, 1);
                if (!pua) {
                        _E("Not enough memory");
-                       if (applist)
-                               g_slist_free_full(applist, g_free);
+                       if (*applist)
+                               g_slist_free_full(*applist, g_free);
                        closedir(dp);
-                       return NULL;
+                       return -ENOMEM;
                }
 
                strncpy(pua->appid, label + 11, NAME_MAX);
                pua->pid = pid;
 
-               applist = g_slist_prepend(applist, pua);
+               *applist = g_slist_prepend(*applist, pua);
+               ++count;
        }
 
        closedir(dp);
-       return applist;
+       return count;
 }
 
+/**
+ * @return  NULL if error occurs. Otherwise, (appid, memory usage) list
+ * @notice  If there is no launched app, return empty list.
+ *          Empty list is different with NULL
+ */
 GVariant *proc_usage_application_get_all_memory_usages(void)
 {
        int ret;
@@ -103,14 +121,14 @@ GVariant *proc_usage_application_get_all_memory_usages(void)
        struct proc_usage_application *app = NULL;
        struct procfs_pid_statm pps;
 
-       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
-
-       applist = proc_usage_application_get_list();
-       if (!applist) {
-               _E("Failed to get applist");
+       ret = proc_usage_application_get_list(&applist);
+       if (ret < 0) {
+               _E("Failed to get applist (%d)", ret);
                return NULL;
        }
 
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
+
        G_SLIST_FOREACH_WITH_REMOVE(applist, app) {
                g_assert(app);
 
@@ -130,13 +148,17 @@ GVariant *proc_usage_application_get_all_memory_usages(void)
                appusage = g_variant_new("(su)", app->appid,
                                PAGE_TO_KBYTE(pps.resident - pps.shared));
                g_assert(appusage);
-
                g_variant_builder_add_value(&builder, appusage);
        }
 
        return g_variant_new("(a(su))", &builder);
 }
 
+/**
+ * @return  NULL if error occurs. Otherwise, (appid, CPU usage) list
+ * @notice  If there is no launched app, return empty list.
+ *          Empty list is different with NULL
+ */
 GVariant *proc_usage_application_get_all_cpu_usages(void)
 {
        int ret;
@@ -148,14 +170,6 @@ GVariant *proc_usage_application_get_all_cpu_usages(void)
        struct proc_usage_application *app = NULL;
        struct procfs_pid_stat pps;
 
-       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
-
-       applist = proc_usage_application_get_list();
-       if (!applist) {
-               _E("Failed to get applist");
-               return NULL;
-       }
-
        hz = sysconf(_SC_CLK_TCK);
        if (hz <= 0) {
                _E("Failed to get Hertz : %m");
@@ -168,6 +182,14 @@ GVariant *proc_usage_application_get_all_cpu_usages(void)
                return NULL;
        }
 
+       ret = proc_usage_application_get_list(&applist);
+       if (ret < 0) {
+               _E("Failed to get applist (%d)", ret);
+               return NULL;
+       }
+
+       g_variant_builder_init(&builder, G_VARIANT_TYPE("a(su)"));
+
        G_SLIST_FOREACH_WITH_REMOVE(applist, app) {
                g_assert(app);