From c3997a04a23f032a2817cd857cb786516f8dd0e3 Mon Sep 17 00:00:00 2001 From: "adhavan.m" Date: Tue, 10 Apr 2018 17:46:05 +0530 Subject: [PATCH] Added appcmd arguments parsing Change-Id: Ifb7c03e2659eac06d9f26740900e2d08d356893a Signed-off-by: adhavan.m --- src/default_plugin_appcmd.c | 258 ++++++++++++++++++++++++++++++-------------- src/strutils.c | 19 ++++ 2 files changed, 197 insertions(+), 80 deletions(-) diff --git a/src/default_plugin_appcmd.c b/src/default_plugin_appcmd.c index 07396dc..6d511c3 100644 --- a/src/default_plugin_appcmd.c +++ b/src/default_plugin_appcmd.c @@ -72,27 +72,47 @@ static int appcmd_install_gen_shellcmd(appcmd_info* p_info) { return -1; } - type = p_info->args[1]; - pkgpath = p_info->args[2]; - pkgid = p_info->args[3]; - teppath = p_info->args[4]; - - D("args: type=%s, pkgpath=%s, pkgid=%s, teppath=%s\n", type, pkgpath, pkgid, teppath); - - if (strncmp(pkgid, "null", 4) == 0) { - if (strncmp(teppath, "null", 4) == 0) { - /* Normal install case */ - snprintf(buf, len, "pkgcmd -i -q -t \'%s\' -p \'%s\' -G", type, pkgpath); - } else { - /* TEP install case */ - snprintf(buf, len, "pkgcmd -i -q -p \'%s\' -e \'%s\' -G", pkgpath, teppath); - } - } else { - /* Re-install case */ - snprintf(buf, len, "pkgcmd -r -q -t \'%s\' -n \'%s\'", type, pkgid); - } - - return 0; + type = parse_arg(p_info->args[1]); + pkgpath = parse_arg(p_info->args[2]); + pkgid = parse_arg(p_info->args[3]); + teppath = parse_arg(p_info->args[4]); + + if (type != NULL && pkgpath != NULL && pkgid != NULL && teppath != NULL) { + D("args: type=%s, pkgpath=%s, pkgid=%s, teppath=%s\n", type, pkgpath, pkgid, teppath); + + if (strncmp(pkgid, "null", 4) == 0) { + if (strncmp(teppath, "null", 4) == 0) { + /* Normal install case */ + snprintf(buf, len, "pkgcmd -i -q -t %s -p %s -G", type, pkgpath); + } else { + /* TEP install case */ + snprintf(buf, len, "pkgcmd -i -q -p %s -e %s -G", pkgpath, teppath); + } + } else { + /* Re-install case */ + snprintf(buf, len, "pkgcmd -r -q -t %s -n %s", type, pkgid); + } + + free(type); + free(pkgpath); + free(pkgid); + free(teppath); + + return 0; + } + if (type != NULL) { + free(type); + } + if (pkgpath != NULL) { + free(pkgpath); + } + if (pkgid != NULL) { + free(pkgid); + } + if (teppath != NULL) { + free(teppath); + } + return -1; } static int appcmd_uninstall_gen_shellcmd(appcmd_info* p_info) { @@ -105,13 +125,18 @@ static int appcmd_uninstall_gen_shellcmd(appcmd_info* p_info) { return -1; } - pkgid = p_info->args[1]; + pkgid = parse_arg(p_info->args[1]); - D("args: pkgid=%s\n", pkgid); + if (pkgid != NULL) { + D("args: pkgid=%s\n", pkgid); - snprintf(buf, len, "pkgcmd -u -q -n \'%s\'", pkgid); + snprintf(buf, len, "pkgcmd -u -q -n %s", pkgid); - return 0; + free(pkgid); + + return 0; + } + return -1; } static int appcmd_runapp_gen_shellcmd(appcmd_info* p_info) { @@ -124,16 +149,21 @@ static int appcmd_runapp_gen_shellcmd(appcmd_info* p_info) { return -1; } - appid = p_info->args[1]; + appid = parse_arg(p_info->args[1]); - D("args: appid=%s\n", appid); + if (appid != NULL) { + D("args: appid=%s\n", appid); - snprintf(buf, len, "/usr/bin/pkginfo --app \'%s\' | grep component: | awk '{print $2}'" - " | while read var; do if [ $var = watchapp ] || [ $var = widgetapp ]; then /usr/bin/app_launcher " - "--start org.tizen.widget_viewer_sdk widget_id \'%s\'; else /usr/bin/app_launcher " - "--start \'%s\'; fi; done", appid, appid, appid); + snprintf(buf, len, "/usr/bin/pkginfo --app %s | grep component: | awk '{print $2}'" + " | while read var; do if [ $var = watchapp ] || [ $var = widgetapp ]; then /usr/bin/app_launcher " + "--start org.tizen.widget_viewer_sdk widget_id %s; else /usr/bin/app_launcher " + "--start %s; fi; done", appid, appid, appid); - return 0; + free(appid); + + return 0; + } + return -1; } static int appcmd_rununittestapp_gen_shellcmd(appcmd_info* p_info) { @@ -159,14 +189,26 @@ static int appcmd_rununittestapp_gen_shellcmd(appcmd_info* p_info) { p_info->args[1] = strdup(p_appid); p_info->args[2] = strdup(ptr); - appid = p_info->args[1]; - usr_args = p_info->args[2]; + appid = parse_arg(p_info->args[1]); + usr_args = parse_arg(p_info->args[2]); - D("args: appid=%s, usr_args=%s\n", appid, usr_args); + if (appid != NULL && usr_args != NULL) { + D("args: appid=%s, usr_args=%s\n", appid, usr_args); - snprintf(buf, len, "/usr/bin/app_launcher -s \'%s\' __AUL_SDK__ UNIT_TEST __LAUNCH_APP_MODE__ SYNC __DLP_UNIT_TEST_ARG__ \'%s\'", appid, usr_args); + snprintf(buf, len, "/usr/bin/app_launcher -s %s __AUL_SDK__ UNIT_TEST __LAUNCH_APP_MODE__ SYNC __DLP_UNIT_TEST_ARG__ \'%s\'", appid, usr_args); - return 0; + free(appid); + free(usr_args); + + return 0; + } + if (appid != NULL) { + free(appid); + } + if (usr_args != NULL) { + free(usr_args); + } + return -1; } static int appcmd_killapp_gen_shellcmd(appcmd_info* p_info) { @@ -179,13 +221,18 @@ static int appcmd_killapp_gen_shellcmd(appcmd_info* p_info) { return -1; } - appid = p_info->args[1]; + appid = parse_arg(p_info->args[1]); - D("args: appid=%s\n", appid); + if (appid != NULL) { + D("args: appid=%s\n", appid); - snprintf(buf, len, "/usr/bin/app_launcher --kill \'%s\'", appid); + snprintf(buf, len, "/usr/bin/app_launcher --kill %s", appid); - return 0; + free(appid); + + return 0; + } + return -1; } static int appcmd_packagelist_gen_shellcmd(appcmd_info* p_info) { @@ -198,13 +245,18 @@ static int appcmd_packagelist_gen_shellcmd(appcmd_info* p_info) { return -1; } - type = p_info->args[1]; + type = parse_arg(p_info->args[1]); - D("args: type=%s\n", type); + if (type != NULL) { + D("args: type=%s\n", type); - snprintf(buf, len, "/usr/bin/pkgcmd -l -t \'%s\'", type); + snprintf(buf, len, "/usr/bin/pkgcmd -l -t %s", type); - return 0; + free(type); + + return 0; + } + return -1; } static int appcmd_debugwebapp_gen_shellcmd(appcmd_info* p_info) { @@ -217,13 +269,18 @@ static int appcmd_debugwebapp_gen_shellcmd(appcmd_info* p_info) { return -1; } - appid = p_info->args[1]; + appid = parse_arg(p_info->args[1]); - D("args: appid=%s\n", appid); + if (appid != NULL) { + D("args: appid=%s\n", appid); - snprintf(buf, len, "/usr/bin/app_launcher --start \'%s\' -w", appid); + snprintf(buf, len, "/usr/bin/app_launcher --start %s -w", appid); - return 0; + free(appid); + + return 0; + } + return -1; } static int appcmd_debugnativeapp_gen_shellcmd(appcmd_info* p_info) { @@ -240,22 +297,42 @@ static int appcmd_debugnativeapp_gen_shellcmd(appcmd_info* p_info) { return -1; } - debug_port = p_info->args[1]; - appid= p_info->args[2]; - pid_str = p_info->args[3]; - gdbserver_path = p_info->args[4]; // not used. for 3.0 platform. + debug_port = parse_arg(p_info->args[1]); + appid= parse_arg(p_info->args[2]); + pid_str = parse_arg(p_info->args[3]); + gdbserver_path = parse_arg(p_info->args[4]); // not used. for 3.0 platform. - pid = atoi(pid_str); - D("args: debug_port=%s, appid=%s, pid=%d, gdbserver_path=%s\n", debug_port, appid, pid, gdbserver_path); + if (debug_port != NULL && appid != NULL && pid_str != NULL && gdbserver_path != NULL) { + pid = atoi(pid_str); + D("args: debug_port=%s, appid=%s, pid=%d, gdbserver_path=%s\n", debug_port, appid, pid, gdbserver_path); - if (pid == -1) { - snprintf(buf, len, "/usr/bin/app_launcher --start \'%s\' __AUL_SDK__ DEBUG __DLP_DEBUG_ARG__ :\'%s\' __DLP_GDBSERVER_PATH__ \'%s\'", appid, debug_port, gdbserver_path); - } else { - /* attach mode */ - snprintf(buf, len, "/usr/bin/launch_debug \'%s\' __AUL_SDK__ ATTACH __DLP_GDBSERVER_PATH__ \'%s\' __DLP_ATTACH_ARG__ --attach,:\'%s\',%d", appid, gdbserver_path, debug_port, pid); - } + if (pid == -1) { + snprintf(buf, len, "/usr/bin/app_launcher --start %s __AUL_SDK__ DEBUG __DLP_DEBUG_ARG__ :%s __DLP_GDBSERVER_PATH__ %s", appid, debug_port, gdbserver_path); + } else { + /* attach mode */ + snprintf(buf, len, "/usr/bin/launch_debug %s __AUL_SDK__ ATTACH __DLP_GDBSERVER_PATH__ %s __DLP_ATTACH_ARG__ --attach,:%s,%d", appid, gdbserver_path, debug_port, pid); + } - return 0; + free(debug_port); + free(appid); + free(pid_str); + free(gdbserver_path); + + return 0; + } + if (debug_port != NULL) { + free(debug_port); + } + if (appid != NULL) { + free(appid); + } + if (pid_str != NULL) { + free(pid_str); + } + if (gdbserver_path != NULL) { + free(gdbserver_path); + } + return -1; } static int appcmd_appinfo_gen_shellcmd(appcmd_info* p_info) { @@ -268,13 +345,18 @@ static int appcmd_appinfo_gen_shellcmd(appcmd_info* p_info) { return -1; } - pkgid = p_info->args[1]; + pkgid = parse_arg(p_info->args[1]); - D("args: pkgid=%s\n", pkgid); + if (pkgid != NULL) { + D("args: pkgid=%s\n", pkgid); - snprintf(buf, len, "/usr/bin/pkginfo --list \'%s\'", pkgid); + snprintf(buf, len, "/usr/bin/pkginfo --list %s", pkgid); - return 0; + free(pkgid); + + return 0; + } + return -1; } static void appcmd_receiver_debugwebapp(int fd_in, int fd_out) @@ -584,18 +666,29 @@ static void run_appcmd_packageinfo(appcmd_info* p_info) { return; } - type = p_info->args[1]; - pkgid= p_info->args[2]; + type = parse_arg(p_info->args[1]); + pkgid= parse_arg(p_info->args[2]); - D("args: type=%s, pkgid=%s\n", type, pkgid); + if (type != NULL && pkgid != NULL) { + D("args: type=%s, pkgid=%s\n", type, pkgid); - if (get_pkg_info(pkgid, pkginfo_buf, sizeof(pkginfo_buf)) == 0) { - D("success to get pkginfo. (%s)\n", pkginfo_buf); - p_info->exitcode = 0; - snprintf(result_buf, sizeof(result_buf), "\n%s:%s\n", MESSAGE_PREFIX_APPCMD_RETURN, pkginfo_buf); - writex(p_info->fd, result_buf, strlen(result_buf)); - } else { - D("failed to get pkginfo.\n"); + if (get_pkg_info(pkgid, pkginfo_buf, sizeof(pkginfo_buf)) == 0) { + D("success to get pkginfo. (%s)\n", pkginfo_buf); + p_info->exitcode = 0; + snprintf(result_buf, sizeof(result_buf), "\n%s:%s\n", MESSAGE_PREFIX_APPCMD_RETURN, pkginfo_buf); + writex(p_info->fd, result_buf, strlen(result_buf)); + } else { + D("failed to get pkginfo.\n"); + } + + free(type); + free(pkgid); + } + if (type != NULL) { + free(type); + } + if (pkgid != NULL) { + free(pkgid); } } #else @@ -609,13 +702,18 @@ static int appcmd_packageinfo_gen_shellcmd(appcmd_info* p_info) { return -1; } - pkgid = p_info->args[1]; + pkgid = parse_arg(p_info->args[1]); - D("args: pkgid=%s\n", pkgid); + if (pkgid != NULL) { + D("args: pkgid=%s\n", pkgid); - snprintf(buf, len, "/usr/bin/pkginfo --pkg %s;/usr/bin/pkgcmd -C -n %s", pkgid, pkgid); + snprintf(buf, len, "/usr/bin/pkginfo --pkg %s;/usr/bin/pkgcmd -C -n %s", pkgid, pkgid); - return 0; + free(pkgid); + + return 0; + } + return -1; } static void appcmd_receiver_packageinfo(int fd_in, int fd_out) diff --git a/src/strutils.c b/src/strutils.c index cba6a23..6ce53cf 100644 --- a/src/strutils.c +++ b/src/strutils.c @@ -91,3 +91,22 @@ char *s_strncpy(char *dest, const char *source, size_t n) { } return start; } + +char *parse_arg(char *arg) { + char *validstr = malloc(strlen(arg) * 2 + 1); + if(validstr == NULL) { + return NULL; + } + char *ret = validstr; + while(*arg) { + if (*arg == '!' || *arg == '&' || *arg == ';' || *arg == '|' || *arg == '(' || *arg == ')') { + *validstr='\\'; + validstr++; + } + *validstr=*arg; + validstr++; + arg++; + } + *validstr = '\0'; + return ret; +} -- 2.7.4