From 926cfa348217d7288c4dcf661669998b8b9701e5 Mon Sep 17 00:00:00 2001 From: DongHun Kwak Date: Thu, 23 Jun 2022 15:27:28 +0900 Subject: [PATCH] refactor: extract common logic - Extract common logic - Adjust code formating Change-Id: Ia0136f4657c507301854c5ab82e8f6175fda50ac --- tzdata-parser/src/tzdata-update-parser.c | 412 ++++++++++++++----------------- 1 file changed, 189 insertions(+), 223 deletions(-) diff --git a/tzdata-parser/src/tzdata-update-parser.c b/tzdata-parser/src/tzdata-update-parser.c index 81559d7..d5b6d49 100755 --- a/tzdata-parser/src/tzdata-update-parser.c +++ b/tzdata-parser/src/tzdata-update-parser.c @@ -15,267 +15,233 @@ * 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. -*/ -#include -#include -#include -#include - -#include -#include -#include - -#include -#include + */ #include -#include #include +#include +#include #include #include +#include +#include +#include +#include +#include +#include +#include +#include #define WORKER "/etc/package-manager/parserlib/worker.sh" - -#define TZDATA_UPDATE_PARSER_TAG "TZDATA_UPDATE_PARSER" - -#define DEBUG_LOG(frmt, args...) \ - do { SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\ - __func__, ##args);} while (0) - -#define DEBUG_WARNING(frmt, args...) \ - do { SLOG(LOG_WARN,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\ - __func__, ##args);} while (0) - -#define DEBUG_ERROR(frmt, args...) \ - do { SLOG(LOG_ERROR,TZDATA_UPDATE_PARSER_TAG, "[tzdata_update_parser] %s: "frmt"\n",\ - __func__, ##args);} while (0) - -#define TRACE_BEGIN do {\ - SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[ENTER tzdata_update_parser] %s\n",\ - __func__);} while (0) - -#define TRACE_END do {\ - SLOG(LOG_DEBUG,TZDATA_UPDATE_PARSER_TAG, "[LEAVE tzdata_update_parser] %s\n",\ - __func__);} while (0) - - -static void alt_system() -{ - pid_t pid; - int status; - pid_t ret = -10000; - //char *const args[2] = {"/home/mjpark", NULL}; - //char **env; - //extern char **environ; - /* ... Sanitize arguments ... */ - pid = fork(); - if (pid == -1) { - /* Handle error */ - } else if (pid != 0) { - while ((ret = waitpid(pid, &status, 0)) == -1) { - if (errno != EINTR) { - /* Handle error */ - break; - } - } - if ((ret != -1) && - (!WIFEXITED(status) || !WEXITSTATUS(status)) ) { - /* Report unexpected child status */ - } - DEBUG_LOG(" ----> parent ? : %d ", ret); - - } else { - /* ... Initialize env as a sanitized copy of environ ... */ - //ret = execl("/home/mjpark/cprog/dir_copy/work.sh", "work.sh", (char *)0); - //if (execve("/bin/ls", args, env) == -1) { - //ret = execl("/bin/ls", "-1", (char *)0); - DEBUG_LOG(" ------------> run execl: %s ", WORKER); - ret = execl("/bin/bash", "bash", "-c", WORKER, (char *) NULL); - if (ret) { - /* Handle error */ - DEBUG_LOG("error occured : %s", WORKER); - printf("error occured\n"); - _Exit(127); - } else { - DEBUG_LOG("run OK !!!!!!!!!!!!!!!!!!!!!!!!!!!!"); - } - } +#define TZDATA_UPDATE_PARSER_TAG "TZDATA_UPDATE_PARSER" + +#define DEBUG_LOG(frmt, args...) \ + do { \ + SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG, \ + "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \ + } while (0) + +#define DEBUG_WARNING(frmt, args...) \ + do { \ + SLOG(LOG_WARN, TZDATA_UPDATE_PARSER_TAG, \ + "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \ + } while (0) + +#define DEBUG_ERROR(frmt, args...) \ + do { \ + SLOG(LOG_ERROR, TZDATA_UPDATE_PARSER_TAG, \ + "[tzdata_update_parser] %s: " frmt "\n", __func__, ##args); \ + } while (0) + +#define TRACE_BEGIN \ + do { \ + SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG, \ + "[ENTER tzdata_update_parser] %s\n", __func__); \ + } while (0) + +#define TRACE_END \ + do { \ + SLOG(LOG_DEBUG, TZDATA_UPDATE_PARSER_TAG, \ + "[LEAVE tzdata_update_parser] %s\n", __func__); \ + } while (0) + +static void alt_system() { + pid_t pid; + int status; + pid_t ret = -10000; + // char *const args[2] = {"/home/mjpark", NULL}; + // char **env; + // extern char **environ; + /* ... Sanitize arguments ... */ + pid = fork(); + if (pid == -1) { + /* Handle error */ + } else if (pid != 0) { + while ((ret = waitpid(pid, &status, 0)) == -1) { + if (errno != EINTR) { + /* Handle error */ + break; + } + } + if ((ret != -1) && (!WIFEXITED(status) || !WEXITSTATUS(status))) { + /* Report unexpected child status */ + } + DEBUG_LOG(" ----> parent ? : %d ", ret); + } else { + /* ... Initialize env as a sanitized copy of environ ... */ + // ret = execl("/home/mjpark/cprog/dir_copy/work.sh", "work.sh", (char *)0); + // if (execve("/bin/ls", args, env) == -1) { + // ret = execl("/bin/ls", "-1", (char *)0); + DEBUG_LOG(" ------------> run execl: %s ", WORKER); + ret = execl("/bin/bash", "bash", "-c", WORKER, (char *)NULL); + if (ret) { + /* Handle error */ + DEBUG_LOG("error occured : %s", WORKER); + printf("error occured\n"); + _Exit(127); + } else { + DEBUG_LOG("run OK !!!!!!!!!!!!!!!!!!!!!!!!!!!!"); + } + } } -//DEBUG_ERROR("srcdir length is not available \n"); -//DEBUG_LOG("srcdir =%s\n",(char *) srcdir); -int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - int ret; - pkgmgrinfo_pkginfo_h handle = NULL; - const char *app_root_path = NULL; - //const char *dest_path = NULL; - uid_t uid = 0; - - pkgmgr_installer_info_get_target_uid(&uid); - ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle); - - if (ret < 0) - { - DEBUG_ERROR("pkgid[%s] handle get fail", pkgid); - return -1; - } - - ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path); - if (ret < 0) - { - DEBUG_ERROR("pkgid[%s] path get fail", pkgid); - goto FAIL; - } - - if (appid == NULL) - { - DEBUG_ERROR("appid is NULL \n"); - goto FAIL; - } - - - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - TRACE_END; - return ret; +static int _get_pkgmgrinfo_handle(const char *pkgid, + pkgmgrinfo_pkginfo_h handle) { + int ret = 0; + uid_t uid = 0; -FAIL: - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - TRACE_END; - return -1; -} + pkgmgr_installer_info_get_target_uid(&uid); + ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle); + if (ret < 0) { + DEBUG_ERROR("pkgid[%s] handle get fail", pkgid); + return -1; + } + return ret; +} -int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - int ret = 0; - pkgmgrinfo_pkginfo_h handle = NULL; - const char* app_root_path = NULL; - //const char* dest_path = NULL; - uid_t uid = 0; - - pkgmgr_installer_info_get_target_uid(&uid); - ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle); - - if (ret < 0) - { - DEBUG_ERROR("pkgid[%s] handle get fail", pkgid); - return -1; - } - - ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path); - if (ret < 0) - { - DEBUG_ERROR("pkgid[%s] path get fail", pkgid); - goto FAIL; - } - - if (appid == NULL) - { - DEBUG_ERROR("appid is NULL \n"); - goto FAIL; - } - - DEBUG_LOG("-----------------------------------------------------"); - DEBUG_LOG("pkgid : %s ", pkgid); - DEBUG_LOG("APP root path : %s ", app_root_path); - DEBUG_LOG("-----------------------------------------------------"); - // RUN SCRIPT HERE - alt_system(); - DEBUG_LOG("-----------------------------------------------------"); - - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - TRACE_END; - return ret; +static int _pkgmgr_plugin_install(const char *pkgid, const char *appid, + GList *list, int upgrade_flag) { + TRACE_BEGIN; + int ret = 0; + pkgmgrinfo_pkginfo_h handle = NULL; + const char *app_root_path = NULL; + // const char* dest_path = NULL; + uid_t uid = 0; + + if (_get_pkgmgrinfo_handle(pkgid, handle) < 0) return -1; + + ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path); + if (ret < 0) { + DEBUG_ERROR("pkgid[%s] path get fail", pkgid); + goto FAIL; + } + + if (appid == NULL) { + DEBUG_ERROR("appid is NULL \n"); + goto FAIL; + } + + if (upgrade_flag == 1) { + DEBUG_LOG("-----------------------------------------------------"); + DEBUG_LOG("pkgid : %s ", pkgid); + DEBUG_LOG("APP root path : %s ", app_root_path); + DEBUG_LOG("-----------------------------------------------------"); + // RUN SCRIPT HERE + alt_system(); + DEBUG_LOG("-----------------------------------------------------"); + } + + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + TRACE_END; + return ret; FAIL: - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - TRACE_END; - return -1; + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + TRACE_END; + return -1; } +// DEBUG_ERROR("srcdir length is not available \n"); +// DEBUG_LOG("srcdir =%s\n",(char *) srcdir); +int COMMON_PKGMGR_PLUGIN_INSTALL(const char *pkgid, const char *appid, + GList *list) { + return _pkgmgr_plugin_install(pkgid, appid, list, 0); +} +int COMMON_PKGMGR_PLUGIN_UPGRADE(const char *pkgid, const char *appid, + GList *list) { + return _pkgmgr_plugin_install(pkgid, appid, list, 1); +} -int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - pkgmgrinfo_pkginfo_h handle = NULL; - const char* app_root_path = NULL; - //const char *dest_path = NULL; - int ret; - uid_t uid = 0; - - if (appid == NULL) - { - DEBUG_ERROR("appid is NULL \n"); - return -1; - } +int COMMON_PKGMGR_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, + GList *list) { + TRACE_BEGIN; + pkgmgrinfo_pkginfo_h handle = NULL; + const char *app_root_path = NULL; + // const char *dest_path = NULL; + int ret; + uid_t uid = 0; - pkgmgr_installer_info_get_target_uid(&uid); - ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle); + if (appid == NULL) { + DEBUG_ERROR("appid is NULL \n"); + return -1; + } - if (ret < 0) - { - DEBUG_ERROR("pkgid[%s] handle get fail", pkgid); - return -1; - } + if (_get_pkgmgrinfo_handle(pkgid, handle) < 0) return -1; - ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path); - if (ret < 0) - { - DEBUG_ERROR("pkgid[%s] path get fail", pkgid); - goto FAIL; - } + ret = pkgmgrinfo_pkginfo_get_root_path(handle, (char **)&app_root_path); + if (ret < 0) { + DEBUG_ERROR("pkgid[%s] path get fail", pkgid); + goto FAIL; + } - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - TRACE_END; - return ret; + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + TRACE_END; + return ret; FAIL: - pkgmgrinfo_pkginfo_destroy_pkginfo(handle); - TRACE_END; - return -1; + pkgmgrinfo_pkginfo_destroy_pkginfo(handle); + TRACE_END; + return -1; } #if USE_META_DATA -int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list); +int PKGMGR_MDPARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, + GList *list) { + TRACE_BEGIN; + return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list); } -int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list); +int PKGMGR_MDPARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, + GList *list) { + TRACE_BEGIN; + return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list); } -int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list); +int PKGMGR_MDPARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, + GList *list) { + TRACE_BEGIN; + return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list); } #endif -int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list); +int PKGMGR_CATEGORY_PARSER_PLUGIN_INSTALL(const char *pkgid, const char *appid, + GList *list) { + TRACE_BEGIN; + return COMMON_PKGMGR_PLUGIN_INSTALL(pkgid, appid, list); } -int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list); +int PKGMGR_CATEGORY_PARSER_PLUGIN_UPGRADE(const char *pkgid, const char *appid, + GList *list) { + TRACE_BEGIN; + return COMMON_PKGMGR_PLUGIN_UPGRADE(pkgid, appid, list); } -int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, const char *appid, GList *list) -{ - TRACE_BEGIN; - return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list); +int PKGMGR_CATEGORY_PARSER_PLUGIN_UNINSTALL(const char *pkgid, + const char *appid, GList *list) { + TRACE_BEGIN; + return COMMON_PKGMGR_PLUGIN_UNINSTALL(pkgid, appid, list); } - /* End of a file */ -- 2.7.4