#define _GNU_SOURCE
#include <errno.h>
#include <pthread.h>
-#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "proc_stat.h"
#include "logging.h"
+extern volatile int global_stop;
+
+extern int is_running(int pid);
+
// TODO!! check errors?
static void split(char *line, char **array, int n)
{
}
}
-static int is_running(int pid)
-{
- return kill(pid, 0) == 0;
-}
-
struct write_proc_stat_context {
FILE *sstat;
FILE *cpuf;
if (c->pstat) fclose(c->pstat);
}
-extern volatile int global_stop;
-
static void write_proc_stat(FILE *stat_file_out, int pid, int timeout_usec, int verbose)
{
struct write_proc_stat_context c;
{
struct outstat_info *oi = (struct outstat_info*)arg;
write_proc_stat(oi->file, oi->pid, oi->timeout_usec, oi->verbose);
+ if (oi->verbose) {
+ log_message("stat output finished");
+ }
free(oi);
return NULL;
}
#include <sys/time.h>
#include <sys/types.h>
+#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/xattr.h>
-#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
volatile int global_stop = 0;
+int is_running(int pid)
+{
+ return kill(pid, 0) == 0;
+}
+
static struct option long_options[] = {
{"appid", required_argument, 0, 'a'},
{"pipe", required_argument, 0, 'p'},
return NULL;
}
-#if USE_AUL
-
-static void *wait_app_exited_thread(void *arg)
-{
- intptr_t app_pid = (intptr_t)arg;
- int app_runs = 1;
-#if TIZEN
- while (!global_stop && (app_runs = (aul_app_get_status_bypid(app_pid) >= 0))) {
- usleep(500000);
- }
-#else
- while (!global_stop && (app_runs = (kill(app_pid, 0) == 0))) {
- usleep(500000);
- }
-#endif
- if (verbose) {
- if (!app_runs) {
- log_message("controlled application finished (pid=%d)", app_pid);
- }
- if (!global_stop) {
- log_message("stop flag set (wait_app_exited_thread)");
- }
- }
- global_stop = 1;
-}
-
-#endif /* USE_AUL */
-
// On Tizen, launch_app won't terminate until stdin, stdout and stderr are closed
// TODO!! is needed?
static void close_stdio()
log_message("line: %s", line);
}
char *reply = line; // echo input line by default
+ char *buf = NULL;
int pid;
int signal = -1;
if (is_command(line, "test", &pid, -1, NULL, 0) == 0) {
#endif /* TIZEN */
}
app_pid = pid;
-#if USE_AUL
- if (verbose) {
- log_message("waiting for controlled application exit (pid=%d)", pid);
- }
- SimpleThread(&wait_app_exited_thread, (void*)(intptr_t)pid);
-#endif /* USE_AUL */
if (doinfo) {
if (stat_file_out == NULL) {
stat_file_out = stdout;
proc_stat_thread_runs = 1;
}
}
- else {
- reply = "!app_set_already"; // report error
+ else { // error
+ reply = "!app_set_already";
+ if (asprintf(&buf, "%s (%s)", reply, line) != -1) {
+ reply = buf;
+ }
}
}
else if (is_command(line, "exit", &pid, app_pid, NULL, 0) == 0) {
signal = arg2;
}
}
- char *buf = NULL;
if (signal >= 0) {
int result = send_signal(pid, signal);
- if (asprintf(&buf, "%s : %d", reply, result) != -1) {
- reply = buf;
+ if (result != 0) { // error
+ reply = "!kill_failed";
+ if (asprintf(&buf, "%s (%s) : %d", reply, line, result) != -1) {
+ reply = buf;
+ }
}
}
if (ctrl_file_out != NULL) {
struct timespec poll_timeout;
poll_timeout.tv_sec = 0;
poll_timeout.tv_nsec = 250 * 1000000L; // 250 msec
+
+ int last_app_pid = -1;
while (!global_stop) {
if (command_loop_thread_runs &&
pthread_timedjoin_np(command_loop_thread_id, NULL, &poll_timeout) == 0)
{
break; // data output thread finished
}
+ if (global_stop) {
+ break;
+ }
+ if (app_pid != -1) {
+#if USE_AUL
+ int app_runs = (aul_app_get_status_bypid(app_pid) >= 0);
+#else
+ int app_runs = (kill(app_pid, 0) == 0);
+#endif
+ if (!app_runs) {
+ if (verbose) {
+ log_message("controlled application finished (pid=%d)", app_pid);
+ }
+ global_stop = 1;
+ break;
+ }
+ if (last_app_pid == -1) {
+ last_app_pid = app_pid;
+ if (verbose) {
+ log_message("waiting for controlled application exit (pid=%d)", app_pid);
+ }
+ }
+ }
// sleep to reduce CPU usage
usleep(100 * 1000); // 100 msec
}