]> git.saurik.com Git - wxWidgets.git/blob - src/mac/corefoundation/utilsexc_cf.cpp
Add comments explaining the workaround for mingw 3.2.3
[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
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
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
22 #include <CoreFoundation/CFMachPort.h>
23 #include <sys/wait.h>
24 extern "C" {
25 #include <mach/mach.h>
26 }
27
28 void wxMAC_MachPortEndProcessDetect(CFMachPortRef port, void *data)
29 {
30 wxEndProcessData *proc_data = (wxEndProcessData*)data;
31 wxLogDebug(wxT("Wow.. this actually worked!"));
32 int status = 0;
33 int rc = waitpid(abs(proc_data->pid), &status, WNOHANG);
34 if(!rc)
35 {
36 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
37 return;
38 }
39 if((rc != -1) && WIFEXITED(status))
40 proc_data->exitcode = WEXITSTATUS(status);
41 else
42 proc_data->exitcode = -1;
43 wxHandleProcessTermination(proc_data);
44 }
45
46 int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
47 {
48 if(pid < 1)
49 return -1;
50 kern_return_t kernResult;
51 mach_port_t taskOfOurProcess;
52 mach_port_t machPortForProcess;
53 taskOfOurProcess = mach_task_self();
54 if(taskOfOurProcess == MACH_PORT_NULL)
55 {
56 wxLogDebug(wxT("No mach_task_self()"));
57 return -1;
58 }
59 wxLogDebug(wxT("pid=%d"),pid);
60 kernResult = task_for_pid(taskOfOurProcess,pid, &machPortForProcess);
61 if(kernResult != KERN_SUCCESS)
62 {
63 wxLogDebug(wxT("no task_for_pid()"));
64 // try seeing if it is already dead or something
65 // FIXME: a better method would be to call the callback function
66 // from idle time until the process terminates. Of course, how
67 // likely is it that it will take more than 0.1 seconds for the
68 // mach terminate event to make its way to the BSD subsystem?
69 usleep(100); // sleep for 0.1 seconds
70 wxMAC_MachPortEndProcessDetect(NULL, (void*)proc_data);
71 return -1;
72 }
73 CFMachPortContext termcb_contextinfo;
74 termcb_contextinfo.version = 0;
75 termcb_contextinfo.info = (void*)proc_data;
76 termcb_contextinfo.retain = NULL;
77 termcb_contextinfo.release = NULL;
78 termcb_contextinfo.copyDescription = NULL;
79 CFMachPortRef CFMachPortForProcess;
80 Boolean ShouldFreePort;
81 CFMachPortForProcess = CFMachPortCreateWithPort(NULL, machPortForProcess, NULL, &termcb_contextinfo, &ShouldFreePort);
82 if(!CFMachPortForProcess)
83 {
84 wxLogDebug(wxT("No CFMachPortForProcess"));
85 mach_port_deallocate(taskOfOurProcess, machPortForProcess);
86 return -1;
87 }
88 if(ShouldFreePort)
89 {
90 kernResult = mach_port_deallocate(taskOfOurProcess, machPortForProcess);
91 if(kernResult!=KERN_SUCCESS)
92 {
93 wxLogDebug(wxT("Couldn't deallocate mach port"));
94 return -1;
95 }
96 }
97 CFMachPortSetInvalidationCallBack(CFMachPortForProcess, &wxMAC_MachPortEndProcessDetect);
98 CFRunLoopSourceRef runloopsource;
99 runloopsource = CFMachPortCreateRunLoopSource(NULL,CFMachPortForProcess, (CFIndex)0);
100 if(!runloopsource)
101 {
102 wxLogDebug(wxT("Couldn't create runloopsource"));
103 return -1;
104 }
105
106 CFRelease(CFMachPortForProcess);
107
108 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource,kCFRunLoopDefaultMode);
109 CFRelease(runloopsource);
110 wxLogDebug(wxT("Successfully added notification to the runloop"));
111 return 0;
112 }
113
114 // NOTE: This doens't really belong here but this was a handy file to
115 // put it in because it's already compiled for wxCocoa and wxMac GUI lib.
116 static wxStandardPathsCF gs_stdPaths;
117 wxStandardPathsBase& wxGUIAppTraits::GetStandardPaths()
118 {
119 return gs_stdPaths;
120 }
121