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)
8 // Copyright: (c) David Elliott
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"
22 #include <CoreFoundation/CFMachPort.h>
25 #include <mach/mach.h>
28 void wxMAC_MachPortEndProcessDetect(CFMachPortRef port
, void *data
)
30 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
31 wxLogDebug(wxT("Wow.. this actually worked!"));
33 int rc
= waitpid(abs(proc_data
->pid
), &status
, WNOHANG
);
36 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
39 if((rc
!= -1) && WIFEXITED(status
))
40 proc_data
->exitcode
= WEXITSTATUS(status
);
42 proc_data
->exitcode
= -1;
43 wxHandleProcessTermination(proc_data
);
46 int wxAddProcessCallbackForPid(wxEndProcessData
*proc_data
, int pid
)
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
)
56 wxLogDebug(wxT("No mach_task_self()"));
59 wxLogDebug(wxT("pid=%d"),pid
);
60 kernResult
= task_for_pid(taskOfOurProcess
,pid
, &machPortForProcess
);
61 if(kernResult
!= KERN_SUCCESS
)
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
);
73 CFMachPortContext termcb_contextinfo
;
74 termcb_contextinfo
.version
= NULL
;
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
)
84 wxLogDebug(wxT("No CFMachPortForProcess"));
85 mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
90 kernResult
= mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
91 if(kernResult
!=KERN_SUCCESS
)
93 wxLogDebug(wxT("Couldn't deallocate mach port"));
97 CFMachPortSetInvalidationCallBack(CFMachPortForProcess
, &wxMAC_MachPortEndProcessDetect
);
98 CFRunLoopSourceRef runloopsource
;
99 runloopsource
= CFMachPortCreateRunLoopSource(NULL
,CFMachPortForProcess
, (CFIndex
)0);
102 wxLogDebug(wxT("Couldn't create runloopsource"));
106 CFRelease(CFMachPortForProcess
);
108 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource
,kCFRunLoopDefaultMode
);
109 CFRelease(runloopsource
);
110 wxLogDebug(wxT("Successfully added notification to the runloop"));
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()