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"
24 // Use polling instead of Mach ports, which doesn't work on Intel
25 // due to task_for_pid security issues.
27 // What's a better test for Intel vs PPC?
28 #ifdef WORDS_BIGENDIAN
36 class wxProcessTerminationEventHandler
: public wxEvtHandler
39 wxProcessTerminationEventHandler(wxEndProcessData
* data
)
42 Connect(-1, wxEVT_END_PROCESS
, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate
));
45 void OnTerminate(wxProcessEvent
& event
)
47 Disconnect(-1, wxEVT_END_PROCESS
, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate
));
48 wxHandleProcessTermination(m_data
);
52 wxEndProcessData
* m_data
;
55 class wxProcessTerminationThread
: public wxThread
58 wxProcessTerminationThread(wxEndProcessData
* data
, wxProcessTerminationEventHandler
* handler
): wxThread(wxTHREAD_DETACHED
)
64 virtual void* Entry();
66 wxProcessTerminationEventHandler
* m_handler
;
67 wxEndProcessData
* m_data
;
70 // The problem with this is that we may be examining the
71 // process e.g. in OnIdle at the point this cleans up the process,
72 // so we need to delay until it's safe.
74 void* wxProcessTerminationThread::Entry()
80 int rc
= waitpid(abs(m_data
->pid
), & status
, WNOHANG
);
83 if ((rc
!= -1) && WIFEXITED(status
))
84 m_data
->exitcode
= WEXITSTATUS(status
);
86 m_data
->exitcode
= -1;
89 wxPostEvent(m_handler
, event
);
98 int wxAddProcessCallbackForPid(wxEndProcessData
*proc_data
, int pid
)
103 wxProcessTerminationEventHandler
* handler
= new wxProcessTerminationEventHandler(proc_data
);
104 wxProcessTerminationThread
* thread
= new wxProcessTerminationThread(proc_data
, handler
);
106 if (thread
->Create() != wxTHREAD_NO_ERROR
)
108 wxLogDebug(wxT("Could not create termination detection thread."));
121 #include <CoreFoundation/CFMachPort.h>
122 #include <sys/wait.h>
124 #include <mach/mach.h>
127 void wxMAC_MachPortEndProcessDetect(CFMachPortRef port
, void *data
)
129 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
130 wxLogDebug(wxT("Process ended"));
132 int rc
= waitpid(abs(proc_data
->pid
), &status
, WNOHANG
);
135 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
138 if((rc
!= -1) && WIFEXITED(status
))
139 proc_data
->exitcode
= WEXITSTATUS(status
);
141 proc_data
->exitcode
= -1;
142 wxHandleProcessTermination(proc_data
);
145 int wxAddProcessCallbackForPid(wxEndProcessData
*proc_data
, int pid
)
149 kern_return_t kernResult
;
150 mach_port_t taskOfOurProcess
;
151 mach_port_t machPortForProcess
;
152 taskOfOurProcess
= mach_task_self();
153 if(taskOfOurProcess
== MACH_PORT_NULL
)
155 wxLogDebug(wxT("No mach_task_self()"));
158 wxLogDebug(wxT("pid=%d"),pid
);
159 kernResult
= task_for_pid(taskOfOurProcess
,pid
, &machPortForProcess
);
160 if(kernResult
!= KERN_SUCCESS
)
162 wxLogDebug(wxT("no task_for_pid()"));
163 // try seeing if it is already dead or something
164 // FIXME: a better method would be to call the callback function
165 // from idle time until the process terminates. Of course, how
166 // likely is it that it will take more than 0.1 seconds for the
167 // mach terminate event to make its way to the BSD subsystem?
168 usleep(100); // sleep for 0.1 seconds
169 wxMAC_MachPortEndProcessDetect(NULL
, (void*)proc_data
);
172 CFMachPortContext termcb_contextinfo
;
173 termcb_contextinfo
.version
= 0;
174 termcb_contextinfo
.info
= (void*)proc_data
;
175 termcb_contextinfo
.retain
= NULL
;
176 termcb_contextinfo
.release
= NULL
;
177 termcb_contextinfo
.copyDescription
= NULL
;
178 CFMachPortRef CFMachPortForProcess
;
179 Boolean ShouldFreePort
;
180 CFMachPortForProcess
= CFMachPortCreateWithPort(NULL
, machPortForProcess
, NULL
, &termcb_contextinfo
, &ShouldFreePort
);
181 if(!CFMachPortForProcess
)
183 wxLogDebug(wxT("No CFMachPortForProcess"));
184 mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
189 kernResult
= mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
190 if(kernResult
!=KERN_SUCCESS
)
192 wxLogDebug(wxT("Couldn't deallocate mach port"));
196 CFMachPortSetInvalidationCallBack(CFMachPortForProcess
, &wxMAC_MachPortEndProcessDetect
);
197 CFRunLoopSourceRef runloopsource
;
198 runloopsource
= CFMachPortCreateRunLoopSource(NULL
,CFMachPortForProcess
, (CFIndex
)0);
201 wxLogDebug(wxT("Couldn't create runloopsource"));
205 CFRelease(CFMachPortForProcess
);
207 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource
,kCFRunLoopDefaultMode
);
208 CFRelease(runloopsource
);
209 wxLogDebug(wxT("Successfully added notification to the runloop"));
216 // NOTE: This doens'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.
218 static wxStandardPathsCF gs_stdPaths
;
219 wxStandardPathsBase
& wxGUIAppTraits::GetStandardPaths()