cleanup: add log file for file space usage
[platform/core/system/storaged.git] / src / storage / cleanup.c
index f27f4b1..712ac86 100644 (file)
  * limitations under the License.
  */
 
+#define _GNU_SOURCE
 #include <unistd.h>
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdlib.h>
 #include <pthread.h>
 #include <assert.h>
 #include <json-c/json.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <grp.h>
 #include <gmodule.h>
 #include <limits.h>
 #include <regex.h>
@@ -64,6 +68,10 @@ struct cleanup_history {
 
 #define CLEANUP_INTERVAL_SEC   (10 * 60)
 
+#define CLEANUP_STORAGE_FULL_LOG_PATH          "/var/log/storage"
+#define CLEANUP_STORAGE_FULL_SYSTEM_FILE       CLEANUP_STORAGE_FULL_LOG_PATH"/opt_full.log"
+#define CLEANUP_STORAGE_FULL_USER_FILE         CLEANUP_STORAGE_FULL_LOG_PATH"/opt_usr_full.log"
+
 static pthread_mutex_t mutex_cancel;
 static pthread_mutex_t mutex_lock;
 static pthread_t cleanup_th = 0;
@@ -260,6 +268,68 @@ static void update_history(enum cleanup_running_type type, int level)
        }
 }
 
+static void save_log_file(enum cleanup_running_type type, int path_id)
+{
+       const char *logpath;
+       char *du_cmd = NULL;
+       int ret;
+       struct group *group_entry;
+       const char *cleanup_path;
+
+       if (type == CLEANUP_TYPE_SYSTEM)
+               logpath = CLEANUP_STORAGE_FULL_SYSTEM_FILE;
+       else if (type == CLEANUP_TYPE_USER)
+               logpath = CLEANUP_STORAGE_FULL_USER_FILE;
+       else {
+               _E("Invalied cleanup type : %u", type);
+               return ;
+       }
+
+       cleanup_path = tzplatform_getenv(path_id);
+       if (!cleanup_path) {
+               _E("Failed to get cleanup root path.");
+               return ;
+       }
+
+       errno = 0;
+       ret = mkdir(CLEANUP_STORAGE_FULL_LOG_PATH, 0755);
+       if ((ret < 0) && (errno != EEXIST)) {
+               _E("Failed to mkdir: %m");
+               return ;
+       }
+
+       ret = asprintf(&du_cmd, "du -ah %s/>%s 2>&1", cleanup_path, logpath);
+       if (ret < 0) {
+               _E("Failed to allocate memory.");
+               return ;
+       }
+
+       ret = system(du_cmd);
+       if ((ret == -1) || (ret == 127))
+               _E("Failed to run '%s' command: %d", du_cmd, ret);
+       else
+               _D("Save log: %s", du_cmd);
+
+       free(du_cmd);
+
+       errno = 0;
+       group_entry = getgrnam("system_share");
+       if (!group_entry) {
+               _E("Failed to getgrnam: %m");
+               return ;
+       }
+
+       if (chown(logpath, -1, group_entry->gr_gid) < 0) {
+               _E("Failed to chown: %m");
+               return ;
+       }
+
+       if (chmod(logpath, 0666) < 0) {
+               _E("Failed to chmod: %m");
+               return ;
+       }
+}
+
 static int add_request_queue(int type, int level)
 {
        int ret = 0;
@@ -384,6 +454,7 @@ void cleanup_storage(enum tzplatform_variable path_id, int level)
        //_D("Add cleanup request.(type:%d, level:%d)", type, level);
 
        update_history(type, level);
+       save_log_file(type, path_id);
 
        if (bth) {
                ret = pthread_create(&cleanup_th, NULL, cleanup_storage_start, NULL);