manually jump forward state of system_exec files to avoid various cherry-picking-related problems during merge with ardour

This commit is contained in:
Paul Davis 2014-08-16 17:53:33 -04:00
parent c2bdb00b0c
commit c934033e23
4 changed files with 89 additions and 43 deletions

View file

@ -32,6 +32,7 @@ class LIBARDOUR_API SystemExec
public: public:
SystemExec (std::string c, std::string a = ""); SystemExec (std::string c, std::string a = "");
SystemExec (std::string c, char ** a); SystemExec (std::string c, char ** a);
SystemExec (std::string c, const std::map<char, std::string> subs);
~SystemExec (); ~SystemExec ();
int start (int stderr_mode = 1) { int start (int stderr_mode = 1) {

View file

@ -38,9 +38,7 @@
#include "ardour/export_filename.h" #include "ardour/export_filename.h"
#include "ardour/session_metadata.h" #include "ardour/session_metadata.h"
#include "ardour/soundcloud_upload.h" #include "ardour/soundcloud_upload.h"
#include "pbd/openuri.h" #include "ardour/system_exec.h"
#include "pbd/basename.h"
#include "pbd/system_exec.h"
#include "i18n.h" #include "i18n.h"
@ -297,18 +295,18 @@ ExportHandler::finish_timespan ()
while (config_map.begin() != timespan_bounds.second) { while (config_map.begin() != timespan_bounds.second) {
ExportFormatSpecPtr fmt = config_map.begin()->second.format; ExportFormatSpecPtr fmt = config_map.begin()->second.format;
std::string filepath = config_map.begin()->second.filename->get_path(fmt); std::string filename = config_map.begin()->second.filename->get_path(fmt);
if (fmt->with_cue()) { if (fmt->with_cue()) {
export_cd_marker_file (current_timespan, fmt, filepath, CDMarkerCUE); export_cd_marker_file (current_timespan, fmt, filename, CDMarkerCUE);
} }
if (fmt->with_toc()) { if (fmt->with_toc()) {
export_cd_marker_file (current_timespan, fmt, filepath, CDMarkerTOC); export_cd_marker_file (current_timespan, fmt, filename, CDMarkerTOC);
} }
if (fmt->tag()) { if (fmt->tag()) {
AudiofileTagger::tag_file(filepath, *SessionMetadata::Metadata()); AudiofileTagger::tag_file(filename, *SessionMetadata::Metadata());
} }
if (!fmt->command().empty()) { if (!fmt->command().empty()) {
@ -325,15 +323,14 @@ ExportHandler::finish_timespan ()
PBD::ScopedConnection command_connection; PBD::ScopedConnection command_connection;
std::map<char, std::string> subs; std::map<char, std::string> subs;
subs.insert (std::pair<char, std::string> ('f', filepath)); subs.insert (std::pair<char, std::string> ('f', filename));
subs.insert (std::pair<char, std::string> ('d', Glib::path_get_dirname(filepath))); subs.insert (std::pair<char, std::string> ('d', Glib::path_get_dirname(filename)));
subs.insert (std::pair<char, std::string> ('b', PBD::basename_nosuffix(filepath))); subs.insert (std::pair<char, std::string> ('b', PBD::basename_nosuffix(filename)));
subs.insert (std::pair<char, std::string> ('u', upload_username)); subs.insert (std::pair<char, std::string> ('s', session.path ()));
subs.insert (std::pair<char, std::string> ('p', upload_password)); subs.insert (std::pair<char, std::string> ('n', session.name ()));
std::cerr << "running command: " << fmt->command() << "..." << std::endl; std::cerr << "running command: " << fmt->command() << "..." << std::endl;
SystemExec *se = new SystemExec(fmt->command(), subs); ARDOUR::SystemExec *se = new SystemExec(fmt->command(), subs);
se->ReadStdout.connect_same_thread(command_connection, boost::bind(&ExportHandler::command_output, this, _1, _2)); se->ReadStdout.connect_same_thread(command_connection, boost::bind(&ExportHandler::command_output, this, _1, _2));
if (se->start (2) == 0) { if (se->start (2) == 0) {
@ -357,8 +354,8 @@ ExportHandler::finish_timespan ()
"uploading %1 - username=%2, password=%3, token=%4", "uploading %1 - username=%2, password=%3, token=%4",
filename, soundcloud_username, soundcloud_password, token) ); filename, soundcloud_username, soundcloud_password, token) );
std::string path = soundcloud_uploader->Upload ( std::string path = soundcloud_uploader->Upload (
filepath, filename,
PBD::basename_nosuffix(filepath), // title PBD::basename_nosuffix(filename), // title
token, token,
soundcloud_make_public, soundcloud_make_public,
soundcloud_downloadable, soundcloud_downloadable,

View file

@ -42,6 +42,8 @@
#include <string> #include <string>
#include <pthread.h> #include <pthread.h>
#include <signal.h> #include <signal.h>
#include <map>
#ifdef NOPBD /* unit-test outside ardour */ #ifdef NOPBD /* unit-test outside ardour */
#include <sigc++/bind.h> #include <sigc++/bind.h>
#include <sigc++/signal.h> #include <sigc++/signal.h>
@ -94,6 +96,23 @@ class LIBPBD_API SystemExec
* *
*/ */
SystemExec (std::string c, char ** a); SystemExec (std::string c, char ** a);
/** similar to \ref SystemExec but expects a whole command line, and
* handles some simple escape sequences.
*
* @param command complete command-line to be executed
* @param subs a map of <char, std::string> listing the % substitutions to
* be made.
*
* creates an argv array from the given command string, splitting into
* parameters at spaces.
* "\ " is non-splitting space, "\\" (and "\" at end of command) as "\",
* for "%<char>", <char> is looked up in subs and the corresponding string
* substituted. "%%" (and "%" at end of command)
* returns an argv array suitable for creating a new SystemExec with
*/
SystemExec (std::string command, const std::map<char, std::string> subs);
virtual ~SystemExec (); virtual ~SystemExec ();
/** fork and execute the given program /** fork and execute the given program
@ -182,6 +201,7 @@ class LIBPBD_API SystemExec
int nicelevel; ///< process nice level - defaults to 0 int nicelevel; ///< process nice level - defaults to 0
void make_argp(std::string); void make_argp(std::string);
void make_argp_escaped(std::string command, const std::map<char, std::string> subs);
void make_envp(); void make_envp();
char **argp; char **argp;
@ -198,6 +218,7 @@ class LIBPBD_API SystemExec
#else #else
pid_t pid; pid_t pid;
#endif #endif
void init ();
pthread_mutex_t write_lock; pthread_mutex_t write_lock;
int fdin; ///< file-descriptor for writing to child's STDIN. This variable is identical to pin[1] but also used as status check if the stdin pipe is open: <0 means closed. int fdin; ///< file-descriptor for writing to child's STDIN. This variable is identical to pin[1] but also used as status check if the stdin pipe is open: <0 means closed.

View file

@ -43,7 +43,12 @@
#include <sys/resource.h> #include <sys/resource.h>
#endif #endif
#include <glibmm/miscutils.h>
#define USE_VFORK
#include "pbd/file_utils.h"
#include "pbd/search_path.h"
#include "pbd/system_exec.h" #include "pbd/system_exec.h"
using namespace std; using namespace std;
@ -149,9 +154,8 @@ static int close_allv(const int except_fds[]) {
} }
#endif /* not on windows, nor vfork */ #endif /* not on windows, nor vfork */
void
SystemExec::SystemExec (std::string c, std::string a) SystemExec::init ()
: cmd(c)
{ {
pthread_mutex_init(&write_lock, NULL); pthread_mutex_init(&write_lock, NULL);
thread_active=false; thread_active=false;
@ -159,12 +163,19 @@ SystemExec::SystemExec (std::string c, std::string a)
pin[1] = -1; pin[1] = -1;
nicelevel = 0; nicelevel = 0;
envp = NULL; envp = NULL;
argp = NULL;
#ifdef PLATFORM_WINDOWS #ifdef PLATFORM_WINDOWS
stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE; stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE; stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE; stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
#endif #endif
}
SystemExec::SystemExec (std::string c, std::string a)
: cmd(c)
{
init ();
argp = NULL;
make_envp(); make_envp();
make_argp(a); make_argp(a);
} }
@ -172,16 +183,9 @@ SystemExec::SystemExec (std::string c, std::string a)
SystemExec::SystemExec (std::string c, char **a) SystemExec::SystemExec (std::string c, char **a)
: cmd(c) , argp(a) : cmd(c) , argp(a)
{ {
pthread_mutex_init(&write_lock, NULL); init ();
thread_active=false;
pid = 0;
pin[1] = -1;
nicelevel = 0;
envp = NULL;
#ifdef PLATFORM_WINDOWS #ifdef PLATFORM_WINDOWS
stdinP[0] = stdinP[1] = INVALID_HANDLE_VALUE;
stdoutP[0] = stdoutP[1] = INVALID_HANDLE_VALUE;
stderrP[0] = stderrP[1] = INVALID_HANDLE_VALUE;
make_wargs(a); make_wargs(a);
#endif #endif
make_envp(); make_envp();
@ -191,8 +195,17 @@ SystemExec::SystemExec (std::string command, const std::map<char, std::string> s
{ {
init (); init ();
make_argp_escaped(command, subs); make_argp_escaped(command, subs);
cmd = argp[0];
// cmd = strdup(argp[0]); if (find_file_in_search_path (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 ());
}
// else argp[0] not found in path - leave it as-is, it might be an absolute path
// Glib::find_program_in_path () is only available in Glib >= 2.28
// cmd = Glib::find_program_in_path (argp[0]);
make_envp(); make_envp();
} }
@ -264,9 +277,6 @@ SystemExec::make_argp_escaped(std::string command, const std::map<char, std::str
} }
} }
argp[n] = NULL; argp[n] = NULL;
char *p = argp[0];
n = 0;
} }
SystemExec::~SystemExec () SystemExec::~SystemExec ()
@ -274,13 +284,13 @@ SystemExec::~SystemExec ()
terminate (); terminate ();
if (envp) { if (envp) {
for (int i=0;envp[i];++i) { for (int i=0;envp[i];++i) {
free(envp[i]); free(envp[i]);
} }
free (envp); free (envp);
} }
if (argp) { if (argp) {
for (int i=0;argp[i];++i) { for (int i=0;argp[i];++i) {
free(argp[i]); free(argp[i]);
} }
free (argp); free (argp);
} }
@ -403,8 +413,7 @@ int
SystemExec::wait (int options) SystemExec::wait (int options)
{ {
while (is_running()) { while (is_running()) {
WaitForSingleObject(pid->hProcess, INFINITE); WaitForSingleObject(pid->hProcess, 40);
Sleep(20);
} }
return 0; return 0;
} }
@ -412,7 +421,12 @@ SystemExec::wait (int options)
bool bool
SystemExec::is_running () SystemExec::is_running ()
{ {
return pid?true:false; if (!pid) return false;
DWORD exit_code;
if (GetExitCodeProcess(pid->hProcess, &exit_code)) {
if (exit_code == STILL_ACTIVE) return true;
}
return false;
} }
int int
@ -602,7 +616,7 @@ SystemExec::make_argp(std::string args) {
*cp2 = '\0'; *cp2 = '\0';
argp[argn++] = strdup(cp1); argp[argn++] = strdup(cp1);
cp1 = cp2 + 1; cp1 = cp2 + 1;
argp = (char **) realloc(argp, (argn + 1) * sizeof(char *)); argp = (char **) realloc(argp, (argn + 1) * sizeof(char *));
} }
} }
if (cp2 != cp1) { if (cp2 != cp1) {
@ -626,7 +640,7 @@ SystemExec::terminate ()
close_stdin(); close_stdin();
if (pid) { if (pid) {
::usleep(50000); ::usleep(200000);
sched_yield(); sched_yield();
wait(WNOHANG); wait(WNOHANG);
} }
@ -637,7 +651,7 @@ SystemExec::terminate ()
if (pid) { if (pid) {
::kill(pid, SIGTERM); ::kill(pid, SIGTERM);
usleep(50000); ::usleep(250000);
sched_yield(); sched_yield();
wait(WNOHANG); wait(WNOHANG);
} }
@ -795,6 +809,10 @@ SystemExec::start (int stderr_mode, const char *vfork_exec_wrapper)
#else #else
signal(SIGPIPE, SIG_DFL); signal(SIGPIPE, SIG_DFL);
#endif #endif
if (!vfork_exec_wrapper) {
error << _("Cannot start external process, no vfork wrapper") << endmsg;
return -1;
}
int good_fds[2] = { pok[1], -1 }; int good_fds[2] = { pok[1], -1 };
close_allv(good_fds); close_allv(good_fds);
@ -857,7 +875,16 @@ SystemExec::output_interposer()
for (;fcntl(rfd, F_GETFL)!=-1;) { for (;fcntl(rfd, F_GETFL)!=-1;) {
r = read(rfd, buf, sizeof(buf)); r = read(rfd, buf, sizeof(buf));
if (r < 0 && (errno == EINTR || errno == EAGAIN)) { if (r < 0 && (errno == EINTR || errno == EAGAIN)) {
::usleep(1000); fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(rfd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 10000;
int rv = select(1, &rfds, NULL, NULL, &tv);
if (rv == -1) {
break;
}
continue; continue;
} }
if (r <= 0) { if (r <= 0) {