/*
Copyright (C) 2010 Paul Davis
- Copyright 2005-2008 Lennart Poettering
- Author: Robin Gareus <robin@gareus.org>
+ Copyright (C) 2010-2014 Robin Gareus <robin@gareus.org>
+ Copyright (C) 2005-2008 Lennart Poettering
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
#include <string.h>
#include <errno.h>
#include <unistd.h>
+#include <algorithm>
#include <assert.h>
+
+#ifndef COMPILER_MSVC
#include <dirent.h>
+#endif
-#ifdef __WIN32__
+#ifdef PLATFORM_WINDOWS
#include <windows.h>
#else
#include <fcntl.h>
#endif
+#define USE_VFORK
+
#include "pbd/system_exec.h"
using namespace std;
-void * interposer_thread (void *arg);
+using namespace PBD;
+static void * interposer_thread (void *arg);
static void close_fd (int& fd) { if (fd >= 0) ::close (fd); fd = -1; }
-#ifndef __WIN32__
+#if (!defined PLATFORM_WINDOWS && defined NO_VFORK)
/*
* This function was part of libasyncns.
* LGPL v2.1
return 0;
}
-#endif /* not on windows */
+#endif /* not on windows, nor vfork */
void
SystemExec::init ()
pin[1] = -1;
nicelevel = 0;
envp = NULL;
-#ifdef __WIN32__
+#ifdef PLATFORM_WINDOWS
stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
{
init ();
-#ifdef __WIN32__
+#ifdef PLATFORM_WINDOWS
make_wargs(a);
#endif
make_envp();
}
free (argp);
}
-#ifdef __WIN32__
+#ifdef PLATFORM_WINDOWS
if (w_args) free(w_args);
#endif
pthread_mutex_destroy(&write_lock);
}
-void *
+static void *
interposer_thread (void *arg) {
SystemExec *sex = static_cast<SystemExec *>(arg);
sex->output_interposer();
return 0;
}
-#ifdef __WIN32__ /* Windows Process */
+#ifdef PLATFORM_WINDOWS /* Windows Process */
/* HELPER FUNCTIONS */
std::string wa = cmd;
if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
std::replace(cmd.begin(), cmd.end(), '/', '\\' );
- char **tmp = a;
+ char **tmp = ++a;
while (tmp && *tmp) {
wa.append(" \"");
- wa.append(*tmp);
+ std::string arg(*tmp);
+ size_t start_pos = 0;
+ while((start_pos = arg.find("\\", start_pos)) != std::string::npos) {
+ arg.replace(start_pos, 1, "\\\\");
+ start_pos += 2;
+ }
+ wa.append(arg);
wa.append("\"");
tmp++;
}
SystemExec::terminate ()
{
::pthread_mutex_lock(&write_lock);
+
+ close_stdin();
+
if (pid) {
/* terminate */
EnumWindows(my_terminateApp, (LPARAM)pid->dwProcessId);
}
int
-SystemExec::start (int stderr_mode)
+SystemExec::start (int stderr_mode, const char * /*vfork_exec_wrapper*/)
{
char* working_dir = 0;
if (bytesAvail < 1) {Sleep(500); printf("N/A\n"); continue;}
#endif
if (stdoutP[0] == INVALID_HANDLE_VALUE) break;
- if (!ReadFile(stdoutP[0], data, BUFSIZ, &bytesRead, 0)) break;
+ if (!ReadFile(stdoutP[0], data, BUFSIZ, &bytesRead, 0)) {
+ DWORD err = GetLastError();
+ if (err == ERROR_IO_PENDING) continue;
+ break;
+ }
if (bytesRead < 1) continue; /* actually not needed; but this is safe. */
data[bytesRead] = 0;
ReadStdout(data, bytesRead);/* EMIT SIGNAL */
if (pid) {
::kill(pid, SIGTERM);
- ::usleep(50000);
+ usleep(50000);
sched_yield();
wait(WNOHANG);
}
}
int
-SystemExec::start (int stderr_mode)
+SystemExec::start (int stderr_mode, const char *vfork_exec_wrapper)
{
if (is_running()) {
return 0; // mmh what to return here?
return -1;
}
+#ifndef NO_VFORK
+ r = ::vfork();
+#else
r = ::fork();
+#endif
if (r < 0) {
/* failed to fork */
return -2;
/* child process returned from execve */
pid=0;
close_fd(pok[0]);
+ close_fd(pok[1]);
close_fd(pin[1]);
close_fd(pin[0]);
close_fd(pout[1]);
close_fd(pout[0]);
- pin[1] = -1;
return -3;
} else if ( n==-1 ) {
if ( errno==EAGAIN || errno==EINTR )
close_fd(pok[0]);
/* child started successfully */
-#if 0
-/* use fork for output-interposer
- * it will run in a separated process
- */
- /* catch stdout thread */
- r = ::fork();
- if (r < 0) {
- // failed to fork
- terminate();
- return -2;
- }
- if (r == 0) {
- /* 2nd child process - catch stdout */
- close_fd(pin[1]);
- close_fd(pout[1]);
- output_interposer();
- exit(0);
- }
- close_fd(pout[1]);
- close_fd(pin[0]);
- close_fd(pout[0]);
-#else /* use pthread */
close_fd(pout[1]);
close_fd(pin[0]);
int rv = pthread_create(&thread_id_tt, NULL, interposer_thread, this);
terminate();
return -2;
}
-#endif
return 0; /* all systems go - return to main */
}
+#ifdef NO_VFORK
/* child process - exec external process */
close_fd(pok[0]);
::fcntl(pok[1], F_SETFD, FD_CLOEXEC);
::nice(nicelevel);
}
-#if 0
- /* chdir to executable dir */
- char *directory;
- directory = strdup(cmd.c_str());
- if (strrchr(directory, '/') != (char *) 0) {
- ::chdir(directory);
- }
- free(directory);
-#endif
-
#ifdef HAVE_SIGSET
sigset(SIGPIPE, SIG_DFL);
#else
signal(SIGPIPE, SIG_DFL);
#endif
- int good_fds[1] = { -1 };
+ int good_fds[2] = { pok[1], -1 };
close_allv(good_fds);
::execve(argp[0], argp, envp);
close_fd(pok[1]);
exit(-1);
return -1;
+#else
+
+ /* XXX this should be done before vfork()
+ * calling malloc here only increases the time vfork() blocks
+ */
+ int argn = 0;
+ for (int i=0;argp[i];++i) { argn++; }
+ char **argx = (char **) malloc((argn + 10) * sizeof(char *));
+ argx[0] = strdup(vfork_exec_wrapper); // XXX
+
+#define FDARG(NUM, FDN) \
+ argx[NUM] = (char*) calloc(6, sizeof(char)); snprintf(argx[NUM], 6, "%d", FDN);
+
+ FDARG(1, pok[0])
+ FDARG(2, pok[1])
+ FDARG(3, pin[0])
+ FDARG(4, pin[1])
+ FDARG(5, pout[0])
+ FDARG(6, pout[1])
+ FDARG(7, stderr_mode)
+ FDARG(8, nicelevel)
+
+ for (int i=0;argp[i];++i) {
+ argx[9+i] = argp[i];
+ }
+ argx[argn+9] = NULL;
+
+ ::execve(argx[0], argx, envp);
+
+ /* if we reach here something went wrong.. */
+ char buf = 0;
+ (void) ::write(pok[1], &buf, 1 );
+ close_fd(pok[1]);
+ exit(-1);
+ return -1;
+#endif
}
void