VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvHostDVD.cpp@ 11715

Last change on this file since 11715 was 11284, checked in by vboxsync, 16 years ago

Devices: %Vrc -> %Rrc (just preferred, not mandatory (yet))

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.9 KB
Line 
1/* $Id: DrvHostDVD.cpp 11284 2008-08-08 22:32:08Z vboxsync $ */
2/** @file
3 * DrvHostDVD - Host DVD block driver.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
18 * Clara, CA 95054 USA or visit http://www.sun.com if you need
19 * additional information or have any questions.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#define LOG_GROUP LOG_GROUP_DRV_HOST_DVD
27#define __STDC_LIMIT_MACROS
28#define __STDC_CONSTANT_MACROS
29#ifdef RT_OS_DARWIN
30# include <mach/mach.h>
31# include <Carbon/Carbon.h>
32# include <IOKit/IOKitLib.h>
33# include <IOKit/IOCFPlugIn.h>
34# include <IOKit/scsi-commands/SCSITaskLib.h>
35# include <IOKit/scsi-commands/SCSICommandOperationCodes.h>
36# include <IOKit/storage/IOStorageDeviceCharacteristics.h>
37# include <mach/mach_error.h>
38# define USE_MEDIA_POLLING
39
40#elif defined(RT_OS_L4)
41/* nothing (yet). */
42
43#elif defined RT_OS_LINUX
44# include <sys/ioctl.h>
45/* This is a hack to work around conflicts between these linux kernel headers
46 * and the GLIBC tcpip headers. They have different declarations of the 4
47 * standard byte order functions. */
48# define _LINUX_BYTEORDER_GENERIC_H
49/* This is another hack for not bothering with C++ unfriendly byteswap macros. */
50# define _LINUX_BYTEORDER_SWAB_H
51/* Those macros that are needed are defined in the header below */
52# include "swab.h"
53# include <linux/cdrom.h>
54# include <sys/fcntl.h>
55# include <errno.h>
56# include <limits.h>
57# define USE_MEDIA_POLLING
58
59#elif defined(RT_OS_SOLARIS)
60# include <stropts.h>
61# include <fcntl.h>
62# include <ctype.h>
63# include <errno.h>
64# include <pwd.h>
65# include <unistd.h>
66# include <syslog.h>
67# ifdef VBOX_WITH_SUID_WRAPPER
68# include <auth_attr.h>
69# endif
70# include <sys/dkio.h>
71# include <sys/sockio.h>
72# include <sys/scsi/scsi.h>
73# define USE_MEDIA_POLLING
74
75#elif defined(RT_OS_WINDOWS)
76# include <Windows.h>
77# include <winioctl.h>
78# include <ntddscsi.h>
79# undef USE_MEDIA_POLLING
80
81#else
82# error "Unsupported Platform."
83#endif
84
85#include <VBox/pdmdrv.h>
86#include <iprt/assert.h>
87#include <iprt/file.h>
88#include <iprt/string.h>
89#include <iprt/thread.h>
90#include <iprt/critsect.h>
91#include <VBox/scsi.h>
92
93#include "Builtins.h"
94#include "DrvHostBase.h"
95
96
97/* Forward declarations. */
98
99static DECLCALLBACK(int) drvHostDvdDoLock(PDRVHOSTBASE pThis, bool fLock);
100#ifdef VBOX_WITH_SUID_WRAPPER
101static int solarisCheckUserAuth();
102static int solarisEnterRootMode(uid_t *pEffUserID);
103static int solarisExitRootMode(uid_t *pEffUserID);
104#endif
105
106
107/** @copydoc PDMIMOUNT::pfnUnmount */
108static DECLCALLBACK(int) drvHostDvdUnmount(PPDMIMOUNT pInterface, bool fForce)
109{
110 PDRVHOSTBASE pThis = PDMIMOUNT_2_DRVHOSTBASE(pInterface);
111 RTCritSectEnter(&pThis->CritSect);
112
113 /*
114 * Validate state.
115 */
116 int rc = VINF_SUCCESS;
117 if (!pThis->fLocked || fForce)
118 {
119 /* Unlock drive if necessary. */
120 if (pThis->fLocked)
121 drvHostDvdDoLock(pThis, false);
122
123 /*
124 * Eject the disc.
125 */
126#ifdef RT_OS_DARWIN
127 uint8_t abCmd[16] =
128 {
129 SCSI_START_STOP_UNIT, 0, 0, 0, 2 /*eject+stop*/, 0,
130 0,0,0,0,0,0,0,0,0,0
131 };
132 rc = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMBLOCKTXDIR_NONE, NULL, NULL, NULL, 0, 0);
133
134#elif defined(RT_OS_LINUX)
135 rc = ioctl(pThis->FileDevice, CDROMEJECT, 0);
136 if (rc < 0)
137 {
138 if (errno == EBUSY)
139 rc = VERR_PDM_MEDIA_LOCKED;
140 else if (errno == ENOSYS)
141 rc = VERR_NOT_SUPPORTED;
142 else
143 rc = RTErrConvertFromErrno(errno);
144 }
145
146#elif defined(RT_OS_SOLARIS)
147 rc = ioctl(pThis->FileRawDevice, DKIOCEJECT, 0);
148 if (rc < 0)
149 {
150 if (errno == EBUSY)
151 rc = VERR_PDM_MEDIA_LOCKED;
152 else if (errno == ENOSYS || errno == ENOTSUP)
153 rc = VERR_NOT_SUPPORTED;
154 else if (errno == ENODEV)
155 rc = VERR_PDM_MEDIA_NOT_MOUNTED;
156 else
157 rc = RTErrConvertFromErrno(errno);
158 }
159
160#elif defined(RT_OS_WINDOWS)
161 RTFILE FileDevice = pThis->FileDevice;
162 if (FileDevice == NIL_RTFILE) /* obsolete crap */
163 rc = RTFileOpen(&FileDevice, pThis->pszDeviceOpen, RTFILE_O_READ | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
164 if (RT_SUCCESS(rc))
165 {
166 /* do ioctl */
167 DWORD cbReturned;
168 if (DeviceIoControl((HANDLE)FileDevice, IOCTL_STORAGE_EJECT_MEDIA,
169 NULL, 0,
170 NULL, 0, &cbReturned,
171 NULL))
172 rc = VINF_SUCCESS;
173 else
174 rc = RTErrConvertFromWin32(GetLastError());
175
176 /* clean up handle */
177 if (FileDevice != pThis->FileDevice)
178 RTFileClose(FileDevice);
179 }
180 else
181 AssertMsgFailed(("Failed to open '%s' for ejecting this tray.\n", rc));
182
183
184#else
185 AssertMsgFailed(("Eject is not implemented!\n"));
186 rc = VINF_SUCCESS;
187#endif
188
189 /*
190 * Media is no longer present.
191 */
192 DRVHostBaseMediaNotPresent(pThis); /** @todo This isn't thread safe! */
193 }
194 else
195 {
196 Log(("drvHostDvdUnmount: Locked\n"));
197 rc = VERR_PDM_MEDIA_LOCKED;
198 }
199
200 RTCritSectLeave(&pThis->CritSect);
201 LogFlow(("drvHostDvdUnmount: returns %Rrc\n", rc));
202 return rc;
203}
204
205
206/**
207 * Locks or unlocks the drive.
208 *
209 * @returns VBox status code.
210 * @param pThis The instance data.
211 * @param fLock True if the request is to lock the drive, false if to unlock.
212 */
213static DECLCALLBACK(int) drvHostDvdDoLock(PDRVHOSTBASE pThis, bool fLock)
214{
215#ifdef RT_OS_DARWIN
216 uint8_t abCmd[16] =
217 {
218 SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL, 0, 0, 0, fLock, 0,
219 0,0,0,0,0,0,0,0,0,0
220 };
221 int rc = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMBLOCKTXDIR_NONE, NULL, NULL, NULL, 0, 0);
222
223#elif defined(RT_OS_LINUX)
224 int rc = ioctl(pThis->FileDevice, CDROM_LOCKDOOR, (int)fLock);
225 if (rc < 0)
226 {
227 if (errno == EBUSY)
228 rc = VERR_ACCESS_DENIED;
229 else if (errno == EDRIVE_CANT_DO_THIS)
230 rc = VERR_NOT_SUPPORTED;
231 else
232 rc = RTErrConvertFromErrno(errno);
233 }
234
235#elif defined(RT_OS_SOLARIS)
236 int rc = ioctl(pThis->FileRawDevice, fLock ? DKIOCLOCK : DKIOCUNLOCK, 0);
237 if (rc < 0)
238 {
239 if (errno == EBUSY)
240 rc = VERR_ACCESS_DENIED;
241 else if (errno == ENOTSUP || errno == ENOSYS)
242 rc = VERR_NOT_SUPPORTED;
243 else
244 rc = RTErrConvertFromErrno(errno);
245 }
246
247#elif defined(RT_OS_WINDOWS)
248
249 PREVENT_MEDIA_REMOVAL PreventMediaRemoval = {fLock};
250 DWORD cbReturned;
251 int rc;
252 if (DeviceIoControl((HANDLE)pThis->FileDevice, IOCTL_STORAGE_MEDIA_REMOVAL,
253 &PreventMediaRemoval, sizeof(PreventMediaRemoval),
254 NULL, 0, &cbReturned,
255 NULL))
256 rc = VINF_SUCCESS;
257 else
258 /** @todo figure out the return codes for already locked. */
259 rc = RTErrConvertFromWin32(GetLastError());
260
261#else
262 AssertMsgFailed(("Lock/Unlock is not implemented!\n"));
263 int rc = VINF_SUCCESS;
264
265#endif
266
267 LogFlow(("drvHostDvdDoLock(, fLock=%RTbool): returns %Rrc\n", fLock, rc));
268 return rc;
269}
270
271
272
273#ifdef RT_OS_LINUX
274/**
275 * Get the media size.
276 *
277 * @returns VBox status code.
278 * @param pThis The instance data.
279 * @param pcb Where to store the size.
280 */
281static int drvHostDvdGetMediaSize(PDRVHOSTBASE pThis, uint64_t *pcb)
282{
283 /*
284 * Query the media size.
285 */
286 /* Clear the media-changed-since-last-call-thingy just to be on the safe side. */
287 ioctl(pThis->FileDevice, CDROM_MEDIA_CHANGED, CDSL_CURRENT);
288 return RTFileSeek(pThis->FileDevice, 0, RTFILE_SEEK_END, pcb);
289
290}
291#endif /* RT_OS_LINUX */
292
293
294#ifdef USE_MEDIA_POLLING
295/**
296 * Do media change polling.
297 */
298DECLCALLBACK(int) drvHostDvdPoll(PDRVHOSTBASE pThis)
299{
300 /*
301 * Poll for media change.
302 */
303#ifdef RT_OS_DARWIN
304 AssertReturn(pThis->ppScsiTaskDI, VERR_INTERNAL_ERROR);
305
306 /*
307 * Issue a TEST UNIT READY request.
308 */
309 bool fMediaChanged = false;
310 bool fMediaPresent = false;
311 uint8_t abCmd[16] = { SCSI_TEST_UNIT_READY, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
312 uint8_t abSense[32];
313 int rc2 = DRVHostBaseScsiCmd(pThis, abCmd, 6, PDMBLOCKTXDIR_NONE, NULL, NULL, abSense, sizeof(abSense), 0);
314 if (RT_SUCCESS(rc2))
315 fMediaPresent = true;
316 else if ( rc2 == VERR_UNRESOLVED_ERROR
317 && abSense[2] == 6 /* unit attention */
318 && ( (abSense[12] == 0x29 && abSense[13] < 5 /* reset */)
319 || (abSense[12] == 0x2a && abSense[13] == 0 /* parameters changed */) //???
320 || (abSense[12] == 0x3f && abSense[13] == 0 /* target operating conditions have changed */) //???
321 || (abSense[12] == 0x3f && abSense[13] == 2 /* changed operating definition */) //???
322 || (abSense[12] == 0x3f && abSense[13] == 3 /* inquery parameters changed */)
323 || (abSense[12] == 0x3f && abSense[13] == 5 /* device identifier changed */)
324 )
325 )
326 {
327 fMediaPresent = false;
328 fMediaChanged = true;
329 /** @todo check this media chance stuff on Darwin. */
330 }
331
332#elif defined(RT_OS_LINUX)
333 bool fMediaPresent = ioctl(pThis->FileDevice, CDROM_DRIVE_STATUS, CDSL_CURRENT) == CDS_DISC_OK;
334
335#elif defined(RT_OS_SOLARIS)
336 bool fMediaPresent = false;
337 bool fMediaChanged = false;
338
339 /* Need to pass the previous state and DKIO_NONE for the first time. */
340 static dkio_state s_DeviceState = DKIO_NONE;
341 dkio_state PreviousState = s_DeviceState;
342 int rc2 = ioctl(pThis->FileRawDevice, DKIOCSTATE, &s_DeviceState);
343 if (rc2 == 0)
344 {
345 fMediaPresent = (s_DeviceState == DKIO_INSERTED);
346 if (PreviousState != s_DeviceState)
347 fMediaChanged = true;
348 }
349 else
350 fMediaChanged = true;
351
352#else
353# error "Unsupported platform."
354#endif
355
356 RTCritSectEnter(&pThis->CritSect);
357
358 int rc = VINF_SUCCESS;
359 if (pThis->fMediaPresent != fMediaPresent)
360 {
361 LogFlow(("drvHostDvdPoll: %d -> %d\n", pThis->fMediaPresent, fMediaPresent));
362 pThis->fMediaPresent = false;
363 if (fMediaPresent)
364 rc = DRVHostBaseMediaPresent(pThis);
365 else
366 DRVHostBaseMediaNotPresent(pThis);
367 }
368 else if (fMediaPresent)
369 {
370 /*
371 * Poll for media change.
372 */
373#if defined(RT_OS_DARWIN) || defined(RT_OS_SOLARIS)
374 /* taken care of above. */
375#elif defined(RT_OS_LINUX)
376 bool fMediaChanged = ioctl(pThis->FileDevice, CDROM_MEDIA_CHANGED, CDSL_CURRENT) == 1;
377#else
378# error "Unsupported platform."
379#endif
380 if (fMediaChanged)
381 {
382 LogFlow(("drvHostDVDMediaThread: Media changed!\n"));
383 DRVHostBaseMediaNotPresent(pThis);
384 rc = DRVHostBaseMediaPresent(pThis);
385 }
386 }
387
388 RTCritSectLeave(&pThis->CritSect);
389 return rc;
390}
391#endif /* USE_MEDIA_POLLING */
392
393
394/** @copydoc PDMIBLOCK::pfnSendCmd */
395static int drvHostDvdSendCmd(PPDMIBLOCK pInterface, const uint8_t *pbCmd, PDMBLOCKTXDIR enmTxDir, void *pvBuf, size_t *pcbBuf,
396 uint8_t *pbStat, uint32_t cTimeoutMillies)
397{
398 PDRVHOSTBASE pThis = PDMIBLOCK_2_DRVHOSTBASE(pInterface);
399 int rc;
400 LogFlow(("%s: cmd[0]=%#04x txdir=%d pcbBuf=%d timeout=%d\n", __FUNCTION__, pbCmd[0], enmTxDir, *pcbBuf, cTimeoutMillies));
401
402#ifdef RT_OS_DARWIN
403 /*
404 * Pass the request on to the internal scsi command interface.
405 * The command seems to be 12 bytes long, the docs a bit copy&pasty on the command length point...
406 */
407 if (enmTxDir == PDMBLOCKTXDIR_FROM_DEVICE)
408 memset(pvBuf, '\0', *pcbBuf); /* we got read size, but zero it anyway. */
409 uint8_t abSense[32];
410 rc = DRVHostBaseScsiCmd(pThis, pbCmd, 12, PDMBLOCKTXDIR_FROM_DEVICE, pvBuf, pcbBuf, abSense, sizeof(abSense), cTimeoutMillies);
411 if (rc == VERR_UNRESOLVED_ERROR)
412 {
413 *pbStat = abSense[2] & 0x0f;
414 rc = VINF_SUCCESS;
415 }
416
417#elif defined(RT_OS_L4)
418 /* Not really ported to L4 yet. */
419 rc = VERR_INTERNAL_ERROR;
420
421#elif defined(RT_OS_LINUX)
422 int direction;
423 struct cdrom_generic_command cgc;
424 request_sense sense;
425
426 switch (enmTxDir)
427 {
428 case PDMBLOCKTXDIR_NONE:
429 Assert(*pcbBuf == 0);
430 direction = CGC_DATA_NONE;
431 break;
432 case PDMBLOCKTXDIR_FROM_DEVICE:
433 Assert(*pcbBuf != 0);
434 /* Make sure that the buffer is clear for commands reading
435 * data. The actually received data may be shorter than what
436 * we expect, and due to the unreliable feedback about how much
437 * data the ioctl actually transferred, it's impossible to
438 * prevent that. Returning previous buffer contents may cause
439 * security problems inside the guest OS, if users can issue
440 * commands to the CDROM device. */
441 memset(pvBuf, '\0', *pcbBuf);
442 direction = CGC_DATA_READ;
443 break;
444 case PDMBLOCKTXDIR_TO_DEVICE:
445 Assert(*pcbBuf != 0);
446 direction = CGC_DATA_WRITE;
447 break;
448 default:
449 AssertMsgFailed(("enmTxDir invalid!\n"));
450 direction = CGC_DATA_NONE;
451 }
452 memset(&cgc, '\0', sizeof(cgc));
453 memcpy(cgc.cmd, pbCmd, CDROM_PACKET_SIZE);
454 cgc.buffer = (unsigned char *)pvBuf;
455 cgc.buflen = *pcbBuf;
456 cgc.stat = 0;
457 cgc.sense = &sense;
458 cgc.data_direction = direction;
459 cgc.quiet = false;
460 cgc.timeout = cTimeoutMillies;
461 rc = ioctl(pThis->FileDevice, CDROM_SEND_PACKET, &cgc);
462 if (rc < 0)
463 {
464 if (errno == EBUSY)
465 rc = VERR_PDM_MEDIA_LOCKED;
466 else if (errno == ENOSYS)
467 rc = VERR_NOT_SUPPORTED;
468 else
469 {
470 if (rc == VERR_ACCESS_DENIED && cgc.sense->sense_key == SCSI_SENSE_NONE)
471 cgc.sense->sense_key = SCSI_SENSE_ILLEGAL_REQUEST;
472 *pbStat = cgc.sense->sense_key;
473 rc = RTErrConvertFromErrno(errno);
474 Log2(("%s: error status %d, rc=%Rrc\n", __FUNCTION__, cgc.stat, rc));
475 }
476 }
477 Log2(("%s: after ioctl: cgc.buflen=%d txlen=%d\n", __FUNCTION__, cgc.buflen, *pcbBuf));
478 /* The value of cgc.buflen does not reliably reflect the actual amount
479 * of data transferred (for packet commands with little data transfer
480 * it's 0). So just assume that everything worked ok. */
481
482#elif defined(RT_OS_SOLARIS)
483 struct uscsi_cmd usc;
484 union scsi_cdb scdb;
485 memset(&usc, 0, sizeof(struct uscsi_cmd));
486 memset(&scdb, 0, sizeof(scdb));
487
488 switch (enmTxDir)
489 {
490 case PDMBLOCKTXDIR_NONE:
491 Assert(*pcbBuf == 0);
492 usc.uscsi_flags = USCSI_READ;
493 /* nothing to do */
494 break;
495
496 case PDMBLOCKTXDIR_FROM_DEVICE:
497 Assert(*pcbBuf != 0);
498 /* Make sure that the buffer is clear for commands reading
499 * data. The actually received data may be shorter than what
500 * we expect, and due to the unreliable feedback about how much
501 * data the ioctl actually transferred, it's impossible to
502 * prevent that. Returning previous buffer contents may cause
503 * security problems inside the guest OS, if users can issue
504 * commands to the CDROM device. */
505 memset(pvBuf, '\0', *pcbBuf);
506 usc.uscsi_flags = USCSI_READ;
507 break;
508 case PDMBLOCKTXDIR_TO_DEVICE:
509 Assert(*pcbBuf != 0);
510 usc.uscsi_flags = USCSI_WRITE;
511 break;
512 default:
513 AssertMsgFailedReturn(("%d\n", enmTxDir), VERR_INTERNAL_ERROR);
514 }
515 char aSense[32];
516 usc.uscsi_flags |= USCSI_RQENABLE;
517 usc.uscsi_rqbuf = aSense;
518 usc.uscsi_rqlen = 32;
519 usc.uscsi_cdb = (caddr_t)&scdb;
520 usc.uscsi_cdblen = 12;
521 memcpy (usc.uscsi_cdb, pbCmd, usc.uscsi_cdblen);
522 usc.uscsi_bufaddr = (caddr_t)pvBuf;
523 usc.uscsi_buflen = *pcbBuf;
524 usc.uscsi_timeout = (cTimeoutMillies + 999) / 1000;
525
526 /* We need root privileges for user-SCSI under Solaris. */
527#ifdef VBOX_WITH_SUID_WRAPPER
528 uid_t effUserID = geteuid();
529 solarisEnterRootMode(&effUserID); /** @todo check return code when this really works. */
530#endif
531 rc = ioctl(pThis->FileRawDevice, USCSICMD, &usc);
532#ifdef VBOX_WITH_SUID_WRAPPER
533 solarisExitRootMode(&effUserID);
534#endif
535 if (rc < 0)
536 {
537 if (errno == EPERM)
538 return VERR_PERMISSION_DENIED;
539 if (usc.uscsi_status)
540 {
541 *pbStat = aSense[2] & 0x0f;
542 rc = RTErrConvertFromErrno(errno);
543 Log2(("%s: error status. rc=%Rrc\n", __FUNCTION__, rc));
544 }
545 else
546 *pbStat = 0;
547 }
548 Log2(("%s: after ioctl: residual buflen=%d original buflen=%d\n", __FUNCTION__, usc.uscsi_resid, usc.uscsi_buflen));
549
550#elif defined(RT_OS_WINDOWS)
551 int direction;
552 struct _REQ
553 {
554 SCSI_PASS_THROUGH_DIRECT spt;
555 uint8_t aSense[18];
556 } Req;
557 DWORD cbReturned = 0;
558
559 switch (enmTxDir)
560 {
561 case PDMBLOCKTXDIR_NONE:
562 direction = SCSI_IOCTL_DATA_UNSPECIFIED;
563 break;
564 case PDMBLOCKTXDIR_FROM_DEVICE:
565 Assert(*pcbBuf != 0);
566 /* Make sure that the buffer is clear for commands reading
567 * data. The actually received data may be shorter than what
568 * we expect, and due to the unreliable feedback about how much
569 * data the ioctl actually transferred, it's impossible to
570 * prevent that. Returning previous buffer contents may cause
571 * security problems inside the guest OS, if users can issue
572 * commands to the CDROM device. */
573 memset(pvBuf, '\0', *pcbBuf);
574 direction = SCSI_IOCTL_DATA_IN;
575 break;
576 case PDMBLOCKTXDIR_TO_DEVICE:
577 direction = SCSI_IOCTL_DATA_OUT;
578 break;
579 default:
580 AssertMsgFailed(("enmTxDir invalid!\n"));
581 direction = SCSI_IOCTL_DATA_UNSPECIFIED;
582 }
583 memset(&Req, '\0', sizeof(Req));
584 Req.spt.Length = sizeof(Req.spt);
585 Req.spt.CdbLength = 12;
586 memcpy(Req.spt.Cdb, pbCmd, Req.spt.CdbLength);
587 Req.spt.DataBuffer = pvBuf;
588 Req.spt.DataTransferLength = *pcbBuf;
589 Req.spt.DataIn = direction;
590 Req.spt.TimeOutValue = (cTimeoutMillies + 999) / 1000; /* Convert to seconds */
591 Req.spt.SenseInfoLength = sizeof(Req.aSense);
592 Req.spt.SenseInfoOffset = RT_OFFSETOF(struct _REQ, aSense);
593 if (DeviceIoControl((HANDLE)pThis->FileDevice, IOCTL_SCSI_PASS_THROUGH_DIRECT,
594 &Req, sizeof(Req), &Req, sizeof(Req), &cbReturned, NULL))
595 {
596 if (cbReturned > RT_OFFSETOF(struct _REQ, aSense))
597 *pbStat = Req.aSense[2] & 0x0f;
598 else
599 *pbStat = 0;
600 /* Windows shares the property of not properly reflecting the actually
601 * transferred data size. See above. Assume that everything worked ok. */
602 rc = VINF_SUCCESS;
603 }
604 else
605 rc = RTErrConvertFromWin32(GetLastError());
606 Log2(("%s: scsistatus=%d bytes returned=%d tlength=%d\n", __FUNCTION__, Req.spt.ScsiStatus, cbReturned, Req.spt.DataTransferLength));
607
608#else
609# error "Unsupported platform."
610#endif
611 LogFlow(("%s: rc=%Rrc\n", __FUNCTION__, rc));
612 return rc;
613}
614
615#ifdef VBOX_WITH_SUID_WRAPPER
616/* These functions would have to go into a seperate solaris binary with
617 * the setuid permission set, which would run the user-SCSI ioctl and
618 * return the value. BUT... this might be prohibitively slow.
619 */
620#ifdef RT_OS_SOLARIS
621/**
622 * Checks if the current user is authorized using Solaris' role-based access control.
623 * Made as a seperate function with so that it need not be invoked each time we need
624 * to gain root access.
625 *
626 * @returns VBox error code.
627 */
628static int solarisCheckUserAuth()
629{
630 /* Uses Solaris' role-based access control (RBAC).*/
631 struct passwd *pPass = getpwuid(getuid());
632 if (pPass == NULL || chkauthattr("solaris.device.cdrw", pPass->pw_name) == 0)
633 return VERR_PERMISSION_DENIED;
634
635 return VINF_SUCCESS;
636}
637
638/**
639 * Setuid wrapper to gain root access.
640 *
641 * @returns VBox error code.
642 * @param pEffUserID Pointer to effective user ID.
643 */
644static int solarisEnterRootMode(uid_t *pEffUserID)
645{
646 /* Increase privilege if required */
647 if (*pEffUserID != 0)
648 {
649 if (seteuid(0) == 0)
650 {
651 *pEffUserID = 0;
652 return VINF_SUCCESS;
653 }
654 return VERR_PERMISSION_DENIED;
655 }
656 return VINF_SUCCESS;
657}
658
659/**
660 * Setuid wrapper to relinquish root access.
661 *
662 * @returns VBox error code.
663 * @param pEffUserID Pointer to effective user ID.
664 */
665static int solarisExitRootMode(uid_t *pEffUserID)
666{
667 /* Get back to user mode. */
668 if (*pEffUserID == 0)
669 {
670 uid_t realID = getuid();
671 if (seteuid(realID) == 0)
672 {
673 *pEffUserID = realID;
674 return VINF_SUCCESS;
675 }
676 return VERR_PERMISSION_DENIED;
677 }
678 return VINF_SUCCESS;
679}
680#endif /* RT_OS_SOLARIS */
681#endif
682
683/* -=-=-=-=- driver interface -=-=-=-=- */
684
685
686/**
687 * Construct a host dvd drive driver instance.
688 *
689 * @returns VBox status.
690 * @param pDrvIns The driver instance data.
691 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
692 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
693 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
694 * iInstance it's expected to be used a bit in this function.
695 */
696static DECLCALLBACK(int) drvHostDvdConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
697{
698 PDRVHOSTBASE pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTBASE);
699 LogFlow(("drvHostDvdConstruct: iInstance=%d\n", pDrvIns->iInstance));
700
701 /*
702 * Validate configuration.
703 */
704 if (!CFGMR3AreValuesValid(pCfgHandle, "Path\0Interval\0Locked\0BIOSVisible\0AttachFailError\0Passthrough\0"))
705 return VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES;
706
707
708 /*
709 * Init instance data.
710 */
711 int rc = DRVHostBaseInitData(pDrvIns, pCfgHandle, PDMBLOCKTYPE_DVD);
712 if (RT_SUCCESS(rc))
713 {
714 /*
715 * Override stuff.
716 */
717
718#ifndef RT_OS_L4 /* Passthrough is not supported on L4 yet */
719 bool fPassthrough;
720 rc = CFGMR3QueryBool(pCfgHandle, "Passthrough", &fPassthrough);
721 if (RT_SUCCESS(rc) && fPassthrough)
722 {
723 pThis->IBlock.pfnSendCmd = drvHostDvdSendCmd;
724 /* Passthrough requires opening the device in R/W mode. */
725 pThis->fReadOnlyConfig = false;
726# ifdef VBOX_WITH_SUID_WRAPPER /* Solaris setuid for Passthrough mode. */
727 rc = solarisCheckUserAuth();
728 if (RT_FAILURE(rc))
729 {
730 Log(("DVD: solarisCheckUserAuth failed. Permission denied!\n"));
731 return rc;
732 }
733# endif /* VBOX_WITH_SUID_WRAPPER */
734 }
735#endif /* !RT_OS_L4 */
736
737 pThis->IMount.pfnUnmount = drvHostDvdUnmount;
738 pThis->pfnDoLock = drvHostDvdDoLock;
739#ifdef USE_MEDIA_POLLING
740 if (!fPassthrough)
741 pThis->pfnPoll = drvHostDvdPoll;
742 else
743 pThis->pfnPoll = NULL;
744#endif
745#ifdef RT_OS_LINUX
746 pThis->pfnGetMediaSize = drvHostDvdGetMediaSize;
747#endif
748
749 /*
750 * 2nd init part.
751 */
752 rc = DRVHostBaseInitFinish(pThis);
753 }
754 if (RT_FAILURE(rc))
755 {
756 if (!pThis->fAttachFailError)
757 {
758 /* Suppressing the attach failure error must not affect the normal
759 * DRVHostBaseDestruct, so reset this flag below before leaving. */
760 pThis->fKeepInstance = true;
761 rc = VINF_SUCCESS;
762 }
763 DRVHostBaseDestruct(pDrvIns);
764 pThis->fKeepInstance = false;
765 }
766
767 LogFlow(("drvHostDvdConstruct: returns %Rrc\n", rc));
768 return rc;
769}
770
771
772/**
773 * Block driver registration record.
774 */
775const PDMDRVREG g_DrvHostDVD =
776{
777 /* u32Version */
778 PDM_DRVREG_VERSION,
779 /* szDriverName */
780 "HostDVD",
781 /* pszDescription */
782 "Host DVD Block Driver.",
783 /* fFlags */
784 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
785 /* fClass. */
786 PDM_DRVREG_CLASS_BLOCK,
787 /* cMaxInstances */
788 ~0,
789 /* cbInstance */
790 sizeof(DRVHOSTBASE),
791 /* pfnConstruct */
792 drvHostDvdConstruct,
793 /* pfnDestruct */
794 DRVHostBaseDestruct,
795 /* pfnIOCtl */
796 NULL,
797 /* pfnPowerOn */
798 NULL,
799 /* pfnReset */
800 NULL,
801 /* pfnSuspend */
802 NULL,
803 /* pfnResume */
804 NULL,
805 /* pfnDetach */
806 NULL
807};
808
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