From 2daa638dfeb2628f9668ae4ed3a739c37e6f7453 Mon Sep 17 00:00:00 2001 From: Yunhee Seo Date: Fri, 28 Apr 2023 15:36:37 +0900 Subject: [PATCH] display: Relocate get_pname() to reduce plugin duplication Remove and redefine get_pname function in plugins/display/core.c This function is added to below display-misc. void display_misc_get_process_name(pid_t pid, char *pname) -> This function gets process name by pid parameter. During display plugins refactoring, this function will be relocated. To use macro value in poll.h, poll.h is relocated to core from plugin-common. All plugin-common files will be moved to display core during refactoring process. Change-Id: Iaaa32254bd93ee37f7bf5bfe5edafac4beb5ab83 Signed-off-by: Yunhee Seo --- plugins/iot-headed/display/core.c | 32 +++----------------------------- plugins/mobile/display/core.c | 32 +++----------------------------- plugins/tv/display/core.c | 32 +++----------------------------- plugins/wearable/display/core.c | 32 +++----------------------------- src/display/display-misc.c | 31 +++++++++++++++++++++++++++++++ src/display/display-misc.h | 2 ++ src/display/{plugin-common => }/poll.h | 0 7 files changed, 45 insertions(+), 116 deletions(-) rename src/display/{plugin-common => }/poll.h (100%) diff --git a/plugins/iot-headed/display/core.c b/plugins/iot-headed/display/core.c index 5fef73b..1da1c84 100644 --- a/plugins/iot-headed/display/core.c +++ b/plugins/iot-headed/display/core.c @@ -377,32 +377,6 @@ void set_lcd_paneloff_mode(int val) disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL); } -void get_pname(pid_t pid, char *pname) -{ - char buf[PATH_MAX]; - int cmdline, r; - - if (pid >= INTERNAL_LOCK_BASE) - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", getpid()); - else - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid); - - cmdline = open(buf, O_RDONLY); - if (cmdline < 0) { - pname[0] = '\0'; - _E("Process(%d) does not exist now(may be dead without unlock).", pid); - return; - } - - r = read(cmdline, pname, PATH_MAX); - if ((r >= 0) && (r < PATH_MAX)) - pname[r] = '\0'; - else - pname[0] = '\0'; - - close(cmdline); -} - static void del_state_cond(void *data, enum state_t state) { PmLockNode *tmp = NULL; @@ -858,7 +832,7 @@ static void proc_condition_lock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && (pm_get_power_lock() == POWER_UNLOCK)) @@ -939,7 +913,7 @@ static void proc_condition_unlock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); tmp = find_node(state, pid); del_node(state, tmp); @@ -1073,7 +1047,7 @@ void print_info(int fd) for (s_index = S_NORMAL; s_index < S_END; s_index++) { SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) { - get_pname((pid_t)t->pid, pname); + display_misc_get_process_name((pid_t)t->pid, pname); ctime_r(&t->time, time_buf); time_buf[strlen(time_buf) - 1] = 0; snprintf(buf, sizeof(buf), diff --git a/plugins/mobile/display/core.c b/plugins/mobile/display/core.c index 2762b3b..cf359f1 100644 --- a/plugins/mobile/display/core.c +++ b/plugins/mobile/display/core.c @@ -384,32 +384,6 @@ void set_lcd_paneloff_mode(int val) disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL); } -void get_pname(pid_t pid, char *pname) -{ - char buf[PATH_MAX]; - int cmdline, r; - - if (pid >= INTERNAL_LOCK_BASE) - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", getpid()); - else - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid); - - cmdline = open(buf, O_RDONLY); - if (cmdline < 0) { - pname[0] = '\0'; - _E("Process(%d) does not exist now(may be dead without unlock).", pid); - return; - } - - r = read(cmdline, pname, PATH_MAX); - if ((r >= 0) && (r < PATH_MAX)) - pname[r] = '\0'; - else - pname[0] = '\0'; - - close(cmdline); -} - static void del_state_cond(void *data, enum state_t state) { PmLockNode *tmp = NULL; @@ -869,7 +843,7 @@ static void proc_condition_lock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && (pm_get_power_lock() == POWER_UNLOCK)) @@ -949,7 +923,7 @@ static void proc_condition_unlock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); tmp = find_node(state, pid); del_node(state, tmp); @@ -1083,7 +1057,7 @@ void print_info(int fd) for (s_index = S_NORMAL; s_index < S_END; s_index++) { SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) { - get_pname((pid_t)t->pid, pname); + display_misc_get_process_name((pid_t)t->pid, pname); ctime_r(&t->time, time_buf); time_buf[strlen(time_buf) - 1] = 0; snprintf(buf, sizeof(buf), diff --git a/plugins/tv/display/core.c b/plugins/tv/display/core.c index bb65640..f6adbad 100644 --- a/plugins/tv/display/core.c +++ b/plugins/tv/display/core.c @@ -377,32 +377,6 @@ void set_lcd_paneloff_mode(int val) disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL); } -void get_pname(pid_t pid, char *pname) -{ - char buf[PATH_MAX]; - int cmdline, r; - - if (pid >= INTERNAL_LOCK_BASE) - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", getpid()); - else - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid); - - cmdline = open(buf, O_RDONLY); - if (cmdline < 0) { - pname[0] = '\0'; - _E("Process(%d) does not exist now(may be dead without unlock).", pid); - return; - } - - r = read(cmdline, pname, PATH_MAX); - if ((r >= 0) && (r < PATH_MAX)) - pname[r] = '\0'; - else - pname[0] = '\0'; - - close(cmdline); -} - static void del_state_cond(void *data, enum state_t state) { PmLockNode *tmp = NULL; @@ -858,7 +832,7 @@ static void proc_condition_lock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && (pm_get_power_lock() == POWER_UNLOCK)) @@ -939,7 +913,7 @@ static void proc_condition_unlock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); tmp = find_node(state, pid); del_node(state, tmp); @@ -1073,7 +1047,7 @@ void print_info(int fd) for (s_index = S_NORMAL; s_index < S_END; s_index++) { SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) { - get_pname((pid_t)t->pid, pname); + display_misc_get_process_name((pid_t)t->pid, pname); ctime_r(&t->time, time_buf); time_buf[strlen(time_buf) - 1] = 0; snprintf(buf, sizeof(buf), diff --git a/plugins/wearable/display/core.c b/plugins/wearable/display/core.c index f0e71dd..4f52af4 100644 --- a/plugins/wearable/display/core.c +++ b/plugins/wearable/display/core.c @@ -654,32 +654,6 @@ void set_lcd_paneloff_mode(int val) disp_plgn->pm_change_internal(INTERNAL_LOCK_PM, LCD_NORMAL); } -void get_pname(pid_t pid, char *pname) -{ - char buf[PATH_MAX]; - int cmdline, r; - - if (pid >= INTERNAL_LOCK_BASE) - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", getpid()); - else - snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid); - - cmdline = open(buf, O_RDONLY); - if (cmdline < 0) { - pname[0] = '\0'; - _E("Process(%d) does not exist now(may be dead without unlock).", pid); - return; - } - - r = read(cmdline, pname, PATH_MAX); - if ((r >= 0) && (r < PATH_MAX)) - pname[r] = '\0'; - else - pname[0] = '\0'; - - close(cmdline); -} - static void del_state_cond(void *data, enum state_t state) { PmLockNode *tmp = NULL; @@ -1168,7 +1142,7 @@ static void proc_condition_lock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); if ((state == S_LCDOFF) && (get_pm_cur_state() == S_SLEEP) && (pm_get_power_lock() == POWER_UNLOCK)) @@ -1249,7 +1223,7 @@ static void proc_condition_unlock(PMMsg *data) return; flags = GET_COND_FLAG(data->cond); - get_pname(pid, pname); + display_misc_get_process_name(pid, pname); tmp = find_node(state, pid); del_node(state, tmp); @@ -1383,7 +1357,7 @@ void print_info(int fd) for (s_index = S_NORMAL; s_index < S_END; s_index++) { SYS_G_LIST_FOREACH(get_cond_head(s_index), elem, t) { - get_pname((pid_t)t->pid, pname); + display_misc_get_process_name((pid_t)t->pid, pname); ctime_r(&t->time, time_buf); time_buf[strlen(time_buf) - 1] = 0; snprintf(buf, sizeof(buf), diff --git a/src/display/display-misc.c b/src/display/display-misc.c index eac18f0..5acd18e 100644 --- a/src/display/display-misc.c +++ b/src/display/display-misc.c @@ -16,9 +16,13 @@ * limitations under the License. */ +#include + #include +#include "shared/log.h" #include "display-misc.h" +#include "poll.h" /* FIXME: This fucntion should be relocated under the battery module */ bool display_misc_is_low_battery_state(int val) @@ -32,3 +36,30 @@ bool display_misc_is_low_battery_state(int val) return false; } + +/* FIXME: The location of this function should be discussed later */ +void display_misc_get_process_name(pid_t pid, char *pname) +{ + char buf[PATH_MAX]; + int cmdline, r; + + if (pid >= INTERNAL_LOCK_BASE) + snprintf(buf, PATH_MAX, "/proc/%d/cmdline", getpid()); + else + snprintf(buf, PATH_MAX, "/proc/%d/cmdline", pid); + + cmdline = open(buf, O_RDONLY); + if (cmdline < 0) { + pname[0] = '\0'; + _E("Process(%d) does not exist now(may be dead without unlock).", pid); + return; + } + + r = read(cmdline, pname, PATH_MAX); + if ((r >= 0) && (r < PATH_MAX)) + pname[r] = '\0'; + else + pname[0] = '\0'; + + close(cmdline); +} diff --git a/src/display/display-misc.h b/src/display/display-misc.h index aabb477..ef4a78e 100644 --- a/src/display/display-misc.h +++ b/src/display/display-misc.h @@ -25,7 +25,9 @@ #define __DISPLAY_MISC_H__ #include +#include bool display_misc_is_low_battery_state(int val); +void display_misc_get_process_name(pid_t pid, char *pname); #endif /* __DISPLAY_MISC_H__ */ \ No newline at end of file diff --git a/src/display/plugin-common/poll.h b/src/display/poll.h similarity index 100% rename from src/display/plugin-common/poll.h rename to src/display/poll.h -- 2.7.4