From: Aleksei Vereshchagin Date: Tue, 4 Sep 2018 20:20:21 +0000 (+0300) Subject: Add possibility to launch commands via profctl X-Git-Tag: submit/tizen/20180911.125435~4 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=71ef4038d9d0bc14361b6514ac9002d13e6b3707;p=sdk%2Ftools%2Fprofctl.git Add possibility to launch commands via profctl --- diff --git a/README.md b/README.md index 69c3b78..f2231d9 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ gbs build -P ... -A ... ## Usage ``` -profctl [options...] +profctl [options...] [command [args]] ``` ### Options diff --git a/profctl.c b/profctl.c index 352cd76..7c91f39 100644 --- a/profctl.c +++ b/profctl.c @@ -22,6 +22,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,7 @@ static struct termios sterm; static char *appid = NULL; static int app_pid = -1; +static int cmd_pid = -1; static char *pname = NULL; static char *ename = NULL; static char *oname = NULL; @@ -224,11 +226,6 @@ static int process_option(int argc, char **argv) case 's': statPort = atoi(optarg); break; case '?': exit(1); default: - if (optind != argc) - { - log_error("extra argument %s", argv[optind]); - exit(1); - } return -1; } return 0; @@ -249,6 +246,7 @@ static pthread_t SimpleThread(void* (*func)(void *), void *arg) return thread; } +// TODO: should be refactored: use separated launcher static int openFileProcess() { if (ename == NULL) { @@ -297,7 +295,9 @@ static int openFileProcess() return -1; } - if (pid == 0) { /* Child */ + if (pid == 0) { + /* Child branch */ + if (close(pipefd[0]) != 0) { // not critical log_system_error("CHILD close(%d)", pipefd[0]); @@ -475,7 +475,7 @@ static void *command_loop_thread(void *arg) char *buf = NULL; int pid; int signal = -1; - if (is_command(line, "test", &pid, -1, NULL, 0) == 0) { + if (is_command(line, "test", &pid, cmd_pid, NULL, 0) == 0) { if (app_pid == -1) { #if !TIZEN if (pid == -1) { @@ -622,6 +622,30 @@ int main(int argc, char **argv) } #endif /* TIZEN */ + if (optind != argc) { + // TODO: should be refactored: use separated launcher + + if (verbose) { + log_message("launch command process %s", argv[optind]); + } + + cmd_pid = fork(); + if (!cmd_pid) { + /* Child branch */ + + // TODO: close unneeded descriptors + + execv(argv[optind], &argv[optind]); + exit(1); + } else if (cmd_pid == -1) { + log_system_error_and_exit("cannot fork command process"); + } else { + if (verbose) { + log_message("waiting for command process exit (pid=%d)", cmd_pid); + } + } + } + if (controlPort > 0) { control_socket = openPort(controlPort); if (control_socket < 0) { @@ -736,6 +760,17 @@ int main(int argc, char **argv) } } } + // TODO: should separated launcher to be used for pid control? + if (cmd_pid != -1) { + if (waitpid(cmd_pid, NULL, WNOHANG) > 0) { + if (verbose) { + log_message("command process finished (pid=%d)", cmd_pid); + } + cmd_pid = -1; + global_stop = 1; + break; + } + } // sleep to reduce CPU usage usleep(100 * 1000); // 100 msec } @@ -762,5 +797,48 @@ int main(int argc, char **argv) } } + // TODO: should be refactored: move to separated launcher + if (cmd_pid != -1) { + int result = 0; + for (int retry = 0; retry < 10; retry++) { + result = waitpid(cmd_pid, NULL, WNOHANG); + if (result != 0) { + break; + } + usleep(100 * 1000); // 100 ms + } + if (result == 0) { + if (verbose) { + log_message("kill command process (pid=%d)", cmd_pid); + } + result = send_signal(cmd_pid, SIGINT); + if (result == 0) + { + for (int retry = 0; retry < 10; retry++) { + result = waitpid(cmd_pid, NULL, WNOHANG); + if (result != 0) { + break; + } + usleep(100 * 1000); // 100 ms + } + if (result == 0) { + result = send_signal(cmd_pid, SIGKILL); + if (result == 0) + { + result = waitpid(cmd_pid, NULL, 0); + } + } + } + } + if (result > 0) { + if (verbose) { + log_message("command process finished (pid=%d)", cmd_pid); + } + cmd_pid = -1; + } else if (result == -1) { + log_system_error("cannot finish command process (pid=%d)", cmd_pid); + } + } + return 0; }