VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp@ 28800

Last change on this file since 28800 was 28800, checked in by vboxsync, 14 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.8 KB
Line 
1/* $Id: RTSystemQueryDmiString-darwin.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemQueryDmiString, darwin ring-3.
4 */
5
6/*
7 * Copyright (C) 2010 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#include <iprt/system.h>
32#include "internal/iprt.h"
33
34#include <iprt/assert.h>
35#include <iprt/err.h>
36#include <iprt/mem.h>
37#include <iprt/string.h>
38
39#include <mach/mach_port.h>
40#include <IOKit/IOKitLib.h>
41
42
43/*******************************************************************************
44* Defined Constants And Macros *
45*******************************************************************************/
46#define IOCLASS_PLATFORMEXPERTDEVICE "IOPlatformExpertDevice"
47#define PROP_PRODUCT_NAME "product-name"
48#define PROP_PRODUCT_VERSION "version"
49#define PROP_PRODUCT_SERIAL "IOPlatformSerialNumber"
50#define PROP_PRODUCT_UUID "IOPlatformUUID"
51
52
53RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf)
54{
55 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
56 AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
57 *pszBuf = '\0';
58 AssertReturn(enmString > RTSYSDMISTR_INVALID && enmString < RTSYSDMISTR_END, VERR_INVALID_PARAMETER);
59
60 CFStringRef PropStringRef = NULL;
61 switch (enmString)
62 {
63 case RTSYSDMISTR_PRODUCT_NAME: PropStringRef = CFSTR(PROP_PRODUCT_NAME); break;
64 case RTSYSDMISTR_PRODUCT_VERSION: PropStringRef = CFSTR(PROP_PRODUCT_VERSION); break;
65 case RTSYSDMISTR_PRODUCT_SERIAL: PropStringRef = CFSTR(PROP_PRODUCT_SERIAL); break;
66 case RTSYSDMISTR_PRODUCT_UUID: PropStringRef = CFSTR(PROP_PRODUCT_UUID); break;
67 default:
68 return VERR_NOT_SUPPORTED;
69 }
70
71 mach_port_t MasterPort;
72 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
73 if (kr != kIOReturnSuccess)
74 {
75 if (kr == KERN_NO_ACCESS)
76 return VERR_ACCESS_DENIED;
77 return RTErrConvertFromDarwinIO(kr);
78 }
79
80 CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_PLATFORMEXPERTDEVICE);
81 if (!ClassToMatch)
82 return VERR_NOT_SUPPORTED;
83
84 /* IOServiceGetMatchingServices will always consume ClassToMatch. */
85 io_iterator_t Iterator;
86 kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
87 if (kr != kIOReturnSuccess)
88 return RTErrConvertFromDarwinIO(kr);
89
90 int rc = VERR_NOT_SUPPORTED;
91 io_service_t ServiceObject;
92 while ((ServiceObject = IOIteratorNext(Iterator)))
93 {
94 if ( enmString == RTSYSDMISTR_PRODUCT_NAME
95 || enmString == RTSYSDMISTR_PRODUCT_VERSION)
96 {
97 CFDataRef DataRef = (CFDataRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef,
98 kCFAllocatorDefault, kNilOptions);
99 if (DataRef)
100 {
101 size_t cbData = CFDataGetLength(DataRef);
102 const char *pchData = (const char *)CFDataGetBytePtr(DataRef);
103 rc = RTStrCopyEx(pszBuf, cbBuf, pchData, cbData);
104 CFRelease(DataRef);
105 break;
106 }
107 }
108 else
109 {
110 CFStringRef StringRef = (CFStringRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef,
111 kCFAllocatorDefault, kNilOptions);
112 if (StringRef)
113 {
114 Boolean fRc = CFStringGetCString(StringRef, pszBuf, cbBuf, kCFStringEncodingUTF8);
115 if (fRc)
116 rc = VINF_SUCCESS;
117 else
118 {
119 CFIndex cwc = CFStringGetLength(StringRef);
120 size_t cbTmp = cwc + 1;
121 char *pszTmp = (char *)RTMemTmpAlloc(cbTmp);
122 int cTries = 1;
123 while ( pszTmp
124 && (fRc = CFStringGetCString(StringRef, pszTmp, cbTmp, kCFStringEncodingUTF8)) == FALSE
125 && cTries++ < 4)
126 {
127 RTMemTmpFree(pszTmp);
128 cbTmp *= 2;
129 pszTmp = (char *)RTMemTmpAlloc(cbTmp);
130 }
131 if (fRc)
132 rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
133 else if (!pszTmp)
134 rc = VERR_NO_TMP_MEMORY;
135 else
136 rc = VERR_ACCESS_DENIED;
137 RTMemFree(pszTmp);
138 }
139 CFRelease(StringRef);
140 break;
141 }
142 }
143 }
144
145 IOObjectRelease(ServiceObject);
146 IOObjectRelease(Iterator);
147 return rc;
148}
149
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