stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
+ w_args = NULL;
+#elif !defined NO_VFORK
+ argx = NULL;
#endif
}
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]);
// Glib::find_program_in_path () is only available in Glib >= 2.28
// cmd = Glib::find_program_in_path (argp[0]);
-
+#endif
make_envp();
}
+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;
+ }
+
+ start_pos = 0;
+ while((start_pos = v1.find("\"", start_pos)) != std::string::npos) {
+ v1.replace(start_pos, 1, "\\\"");
+ start_pos += 2;
+ }
+
+ size_t len = key.length() + v1.length() + 4;
+ char *mds = (char*) calloc(len, sizeof(char));
+#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)
+ */
+ snprintf(mds, len, "%s=%s", key.c_str(), v1.c_str());
+#else
+ snprintf(mds, len, "%s=\"%s\"", key.c_str(), v1.c_str());
+#endif
+ return mds;
+}
+
void
SystemExec::make_argp_escaped(std::string command, const std::map<char, std::string> subs)
{
switch (c) {
case ' ' :
case '"' : arg += c; break; // "\\", "\" at end-of-string => "\"
- case '\0':
+ case '\0':
case '\\': arg += '\\'; break;
default : arg += '\\'; arg += c; break;
}
}
#ifdef PLATFORM_WINDOWS
if (w_args) free(w_args);
+#elif !defined NO_VFORK
+ if (argx) {
+ /* argx[0 .. 8] are fixed parameters to vfork-exec-wrapper */
+ for (int i = 0; i < 9; ++i) {
+ free (argx[i]);
+ }
+ free (argx);
+ }
#endif
pthread_mutex_destroy(&write_lock);
}
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
+}
+
+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);
+}
+
#ifdef PLATFORM_WINDOWS /* Windows Process */
/* HELPER FUNCTIONS */
char **tmp = ++a;
while (tmp && *tmp) {
wa.append(" \"");
- 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(*tmp);
+ if (strlen(*tmp) > 0 && (*tmp)[strlen(*tmp) - 1] == '\\') {
+ wa.append("\\");
}
- wa.append(arg);
wa.append("\"");
tmp++;
}
ReadStdout(data, bytesRead);/* EMIT SIGNAL */
}
Terminated();/* EMIT SIGNAL */
+ pthread_exit(0);
}
void
destroy_pipe(stdinP);
}
-int
-SystemExec::write_to_stdin(std::string d, size_t len)
+size_t
+SystemExec::write_to_stdin(const void* data, size_t bytes)
{
- const char *data;
DWORD r,c;
::pthread_mutex_lock(&write_lock);
- data=d.c_str();
- if (len == 0) {
- len=(d.length());
- }
c=0;
- while (c < len) {
- if (!WriteFile(stdinP[1], data+c, len-c, &r, NULL)) {
+ while (c < bytes) {
+ if (!WriteFile(stdinP[1], &((const char*)data)[c], bytes - c, &r, NULL)) {
if (GetLastError() == 0xE8 /*NT_STATUS_INVALID_USER_BUFFER*/) {
Sleep(100);
continue;
/* 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(250000);
wait();
if (thread_active) pthread_join(thread_id_tt, NULL);
thread_active = false;
+ assert(pid == 0);
::pthread_mutex_unlock(&write_lock);
}
*/
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
+
+ argx = (char **) malloc((argn + 10) * sizeof(char *));
+ argx[0] = strdup(vfork_exec_wrapper);
#define FDARG(NUM, FDN) \
argx[NUM] = (char*) calloc(6, sizeof(char)); snprintf(argx[NUM], 6, "%d", FDN);
ReadStdout(rv, r);/* EMIT SIGNAL */
}
Terminated();/* EMIT SIGNAL */
+ pthread_exit(0);
}
void
close_fd(pout[1]);
}
-int
-SystemExec::write_to_stdin(std::string d, size_t len)
+size_t
+SystemExec::write_to_stdin(const void* data, size_t bytes)
{
- const char *data;
ssize_t r;
size_t c;
::pthread_mutex_lock(&write_lock);
- data=d.c_str();
- if (len == 0) {
- len=(d.length());
- }
c=0;
- while (c < len) {
+ while (c < bytes) {
for (;;) {
- r=::write(pin[1], data+c, len-c);
+ r = ::write(pin[1], &((const char*)data)[c], bytes - c);
if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
sleep(1);
continue;
}
- if ((size_t) r != (len-c)) {
+ if ((size_t) r != (bytes-c)) {
::pthread_mutex_unlock(&write_lock);
return c;
}