From d0afa4f141825fe0b6b426e9cdceb3efefded1ec Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 28 Dec 2015 10:07:53 +0900 Subject: [PATCH] Rafactor aul proc api - rename the api prefix - remove unnecessary apis Change-Id: I593025eaf3a4c2ceb892cc048fd432c3b673213d Signed-off-by: Hwankyu Jhun --- CMakeLists.txt | 13 +-- include/aul_proc.h | 35 ++++++ include/aul_util.h | 31 +++-- include/menu_db_util.h | 3 +- include/simple_util.h | 95 --------------- src/app_group.c | 1 - src/app_signal.c | 2 +- src/aul_path.c | 1 - src/aul_proc.c | 153 +++++++++++++++++++++++++ src/aul_sock.c | 2 +- src/key.c | 1 - src/launch.c | 3 +- src/launch_glib.c | 3 +- src/launch_with_result.c | 3 +- src/mime.c | 2 +- src/miregex.c | 5 +- src/pkginfo.c | 8 +- src/service.c | 3 +- src/service_db.c | 2 +- src/simple_util.c | 292 ----------------------------------------------- 20 files changed, 225 insertions(+), 433 deletions(-) create mode 100644 include/aul_proc.h delete mode 100644 include/simple_util.h create mode 100644 src/aul_proc.c delete mode 100644 src/simple_util.c diff --git a/CMakeLists.txt b/CMakeLists.txt index b8f827c..71cd4ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -44,11 +44,6 @@ SET(CMAKE_SKIP_BUILD_RPATH true) # Linker flags SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed") -# aul_mods : modules (static library) -add_library(aul_mods STATIC - src/simple_util.c - ) - # aul add_library(aul SHARED src/pkginfo.c @@ -66,8 +61,9 @@ add_library(aul SHARED src/app_group.c src/aul_rsc_mgr.c src/aul_sock.c + src/aul_proc.c ) -TARGET_LINK_LIBRARIES(aul aul_mods ${libpkgs_LDFLAGS}) +TARGET_LINK_LIBRARIES(aul ${libpkgs_LDFLAGS}) SET_TARGET_PROPERTIES(aul PROPERTIES SOVERSION ${MAJORVER}) SET_TARGET_PROPERTIES(aul PROPERTIES VERSION ${FULLVER}) INSTALL(TARGETS aul DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) @@ -76,14 +72,14 @@ INSTALL(TARGETS aul DESTINATION ${LIB_INSTALL_DIR} COMPONENT RuntimeLibraries) SET(APP_LAUNCHER "app_launcher") ADD_EXECUTABLE(${APP_LAUNCHER} app_launcher.c) SET_TARGET_PROPERTIES(${APP_LAUNCHER} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS} -fPIE") -TARGET_LINK_LIBRARIES(${APP_LAUNCHER} aul aul_mods ${APP_LAUNCHER_DEPS_LDFLAGS} "-pie") +TARGET_LINK_LIBRARIES(${APP_LAUNCHER} aul ${APP_LAUNCHER_DEPS_LDFLAGS} "-pie") INSTALL(TARGETS app_launcher DESTINATION bin) # appgroup info tool SET(APPGROUP_INFO "appgroup_info") ADD_EXECUTABLE(${APPGROUP_INFO} tool/app_group_info.c) SET_TARGET_PROPERTIES(${APPGROUP_INFO} PROPERTIES COMPILE_FLAGS "${EXTRA_CFLAGS} -fPIE") -TARGET_LINK_LIBRARIES(${APPGROUP_INFO} aul aul_mods ${APPGROUP_INFO_DEPS_LDFLAGS} "-pie") +TARGET_LINK_LIBRARIES(${APPGROUP_INFO} aul ${APPGROUP_INFO_DEPS_LDFLAGS} "-pie") INSTALL(TARGETS appgroup_info DESTINATION bin) # pkgconfig file @@ -94,6 +90,7 @@ CONFIGURE_FILE(feature/preexec_list.txt.in feature/preexec_list.txt @ONLY) # Install headers, other files INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/aul.h DESTINATION include/aul) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/aul_sock.h DESTINATION include/aul) +INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/aul_proc.h DESTINATION include/aul) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/aul_rsc_mgr.h DESTINATION include/aul) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/aul_svc.h DESTINATION include/aul) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/aul_cmd.h DESTINATION include/aul) diff --git a/include/aul_proc.h b/include/aul_proc.h new file mode 100644 index 0000000..d49f249 --- /dev/null +++ b/include/aul_proc.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +/* + * This API is only for Appfw internally. + */ +int aul_proc_iter_appid(int (*iterfunc)(const char *dname, const char *appid, void *priv, uid_t uid), void *priv); + +/* + * This API is only for Appfw internally. + */ +char *aul_proc_get_appid_bypid(int pid); + +/* + * This API is only for Appfw internally. + */ +uid_t aul_proc_get_usr_bypid(int pid); diff --git a/include/aul_util.h b/include/aul_util.h index 5df4564..fae8ca7 100644 --- a/include/aul_util.h +++ b/include/aul_util.h @@ -17,25 +17,20 @@ #pragma once #include -#include +#include +#include -#define USE_ENGINE(engine) setenv("ELM_ENGINE", engine, 1); +#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) -#define AUL_UTIL_PID -2 - -#define MAX_PACKAGE_STR_SIZE 512 -#define MAX_PACKAGE_APP_PATH_SIZE 512 -#define MAX_RUNNING_APP_INFO 512 - -#define GSLIST_FOREACH_SAFE(list, l, l_next) \ - for (l = list, \ - l_next = g_slist_next(l); \ - l; \ - l = l_next, \ - l_next = g_slist_next(l)) - -int _add_app_status_info_list(char *appid, int pid, uid_t uid); -int _update_app_status_info_list(int pid, int status, uid_t uid); -int _remove_app_status_info_list(int pid, uid_t uid); +#undef LOG_TAG +#define LOG_TAG "AUL" +#define _E(fmt, arg...) LOGE(fmt, ##arg) +#define _D(fmt, arg...) LOGD(fmt, ##arg) +#define _W(fmt, arg...) LOGW(fmt, ##arg) +#define AUL_UTIL_PID -2 +#define MAX_LOCAL_BUFSZ 128 +#define MAX_PACKAGE_STR_SIZE 512 +#define MAX_PID_STR_BUFSZ 20 +#define MAX_UID_STR_BUFSZ 20 diff --git a/include/menu_db_util.h b/include/menu_db_util.h index 5d15a6f..383c948 100644 --- a/include/menu_db_util.h +++ b/include/menu_db_util.h @@ -19,7 +19,8 @@ #include #include #include -#include "simple_util.h" + +#include "aul_util.h" #define MAX_PATH_LEN 1024 diff --git a/include/simple_util.h b/include/simple_util.h deleted file mode 100644 index 625a913..0000000 --- a/include/simple_util.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#pragma once - -#include -#include -#include -#include - -#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER) - -#undef LOG_TAG -#define LOG_TAG "AUL" - -#ifdef LAUNCHPAD_LOG -#undef LOG_TAG -#define LOG_TAG "AUL_PAD" -#endif - -#ifdef AMD_LOG -#undef LOG_TAG -#define LOG_TAG "AUL_AMD" -#endif - -#ifdef AGENT_LOG -#undef LOG_TAG -#define LOG_TAG "AUL_AGENT" -#endif - -#define MAX_LOCAL_BUFSZ 128 -#define MAX_PID_STR_BUFSZ 20 -#define MAX_UID_STR_BUFSZ 20 - -#define _E(fmt, arg...) LOGE(fmt, ##arg) -#define _D(fmt, arg...) LOGD(fmt, ##arg) -#define _W(fmt, arg...) LOGW(fmt, ##arg) - -#define retvm_if(expr, val, fmt, arg...) do { \ - if (expr) { \ - _E(fmt, ##arg); \ - _E("(%s) -> %s() return", #expr, __FUNCTION__); \ - return (val); \ - } \ -} while (0) - -#define retv_if(expr, val) do { \ - if (expr) { \ - _E("(%s) -> %s() return", #expr, __FUNCTION__); \ - return (val); \ - } \ -} while (0) - -int __proc_iter_appid(int (*iterfunc) - (const char *dname, const char *appid, void *priv, uid_t uid), - void *priv); -int __proc_iter_pgid(int pgid, int (*iterfunc) (int pid, void *priv, uid_t uid), - void *priv); -char *__proc_get_appid_bypid(int pid); -char *__proc_get_cmdline_bypid(int pid); -char *__proc_get_exe_bypid(int pid); -uid_t __proc_get_usr_bypid(int pid); - -static inline const char *FILENAME(const char *filename) -{ - const char *p; - const char *r; - - if (!filename) - return NULL; - - r = p = filename; - while (*p) { - if (*p == '/') - r = p + 1; - p++; - } - - return r; -} - - diff --git a/src/app_group.c b/src/app_group.c index e310e09..acd186a 100644 --- a/src/app_group.c +++ b/src/app_group.c @@ -27,7 +27,6 @@ #include "aul_util.h" #include "aul_sock.h" #include "launch.h" -#include "simple_util.h" API int aul_app_group_get_window(int pid) { diff --git a/src/app_signal.c b/src/app_signal.c index 4992808..21e5d7b 100644 --- a/src/app_signal.c +++ b/src/app_signal.c @@ -22,7 +22,7 @@ #include "app_signal.h" #include "aul_api.h" -#include "simple_util.h" +#include "aul_util.h" #include "aul.h" static int (*_app_dead_handler) (int pid, void *data); diff --git a/src/aul_path.c b/src/aul_path.c index 924fee5..4ef0390 100644 --- a/src/aul_path.c +++ b/src/aul_path.c @@ -29,7 +29,6 @@ #include "aul_api.h" #include "aul_util.h" -#include "simple_util.h" #include "aul.h" #define ROOT_UID 0 diff --git a/src/aul_proc.c b/src/aul_proc.c new file mode 100644 index 0000000..dfa9e1a --- /dev/null +++ b/src/aul_proc.c @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an AS IS BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +#include "aul_api.h" +#include "aul_proc.h" +#include "aul_util.h" + +#define MAX_CMD_BUFSZ 1024 +#define APP_LABEL_PREFIX "User::App::" + +static int __read_proc(const char *path, char *buf, int size) +{ + int fd; + int ret; + + if (buf == NULL || path == NULL) + return -1; + + fd = open(path, O_RDONLY); + if (fd < 0) + return -1; + + ret = read(fd, buf, size - 1); + if (ret <= 0) { + close(fd); + return -1; + } else + buf[ret] = 0; + + close(fd); + + return ret; +} + +static int __find_pid_by_appid(const char *dname, const char *appid, + void *priv, uid_t uid) +{ + int pid = 0; + + if (appid && priv + && strncmp(appid, (char *)priv, MAX_LOCAL_BUFSZ-1) == 0) { + pid = atoi(dname); + if (pid != getpgid(pid)) + pid = 0; + } + + return pid; +} + +API int aul_proc_iter_appid(int (*iterfunc)(const char *dname, const char *appid, void *priv, uid_t uid), void *priv) +{ + DIR *dp; + struct dirent *dentry; + int pid; + int ret; + char buf[MAX_LOCAL_BUFSZ]; + char *p; + uid_t uid; + + dp = opendir("/proc"); + if (dp == NULL) + return -1; + + if (iterfunc == NULL) + iterfunc = __find_pid_by_appid; + + while ((dentry = readdir(dp)) != NULL) { + if (!isdigit(dentry->d_name[0])) + continue; + + uid = aul_proc_get_usr_bypid(atoi(dentry->d_name)); + + snprintf(buf, sizeof(buf), "/proc/%s/attr/current", dentry->d_name); + ret = __read_proc(buf, buf, sizeof(buf)); + if (ret <= 0) + continue; + + p = strstr(buf, APP_LABEL_PREFIX); + /* not an app */ + if (p == NULL) + continue; + p = p + strlen(APP_LABEL_PREFIX); + pid = iterfunc(dentry->d_name, p, priv, uid); + + if (pid > 0) { + closedir(dp); + return pid; + } + } + + closedir(dp); + return -1; +} + +API uid_t aul_proc_get_usr_bypid(int pid) +{ + char buf[MAX_CMD_BUFSZ]; + int ret; + uid_t uid; + struct stat DirStat; + + snprintf(buf, sizeof(buf), "/proc/%d", pid); + ret = stat(buf, &DirStat); + if (ret < 0) + uid = (uid_t)-1; + else + uid = DirStat.st_uid; + + return uid; +} + +API char *aul_proc_get_appid_bypid(int pid) +{ + char buf[MAX_CMD_BUFSZ]; + char *p; + int ret; + + snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid); + ret = __read_proc(buf, buf, sizeof(buf)); + if (ret <= 0) + return NULL; + + p = strstr(buf, APP_LABEL_PREFIX); + /* not an app */ + if (p == NULL) + return NULL; + + p = p + strlen(APP_LABEL_PREFIX); + + return strdup(p); +} diff --git a/src/aul_sock.c b/src/aul_sock.c index 1631081..21929d6 100644 --- a/src/aul_sock.c +++ b/src/aul_sock.c @@ -26,7 +26,7 @@ #include "aul_api.h" #include "aul_sock.h" -#include "simple_util.h" +#include "aul_util.h" #define MAX_NR_OF_DESCRIPTORS 2 diff --git a/src/key.c b/src/key.c index 6c55862..71a26ae 100644 --- a/src/key.c +++ b/src/key.c @@ -25,7 +25,6 @@ #include "aul.h" #include "aul_api.h" #include "menu_db_util.h" -#include "simple_util.h" #include "aul_sock.h" #include "aul_util.h" #include "launch.h" diff --git a/src/launch.c b/src/launch.c index a18f068..5bf2a79 100644 --- a/src/launch.c +++ b/src/launch.c @@ -32,10 +32,9 @@ #include "aul_api.h" #include "aul_sock.h" #include "perf.h" -#include "simple_util.h" +#include "aul_util.h" #include "launch.h" #include "key.h" -#include "aul_util.h" #include "app_signal.h" #define TEP_ISMOUNT_MAX_RETRY_CNT 20 diff --git a/src/launch_glib.c b/src/launch_glib.c index 28c18b7..b91af72 100644 --- a/src/launch_glib.c +++ b/src/launch_glib.c @@ -19,10 +19,11 @@ #include #include #include + #include "aul.h" #include "aul_api.h" +#include "aul_util.h" #include "launch.h" -#include "simple_util.h" static GSource *src; diff --git a/src/launch_with_result.c b/src/launch_with_result.c index f07c9ba..d22613f 100644 --- a/src/launch_with_result.c +++ b/src/launch_with_result.c @@ -25,9 +25,8 @@ #include "aul.h" #include "aul_api.h" #include "aul_sock.h" -#include "simple_util.h" -#include "launch.h" #include "aul_util.h" +#include "launch.h" /*#define ACTIVATE_PREEMPT_FEATURE*/ diff --git a/src/mime.c b/src/mime.c index e85752b..4e72298 100644 --- a/src/mime.c +++ b/src/mime.c @@ -24,7 +24,7 @@ #include "aul_api.h" #include "miregex.h" #include "menu_db_util.h" -#include "simple_util.h" +#include "aul_util.h" static int __match_content_with_regex(const char *content, regex_t *regex_preg) { diff --git a/src/miregex.c b/src/miregex.c index 890ebdc..9efc06d 100644 --- a/src/miregex.c +++ b/src/miregex.c @@ -15,8 +15,6 @@ */ #define _GNU_SOURCE -#include "miregex.h" -#include "simple_util.h" #include #include #include @@ -25,6 +23,9 @@ #include #include +#include "miregex.h" +#include "aul_util.h" + #define MIREGEX_DIR SHARE_PREFIX"/miregex" #define ONELINE_BUF 1024 diff --git a/src/pkginfo.c b/src/pkginfo.c index 0f6c0a8..498dea6 100644 --- a/src/pkginfo.c +++ b/src/pkginfo.c @@ -22,9 +22,9 @@ #include "aul.h" #include "aul_api.h" #include "menu_db_util.h" -#include "simple_util.h" #include "aul_sock.h" #include "aul_util.h" +#include "aul_proc.h" typedef struct _internal_param_t { aul_app_info_iter_fn enum_fn; @@ -114,10 +114,10 @@ static int __get_info_bypid(int pid, char *appid, int len, int cmd) char *result; app_info_from_db *menu_info; uid_t uid; - result = __proc_get_appid_bypid(pid); + result = aul_proc_get_appid_bypid(pid); if (result == NULL) return -1; - uid = __proc_get_usr_bypid(pid); + uid = aul_proc_get_usr_bypid(pid); if ((menu_info = _get_app_info_from_db_by_appid_user(result, uid)) == NULL) { free(result); @@ -144,7 +144,7 @@ static int __get_appid_bypid(int pid, char *appid, int len) { char *result; - result = __proc_get_appid_bypid(pid); + result = aul_proc_get_appid_bypid(pid); if (result == NULL) return -1; diff --git a/src/service.c b/src/service.c index 1b9792f..f0c850f 100755 --- a/src/service.c +++ b/src/service.c @@ -28,12 +28,13 @@ #include #include #include + #include "aul.h" #include "aul_api.h" #include "aul_svc.h" #include "aul_sock.h" #include "aul_svc_db.h" -#include "simple_util.h" +#include "aul_util.h" #include "aul_svc_priv_key.h" #include "launch.h" diff --git a/src/service_db.c b/src/service_db.c index a8a538e..3209b04 100755 --- a/src/service_db.c +++ b/src/service_db.c @@ -25,7 +25,7 @@ #include #include "aul_svc_db.h" -#include "simple_util.h" +#include "aul_util.h" #define APP_INFO_DB_PATH tzplatform_mkpath(TZ_SYS_DB, ".pkgmgr_parser.db") diff --git a/src/simple_util.c b/src/simple_util.c deleted file mode 100644 index 958363b..0000000 --- a/src/simple_util.c +++ /dev/null @@ -1,292 +0,0 @@ -/* - * Copyright (c) 2000 - 2015 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the License); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an AS IS BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#define _GNU_SOURCE -#include -#include -#include -#include -#include -#include -#include -#include "simple_util.h" - -#define BINSH_NAME "/bin/sh" -#define BINSH_SIZE 7 -#define VALGRIND_NAME "/home/developer/sdk_tools/valgrind/usr/bin/valgrind" -#define VALGRIND_SIZE 51 -#define BASH_NAME "/bin/bash" -#define BASH_SIZE 9 -#define OPROFILE_NAME "/usr/bin/oprofile_command" -#define OPROFILE_SIZE 25 -#define OPTION_VALGRIND_NAME "valgrind" -#define OPTION_VALGRIND_SIZE 8 - -#define PROC_STAT_GID_POS 5 - -#define MAX_CMD_BUFSZ 1024 -#define APP_LABEL_PREFIX "User::App::" - -static inline int __read_proc(const char *path, char *buf, int size); -static inline int __find_pid_by_appid(const char *dname, const char *appid, - void *priv, uid_t uid); -static inline int __get_pgid_from_stat(int pid); - - -static inline int __read_proc(const char *path, char *buf, int size) -{ - int fd; - int ret; - - if (buf == NULL || path == NULL) - return -1; - - fd = open(path, O_RDONLY); - if (fd < 0) - return -1; - - ret = read(fd, buf, size - 1); - if (ret <= 0) { - close(fd); - return -1; - } else - buf[ret] = 0; - - close(fd); - - return ret; -} - -static inline int __find_pid_by_appid(const char *dname, const char *appid, - void *priv, uid_t uid) -{ - int pid = 0; - - if (strncmp(appid, (char *)priv, MAX_LOCAL_BUFSZ-1) == 0) { - pid = atoi(dname); - if (pid != getpgid(pid)) - pid = 0; - } - - return pid; -} - -int __proc_iter_appid( - int (*iterfunc)(const char *dname, const char *appid, void *priv, uid_t uid), - void *priv) -{ - DIR *dp; - struct dirent *dentry; - int pid; - int ret; - char buf[MAX_LOCAL_BUFSZ]; - char *p; - uid_t uid; - - dp = opendir("/proc"); - if (dp == NULL) - return -1; - - if (iterfunc == NULL) - iterfunc = __find_pid_by_appid; - - while ((dentry = readdir(dp)) != NULL) { - if (!isdigit(dentry->d_name[0])) - continue; - - uid = __proc_get_usr_bypid(atoi(dentry->d_name)); - - snprintf(buf, sizeof(buf), "/proc/%s/attr/current", dentry->d_name); - ret = __read_proc(buf, buf, sizeof(buf)); - if (ret <= 0) - continue; - - p = strstr(buf, APP_LABEL_PREFIX); - /* not an app */ - if (p == NULL) - continue; - p = p + strlen(APP_LABEL_PREFIX); - pid = iterfunc(dentry->d_name, p, priv, uid); - - if (pid > 0) { - closedir(dp); - return pid; - } - } - - closedir(dp); - return -1; -} - - -uid_t __proc_get_usr_bypid(int pid) -{ - char buf[MAX_CMD_BUFSZ]; - int ret; - uid_t uid; - struct stat DirStat; - snprintf(buf, sizeof(buf), "/proc/%d", pid); - ret = stat(buf, &DirStat); - if (ret < 0) - uid = (uid_t)-1; - else - uid = DirStat.st_uid; - return uid; -} - -char *__proc_get_appid_bypid(int pid) -{ - char buf[MAX_CMD_BUFSZ]; - char *p; - int ret; - - snprintf(buf, sizeof(buf), "/proc/%d/attr/current", pid); - ret = __read_proc(buf, buf, sizeof(buf)); - if (ret <= 0) - return NULL; - - p = strstr(buf, APP_LABEL_PREFIX); - /* not an app */ - if (p == NULL) - return NULL; - - p = p + strlen(APP_LABEL_PREFIX); - - return strdup(p); -} - -char *__proc_get_cmdline_bypid(int pid) -{ - char buf[MAX_CMD_BUFSZ]; - int ret; - - snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); - ret = __read_proc(buf, buf, sizeof(buf)); - if (ret <= 0) - return NULL; - - /* support app launched by shell script */ - if (strncmp(buf, BINSH_NAME, BINSH_SIZE) == 0) { - return strdup(&buf[BINSH_SIZE + 1]); - } else if (strncmp(buf, VALGRIND_NAME, VALGRIND_SIZE) == 0) { - char* ptr = buf; - - /* buf comes with double null-terminated string */ - while (1) { - while (*ptr) - ptr++; - - ptr++; - - if (!(*ptr)) - break; - - /* ignore trailing "--" */ - if (strncmp(ptr, "-", 1) != 0) - break; - }; - - return strdup(ptr); - } else if (strncmp(buf, BASH_NAME, BASH_SIZE) == 0) { - if (strncmp(&buf[BASH_SIZE + 1], OPROFILE_NAME, OPROFILE_SIZE) == 0) { - if (strncmp(&buf[BASH_SIZE + OPROFILE_SIZE + 2], OPTION_VALGRIND_NAME, OPTION_VALGRIND_SIZE) == 0) - return strdup(&buf[BASH_SIZE + OPROFILE_SIZE + OPTION_VALGRIND_SIZE + 3]); - } - } - - return strdup(buf); -} - -char *__proc_get_exe_bypid(int pid) -{ - char buf[MAX_CMD_BUFSZ]; - char buf2[MAX_CMD_BUFSZ]; - ssize_t len; - - snprintf(buf, sizeof(buf), "/proc/%d/exe", pid); - len = readlink(buf, buf2, MAX_CMD_BUFSZ - 1); - if (len <= 0) - return NULL; - buf2[len] = 0; - return strdup(buf2); -} - -static inline int __get_pgid_from_stat(int pid) -{ - char buf[MAX_LOCAL_BUFSZ]; - char *str = NULL; - int ret; - int i; - int count = 0; - - if (pid <= 1) - return -1; - - snprintf(buf, sizeof(buf), "/proc/%d/stat", pid); - ret = __read_proc(buf, buf, sizeof(buf)); - if (ret < 0) - return -1; - - for (i = 0; i < (ret - 1); i++) { - if (buf[i] == ' ') { - count++; - if (count == PROC_STAT_GID_POS - 1) - str = &(buf[i + 1]); - else if (count == PROC_STAT_GID_POS) { - buf[i] = 0; - break; - } - } - } - - if (count == PROC_STAT_GID_POS && str != NULL) - pid = atoi(str); - else - pid = -1; - - return pid; -} - -int __proc_iter_pgid(int pgid, int (*iterfunc)(int pid, void *priv, uid_t uid), - void *priv) -{ - DIR *dp; - struct dirent *dentry; - int _pgid; - int ret = -1; - uid_t uid; - - dp = opendir("/proc"); - if (dp == NULL) - return -1; - - while ((dentry = readdir(dp)) != NULL) { - if (!isdigit(dentry->d_name[0])) - continue; - - _pgid = __get_pgid_from_stat(atoi(dentry->d_name)); - if (pgid == _pgid) { - uid = __proc_get_usr_bypid(atoi(dentry->d_name)); - ret = iterfunc(atoi(dentry->d_name), priv, uid); - if (ret >= 0) - break; - } - } - - closedir(dp); - return ret; -} - -- 2.7.4