#include <string.h>
#include <errno.h>
#include <unistd.h>
+#include <algorithm>
#include <assert.h>
#include <sys/resource.h>
#endif
+#include <glibmm/miscutils.h>
#define USE_VFORK
+#include "pbd/file_utils.h"
+#include "pbd/search_path.h"
#include "pbd/system_exec.h"
using namespace std;
using namespace PBD;
static void * interposer_thread (void *arg);
+
+#ifndef PLATFORM_WINDOWS /* POSIX Process only */
static void close_fd (int& fd) { if (fd >= 0) ::close (fd); fd = -1; }
+#endif
#if (!defined PLATFORM_WINDOWS && defined NO_VFORK)
/*
}
#endif /* not on windows, nor vfork */
-
-SystemExec::SystemExec (std::string c, std::string a)
- : cmd(c)
+void
+SystemExec::init ()
{
pthread_mutex_init(&write_lock, NULL);
thread_active=false;
pin[1] = -1;
nicelevel = 0;
envp = NULL;
- argp = NULL;
#ifdef PLATFORM_WINDOWS
stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
+ w_args = NULL;
#endif
+}
+
+SystemExec::SystemExec (std::string c, std::string a)
+ : cmd(c)
+{
+ init ();
+
+ argp = NULL;
make_envp();
make_argp(a);
}
SystemExec::SystemExec (std::string c, char **a)
: cmd(c) , argp(a)
{
- pthread_mutex_init(&write_lock, NULL);
- thread_active=false;
- pid = 0;
- pin[1] = -1;
- nicelevel = 0;
- envp = NULL;
+ init ();
+
#ifdef PLATFORM_WINDOWS
- stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
- stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
- stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
make_wargs(a);
#endif
make_envp();
}
+SystemExec::SystemExec (std::string command, const std::map<char, std::string> subs)
+{
+ init ();
+ make_argp_escaped(command, subs);
+
+#ifdef PLATFORM_WINDOWS
+ if (argp[0] && strlen (argp[0]) > 0) {
+ std::string wa = argp[0];
+ // only add quotes to command if required..
+ if (argp[0][0] != '"'
+ && argp[0][strlen(argp[0])-1] != '"'
+ && strchr(argp[0], ' ')) {
+ wa = "\"";
+ wa += argp[0];
+ wa += "\"";
+ }
+ // ...but always quote all args
+ for (int i = 1; argp[i]; ++i) {
+ std::string tmp (argp[i]);
+ while (tmp.find("\"") != std::string::npos)
+ tmp.replace(tmp.find("\""), 1, "\\\"");
+ wa += " \"";
+ wa += tmp;
+ wa += '"';
+ }
+ w_args = strdup(wa.c_str());
+ }
+#else
+ if (find_file (Searchpath (Glib::getenv ("PATH")), argp[0], cmd)) {
+ // argp[0] exists in $PATH` - set it to the actual path where it was found
+ free (argp[0]);
+ argp[0] = strdup(cmd.c_str ());
+ }
+ // else argp[0] not found in path - leave it as-is, it might be an absolute path
+
+ // Glib::find_program_in_path () is only available in Glib >= 2.28
+ // cmd = Glib::find_program_in_path (argp[0]);
+#endif
+ make_envp();
+}
+
+void
+SystemExec::make_argp_escaped(std::string command, const std::map<char, std::string> subs)
+{
+
+ int inquotes = 0;
+ int n = 0;
+ size_t i = 0;
+ std::string arg = "";
+
+ argp = (char **) malloc(sizeof(char *));
+
+ for (i = 0; i <= command.length(); i++) { // include terminating '\0'
+ char c = command.c_str()[i];
+ if (inquotes) {
+ if (c == '"') {
+ inquotes = 0;
+ } else {
+ // still in quotes - just copy
+ arg += c;
+ }
+ } else switch (c) {
+ case '%' :
+ c = command.c_str()[++i];
+ if (c == '%' || c == '\0') {
+ // "%%", "%" at end-of-string => "%"
+ arg += '%';
+ } else {
+ // search subs for string to substitute for char
+ std::map<char, std::string>::const_iterator s = subs.find(c);
+ if (s != subs.end()) {
+ // found substitution
+ arg += s->second;
+ } else {
+ // not a valid substitution, just copy
+ arg += '%';
+ arg += c;
+ }
+ }
+ break;
+ case '\\':
+ c = command.c_str()[++i];
+ switch (c) {
+ case ' ' :
+ case '"' : arg += c; break; // "\\", "\" at end-of-string => "\"
+ case '\0':
+ case '\\': arg += '\\'; break;
+ default : arg += '\\'; arg += c; break;
+ }
+ break;
+ case '"' :
+ inquotes = 1;
+ break;
+ case ' ' :
+ case '\t':
+ case '\0':
+ if (arg.length() > 0) {
+ // if there wasn't already a space or tab, start a new parameter
+ argp = (char **) realloc(argp, (n + 2) * sizeof(char *));
+ argp[n++] = strdup (arg.c_str());
+ arg = "";
+ }
+ break;
+ default :
+ arg += c;
+ break;
+ }
+ }
+ argp[n] = NULL;
+}
+
SystemExec::~SystemExec ()
{
terminate ();
if (envp) {
for (int i=0;envp[i];++i) {
- free(envp[i]);
+ free(envp[i]);
}
free (envp);
}
if (argp) {
for (int i=0;argp[i];++i) {
- free(argp[i]);
+ free(argp[i]);
}
free (argp);
}
return 0;
}
+string
+SystemExec::to_s () const
+{
+#ifdef PLATFORM_WINDOWS
+ return string (w_args ? w_args : "");
+#else
+ stringstream out;
+ if (argp) {
+ for (int i = 0; argp[i]; ++i) {
+ out << argp[i] << " ";
+ }
+ }
+ return out.str();
+#endif
+}
+
#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);
+ if (strlen(*tmp) > 0 && (*tmp)[strlen(*tmp) - 1] == '\\') {
+ wa.append("\\");
+ }
wa.append("\"");
tmp++;
}
SystemExec::terminate ()
{
::pthread_mutex_lock(&write_lock);
+
+ close_stdin();
+
if (pid) {
/* terminate */
EnumWindows(my_terminateApp, (LPARAM)pid->dwProcessId);
SystemExec::wait (int options)
{
while (is_running()) {
- WaitForSingleObject(pid->hProcess, INFINITE);
- Sleep(20);
+ WaitForSingleObject(pid->hProcess, 40);
}
return 0;
}
bool
SystemExec::is_running ()
{
- return pid?true:false;
+ if (!pid) return false;
+ DWORD exit_code;
+ if (GetExitCodeProcess(pid->hProcess, &exit_code)) {
+ if (exit_code == STILL_ACTIVE) return true;
+ }
+ return false;
}
int
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 */
}
Terminated();/* EMIT SIGNAL */
+ pthread_exit(0);
}
void
*cp2 = '\0';
argp[argn++] = strdup(cp1);
cp1 = cp2 + 1;
- argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
+ argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
}
}
if (cp2 != cp1) {
close_stdin();
if (pid) {
- ::usleep(50000);
+ ::usleep(200000);
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);
+ ::usleep(250000);
sched_yield();
wait(WNOHANG);
}
wait();
if (thread_active) pthread_join(thread_id_tt, NULL);
thread_active = false;
+ assert(pid == 0);
::pthread_mutex_unlock(&write_lock);
}
#else
signal(SIGPIPE, SIG_DFL);
#endif
+ if (!vfork_exec_wrapper) {
+ error << _("Cannot start external process, no vfork wrapper") << endmsg;
+ return -1;
+ }
int good_fds[2] = { pok[1], -1 };
close_allv(good_fds);
for (;fcntl(rfd, F_GETFL)!=-1;) {
r = read(rfd, buf, sizeof(buf));
if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
- ::usleep(1000);
+ fd_set rfds;
+ struct timeval tv;
+ FD_ZERO(&rfds);
+ FD_SET(rfd, &rfds);
+ tv.tv_sec = 0;
+ tv.tv_usec = 10000;
+ int rv = select(1, &rfds, NULL, NULL, &tv);
+ if (rv == -1) {
+ break;
+ }
continue;
}
if (r <= 0) {
ReadStdout(rv, r);/* EMIT SIGNAL */
}
Terminated();/* EMIT SIGNAL */
+ pthread_exit(0);
}
void