X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fpbd%2Fsystem_exec.cc;h=60e73badfcf19eb567ebe72f91a2a63253608819;hb=b11a18d226ba505c3f873f9bbf242fbb9391f42f;hp=d26eedad6d8e3d987149de4ffaf2f6a976023cd3;hpb=070bb7a8abcd787958b3ea07e77043dd404d653c;p=ardour.git diff --git a/libs/pbd/system_exec.cc b/libs/pbd/system_exec.cc index d26eedad6d..60e73badfc 100644 --- a/libs/pbd/system_exec.cc +++ b/libs/pbd/system_exec.cc @@ -1,7 +1,7 @@ /* Copyright (C) 2010 Paul Davis - Copyright 2005-2008 Lennart Poettering - Author: Robin Gareus + Copyright (C) 2010-2014 Robin Gareus + 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 @@ -23,11 +23,15 @@ #include #include #include +#include #include + +#ifndef COMPILER_MSVC #include +#endif -#ifdef __WIN32__ +#ifdef PLATFORM_WINDOWS #include #else #include @@ -39,15 +43,24 @@ #include #endif +#include + +#define USE_VFORK +#include "pbd/file_utils.h" +#include "pbd/search_path.h" #include "pbd/system_exec.h" using namespace std; -void * interposer_thread (void *arg); +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 -#ifndef __WIN32__ +#if (!defined PLATFORM_WINDOWS && defined NO_VFORK) /* * This function was part of libasyncns. * LGPL v2.1 @@ -142,11 +155,10 @@ static int close_allv(const int except_fds[]) { return 0; } -#endif /* not on windows */ - +#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; @@ -154,12 +166,19 @@ SystemExec::SystemExec (std::string c, std::string a) pin[1] = -1; nicelevel = 0; envp = NULL; - argp = 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; #endif +} + +SystemExec::SystemExec (std::string c, std::string a) + : cmd(c) +{ + init (); + + argp = NULL; make_envp(); make_argp(a); } @@ -167,43 +186,124 @@ SystemExec::SystemExec (std::string c, std::string 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; -#ifdef __WIN32__ - stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE; - stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE; - stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE; + init (); + +#ifdef PLATFORM_WINDOWS make_wargs(a); #endif make_envp(); } +SystemExec::SystemExec (std::string command, const std::map subs) +{ + init (); + make_argp_escaped(command, subs); + + 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]); + + make_envp(); +} + +void +SystemExec::make_argp_escaped(std::string command, const std::map 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::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); } -#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(arg); sex->output_interposer(); @@ -211,7 +311,7 @@ interposer_thread (void *arg) { return 0; } -#ifdef __WIN32__ /* Windows Process */ +#ifdef PLATFORM_WINDOWS /* Windows Process */ /* HELPER FUNCTIONS */ @@ -260,10 +360,16 @@ SystemExec::make_wargs(char **a) { 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++; } @@ -284,6 +390,9 @@ void SystemExec::terminate () { ::pthread_mutex_lock(&write_lock); + + close_stdin(); + if (pid) { /* terminate */ EnumWindows(my_terminateApp, (LPARAM)pid->dwProcessId); @@ -307,8 +416,7 @@ int SystemExec::wait (int options) { while (is_running()) { - WaitForSingleObject(pid->hProcess, INFINITE); - Sleep(20); + WaitForSingleObject(pid->hProcess, 40); } return 0; } @@ -316,11 +424,16 @@ SystemExec::wait (int options) 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 -SystemExec::start (int stderr_mode) +SystemExec::start (int stderr_mode, const char * /*vfork_exec_wrapper*/) { char* working_dir = 0; @@ -410,7 +523,11 @@ SystemExec::output_interposer() 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 */ @@ -502,7 +619,7 @@ SystemExec::make_argp(std::string args) { *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) { @@ -526,7 +643,7 @@ SystemExec::terminate () close_stdin(); if (pid) { - ::usleep(50000); + ::usleep(200000); sched_yield(); wait(WNOHANG); } @@ -537,7 +654,7 @@ SystemExec::terminate () if (pid) { ::kill(pid, SIGTERM); - ::usleep(50000); + ::usleep(250000); sched_yield(); wait(WNOHANG); } @@ -592,7 +709,7 @@ SystemExec::is_running () } 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? @@ -604,7 +721,11 @@ SystemExec::start (int stderr_mode) return -1; } +#ifndef NO_VFORK + r = ::vfork(); +#else r = ::fork(); +#endif if (r < 0) { /* failed to fork */ return -2; @@ -623,11 +744,11 @@ SystemExec::start (int stderr_mode) /* 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 ) @@ -638,28 +759,6 @@ SystemExec::start (int stderr_mode) 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); @@ -670,10 +769,10 @@ SystemExec::start (int stderr_mode) 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); @@ -708,23 +807,17 @@ SystemExec::start (int stderr_mode) ::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 + if (!vfork_exec_wrapper) { + error << _("Cannot start external process, no vfork wrapper") << endmsg; + return -1; + } - int good_fds[1] = { -1 }; + int good_fds[2] = { pok[1], -1 }; close_allv(good_fds); ::execve(argp[0], argp, envp); @@ -734,6 +827,42 @@ SystemExec::start (int stderr_mode) 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 @@ -749,7 +878,16 @@ SystemExec::output_interposer() 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) {