]> git.saurik.com Git - wxWidgets.git/blob - src/mac/corefoundation/utilsexc_cf.cpp
Moved duplicated wxMac/wxCocoa code into src/mac/corefoundation/utilsexc_cf.cpp
[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
20 #include <CoreFoundation/CFMachPort.h>
21 #include <sys/wait.h>
22 extern "C" {
23 #include <mach/mach.h>
24 }
25
26 void wxMAC_MachPortEndProcessDetect(CFMachPortRef port, void *data)
27 {
28 wxEndProcessData *proc_data = (wxEndProcessData*)data;
29 wxLogDebug(wxT("Wow.. this actually worked!"));
30 int status = 0;
31 int rc = waitpid(abs(proc_data->pid), &status, WNOHANG);
32 if(!rc)
33 {
34 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
35 return;
36 }
37 if((rc != -1) && WIFEXITED(status))
38 proc_data->exitcode = WEXITSTATUS(status);
39 else
40 proc_data->exitcode = -1;
41 wxHandleProcessTermination(proc_data);
42 }
43
44 int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
45 {
46 if(pid < 1)
47 return -1;
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)
53 {
54 wxLogDebug(wxT("No mach_task_self()"));
55 return -1;
56 }
57 wxLogDebug(wxT("pid=%d"),pid);
58 kernResult = task_for_pid(taskOfOurProcess,pid, &machPortForProcess);
59 if(kernResult != KERN_SUCCESS)
60 {
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);
69 return -1;
70 }
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)
81 {
82 wxLogDebug(wxT("No CFMachPortForProcess"));
83 mach_port_deallocate(taskOfOurProcess, machPortForProcess);
84 return -1;
85 }
86 if(ShouldFreePort)
87 {
88 kernResult = mach_port_deallocate(taskOfOurProcess, machPortForProcess);
89 if(kernResult!=KERN_SUCCESS)
90 {
91 wxLogDebug(wxT("Couldn't deallocate mach port"));
92 return -1;
93 }
94 }
95 CFMachPortSetInvalidationCallBack(CFMachPortForProcess, &wxMAC_MachPortEndProcessDetect);
96 CFRunLoopSourceRef runloopsource;
97 runloopsource = CFMachPortCreateRunLoopSource(NULL,CFMachPortForProcess, (CFIndex)0);
98 if(!runloopsource)
99 {
100 wxLogDebug(wxT("Couldn't create runloopsource"));
101 return -1;
102 }
103
104 CFRelease(CFMachPortForProcess);
105
106 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource,kCFRunLoopDefaultMode);
107 CFRelease(runloopsource);
108 wxLogDebug(wxT("Successfully added notification to the runloop"));
109 return 0;
110 }
111