[11350] | 1 | /* $Id: RTSystemQueryOSInfo-posix.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * IPRT - RTSystemQueryOSInfo, POSIX implementation.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2008-2023 Oracle and/or its affiliates.
|
---|
[11350] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
[11350] | 11 | *
|
---|
[96407] | 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 | *
|
---|
[11350] | 25 | * The contents of this file may alternatively be used under the terms
|
---|
| 26 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 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
|
---|
[11350] | 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.
|
---|
[96407] | 33 | *
|
---|
| 34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[11350] | 35 | */
|
---|
| 36 |
|
---|
[57358] | 37 |
|
---|
| 38 | /*********************************************************************************************************************************
|
---|
| 39 | * Header Files *
|
---|
| 40 | *********************************************************************************************************************************/
|
---|
[11350] | 41 | #include <iprt/system.h>
|
---|
| 42 | #include <iprt/assert.h>
|
---|
| 43 | #include <iprt/string.h>
|
---|
[76452] | 44 | #include <iprt/errcore.h>
|
---|
[11350] | 45 |
|
---|
| 46 | #include <errno.h>
|
---|
| 47 | #include <sys/utsname.h>
|
---|
| 48 |
|
---|
| 49 |
|
---|
| 50 | RTDECL(int) RTSystemQueryOSInfo(RTSYSOSINFO enmInfo, char *pszInfo, size_t cchInfo)
|
---|
| 51 | {
|
---|
| 52 | /*
|
---|
| 53 | * Quick validation.
|
---|
| 54 | */
|
---|
| 55 | AssertReturn(enmInfo > RTSYSOSINFO_INVALID && enmInfo < RTSYSOSINFO_END, VERR_INVALID_PARAMETER);
|
---|
| 56 | AssertPtrReturn(pszInfo, VERR_INVALID_POINTER);
|
---|
| 57 | if (!cchInfo)
|
---|
| 58 | return VERR_BUFFER_OVERFLOW;
|
---|
| 59 |
|
---|
| 60 | /*
|
---|
| 61 | * Handle the request.
|
---|
| 62 | */
|
---|
| 63 | switch (enmInfo)
|
---|
| 64 | {
|
---|
| 65 | case RTSYSOSINFO_PRODUCT:
|
---|
| 66 | case RTSYSOSINFO_RELEASE:
|
---|
| 67 | case RTSYSOSINFO_VERSION:
|
---|
| 68 | {
|
---|
| 69 | struct utsname UtsInfo;
|
---|
[15850] | 70 | if (uname(&UtsInfo) < 0)
|
---|
[11350] | 71 | return RTErrConvertFromErrno(errno);
|
---|
| 72 | const char *pszSrc;
|
---|
| 73 | switch (enmInfo)
|
---|
| 74 | {
|
---|
| 75 | case RTSYSOSINFO_PRODUCT: pszSrc = UtsInfo.sysname; break;
|
---|
| 76 | case RTSYSOSINFO_RELEASE: pszSrc = UtsInfo.release; break;
|
---|
| 77 | case RTSYSOSINFO_VERSION: pszSrc = UtsInfo.version; break;
|
---|
| 78 | default: AssertFatalFailed(); /* screw gcc */
|
---|
| 79 | }
|
---|
| 80 | size_t cch = strlen(pszSrc);
|
---|
| 81 | if (cch < cchInfo)
|
---|
| 82 | {
|
---|
| 83 | memcpy(pszInfo, pszSrc, cch + 1);
|
---|
| 84 | return VINF_SUCCESS;
|
---|
| 85 | }
|
---|
| 86 | memcpy(pszInfo, pszSrc, cchInfo - 1);
|
---|
| 87 | pszInfo[cchInfo - 1] = '\0';
|
---|
| 88 | return VERR_BUFFER_OVERFLOW;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
| 91 |
|
---|
[11352] | 92 | case RTSYSOSINFO_SERVICE_PACK:
|
---|
[11350] | 93 | default:
|
---|
| 94 | *pszInfo = '\0';
|
---|
| 95 | return VERR_NOT_SUPPORTED;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | return VINF_SUCCESS;
|
---|
| 99 | }
|
---|
| 100 |
|
---|