storage: Check /opt partition storage size and add NeedCleanup signal 14/181114/1 accepted/tizen/4.0/unified/20180614.125423 submit/tizen_4.0/20180614.073053
authorpr.jung <pr.jung@samsung.com>
Fri, 8 Jun 2018 07:58:37 +0000 (16:58 +0900)
committerpr.jung <pr.jung@samsung.com>
Fri, 8 Jun 2018 07:58:37 +0000 (16:58 +0900)
- Check /opt/usr, /tmp, /opt size
- Broadcast NeedCleanup signal when memory level is changed
   normal -> warning/critical/full
   warning -> critical/full
   critical -> full

Change-Id: I13019339bf8ab96453ad967386f2b123bb48c8e1
Signed-off-by: pr.jung <pr.jung@samsung.com>
src/storage/storage.c

index 79c1c1f..8f50517 100755 (executable)
@@ -37,6 +37,7 @@
 #include "module-intf.h"
 
 #define MEMORY_STATUS_TMP_PATH  "/tmp"
+#define MEMORY_STATUS_OPT_PATH  "/opt"
 #define MEMNOTI_TMP_CRITICAL_VALUE (20)
 
 #define MEMORY_MEGABYTE_VALUE   1048576
@@ -45,6 +46,7 @@
 #define MEMNOTI_CRITICAL_VALUE (0.1) /* 0.1% under */
 #define MEMNOTI_FULL_VALUE     (0.0) /* 0.0% under */
 
+#define SIGNAL_NEED_CLEANUP     "NeedCleanup"
 #define SIGNAL_LOWMEM_STATE     "ChangeState"
 #define SIGNAL_LOWMEM_FULL      "Full"
 #define MEMNOTI_TIMER_INTERVAL  5000 /* milliseconds */
 #define ARRAY_SIZE(name) (sizeof(name)/sizeof(name[0]))
 
 enum memnoti_level {
-       MEMNOTI_LEVEL_CRITICAL = 0,
+       MEMNOTI_LEVEL_FULL = 0,
+       MEMNOTI_LEVEL_CRITICAL,
        MEMNOTI_LEVEL_WARNING,
        MEMNOTI_LEVEL_NORMAL,
-       MEMNOTI_LEVEL_FULL,
 };
 
 enum memnoti_status {
@@ -67,7 +69,14 @@ enum memnoti_status {
        MEMNOTI_ENABLE,
 };
 
+enum memory_id {
+       MEMORY_INTERNAL = 0,
+       MEMORY_TMP,
+       MEMORY_OPT,
+};
+
 struct storage_config_info {
+       enum memory_id mem_id;
        enum memnoti_level current_noti_level;
        double warning_level;
        double critical_level;
@@ -77,6 +86,7 @@ struct storage_config_info {
 static guint memnoti_timer;
 
 static struct storage_config_info storage_internal_info = {
+       .mem_id             = MEMORY_INTERNAL,
        .current_noti_level = MEMNOTI_LEVEL_NORMAL,
        .warning_level      = MEMNOTI_WARNING_VALUE,
        .critical_level     = MEMNOTI_CRITICAL_VALUE,
@@ -84,17 +94,29 @@ static struct storage_config_info storage_internal_info = {
 };
 
 static struct storage_config_info storage_tmp_info = {
+       .mem_id             = MEMORY_TMP,
        .current_noti_level = MEMNOTI_LEVEL_NORMAL,
        .warning_level      = MEMNOTI_TMP_CRITICAL_VALUE,
        .critical_level     = MEMNOTI_TMP_CRITICAL_VALUE,
        .full_level         = MEMNOTI_FULL_VALUE,
 };
 
-static void memnoti_send_broadcast(char *signal, int status)
+static struct storage_config_info storage_opt_info = {
+       .mem_id             = MEMORY_OPT,
+       .current_noti_level = MEMNOTI_LEVEL_NORMAL,
+       .warning_level      = MEMNOTI_WARNING_VALUE,
+       .critical_level     = MEMNOTI_CRITICAL_VALUE,
+       .full_level         = MEMNOTI_FULL_VALUE,
+};
+
+static void memstatus_send_broadcast(enum memory_id no, char *signal, int status)
 {
        char *arr[1];
        char str_status[32];
 
+       if (no == MEMORY_OPT)
+               return;
+
        _I("signal %s status %d", signal, status);
        snprintf(str_status, sizeof(str_status), "%d", status);
        arr[0] = str_status;
@@ -102,6 +124,40 @@ static void memnoti_send_broadcast(char *signal, int status)
                        signal, "i", arr);
 }
 
+static void memcleanup_send_broadcast(struct storage_config_info *info, enum memnoti_level level, enum memnoti_level prev_level)
+{
+       const char *path;
+       char *value;
+       char *arr[2];
+
+       if (info->mem_id == MEMORY_INTERNAL)
+               path = tzplatform_getenv(TZ_SYS_HOME);
+       else if (info->mem_id == MEMORY_TMP)
+               path = MEMORY_STATUS_TMP_PATH;
+       else if (info->mem_id == MEMORY_OPT)
+               path = MEMORY_STATUS_OPT_PATH;
+       else
+               return;
+
+       if (prev_level <= level)
+               return;
+
+       if (level == MEMNOTI_LEVEL_WARNING)
+               value = "warning";
+       else if (level == MEMNOTI_LEVEL_CRITICAL)
+               value = "critical";
+       else if (level == MEMNOTI_LEVEL_FULL)
+               value = "full";
+       else
+               return;
+
+       _I("%s level: %s", path, value);
+       arr[0] = (char *)path;
+       arr[1] = value;
+       broadcast_dbus_signal(STORAGED_PATH_LOWMEM, STORAGED_INTERFACE_LOWMEM,
+                       SIGNAL_NEED_CLEANUP, "ss", arr);
+}
+
 static int launch_memory_popup(int num, ...)
 {
        int ret;
@@ -150,15 +206,20 @@ static void storage_status_broadcast(struct storage_config_info *info, double to
 {
        double level = (avail/total)*100;
        int status = MEMNOTI_DISABLE;
+       enum memnoti_level prev_noti_level;
 
+       prev_noti_level = info->current_noti_level;
        if (level <= info->full_level) {
                if (info->current_noti_level == MEMNOTI_LEVEL_FULL)
                        return;
                info->current_noti_level = MEMNOTI_LEVEL_FULL;
                status = MEMNOTI_ENABLE;
-               memnoti_send_broadcast(SIGNAL_LOWMEM_FULL, status);
-               _I("current level %4.4lf w:%4.4lf c:%4.4lf f:%4.4lf",
-                       level, info->warning_level, info->critical_level, info->full_level);
+               memstatus_send_broadcast(info->mem_id, SIGNAL_LOWMEM_FULL, status);
+
+               memcleanup_send_broadcast(info, info->current_noti_level, prev_noti_level);
+
+               _I("%d current level %4.4lf w:%4.4lf c:%4.4lf f:%4.4lf",
+                       info->mem_id, level, info->warning_level, info->critical_level, info->full_level);
                return;
        }
 
@@ -166,22 +227,29 @@ static void storage_status_broadcast(struct storage_config_info *info, double to
                if (info->current_noti_level == MEMNOTI_LEVEL_CRITICAL)
                        return;
                if (info->current_noti_level == MEMNOTI_LEVEL_FULL)
-                       memnoti_send_broadcast(SIGNAL_LOWMEM_FULL, status);
+                       memstatus_send_broadcast(info->mem_id, SIGNAL_LOWMEM_FULL, status);
                info->current_noti_level = MEMNOTI_LEVEL_CRITICAL;
                status = MEMNOTI_ENABLE;
-               memnoti_send_broadcast(SIGNAL_LOWMEM_STATE, status);
-               _I("current level %4.4lf w:%4.4lf c:%4.4lf f:%4.4lf",
-                       level, info->warning_level, info->critical_level, info->full_level);
+               memstatus_send_broadcast(info->mem_id, SIGNAL_LOWMEM_STATE, status);
+
+               memcleanup_send_broadcast(info, info->current_noti_level, prev_noti_level);
+
+               _I("%d current level %4.4lf w:%4.4lf c:%4.4lf f:%4.4lf",
+                       info->mem_id, level, info->warning_level, info->critical_level, info->full_level);
                return;
        }
 
        if (info->current_noti_level == MEMNOTI_LEVEL_FULL)
-               memnoti_send_broadcast(SIGNAL_LOWMEM_FULL, status);
+               memstatus_send_broadcast(info->mem_id, SIGNAL_LOWMEM_FULL, status);
        if (info->current_noti_level == MEMNOTI_LEVEL_CRITICAL)
-               memnoti_send_broadcast(SIGNAL_LOWMEM_STATE, status);
-       if (level <= info->warning_level)
+               memstatus_send_broadcast(info->mem_id, SIGNAL_LOWMEM_STATE, status);
+       if (level <= info->warning_level) {
                info->current_noti_level = MEMNOTI_LEVEL_WARNING;
-       else
+               _I("%d current level %4.4lf w:%4.4lf c:%4.4lf f:%4.4lf",
+                       info->mem_id, level, info->warning_level, info->critical_level, info->full_level);
+
+               memcleanup_send_broadcast(info, info->current_noti_level, prev_noti_level);
+       } else
                info->current_noti_level = MEMNOTI_LEVEL_NORMAL;
 }
 
@@ -258,6 +326,11 @@ static gboolean check_storage_status(gpointer data)
        dTotal = (double)s.f_frsize * s.f_blocks;
        dAvail = (double)s.f_bsize * s.f_bavail;
        storage_status_broadcast(&storage_tmp_info, dTotal, dAvail);
+       /* check opt */
+       storage_get_memory_size(MEMORY_STATUS_OPT_PATH, &s);
+       dTotal = (double)s.f_frsize * s.f_blocks;
+       dAvail = (double)s.f_bsize * s.f_bavail;
+       storage_status_broadcast(&storage_opt_info, dTotal, dAvail);
 
        if (memnoti_timer) {
                g_source_remove(memnoti_timer);
@@ -272,6 +345,7 @@ static int init_storage_config_info_all(void)
 {
        init_storage_config_info(tzplatform_getenv(TZ_SYS_HOME), &storage_internal_info);
        init_storage_config_info(MEMORY_STATUS_TMP_PATH, &storage_tmp_info);
+       init_storage_config_info(MEMORY_STATUS_OPT_PATH, &storage_opt_info);
        memnoti_timer = g_timeout_add(MEMNOTI_TIMER_INTERVAL,
                                check_storage_status, NULL);
        if (memnoti_timer == 0)