VirtualBox

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

Last change on this file was 101720, checked in by vboxsync, 6 months ago

Additions: Guest Library: Fix valgrind warnings about uninitialized memory usage, bugref:10194.

  • Property svn:eol-style set to native
  • Property svn:keyword set to Id
  • Property svn:keywords set to Id Revision
File size: 15.1 KB
Line 
1/* $Id: VBoxGuestR3Lib.cpp 101720 2023-11-02 15:21: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.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 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
303 RT_ZERO(VerInfo);
304
305 VBGLREQHDR_INIT(&VerInfo.Hdr, DRIVER_VERSION_INFO);
306 VerInfo.u.In.uMinVersion = VBGL_IOC_VERSION & UINT32_C(0xffff0000);
307 VerInfo.u.In.uReqVersion = VBGL_IOC_VERSION;
308 VerInfo.u.In.uReserved1 = 0;
309 VerInfo.u.In.uReserved2 = 0;
310 rc2 = vbglR3DoIOCtl(VBGL_IOCTL_DRIVER_VERSION_INFO, &VerInfo.Hdr, sizeof(VerInfo));
311#ifndef VBOX_VBGLR3_XSERVER
312 AssertRC(rc2); /* otherwise ignored for now*/
313#endif
314 }
315
316
317#ifndef VBOX_VBGLR3_XSERVER
318 /*
319 * Create release logger
320 */
321 PRTLOGGER pReleaseLogger;
322 static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
323 rc2 = RTLogCreate(&pReleaseLogger, 0, "all", "VBOX_RELEASE_LOG",
324 RT_ELEMENTS(s_apszGroups), &s_apszGroups[0], RTLOGDEST_USER, NULL);
325 /* This may legitimately fail if we are using the mini-runtime. */
326 if (RT_SUCCESS(rc2))
327 RTLogRelSetDefaultInstance(pReleaseLogger);
328#endif
329
330 return VINF_SUCCESS;
331}
332
333
334/**
335 * Open the VBox R3 Guest Library. This should be called by system daemons
336 * and processes.
337 */
338VBGLR3DECL(int) VbglR3Init(void)
339{
340 return vbglR3Init(VBOXGUEST_DEVICE_NAME);
341}
342
343
344/**
345 * Open the VBox R3 Guest Library. Equivalent to VbglR3Init, but for user
346 * session processes.
347 */
348VBGLR3DECL(int) VbglR3InitUser(void)
349{
350 return vbglR3Init(VBOXGUEST_USER_DEVICE_NAME);
351}
352
353
354VBGLR3DECL(void) VbglR3Term(void)
355{
356 /*
357 * Decrement the reference count and see if we're the last one out.
358 */
359 uint32_t cInits = ASMAtomicDecU32(&g_cInits);
360 if (cInits > 0)
361 return;
362#if !defined(VBOX_VBGLR3_XSERVER)
363 AssertReturnVoid(!cInits);
364
365# if defined(RT_OS_WINDOWS)
366 HANDLE hFile = g_hFile;
367 g_hFile = INVALID_HANDLE_VALUE;
368 AssertReturnVoid(hFile != INVALID_HANDLE_VALUE);
369 BOOL fRc = CloseHandle(hFile);
370 Assert(fRc); NOREF(fRc);
371
372# elif defined(RT_OS_OS2)
373 RTFILE File = g_File;
374 g_File = NIL_RTFILE;
375 AssertReturnVoid(File != NIL_RTFILE);
376 APIRET rc = DosClose((uintptr_t)File);
377 AssertMsg(!rc, ("%ld\n", rc));
378
379#elif defined(RT_OS_DARWIN)
380 io_connect_t uConnection = g_uConnection;
381 RTFILE hFile = g_File;
382 g_uConnection = 0;
383 g_File = NIL_RTFILE;
384 kern_return_t kr = IOServiceClose(uConnection);
385 AssertMsg(kr == kIOReturnSuccess, ("%#x (%d)\n", kr, kr)); NOREF(kr);
386 int rc = RTFileClose(hFile);
387 AssertRC(rc);
388
389# else /* The IPRT case. */
390 RTFILE File = g_File;
391 g_File = NIL_RTFILE;
392 AssertReturnVoid(File != NIL_RTFILE);
393 int rc = RTFileClose(File);
394 AssertRC(rc);
395# endif
396
397#else /* VBOX_VBGLR3_XSERVER */
398 int File = g_File;
399 g_File = -1;
400 if (File == -1)
401 return;
402 xf86close(File);
403#endif /* VBOX_VBGLR3_XSERVER */
404}
405
406
407/**
408 * Internal wrapper around various OS specific ioctl implementations.
409 *
410 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
411 * an failure returned by the OS specific ioctl APIs.
412 *
413 * @param uFunction The requested function.
414 * @param pHdr The input and output request buffer.
415 * @param cbReq The size of the request buffer.
416 */
417int vbglR3DoIOCtlRaw(uintptr_t uFunction, PVBGLREQHDR pHdr, size_t cbReq)
418{
419 Assert(cbReq == RT_MAX(pHdr->cbIn, pHdr->cbOut)); RT_NOREF1(cbReq);
420 Assert(pHdr->cbOut != 0);
421
422#if defined(RT_OS_WINDOWS)
423# if 0 /*def USE_NT_DEVICE_IO_CONTROL_FILE*/
424 IO_STATUS_BLOCK Ios;
425 Ios.Status = -1;
426 Ios.Information = 0;
427 NTSTATUS rcNt = NtDeviceIoControlFile(g_hFile, NULL /*hEvent*/, NULL /*pfnApc*/, NULL /*pvApcCtx*/, &Ios,
428 (ULONG)uFunction,
429 pHdr /*pvInput */, pHdr->cbIn /* cbInput */,
430 pHdr /*pvOutput*/, pHdr->cbOut /* cbOutput */);
431 if (NT_SUCCESS(rcNt))
432 {
433 if (NT_SUCCESS(Ios.Status))
434 return VINF_SUCCESS;
435 rcNt = Ios.Status;
436 }
437 return RTErrConvertFromNtStatus(rcNt);
438
439# else
440 DWORD cbReturned = (ULONG)pHdr->cbOut;
441 if (DeviceIoControl(g_hFile, uFunction, pHdr, pHdr->cbIn, pHdr, cbReturned, &cbReturned, NULL))
442 return 0;
443 return RTErrConvertFromWin32(GetLastError());
444# endif
445
446#elif defined(RT_OS_OS2)
447 ULONG cbOS2Parm = cbReq;
448 APIRET rc = DosDevIOCtl((uintptr_t)g_File, VBGL_IOCTL_CATEGORY, uFunction, pHdr, cbReq, &cbOS2Parm, NULL, 0, NULL);
449 if (RT_LIKELY(rc == NO_ERROR))
450 return VINF_SUCCESS;
451 return RTErrConvertFromOS2(rc);
452
453#elif defined(VBOX_VBGLR3_XSERVER)
454 if (g_File != -1)
455 {
456 if (RT_LIKELY(xf86ioctl((int)g_File, uFunction, pHdr) >= 0))
457 return VINF_SUCCESS;
458 return VERR_FILE_IO_ERROR;
459 }
460 return VERR_INVALID_HANDLE;
461
462#else
463 if (g_File != NIL_RTFILE)
464 {
465 if (RT_LIKELY(ioctl((int)(intptr_t)g_File, uFunction, pHdr) >= 0))
466 return VINF_SUCCESS;
467 return RTErrConvertFromErrno(errno);
468 }
469 return VERR_INVALID_HANDLE;
470#endif
471}
472
473
474/**
475 * Internal wrapper around various OS specific ioctl implementations, that
476 * returns the status from the header.
477 *
478 * @returns VBox status code as returned by VBoxGuestCommonIOCtl, or
479 * an failure returned by the OS specific ioctl APIs.
480 *
481 * @param uFunction The requested function.
482 * @param pHdr The input and output request buffer.
483 * @param cbReq The size of the request buffer.
484 */
485int vbglR3DoIOCtl(uintptr_t uFunction, PVBGLREQHDR pHdr, size_t cbReq)
486{
487 int rc = vbglR3DoIOCtlRaw(uFunction, pHdr, cbReq);
488 if (RT_SUCCESS(rc))
489 rc = pHdr->rc;
490 return rc;
491}
492
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use