Optimize automation-event process splitting
[ardour.git] / libs / pbd / system_exec.cc
index 60e73badfcf19eb567ebe72f91a2a63253608819..6f86e5edb987207184d760badbb68c2013f4dfcb 100644 (file)
@@ -170,6 +170,9 @@ SystemExec::init ()
        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
 }
 
@@ -199,6 +202,29 @@ SystemExec::SystemExec (std::string command, const std::map<char, std::string> s
        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]);
@@ -208,10 +234,43 @@ SystemExec::SystemExec (std::string command, const std::map<char, std::string> s
 
        // 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)
 {
@@ -256,7 +315,7 @@ SystemExec::make_argp_escaped(std::string command, const std::map<char, std::str
                                switch (c) {
                                        case ' ' :
                                        case '"' : arg += c; break; // "\\", "\" at end-of-string => "\"
-                                       case '\0': 
+                                       case '\0':
                                        case '\\': arg += '\\'; break;
                                        default  : arg += '\\'; arg += c; break;
                                }
@@ -299,6 +358,14 @@ SystemExec::~SystemExec ()
        }
 #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);
 }
@@ -311,6 +378,32 @@ interposer_thread (void *arg) {
        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 */
@@ -363,13 +456,10 @@ SystemExec::make_wargs(char **a) {
        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++;
        }
@@ -533,6 +623,7 @@ SystemExec::output_interposer()
                ReadStdout(data, bytesRead);/* EMIT SIGNAL */
        }
        Terminated();/* EMIT SIGNAL */
+       pthread_exit(0);
 }
 
 void
@@ -544,21 +635,16 @@ SystemExec::close_stdin()
        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;
@@ -651,7 +737,7 @@ SystemExec::terminate ()
        /* 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);
@@ -671,6 +757,7 @@ SystemExec::terminate ()
        wait();
        if (thread_active) pthread_join(thread_id_tt, NULL);
        thread_active = false;
+       assert(pid == 0);
        ::pthread_mutex_unlock(&write_lock);
 }
 
@@ -834,8 +921,9 @@ SystemExec::start (int stderr_mode, const char *vfork_exec_wrapper)
         */
        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);
@@ -898,6 +986,7 @@ SystemExec::output_interposer()
                ReadStdout(rv, r);/* EMIT SIGNAL */
        }
        Terminated();/* EMIT SIGNAL */
+       pthread_exit(0);
 }
 
 void
@@ -910,27 +999,22 @@ SystemExec::close_stdin()
        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;
                        }