fixed error checking in wxKill
[wxWidgets.git] / src / common / process.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: process.cpp
3 // Purpose: Process termination classes
4 // Author: Guilhem Lavaux
5 // Modified by: Vadim Zeitlin to check error codes, added Detach() method
6 // Created: 24/06/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "process.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/process.h"
32
33 // ----------------------------------------------------------------------------
34 // event tables and such
35 // ----------------------------------------------------------------------------
36
37 DEFINE_EVENT_TYPE(wxEVT_END_PROCESS)
38
39 IMPLEMENT_DYNAMIC_CLASS(wxProcess, wxEvtHandler)
40 IMPLEMENT_DYNAMIC_CLASS(wxProcessEvent, wxEvent)
41
42 // ============================================================================
43 // wxProcess implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // wxProcess creation
48 // ----------------------------------------------------------------------------
49
50 void wxProcess::Init(wxEvtHandler *parent, int id, bool redirect)
51 {
52 if ( parent )
53 SetNextHandler(parent);
54
55 m_id = id;
56 m_redirect = redirect;
57
58 #if wxUSE_STREAMS
59 m_inputStream = NULL;
60 m_errorStream = NULL;
61 m_outputStream = NULL;
62 #endif // wxUSE_STREAMS
63 }
64
65 // ----------------------------------------------------------------------------
66 // wxProcess termination
67 // ----------------------------------------------------------------------------
68
69 wxProcess::~wxProcess()
70 {
71 #if wxUSE_STREAMS
72 delete m_inputStream;
73 delete m_errorStream;
74 delete m_outputStream;
75 #endif // wxUSE_STREAMS
76 }
77
78 void wxProcess::OnTerminate(int pid, int status)
79 {
80 wxProcessEvent event(m_id, pid, status);
81
82 if ( !ProcessEvent(event) )
83 delete this;
84 //else: the object which processed the event is responsible for deleting
85 // us!
86 }
87
88 void wxProcess::Detach()
89 {
90 SetNextHandler(NULL);
91 }
92
93 // ----------------------------------------------------------------------------
94 // process IO redirection
95 // ----------------------------------------------------------------------------
96
97 #if wxUSE_STREAMS
98
99 void wxProcess::SetPipeStreams(wxInputStream *inputSstream,
100 wxOutputStream *outputStream,
101 wxInputStream *errorStream)
102 {
103 m_inputStream = inputSstream;
104 m_errorStream = errorStream;
105 m_outputStream = outputStream;
106 }
107
108 #endif // wxUSE_STREAMS
109
110 // ----------------------------------------------------------------------------
111 // process killing
112 // ----------------------------------------------------------------------------
113
114 /* static */
115 wxKillError wxProcess::Kill(int pid, wxSignal sig)
116 {
117 wxKillError rc;
118 (void)wxKill(pid, sig, &rc);
119
120 return rc;
121 }
122
123 /* static */
124 bool wxProcess::Exists(int pid)
125 {
126 switch ( Kill(pid, wxSIGNONE) )
127 {
128 case wxKILL_OK:
129 case wxKILL_ACCESS_DENIED:
130 return TRUE;
131
132 default:
133 case wxKILL_ERROR:
134 case wxKILL_BAD_SIGNAL:
135 wxFAIL_MSG( _T("unexpected wxProcess::Kill() return code") );
136 // fall through
137
138 case wxKILL_NO_PROCESS:
139 return FALSE;
140 }
141 }
142