VirtualBox

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

Last change on this file since 97929 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id Revision
File size: 14.8 KB
Line 
1/* $Id: VBoxGuestR3Lib.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * VBoxGuestR3Lib - Ring-3 Support Library for VirtualBox guest additions, Core.
4 */
5
6/*
7 * Copyright (C) 2007-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.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 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
235 if (kr != kIOReturnSuccess)
236 {
237 LogRel(("IOMasterPort -> %d\n", kr));
238 return VERR_GENERAL_FAILURE;
239 }
240
241 CFDictionaryRef ClassToMatch = IOServiceMatching("org_virtualbox_VBoxGuest");
242 if (!ClassToMatch)
243 {
244 LogRel(("IOServiceMatching(\"org_virtualbox_VBoxGuest\") failed.\n"));
245 return VERR_GENERAL_FAILURE;
246 }
247
248 io_service_t ServiceObject = IOServiceGetMatchingService(kIOMasterPortDefault, ClassToMatch);
249 if (!ServiceObject)
250 {
251 LogRel(("IOServiceGetMatchingService returned NULL\n"));
252 return VERR_NOT_FOUND;
253 }
254
255 io_connect_t uConnection;
256 kr = IOServiceOpen(ServiceObject, mach_task_self(), VBOXGUEST_DARWIN_IOSERVICE_COOKIE, &uConnection);
257 IOObjectRelease(ServiceObject);
258 if (kr != kIOReturnSuccess)
259 {
260 LogRel(("IOServiceOpen returned %d. Driver open failed.\n", kr));
261 return VERR_OPEN_FAILED;
262 }
263
264 /* Regular unix FD. */
265 RTFILE hFile;
266 int rc = RTFileOpen(&hFile, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
267 if (RT_FAILURE(rc))
268 {
269 LogRel(("RTFileOpen(%s) returned %Rrc. Driver open failed.\n", pszDeviceName, rc));
270 IOServiceClose(uConnection);
271 return rc;
272 }
273 g_File = hFile;
274 g_uConnection = uConnection;
275
276#elif defined(VBOX_VBGLR3_XSERVER)
277 int File = xf86open(pszDeviceName, XF86_O_RDWR);
278 if (File == -1)
279 return VERR_OPEN_FAILED;
280 g_File = File;
281
282#else
283
284 /* The default implementation. (linux, solaris, freebsd, netbsd, haiku) */
285 RTFILE File;
286 int rc = RTFileOpen(&File, pszDeviceName, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
287 if (RT_FAILURE(rc))
288 return rc;
289 g_File = File;
290
291#endif
292
293 /*
294 * Adjust the I/O control interface version.
295 */
296 {
297 VBGLIOCDRIVERVERSIONINFO VerInfo;
298 VBGLREQHDR_INIT(&VerInfo.Hdr, DRIVER_VERSION_INFO);
299 VerInfo.u.In.uMinVersion = VBGL_IOC_VERSION & UINT32_C(0xffff0000);
300 VerInfo.u.In.uReqVersion = VBGL_IOC_VERSION;
301 VerInfo.u.In.uReserved1 = 0;
302 VerInfo.u.In.uReserved2 = 0;
303 rc2 = vbglR3DoIOCtl(VBGL_IOCTL_DRIVER_VERSION_INFO, &VerInfo.Hdr, sizeof(VerInfo));
304#ifndef VBOX_VBGLR3_XSERVER
305 AssertRC(rc2); /* otherwise ignored for now*/
306#endif
307 }
308
309
310#ifndef VBOX_VBGLR3_XSERVER
311 /*
312 * Create release logger
313 */
314 PRTLOGGER pReleaseLogger;
315 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
316 rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
317 RT_ELEMENTS(s_apszGroups), &s_apszGroups[0], RTLOGDEST_USER, NULL);
318 /* This may legitimately fail if we are using the mini-runtime. */
319 if (RT_SUCCESS(rc2))
320 RTLogRelSetDefaultInstance(pReleaseLogger);
321#endif
322
323 return VINF_SUCCESS;
324}
325
326
327/**
328 * Open the VBox R3 Guest Library. This should be called by system daemons
329 * and processes.
330 */
331VBGLR3DECL(int) VbglR3Init(void)
332{
333 return vbglR3Init(VBOXGUEST_DEVICE_NAME);
334}
335
336
337/**
338 * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
339 * session processes.
340 */
341VBGLR3DECL(int) VbglR3InitUser(void)
342{
343 return vbglR3Init(VBOXGUEST_USER_DEVICE_NAME);
344}
345
346
347VBGLR3DECL(void) VbglR3Term(void)
348{
349 /*
350 * Decrement the reference count and see if we're the last one out.
351 */
352 uint32_t cInits = ASMAtomicDecU32(&g_cInits);
353 if (cInits > 0)
354 return;
355#if !defined(VBOX_VBGLR3_XSERVER)
356 AssertReturnVoid(!cInits);
357
358# if defined(RT_OS_WINDOWS)
359 HANDLE hFile = g_hFile;
360 g_hFile = INVALID_HANDLE_VALUE;
361 AssertReturnVoid(hFile != INVALID_HANDLE_VALUE);
362 BOOL fRc = CloseHandle(hFile);
363 Assert(fRc); NOREF(fRc);
364
365# elif defined(RT_OS_OS2)
366 RTFILE File = g_File;
367 g_File = NIL_RTFILE;
368 AssertReturnVoid(File != NIL_RTFILE);
369 APIRET rc = DosClose((uintptr_t)File);
370 AssertMsg(!rc, ("%ld\n", rc));
371
372#elif defined(RT_OS_DARWIN)
373 io_connect_t uConnection = g_uConnection;
374 RTFILE hFile = g_File;
375 g_uConnection = 0;
376 g_File = NIL_RTFILE;
377 kern_return_t kr = IOServiceClose(uConnection);
378 AssertMsg(kr == kIOReturnSuccess, ("%#x (%d)\n", kr, kr)); NOREF(kr);
379 int rc = RTFileClose(hFile);
380 AssertRC(rc);
381
382# else /* The IPRT case. */
383 RTFILE File = g_File;
384 g_File = NIL_RTFILE;
385 AssertReturnVoid(File != NIL_RTFILE);
386 int rc = RTFileClose(File);
387 AssertRC(rc);
388# endif
389
390#else /* VBOX_VBGLR3_XSERVER */
391 int File = g_File;
392 g_File = -1;
393 if (File == -1)
394 return;
395 xf86close(File);
396#endif /* VBOX_VBGLR3_XSERVER */
397}
398
399
400/**
401 * Internal wrapper around various OS specific ioctl implementations.
402 *
403 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
404 * an failure returned by the OS specific ioctl APIs.
405 *
406 * @param uFunction The requested function.
407 * @param pHdr The input and output request buffer.
408 * @param cbReq The size of the request buffer.
409 */
410int vbglR3DoIOCtlRaw(uintptr_t uFunction, PVBGLREQHDR pHdr, size_t cbReq)
411{
412 Assert(cbReq == RT_MAX(pHdr->cbIn, pHdr->cbOut)); RT_NOREF1(cbReq);
413 Assert(pHdr->cbOut != 0);
414
415#if defined(RT_OS_WINDOWS)
416# if 0 /*def USE_NT_DEVICE_IO_CONTROL_FILE*/
417 IO_STATUS_BLOCK Ios;
418 Ios.Status = -1;
419 Ios.Information = 0;
420 NTSTATUS rcNt = NtDeviceIoControlFile(g_hFile, NULL /*hEvent*/, NULL /*pfnApc*/, NULL /*pvApcCtx*/, &Ios,
421 (ULONG)uFunction,
422 pHdr /*pvInput */, pHdr->cbIn /* cbInput */,
423 pHdr /*pvOutput*/, pHdr->cbOut /* cbOutput */);
424 if (NT_SUCCESS(rcNt))
425 {
426 if (NT_SUCCESS(Ios.Status))
427 return VINF_SUCCESS;
428 rcNt = Ios.Status;
429 }
430 return RTErrConvertFromNtStatus(rcNt);
431
432# else
433 DWORD cbReturned = (ULONG)pHdr->cbOut;
434 if (DeviceIoControl(g_hFile, uFunction, pHdr, pHdr->cbIn, pHdr, cbReturned, &cbReturned, NULL))
435 return 0;
436 return RTErrConvertFromWin32(GetLastError());
437# endif
438
439#elif defined(RT_OS_OS2)
440 ULONG cbOS2Parm = cbReq;
441 APIRET rc = DosDevIOCtl((uintptr_t)g_File, VBGL_IOCTL_CATEGORY, uFunction, pHdr, cbReq, &cbOS2Parm, NULL, 0, NULL);
442 if (RT_LIKELY(rc == NO_ERROR))
443 return VINF_SUCCESS;
444 return RTErrConvertFromOS2(rc);
445
446#elif defined(VBOX_VBGLR3_XSERVER)
447 if (g_File != -1)
448 {
449 if (RT_LIKELY(xf86ioctl((int)g_File, uFunction, pHdr) >= 0))
450 return VINF_SUCCESS;
451 return VERR_FILE_IO_ERROR;
452 }
453 return VERR_INVALID_HANDLE;
454
455#else
456 if (g_File != NIL_RTFILE)
457 {
458 if (RT_LIKELY(ioctl((int)(intptr_t)g_File, uFunction, pHdr) >= 0))
459 return VINF_SUCCESS;
460 return RTErrConvertFromErrno(errno);
461 }
462 return VERR_INVALID_HANDLE;
463#endif
464}
465
466
467/**
468 * Internal wrapper around various OS specific ioctl implementations, that
469 * returns the status from the header.
470 *
471 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
472 * an failure returned by the OS specific ioctl APIs.
473 *
474 * @param uFunction The requested function.
475 * @param pHdr The input and output request buffer.
476 * @param cbReq The size of the request buffer.
477 */
478int vbglR3DoIOCtl(uintptr_t uFunction, PVBGLREQHDR pHdr, size_t cbReq)
479{
480 int rc = vbglR3DoIOCtlRaw(uFunction, pHdr, cbReq);
481 if (RT_SUCCESS(rc))
482 rc = pHdr->rc;
483 return rc;
484}
485
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