From e8cab4f3a451b81930d19790d5cf951b269a1c53 Mon Sep 17 00:00:00 2001 From: Yunmi Ha Date: Tue, 28 Apr 2020 15:20:30 +0900 Subject: [PATCH] cleanup: add log file for file space usage - When cleanup is needed, save result of "du -ah" command. - system type: "du -ah /opt/ > /var/log/storage/opt_full.log" - user type: "du -ah /opt/usr/ > /var/log/storage/opt_usr_full.log" Change-Id: I1c13873e88bb8305b038ba93604bc572f3e1b48b Signed-off-by: Yunmi Ha Signed-off-by: Hyotaek Shim --- src/storage/cleanup.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/storage/cleanup.c b/src/storage/cleanup.c index f27f4b1..712ac86 100644 --- a/src/storage/cleanup.c +++ b/src/storage/cleanup.c @@ -14,13 +14,17 @@ * limitations under the License. */ +#define _GNU_SOURCE #include #include #include +#include #include #include #include #include +#include +#include #include #include #include @@ -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); -- 2.7.4