1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/corefoundation/utilsexec_cf.cpp
3 // Purpose: Execution-related utilities for Darwin
4 // Author: David Elliott, Ryan Norton (wxMacExecute)
5 // Modified by: Stefan Csomor (added necessary wxT for unicode builds)
8 // Copyright: (c) David Elliott, Ryan Norton
9 // Licence: wxWindows licence
10 // Notes: This code comes from src/mac/carbon/utilsexc.cpp,1.11
11 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/wxprec.h"
17 #endif //ndef WX_PRECOMP
18 #include "wx/unix/execute.h"
19 #include "wx/stdpaths.h"
20 #include "wx/apptrait.h"
21 #include "wx/thread.h"
22 #include "wx/process.h"
26 // Use polling instead of Mach ports, which doesn't work on Intel
27 // due to task_for_pid security issues.
29 // What's a better test for Intel vs PPC?
30 #ifdef WORDS_BIGENDIAN
38 class wxProcessTerminationEventHandler
: public wxEvtHandler
41 wxProcessTerminationEventHandler(wxEndProcessData
* data
)
44 Connect(-1, wxEVT_END_PROCESS
, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate
));
47 void OnTerminate(wxProcessEvent
& event
)
49 Disconnect(-1, wxEVT_END_PROCESS
, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate
));
50 wxHandleProcessTermination(m_data
);
54 wxEndProcessData
* m_data
;
57 class wxProcessTerminationThread
: public wxThread
60 wxProcessTerminationThread(wxEndProcessData
* data
, wxProcessTerminationEventHandler
* handler
): wxThread(wxTHREAD_DETACHED
)
66 virtual void* Entry();
68 wxProcessTerminationEventHandler
* m_handler
;
69 wxEndProcessData
* m_data
;
72 // The problem with this is that we may be examining the
73 // process e.g. in OnIdle at the point this cleans up the process,
74 // so we need to delay until it's safe.
76 void* wxProcessTerminationThread::Entry()
82 int rc
= waitpid(abs(m_data
->pid
), & status
, WNOHANG
);
85 if ((rc
!= -1) && WIFEXITED(status
))
86 m_data
->exitcode
= WEXITSTATUS(status
);
88 m_data
->exitcode
= -1;
91 wxPostEvent(m_handler
, event
);
100 int wxAddProcessCallbackForPid(wxEndProcessData
*proc_data
, int pid
)
105 wxProcessTerminationEventHandler
* handler
= new wxProcessTerminationEventHandler(proc_data
);
106 wxProcessTerminationThread
* thread
= new wxProcessTerminationThread(proc_data
, handler
);
108 if (thread
->Create() != wxTHREAD_NO_ERROR
)
110 wxLogDebug(wxT("Could not create termination detection thread."));
121 #else // !USE_POLLING
123 #include <CoreFoundation/CFMachPort.h>
125 #include <mach/mach.h>
128 void wxMAC_MachPortEndProcessDetect(CFMachPortRef port
, void *data
)
130 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
131 wxLogDebug(wxT("Process ended"));
133 int rc
= waitpid(abs(proc_data
->pid
), &status
, WNOHANG
);
136 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
139 if((rc
!= -1) && WIFEXITED(status
))
140 proc_data
->exitcode
= WEXITSTATUS(status
);
142 proc_data
->exitcode
= -1;
143 wxHandleProcessTermination(proc_data
);
146 int wxAddProcessCallbackForPid(wxEndProcessData
*proc_data
, int pid
)
150 kern_return_t kernResult
;
151 mach_port_t taskOfOurProcess
;
152 mach_port_t machPortForProcess
;
153 taskOfOurProcess
= mach_task_self();
154 if(taskOfOurProcess
== MACH_PORT_NULL
)
156 wxLogDebug(wxT("No mach_task_self()"));
159 wxLogDebug(wxT("pid=%d"),pid
);
160 kernResult
= task_for_pid(taskOfOurProcess
,pid
, &machPortForProcess
);
161 if(kernResult
!= KERN_SUCCESS
)
163 wxLogDebug(wxT("no task_for_pid()"));
164 // try seeing if it is already dead or something
165 // FIXME: a better method would be to call the callback function
166 // from idle time until the process terminates. Of course, how
167 // likely is it that it will take more than 0.1 seconds for the
168 // mach terminate event to make its way to the BSD subsystem?
169 usleep(100); // sleep for 0.1 seconds
170 wxMAC_MachPortEndProcessDetect(NULL
, (void*)proc_data
);
173 CFMachPortContext termcb_contextinfo
;
174 termcb_contextinfo
.version
= 0;
175 termcb_contextinfo
.info
= (void*)proc_data
;
176 termcb_contextinfo
.retain
= NULL
;
177 termcb_contextinfo
.release
= NULL
;
178 termcb_contextinfo
.copyDescription
= NULL
;
179 CFMachPortRef CFMachPortForProcess
;
180 Boolean ShouldFreePort
;
181 CFMachPortForProcess
= CFMachPortCreateWithPort(NULL
, machPortForProcess
, NULL
, &termcb_contextinfo
, &ShouldFreePort
);
182 if(!CFMachPortForProcess
)
184 wxLogDebug(wxT("No CFMachPortForProcess"));
185 mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
190 kernResult
= mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
191 if(kernResult
!=KERN_SUCCESS
)
193 wxLogDebug(wxT("Couldn't deallocate mach port"));
197 CFMachPortSetInvalidationCallBack(CFMachPortForProcess
, &wxMAC_MachPortEndProcessDetect
);
198 CFRunLoopSourceRef runloopsource
;
199 runloopsource
= CFMachPortCreateRunLoopSource(NULL
,CFMachPortForProcess
, (CFIndex
)0);
202 wxLogDebug(wxT("Couldn't create runloopsource"));
206 CFRelease(CFMachPortForProcess
);
208 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource
,kCFRunLoopDefaultMode
);
209 CFRelease(runloopsource
);
210 wxLogDebug(wxT("Successfully added notification to the runloop"));
214 #endif // USE_POLLING/!USE_POLLING
216 // NOTE: This doesn't really belong here but this was a handy file to
217 // put it in because it's already compiled for wxCocoa and wxMac GUI lib.
220 static wxStandardPathsCF gs_stdPaths
;
221 wxStandardPathsBase
& wxGUIAppTraits::GetStandardPaths()