2 Copyright (C) 2010 Paul Davis
3 Copyright (C) 2010-2014 Robin Gareus <robin@gareus.org>
4 Copyright (C) 2005-2008 Lennart Poettering
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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>
50 #include "pbd/file_utils.h"
51 #include "pbd/search_path.h"
52 #include "pbd/system_exec.h"
57 static void * interposer_thread (void *arg);
59 #ifndef PLATFORM_WINDOWS /* POSIX Process only */
60 static void close_fd (int& fd) { if (fd >= 0) ::close (fd); fd = -1; }
63 #if (!defined PLATFORM_WINDOWS && defined NO_VFORK)
65 * This function was part of libasyncns.
67 * Copyright 2005-2008 Lennart Poettering
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)
158 #endif /* not on windows, nor vfork */
163 pthread_mutex_init (&write_lock, NULL);
164 thread_active = false;
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;
174 #elif !defined NO_VFORK
179 SystemExec::SystemExec (std::string c, std::string a)
189 SystemExec::SystemExec (std::string c, char **a)
194 #ifdef PLATFORM_WINDOWS
200 SystemExec::SystemExec (std::string command, const std::map<char, std::string> subs)
203 make_argp_escaped(command, subs);
205 #ifdef PLATFORM_WINDOWS
206 if (argp[0] && strlen (argp[0]) > 0) {
207 std::string wa = argp[0];
208 // only add quotes to command if required..
209 if (argp[0][0] != '"'
210 && argp[0][strlen(argp[0])-1] != '"'
211 && strchr(argp[0], ' ')) {
216 // ...but always quote all args
217 for (int i = 1; argp[i]; ++i) {
218 std::string tmp (argp[i]);
219 while (tmp.find("\"") != std::string::npos)
220 tmp.replace(tmp.find("\""), 1, "\\\"");
225 w_args = strdup(wa.c_str());
228 if (find_file (Searchpath (Glib::getenv ("PATH")), argp[0], cmd)) {
229 // argp[0] exists in $PATH` - set it to the actual path where it was found
231 argp[0] = strdup(cmd.c_str ());
233 // else argp[0] not found in path - leave it as-is, it might be an absolute path
235 // Glib::find_program_in_path () is only available in Glib >= 2.28
236 // cmd = Glib::find_program_in_path (argp[0]);
242 SystemExec::format_key_value_parameter (std::string key, std::string value)
244 size_t start_pos = 0;
245 std::string v1 = value;
246 while((start_pos = v1.find_first_not_of(
247 "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789(),.\"'",
248 start_pos)) != std::string::npos)
250 v1.replace(start_pos, 1, "_");
255 while((start_pos = v1.find("\"", start_pos)) != std::string::npos) {
256 v1.replace(start_pos, 1, "\\\"");
260 size_t len = key.length() + v1.length() + 4;
261 char *mds = (char*) calloc(len, sizeof(char));
262 #ifdef PLATFORM_WINDOWS
263 /* SystemExec::make_wargs() adds quotes around the complete argument
264 * windows uses CreateProcess() with a parameter string
265 * (and not an array list of separate arguments)
267 snprintf(mds, len, "%s=%s", key.c_str(), v1.c_str());
269 snprintf(mds, len, "%s=\"%s\"", key.c_str(), v1.c_str());
275 SystemExec::make_argp_escaped (std::string command, const std::map<char, std::string> subs)
281 std::string arg = "";
283 argp = (char**) malloc (sizeof(char*));
285 for (i = 0; i <= command.length(); i++) { // include terminating '\0'
286 char c = command.c_str()[i];
291 // still in quotes - just copy
296 c = command.c_str()[++i];
297 if (c == '%' || c == '\0') {
298 // "%%", "%" at end-of-string => "%"
301 // search subs for string to substitute for char
302 std::map<char, std::string>::const_iterator s = subs.find(c);
303 if (s != subs.end()) {
304 // found substitution
307 // not a valid substitution, just copy
314 c = command.c_str()[++i];
317 case '"' : arg += c; break; // "\\", "\" at end-of-string => "\"
319 case '\\': arg += '\\'; break;
320 default : arg += '\\'; arg += c; break;
329 if (arg.length() > 0) {
330 // if there wasn't already a space or tab, start a new parameter
331 argp = (char **) realloc(argp, (n + 2) * sizeof(char *));
332 argp[n++] = strdup (arg.c_str());
344 SystemExec::~SystemExec ()
348 for (int i = 0; envp[i]; ++i) {
354 for (int i = 0; argp[i]; ++i) {
359 #ifdef PLATFORM_WINDOWS
360 if (w_args) free(w_args);
361 #elif !defined NO_VFORK
363 /* argx[0 .. 8] are fixed parameters to vfork-exec-wrapper */
364 for (int i = 0; i < 9; ++i) {
370 pthread_mutex_destroy(&write_lock);
374 interposer_thread (void *arg) {
375 SystemExec *sex = static_cast<SystemExec *>(arg);
376 sex->output_interposer();
382 SystemExec::to_s () const
384 #ifdef PLATFORM_WINDOWS
385 return string (w_args ? w_args : "");
389 for (int i = 0; argp[i]; ++i) {
390 out << argp[i] << " ";
398 SystemExec::write_to_stdin (std::string const& d, size_t len)
400 const char *data = d.c_str();
404 return write_to_stdin ((const void*)data, len);
407 #ifdef PLATFORM_WINDOWS /* Windows Process */
409 /* HELPER FUNCTIONS */
412 create_pipe (HANDLE *pipe, bool in)
414 SECURITY_ATTRIBUTES secAtt = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
417 if (!CreatePipe(&pipe[0], &tmpHandle, &secAtt, 1024 * 1024)) return;
418 if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
420 if (!CreatePipe(&tmpHandle, &pipe[1], &secAtt, 1024 * 1024)) return;
421 if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
423 CloseHandle(tmpHandle);
427 destroy_pipe (HANDLE pipe[2])
429 if (pipe[0] != INVALID_HANDLE_VALUE) {
430 CloseHandle(pipe[0]);
431 pipe[0] = INVALID_HANDLE_VALUE;
433 if (pipe[1] != INVALID_HANDLE_VALUE) {
434 CloseHandle(pipe[1]);
435 pipe[1] = INVALID_HANDLE_VALUE;
440 CALLBACK my_terminateApp(HWND hwnd, LPARAM procId)
442 DWORD currentProcId = 0;
443 GetWindowThreadProcessId(hwnd, ¤tProcId);
444 if (currentProcId == (DWORD)procId)
445 PostMessage(hwnd, WM_CLOSE, 0, 0);
452 SystemExec::make_envp()
454 ; /* environemt is copied over with CreateProcess(...,env=0 ,..) */
458 SystemExec::make_wargs (char** a)
460 std::string wa = cmd;
461 if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
462 std::replace(cmd.begin(), cmd.end(), '/', '\\' );
464 while (tmp && *tmp) {
467 if (strlen(*tmp) > 0 && (*tmp)[strlen(*tmp) - 1] == '\\') {
473 w_args = strdup(wa.c_str());
477 SystemExec::make_argp (std::string args)
479 std::string wa = cmd;
480 if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
481 std::replace(cmd.begin(), cmd.end(), '/', '\\' );
484 w_args = strdup(wa.c_str());
488 SystemExec::terminate ()
490 ::pthread_mutex_lock(&write_lock);
496 EnumWindows(my_terminateApp, (LPARAM)pid->dwProcessId);
497 PostThreadMessage(pid->dwThreadId, WM_CLOSE, 0, 0);
500 TerminateProcess(pid->hProcess, 0xf291);
502 CloseHandle(pid->hThread);
503 CloseHandle(pid->hProcess);
504 destroy_pipe(stdinP);
505 destroy_pipe(stdoutP);
506 destroy_pipe(stderrP);
510 ::pthread_mutex_unlock(&write_lock);
514 SystemExec::wait (int options)
516 while (is_running()) {
517 WaitForSingleObject(pid->hProcess, 40);
523 SystemExec::is_running ()
525 if (!pid) return false;
527 if (GetExitCodeProcess(pid->hProcess, &exit_code)) {
528 if (exit_code == STILL_ACTIVE) return true;
534 SystemExec::start (int stderr_mode, const char * /*vfork_exec_wrapper*/)
536 char* working_dir = 0;
538 if (pid) { return 0; }
540 pid = new PROCESS_INFORMATION;
541 memset(pid, 0, sizeof(PROCESS_INFORMATION));
543 create_pipe(stdinP, true);
544 create_pipe(stdoutP, false);
546 if (stderr_mode == 2) {
547 /* merge stout & stderr */
548 DuplicateHandle(GetCurrentProcess(), stdoutP[1], GetCurrentProcess(), &stderrP[1], 0, TRUE, DUPLICATE_SAME_ACCESS);
549 } else if (stderr_mode == 1) {
550 //TODO read/flush this pipe or close it...
551 create_pipe(stderrP, false);
553 //TODO: keep stderr of this process mode.
556 bool success = false;
557 STARTUPINFOA startupInfo = { sizeof(STARTUPINFO), 0, 0, 0,
558 (unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
559 (unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
561 STARTF_USESTDHANDLES,
563 stdinP[0], stdoutP[1], stderrP[1]
566 success = CreateProcess(0, w_args,
567 0, 0, /* bInheritHandles = */ TRUE,
568 (CREATE_NO_WINDOW&0) | CREATE_UNICODE_ENVIRONMENT | (0&CREATE_NEW_CONSOLE),
573 if (stdinP[0] != INVALID_HANDLE_VALUE) {
574 CloseHandle(stdinP[0]);
575 stdinP[0] = INVALID_HANDLE_VALUE;
577 if (stdoutP[1] != INVALID_HANDLE_VALUE) {
578 CloseHandle(stdoutP[1]);
579 stdoutP[1] = INVALID_HANDLE_VALUE;
581 if (stderrP[1] != INVALID_HANDLE_VALUE) {
582 CloseHandle(stderrP[1]);
583 stderrP[1] = INVALID_HANDLE_VALUE;
587 CloseHandle(pid->hThread);
588 CloseHandle(pid->hProcess);
589 destroy_pipe(stdinP);
590 destroy_pipe(stdoutP);
591 destroy_pipe(stderrP);
597 int rv = pthread_create (&thread_id_tt, NULL, interposer_thread, this);
609 SystemExec::output_interposer()
613 #if 0 // untested code to set up nonblocking
615 ioctlsocket(stdoutP[0], FIONBIO, &l);
618 #if 0 // for non-blocking pipes..
619 DWORD bytesAvail = 0;
620 PeekNamedPipe(stdoutP[0], 0, 0, 0, &bytesAvail, 0);
621 if (bytesAvail < 1) {Sleep(500); printf("N/A\n"); continue;}
623 if (stdoutP[0] == INVALID_HANDLE_VALUE) break;
624 if (!ReadFile(stdoutP[0], data, BUFSIZ - 1, &bytesRead, 0)) {
625 DWORD err = GetLastError();
626 if (err == ERROR_IO_PENDING) continue;
629 if (bytesRead < 1) continue; /* actually not needed; but this is safe. */
631 ReadStdout(data, bytesRead); /* EMIT SIGNAL */
633 Terminated(); /* EMIT SIGNAL */
638 SystemExec::close_stdin()
640 if (stdinP[0] != INVALID_HANDLE_VALUE) FlushFileBuffers (stdinP[0]);
641 if (stdinP[1] != INVALID_HANDLE_VALUE) FlushFileBuffers (stdinP[1]);
643 destroy_pipe (stdinP);
647 SystemExec::write_to_stdin (const void* data, size_t bytes)
651 ::pthread_mutex_lock (&write_lock);
655 if (!WriteFile (stdinP[1], &((const char*)data)[c], bytes - c, &r, NULL)) {
656 if (GetLastError() == 0xE8 /*NT_STATUS_INVALID_USER_BUFFER*/) {
660 fprintf(stderr, "SYSTEM-EXEC: stdin write error.\n");
666 ::pthread_mutex_unlock(&write_lock);
671 /* end windows process */
673 /* UNIX/POSIX process */
675 extern char **environ;
678 SystemExec::make_envp()
681 envp = (char **) calloc(1, sizeof(char*));
682 /* copy current environment */
683 for (i = 0; environ[i]; ++i) {
684 envp[i] = strdup(environ[i]);
685 envp = (char **) realloc(envp, (i+2) * sizeof(char*));
691 SystemExec::make_argp(std::string args)
697 char *carg = strdup(args.c_str());
699 argp = (char **) malloc((argn + 1) * sizeof(char *));
700 if (argp == (char **) 0) {
705 argp[0] = strdup(cmd.c_str());
707 /* TODO: quotations and escapes
708 * http://stackoverflow.com/questions/1511797/convert-string-to-argv-in-c
710 * It's actually not needed. All relevant invocations specify 'argp' directly.
711 * Only 'xjadeo -L -R' uses this function and that uses neither quotations
712 * nor arguments with white-space.
714 for (cp1 = cp2 = carg; *cp2 != '\0'; ++cp2) {
717 argp[argn++] = strdup(cp1);
719 argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
723 argp[argn++] = strdup(cp1);
724 argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
726 argp[argn] = (char *) 0;
731 SystemExec::terminate ()
733 ::pthread_mutex_lock(&write_lock);
735 /* close stdin in an attempt to get the child to exit cleanly.
746 /* if pid is non-zero, the child task is still executing (i.e. it did
747 * not exit in response to stdin being closed). try to kill it.
751 ::kill(pid, SIGTERM);
757 /* if pid is non-zero, the child task is STILL executing after being
758 * sent SIGTERM. Act tough ... send SIGKILL
762 ::fprintf(stderr, "Process is still running! trying SIGKILL\n");
763 ::kill(pid, SIGKILL);
767 if (thread_active) pthread_join(thread_id_tt, NULL);
768 thread_active = false;
770 ::pthread_mutex_unlock(&write_lock);
774 SystemExec::wait (int options)
779 if (pid == 0) return -1;
781 ret = waitpid (pid, &status, options);
784 if (WEXITSTATUS(status) || WIFSIGNALED(status)) {
789 if (errno == ECHILD) {
790 /* no currently running children, reset pid */
793 } /* else the process is still running */
799 SystemExec::is_running ()
805 if (::waitpid (pid, &status, WNOHANG)==0) {
812 SystemExec::start (int stderr_mode, const char *vfork_exec_wrapper)
820 if (::pipe(pin) < 0 || ::pipe(pout) < 0 || ::pipe(pok) < 0) {
821 /* Something unexpected went wrong creating a pipe. */
839 /* check if execve was successful. */
843 ssize_t n = ::read (pok[0], &buf, 1);
845 /* child process returned from execve */
854 } else if (n == -1) {
855 if (errno==EAGAIN || errno==EINTR) {
863 /* child started successfully */
868 int rv = pthread_create (&thread_id_tt, NULL, interposer_thread, this);
876 return 0; /* all systems go - return to main */
880 /* child process - exec external process */
882 ::fcntl (pok[1], F_SETFD, FD_CLOEXEC);
885 if (pin[0] != STDIN_FILENO) {
886 ::dup2 (pin[0], STDIN_FILENO);
890 if (pout[1] != STDOUT_FILENO) {
891 ::dup2 (pout[1], STDOUT_FILENO);
894 if (stderr_mode == 2) {
895 /* merge STDERR into output */
896 if (pout[1] != STDERR_FILENO) {
897 ::dup2(pout[1], STDERR_FILENO);
899 } else if (stderr_mode == 1) {
901 ::close(STDERR_FILENO);
906 if (pout[1] != STDOUT_FILENO && pout[1] != STDERR_FILENO) {
915 sigset (SIGPIPE, SIG_DFL);
917 signal (SIGPIPE, SIG_DFL);
919 if (!vfork_exec_wrapper) {
920 error << _("Cannot start external process, no vfork wrapper") << endmsg;
924 int good_fds[2] = { pok[1], -1 };
925 close_allv(good_fds);
927 ::execve(argp[0], argp, envp);
929 #else /* ! NO_VFORK */
931 /* XXX this should be done before vfork()
932 * calling malloc here only increases the time vfork() blocks
935 for (int i = 0; argp[i]; ++i) { argn++; }
937 argx = (char **) malloc ((argn + 10) * sizeof(char*));
938 argx[0] = strdup (vfork_exec_wrapper);
940 #define FDARG(NUM, FDN) \
941 argx[NUM] = (char*) calloc(6, sizeof(char)); snprintf(argx[NUM], 6, "%d", FDN);
949 FDARG (7, stderr_mode)
952 for (int i = 0; argp[i]; ++i) {
957 ::execve (argx[0], argx, envp);
960 /* if we reach here something went wrong.. */
962 (void) ::write (pok[1], &buf, 1);
969 SystemExec::output_interposer ()
976 ioctl (rfd, FIONBIO, &l); // set non-blocking I/O
978 for (;fcntl (rfd, F_GETFL) != -1;) {
979 r = read (rfd, buf, BUFSIZ - 1);
980 if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
987 int rv = select (1, &rfds, NULL, NULL, &tv);
997 std::string rv = std::string (buf, r);
998 ReadStdout (rv, r); /* EMIT SIGNAL */
1000 Terminated (); /* EMIT SIGNAL */
1005 SystemExec::close_stdin()
1017 SystemExec::write_to_stdin (const void* data, size_t bytes)
1021 ::pthread_mutex_lock (&write_lock);
1026 r = ::write (pin[1], &((const char*)data)[c], bytes - c);
1027 if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
1031 if ((size_t) r != (bytes-c)) {
1032 ::pthread_mutex_unlock(&write_lock);
1040 ::pthread_mutex_unlock(&write_lock);
1044 #endif // end UNIX process