]> git.saurik.com Git - wxWidgets.git/blame - src/mac/utilsexc.cpp
corrections for buffers
[wxWidgets.git] / src / mac / utilsexc.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: utilsexec.cpp
3// Purpose: Execution-related utilities
a31a5f85 4// Author: Stefan Csomor
c8023fed 5// Modified by: David Elliott
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
e40298d5 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
90771fe5 13//#pragma implementation
e9576ca5
SC
14#endif
15
c8023fed 16#include "wx/log.h"
e9576ca5 17#include "wx/utils.h"
f5c6eb5c 18#ifdef __DARWIN__
5fde6fcc 19#include "wx/unix/execute.h"
c8023fed
DE
20#include <unistd.h>
21#include <sys/wait.h>
90771fe5 22extern "C" {
c8023fed 23#include <mach/mach.h>
90771fe5 24}
c8023fed 25#include <CoreFoundation/CFMachPort.h>
5fde6fcc 26#endif
e9576ca5
SC
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
f5c6eb5c 32#ifndef __DARWIN__
e9576ca5
SC
33#define wxEXECUTE_WIN_MESSAGE 10000
34
171d29f9 35long wxExecute(const wxString& command, int flags, wxProcess *handler)
e9576ca5
SC
36{
37 // TODO
31d560bf 38 wxFAIL_MSG( _T("wxExecute() not yet implemented") );
e9576ca5
SC
39 return 0;
40}
5fde6fcc
GD
41#endif
42
f5c6eb5c 43#ifdef __DARWIN__
c8023fed 44void wxMAC_MachPortEndProcessDetect(CFMachPortRef port, void *data)
5fde6fcc 45{
c8023fed 46 wxEndProcessData *proc_data = (wxEndProcessData*)data;
5f3f0f17 47 wxLogDebug(wxT("Wow.. this actually worked!"));
c8023fed
DE
48 int status = 0;
49 int rc = waitpid(abs(proc_data->pid), &status, WNOHANG);
50 if(!rc)
51 {
5f3f0f17 52 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
c8023fed
DE
53 return;
54 }
55 if((rc != -1) && WIFEXITED(status))
56 proc_data->exitcode = WEXITSTATUS(status);
57 else
58 proc_data->exitcode = -1;
59 wxHandleProcessTermination(proc_data);
60}
61
62int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
63{
64 if(pid < 1)
65 return -1;
66 kern_return_t kernResult;
67 mach_port_t taskOfOurProcess;
68 mach_port_t machPortForProcess;
69 taskOfOurProcess = mach_task_self();
70 if(taskOfOurProcess == MACH_PORT_NULL)
71 {
5f3f0f17 72 wxLogDebug(wxT("No mach_task_self()"));
c8023fed
DE
73 return -1;
74 }
5f3f0f17 75 wxLogDebug(wxT("pid=%d"),pid);
c8023fed
DE
76 kernResult = task_for_pid(taskOfOurProcess,pid, &machPortForProcess);
77 if(kernResult != KERN_SUCCESS)
78 {
c32a538a 79 wxLogDebug(wxT("no task_for_pid()"));
c8023fed
DE
80 // try seeing if it is already dead or something
81 // FIXME: a better method would be to call the callback function
82 // from idle time until the process terminates. Of course, how
83 // likely is it that it will take more than 0.1 seconds for the
84 // mach terminate event to make its way to the BSD subsystem?
85 usleep(100); // sleep for 0.1 seconds
86 wxMAC_MachPortEndProcessDetect(NULL, (void*)proc_data);
87 return -1;
88 }
89 CFMachPortContext termcb_contextinfo;
90 termcb_contextinfo.version = NULL;
91 termcb_contextinfo.info = (void*)proc_data;
92 termcb_contextinfo.retain = NULL;
93 termcb_contextinfo.release = NULL;
94 termcb_contextinfo.copyDescription = NULL;
95 CFMachPortRef CFMachPortForProcess;
96 Boolean ShouldFreePort;
97 CFMachPortForProcess = CFMachPortCreateWithPort(NULL, machPortForProcess, NULL, &termcb_contextinfo, &ShouldFreePort);
98 if(!CFMachPortForProcess)
99 {
5f3f0f17 100 wxLogDebug(wxT("No CFMachPortForProcess"));
c8023fed
DE
101 mach_port_deallocate(taskOfOurProcess, machPortForProcess);
102 return -1;
103 }
104 if(ShouldFreePort)
105 {
106 kernResult = mach_port_deallocate(taskOfOurProcess, machPortForProcess);
107 if(kernResult!=KERN_SUCCESS)
108 {
5f3f0f17 109 wxLogDebug(wxT("Couldn't deallocate mach port"));
c8023fed
DE
110 return -1;
111 }
112 }
113 CFMachPortSetInvalidationCallBack(CFMachPortForProcess, &wxMAC_MachPortEndProcessDetect);
114 CFRunLoopSourceRef runloopsource;
115 runloopsource = CFMachPortCreateRunLoopSource(NULL,CFMachPortForProcess, (CFIndex)0);
116 if(!runloopsource)
117 {
5f3f0f17 118 wxLogDebug(wxT("Couldn't create runloopsource"));
c8023fed
DE
119 return -1;
120 }
121
122 CFRelease(CFMachPortForProcess);
123
124 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource,kCFRunLoopDefaultMode);
125 CFRelease(runloopsource);
5f3f0f17 126 wxLogDebug(wxT("Successfully added notification to the runloop"));
c8023fed 127 return 0;
5fde6fcc 128}
f5c6eb5c 129#endif