summaryrefslogtreecommitdiff
path: root/gtk2_ardour/system_exec.cc
diff options
context:
space:
mode:
Diffstat (limited to 'gtk2_ardour/system_exec.cc')
-rw-r--r--gtk2_ardour/system_exec.cc42
1 files changed, 34 insertions, 8 deletions
diff --git a/gtk2_ardour/system_exec.cc b/gtk2_ardour/system_exec.cc
index 566c87dfbb..c63d1d183b 100644
--- a/gtk2_ardour/system_exec.cc
+++ b/gtk2_ardour/system_exec.cc
@@ -414,19 +414,33 @@ void
SystemExec::terminate ()
{
::pthread_mutex_lock(&write_lock);
+
+ /* close stdin in an attempt to get the child to exit cleanly.
+ */
+
close_stdin();
+
if (pid) {
::usleep(50000);
sched_yield();
wait(WNOHANG);
}
+ /* if pid is non-zero, the child task is still executing (i.e. it did
+ * not exit in response to stdin being closed). try to kill it.
+ */
+
if (pid) {
::kill(pid, SIGTERM);
::usleep(50000);
sched_yield();
wait(WNOHANG);
}
+
+ /* if pid is non-zero, the child task is STILL executing after being
+ * sent SIGTERM. Act tough ... send SIGKILL
+ */
+
if (pid) {
::fprintf(stderr, "Process is still running! trying SIGKILL\n");
::kill(pid, SIGKILL);
@@ -442,12 +456,23 @@ int
SystemExec::wait (int options)
{
int status=0;
+ int ret;
+
if (pid==0) return -1;
- if (pid==::waitpid(pid, &status, options)) {
- pid=0;
- }
- if (errno == ECHILD) {
- pid=0;
+
+ ret = waitpid (pid, &status, options);
+
+ if (ret == pid) {
+ if (WEXITSTATUS(status) || WIFSIGNALED(status)) {
+ pid=0;
+ }
+ } else {
+ if (ret != 0) {
+ if (errno == ECHILD) {
+ /* no currently running children, reset pid */
+ pid=0;
+ }
+ } /* else the process is still running */
}
return status;
}
@@ -611,7 +636,7 @@ SystemExec::output_interposer()
ssize_t r;
unsigned long l = 1;
- ioctl(rfd, FIONBIO, &l); // set non-blocking I/O
+ ioctl(rfd, FIONBIO, &l); // set non-blocking I/O
for (;fcntl(rfd, F_GETFL)!=-1;) {
r = read(rfd, buf, sizeof(buf));
@@ -643,7 +668,8 @@ int
SystemExec::write_to_stdin(std::string d, size_t len)
{
const char *data;
- size_t r,c;
+ ssize_t r;
+ size_t c;
::pthread_mutex_lock(&write_lock);
data=d.c_str();
@@ -658,7 +684,7 @@ SystemExec::write_to_stdin(std::string d, size_t len)
sleep(1);
continue;
}
- if (r != (len-c)) {
+ if ((size_t) r != (len-c)) {
::pthread_mutex_unlock(&write_lock);
return c;
}