VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/darwin/SUPLib-darwin.cpp@ 4871

Last change on this file since 4871 was 4871, checked in by vboxsync, 17 years ago

The vboxdrv descriptor must be marked close on exec.

File size: 6.1 KB
Line 
1/** @file
2 *
3 * VBox host drivers - Ring-0 support drivers - Darwin host:
4 * Darwin implementations for support library
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19
20/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23#define LOG_GROUP LOG_GROUP_SUP
24#include <VBox/types.h>
25#include <VBox/sup.h>
26#include <VBox/param.h>
27#include <VBox/err.h>
28#include <VBox/log.h>
29#include <iprt/path.h>
30#include <iprt/assert.h>
31#include <iprt/err.h>
32#include <iprt/string.h>
33#include "SUPLibInternal.h"
34#include "SUPDRVIOC.h"
35
36#include <sys/fcntl.h>
37#include <sys/ioctl.h>
38#include <errno.h>
39#include <unistd.h>
40#include <stdlib.h>
41#include <mach/mach_port.h>
42#include <IOKit/IOKitLib.h>
43
44
45/*******************************************************************************
46* Defined Constants And Macros *
47*******************************************************************************/
48/** BSD Device name. */
49#define DEVICE_NAME "/dev/vboxdrv"
50/** The IOClass key of the service (see SUPDrv-darwin.cpp / Info.plist). */
51#define IOCLASS_NAME "org_virtualbox_SupDrv"
52
53
54/*******************************************************************************
55* Global Variables *
56*******************************************************************************/
57/** Handle to the open device. */
58static int g_hDevice = -1;
59/** The IOMasterPort. */
60static mach_port_t g_MasterPort = 0;
61/** The current service connection. */
62static io_connect_t g_Connection = NULL;
63
64
65int suplibOsInit(size_t cbReserve)
66{
67 /*
68 * Check if already initialized.
69 */
70 if (g_hDevice >= 0)
71 return VINF_SUCCESS;
72
73 /*
74 * Open the IOKit client first - The first step is finding the service.
75 */
76 mach_port_t MasterPort;
77 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
78 if (kr != kIOReturnSuccess)
79 {
80 LogRel(("IOMasterPort -> %d\n", kr));
81 return VERR_GENERAL_FAILURE;
82 }
83
84 CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_NAME);
85 if (!ClassToMatch)
86 {
87 LogRel(("IOServiceMatching(\"%s\") failed.\n", IOCLASS_NAME));
88 return VERR_GENERAL_FAILURE;
89 }
90
91 /* Create an io_iterator_t for all instances of our drivers class that exist in the IORegistry. */
92 io_iterator_t Iterator;
93 kr = IOServiceGetMatchingServices(g_MasterPort, ClassToMatch, &Iterator);
94 if (kr != kIOReturnSuccess)
95 {
96 LogRel(("IOServiceGetMatchingServices returned %d\n", kr));
97 return VERR_GENERAL_FAILURE;
98 }
99
100 /* Get the first item in the iterator and release it. */
101 io_service_t ServiceObject = IOIteratorNext(Iterator);
102 IOObjectRelease(Iterator);
103 if (!ServiceObject)
104 {
105 LogRel(("Couldn't find any matches.\n"));
106 return VERR_GENERAL_FAILURE;
107 }
108
109 /*
110 * Open the service.
111 * This will cause the user client class in SUPDrv-darwin.cpp to be instantiated.
112 */
113 kr = IOServiceOpen(ServiceObject, mach_task_self(), 0, &g_Connection);
114 IOObjectRelease(ServiceObject);
115 if (kr != kIOReturnSuccess)
116 {
117 LogRel(("IOServiceOpen returned %d\n", kr));
118 return VERR_GENERAL_FAILURE;
119 }
120
121 /*
122 * Now, try open the BSD device.
123 */
124 g_hDevice = open(DEVICE_NAME, O_RDWR, 0);
125 if (g_hDevice < 0)
126 {
127 int rc = errno;
128 LogRel(("Failed to open \"%s\", errno=%d\n", rc));
129 kr = IOServiceClose(g_Connection);
130 if (kr != kIOReturnSuccess)
131 LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
132 return RTErrConvertFromErrno(rc);
133 }
134
135 /*
136 * Mark the file handle close on exec.
137 */
138 if (fcntl(g_hDevice, F_SETFD, FD_CLOEXEC) != 0)
139 {
140 int rc = errno;
141 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d\n", errno));
142 close(g_hDevice);
143 g_hDevice = -1;
144 return RTErrConvertFromErrno(rc);
145 }
146
147 /*
148 * We're done.
149 */
150 NOREF(cbReserve);
151 return VINF_SUCCESS;
152}
153
154
155int suplibOsTerm(void)
156{
157 /*
158 * Check if we're initited at all.
159 */
160 if (g_hDevice >= 0)
161 {
162 if (close(g_hDevice))
163 AssertFailed();
164 g_hDevice = -1;
165 }
166
167 /*
168 * Close the connection to the IOService and destroy the connection handle.
169 */
170 if (g_Connection)
171 {
172 kern_return_t kr = IOServiceClose(g_Connection);
173 if (kr != kIOReturnSuccess)
174 {
175 LogRel(("Warning: IOServiceClose(%p) returned %d\n", g_Connection, kr));
176 AssertFailed();
177 }
178 g_Connection = NULL;
179 }
180
181 return VINF_SUCCESS;
182}
183
184
185int suplibOsInstall(void)
186{
187 return VERR_NOT_IMPLEMENTED;
188}
189
190
191int suplibOsUninstall(void)
192{
193 return VERR_NOT_IMPLEMENTED;
194}
195
196
197int suplibOsIOCtl(uintptr_t uFunction, void *pvReq, size_t cbReq)
198{
199 AssertMsg(g_hDevice != -1, ("SUPLIB not initiated successfully!\n"));
200
201 if (RT_LIKELY(ioctl(g_hDevice, uFunction, pvReq) >= 0))
202 return VINF_SUCCESS;
203 return RTErrConvertFromErrno(errno);
204}
205
206
207int suplibOSIOCtlFast(uintptr_t uFunction)
208{
209 int rc = ioctl(g_hDevice, uFunction, NULL);
210 if (rc == -1)
211 rc = errno;
212 return rc;
213}
214
215
216int suplibOsPageAlloc(size_t cPages, void **ppvPages)
217{
218 *ppvPages = valloc(cPages << PAGE_SHIFT);
219 if (*ppvPages)
220 {
221 memset(*ppvPages, 0, cPages << PAGE_SHIFT);
222 return VINF_SUCCESS;
223 }
224 return RTErrConvertFromErrno(errno);
225}
226
227
228int suplibOsPageFree(void *pvPages, size_t /* cPages */)
229{
230 free(pvPages);
231 return VINF_SUCCESS;
232}
233
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette