1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        wx/unix/execute.h
3 // Purpose:     private details of wxExecute() implementation
4 // Author:      Vadim Zeitlin
5 // Copyright:   (c) 1998 Robert Roebling, Julian Smart, Vadim Zeitlin
6 //              (c) 2013 Vadim Zeitlin
7 // Licence:     wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9 
10 #ifndef _WX_UNIX_EXECUTE_H
11 #define _WX_UNIX_EXECUTE_H
12 
13 #include "wx/app.h"
14 #include "wx/hashmap.h"
15 #include "wx/process.h"
16 
17 #if wxUSE_STREAMS
18     #include "wx/unix/pipe.h"
19     #include "wx/private/streamtempinput.h"
20 #endif
21 
22 class wxEventLoopBase;
23 
24 // Information associated with a running child process.
25 class wxExecuteData
26 {
27 public:
wxExecuteData()28     wxExecuteData()
29     {
30         flags =
31         pid = 0;
32         exitcode = -1;
33 
34         process = NULL;
35 
36         syncEventLoop = NULL;
37 
38 #if wxUSE_STREAMS
39         fdOut =
40         fdErr = wxPipe::INVALID_FD;
41 #endif // wxUSE_STREAMS
42     }
43 
44     // This must be called in the parent process as soon as fork() returns to
45     // update us with the effective child PID. It also ensures that we handle
46     // SIGCHLD to be able to detect when this PID exits, so wxTheApp must be
47     // available.
48     void OnStart(int pid);
49 
50     // Called when the child process exits.
51     void OnExit(int exitcode);
52 
53     // Return true if we should (or already did) redirect the child IO.
IsRedirected()54     bool IsRedirected() const { return process && process->IsRedirected(); }
55 
56 
57     // wxExecute() flags
58     int flags;
59 
60     // the pid of the child process
61     int pid;
62 
63     // The exit code of the process, set once the child terminates.
64     int exitcode;
65 
66     // the associated process object or NULL
67     wxProcess *process;
68 
69     // Local event loop used to wait for the child process termination in
70     // synchronous execution case. We can't create it ourselves as its exact
71     // type depends on the application kind (console/GUI), so we rely on
72     // wxAppTraits setting up this pointer to point to the appropriate object.
73     wxEventLoopBase *syncEventLoop;
74 
75 #if wxUSE_STREAMS
76     // the input buffer bufOut is connected to stdout, this is why it is
77     // called bufOut and not bufIn
78     wxStreamTempInputBuffer bufOut,
79                             bufErr;
80 
81     // the corresponding FDs, -1 if not redirected
82     int fdOut,
83         fdErr;
84 #endif // wxUSE_STREAMS
85 
86 
87 private:
88     // SIGCHLD signal handler that checks whether any of the currently running
89     // children have exited.
90     static void OnSomeChildExited(int sig);
91 
92     // All currently running child processes indexed by their PID.
93     //
94     // Notice that the container doesn't own its elements.
95     WX_DECLARE_HASH_MAP(int, wxExecuteData*, wxIntegerHash, wxIntegerEqual,
96                         ChildProcessesData);
97     static ChildProcessesData ms_childProcesses;
98 
99     wxDECLARE_NO_COPY_CLASS(wxExecuteData);
100 };
101 
102 #endif // _WX_UNIX_EXECUTE_H
103