safe-kill: Add reason to send signal in the log 03/313703/2
authorUnsung Lee <unsung.lee@samsung.com>
Fri, 21 Jun 2024 03:34:02 +0000 (12:34 +0900)
committerUnsung Lee <unsung.lee@samsung.com>
Mon, 1 Jul 2024 07:10:03 +0000 (16:10 +0900)
Add reason to send signal when printing the debug log.

Change-Id: I3c5cd6b93e7c96435764be47cff7a50704c0e01f
Signed-off-by: Unsung Lee <unsung.lee@samsung.com>
src/common/ipc/safe-kill.c
src/common/ipc/safe-kill.h
src/process/proc-monitor.c
src/process/proc-process.c
src/process/watchdog/app-watchdog.c
src/process/watchdog/proc-watchdog-handler.c
src/resource-limiter/freezer/freezer.c
src/resource-limiter/memory/lowmem-controller.c
src/resource-limiter/memory/lowmem-limit.c
tests/test-safe-kill.c

index 7b95e1e..dfad50e 100644 (file)
@@ -60,7 +60,29 @@ EXPORT_TEST bool is_process_coredumping(pid_t pid)
        return (val == 1);
 }
 
-int safe_kill(pid_t pid, int sig)
+static char* convert_reason_to_string(enum safe_kill_reason safe_kill_reason)
+{
+       switch (safe_kill_reason) {
+       case SAFE_KILL_BY_UNKNOWN_REASON:
+               return "unknown reason";
+       case SAFE_KILL_BY_OOM:
+               return "out of memory";
+       case SAFE_KILL_BY_WATCHDOG:
+               return "watchdog";
+       case SAFE_KILL_BY_MEMORY_LIMIT:
+               return "memory limit";
+       case SAFE_KILL_BY_CGROUP_PROC_KILL:
+               return "cgroup process killed";
+       case SAFE_KILL_BY_FREEZER:
+               return "freezer";
+       case SAFE_KILL_BY_MEMORY_SWEEP:
+               return "memory sweep";
+       default:
+               return "";
+       }
+}
+
+int safe_kill(pid_t pid, int sig, enum safe_kill_reason safe_kill_reason)
 {
        /* Pass signals other than SIGKILL, as other signals are not delivered to
         * a process that's dumping a core anyway, and SIGKILL is the only one that
@@ -68,7 +90,8 @@ int safe_kill(pid_t pid, int sig)
         * Alternatively, with SIGKILL check if process is coredumping.
         */
        if (sig != SIGKILL || !is_process_coredumping(pid)) {
-               _D("safe-kill: delivering signal %d to process %d immediately", sig, pid);
+               _D("safe-kill: Send signal (%d) to process (%d) by %s",
+                               sig, pid, convert_reason_to_string(safe_kill_reason));
                return kill(pid, sig);
        }
 
index 7f0de4b..e44aba1 100644 (file)
 extern "C" {
 #endif /* __cplusplus */
 
-int safe_kill(pid_t pid, int sig);
+enum safe_kill_reason {
+       SAFE_KILL_BY_UNKNOWN_REASON = 0,
+       SAFE_KILL_BY_OOM,
+       SAFE_KILL_BY_WATCHDOG,
+       SAFE_KILL_BY_MEMORY_LIMIT,
+       SAFE_KILL_BY_CGROUP_PROC_KILL,
+       SAFE_KILL_BY_FREEZER,
+       SAFE_KILL_BY_MEMORY_SWEEP,
+};
+
+int safe_kill(pid_t pid, int sig, enum safe_kill_reason safe_kill_reason);
 
 #ifdef _UNIT_TEST
 bool is_process_coredumping(pid_t pid);
index c4e9246..c4906b0 100644 (file)
@@ -933,7 +933,7 @@ static gboolean check_app_watchdog_cb(gpointer data)
        ret = proc_get_oom_score_adj(pid, &oom_score_adj);
        if (!ret) {
                _E("app watchdog pid %d not terminated, kill again\n", pid);
-               safe_kill(pid, SIGKILL);
+               safe_kill(pid, SIGKILL, SAFE_KILL_BY_WATCHDOG);
        }
        app_watchdog_check_timer = NULL;
        app_watchdog.pid = -1;
@@ -1006,7 +1006,7 @@ static void proc_dbus_app_watchdog_handler(GVariant *params)
        if (app_watchdog_check_timer) {
                if (app_watchdog.pid == pid) {
                        _E("[WATCHDOG] app %s, pid %d has already received watchdog siganl but not terminated", pai->appid, pid);
-                       safe_kill(pid, SIGKILL);
+                       safe_kill(pid, SIGKILL, SAFE_KILL_BY_WATCHDOG);
                        app_watchdog.pid = -1;
                        app_watchdog.signum = -1;
                        return;
@@ -1015,7 +1015,7 @@ static void proc_dbus_app_watchdog_handler(GVariant *params)
 
        resourced_proc_status_change(PROC_CGROUP_SET_TERMINATE_REQUEST,
                    pid, NULL, NULL, PROC_TYPE_NONE);
-       safe_kill(pid, SIGABRT);
+       safe_kill(pid, SIGABRT, SAFE_KILL_BY_WATCHDOG);
        if (!app_watchdog_check_timer) {
                app_watchdog_check_timer = g_timeout_source_new_seconds(APP_WATCHDOG_TIMER_INTERVAL);
                g_source_set_callback(app_watchdog_check_timer, check_app_watchdog_cb, NULL, NULL);
index 976f185..ba9a712 100644 (file)
@@ -320,8 +320,7 @@ void proc_kill_victiom(gpointer key, gpointer value, gpointer user_data)
        }
        cur_oom = atoi(buf);
        if (cur_oom >= OOMADJ_FAVORITE) {
-               safe_kill(pid, SIGKILL);
-               _D("sweep memory : background process %d killed by sigkill", pid);
+               safe_kill(pid, SIGKILL, SAFE_KILL_BY_MEMORY_SWEEP);
        }
        fclose(fp);
 }
@@ -390,7 +389,7 @@ int proc_sweep_memory(enum proc_sweep_type type, pid_t callpid)
                        piddata = g_new(gint, 1);
                        *piddata = pid;
                        g_hash_table_insert(proc_sweep_list, piddata, NULL);
-                       safe_kill(pid, SIGTERM);
+                       safe_kill(pid, SIGTERM, SAFE_KILL_BY_MEMORY_SWEEP);
                        _D("sweep memory : background process %d(%s) killed",
                                        pid, appname);
                        count++;
index fa57258..94e8810 100644 (file)
@@ -82,7 +82,7 @@ static gboolean app_watchdog_cb(gpointer data)
        if (!g_slist_find(app_watchdog_list, wai))
                return false;
 
-       if (safe_kill(wai->pid, 0) == -1) {
+       if (safe_kill(wai->pid, 0, SAFE_KILL_BY_WATCHDOG) == -1) {
                _I("[WATCHDOG] %d process doesn't exit, remove watchdog handler", wai->pid);
                app_watchdog_list = g_slist_remove(app_watchdog_list, wai);
                free(wai);
index c9aad68..79a8ae4 100644 (file)
@@ -57,7 +57,7 @@ static int run_exec(char **argv)
                        } else {
                                _D("[WATCHDOG] timeout!!, kill child process(%s)\n",
                                    argv[0]);
-                               safe_kill(pid, SIGKILL);
+                               safe_kill(pid, SIGKILL, SAFE_KILL_BY_CGROUP_PROC_KILL);
                        }
                }
        } while (wpid == 0 && times <= MAX_TIMES);
index 0cf0b66..743f537 100644 (file)
@@ -219,7 +219,7 @@ static void maybe_kill(int pid, const struct proc_app_info *pai)
                return;
 
        _E("Kill broken app=%s(DBus ConnectionOverflow)", pai->appid);
-       safe_kill(pid, SIGKILL); // ðŸš®
+       safe_kill(pid, SIGKILL, SAFE_KILL_BY_FREEZER); // ðŸš®
 }
 
 static bool was_thawed_recently(const struct proc_app_info *pai)
index 379257a..2f89429 100644 (file)
@@ -94,9 +94,9 @@ static int lowmem_kill_victim(const struct task_info *tsk, int flags,
        }
 
        if (sigterm)
-               safe_kill(pid, SIGTERM);
+               safe_kill(pid, SIGTERM, SAFE_KILL_BY_OOM);
        else
-               safe_kill(pid, SIGKILL);
+               safe_kill(pid, SIGKILL, SAFE_KILL_BY_OOM);
 
        _D("[LMK] we killed, force(%d), %d (%s) score = %d, size: rss = %u KB, sigterm = %d\n",
                        flags & OOM_FORCE, pid, appname, tsk->oom_score_adj,
index ec9c9b0..3a562d6 100644 (file)
@@ -161,7 +161,7 @@ static gboolean liveness_check_cb(gpointer data)
 
                if (kill(pid, 0) == 0) {
                        _I("[MEMORY-LIMIT] pid (%d) is still alive, so kill forcibly", pid);
-                       safe_kill(pid, SIGKILL);
+                       safe_kill(pid, SIGKILL, SAFE_KILL_BY_MEMORY_LIMIT);
                }
        }
 
@@ -239,7 +239,7 @@ static int handle_trigger_threshold(enum proc_action proc_action,
 
                        if (lowmem_limit_broadcast(pid)) {
                                _E("[MEMORY-LIMIT] Failed to broadcast of process (%s)", cg_dir);
-                               safe_kill(pid, SIGTERM);
+                               safe_kill(pid, SIGTERM, SAFE_KILL_BY_MEMORY_LIMIT);
                        }
                }
 
@@ -260,7 +260,7 @@ static int handle_trigger_threshold(enum proc_action proc_action,
                        if (!pid)
                                continue;
 
-                       safe_kill(pid, SIGTERM);
+                       safe_kill(pid, SIGTERM, SAFE_KILL_BY_MEMORY_LIMIT);
                }
                g_timeout_add_seconds(2, liveness_check_cb, pids_array);
                break;
index 11d0665..edfa689 100644 (file)
@@ -105,7 +105,7 @@ static void safe_kill_positive(void **state)
        expect_string(__wrap_fopen, pathname, "/proc/3/status");
        expect_string(__wrap_fopen, mode, "r");
 
-       assert(safe_kill(3, SIGKILL) == 0);
+       assert(safe_kill(3, SIGKILL, SAFE_KILL_BY_UNKNOWN_REASON) == 0);
 }
 
 static void safe_kill_negative(void **state)
@@ -114,7 +114,7 @@ static void safe_kill_negative(void **state)
        will_return(__wrap_kill, true);
        expect_value(__wrap_kill, pid, 1);
        expect_value(__wrap_kill, sig, SIGUSR1);
-       assert(safe_kill(1, SIGUSR1) == 0);
+       assert(safe_kill(1, SIGUSR1, SAFE_KILL_BY_UNKNOWN_REASON) == 0);
 
        /* SIGKILL should be delivered if the process is not coredumping */
        will_return(__wrap_fopen, 1);
@@ -125,7 +125,7 @@ static void safe_kill_negative(void **state)
        will_return(__wrap_kill, true);
        expect_value(__wrap_kill, pid, 2);
        expect_value(__wrap_kill, sig, SIGKILL);
-       assert(safe_kill(2, SIGKILL) == 0);
+       assert(safe_kill(2, SIGKILL, SAFE_KILL_BY_UNKNOWN_REASON) == 0);
 
 }