VirtualBox

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

Last change on this file since 82968 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.7 KB
Line 
1/* $Id: SUPLib-solaris.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * VirtualBox Support Library - Solaris specific parts.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP LOG_GROUP_SUP
32#ifdef IN_SUP_HARDENED_R3
33# undef DEBUG /* Warning: disables RT_STRICT */
34# ifndef LOG_DISABLED
35# define LOG_DISABLED
36# endif
37# define RTLOG_REL_DISABLED
38# include <iprt/log.h>
39#endif
40
41#include <VBox/types.h>
42#include <VBox/sup.h>
43#include <VBox/param.h>
44#include <VBox/err.h>
45#include <VBox/log.h>
46#include <iprt/path.h>
47#include <iprt/assert.h>
48#include <iprt/mem.h>
49#include <iprt/err.h>
50#include <iprt/string.h>
51#include "../SUPLibInternal.h"
52#include "../SUPDrvIOC.h"
53
54#include <sys/fcntl.h>
55#include <sys/ioctl.h>
56#include <sys/zone.h>
57
58#include <fcntl.h>
59#include <errno.h>
60#include <unistd.h>
61#include <sys/mman.h>
62#include <stdlib.h>
63#include <stdio.h>
64#include <zone.h>
65
66
67/*********************************************************************************************************************************
68* Defined Constants And Macros *
69*********************************************************************************************************************************/
70/** Solaris device link - system. */
71#define DEVICE_NAME_SYS "/devices/pseudo/vboxdrv@0:vboxdrv"
72/** Solaris device link - user. */
73#define DEVICE_NAME_USR "/devices/pseudo/vboxdrv@0:vboxdrvu"
74/** Solaris device link - system (non-global zone). */
75#define DEVICE_NAME_SYS_ZONE "/dev/vboxdrv"
76/** Solaris device link - user (non-global zone). */
77#define DEVICE_NAME_USR_ZONE "/dev/vboxdrvu"
78
79
80
81int suplibOsInit(PSUPLIBDATA pThis, bool fPreInited, bool fUnrestricted, SUPINITOP *penmWhat, PRTERRINFO pErrInfo)
82{
83 /*
84 * Nothing to do if pre-inited.
85 */
86 if (fPreInited)
87 return VINF_SUCCESS;
88
89 /*
90 * Open dummy files to preallocate file descriptors, see @bugref{4650}.
91 */
92 for (int i = 0; i < SUPLIB_FLT_DUMMYFILES; i++)
93 {
94 pThis->ahDummy[i] = -1;
95 int hDummy = open("/dev/null", O_RDWR, 0);
96 if (hDummy >= 0)
97 {
98 if (fcntl(hDummy, F_SETFD, FD_CLOEXEC) == 0)
99 pThis->ahDummy[i] = hDummy;
100 else
101 {
102 close(hDummy);
103 LogRel(("Failed to set close on exec [%d] /dev/null! errno=%d\n", i, errno));
104 }
105 }
106 else
107 LogRel(("Failed to open[%d] /dev/null! errno=%d\n", i, errno));
108 }
109
110 /*
111 * Try to open the device.
112 */
113 const char *pszDeviceNm;
114 if (getzoneid() == GLOBAL_ZONEID)
115 pszDeviceNm = fUnrestricted ? DEVICE_NAME_SYS : DEVICE_NAME_USR;
116 else
117 pszDeviceNm = fUnrestricted ? DEVICE_NAME_SYS_ZONE : DEVICE_NAME_USR_ZONE;
118 int hDevice = open(pszDeviceNm, O_RDWR, 0);
119 if (hDevice < 0)
120 {
121 int rc;
122 switch (errno)
123 {
124 case ENODEV: rc = VERR_VM_DRIVER_LOAD_ERROR; break;
125 case EPERM:
126 case EACCES: rc = VERR_VM_DRIVER_NOT_ACCESSIBLE; break;
127 case ENOENT: rc = VERR_VM_DRIVER_NOT_INSTALLED; break;
128 default: rc = VERR_VM_DRIVER_OPEN_ERROR; break;
129 }
130 LogRel(("Failed to open \"%s\", errno=%d, rc=%Rrc\n", pszDeviceNm, errno, rc));
131 return rc;
132 }
133
134 /*
135 * Mark the file handle close on exec.
136 */
137 if (fcntl(hDevice, F_SETFD, FD_CLOEXEC) != 0)
138 {
139#ifdef IN_SUP_HARDENED_R3
140 int rc = VERR_INTERNAL_ERROR;
141#else
142 int err = errno;
143 int rc = RTErrConvertFromErrno(err);
144 LogRel(("suplibOSInit: setting FD_CLOEXEC failed, errno=%d (%Rrc)\n", err, rc));
145#endif
146 close(hDevice);
147 return rc;
148 }
149
150 pThis->hDevice = hDevice;
151 pThis->fUnrestricted = fUnrestricted;
152 return VINF_SUCCESS;
153}
154
155
156int suplibOsTerm(PSUPLIBDATA pThis)
157{
158 /*
159 * Close the dummy files first.
160 */
161 for (int i = 0; i < SUPLIB_FLT_DUMMYFILES; i++)
162 {
163 if (pThis->ahDummy[i] != -1)
164 {
165 close(pThis->ahDummy[i]);
166 pThis->ahDummy[i] = -1;
167 }
168 }
169
170 /*
171 * Check if we're initialized
172 */
173 if (pThis->hDevice != (intptr_t)NIL_RTFILE)
174 {
175 if (close(pThis->hDevice))
176 AssertFailed();
177 pThis->hDevice = (intptr_t)NIL_RTFILE;
178 }
179
180 return VINF_SUCCESS;
181}
182
183
184#ifndef IN_SUP_HARDENED_R3
185
186int suplibOsInstall(void)
187{
188 return VERR_NOT_IMPLEMENTED;
189}
190
191int suplibOsUninstall(void)
192{
193 return VERR_NOT_IMPLEMENTED;
194}
195
196
197int suplibOsIOCtl(PSUPLIBDATA pThis, uintptr_t uFunction, void *pvReq, size_t cbReq)
198{
199 if (RT_LIKELY(ioctl(pThis->hDevice, uFunction, pvReq) >= 0))
200 return VINF_SUCCESS;
201 return RTErrConvertFromErrno(errno);
202}
203
204
205int suplibOsIOCtlFast(PSUPLIBDATA pThis, uintptr_t uFunction, uintptr_t idCpu)
206{
207 int rc = ioctl(pThis->hDevice, uFunction, idCpu);
208 if (rc == -1)
209 rc = errno;
210 return rc;
211}
212
213
214int suplibOsPageAlloc(PSUPLIBDATA pThis, size_t cPages, void **ppvPages)
215{
216 NOREF(pThis);
217 *ppvPages = mmap(NULL, cPages * PAGE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE,
218 MAP_PRIVATE | MAP_ANON, -1, 0);
219 if (*ppvPages != (void *)-1)
220 return VINF_SUCCESS;
221 if (errno == EAGAIN)
222 return VERR_NO_MEMORY;
223 return RTErrConvertFromErrno(errno);
224}
225
226
227int suplibOsPageFree(PSUPLIBDATA pThis, void *pvPages, size_t cPages)
228{
229 NOREF(pThis);
230 munmap(pvPages, cPages * PAGE_SIZE);
231 return VINF_SUCCESS;
232}
233
234#endif /* !IN_SUP_HARDENED_R3 */
235
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