]> git.saurik.com Git - wxWidgets.git/blob - src/mac/corefoundation/utilsexc_cf.cpp
return wxString from wxDynamicLibrary::GetDllExt()
[wxWidgets.git] / src / mac / corefoundation / utilsexc_cf.cpp
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)
6 // Created: 2004-11-04
7 // RCS-ID: $Id$
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 /////////////////////////////////////////////////////////////////////////////
12
13 #include "wx/wxprec.h"
14 #ifndef WX_PRECOMP
15 #include "wx/log.h"
16 #include "wx/utils.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"
23
24 #include <sys/wait.h>
25
26 // Use polling instead of Mach ports, which doesn't work on Intel
27 // due to task_for_pid security issues.
28
29 // http://developer.apple.com/technotes/tn/tn2050.html
30
31 // What's a better test for Intel vs PPC?
32 #ifdef WORDS_BIGENDIAN
33 #define USE_POLLING 0
34 #else
35 #define USE_POLLING 1
36 #endif
37
38 #if USE_POLLING
39
40 #if wxUSE_THREADS
41 class wxProcessTerminationEventHandler: public wxEvtHandler
42 {
43 public:
44 wxProcessTerminationEventHandler(wxEndProcessData* data)
45 {
46 m_data = data;
47 Connect(-1, wxEVT_END_PROCESS, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate));
48 }
49
50 void OnTerminate(wxProcessEvent& event)
51 {
52 Disconnect(-1, wxEVT_END_PROCESS, wxProcessEventHandler(wxProcessTerminationEventHandler::OnTerminate));
53 wxHandleProcessTermination(m_data);
54 delete this;
55 }
56
57 wxEndProcessData* m_data;
58 };
59
60 class wxProcessTerminationThread: public wxThread
61 {
62 public:
63 wxProcessTerminationThread(wxEndProcessData* data, wxProcessTerminationEventHandler* handler): wxThread(wxTHREAD_DETACHED)
64 {
65 m_data = data;
66 m_handler = handler;
67 }
68
69 virtual void* Entry();
70
71 wxProcessTerminationEventHandler* m_handler;
72 wxEndProcessData* m_data;
73 };
74
75 // The problem with this is that we may be examining the
76 // process e.g. in OnIdle at the point this cleans up the process,
77 // so we need to delay until it's safe.
78
79 void* wxProcessTerminationThread::Entry()
80 {
81 while (true)
82 {
83 usleep(100);
84 int status = 0;
85 int rc = waitpid(abs(m_data->pid), & status, 0);
86 if (rc != 0)
87 {
88 if ((rc != -1) && WIFEXITED(status))
89 m_data->exitcode = WEXITSTATUS(status);
90 else
91 m_data->exitcode = -1;
92
93 wxProcessEvent event;
94 wxPostEvent(m_handler, event);
95
96 break;
97 }
98 }
99
100 return NULL;
101 }
102
103 int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
104 {
105 if (pid < 1)
106 return -1;
107
108 wxProcessTerminationEventHandler* handler = new wxProcessTerminationEventHandler(proc_data);
109 wxProcessTerminationThread* thread = new wxProcessTerminationThread(proc_data, handler);
110
111 if (thread->Create() != wxTHREAD_NO_ERROR)
112 {
113 wxLogDebug(wxT("Could not create termination detection thread."));
114 delete thread;
115 delete handler;
116 return -1;
117 }
118
119 thread->Run();
120
121 return 0;
122 }
123 #else // !wxUSE_THREADS
124 int wxAddProcessCallbackForPid(wxEndProcessData*, int)
125 {
126 wxLogDebug(wxT("Could not create termination detection thread."));
127 return -1;
128 }
129 #endif // wxUSE_THREADS/!wxUSE_THREADS
130
131 #else // !USE_POLLING
132
133 #include <CoreFoundation/CFMachPort.h>
134 extern "C" {
135 #include <mach/mach.h>
136 }
137
138 void wxMAC_MachPortEndProcessDetect(CFMachPortRef port, void *data)
139 {
140 wxEndProcessData *proc_data = (wxEndProcessData*)data;
141 wxLogDebug(wxT("Process ended"));
142 int status = 0;
143 int rc = waitpid(abs(proc_data->pid), &status, WNOHANG);
144 if(!rc)
145 {
146 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
147 return;
148 }
149 if((rc != -1) && WIFEXITED(status))
150 proc_data->exitcode = WEXITSTATUS(status);
151 else
152 proc_data->exitcode = -1;
153 wxHandleProcessTermination(proc_data);
154 }
155
156 int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
157 {
158 if(pid < 1)
159 return -1;
160 kern_return_t kernResult;
161 mach_port_t taskOfOurProcess;
162 mach_port_t machPortForProcess;
163 taskOfOurProcess = mach_task_self();
164 if(taskOfOurProcess == MACH_PORT_NULL)
165 {
166 wxLogDebug(wxT("No mach_task_self()"));
167 return -1;
168 }
169 wxLogDebug(wxT("pid=%d"),pid);
170 kernResult = task_for_pid(taskOfOurProcess,pid, &machPortForProcess);
171 if(kernResult != KERN_SUCCESS)
172 {
173 wxLogDebug(wxT("no task_for_pid()"));
174 // try seeing if it is already dead or something
175 // FIXME: a better method would be to call the callback function
176 // from idle time until the process terminates. Of course, how
177 // likely is it that it will take more than 0.1 seconds for the
178 // mach terminate event to make its way to the BSD subsystem?
179 usleep(100); // sleep for 0.1 seconds
180 wxMAC_MachPortEndProcessDetect(NULL, (void*)proc_data);
181 return -1;
182 }
183 CFMachPortContext termcb_contextinfo;
184 termcb_contextinfo.version = 0;
185 termcb_contextinfo.info = (void*)proc_data;
186 termcb_contextinfo.retain = NULL;
187 termcb_contextinfo.release = NULL;
188 termcb_contextinfo.copyDescription = NULL;
189 CFMachPortRef CFMachPortForProcess;
190 Boolean ShouldFreePort;
191 CFMachPortForProcess = CFMachPortCreateWithPort(NULL, machPortForProcess, NULL, &termcb_contextinfo, &ShouldFreePort);
192 if(!CFMachPortForProcess)
193 {
194 wxLogDebug(wxT("No CFMachPortForProcess"));
195 mach_port_deallocate(taskOfOurProcess, machPortForProcess);
196 return -1;
197 }
198 if(ShouldFreePort)
199 {
200 kernResult = mach_port_deallocate(taskOfOurProcess, machPortForProcess);
201 if(kernResult!=KERN_SUCCESS)
202 {
203 wxLogDebug(wxT("Couldn't deallocate mach port"));
204 return -1;
205 }
206 }
207 CFMachPortSetInvalidationCallBack(CFMachPortForProcess, &wxMAC_MachPortEndProcessDetect);
208 CFRunLoopSourceRef runloopsource;
209 runloopsource = CFMachPortCreateRunLoopSource(NULL,CFMachPortForProcess, (CFIndex)0);
210 if(!runloopsource)
211 {
212 wxLogDebug(wxT("Couldn't create runloopsource"));
213 return -1;
214 }
215
216 CFRelease(CFMachPortForProcess);
217
218 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource,kCFRunLoopDefaultMode);
219 CFRelease(runloopsource);
220 wxLogDebug(wxT("Successfully added notification to the runloop"));
221 return 0;
222 }
223
224 #endif // USE_POLLING/!USE_POLLING
225
226 // NOTE: This doesn't really belong here but this was a handy file to
227 // put it in because it's already compiled for wxCocoa and wxMac GUI lib.
228 #if wxUSE_GUI
229
230 static wxStandardPathsCF gs_stdPaths;
231 wxStandardPathsBase& wxGUIAppTraits::GetStandardPaths()
232 {
233 return gs_stdPaths;
234 }
235
236 #endif // wxUSE_GUI
237