VirtualBox

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

Last change on this file since 47363 was 44528, checked in by vboxsync, 12 years ago

header (C) fixes

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