Ardour  9.0-pre0-582-g084a23a80d
pbd/pbd/system_exec.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2019 Robin Gareus <robin@gareus.org>
3  * Copyright (C) 2013-2014 Colin Fletcher <colin.m.fletcher@googlemail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 #ifndef _libpbd_system_exec_h_
20 #define _libpbd_system_exec_h_
21 
22 #ifndef STDIN_FILENO
23 #define STDIN_FILENO 0
24 #endif
25 #ifndef STDOUT_FILENO
26 #define STDOUT_FILENO 1
27 #endif
28 #ifndef STDERR_FILENO
29 #define STDERR_FILENO 2
30 #endif
31 
32 #ifdef PLATFORM_WINDOWS
33 #include <windows.h>
34 
35 #ifdef interface
36 #undef interface // VKamyshniy: to avoid "include/giomm-2.4/giomm/dbusmessage.h:270:94: error: expected ',' or '...' before 'struct'"
37 #endif
38 
39 #else // posix
40 #include <sys/types.h>
41 #include <sys/wait.h> /* WNOHANG is part of the exposed API */
42 #endif
43 
44 #include <string>
45 #include <pthread.h>
46 #include <signal.h>
47 #include <map>
48 
49 #ifdef NOPBD /* unit-test outside ardour */
50 #include <sigc++/bind.h>
51 #include <sigc++/signal.h>
52 #else
53 #include "pbd/signals.h"
54 #endif
55 
56 namespace PBD {
57 
73 {
74  public:
89  SystemExec (std::string cmd, std::string argv = "", bool supress_ld_env = false);
101  SystemExec (std::string cmd, char **argv, bool supress_ld_env = false);
102 
117  SystemExec (std::string command, const std::map<char, std::string> subs, bool supress_ld_env = false);
118 
119  virtual ~SystemExec ();
120 
121  static char* format_key_value_parameter (std::string, std::string);
122 
123  std::string to_s() const;
124 
125  enum StdErrMode {
126  ShareWithParent = 0,
127  IgnoreAndClose = 1,
128  MergeWithStdin = 2
129  };
130 
143  int start (StdErrMode stderr_mode, const char *_vfork_exec_wrapper);
149  void terminate ();
156  bool is_running ();
167  int wait (int options=0);
174  void close_stdin ();
181  size_t write_to_stdin (std::string const& d, size_t len=0);
182 
189  size_t write_to_stdin (const char* d, size_t len=0);
190 
196  size_t write_to_stdin (const void* data, size_t bytes=0);
197 
202 #ifdef NOPBD /* outside ardour */
203  sigc::signal<void, std::string,size_t> ReadStdout;
204 #else
205  PBD::Signal<void(std::string,size_t)> ReadStdout;
206 #endif
207 
209 #ifdef NOPBD /* outside ardour */
210  sigc::signal<void> Terminated;
211 #else
213 #endif
214 
227 
228  protected:
229  std::string cmd;
230  int nicelevel;
231 
232  void make_argp(std::string);
233  void make_argp_escaped(std::string command, const std::map<char, std::string> subs);
234  void make_envp (bool supress_ld_env);
235 
236  char **argp;
237  char **envp;
238 
239  private:
240 #ifdef PLATFORM_WINDOWS
241  PROCESS_INFORMATION *pid;
242  HANDLE stdinP[2];
243  HANDLE stdoutP[2];
244  HANDLE stderrP[2];
245  char *w_args;
246  void make_wargs(char **);
247 #else
248  pid_t pid;
249  char **argx;
250 #endif
251  void init ();
252  pthread_mutex_t write_lock;
253 
254  int pok[2];
255  int pin[2];
256  int pout[2];
257 
258  pthread_t thread_id_tt;
260 
261 }; /* end class */
262 
263 }; /* end namespace */
264 
265 #endif /* _libpbd_system_exec_h_ */
execute an external command
int wait(int options=0)
SystemExec(std::string cmd, char **argv, bool supress_ld_env=false)
std::string cmd
path to command - set when creating the class
virtual ~SystemExec()
void make_argp(std::string)
size_t write_to_stdin(const char *d, size_t len=0)
size_t write_to_stdin(const void *data, size_t bytes=0)
void make_argp_escaped(std::string command, const std::map< char, std::string > subs)
pthread_mutex_t write_lock
SystemExec(std::string command, const std::map< char, std::string > subs, bool supress_ld_env=false)
PBD::Signal< void()> Terminated
PBD::Signal< void(std::string, size_t)> ReadStdout
std::string to_s() const
size_t write_to_stdin(std::string const &d, size_t len=0)
int nicelevel
process nice level - defaults to 0
static char * format_key_value_parameter(std::string, std::string)
SystemExec(std::string cmd, std::string argv="", bool supress_ld_env=false)
int start(StdErrMode stderr_mode, const char *_vfork_exec_wrapper)
void output_interposer()
void make_envp(bool supress_ld_env)
#define LIBPBD_API
Definition: axis_view.h:42
minimum disk read bytes