#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
+#include <sys/wait.h>
#include <sys/xattr.h>
#include <netinet/in.h>
#include <fcntl.h>
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;
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;
return thread;
}
+// TODO: should be refactored: use separated launcher
static int openFileProcess()
{
if (ename == NULL) {
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]);
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) {
}
#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) {
}
}
}
+ // 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
}
}
}
+ // 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;
}