VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxGuest/lib/VBoxGuestR3Lib.cpp@ 100108

最後變更 在這個檔案從100108是 100108,由 vboxsync 提交於 22 月 前

*: Fix build issues when setting VBOX_WITH_WARNINGS_AS_ERRORS=1 on darwin.arm64 and make it a default, bugref:10469

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keyword 設為 Id
  • 屬性 svn:keywords 設為 Id Revision
檔案大小: 15.1 KB
 
1/* $Id: VBoxGuestR3Lib.cpp 100108 2023-06-07 20:05:13Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
4 */
5
6/*
7 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.alldomusa.eu.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#if defined(RT_OS_WINDOWS)
42# include <iprt/nt/nt-and-windows.h>
43
44#elif defined(RT_OS_OS2)
45# define INCL_BASE
46# define INCL_ERRORS
47# include <os2.h>
48
49#elif defined(RT_OS_DARWIN) \
50 || defined(RT_OS_FREEBSD) \
51 || defined(RT_OS_HAIKU) \
52 || defined(RT_OS_LINUX) \
53 || defined(RT_OS_NETBSD) \
54 || defined(RT_OS_SOLARIS)
55# include <sys/types.h>
56# include <sys/stat.h>
57# if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined(RT_OS_NETBSD)
58 /** @todo check this on solaris+freebsd as well. */
59# include <sys/ioctl.h>
60# endif
61# if defined(RT_OS_DARWIN)
62# include <mach/mach_port.h>
63# include <IOKit/IOKitLib.h>
64# endif
65# include <errno.h>
66# include <unistd.h>
67#endif
68
69#include <iprt/assert.h>
70#include <iprt/asm.h>
71#include <iprt/err.h>
72#include <iprt/file.h>
73#include <iprt/time.h>
74#include <iprt/string.h>
75#include <iprt/thread.h>
76#include <VBox/log.h>
77#include "VBoxGuestR3LibInternal.h"
78
79#ifdef VBOX_VBGLR3_XFREE86
80/* Rather than try to resolve all the header file conflicts, I will just
81 prototype what we need here. */
82# define XF86_O_RDWR 0x0002
83typedef void *pointer;
84extern "C" int xf86open(const char *, int, ...);
85extern "C" int xf86close(int);
86extern "C" int xf86ioctl(int, unsigned long, pointer);
87# define VBOX_VBGLR3_XSERVER
88#elif defined(VBOX_VBGLR3_XORG)
89# include <sys/stat.h>
90# include <fcntl.h>
91# include <unistd.h>
92# include <sys/ioctl.h>
93# define xf86open open
94# define xf86close close
95# define xf86ioctl ioctl
96# define XF86_O_RDWR O_RDWR
97# define VBOX_VBGLR3_XSERVER
98#endif
99
100
101/*********************************************************************************************************************************
102* Global Variables *
103*********************************************************************************************************************************/
104/** The VBoxGuest device handle. */
105#ifdef VBOX_VBGLR3_XSERVER
106static int g_File = -1;
107#elif defined(RT_OS_WINDOWS)
108static HANDLE g_hFile = INVALID_HANDLE_VALUE;
109#else
110static RTFILE g_File = NIL_RTFILE;
111#endif
112/** User counter.
113 * A counter of the number of times the library has been initialised, for use with
114 * X.org drivers, where the library may be shared by multiple independent modules
115 * inside a single process space.
116 */
117static uint32_t volatile g_cInits = 0;
118#ifdef RT_OS_DARWIN
119/** I/O Kit connection handle. */
120static io_connect_t g_uConnection = 0;
121#endif
122
123
124
125/**
126 * Implementation of VbglR3Init and VbglR3InitUser
127 */
128static int vbglR3Init(const char *pszDeviceName)
129{
130 int rc2;
131 uint32_t cInits = ASMAtomicIncU32(&g_cInits);
132 Assert(cInits > 0);
133 if (cInits > 1)
134 {
135 /*
136 * This will fail if two (or more) threads race each other calling VbglR3Init.
137 * However it will work fine for single threaded or otherwise serialized
138 * processed calling us more than once.
139 */
140#ifdef RT_OS_WINDOWS
141 if (g_hFile == INVALID_HANDLE_VALUE)
142#elif !defined (VBOX_VBGLR3_XSERVER)
143 if (g_File == NIL_RTFILE)
144#else
145 if (g_File == -1)
146#endif
147 return VERR_INTERNAL_ERROR;
148 return VINF_SUCCESS;
149 }
150#if defined(RT_OS_WINDOWS)
151 if (g_hFile != INVALID_HANDLE_VALUE)
152#elif !defined(VBOX_VBGLR3_XSERVER)
153 if (g_File != NIL_RTFILE)
154#else
155 if (g_File != -1)
156#endif
157 return VERR_INTERNAL_ERROR;
158
159#if defined(RT_OS_WINDOWS)
160 /*
161 * Have to use CreateFile here as we want to specify FILE_FLAG_OVERLAPPED
162 * and possible some other bits not available thru iprt/file.h.
163 */
164 HANDLE hFile = CreateFile(pszDeviceName,
165 GENERIC_READ | GENERIC_WRITE,
166 FILE_SHARE_READ | FILE_SHARE_WRITE,
167 NULL,
168 OPEN_EXISTING,
169 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
170 NULL);
171
172 if (hFile == INVALID_HANDLE_VALUE)
173 return VERR_OPEN_FAILED;
174 g_hFile = hFile;
175
176#elif defined(RT_OS_OS2)
177 /*
178 * We might wish to compile this with Watcom, so stick to
179 * the OS/2 APIs all the way. And in any case we have to use
180 * DosDevIOCtl for the requests, why not use Dos* for everything.
181 */
182 HFILE hf = NULLHANDLE;
183 ULONG ulAction = 0;
184 APIRET rc = DosOpen((PCSZ)pszDeviceName, &hf, &ulAction, 0, FILE_NORMAL,
185 OPEN_ACTION_OPEN_IF_EXISTS,
186 OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE | OPEN_ACCESS_READWRITE,
187 NULL);
188 if (rc)
189 return RTErrConvertFromOS2(rc);
190
191 if (hf < 16)
192 {
193 HFILE ahfs[16];
194 unsigned i;
195 for (i = 0; i < RT_ELEMENTS(ahfs); i++)
196 {
197 ahfs[i] = 0xffffffff;
198 rc = DosDupHandle(hf, &ahfs[i]);
199 if (rc)
200 break;
201 }
202
203 if (i-- > 1)
204 {
205 ULONG fulState = 0;
206 rc = DosQueryFHState(ahfs[i], &fulState);
207 if (!rc)
208 {
209 fulState |= OPEN_FLAGS_NOINHERIT;
210 fulState &= OPEN_FLAGS_WRITE_THROUGH | OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_CACHE | OPEN_FLAGS_NOINHERIT; /* Turn off non-participating bits. */
211 rc = DosSetFHState(ahfs[i], fulState);
212 }
213 if (!rc)
214 {
215 rc = DosClose(hf);
216 AssertMsg(!rc, ("%ld\n", rc));
217 hf = ahfs[i];
218 }
219 else
220 i++;
221 while (i-- > 0)
222 DosClose(ahfs[i]);
223 }
224 }
225 g_File = (RTFILE)hf;
226
227#elif defined(RT_OS_DARWIN)
228 /*
229 * Darwin is kind of special we need to engage the device via I/O first
230 * before we open it via the BSD device node.
231 */
232 /* IOKit */
233 mach_port_t MasterPort;
234 RT_GCC_NO_WARN_DEPRECATED_BEGIN
235 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort); /* Deprecated since 12.0. */
236 RT_GCC_NO_WARN_DEPRECATED_END
237 if (kr != kIOReturnSuccess)
238 {
239 LogRel(("IOMasterPort -> %d\n", kr));
240 return VERR_GENERAL_FAILURE;
241 }
242
243 CFDictionaryRef ClassToMatch = IOServiceMatching("org_virtualbox_VBoxGuest");
244 if (!ClassToMatch)
245 {
246 LogRel(("IOServiceMatching(\"org_virtualbox_VBoxGuest\") failed.\n"));
247 return VERR_GENERAL_FAILURE;
248 }
249
250 RT_GCC_NO_WARN_DEPRECATED_BEGIN
251 io_service_t ServiceObject = IOServiceGetMatchingService(kIOMasterPortDefault, ClassToMatch); /* kIOMasterPortDefault: Deprecated since 12.0. */
252 RT_GCC_NO_WARN_DEPRECATED_END
253 if (!ServiceObject)
254 {
255 LogRel(("IOServiceGetMatchingService returned NULL\n"));
256 return VERR_NOT_FOUND;
257 }
258
259 io_connect_t uConnection;
260 kr = IOServiceOpen(ServiceObject, mach_task_self(), VBOXGUEST_DARWIN_IOSERVICE_COOKIE, &uConnection);
261 IOObjectRelease(ServiceObject);
262 if (kr != kIOReturnSuccess)
263 {
264 LogRel(("IOServiceOpen returned %d. Driver open failed.\n", kr));
265 return VERR_OPEN_FAILED;
266 }
267
268 /* Regular unix FD. */
269 RTFILE hFile;
270 int rc = RTFileOpen(&hFile, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
271 if (RT_FAILURE(rc))
272 {
273 LogRel(("RTFileOpen(%s) returned %Rrc. Driver open failed.\n", pszDeviceName, rc));
274 IOServiceClose(uConnection);
275 return rc;
276 }
277 g_File = hFile;
278 g_uConnection = uConnection;
279
280#elif defined(VBOX_VBGLR3_XSERVER)
281 int File = xf86open(pszDeviceName, XF86_O_RDWR);
282 if (File == -1)
283 return VERR_OPEN_FAILED;
284 g_File = File;
285
286#else
287
288 /* The default implementation. (linux, solaris, freebsd, netbsd, haiku) */
289 RTFILE File;
290 int rc = RTFileOpen(&File, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
291 if (RT_FAILURE(rc))
292 return rc;
293 g_File = File;
294
295#endif
296
297 /*
298 * Adjust the I/O control interface version.
299 */
300 {
301 VBGLIOCDRIVERVERSIONINFO VerInfo;
302 VBGLREQHDR_INIT(&VerInfo.Hdr, DRIVER_VERSION_INFO);
303 VerInfo.u.In.uMinVersion = VBGL_IOC_VERSION & UINT32_C(0xffff0000);
304 VerInfo.u.In.uReqVersion = VBGL_IOC_VERSION;
305 VerInfo.u.In.uReserved1 = 0;
306 VerInfo.u.In.uReserved2 = 0;
307 rc2 = vbglR3DoIOCtl(VBGL_IOCTL_DRIVER_VERSION_INFO, &VerInfo.Hdr, sizeof(VerInfo));
308#ifndef VBOX_VBGLR3_XSERVER
309 AssertRC(rc2); /* otherwise ignored for now*/
310#endif
311 }
312
313
314#ifndef VBOX_VBGLR3_XSERVER
315 /*
316 * Create release logger
317 */
318 PRTLOGGER pReleaseLogger;
319 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
320 rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
321 RT_ELEMENTS(s_apszGroups), &s_apszGroups[0], RTLOGDEST_USER, NULL);
322 /* This may legitimately fail if we are using the mini-runtime. */
323 if (RT_SUCCESS(rc2))
324 RTLogRelSetDefaultInstance(pReleaseLogger);
325#endif
326
327 return VINF_SUCCESS;
328}
329
330
331/**
332 * Open the VBox R3 Guest Library. This should be called by system daemons
333 * and processes.
334 */
335VBGLR3DECL(int) VbglR3Init(void)
336{
337 return vbglR3Init(VBOXGUEST_DEVICE_NAME);
338}
339
340
341/**
342 * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
343 * session processes.
344 */
345VBGLR3DECL(int) VbglR3InitUser(void)
346{
347 return vbglR3Init(VBOXGUEST_USER_DEVICE_NAME);
348}
349
350
351VBGLR3DECL(void) VbglR3Term(void)
352{
353 /*
354 * Decrement the reference count and see if we're the last one out.
355 */
356 uint32_t cInits = ASMAtomicDecU32(&g_cInits);
357 if (cInits > 0)
358 return;
359#if !defined(VBOX_VBGLR3_XSERVER)
360 AssertReturnVoid(!cInits);
361
362# if defined(RT_OS_WINDOWS)
363 HANDLE hFile = g_hFile;
364 g_hFile = INVALID_HANDLE_VALUE;
365 AssertReturnVoid(hFile != INVALID_HANDLE_VALUE);
366 BOOL fRc = CloseHandle(hFile);
367 Assert(fRc); NOREF(fRc);
368
369# elif defined(RT_OS_OS2)
370 RTFILE File = g_File;
371 g_File = NIL_RTFILE;
372 AssertReturnVoid(File != NIL_RTFILE);
373 APIRET rc = DosClose((uintptr_t)File);
374 AssertMsg(!rc, ("%ld\n", rc));
375
376#elif defined(RT_OS_DARWIN)
377 io_connect_t uConnection = g_uConnection;
378 RTFILE hFile = g_File;
379 g_uConnection = 0;
380 g_File = NIL_RTFILE;
381 kern_return_t kr = IOServiceClose(uConnection);
382 AssertMsg(kr == kIOReturnSuccess, ("%#x (%d)\n", kr, kr)); NOREF(kr);
383 int rc = RTFileClose(hFile);
384 AssertRC(rc);
385
386# else /* The IPRT case. */
387 RTFILE File = g_File;
388 g_File = NIL_RTFILE;
389 AssertReturnVoid(File != NIL_RTFILE);
390 int rc = RTFileClose(File);
391 AssertRC(rc);
392# endif
393
394#else /* VBOX_VBGLR3_XSERVER */
395 int File = g_File;
396 g_File = -1;
397 if (File == -1)
398 return;
399 xf86close(File);
400#endif /* VBOX_VBGLR3_XSERVER */
401}
402
403
404/**
405 * Internal wrapper around various OS specific ioctl implementations.
406 *
407 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
408 * an failure returned by the OS specific ioctl APIs.
409 *
410 * @param uFunction The requested function.
411 * @param pHdr The input and output request buffer.
412 * @param cbReq The size of the request buffer.
413 */
414int vbglR3DoIOCtlRaw(uintptr_t uFunction, PVBGLREQHDR pHdr, size_t cbReq)
415{
416 Assert(cbReq == RT_MAX(pHdr->cbIn, pHdr->cbOut)); RT_NOREF1(cbReq);
417 Assert(pHdr->cbOut != 0);
418
419#if defined(RT_OS_WINDOWS)
420# if 0 /*def USE_NT_DEVICE_IO_CONTROL_FILE*/
421 IO_STATUS_BLOCK Ios;
422 Ios.Status = -1;
423 Ios.Information = 0;
424 NTSTATUS rcNt = NtDeviceIoControlFile(g_hFile, NULL /*hEvent*/, NULL /*pfnApc*/, NULL /*pvApcCtx*/, &Ios,
425 (ULONG)uFunction,
426 pHdr /*pvInput */, pHdr->cbIn /* cbInput */,
427 pHdr /*pvOutput*/, pHdr->cbOut /* cbOutput */);
428 if (NT_SUCCESS(rcNt))
429 {
430 if (NT_SUCCESS(Ios.Status))
431 return VINF_SUCCESS;
432 rcNt = Ios.Status;
433 }
434 return RTErrConvertFromNtStatus(rcNt);
435
436# else
437 DWORD cbReturned = (ULONG)pHdr->cbOut;
438 if (DeviceIoControl(g_hFile, uFunction, pHdr, pHdr->cbIn, pHdr, cbReturned, &cbReturned, NULL))
439 return 0;
440 return RTErrConvertFromWin32(GetLastError());
441# endif
442
443#elif defined(RT_OS_OS2)
444 ULONG cbOS2Parm = cbReq;
445 APIRET rc = DosDevIOCtl((uintptr_t)g_File, VBGL_IOCTL_CATEGORY, uFunction, pHdr, cbReq, &cbOS2Parm, NULL, 0, NULL);
446 if (RT_LIKELY(rc == NO_ERROR))
447 return VINF_SUCCESS;
448 return RTErrConvertFromOS2(rc);
449
450#elif defined(VBOX_VBGLR3_XSERVER)
451 if (g_File != -1)
452 {
453 if (RT_LIKELY(xf86ioctl((int)g_File, uFunction, pHdr) >= 0))
454 return VINF_SUCCESS;
455 return VERR_FILE_IO_ERROR;
456 }
457 return VERR_INVALID_HANDLE;
458
459#else
460 if (g_File != NIL_RTFILE)
461 {
462 if (RT_LIKELY(ioctl((int)(intptr_t)g_File, uFunction, pHdr) >= 0))
463 return VINF_SUCCESS;
464 return RTErrConvertFromErrno(errno);
465 }
466 return VERR_INVALID_HANDLE;
467#endif
468}
469
470
471/**
472 * Internal wrapper around various OS specific ioctl implementations, that
473 * returns the status from the header.
474 *
475 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
476 * an failure returned by the OS specific ioctl APIs.
477 *
478 * @param uFunction The requested function.
479 * @param pHdr The input and output request buffer.
480 * @param cbReq The size of the request buffer.
481 */
482int vbglR3DoIOCtl(uintptr_t uFunction, PVBGLREQHDR pHdr, size_t cbReq)
483{
484 int rc = vbglR3DoIOCtlRaw(uFunction, pHdr, cbReq);
485 if (RT_SUCCESS(rc))
486 rc = pHdr->rc;
487 return rc;
488}
489
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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