diff --git a/lmkd.cpp b/lmkd.cpp index b028d2c..7e23f77 100644 --- a/lmkd.cpp +++ b/lmkd.cpp @@ -2088,7 +2088,7 @@ static void watchdog_callback() { continue; } - if (reaper.kill({ target.pidfd, target.pid }, true) == 0) { + if (reaper.kill({ target.pidfd, target.pid, target.uid }, true) == 0) { ALOGW("lmkd watchdog killed process %d, oom_score_adj %d", target.pid, oom_score); killinfo_log(&target, 0, 0, 0, NULL, NULL, NULL, NULL); break; @@ -2260,7 +2260,7 @@ static int kill_one_process(struct proc* procp, int min_oom_score, struct kill_i trace_kill_start(pid, desc); start_wait_for_proc_kill(pidfd < 0 ? pid : pidfd); - kill_result = reaper.kill({ pidfd, pid }, false); + kill_result = reaper.kill({ pidfd, pid, uid }, false); trace_kill_end(); diff --git a/reaper.cpp b/reaper.cpp index af4cf5b..2c9e737 100644 --- a/reaper.cpp +++ b/reaper.cpp @@ -141,12 +141,16 @@ bool Reaper::init(int comm_fd) { return true; } -static void set_process_group_and_prio(int pid, const std::vector& profiles, +static void set_process_group_and_prio(uid_t uid, int pid, const std::vector& profiles, int prio) { DIR* d; char proc_path[PATH_MAX]; struct dirent* de; + if (!SetProcessProfilesCached(uid, pid, profiles)) { + ALOGW("Failed to set task profiles for the process (%d) being killed", pid); + } + snprintf(proc_path, sizeof(proc_path), "/proc/%d/task", pid); if (!(d = opendir(proc_path))) { ALOGW("Failed to open %s; errno=%d: process pid(%d) might have died", proc_path, errno, @@ -168,11 +172,6 @@ static void set_process_group_and_prio(int pid, const std::vector& if (setpriority(PRIO_PROCESS, t_pid, prio) && errno != ESRCH) { ALOGW("Unable to raise priority of killing t_pid (%d): errno=%d", t_pid, errno); } - - if (!SetTaskProfiles(t_pid, profiles, true)) { - ALOGW("Failed to set task_profiles on pid(%d) t_pid(%d)", pid, t_pid); - continue; - } } closedir(d); } @@ -195,12 +194,13 @@ bool Reaper::async_kill(const struct target_proc& target) { // Duplicate pidfd instead of reusing the original one to avoid synchronization and refcounting // when both reaper and main threads are using or closing the pidfd - queue_.push_back({ dup(target.pidfd), target.pid }); + queue_.push_back({ dup(target.pidfd), target.pid, target.uid }); // Wake up a reaper thread cond_.notify_one(); mutex_.unlock(); - set_process_group_and_prio(target.pid, {"CPUSET_SP_FOREGROUND", "SCHED_SP_FOREGROUND"}, + set_process_group_and_prio(target.uid, target.pid, + {"CPUSET_SP_FOREGROUND", "SCHED_SP_FOREGROUND"}, ANDROID_PRIORITY_HIGHEST); return true; diff --git a/reaper.h b/reaper.h index 36fa3f5..e20d892 100644 --- a/reaper.h +++ b/reaper.h @@ -25,6 +25,7 @@ public: struct target_proc { int pidfd; int pid; + uid_t uid; }; private: // mutex_ and cond_ are used to wakeup the reaper thread.