34 #ifdef PLATFORM_WINDOWS
38 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/ioctl.h>
43 #include <sys/resource.h>
46 #include <glibmm/miscutils.h>
59 #ifndef PLATFORM_WINDOWS
60 static void close_fd (
int& fd) {
if (fd >= 0) ::close (fd); fd = -1; }
63 #if (!defined PLATFORM_WINDOWS && defined NO_VFORK)
69 static int close_allv(
const int except_fds[]) {
79 if ((d = opendir(
"/proc/self/fd"))) {
82 while ((de = readdir(d))) {
88 if (de->d_name[0] ==
'.')
92 l = strtol(de->d_name, &e, 10);
93 if (errno != 0 || !e || *e) {
101 if ((
long) fd != l) {
114 for (i = 0; except_fds[i] >= 0; i++)
115 if (except_fds[i] == fd) {
139 if (getrlimit(RLIMIT_NOFILE, &rl) < 0)
142 for (fd = 0; fd < (int) rl.rlim_max; fd++) {
148 for (i = 0; except_fds[i] >= 0; i++)
149 if (except_fds[i] == fd)
152 if (close(fd) < 0 && errno != EBADF)
163 pthread_mutex_init(&write_lock, NULL);
169 #ifdef PLATFORM_WINDOWS
170 stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
171 stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
172 stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
176 SystemExec::SystemExec (std::string c, std::string a)
191 #ifdef PLATFORM_WINDOWS
205 argp[0] = strdup(
cmd.c_str ());
222 std::string arg =
"";
224 argp = (
char **) malloc(
sizeof(
char *));
226 for (i = 0; i <= command.length(); i++) {
227 char c = command.c_str()[i];
237 c = command.c_str()[++i];
238 if (c ==
'%' || c ==
'\0') {
243 std::map<char, std::string>::const_iterator s = subs.find(c);
244 if (s != subs.end()) {
255 c = command.c_str()[++i];
258 case '"' : arg += c;
break;
260 case '\\': arg +=
'\\';
break;
261 default : arg +=
'\\'; arg += c;
break;
270 if (arg.length() > 0) {
272 argp = (
char **) realloc(
argp, (n + 2) *
sizeof(
char *));
273 argp[n++] = strdup (arg.c_str());
289 for (
int i=0;
envp[i];++i) {
295 for (
int i=0;
argp[i];++i) {
300 #ifdef PLATFORM_WINDOWS
301 if (w_args) free(w_args);
314 #ifdef PLATFORM_WINDOWS
318 static void create_pipe (HANDLE *pipe,
bool in) {
319 SECURITY_ATTRIBUTES secAtt = {
sizeof( SECURITY_ATTRIBUTES ), NULL, TRUE };
322 if (!CreatePipe(&pipe[0], &tmpHandle, &secAtt, 1024 * 1024))
return;
323 if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[1], 0, FALSE, DUPLICATE_SAME_ACCESS))
return;
325 if (!CreatePipe(&tmpHandle, &pipe[1], &secAtt, 1024 * 1024))
return;
326 if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[0], 0, FALSE, DUPLICATE_SAME_ACCESS))
return;
328 CloseHandle(tmpHandle);
331 static void destroy_pipe (HANDLE pipe[2]) {
332 if (pipe[0] != INVALID_HANDLE_VALUE) {
333 CloseHandle(pipe[0]);
334 pipe[0] = INVALID_HANDLE_VALUE;
336 if (pipe[1] != INVALID_HANDLE_VALUE) {
337 CloseHandle(pipe[1]);
338 pipe[1] = INVALID_HANDLE_VALUE;
342 static BOOL CALLBACK my_terminateApp(HWND hwnd, LPARAM procId)
344 DWORD currentProcId = 0;
345 GetWindowThreadProcessId(hwnd, ¤tProcId);
346 if (currentProcId == (DWORD)procId)
347 PostMessage(hwnd, WM_CLOSE, 0, 0);
359 SystemExec::make_wargs(
char **a) {
360 std::string wa =
cmd;
361 if (
cmd[0] !=
'"' &&
cmd[
cmd.size()] !=
'"' && strchr(
cmd.c_str(),
' ')) { wa =
"\"" +
cmd +
"\""; }
362 std::replace(
cmd.begin(),
cmd.end(),
'/',
'\\' );
364 while (tmp && *tmp) {
366 std::string arg(*tmp);
367 size_t start_pos = 0;
368 while((start_pos = arg.find(
"\\", start_pos)) != std::string::npos) {
369 arg.replace(start_pos, 1,
"\\\\");
376 w_args = strdup(wa.c_str());
381 std::string wa =
cmd;
382 if (
cmd[0] !=
'"' &&
cmd[
cmd.size()] !=
'"' && strchr(
cmd.c_str(),
' ')) { wa =
"\"" +
cmd +
"\""; }
383 std::replace(
cmd.begin(),
cmd.end(),
'/',
'\\' );
386 w_args = strdup(wa.c_str());
398 EnumWindows(my_terminateApp, (LPARAM)
pid->dwProcessId);
399 PostThreadMessage(
pid->dwThreadId, WM_CLOSE, 0, 0);
402 TerminateProcess(
pid->hProcess, 0xf291);
404 CloseHandle(
pid->hThread);
405 CloseHandle(
pid->hProcess);
406 destroy_pipe(stdinP);
407 destroy_pipe(stdoutP);
408 destroy_pipe(stderrP);
419 WaitForSingleObject(
pid->hProcess, 40);
427 if (!
pid)
return false;
429 if (GetExitCodeProcess(
pid->hProcess, &exit_code)) {
430 if (exit_code == STILL_ACTIVE)
return true;
438 char* working_dir = 0;
440 if (
pid) {
return 0; }
442 pid =
new PROCESS_INFORMATION;
443 memset(
pid, 0,
sizeof(PROCESS_INFORMATION));
445 create_pipe(stdinP,
true);
446 create_pipe(stdoutP,
false);
448 if (stderr_mode == 2) {
450 DuplicateHandle(GetCurrentProcess(), stdoutP[1], GetCurrentProcess(), &stderrP[1], 0, TRUE, DUPLICATE_SAME_ACCESS);
451 }
else if (stderr_mode == 1) {
453 create_pipe(stderrP,
false);
458 bool success =
false;
459 STARTUPINFOA startupInfo = {
sizeof( STARTUPINFO ), 0, 0, 0,
460 (
unsigned long)CW_USEDEFAULT, (
unsigned long)CW_USEDEFAULT,
461 (
unsigned long)CW_USEDEFAULT, (
unsigned long)CW_USEDEFAULT,
463 STARTF_USESTDHANDLES,
465 stdinP[0], stdoutP[1], stderrP[1]
468 success = CreateProcess(0, w_args,
470 (CREATE_NO_WINDOW&0) | CREATE_UNICODE_ENVIRONMENT | (0&CREATE_NEW_CONSOLE),
475 if (stdinP[0] != INVALID_HANDLE_VALUE) {
476 CloseHandle(stdinP[0]);
477 stdinP[0] = INVALID_HANDLE_VALUE;
479 if (stdoutP[1] != INVALID_HANDLE_VALUE) {
480 CloseHandle(stdoutP[1]);
481 stdoutP[1] = INVALID_HANDLE_VALUE;
483 if (stderrP[1] != INVALID_HANDLE_VALUE) {
484 CloseHandle(stderrP[1]);
485 stderrP[1] = INVALID_HANDLE_VALUE;
489 CloseHandle(
pid->hThread);
490 CloseHandle(
pid->hProcess);
491 destroy_pipe(stdinP);
492 destroy_pipe(stdoutP);
493 destroy_pipe(stderrP);
515 #if 0 // untested code to set up nonblocking
517 ioctlsocket(stdoutP[0], FIONBIO, &l);
520 #if 0 // for non-blocking pipes..
521 DWORD bytesAvail = 0;
522 PeekNamedPipe(stdoutP[0], 0, 0, 0, &bytesAvail, 0);
523 if (bytesAvail < 1) {Sleep(500); printf(
"N/A\n");
continue;}
525 if (stdoutP[0] == INVALID_HANDLE_VALUE)
break;
526 if (!ReadFile(stdoutP[0], data, BUFSIZ, &bytesRead, 0)) {
527 DWORD err = GetLastError();
528 if (err == ERROR_IO_PENDING)
continue;
531 if (bytesRead < 1)
continue;
541 if (stdinP[0]!= INVALID_HANDLE_VALUE) FlushFileBuffers(stdinP[0]);
542 if (stdinP[1]!= INVALID_HANDLE_VALUE) FlushFileBuffers(stdinP[1]);
544 destroy_pipe(stdinP);
561 if (!WriteFile(stdinP[1], data+c, len-c, &r, NULL)) {
562 if (GetLastError() == 0xE8 ) {
566 fprintf(stderr,
"SYSTEM-EXEC: stdin write error.\n");
585 envp = (
char **) calloc(1,
sizeof(
char*));
587 for (i=0;environ[i];++i) {
588 envp[i] = strdup(environ[i]);
589 envp = (
char **) realloc(
envp, (i+2) *
sizeof(
char*));
600 char *carg = strdup(args.c_str());
602 argp = (
char **) malloc((argn + 1) *
sizeof(
char *));
603 if (argp == (
char **) 0) {
608 argp[0] = strdup(
cmd.c_str());
617 for (cp1 = cp2 = carg; *cp2 !=
'\0'; ++cp2) {
620 argp[argn++] = strdup(cp1);
622 argp = (
char **) realloc(argp, (argn + 1) *
sizeof(
char *));
626 argp[argn++] = strdup(cp1);
627 argp = (
char **) realloc(argp, (argn + 1) *
sizeof(
char *));
629 argp[argn] = (
char *) 0;
656 ::kill(
pid, SIGTERM);
667 ::fprintf(stderr,
"Process is still running! trying SIGKILL\n");
668 ::kill(
pid, SIGKILL);
684 if (
pid==0)
return -1;
686 ret = waitpid (
pid, &status, options);
689 if (WEXITSTATUS(status) || WIFSIGNALED(status)) {
694 if (errno == ECHILD) {
707 if (
pid==0)
return false;
708 if (::waitpid(
pid, &status, WNOHANG)==0)
return true;
720 if (::pipe(
pin) < 0 || ::pipe(
pout) < 0 || ::pipe(
pok) < 0) {
743 ssize_t n = ::read(
pok[0], &buf, 1 );
754 }
else if ( n==-1 ) {
755 if ( errno==EAGAIN || errno==EINTR )
779 ::fcntl(
pok[1], F_SETFD, FD_CLOEXEC);
791 if (stderr_mode == 2) {
796 }
else if (stderr_mode == 1) {
812 sigset(SIGPIPE, SIG_DFL);
816 if (!vfork_exec_wrapper) {
817 error <<
_(
"Cannot start external process, no vfork wrapper") <<
endmsg;
821 int good_fds[2] = {
pok[1], -1 };
822 close_allv(good_fds);
827 (void) ::write(
pok[1], &buf, 1 );
837 for (
int i=0;
argp[i];++i) { argn++; }
838 char **argx = (
char **) malloc((argn + 10) *
sizeof(
char *));
839 argx[0] = strdup(vfork_exec_wrapper);
841 #define FDARG(NUM, FDN) \
842 argx[NUM] = (char*) calloc(6, sizeof(char)); snprintf(argx[NUM], 6, "%d", FDN);
850 FDARG(7, stderr_mode)
853 for (
int i=0;
argp[i];++i) {
858 ::execve(argx[0], argx,
envp);
862 (void) ::write(
pok[1], &buf, 1 );
877 ioctl(rfd, FIONBIO, &l);
879 for (;fcntl(rfd, F_GETFL)!=-1;) {
880 r = read(rfd, buf,
sizeof(buf));
881 if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
888 int rv =
select(1, &rfds, NULL, NULL, &tv);
898 std::string rv = std::string(buf,r);
907 if (
pin[1]<0)
return;
929 r=::write(
pin[1], data+c, len-c);
930 if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
934 if ((
size_t) r != (len-c)) {
947 #endif // end UNIX process
SystemExec(std::string c, std::string a="")
PBD::Signal0< void > Terminated
void make_argp(std::string)
LIBPBD_API Transmitter error
std::string cmd
path to command - set when creating the class
std::ostream & endmsg(std::ostream &ostr)
bool find_file(const Searchpath &search_path, const string &filename, std::string &result)
static void close_fd(int &fd)
pthread_mutex_t write_lock
int write_to_stdin(std::string d, size_t len=0)
int nicelevel
process nice level - defaults to 0
execute an external command
int start(int stderr_mode, const char *_vfork_exec_wrapper)
static void * interposer_thread(void *arg)
LIBARDOUR_API PBD::PropertyDescriptor< bool > select
PBD::Signal2< void, std::string, size_t > ReadStdout
LIBARDOUR_API bool init(bool with_vst, bool try_optimization, const char *localedir)
int pthread_create(pthread_t *__restrict thread, __const pthread_attr_t *__restrict attr, void *(*start_routine)(void *), void *__restrict arg)
void make_argp_escaped(std::string command, const std::map< char, std::string > subs)