diff options
Diffstat (limited to 'include/iprt/linux')
-rw-r--r-- | include/iprt/linux/Makefile.kup | 0 | ||||
-rw-r--r-- | include/iprt/linux/symvers.h | 89 | ||||
-rw-r--r-- | include/iprt/linux/sysfs.h | 487 | ||||
-rw-r--r-- | include/iprt/linux/version.h | 205 |
4 files changed, 781 insertions, 0 deletions
diff --git a/include/iprt/linux/Makefile.kup b/include/iprt/linux/Makefile.kup new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/include/iprt/linux/Makefile.kup diff --git a/include/iprt/linux/symvers.h b/include/iprt/linux/symvers.h new file mode 100644 index 00000000..0a8ef1b7 --- /dev/null +++ b/include/iprt/linux/symvers.h @@ -0,0 +1,89 @@ +/** @file + * IPRT - Linux symver and compatibility definitions. + */ + +/* + * Copyright (C) 2008-2022 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + +/* Various tricks to produce binaries which can be run on old Linux + * distributions. This will almost certainly need updating as time + * goes by. */ + +#ifndef IPRT_INCLUDED_linux_symvers_h +#define IPRT_INCLUDED_linux_symvers_h +#ifndef RT_WITHOUT_PRAGMA_ONCE +# pragma once +#endif + +/* Please use -fno-stack-protector on the command line to avoid stack check + * functions which are not available in EL3 for 32-bit builds. */ + +/* Use versions of glibc symbols which are available in 32-bit EL3 or + * 64-bit EL4. Currently only those symbols needed by the Additions, + * though this could probably be extended to work for host builds too. */ +#if defined(RT_ARCH_AMD64) +__asm__(".symver memcpy,memcpy@GLIBC_2.2.5"); +__asm__(".symver posix_spawn,posix_spawn@GLIBC_2.2.5"); +#else /* RT_ARCH_X86 */ +__asm__(".symver posix_spawn,posix_spawn@GLIBC_2.2"); +#endif + +/* Do not use *_chk functions */ +#undef _FORTIFY_SOURCE + +/* Do not use __isoc99_* functions */ +#undef __USE_GNU +#define __USE_GNU 1 + +/* And EL5 wants this too with __USE_GNU */ +#undef _GNU_SOURCE +#define _GNU_SOURCE 1 + +/* Tell IPRT not to use newer functions */ +#include <features.h> +#undef __GLIBC_MINOR__ +#define __GLIBC_MINOR__ 3 + +/* Do not use fcntl64 */ +#include <fcntl.h> +#ifdef fnctl +# undef fcntl +#endif +#if defined(RT_ARCH_AMD64) +__asm__(".symver fcntl64,fcntl@GLIBC_2.2.5"); +#else +__asm__(".symver fcntl64,fcntl@GLIBC_2.0"); +#endif + +/* Do not use ISO C99 scanf which has a glibc 2.7 dependency. */ +#undef __GLIBC_USE_DEPRECATED_SCANF +#define __GLIBC_USE_DEPRECATED_SCANF 1 +#endif /* !IPRT_INCLUDED_linux_symvers_h */ diff --git a/include/iprt/linux/sysfs.h b/include/iprt/linux/sysfs.h new file mode 100644 index 00000000..02c3af39 --- /dev/null +++ b/include/iprt/linux/sysfs.h @@ -0,0 +1,487 @@ +/* $Id: sysfs.h $ */ +/** @file + * IPRT - Linux sysfs access. + */ + +/* + * Copyright (C) 2008-2022 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + +#ifndef IPRT_INCLUDED_linux_sysfs_h +#define IPRT_INCLUDED_linux_sysfs_h +#ifndef RT_WITHOUT_PRAGMA_ONCE +# pragma once +#endif + +#include <iprt/cdefs.h> +#include <iprt/types.h> +#include <iprt/stdarg.h> + +#include <sys/types.h> /* for dev_t */ + +RT_C_DECLS_BEGIN + +/** @defgroup grp_rt_linux_sysfs RTLinuxSysfs - Linux sysfs + * @ingroup grp_rt + * @{ + */ + +/** + * Checks if a sysfs file (or directory, device, symlink, whatever) exists. + * + * @returns true if the sysfs object exists. + * false otherwise or if an error occurred. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param va The format args. + */ +RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0); + +/** + * Checks if a sysfs object (directory, device, symlink, whatever) exists. + * + * @returns IPRT status code. + * @retval VINF_SUCCESS if the sysfs object exists. + * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param va The format args. + */ +RTDECL(int) RTLinuxSysFsExistsExV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0); + +/** + * Checks if a sysfs file (or directory, device, symlink, whatever) exists. + * + * @returns true if the sysfs object exists. + * false otherwise or if an error occurred. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param ... The format args. + */ +RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2); + +/** + * Checks if a sysfs object (directory, device, symlink, whatever) exists. + * + * @returns IPRT status code. + * @retval VINF_SUCCESS if the sysfs object exists. + * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param ... The format args. + */ +RTDECL(int) RTLinuxSysFsExistsEx(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2); + +/** + * Opens a sysfs file for reading. + * + * @returns IPRT status code. + * @param phFile Where to store the file handle on success. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param va The format args. + * + * @note Close the file using RTFileClose(). + */ +RTDECL(int) RTLinuxSysFsOpenV(PRTFILE phFile, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0); + +/** + * Opens a sysfs file - extended version. + * + * @returns IPRT status code. + * @param phFile Where to store the file handle on success. + * @param fOpen Open flags, see RTFileOpen(). + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param va The format args. + */ +RTDECL(int) RTLinuxSysFsOpenExV(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); + +/** + * Opens a sysfs file. + * + * @returns IPRT status code. + * @param phFile Where to store the file handle on success. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param ... The format args. + * + * @note Close the file using RTFileClose(). + */ +RTDECL(int) RTLinuxSysFsOpen(PRTFILE phFile, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3); + +/** + * Opens a sysfs file - extended version. + * + * @returns IPRT status code. + * @param phFile Where to store the file handle on success. + * @param fOpen Open flags, see RTFileOpen(). + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param ... The format args. + */ +RTDECL(int) RTLinuxSysFsOpenEx(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); + +/** + * Reads a string from a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV. + * + * Expects to read the whole file, mind, and will return VERR_BUFFER_OVERFLOW if + * that is not possible with the given buffer size. + * + * @returns IPRT status code. + * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV. + * @param pszBuf Where to store the string. + * @param cchBuf The size of the buffer. Must be at least 2 bytes. + * @param pcchRead Where to store the amount of characters read on success - optional. + */ +RTDECL(int) RTLinuxSysFsReadStr(RTFILE hFile, char *pszBuf, size_t cchBuf, size_t *pcchRead); + +/** + * Writes a string to a file opened with RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV for writing. + * + * @returns IPRT status code. + * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV. + * @param pszBuf The string to write. + * @param cchBuf The length of the string to write - if 0 is given + * the string length is determined before writing it including the zero terminator. + * @param pcchWritten Where to store the amount of characters written on success - optional. + */ +RTDECL(int) RTLinuxSysFsWriteStr(RTFILE hFile, const char *pszBuf, size_t cchBuf, size_t *pcchWritten); + +/** + * Reads the remainder of a file opened with RTLinuxSysFsOpen or + * RTLinuxSysFsOpenV. + * + * @returns IPRT status code. + * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV. + * @param pvBuf Where to store the bits from the file. + * @param cbBuf The size of the buffer. + * @param pcbRead Where to return the number of bytes read. Optional. + */ +RTDECL(int) RTLinuxSysFsReadFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbRead); + +/** + * Writes the given buffer to a file opened with RTLinuxSysFsOpenEx or + * RTLinuxSysFsOpenExV. + * + * @returns IPRT status code. + * @param hFile The file descriptor returned by RTLinuxSysFsOpenEx or RTLinuxSysFsOpenExV. + * @param pvBuf The data to write. + * @param cbBuf The size of the buffer. + * @param pcbWritten Where to return the number of bytes read. Optional. + */ +RTDECL(int) RTLinuxSysFsWriteFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbWritten); + +/** + * Reads a number from a sysfs file. + * + * @returns IPRT status code. + * @param uBase The number base, 0 for autodetect. + * @param pi64 Where to store the 64-bit signed on success. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsReadIntFileV(unsigned uBase, int64_t *pi64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); + +/** + * Reads a number from a sysfs file. + * + * @returns IPRT status code. + * @param uBase The number base, 0 for autodetect. + * @param pi64 Where to store the 64-bit signed on success. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsReadIntFile(unsigned uBase, int64_t *pi64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); + +/** + * Writes an unsigned 8-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u8 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU8FileV(unsigned uBase, uint8_t u8, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); + +/** + * Writes an unsigned 8-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u8 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU8File(unsigned uBase, uint8_t u8, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); + +/** + * Writes an unsigned 16-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u16 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU16FileV(unsigned uBase, uint16_t u16, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); + +/** + * Writes an unsigned 16-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u16 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU16File(unsigned uBase, uint16_t u16, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); + +/** + * Writes an unsigned 32-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u32 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU32FileV(unsigned uBase, uint32_t u32, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); + +/** + * Writes an unsigned 8-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u32 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU32File(unsigned uBase, uint32_t u32, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); + +/** + * Writes an unsigned 64-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u64 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU64FileV(unsigned uBase, uint64_t u64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); + +/** + * Writes an unsigned 8-bit number to a sysfs file. + * + * @returns IPRT status code. + * @param uBase The base format to write the number. Passing 16 here for + * example writes the number as a hexadecimal string with 0x prepended. + * @param u64 The number to write. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsWriteU64File(unsigned uBase, uint32_t u64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); + +/** + * Reads a device number from a sysfs file. + * + * @returns IPRT status code. + * @param pDevNum Where to store the device number on success. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsReadDevNumFileV(dev_t *pDevNum, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0); + +/** + * Reads a device number from a sysfs file. + * + * @returns IPRT status code. + * @param pDevNum Where to store the device number on success. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsReadDevNumFile(dev_t *pDevNum, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3); + +/** + * Reads a string from a sysfs file. + * + * If the file contains a newline, we only return the text up until there. This + * differs from the RTLinuxSysFsReadStr() behaviour. + * + * @returns IPRT status code. + * @param pszBuf Where to store the path element. Must be at least two + * characters, but a longer buffer would be advisable. + * @param cchBuf The size of the buffer pointed to by @a pszBuf. + * @param pcchRead Where to store the amount of characters read on success - optional. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0); + +/** + * Reads a string from a sysfs file. If the file contains a newline, we only + * return the text up until there. + * + * @returns IPRT status code. + * @param pszBuf Where to store the path element. Must be at least two + * characters, but a longer buffer would be advisable. + * @param cchBuf The size of the buffer pointed to by @a pszBuf. + * @param pcchRead Where to store the amount of characters read on success - optional. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5); + +/** + * Writes a string to a sysfs file. + * + * @returns IPRT status code. + * @param pszBuf The string to write. + * @param cchBuf The size of the buffer pointed to by @a pszBuf. + * @param pcchWritten Where to store the amount of characters written on success - optional. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsWriteStrFileV(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0); + +/** + * Writes a string to a sysfs file. + * + * @returns IPRT status code. + * @param pszBuf The string to write. + * @param cchBuf The size of the buffer pointed to by @a pszBuf. + * @param pcchWritten Where to store the amount of characters written on success - optional. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsWriteStrFile(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5); + +/** + * Reads the last element of the path of the file pointed to by the symbolic + * link specified. + * + * This is needed at least to get the name of the driver associated with a + * device, where pszFormat should be the "driver" link in the devices sysfs + * directory. + * + * @returns IPRT status code. + * @param pszBuf Where to store the path element. Must be at least two + * characters, but a longer buffer would be advisable. + * @param cchBuf The size of the buffer pointed to by @a pszBuf. + * @param pchBuf Where to store the length of the returned string on success - optional. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param va Format args. + */ +RTDECL(int) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(4, 0); + +/** + * Reads the last element of the path of the file pointed to by the symbolic + * link specified. + * + * This is needed at least to get the name of the driver associated with a + * device, where pszFormat should be the "driver" link in the devices sysfs + * directory. + * + * @returns IPRT status code. + * @param pszBuf Where to store the path element. Must be at least two + * characters, but a longer buffer would be advisable. + * @param cchBuf The size of the buffer pointed to by @a pszBuf. + * @param pchBuf Where to store the length of the returned string on success - optional. + * @param pszFormat The filename format, either absolute or relative to "/sys/". + * @param ... Format args. + */ +RTDECL(int) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(4, 5); + +/** + * Check the path of a device node under /dev, given the device number and a + * pattern and store the path into @a pszBuf. + * + * @returns IPRT status code. + * @retval VERR_FILE_NOT_FOUND if no matching device node could be found. + * @param DevNum The device number to search for. + * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and + * RTFS_TYPE_DEV_BLOCK are valid values. + * @param pszBuf Where to store the path. + * @param cchBuf The size of the buffer. + * @param pszPattern The expected path format of the device node, either + * absolute or relative to "/dev". + * @param va Format args. + */ +RTDECL(int) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf, + const char *pszPattern, va_list va) RT_IPRT_FORMAT_ATTR(5, 0); + +/** + * Check the path of a device node under /dev, given the device number and a + * pattern and store the path into @a pszBuf. + * + * @returns IPRT status code. + * @retval VERR_FILE_NOT_FOUND if no matching device node could be found. + * @param DevNum The device number to search for + * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and + * RTFS_TYPE_DEV_BLOCK are valid values + * @param pszBuf Where to store the path. + * @param cchBuf The size of the buffer. + * @param pszPattern The expected path format of the device node, either + * absolute or relative to "/dev". + * @param ... Format args. + */ +RTDECL(int) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf, + const char *pszPattern, ...) RT_IPRT_FORMAT_ATTR(5, 6); + +/** + * Constructs the path of a sysfs file from the format parameters passed, + * prepending "/sys/" if the path is relative. + * + * @returns IPRT status code. + * @param pszPath Where to write the path. + * @param cbPath The size of the buffer pointed to by @a pszPath. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param va The format args. + */ +RTDECL(int) RTLinuxConstructPathV(char *pszPath, size_t cbPath, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); + +/** + * Constructs the path of a sysfs file from the format parameters passed, + * prepending "/sys/" if the path is relative. + * + * @returns IPRT status code. + * @param pszPath Where to write the path. + * @param cbPath The size of the buffer pointed to by @a pszPath. + * @param pszFormat The name format, either absolute or relative to "/sys/". + * @param ... The format args. + */ +RTDECL(int) RTLinuxConstructPath(char *pszPath, size_t cbPath, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); + +/** @} */ + +RT_C_DECLS_END + +#endif /* !IPRT_INCLUDED_linux_sysfs_h */ + diff --git a/include/iprt/linux/version.h b/include/iprt/linux/version.h new file mode 100644 index 00000000..6badeab0 --- /dev/null +++ b/include/iprt/linux/version.h @@ -0,0 +1,205 @@ +/* $Id: version.h $ */ +/** @file + * IPRT - Linux kernel version. + */ + +/* + * Copyright (C) 2006-2022 Oracle and/or its affiliates. + * + * This file is part of VirtualBox base platform packages, as + * available from https://www.virtualbox.org. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation, in version 3 of the + * License. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see <https://www.gnu.org/licenses>. + * + * The contents of this file may alternatively be used under the terms + * of the Common Development and Distribution License Version 1.0 + * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included + * in the VirtualBox distribution, in which case the provisions of the + * CDDL are applicable instead of those of the GPL. + * + * You may elect to license modified versions of this file under the + * terms and conditions of either the GPL or the CDDL or both. + * + * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0 + */ + +#ifndef IPRT_INCLUDED_linux_version_h +#define IPRT_INCLUDED_linux_version_h +#ifndef RT_WITHOUT_PRAGMA_ONCE +# pragma once +#endif + +#include <linux/version.h> + +/* We need utsrelease.h in order to detect Ubuntu kernel, + * i.e. check if UTS_UBUNTU_RELEASE_ABI is defined. Support kernels + * starting from Ubuntu 14.04 Trusty which is based on upstream + * kernel 3.13.x. */ +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)) +# include <generated/utsrelease.h> +# include <iprt/cdefs.h> +#endif + +/** @def RTLNX_VER_MIN + * Evaluates to true if the linux kernel version is equal or higher to the + * one specfied. */ +#define RTLNX_VER_MIN(a_Major, a_Minor, a_Patch) \ + (LINUX_VERSION_CODE >= KERNEL_VERSION(a_Major, a_Minor, a_Patch)) + +/** @def RTLNX_VER_MAX + * Evaluates to true if the linux kernel version is less to the one specfied + * (exclusive). */ +#define RTLNX_VER_MAX(a_Major, a_Minor, a_Patch) \ + (LINUX_VERSION_CODE < KERNEL_VERSION(a_Major, a_Minor, a_Patch)) + +/** @def RTLNX_VER_RANGE + * Evaluates to true if the linux kernel version is equal or higher to the given + * minimum version and less (but not equal) to the maximum version (exclusive). */ +#define RTLNX_VER_RANGE(a_MajorMin, a_MinorMin, a_PatchMin, a_MajorMax, a_MinorMax, a_PatchMax) \ + ( LINUX_VERSION_CODE >= KERNEL_VERSION(a_MajorMin, a_MinorMin, a_PatchMin) \ + && LINUX_VERSION_CODE < KERNEL_VERSION(a_MajorMax, a_MinorMax, a_PatchMax) ) + + +/** @def RTLNX_RHEL_MIN + * Require a minium RedHat release. + * @param a_iMajor The major release number (RHEL_MAJOR). + * @param a_iMinor The minor release number (RHEL_MINOR). + * @sa RTLNX_RHEL_MAX, RTLNX_RHEL_RANGE, RTLNX_RHEL_MAJ_PREREQ + */ +#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) +# define RTLNX_RHEL_MIN(a_iMajor, a_iMinor) \ + ((RHEL_MAJOR) > (a_iMajor) || ((RHEL_MAJOR) == (a_iMajor) && (RHEL_MINOR) >= (a_iMinor))) +#else +# define RTLNX_RHEL_MIN(a_iMajor, a_iMinor) (0) +#endif + +/** @def RTLNX_RHEL_MAX + * Require a maximum RedHat release, true for all RHEL versions below it. + * @param a_iMajor The major release number (RHEL_MAJOR). + * @param a_iMinor The minor release number (RHEL_MINOR). + * @sa RTLNX_RHEL_MIN, RTLNX_RHEL_RANGE, RTLNX_RHEL_MAJ_PREREQ + */ +#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) +# define RTLNX_RHEL_MAX(a_iMajor, a_iMinor) \ + ((RHEL_MAJOR) < (a_iMajor) || ((RHEL_MAJOR) == (a_iMajor) && (RHEL_MINOR) < (a_iMinor))) +#else +# define RTLNX_RHEL_MAX(a_iMajor, a_iMinor) (0) +#endif + +/** @def RTLNX_RHEL_RANGE + * Check that it's a RedHat kernel in the given version range. + * The max version is exclusive, the minimum inclusive. + * @sa RTLNX_RHEL_MIN, RTLNX_RHEL_MAX, RTLNX_RHEL_MAJ_PREREQ + */ +#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) +# define RTLNX_RHEL_RANGE(a_iMajorMin, a_iMinorMin, a_iMajorMax, a_iMinorMax) \ + (RTLNX_RHEL_MIN(a_iMajorMin, a_iMinorMin) && RTLNX_RHEL_MAX(a_iMajorMax, a_iMinorMax)) +#else +# define RTLNX_RHEL_RANGE(a_iMajorMin, a_iMinorMin, a_iMajorMax, a_iMinorMax) (0) +#endif + +/** @def RTLNX_RHEL_MAJ_PREREQ + * Require a minimum minor release number for the given RedHat release. + * @param a_iMajor RHEL_MAJOR must _equal_ this. + * @param a_iMinor RHEL_MINOR must be greater or equal to this. + * @sa RTLNX_RHEL_MIN, RTLNX_RHEL_MAX + */ +#if defined(RHEL_MAJOR) && defined(RHEL_MINOR) +# define RTLNX_RHEL_MAJ_PREREQ(a_iMajor, a_iMinor) ((RHEL_MAJOR) == (a_iMajor) && (RHEL_MINOR) >= (a_iMinor)) +#else +# define RTLNX_RHEL_MAJ_PREREQ(a_iMajor, a_iMinor) (0) +#endif + + +/** @def RTLNX_SUSE_MAJ_PREREQ + * Require a minimum minor release number for the given SUSE release. + * @param a_iMajor CONFIG_SUSE_VERSION must _equal_ this. + * @param a_iMinor CONFIG_SUSE_PATCHLEVEL must be greater or equal to this. + */ +#if defined(CONFIG_SUSE_VERSION) && defined(CONFIG_SUSE_PATCHLEVEL) +# define RTLNX_SUSE_MAJ_PREREQ(a_iMajor, a_iMinor) ((CONFIG_SUSE_VERSION) == (a_iMajor) && (CONFIG_SUSE_PATCHLEVEL) >= (a_iMinor)) +#else +# define RTLNX_SUSE_MAJ_PREREQ(a_iMajor, a_iMinor) (0) +#endif + + +#if defined(UTS_UBUNTU_RELEASE_ABI) || defined(DOXYGEN_RUNNING) + +/** Hack to make the UTS_UBUNTU_RELEASE_ABI palatable by the C preprocesor. + * + * While the Ubuntu kernel ABI version looks like a decimal number, some + * kernels has a leading zero (e.g. 050818) that makes the preprocessor think + * it's an octal number. To work around that, we turn it into an hexadecimal + * number by prefixing it with '0x'. */ +# define RTLNX_UBUNTU_ABI(a_iAbi) (RT_CONCAT(0x,a_iAbi)) + +/** @def RTLNX_UBUNTU_ABI_MIN + * Require Ubuntu release ABI to be equal or newer than specified version. + * + * The kernel version should exactly match the specified @a a_iMajor, @a + * a_iMinor and @a a_iPatch. The @a a_iAbi number should be equal to or greater + * than the current ABI version. + * + * @param a_iMajor The major kernel version number. + * @param a_iMinor The minor kernel version number. + * @param a_iPatch The kernel patch level. + * @param a_iAbi Ubuntu kernel ABI version number (inclusive). + */ +# define RTLNX_UBUNTU_ABI_MIN(a_iMajor, a_iMinor, a_iPatch, a_iAbi) \ + ( KERNEL_VERSION(a_iMajor, a_iMinor, a_iPatch) == LINUX_VERSION_CODE \ + && RTLNX_UBUNTU_ABI(UTS_UBUNTU_RELEASE_ABI) >= RTLNX_UBUNTU_ABI(a_iAbi)) + +/** @def RTLNX_UBUNTU_ABI_MAX + * Require Ubuntu release ABI to be older than specified version. + * + * The kernel version should exactly match the specified @a a_iMajor, @a + * a_iMinor and @a a_iPatch. The @a a_iAbi number should be less than the + * current ABI version. + * + * @param a_iMajor The major kernel version number. + * @param a_iMinor The minor kernel version number. + * @param a_iPatch The kernel patch level. + * @param a_iAbi Ubuntu kernel ABI version number (exclusive). + */ +# define RTLNX_UBUNTU_ABI_MAX(a_iMajor, a_iMinor, a_iPatch, a_iAbi) \ + ( KERNEL_VERSION(a_iMajor, a_iMinor, a_iPatch) == LINUX_VERSION_CODE \ + && RTLNX_UBUNTU_ABI(UTS_UBUNTU_RELEASE_ABI) < RTLNX_UBUNTU_ABI(a_iAbi)) + +/** @def RTLNX_UBUNTU_ABI_RANGE + * Require Ubuntu release ABI to be in specified range. + * + * The kernel version should exactly match the specified @a a_iMajor, @a + * a_iMinor and @a a_iPatch. The numbers @a a_iAbiMin and @a a_iAbiMax specify + * ABI versions range. The max ABI version is exclusive, the minimum inclusive. + * + * @param a_iMajor The major kernel version number. + * @param a_iMinor The minor kernel version number. + * @param a_iPatch The kernel patch level. + * @param a_iAbiMin The minimum Ubuntu kernel ABI version number (inclusive). + * @param a_iAbiMax The maximum Ubuntu kernel ABI version number (exclusive). + */ +# define RTLNX_UBUNTU_ABI_RANGE(a_iMajor, a_iMinor, a_iPatch, a_iAbiMin, a_iAbiMax) \ + ( RTLNX_UBUNTU_ABI_MIN(a_iMajor, a_iMinor, a_iPatch, a_iAbiMin) \ + && RTLNX_UBUNTU_ABI_MAX(a_iMajor, a_iMinor, a_iPatch, a_iAbiMax)) + +#else /* !UTS_UBUNTU_RELEASE_ABI */ + +# define RTLNX_UBUNTU_ABI_MIN(a_iMajor, a_iMinor, a_iPatch, a_iAbi) (0) +# define RTLNX_UBUNTU_ABI_MAX(a_iMajor, a_iMinor, a_iPatch, a_iAbi) (0) +# define RTLNX_UBUNTU_ABI_RANGE(a_iMajorMin, a_iMinorMin, a_iPatchMin, a_iAbiMin, a_iAbiMax) (0) + +#endif /* !UTS_UBUNTU_RELEASE_ABI */ + +#endif /* !IPRT_INCLUDED_linux_version_h */ + |