VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/solaris/SUPLib-solaris.cpp

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 7.2 KB
Line 
1/* $Id: SUPLib-solaris.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Solaris specific parts.
4 */
5
6/*
7 * Copyright (C) 2006-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#define LOG_GROUP LOG_GROUP_SUP
42#ifdef IN_SUP_HARDENED_R3
43# undef DEBUG /* Warning: disables RT_STRICT */
44# ifndef LOG_DISABLED
45# define LOG_DISABLED
46# endif
47# define RTLOG_REL_DISABLED
48# include <iprt/log.h>
49#endif
50
51#include <VBox/types.h>
52#include <VBox/sup.h>
53#include <VBox/param.h>
54#include <VBox/err.h>
55#include <VBox/log.h>
56#include <iprt/path.h>
57#include <iprt/assert.h>
58#include <iprt/mem.h>
59#include <iprt/err.h>
60#include <iprt/string.h>
61#include "../SUPLibInternal.h"
62#include "../SUPDrvIOC.h"
63
64#include <sys/fcntl.h>
65#include <sys/ioctl.h>
66#include <sys/zone.h>
67
68#include <fcntl.h>
69#include <errno.h>
70#include <unistd.h>
71#include <sys/mman.h>
72#include <stdlib.h>
73#include <stdio.h>
74#include <zone.h>
75
76
77/*********************************************************************************************************************************
78* Defined Constants And Macros *
79*********************************************************************************************************************************/
80/** Solaris device link - system. */
81#define DEVICE_NAME_SYS "/devices/pseudo/vboxdrv@0:vboxdrv"
82/** Solaris device link - user. */
83#define DEVICE_NAME_USR "/devices/pseudo/vboxdrv@0:vboxdrvu"
84/** Solaris device link - system (non-global zone). */
85#define DEVICE_NAME_SYS_ZONE "/dev/vboxdrv"
86/** Solaris device link - user (non-global zone). */
87#define DEVICE_NAME_USR_ZONE "/dev/vboxdrvu"
88
89
90
91DECLHIDDEN(int) suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, uint32_t fFlags, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
92{
93 /*
94 * Nothing to do if pre-inited.
95 */
96 if (fPreInited)
97 return VINF_SUCCESS;
98
99 /*
100 * Open dummy files to preallocate file descriptors, see @bugref{4650}.
101 */
102 for (int i = 0; i < SUPLIB_FLT_DUMMYFILES; i++)
103 {
104 pThis->ahDummy[i] = -1;
105 int hDummy = open("/dev/null", O_RDWR, 0);
106 if (hDummy >= 0)
107 {
108 if (fcntl(hDummy, F_SETFD, FD_CLOEXEC) == 0)
109 pThis->ahDummy[i] = hDummy;
110 else
111 {
112 close(hDummy);
113 LogRel(("Failed to set close on exec [%d] /dev/null! errno=%d\n", i, errno));
114 }
115 }
116 else
117 LogRel(("Failed to open[%d] /dev/null! errno=%d\n", i, errno));
118 }
119
120 /*
121 * Try to open the device.
122 */
123 const char *pszDeviceNm;
124 if (getzoneid() == GLOBAL_ZONEID)
125 pszDeviceNm = fFlags & SUPR3INIT_F_UNRESTRICTED ? DEVICE_NAME_SYS : DEVICE_NAME_USR;
126 else
127 pszDeviceNm = fFlags & SUPR3INIT_F_UNRESTRICTED ? DEVICE_NAME_SYS_ZONE : DEVICE_NAME_USR_ZONE;
128 int hDevice = open(pszDeviceNm, O_RDWR, 0);
129 if (hDevice < 0)
130 {
131 int rc;
132 switch (errno)
133 {
134 case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
135 case EPERM:
136 case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
137 case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
138 default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
139 }
140 LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", pszDeviceNm, errno, rc));
141 return rc;
142 }
143
144 /*
145 * Mark the file handle close on exec.
146 */
147 if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
148 {
149#ifdef IN_SUP_HARDENED_R3
150 int rc = VERR_INTERNAL_ERROR;
151#else
152 int err = errno;
153 int rc = RTErrConvertFromErrno(err);
154 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
155#endif
156 close(hDevice);
157 return rc;
158 }
159
160 pThis->hDevice = hDevice;
161 pThis->fUnrestricted = RT_BOOL(fFlags & SUPR3INIT_F_UNRESTRICTED);
162 return VINF_SUCCESS;
163}
164
165
166DECLHIDDEN(int) suplibOsTerm(PSUPLIBDATA pThis)
167{
168 /*
169 * Close the dummy files first.
170 */
171 for (int i = 0; i < SUPLIB_FLT_DUMMYFILES; i++)
172 {
173 if (pThis->ahDummy[i] != -1)
174 {
175 close(pThis->ahDummy[i]);
176 pThis->ahDummy[i] = -1;
177 }
178 }
179
180 /*
181 * Check if we're initialized
182 */
183 if (pThis->hDevice != (intptr_t)NIL_RTFILE)
184 {
185 if (close(pThis->hDevice))
186 AssertFailed();
187 pThis->hDevice = (intptr_t)NIL_RTFILE;
188 }
189
190 return VINF_SUCCESS;
191}
192
193
194#ifndef IN_SUP_HARDENED_R3
195
196DECLHIDDEN(int) suplibOsInstall(void)
197{
198 return VERR_NOT_IMPLEMENTED;
199}
200
201DECLHIDDEN(int) suplibOsUninstall(void)
202{
203 return VERR_NOT_IMPLEMENTED;
204}
205
206
207DECLHIDDEN(int) suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
208{
209 if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
210 return VINF_SUCCESS;
211 return RTErrConvertFromErrno(errno);
212}
213
214
215DECLHIDDEN(int) suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
216{
217 int rc = ioctl(pThis->hDevice, uFunction, idCpu);
218 if (rc == -1)
219 rc = errno;
220 return rc;
221}
222
223
224DECLHIDDEN(int) suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, uint32_t fFlags, void **ppvPages)
225{
226 RT_NOREF(pThis, fFlags);
227 *ppvPages = mmap(NULL, cPages * PAGE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE,
228 MAP_PRIVATE | MAP_ANON, -1, 0);
229 if (*ppvPages != (void *)-1)
230 return VINF_SUCCESS;
231 if (errno == EAGAIN)
232 return VERR_NO_MEMORY;
233 return RTErrConvertFromErrno(errno);
234}
235
236
237DECLHIDDEN(int) suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t cPages)
238{
239 NOREF(pThis);
240 munmap(pvPages, cPages * PAGE_SIZE);
241 return VINF_SUCCESS;
242}
243
244#endif /* !IN_SUP_HARDENED_R3 */
245
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use