2013-03-12 17:00:09 -04:00
|
|
|
/*
|
2019-08-02 23:10:55 -04:00
|
|
|
* Copyright (C) 2005-2008 Lennart Poettering
|
|
|
|
* Copyright (C) 2010-2019 Robin Gareus <robin@gareus.org>
|
|
|
|
* Copyright (C) 2013-2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
|
|
|
|
* Copyright (C) 2014-2015 Paul Davis <paul@linuxaudiosystems.com>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
2013-03-12 17:00:09 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2014-04-19 20:51:00 -04:00
|
|
|
#include <algorithm>
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2013-06-07 14:50:59 -04:00
|
|
|
#include <assert.h>
|
2013-10-14 12:07:45 -04:00
|
|
|
|
|
|
|
#ifndef COMPILER_MSVC
|
2013-06-07 14:50:59 -04:00
|
|
|
#include <dirent.h>
|
2013-10-14 12:07:45 -04:00
|
|
|
#endif
|
2013-06-07 14:50:59 -04:00
|
|
|
|
2013-10-14 12:00:28 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2013-03-12 17:00:09 -04:00
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2021-05-06 16:45:56 -04:00
|
|
|
#include <poll.h>
|
2013-03-12 17:00:09 -04:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <sys/socket.h>
|
2013-04-10 17:41:44 -04:00
|
|
|
#include <sys/ioctl.h>
|
2013-06-07 14:50:59 -04:00
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
2013-03-12 17:00:09 -04:00
|
|
|
#endif
|
|
|
|
|
2014-05-27 05:21:34 -04:00
|
|
|
#include <glibmm/miscutils.h>
|
2013-06-07 14:50:59 -04:00
|
|
|
|
2014-05-27 05:21:34 -04:00
|
|
|
#include "pbd/file_utils.h"
|
|
|
|
#include "pbd/search_path.h"
|
2020-03-29 08:56:22 -04:00
|
|
|
#include "pbd/pthread_utils.h"
|
2014-02-18 21:38:43 -05:00
|
|
|
#include "pbd/system_exec.h"
|
2013-03-12 17:00:09 -04:00
|
|
|
|
|
|
|
using namespace std;
|
2014-02-18 21:38:43 -05:00
|
|
|
using namespace PBD;
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2014-02-18 21:38:43 -05:00
|
|
|
static void * interposer_thread (void *arg);
|
2014-11-23 15:01:06 -05:00
|
|
|
|
|
|
|
#ifndef PLATFORM_WINDOWS /* POSIX Process only */
|
2013-06-07 14:04:43 -04:00
|
|
|
static void close_fd (int& fd) { if (fd >= 0) ::close (fd); fd = -1; }
|
2014-11-23 15:01:06 -05:00
|
|
|
#endif
|
2013-04-29 21:28:11 -04:00
|
|
|
|
2013-10-10 14:50:21 -04:00
|
|
|
void
|
|
|
|
SystemExec::init ()
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
pthread_mutex_init (&write_lock, NULL);
|
|
|
|
thread_active = false;
|
2013-03-12 17:00:09 -04:00
|
|
|
pid = 0;
|
|
|
|
pin[1] = -1;
|
|
|
|
nicelevel = 0;
|
|
|
|
envp = NULL;
|
2013-10-14 12:00:28 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2013-03-12 17:00:09 -04:00
|
|
|
stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
|
|
|
|
stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
|
|
|
|
stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
|
2016-02-13 11:04:11 -05:00
|
|
|
w_args = NULL;
|
2020-10-31 17:30:45 -04:00
|
|
|
#else
|
2018-11-28 20:06:42 -05:00
|
|
|
argx = NULL;
|
2013-03-12 17:00:09 -04:00
|
|
|
#endif
|
2013-10-10 14:50:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
SystemExec::SystemExec (std::string c, std::string a)
|
|
|
|
: cmd(c)
|
|
|
|
{
|
|
|
|
init ();
|
|
|
|
|
|
|
|
argp = NULL;
|
2013-03-12 17:00:09 -04:00
|
|
|
make_envp();
|
|
|
|
make_argp(a);
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemExec::SystemExec (std::string c, char **a)
|
|
|
|
: cmd(c) , argp(a)
|
|
|
|
{
|
2013-10-10 14:50:21 -04:00
|
|
|
init ();
|
|
|
|
|
2013-10-14 12:00:28 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2013-03-12 17:00:09 -04:00
|
|
|
make_wargs(a);
|
|
|
|
#endif
|
|
|
|
make_envp();
|
|
|
|
}
|
|
|
|
|
2013-10-10 14:50:21 -04:00
|
|
|
SystemExec::SystemExec (std::string command, const std::map<char, std::string> subs)
|
|
|
|
{
|
|
|
|
init ();
|
|
|
|
make_argp_escaped(command, subs);
|
2014-07-03 13:30:47 -04:00
|
|
|
|
2016-02-13 11:04:11 -05:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2016-02-13 17:12:50 -05:00
|
|
|
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]);
|
2020-10-31 08:18:12 -04:00
|
|
|
size_t start_pos = 0;
|
|
|
|
while ((start_pos = tmp.find("\"", start_pos)) != std::string::npos) {
|
|
|
|
tmp.replace (start_pos, 1, "\\\"");
|
|
|
|
start_pos += 2;
|
|
|
|
}
|
2016-02-13 17:12:50 -05:00
|
|
|
wa += " \"";
|
2016-02-13 18:02:00 -05:00
|
|
|
wa += tmp;
|
2016-02-13 17:12:50 -05:00
|
|
|
wa += '"';
|
|
|
|
}
|
|
|
|
w_args = strdup(wa.c_str());
|
2016-02-13 11:04:11 -05:00
|
|
|
}
|
|
|
|
#else
|
2014-07-03 13:30:47 -04:00
|
|
|
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 ());
|
2014-05-27 05:21:34 -04:00
|
|
|
}
|
2014-07-03 13:30:47 -04:00
|
|
|
// else argp[0] not found in path - leave it as-is, it might be an absolute path
|
2014-05-27 05:21:34 -04:00
|
|
|
|
|
|
|
// Glib::find_program_in_path () is only available in Glib >= 2.28
|
|
|
|
// cmd = Glib::find_program_in_path (argp[0]);
|
2016-02-13 11:04:11 -05:00
|
|
|
#endif
|
2013-10-10 14:50:21 -04:00
|
|
|
make_envp();
|
|
|
|
}
|
|
|
|
|
2018-11-20 17:56:12 -05:00
|
|
|
char*
|
|
|
|
SystemExec::format_key_value_parameter (std::string key, std::string value)
|
|
|
|
{
|
|
|
|
size_t start_pos = 0;
|
|
|
|
std::string v1 = value;
|
|
|
|
while((start_pos = v1.find_first_not_of(
|
|
|
|
"abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789(),.\"'",
|
|
|
|
start_pos)) != std::string::npos)
|
|
|
|
{
|
|
|
|
v1.replace(start_pos, 1, "_");
|
|
|
|
start_pos += 1;
|
|
|
|
}
|
|
|
|
|
2019-03-26 17:12:48 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
|
|
|
/* SystemExec::make_wargs() adds quotes around the complete argument
|
|
|
|
* windows uses CreateProcess() with a parameter string
|
|
|
|
* (and not an array list of separate arguments like Unix)
|
|
|
|
* so quotes need to be escaped.
|
|
|
|
*/
|
2018-11-20 17:56:12 -05:00
|
|
|
start_pos = 0;
|
|
|
|
while((start_pos = v1.find("\"", start_pos)) != std::string::npos) {
|
|
|
|
v1.replace(start_pos, 1, "\\\"");
|
|
|
|
start_pos += 2;
|
|
|
|
}
|
2019-03-26 17:12:48 -04:00
|
|
|
#endif
|
2018-11-20 17:56:12 -05:00
|
|
|
|
2019-03-26 17:12:48 -04:00
|
|
|
size_t len = key.length() + v1.length() + 2;
|
2018-11-20 17:56:12 -05:00
|
|
|
char *mds = (char*) calloc(len, sizeof(char));
|
|
|
|
snprintf(mds, len, "%s=%s", key.c_str(), v1.c_str());
|
|
|
|
return mds;
|
|
|
|
}
|
|
|
|
|
2013-10-10 14:50:21 -04:00
|
|
|
void
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::make_argp_escaped (std::string command, const std::map<char, std::string> subs)
|
2013-10-10 14:50:21 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
int inquotes = 0;
|
|
|
|
int n = 0;
|
|
|
|
size_t i = 0;
|
|
|
|
std::string arg = "";
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
argp = (char**) malloc (sizeof(char*));
|
2013-10-10 14:50:21 -04:00
|
|
|
|
2014-05-18 15:49:58 -04:00
|
|
|
for (i = 0; i <= command.length(); i++) { // include terminating '\0'
|
2013-10-10 14:50:21 -04:00
|
|
|
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 => "\"
|
2015-10-04 14:51:05 -04:00
|
|
|
case '\0':
|
2013-10-10 14:50:21 -04:00
|
|
|
case '\\': arg += '\\'; break;
|
|
|
|
default : arg += '\\'; arg += c; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '"' :
|
|
|
|
inquotes = 1;
|
|
|
|
break;
|
|
|
|
case ' ' :
|
|
|
|
case '\t':
|
2014-05-18 15:49:58 -04:00
|
|
|
case '\0':
|
2013-10-10 14:50:21 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
SystemExec::~SystemExec ()
|
|
|
|
{
|
|
|
|
terminate ();
|
|
|
|
if (envp) {
|
2019-01-23 06:59:17 -05:00
|
|
|
for (int i = 0; envp[i]; ++i) {
|
|
|
|
free (envp[i]);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
free (envp);
|
|
|
|
}
|
|
|
|
if (argp) {
|
2019-01-23 06:59:17 -05:00
|
|
|
for (int i = 0; argp[i]; ++i) {
|
|
|
|
free (argp[i]);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
free (argp);
|
|
|
|
}
|
2013-10-14 12:00:28 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2013-03-12 17:00:09 -04:00
|
|
|
if (w_args) free(w_args);
|
2020-10-31 17:30:45 -04:00
|
|
|
#else
|
2018-11-28 20:06:42 -05:00
|
|
|
if (argx) {
|
|
|
|
/* argx[0 .. 8] are fixed parameters to vfork-exec-wrapper */
|
|
|
|
for (int i = 0; i < 9; ++i) {
|
|
|
|
free (argx[i]);
|
|
|
|
}
|
|
|
|
free (argx);
|
|
|
|
}
|
2013-03-12 17:00:09 -04:00
|
|
|
#endif
|
|
|
|
pthread_mutex_destroy(&write_lock);
|
|
|
|
}
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
static void*
|
2013-03-12 17:00:09 -04:00
|
|
|
interposer_thread (void *arg) {
|
|
|
|
SystemExec *sex = static_cast<SystemExec *>(arg);
|
2020-03-29 08:56:22 -04:00
|
|
|
pthread_set_name ("ExecStdOut");
|
2013-03-12 17:00:09 -04:00
|
|
|
sex->output_interposer();
|
|
|
|
pthread_exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-02-12 19:39:41 -05:00
|
|
|
string
|
|
|
|
SystemExec::to_s () const
|
|
|
|
{
|
2016-02-13 17:12:50 -05:00
|
|
|
#ifdef PLATFORM_WINDOWS
|
2016-02-13 18:02:00 -05:00
|
|
|
return string (w_args ? w_args : "");
|
2016-02-13 17:12:50 -05:00
|
|
|
#else
|
2016-02-12 19:39:41 -05:00
|
|
|
stringstream out;
|
|
|
|
if (argp) {
|
|
|
|
for (int i = 0; argp[i]; ++i) {
|
|
|
|
out << argp[i] << " ";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out.str();
|
2016-02-13 17:12:50 -05:00
|
|
|
#endif
|
2016-02-12 19:39:41 -05:00
|
|
|
}
|
|
|
|
|
2018-11-18 20:26:43 -05:00
|
|
|
size_t
|
|
|
|
SystemExec::write_to_stdin (std::string const& d, size_t len)
|
|
|
|
{
|
|
|
|
const char *data = d.c_str();
|
|
|
|
if (len == 0) {
|
|
|
|
len = d.length();
|
|
|
|
}
|
|
|
|
return write_to_stdin ((const void*)data, len);
|
|
|
|
}
|
|
|
|
|
2019-11-09 00:28:55 -05:00
|
|
|
size_t
|
|
|
|
SystemExec::write_to_stdin (const char* data, size_t len)
|
|
|
|
{
|
|
|
|
if (len == 0) {
|
|
|
|
len = strlen (data);
|
|
|
|
}
|
|
|
|
return write_to_stdin ((const void*)data, len);
|
|
|
|
}
|
|
|
|
|
2013-10-14 12:00:28 -04:00
|
|
|
#ifdef PLATFORM_WINDOWS /* Windows Process */
|
2013-03-12 17:00:09 -04:00
|
|
|
|
|
|
|
/* HELPER FUNCTIONS */
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
static void
|
|
|
|
create_pipe (HANDLE *pipe, bool in)
|
|
|
|
{
|
|
|
|
SECURITY_ATTRIBUTES secAtt = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
|
2013-03-12 17:00:09 -04:00
|
|
|
HANDLE tmpHandle;
|
|
|
|
if (in) {
|
|
|
|
if (!CreatePipe(&pipe[0], &tmpHandle, &secAtt, 1024 * 1024)) return;
|
|
|
|
if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[1], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
|
|
|
|
} else {
|
|
|
|
if (!CreatePipe(&tmpHandle, &pipe[1], &secAtt, 1024 * 1024)) return;
|
|
|
|
if (!DuplicateHandle(GetCurrentProcess(), tmpHandle, GetCurrentProcess(), &pipe[0], 0, FALSE, DUPLICATE_SAME_ACCESS)) return;
|
|
|
|
}
|
|
|
|
CloseHandle(tmpHandle);
|
|
|
|
}
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
static void
|
|
|
|
destroy_pipe (HANDLE pipe[2])
|
|
|
|
{
|
2013-03-12 17:00:09 -04:00
|
|
|
if (pipe[0] != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(pipe[0]);
|
|
|
|
pipe[0] = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
if (pipe[1] != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(pipe[1]);
|
|
|
|
pipe[1] = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
static BOOL
|
|
|
|
CALLBACK my_terminateApp(HWND hwnd, LPARAM procId)
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
|
|
|
DWORD currentProcId = 0;
|
|
|
|
GetWindowThreadProcessId(hwnd, ¤tProcId);
|
|
|
|
if (currentProcId == (DWORD)procId)
|
|
|
|
PostMessage(hwnd, WM_CLOSE, 0, 0);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PROCESS API */
|
|
|
|
|
|
|
|
void
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::make_envp()
|
|
|
|
{
|
|
|
|
; /* environemt is copied over with CreateProcess(...,env=0 ,..) */
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::make_wargs (char** a)
|
|
|
|
{
|
2013-03-12 17:00:09 -04:00
|
|
|
std::string wa = cmd;
|
|
|
|
if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
|
|
|
|
std::replace(cmd.begin(), cmd.end(), '/', '\\' );
|
2014-04-19 20:51:00 -04:00
|
|
|
char **tmp = ++a;
|
2013-03-12 17:00:09 -04:00
|
|
|
while (tmp && *tmp) {
|
|
|
|
wa.append(" \"");
|
2015-08-03 17:06:08 -04:00
|
|
|
wa.append(*tmp);
|
|
|
|
if (strlen(*tmp) > 0 && (*tmp)[strlen(*tmp) - 1] == '\\') {
|
2015-08-03 17:09:51 -04:00
|
|
|
wa.append("\\");
|
2014-04-19 20:51:00 -04:00
|
|
|
}
|
2013-03-12 17:00:09 -04:00
|
|
|
wa.append("\"");
|
|
|
|
tmp++;
|
|
|
|
}
|
|
|
|
w_args = strdup(wa.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::make_argp (std::string args)
|
|
|
|
{
|
2013-03-12 17:00:09 -04:00
|
|
|
std::string wa = cmd;
|
|
|
|
if (cmd[0] != '"' && cmd[cmd.size()] != '"' && strchr(cmd.c_str(), ' ')) { wa = "\"" + cmd + "\""; }
|
|
|
|
std::replace(cmd.begin(), cmd.end(), '/', '\\' );
|
|
|
|
wa.append(" ");
|
|
|
|
wa.append(args);
|
|
|
|
w_args = strdup(wa.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SystemExec::terminate ()
|
|
|
|
{
|
|
|
|
::pthread_mutex_lock(&write_lock);
|
2014-04-20 17:02:32 -04:00
|
|
|
|
|
|
|
close_stdin();
|
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
if (pid) {
|
|
|
|
/* terminate */
|
|
|
|
EnumWindows(my_terminateApp, (LPARAM)pid->dwProcessId);
|
|
|
|
PostThreadMessage(pid->dwThreadId, WM_CLOSE, 0, 0);
|
|
|
|
|
|
|
|
/* kill ! */
|
|
|
|
TerminateProcess(pid->hProcess, 0xf291);
|
|
|
|
|
|
|
|
CloseHandle(pid->hThread);
|
|
|
|
CloseHandle(pid->hProcess);
|
|
|
|
destroy_pipe(stdinP);
|
|
|
|
destroy_pipe(stdoutP);
|
|
|
|
destroy_pipe(stderrP);
|
|
|
|
delete pid;
|
|
|
|
pid=0;
|
|
|
|
}
|
|
|
|
::pthread_mutex_unlock(&write_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SystemExec::wait (int options)
|
|
|
|
{
|
|
|
|
while (is_running()) {
|
2014-07-11 11:03:55 -04:00
|
|
|
WaitForSingleObject(pid->hProcess, 40);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SystemExec::is_running ()
|
|
|
|
{
|
2014-07-11 11:03:55 -04:00
|
|
|
if (!pid) return false;
|
|
|
|
DWORD exit_code;
|
|
|
|
if (GetExitCodeProcess(pid->hProcess, &exit_code)) {
|
|
|
|
if (exit_code == STILL_ACTIVE) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-03-05 13:06:15 -05:00
|
|
|
SystemExec::start (StdErrMode stderr_mode, const char * /*vfork_exec_wrapper*/)
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
|
|
|
char* working_dir = 0;
|
|
|
|
|
|
|
|
if (pid) { return 0; }
|
|
|
|
|
|
|
|
pid = new PROCESS_INFORMATION;
|
|
|
|
memset(pid, 0, sizeof(PROCESS_INFORMATION));
|
|
|
|
|
|
|
|
create_pipe(stdinP, true);
|
|
|
|
create_pipe(stdoutP, false);
|
|
|
|
|
2019-03-05 13:06:15 -05:00
|
|
|
if (stderr_mode == MergeWithStdin) {
|
2013-03-12 17:00:09 -04:00
|
|
|
/* merge stout & stderr */
|
|
|
|
DuplicateHandle(GetCurrentProcess(), stdoutP[1], GetCurrentProcess(), &stderrP[1], 0, TRUE, DUPLICATE_SAME_ACCESS);
|
2019-03-05 13:06:15 -05:00
|
|
|
} else if (stderr_mode == IgnoreAndClose) {
|
2013-03-12 17:00:09 -04:00
|
|
|
//TODO read/flush this pipe or close it...
|
|
|
|
create_pipe(stderrP, false);
|
|
|
|
} else {
|
|
|
|
//TODO: keep stderr of this process mode.
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = false;
|
2019-01-23 06:59:17 -05:00
|
|
|
STARTUPINFOA startupInfo = { sizeof(STARTUPINFO), 0, 0, 0,
|
2013-03-12 17:00:09 -04:00
|
|
|
(unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
|
|
|
|
(unsigned long)CW_USEDEFAULT, (unsigned long)CW_USEDEFAULT,
|
|
|
|
0, 0, 0,
|
|
|
|
STARTF_USESTDHANDLES,
|
|
|
|
0, 0, 0,
|
|
|
|
stdinP[0], stdoutP[1], stderrP[1]
|
|
|
|
};
|
|
|
|
|
|
|
|
success = CreateProcess(0, w_args,
|
|
|
|
0, 0, /* bInheritHandles = */ TRUE,
|
|
|
|
(CREATE_NO_WINDOW&0) | CREATE_UNICODE_ENVIRONMENT | (0&CREATE_NEW_CONSOLE),
|
|
|
|
/*env = */ 0,
|
|
|
|
working_dir,
|
|
|
|
&startupInfo, pid);
|
|
|
|
|
|
|
|
if (stdinP[0] != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(stdinP[0]);
|
|
|
|
stdinP[0] = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
if (stdoutP[1] != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(stdoutP[1]);
|
|
|
|
stdoutP[1] = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
if (stderrP[1] != INVALID_HANDLE_VALUE) {
|
|
|
|
CloseHandle(stderrP[1]);
|
|
|
|
stderrP[1] = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success) {
|
|
|
|
CloseHandle(pid->hThread);
|
|
|
|
CloseHandle(pid->hProcess);
|
|
|
|
destroy_pipe(stdinP);
|
|
|
|
destroy_pipe(stdoutP);
|
|
|
|
destroy_pipe(stderrP);
|
|
|
|
delete pid;
|
|
|
|
pid=0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
int rv = pthread_create (&thread_id_tt, NULL, interposer_thread, this);
|
2013-03-12 17:00:09 -04:00
|
|
|
thread_active=true;
|
|
|
|
if (rv) {
|
|
|
|
thread_active=false;
|
|
|
|
terminate();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
Sleep(20);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SystemExec::output_interposer()
|
|
|
|
{
|
|
|
|
DWORD bytesRead = 0;
|
|
|
|
char data[BUFSIZ];
|
2013-04-10 17:41:44 -04:00
|
|
|
#if 0 // untested code to set up nonblocking
|
|
|
|
unsigned long l = 1;
|
|
|
|
ioctlsocket(stdoutP[0], FIONBIO, &l);
|
|
|
|
#endif
|
2013-03-12 17:00:09 -04:00
|
|
|
while(1) {
|
|
|
|
#if 0 // for non-blocking pipes..
|
|
|
|
DWORD bytesAvail = 0;
|
|
|
|
PeekNamedPipe(stdoutP[0], 0, 0, 0, &bytesAvail, 0);
|
|
|
|
if (bytesAvail < 1) {Sleep(500); printf("N/A\n"); continue;}
|
|
|
|
#endif
|
|
|
|
if (stdoutP[0] == INVALID_HANDLE_VALUE) break;
|
2019-01-23 06:44:10 -05:00
|
|
|
if (!ReadFile(stdoutP[0], data, BUFSIZ - 1, &bytesRead, 0)) {
|
2014-04-19 20:51:00 -04:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
if (err == ERROR_IO_PENDING) continue;
|
|
|
|
break;
|
|
|
|
}
|
2013-03-12 17:00:09 -04:00
|
|
|
if (bytesRead < 1) continue; /* actually not needed; but this is safe. */
|
|
|
|
data[bytesRead] = 0;
|
2019-01-23 06:59:17 -05:00
|
|
|
ReadStdout(data, bytesRead); /* EMIT SIGNAL */
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
2019-01-23 06:59:17 -05:00
|
|
|
Terminated(); /* EMIT SIGNAL */
|
2015-10-23 17:27:15 -04:00
|
|
|
pthread_exit(0);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SystemExec::close_stdin()
|
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
if (stdinP[0] != INVALID_HANDLE_VALUE) FlushFileBuffers (stdinP[0]);
|
|
|
|
if (stdinP[1] != INVALID_HANDLE_VALUE) FlushFileBuffers (stdinP[1]);
|
2013-03-12 17:00:09 -04:00
|
|
|
Sleep(200);
|
2019-01-23 06:59:17 -05:00
|
|
|
destroy_pipe (stdinP);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
2018-11-18 20:26:43 -05:00
|
|
|
size_t
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::write_to_stdin (const void* data, size_t bytes)
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
DWORD r, c;
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
::pthread_mutex_lock (&write_lock);
|
2013-03-12 17:00:09 -04:00
|
|
|
|
|
|
|
c=0;
|
2018-11-18 20:26:43 -05:00
|
|
|
while (c < bytes) {
|
2019-01-23 06:59:17 -05:00
|
|
|
if (!WriteFile (stdinP[1], &((const char*)data)[c], bytes - c, &r, NULL)) {
|
2013-03-12 17:00:09 -04:00
|
|
|
if (GetLastError() == 0xE8 /*NT_STATUS_INVALID_USER_BUFFER*/) {
|
|
|
|
Sleep(100);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "SYSTEM-EXEC: stdin write error.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c += r;
|
|
|
|
}
|
|
|
|
::pthread_mutex_unlock(&write_lock);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* end windows process */
|
|
|
|
#else
|
|
|
|
/* UNIX/POSIX process */
|
|
|
|
|
|
|
|
extern char **environ;
|
2019-01-23 06:59:17 -05:00
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
void
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::make_envp()
|
|
|
|
{
|
|
|
|
int i = 0;
|
2013-03-12 17:00:09 -04:00
|
|
|
envp = (char **) calloc(1, sizeof(char*));
|
|
|
|
/* copy current environment */
|
2019-01-23 06:59:17 -05:00
|
|
|
for (i = 0; environ[i]; ++i) {
|
2013-03-12 17:00:09 -04:00
|
|
|
envp[i] = strdup(environ[i]);
|
|
|
|
envp = (char **) realloc(envp, (i+2) * sizeof(char*));
|
|
|
|
}
|
|
|
|
envp[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::make_argp(std::string args)
|
|
|
|
{
|
2013-03-12 17:00:09 -04:00
|
|
|
int argn = 1;
|
|
|
|
char *cp1;
|
|
|
|
char *cp2;
|
|
|
|
|
|
|
|
char *carg = strdup(args.c_str());
|
|
|
|
|
|
|
|
argp = (char **) malloc((argn + 1) * sizeof(char *));
|
|
|
|
if (argp == (char **) 0) {
|
|
|
|
free(carg);
|
|
|
|
return; // FATAL
|
|
|
|
}
|
|
|
|
|
|
|
|
argp[0] = strdup(cmd.c_str());
|
|
|
|
|
|
|
|
/* TODO: quotations and escapes
|
|
|
|
* http://stackoverflow.com/questions/1511797/convert-string-to-argv-in-c
|
|
|
|
*
|
|
|
|
* It's actually not needed. All relevant invocations specify 'argp' directly.
|
|
|
|
* Only 'xjadeo -L -R' uses this function and that uses neither quotations
|
|
|
|
* nor arguments with white-space.
|
|
|
|
*/
|
|
|
|
for (cp1 = cp2 = carg; *cp2 != '\0'; ++cp2) {
|
|
|
|
if (*cp2 == ' ') {
|
|
|
|
*cp2 = '\0';
|
|
|
|
argp[argn++] = strdup(cp1);
|
|
|
|
cp1 = cp2 + 1;
|
2013-10-10 14:50:21 -04:00
|
|
|
argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cp2 != cp1) {
|
|
|
|
argp[argn++] = strdup(cp1);
|
|
|
|
argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
|
|
|
|
}
|
|
|
|
argp[argn] = (char *) 0;
|
|
|
|
free(carg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SystemExec::terminate ()
|
|
|
|
{
|
2013-03-28 18:32:02 -04:00
|
|
|
::pthread_mutex_lock(&write_lock);
|
2013-06-07 13:13:12 -04:00
|
|
|
|
|
|
|
/* close stdin in an attempt to get the child to exit cleanly.
|
|
|
|
*/
|
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
close_stdin();
|
2013-06-07 11:59:47 -04:00
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
if (pid) {
|
2014-06-04 20:09:18 -04:00
|
|
|
::usleep(200000);
|
2013-04-10 17:41:44 -04:00
|
|
|
sched_yield();
|
2013-03-12 17:00:09 -04:00
|
|
|
wait(WNOHANG);
|
|
|
|
}
|
|
|
|
|
2013-06-07 13:13:12 -04:00
|
|
|
/* 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.
|
|
|
|
*/
|
2015-10-05 10:17:49 -04:00
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
if (pid) {
|
|
|
|
::kill(pid, SIGTERM);
|
2014-06-04 20:09:18 -04:00
|
|
|
::usleep(250000);
|
2013-04-10 17:41:44 -04:00
|
|
|
sched_yield();
|
2013-03-12 17:00:09 -04:00
|
|
|
wait(WNOHANG);
|
|
|
|
}
|
2013-06-07 13:13:12 -04:00
|
|
|
|
|
|
|
/* if pid is non-zero, the child task is STILL executing after being
|
|
|
|
* sent SIGTERM. Act tough ... send SIGKILL
|
|
|
|
*/
|
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
if (pid) {
|
|
|
|
::fprintf(stderr, "Process is still running! trying SIGKILL\n");
|
|
|
|
::kill(pid, SIGKILL);
|
|
|
|
}
|
|
|
|
|
|
|
|
wait();
|
|
|
|
if (thread_active) pthread_join(thread_id_tt, NULL);
|
2013-03-28 18:32:02 -04:00
|
|
|
thread_active = false;
|
2015-04-30 15:15:35 -04:00
|
|
|
assert(pid == 0);
|
2013-03-28 18:32:02 -04:00
|
|
|
::pthread_mutex_unlock(&write_lock);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
SystemExec::wait (int options)
|
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
int status = 0;
|
2013-06-07 13:13:12 -04:00
|
|
|
int ret;
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
if (pid == 0) return -1;
|
2013-06-07 13:13:12 -04:00
|
|
|
|
|
|
|
ret = waitpid (pid, &status, options);
|
|
|
|
|
|
|
|
if (ret == pid) {
|
2013-06-07 10:43:41 -04:00
|
|
|
if (WEXITSTATUS(status) || WIFSIGNALED(status)) {
|
|
|
|
pid=0;
|
|
|
|
}
|
|
|
|
} else {
|
2013-06-07 13:13:12 -04:00
|
|
|
if (ret != 0) {
|
|
|
|
if (errno == ECHILD) {
|
|
|
|
/* no currently running children, reset pid */
|
|
|
|
pid=0;
|
|
|
|
}
|
|
|
|
} /* else the process is still running */
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
SystemExec::is_running ()
|
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
int status = 0;
|
|
|
|
if (pid == 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (::waitpid (pid, &status, WNOHANG)==0) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-03-12 17:00:09 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-03-05 13:06:15 -05:00
|
|
|
SystemExec::start (StdErrMode stderr_mode, const char *vfork_exec_wrapper)
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
if (is_running ()) {
|
|
|
|
return 0;
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
2019-01-23 06:59:17 -05:00
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (::pipe(pin) < 0 || ::pipe(pout) < 0 || ::pipe(pok) < 0) {
|
|
|
|
/* Something unexpected went wrong creating a pipe. */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-11 10:48:13 -04:00
|
|
|
r = ::vfork();
|
2013-03-12 17:00:09 -04:00
|
|
|
if (r < 0) {
|
|
|
|
/* failed to fork */
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r > 0) {
|
|
|
|
/* main */
|
2019-01-23 06:59:17 -05:00
|
|
|
pid = r;
|
2013-03-12 17:00:09 -04:00
|
|
|
|
|
|
|
/* check if execve was successful. */
|
2019-01-23 06:59:17 -05:00
|
|
|
close_fd (pok[1]);
|
2013-03-12 17:00:09 -04:00
|
|
|
char buf;
|
2019-01-23 06:59:17 -05:00
|
|
|
for (;;) {
|
|
|
|
ssize_t n = ::read (pok[0], &buf, 1);
|
|
|
|
if (n == 1) {
|
2013-03-12 17:00:09 -04:00
|
|
|
/* child process returned from execve */
|
|
|
|
pid=0;
|
2019-01-23 06:59:17 -05:00
|
|
|
close_fd (pok[0]);
|
|
|
|
close_fd (pok[1]);
|
|
|
|
close_fd (pin[1]);
|
|
|
|
close_fd (pin[0]);
|
|
|
|
close_fd (pout[1]);
|
|
|
|
close_fd (pout[0]);
|
2013-03-12 17:00:09 -04:00
|
|
|
return -3;
|
2019-01-23 06:59:17 -05:00
|
|
|
} else if (n == -1) {
|
|
|
|
if (errno==EAGAIN || errno==EINTR) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2019-01-23 06:59:17 -05:00
|
|
|
|
|
|
|
close_fd (pok[0]);
|
2013-03-12 17:00:09 -04:00
|
|
|
/* child started successfully */
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
close_fd (pout[1]);
|
|
|
|
close_fd (pin[0]);
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
int rv = pthread_create (&thread_id_tt, NULL, interposer_thread, this);
|
2013-03-12 17:00:09 -04:00
|
|
|
thread_active=true;
|
2019-01-23 06:59:17 -05:00
|
|
|
|
2013-03-12 17:00:09 -04:00
|
|
|
if (rv) {
|
|
|
|
thread_active=false;
|
|
|
|
terminate();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
return 0; /* all systems go - return to main */
|
|
|
|
}
|
|
|
|
|
2014-03-11 10:48:13 -04:00
|
|
|
/* XXX this should be done before vfork()
|
2014-03-13 11:50:44 -04:00
|
|
|
* calling malloc here only increases the time vfork() blocks
|
2014-03-11 10:48:13 -04:00
|
|
|
*/
|
|
|
|
int argn = 0;
|
2019-01-23 06:59:17 -05:00
|
|
|
for (int i = 0; argp[i]; ++i) { argn++; }
|
2018-11-28 20:06:42 -05:00
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
argx = (char **) malloc ((argn + 10) * sizeof(char*));
|
|
|
|
argx[0] = strdup (vfork_exec_wrapper);
|
2014-03-11 10:48:13 -04:00
|
|
|
|
|
|
|
#define FDARG(NUM, FDN) \
|
|
|
|
argx[NUM] = (char*) calloc(6, sizeof(char)); snprintf(argx[NUM], 6, "%d", FDN);
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
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)
|
2014-03-11 10:48:13 -04:00
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
for (int i = 0; argp[i]; ++i) {
|
2014-03-11 10:48:13 -04:00
|
|
|
argx[9+i] = argp[i];
|
|
|
|
}
|
|
|
|
argx[argn+9] = NULL;
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
::execve (argx[0], argx, envp);
|
2014-03-11 10:48:13 -04:00
|
|
|
|
|
|
|
/* if we reach here something went wrong.. */
|
|
|
|
char buf = 0;
|
2019-01-23 06:59:17 -05:00
|
|
|
(void) ::write (pok[1], &buf, 1);
|
|
|
|
close_fd (pok[1]);
|
2020-10-31 17:33:10 -04:00
|
|
|
_exit (EXIT_FAILURE);
|
2014-03-11 10:48:13 -04:00
|
|
|
return -1;
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::output_interposer ()
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
int rfd = pout[0];
|
2013-03-12 17:00:09 -04:00
|
|
|
char buf[BUFSIZ];
|
2013-04-10 17:41:44 -04:00
|
|
|
ssize_t r;
|
|
|
|
unsigned long l = 1;
|
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
ioctl (rfd, FIONBIO, &l); // set non-blocking I/O
|
2013-04-10 17:41:44 -04:00
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
for (;fcntl (rfd, F_GETFL) != -1;) {
|
|
|
|
r = read (rfd, buf, BUFSIZ - 1);
|
2013-03-12 17:00:09 -04:00
|
|
|
if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
|
2021-06-20 21:48:04 -04:00
|
|
|
|
|
|
|
#ifdef __APPLE__
|
2021-06-20 19:58:40 -04:00
|
|
|
again:
|
2021-06-20 21:48:04 -04:00
|
|
|
#endif
|
2021-05-06 16:38:35 -04:00
|
|
|
|
|
|
|
/* wait till ready to read */
|
|
|
|
struct pollfd pfd;
|
|
|
|
|
|
|
|
pfd.fd = rfd;
|
2021-06-20 19:58:40 -04:00
|
|
|
pfd.events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
|
2021-05-06 16:38:35 -04:00
|
|
|
|
2021-06-20 21:48:04 -04:00
|
|
|
#ifdef __APPLE__
|
|
|
|
/* on macOS poll() will not return when the pipe
|
|
|
|
* is closed in an EOF state.
|
|
|
|
* Work around with a timeout and fail next time
|
|
|
|
* when with POLLNVAL.
|
|
|
|
*/
|
2021-06-20 19:58:40 -04:00
|
|
|
int rv = poll (&pfd, 1, 1000);
|
2021-06-20 21:48:04 -04:00
|
|
|
#else
|
|
|
|
int rv = poll (&pfd, 1, -1);
|
|
|
|
#endif
|
2021-05-06 16:38:35 -04:00
|
|
|
|
2014-07-06 17:44:48 -04:00
|
|
|
if (rv == -1) {
|
|
|
|
break;
|
|
|
|
}
|
2021-05-06 16:38:35 -04:00
|
|
|
|
2021-06-20 19:58:40 -04:00
|
|
|
if (pfd.revents & (POLLERR|POLLHUP|POLLNVAL)) {
|
2021-05-06 16:38:35 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == 1 && pfd.revents & POLLIN) {
|
|
|
|
/* back to read(2) call */
|
|
|
|
continue;
|
|
|
|
}
|
2021-06-20 21:48:04 -04:00
|
|
|
#ifdef __APPLE__
|
2021-06-20 19:58:40 -04:00
|
|
|
if (rv == 0) {
|
|
|
|
/* Timeout, poll again */
|
|
|
|
goto again;
|
|
|
|
}
|
2021-06-20 21:48:04 -04:00
|
|
|
#endif
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
if (r <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[r]=0;
|
2019-01-23 06:59:17 -05:00
|
|
|
std::string rv = std::string (buf, r);
|
|
|
|
ReadStdout (rv, r); /* EMIT SIGNAL */
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
2019-01-23 06:59:17 -05:00
|
|
|
Terminated (); /* EMIT SIGNAL */
|
|
|
|
pthread_exit (0);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SystemExec::close_stdin()
|
|
|
|
{
|
2019-01-23 06:59:17 -05:00
|
|
|
if (pin[1] < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
close_fd (pin[0]);
|
|
|
|
close_fd (pin[1]);
|
|
|
|
close_fd (pout[0]);
|
|
|
|
close_fd (pout[1]);
|
2013-03-12 17:00:09 -04:00
|
|
|
}
|
|
|
|
|
2018-11-18 20:26:43 -05:00
|
|
|
size_t
|
2019-01-23 06:59:17 -05:00
|
|
|
SystemExec::write_to_stdin (const void* data, size_t bytes)
|
2013-03-12 17:00:09 -04:00
|
|
|
{
|
2013-06-07 11:59:47 -04:00
|
|
|
ssize_t r;
|
|
|
|
size_t c;
|
2019-01-23 06:59:17 -05:00
|
|
|
::pthread_mutex_lock (&write_lock);
|
2013-03-12 17:00:09 -04:00
|
|
|
|
2019-01-23 06:59:17 -05:00
|
|
|
c = 0;
|
2018-11-18 20:26:43 -05:00
|
|
|
while (c < bytes) {
|
2013-03-12 17:00:09 -04:00
|
|
|
for (;;) {
|
2019-01-23 06:59:17 -05:00
|
|
|
r = ::write (pin[1], &((const char*)data)[c], bytes - c);
|
2013-03-12 17:00:09 -04:00
|
|
|
if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
|
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
2018-11-18 20:26:43 -05:00
|
|
|
if ((size_t) r != (bytes-c)) {
|
2013-03-12 17:00:09 -04:00
|
|
|
::pthread_mutex_unlock(&write_lock);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c += r;
|
|
|
|
}
|
2019-01-23 06:59:17 -05:00
|
|
|
fsync (pin[1]);
|
2013-03-12 17:00:09 -04:00
|
|
|
::pthread_mutex_unlock(&write_lock);
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // end UNIX process
|