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"
20 #include <CoreFoundation/CFMachPort.h>
23 #include <mach/mach.h>
26 void wxMAC_MachPortEndProcessDetect(CFMachPortRef port
, void *data
)
28 wxEndProcessData
*proc_data
= (wxEndProcessData
*)data
;
29 wxLogDebug(wxT("Wow.. this actually worked!"));
31 int rc
= waitpid(abs(proc_data
->pid
), &status
, WNOHANG
);
34 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
37 if((rc
!= -1) && WIFEXITED(status
))
38 proc_data
->exitcode
= WEXITSTATUS(status
);
40 proc_data
->exitcode
= -1;
41 wxHandleProcessTermination(proc_data
);
44 int wxAddProcessCallbackForPid(wxEndProcessData
*proc_data
, int pid
)
48 kern_return_t kernResult
;
49 mach_port_t taskOfOurProcess
;
50 mach_port_t machPortForProcess
;
51 taskOfOurProcess
= mach_task_self();
52 if(taskOfOurProcess
== MACH_PORT_NULL
)
54 wxLogDebug(wxT("No mach_task_self()"));
57 wxLogDebug(wxT("pid=%d"),pid
);
58 kernResult
= task_for_pid(taskOfOurProcess
,pid
, &machPortForProcess
);
59 if(kernResult
!= KERN_SUCCESS
)
61 wxLogDebug(wxT("no task_for_pid()"));
62 // try seeing if it is already dead or something
63 // FIXME: a better method would be to call the callback function
64 // from idle time until the process terminates. Of course, how
65 // likely is it that it will take more than 0.1 seconds for the
66 // mach terminate event to make its way to the BSD subsystem?
67 usleep(100); // sleep for 0.1 seconds
68 wxMAC_MachPortEndProcessDetect(NULL
, (void*)proc_data
);
71 CFMachPortContext termcb_contextinfo
;
72 termcb_contextinfo
.version
= NULL
;
73 termcb_contextinfo
.info
= (void*)proc_data
;
74 termcb_contextinfo
.retain
= NULL
;
75 termcb_contextinfo
.release
= NULL
;
76 termcb_contextinfo
.copyDescription
= NULL
;
77 CFMachPortRef CFMachPortForProcess
;
78 Boolean ShouldFreePort
;
79 CFMachPortForProcess
= CFMachPortCreateWithPort(NULL
, machPortForProcess
, NULL
, &termcb_contextinfo
, &ShouldFreePort
);
80 if(!CFMachPortForProcess
)
82 wxLogDebug(wxT("No CFMachPortForProcess"));
83 mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
88 kernResult
= mach_port_deallocate(taskOfOurProcess
, machPortForProcess
);
89 if(kernResult
!=KERN_SUCCESS
)
91 wxLogDebug(wxT("Couldn't deallocate mach port"));
95 CFMachPortSetInvalidationCallBack(CFMachPortForProcess
, &wxMAC_MachPortEndProcessDetect
);
96 CFRunLoopSourceRef runloopsource
;
97 runloopsource
= CFMachPortCreateRunLoopSource(NULL
,CFMachPortForProcess
, (CFIndex
)0);
100 wxLogDebug(wxT("Couldn't create runloopsource"));
104 CFRelease(CFMachPortForProcess
);
106 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource
,kCFRunLoopDefaultMode
);
107 CFRelease(runloopsource
);
108 wxLogDebug(wxT("Successfully added notification to the runloop"));