- Timestamp:
- Apr 7, 2016 2:21:30 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/linux/sysfs.h
r57004 r60373 5 5 6 6 /* 7 * Copyright (C) 2008-201 5Oracle Corporation7 * Copyright (C) 2008-2016 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 44 44 * Checks if a sysfs file (or directory, device, symlink, whatever) exists. 45 45 * 46 * @returns true / false, errno is preserved. 46 * @returns true if the sysfs object exists. 47 * false otherwise or if an error occurred. 47 48 * @param pszFormat The name format, either absolute or relative to "/sys/". 48 49 * @param va The format args. … … 51 52 52 53 /** 54 * Checks if a sysfs object (directory, device, symlink, whatever) exists. 55 * 56 * @returns IPRT status code. 57 * @retval VINF_SUCCESS if the sysfs object exists. 58 * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist. 59 * @param pszFormat The name format, either absolute or relative to "/sys/". 60 * @param va The format args. 61 */ 62 RTDECL(int) RTLinuxSysFsExistsExV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0); 63 64 /** 53 65 * Checks if a sysfs file (or directory, device, symlink, whatever) exists. 54 66 * 55 * @returns true / false, errno is preserved. 67 * @returns true if the sysfs object exists. 68 * false otherwise or if an error occurred. 56 69 * @param pszFormat The name format, either absolute or relative to "/sys/". 57 70 * @param ... The format args. … … 60 73 61 74 /** 75 * Checks if a sysfs object (directory, device, symlink, whatever) exists. 76 * 77 * @returns IPRT status code. 78 * @retval VINF_SUCCESS if the sysfs object exists. 79 * @retval VERR_FILE_NOT_FOUND if the sysfs object does not exist. 80 * @param pszFormat The name format, either absolute or relative to "/sys/". 81 * @param ... The format args. 82 */ 83 RTDECL(int) RTLinuxSysFsExistsEx(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2); 84 85 /** 62 86 * Opens a sysfs file. 63 87 * 64 * @returns The file descriptor. -1 and errno on failure. 88 * @returns IPRT status code. 89 * @param phFile Where to store the file handle on success. 65 90 * @param pszFormat The name format, either absolute or relative to "/sys/". 66 91 * @param va The format args. 67 */ 68 RTDECL(int) RTLinuxSysFsOpenV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0); 92 * 93 * @note Close the file using RTFileClose(). 94 */ 95 RTDECL(int) RTLinuxSysFsOpenV(PRTFILE phFile, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0); 69 96 70 97 /** 71 98 * Opens a sysfs file. 72 99 * 73 * @returns The file descriptor. -1 and errno on failure. 100 * @returns IPRT status code. 101 * @param phFile Where to store the file handle on success. 74 102 * @param pszFormat The name format, either absolute or relative to "/sys/". 75 103 * @param ... The format args. 76 */ 77 RTDECL(int) RTLinuxSysFsOpen(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2); 78 79 /** 80 * Closes a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV. 81 * 82 * @param fd File descriptor returned by RTLinuxSysFsOpen or 83 * RTLinuxSysFsOpenV. 84 */ 85 RTDECL(void) RTLinuxSysFsClose(int fd); 104 * 105 * @note Close the file using RTFileClose(). 106 */ 107 RTDECL(int) RTLinuxSysFsOpen(PRTFILE phFile, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2); 86 108 87 109 /** 88 110 * Reads a string from a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV. 89 111 * 90 * @returns The number of bytes read. -1 and errno on failure.91 * @param fdThe file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.112 * @returns IPRT status code. 113 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV. 92 114 * @param pszBuf Where to store the string. 93 115 * @param cchBuf The size of the buffer. Must be at least 2 bytes. 94 */ 95 RTDECL(ssize_t) RTLinuxSysFsReadStr(int fd, char *pszBuf, size_t cchBuf); 116 * @param pcchRead Where to store the amount of characters read on success - optional. 117 */ 118 RTDECL(int) RTLinuxSysFsReadStr(RTFILE hFile, char *pszBuf, size_t cchBuf, size_t *pcchRead); 96 119 97 120 /** … … 100 123 * 101 124 * @returns IPRT status code. 102 * @param fdThe file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.125 * @param hFile The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV. 103 126 * @param pvBuf Where to store the bits from the file. 104 127 * @param cbBuf The size of the buffer. 105 128 * @param pcbRead Where to return the number of bytes read. Optional. 106 129 */ 107 RTDECL(int) RTLinuxSysFsReadFile( int fd, void *pvBuf, size_t cbBuf, size_t *pcbRead);130 RTDECL(int) RTLinuxSysFsReadFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbRead); 108 131 109 132 /** 110 133 * Reads a number from a sysfs file. 111 134 * 112 * @returns 64-bit signed value on success, -1 and errno on failure.135 * @returns IPRT status code. 113 136 * @param uBase The number base, 0 for autodetect. 137 * @param pi64 Where to store the 64-bit signed on success. 114 138 * @param pszFormat The filename format, either absolute or relative to "/sys/". 115 139 * @param va Format args. 116 140 */ 117 RTDECL(int 64_t) RTLinuxSysFsReadIntFileV(unsigned uBase, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);141 RTDECL(int) RTLinuxSysFsReadIntFileV(unsigned uBase, int64_t *pi64, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0); 118 142 119 143 /** 120 144 * Reads a number from a sysfs file. 121 145 * 122 * @returns 64-bit signed value on success, -1 and errno on failure.146 * @returns IPRT status code. 123 147 * @param uBase The number base, 0 for autodetect. 124 * @param pszFormat The filename format, either absolute or relative to "/sys/". 125 * @param ... Format args. 126 */ 127 RTDECL(int64_t) RTLinuxSysFsReadIntFile(unsigned uBase, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3); 148 * @param pi64 Where to store the 64-bit signed on success. 149 * @param pszFormat The filename format, either absolute or relative to "/sys/". 150 * @param ... Format args. 151 */ 152 RTDECL(int) RTLinuxSysFsReadIntFile(unsigned uBase, int64_t *pi64, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3); 128 153 129 154 /** 130 155 * Reads a device number from a sysfs file. 131 156 * 132 * @returns device number on success, 0 and errno on failure. 157 * @returns IPRT status code. 158 * @param pDevNum Where to store the device number on success. 133 159 * @param pszFormat The filename format, either absolute or relative to "/sys/". 134 160 * @param va Format args. 135 161 */ 136 RTDECL( dev_t) RTLinuxSysFsReadDevNumFileV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);162 RTDECL(int) RTLinuxSysFsReadDevNumFileV(dev_t *pDevNum, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0); 137 163 138 164 /** 139 165 * Reads a device number from a sysfs file. 140 166 * 141 * @returns device number on success, 0 and errno on failure. 142 * @param pszFormat The filename format, either absolute or relative to "/sys/". 143 * @param ... Format args. 144 */ 145 RTDECL(dev_t) RTLinuxSysFsReadDevNumFile(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2); 167 * @returns IPRT status code. 168 * @param pDevNum Where to store the device number on success. 169 * @param pszFormat The filename format, either absolute or relative to "/sys/". 170 * @param ... Format args. 171 */ 172 RTDECL(int) RTLinuxSysFsReadDevNumFile(dev_t *pDevNum, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2); 146 173 147 174 /** … … 149 176 * return the text up until there. 150 177 * 151 * @returns number of characters read on success, -1 and errno on failure. 152 * @param pszBuf Where to store the path element. Must be at least two 153 * characters, but a longer buffer would be advisable. 154 * @param cchBuf The size of the buffer pointed to by @a pszBuf. 178 * @returns IPRT status code. 179 * @param pszBuf Where to store the path element. Must be at least two 180 * characters, but a longer buffer would be advisable. 181 * @param cchBuf The size of the buffer pointed to by @a pszBuf. 182 * @param pcchRead Where to store the amount of characters read on success - optional. 155 183 * @param pszFormat The filename format, either absolute or relative to "/sys/". 156 184 * @param va Format args. 157 185 */ 158 RTDECL( ssize_t) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);186 RTDECL(int) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); 159 187 160 188 /** … … 162 190 * return the text up until there. 163 191 * 164 * @returns number of characters read on success, -1 and errno on failure. 165 * @param pszBuf Where to store the path element. Must be at least two 166 * characters, but a longer buffer would be advisable. 167 * @param cchBuf The size of the buffer pointed to by @a pszBuf. 168 * @param pszFormat The filename format, either absolute or relative to "/sys/". 169 * @param ... Format args. 170 */ 171 RTDECL(ssize_t) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); 192 * @returns IPRT status code. 193 * @param pszBuf Where to store the path element. Must be at least two 194 * characters, but a longer buffer would be advisable. 195 * @param cchBuf The size of the buffer pointed to by @a pszBuf. 196 * @param pcchRead Where to store the amount of characters read on success - optional. 197 * @param pszFormat The filename format, either absolute or relative to "/sys/". 198 * @param ... Format args. 199 */ 200 RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); 172 201 173 202 /** … … 179 208 * directory. 180 209 * 181 * @returns The length of the returned string on success, -1 and errno on182 * failure.183 * @param pszBuf Where to store the path element. Must be at least two184 * characters, but a longer buffer would be advisable.185 * @param cchBuf The size of the buffer pointed to by @a pszBuf.210 * @returns IPRT status code. 211 * @param pszBuf Where to store the path element. Must be at least two 212 * characters, but a longer buffer would be advisable. 213 * @param cchBuf The size of the buffer pointed to by @a pszBuf. 214 * @param pchBuf Where to store the length of the returned string on success - optional. 186 215 * @param pszFormat The filename format, either absolute or relative to "/sys/". 187 216 * @param va Format args. 188 217 */ 189 RTDECL( ssize_t) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);218 RTDECL(int) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(3, 0); 190 219 191 220 /** … … 197 226 * directory. 198 227 * 199 * @returns The length of the returned string on success, -1 and errno on200 * failure.201 * @param pszBuf Where to store the path element. Must be at least two202 * characters, but a longer buffer would be advisable.203 * @param cchBuf The size of the buffer pointed to by @a pszBuf.204 * @param pszFormat The filename format, either absolute or relative to "/sys/". 205 * @param ... Format args. 206 */ 207 RTDECL( ssize_t) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4);228 * @returns IPRT status code. 229 * @param pszBuf Where to store the path element. Must be at least two 230 * characters, but a longer buffer would be advisable. 231 * @param cchBuf The size of the buffer pointed to by @a pszBuf. 232 * @param pchBuf Where to store the length of the returned string on success - optional. 233 * @param pszFormat The filename format, either absolute or relative to "/sys/". 234 * @param ... Format args. 235 */ 236 RTDECL(int) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(3, 4); 208 237 209 238 /** … … 211 240 * pattern and store the path into @a pszBuf. 212 241 * 213 * @returns The length of the returned string on success, -1 and errno on 214 * failure. 215 * @returns -1 and ENOENT if no matching device node could be found. 242 * @returns IPRT status code. 243 * @retval VERR_FILE_NOT_FOUND if no matching device node could be found. 216 244 * @param DevNum The device number to search for. 217 245 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and … … 223 251 * @param va Format args. 224 252 */ 225 RTDECL( ssize_t) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,226 253 RTDECL(int) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf, 254 const char *pszPattern, va_list va) RT_IPRT_FORMAT_ATTR(5, 0); 227 255 228 256 /** … … 230 258 * pattern and store the path into @a pszBuf. 231 259 * 232 * @returns The length of the returned string on success, -1 and errno on 233 * failure. 234 * @returns -1 and ENOENT if no matching device node could be found. 260 * @returns IPRT status code. 261 * @retval VERR_FILE_NOT_FOUND if no matching device node could be found. 235 262 * @param DevNum The device number to search for 236 263 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and … … 242 269 * @param ... Format args. 243 270 */ 244 RTDECL( ssize_t) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,245 271 RTDECL(int) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf, 272 const char *pszPattern, ...) RT_IPRT_FORMAT_ATTR(5, 6); 246 273 247 274 /** @} */ -
trunk/include/iprt/mangling.h
r60245 r60373 996 996 # define RTLinuxSysFsClose RT_MANGLER(RTLinuxSysFsClose) 997 997 # define RTLinuxSysFsExists RT_MANGLER(RTLinuxSysFsExists) 998 # define RTLinuxSysFsExistsEx RT_MANGLER(RTLinuxSysFsExistsEx) 999 # define RTLinuxSysFsExistsExV RT_MANGLER(RTLinuxSysFsExistsExV) 998 1000 # define RTLinuxSysFsExistsV RT_MANGLER(RTLinuxSysFsExistsV) 999 1001 # define RTLinuxSysFsGetLinkDest RT_MANGLER(RTLinuxSysFsGetLinkDest) -
trunk/src/VBox/Devices/USB/linux/USBProxyDevice-linux.cpp
r58640 r60373 552 552 ? pProxyDev->paCfgDescs[0].Core.bConfigurationValue 553 553 : 1; 554 return RTLinuxSysFsReadIntFile(10, "%s/bConfigurationValue", pszPath); /* returns -1 on failure */ 554 int64_t bCfg = 0; 555 int rc = RTLinuxSysFsReadIntFile(10, &bCfg, "%s/bConfigurationValue", pszPath); 556 if (RT_FAILURE(rc)) 557 bCfg = -1; 558 return (int)bCfg; 555 559 #else /* !VBOX_USB_WITH_SYSFS */ 556 560 return -1; -
trunk/src/VBox/Main/src-server/linux/HostHardwareLinux.cpp
r59960 r60373 610 610 bool findDeviceNode() 611 611 { 612 dev_t dev = RTLinuxSysFsReadDevNumFile("block/%s/dev", mpcszName); 613 if (dev == 0) 612 dev_t dev = 0; 613 int rc = RTLinuxSysFsReadDevNumFile(&dev, "block/%s/dev", mpcszName); 614 if (RT_FAILURE(rc) || dev == 0) 614 615 { 615 616 misConsistent = false; 616 617 return false; 617 618 } 618 if (RTLinuxCheckDevicePath(dev, RTFS_TYPE_DEV_BLOCK, mszNode, 619 sizeof(mszNode), "%s", mpcszName) < 0) 619 rc = RTLinuxCheckDevicePath(dev, RTFS_TYPE_DEV_BLOCK, mszNode, 620 sizeof(mszNode), "%s", mpcszName); 621 if (RT_FAILURE(rc)) 620 622 return false; 621 623 return true; … … 630 632 { 631 633 char szVendor[128], szModel[128]; 632 ssize_t cchVendor, cchModel; 633 int64_t type = RTLinuxSysFsReadIntFile(10, "block/%s/device/type", 634 mpcszName); 635 if (type >= 0 && type != TYPE_ROM) 634 int64_t type = 0; 635 int rc = RTLinuxSysFsReadIntFile(10, &type, "block/%s/device/type", mpcszName); 636 if (RT_SUCCESS(rc) && type != TYPE_ROM) 636 637 return; 637 638 if (type == TYPE_ROM) 638 639 { 639 cchVendor = RTLinuxSysFsReadStrFile(szVendor, sizeof(szVendor), 640 "block/%s/device/vendor", 641 mpcszName); 642 if (cchVendor >= 0) 640 rc = RTLinuxSysFsReadStrFile(szVendor, sizeof(szVendor), NULL, 641 "block/%s/device/vendor", mpcszName); 642 if (RT_SUCCESS(rc)) 643 643 { 644 cchModel = RTLinuxSysFsReadStrFile(szModel, sizeof(szModel), 645 "block/%s/device/model", 646 mpcszName); 647 if (cchModel >= 0) 644 rc = RTLinuxSysFsReadStrFile(szModel, sizeof(szModel), NULL, 645 "block/%s/device/model", mpcszName); 646 if (RT_SUCCESS(rc)) 648 647 { 649 648 misValid = true; … … 697 696 if (!noProbe()) 698 697 haveName = floppyGetName(mszNode, mpcszName[2] - '0', szName); 699 if (RTLinuxSysFsGetLinkDest(szDriver, sizeof(szDriver), "block/%s/%s", 700 mpcszName, "device/driver") >= 0) 698 int rc = RTLinuxSysFsGetLinkDest(szDriver, sizeof(szDriver), NULL, "block/%s/%s", 699 mpcszName, "device/driver"); 700 if (RT_SUCCESS(rc)) 701 701 { 702 702 if (RTStrCmp(szDriver, "floppy")) -
trunk/src/VBox/Main/src-server/linux/PerformanceLinux.cpp
r56030 r60373 253 253 else 254 254 { 255 int64_t cSize = RTLinuxSysFsReadIntFile(0, pszPath); 256 if (cSize < 0) 257 rc = VERR_ACCESS_DENIED; 258 else 255 int64_t cSize = 0; 256 rc = RTLinuxSysFsReadIntFile(0, &cSize, pszPath); 257 if (RT_SUCCESS(rc)) 259 258 *size = cSize * 512; 260 259 } … … 325 324 return VERR_FILE_NOT_FOUND; 326 325 327 int64_t cSize = RTLinuxSysFsReadIntFile(0, szIfName); 328 if (cSize < 0) 329 return VERR_ACCESS_DENIED; 326 int64_t cSize = 0; 327 int rc = RTLinuxSysFsReadIntFile(0, &cSize, szIfName); 328 if (RT_FAILURE(rc)) 329 return rc; 330 330 331 331 *rx = cSize; … … 335 335 return VERR_FILE_NOT_FOUND; 336 336 337 cSize = RTLinuxSysFsReadIntFile(0, szIfName);338 if ( cSize < 0)339 return VERR_ACCESS_DENIED;337 rc = RTLinuxSysFsReadIntFile(0, &cSize, szIfName); 338 if (RT_FAILURE(rc)) 339 return rc; 340 340 341 341 *tx = cSize; -
trunk/src/VBox/Main/src-server/linux/USBGetDevices.cpp
r60154 r60373 879 879 return VINF_SUCCESS; 880 880 881 int device = RTLinuxSysFsReadIntFile(10, "%s/devnum", pcszNode); 882 if (device < 0) 881 int64_t device; 882 int rc = RTLinuxSysFsReadIntFile(10, &device, "%s/devnum", pcszNode); 883 if (RT_FAILURE(rc)) 883 884 return VINF_SUCCESS; 884 885 885 dev_t devnum = usbsysfsMakeDevNum(bus, device);886 dev_t devnum = usbsysfsMakeDevNum(bus, (int)device); 886 887 if (!devnum) 887 888 return VINF_SUCCESS; 888 889 889 890 char szDevPath[RTPATH_MAX]; 890 ssize_t cchDevPath; 891 cchDevPath = RTLinuxCheckDevicePath(devnum, RTFS_TYPE_DEV_CHAR, 892 szDevPath, sizeof(szDevPath), 893 "%s/%.3d/%.3d", 894 pcszDevicesRoot, bus, device); 895 if (cchDevPath < 0) 891 rc = RTLinuxCheckDevicePath(devnum, RTFS_TYPE_DEV_CHAR, 892 szDevPath, sizeof(szDevPath), 893 "%s/%.3d/%.3d", 894 pcszDevicesRoot, bus, device); 895 if (RT_FAILURE(rc)) 896 896 return VINF_SUCCESS; 897 897 … … 899 899 if (usbsysfsInitDevInfo(&info, szDevPath, pcszNode)) 900 900 { 901 intrc = VEC_PUSH_BACK_OBJ(pvecDevInfo, USBDeviceInfo, &info);901 rc = VEC_PUSH_BACK_OBJ(pvecDevInfo, USBDeviceInfo, &info); 902 902 if (RT_SUCCESS(rc)) 903 903 return VINF_SUCCESS; … … 1227 1227 1228 1228 1229 /** 1230 * Returns the byte value for the given device property or sets the given default if an 1231 * error occurs while obtaining it. 1232 * 1233 * @returns uint8_t value of the given property. 1234 * @param uBase The base of the number in the sysfs property. 1235 * @param bDef The default to set on error. 1236 * @param pszFormat The format string for the property. 1237 * @param ... Arguments for the format string. 1238 */ 1239 static uint8_t usbsysfsReadDevicePropertyU8Def(unsigned uBase, uint8_t bDef, const char *pszFormat, ...) 1240 { 1241 int64_t i64Tmp = 0; 1242 1243 va_list va; 1244 va_start(va, pszFormat); 1245 int rc = RTLinuxSysFsReadIntFileV(uBase, &i64Tmp, pszFormat, va); 1246 va_end(va); 1247 if (RT_SUCCESS(rc)) 1248 return (uint8_t)i64Tmp; 1249 else 1250 return bDef; 1251 } 1252 1253 1254 /** 1255 * Returns the uint16_t value for the given device property or sets the given default if an 1256 * error occurs while obtaining it. 1257 * 1258 * @returns uint16_t value of the given property. 1259 * @param uBase The base of the number in the sysfs property. 1260 * @param u16Def The default to set on error. 1261 * @param pszFormat The format string for the property. 1262 * @param ... Arguments for the format string. 1263 */ 1264 static uint8_t usbsysfsReadDevicePropertyU16Def(unsigned uBase, uint16_t u16Def, const char *pszFormat, ...) 1265 { 1266 int64_t i64Tmp = 0; 1267 1268 va_list va; 1269 va_start(va, pszFormat); 1270 int rc = RTLinuxSysFsReadIntFileV(uBase, &i64Tmp, pszFormat, va); 1271 va_end(va); 1272 if (RT_SUCCESS(rc)) 1273 return (uint16_t)i64Tmp; 1274 else 1275 return u16Def; 1276 } 1277 1278 1229 1279 static void usbsysfsFillInDevice(USBDEVICE *pDev, USBDeviceInfo *pInfo) 1230 1280 { … … 1235 1285 pDev->enmState = USBDEVICESTATE_UNUSED; 1236 1286 pDev->bBus = usbsysfsGetBusFromPath(pszSysfsPath); 1237 pDev->bDeviceClass = RTLinuxSysFsReadIntFile(16, "%s/bDeviceClass", pszSysfsPath);1238 pDev->bDeviceSubClass = RTLinuxSysFsReadIntFile(16, "%s/bDeviceSubClass", pszSysfsPath);1239 pDev->bDeviceProtocol = RTLinuxSysFsReadIntFile(16, "%s/bDeviceProtocol", pszSysfsPath);1240 pDev->bNumConfigurations = RTLinuxSysFsReadIntFile(10, "%s/bNumConfigurations", pszSysfsPath);1241 pDev->idVendor = RTLinuxSysFsReadIntFile(16, "%s/idVendor", pszSysfsPath);1242 pDev->idProduct = RTLinuxSysFsReadIntFile(16, "%s/idProduct", pszSysfsPath);1243 pDev->bDevNum = RTLinuxSysFsReadIntFile(10, "%s/devnum", pszSysfsPath);1287 pDev->bDeviceClass = usbsysfsReadDevicePropertyU8Def(16, 0, "%s/bDeviceClass", pszSysfsPath); 1288 pDev->bDeviceSubClass = usbsysfsReadDevicePropertyU8Def(16, 0, "%s/bDeviceSubClass", pszSysfsPath); 1289 pDev->bDeviceProtocol = usbsysfsReadDevicePropertyU8Def(16, 0, "%s/bDeviceProtocol", pszSysfsPath); 1290 pDev->bNumConfigurations = usbsysfsReadDevicePropertyU8Def(10, 0, "%s/bNumConfigurations", pszSysfsPath); 1291 pDev->idVendor = usbsysfsReadDevicePropertyU16Def(16, 0, "%s/idVendor", pszSysfsPath); 1292 pDev->idProduct = usbsysfsReadDevicePropertyU16Def(16, 0, "%s/idProduct", pszSysfsPath); 1293 pDev->bDevNum = usbsysfsReadDevicePropertyU8Def(10, 0, "%s/devnum", pszSysfsPath); 1244 1294 1245 1295 /* Now deal with the non-numeric bits. */ … … 1247 1297 * will need, and insane devices can be unsupported 1248 1298 * until further notice. */ 1249 s size_t cchRead;1299 size_t cchRead; 1250 1300 1251 1301 /* For simplicity, we just do strcmps on the next one. */ 1252 cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/speed", pszSysfsPath);1253 if ( cchRead <= 0 || (size_t)cchRead == sizeof(szBuf))1302 rc = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), &cchRead, "%s/speed", pszSysfsPath); 1303 if (RT_FAILURE(rc) || cchRead == sizeof(szBuf)) 1254 1304 pDev->enmState = USBDEVICESTATE_UNSUPPORTED; 1255 1305 else … … 1260 1310 : USBDEVICESPEED_UNKNOWN; 1261 1311 1262 cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/version", pszSysfsPath);1263 if ( cchRead <= 0 || (size_t)cchRead == sizeof(szBuf))1312 rc = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), &cchRead, "%s/version", pszSysfsPath); 1313 if (RT_FAILURE(rc) || cchRead == sizeof(szBuf)) 1264 1314 pDev->enmState = USBDEVICESTATE_UNSUPPORTED; 1265 1315 else … … 1273 1323 } 1274 1324 1275 cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/bcdDevice", pszSysfsPath);1276 if ( cchRead <= 0 || (size_t)cchRead == sizeof(szBuf))1325 rc = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), &cchRead, "%s/bcdDevice", pszSysfsPath); 1326 if (RT_FAILURE(rc) || cchRead == sizeof(szBuf)) 1277 1327 pDev->bcdDevice = UINT16_MAX; 1278 1328 else … … 1284 1334 1285 1335 /* Now do things that need string duplication */ 1286 cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/product", pszSysfsPath);1287 if ( cchRead > 0 && (size_t)cchRead < sizeof(szBuf))1336 rc = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), &cchRead, "%s/product", pszSysfsPath); 1337 if (RT_SUCCESS(rc) && cchRead < sizeof(szBuf)) 1288 1338 { 1289 1339 USBLibPurgeEncoding(szBuf); … … 1291 1341 } 1292 1342 1293 cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/serial", pszSysfsPath);1294 if ( cchRead > 0 && (size_t)cchRead < sizeof(szBuf))1343 rc = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), &cchRead, "%s/serial", pszSysfsPath); 1344 if (RT_SUCCESS(rc) && cchRead < sizeof(szBuf)) 1295 1345 { 1296 1346 USBLibPurgeEncoding(szBuf); … … 1299 1349 } 1300 1350 1301 cchRead = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), "%s/manufacturer", pszSysfsPath);1302 if ( cchRead > 0 && (size_t)cchRead < sizeof(szBuf))1351 rc = RTLinuxSysFsReadStrFile(szBuf, sizeof(szBuf), &cchRead, "%s/manufacturer", pszSysfsPath); 1352 if (RT_SUCCESS(rc) && cchRead < sizeof(szBuf)) 1303 1353 { 1304 1354 USBLibPurgeEncoding(szBuf); … … 1314 1364 VEC_FOR_EACH(&pInfo->mvecpszInterfaces, char *, ppszIf) 1315 1365 { 1316 ssize_t cb = RTLinuxSysFsGetLinkDest(szBuf, sizeof(szBuf), "%s/driver", *ppszIf);1317 if ( cb > 0&& pDev->enmState != USBDEVICESTATE_UNSUPPORTED)1366 rc = RTLinuxSysFsGetLinkDest(szBuf, sizeof(szBuf), NULL, "%s/driver", *ppszIf); 1367 if (RT_SUCCESS(rc) && pDev->enmState != USBDEVICESTATE_UNSUPPORTED) 1318 1368 pDev->enmState = (strcmp(szBuf, "hub") == 0) 1319 1369 ? USBDEVICESTATE_UNSUPPORTED 1320 1370 : USBDEVICESTATE_USED_BY_HOST_CAPTURABLE; 1321 if ( RTLinuxSysFsReadIntFile(16, "%s/bInterfaceClass", *ppszIf) == 9 /* hub */)1371 if (usbsysfsReadDevicePropertyU8Def(16, 9 /* bDev */, "%s/bInterfaceClass", *ppszIf) == 9 /* hub */) 1322 1372 pDev->enmState = USBDEVICESTATE_UNSUPPORTED; 1323 1373 } -
trunk/src/VBox/Runtime/r3/linux/RTSystemQueryDmiString-linux.cpp
r57358 r60373 58 58 } 59 59 60 int rc;61 int fd = RTLinuxSysFsOpen("devices/virtual/dmi/%s", pszSysFsName);62 if ( fd < 0)63 fd = RTLinuxSysFsOpen("class/dmi/%s", pszSysFsName);64 if ( fd >= 0)60 size_t cbRead = 0; 61 int rc = RTLinuxSysFsReadStrFile(pszBuf, cbBuf, &cbRead, "devices/virtual/dmi/%s", pszSysFsName); 62 if (RT_FAILURE(rc) && rc != VERR_BUFFER_OVERFLOW) 63 rc = RTLinuxSysFsReadStrFile(pszBuf, cbBuf, &cbRead, "class/dmi/%s", pszSysFsName); 64 if (RT_FAILURE(rc) && rc != VERR_BUFFER_OVERFLOW) 65 65 { 66 size_t cbRead;67 rc = RTLinuxSysFsReadFile(fd, pszBuf, cbBuf, &cbRead);68 if (RT_SUCCESS(rc) || rc == VERR_BUFFER_OVERFLOW)69 {70 /* The file we're reading may end with a newline, remove it. */71 if (cbRead == cbBuf)72 pszBuf[cbRead - 1] = '\0';73 else74 {75 AssertRC(rc);76 pszBuf[cbRead] = '\0';77 if (cbRead > 0 && pszBuf[cbRead - 1] == '\n')78 pszBuf[cbRead - 1] = '\0';79 }80 }81 RTLinuxSysFsClose(fd);82 }83 else84 {85 rc = RTErrConvertFromErrno(errno);86 66 switch (rc) 87 67 { -
trunk/src/VBox/Runtime/r3/linux/mp-linux.cpp
r57358 r60373 139 139 { 140 140 /** @todo check if there is a simpler interface than this... */ 141 int i = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/online", (int)idCpu); 142 if ( i == -1 141 int64_t i = 0; 142 int rc = RTLinuxSysFsReadIntFile(0, &i, "devices/system/cpu/cpu%d/online", (int)idCpu); 143 if ( RT_FAILURE(rc) 143 144 && RTLinuxSysFsExists("devices/system/cpu/cpu%d", (int)idCpu)) 144 145 { … … 148 149 * 2.6.18-164). */ 149 150 i = 1; 151 rc = VINF_SUCCESS; 150 152 } 151 153 152 154 AssertMsg(i == 0 || i == -1 || i == 1, ("i=%d\n", i)); 153 return i != 0 && i != -1;155 return RT_SUCCESS(rc) && i != 0; 154 156 } 155 157 … … 191 193 if (RTMpIsCpuPossible(idCpu)) 192 194 { 193 uint32_t idCore = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);194 uint32_t idPckg = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/physical_package_id", (int)idCpu);195 uint32_t i; 196 for (i = 0; i < cCores; i++)197 if ( paidCores[i] == idCore198 && paidPckgs[i] == idPckg)199 break; 200 if ( i >= cCores)195 int64_t idCore = 0; 196 int64_t idPckg = 0; 197 198 int rc = RTLinuxSysFsReadIntFile(0, &idCore, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu); 199 if (RT_SUCCESS(rc)) 200 rc = RTLinuxSysFsReadIntFile(0, &idPckg, "devices/system/cpu/cpu%d/topology/physical_package_id", (int)idCpu); 201 202 if (RT_SUCCESS(rc)) 201 203 { 202 paidCores[cCores] = idCore; 203 paidPckgs[cCores] = idPckg; 204 cCores++; 204 uint32_t i; 205 206 for (i = 0; i < cCores; i++) 207 if ( paidCores[i] == (uint32_t)idCore 208 && paidPckgs[i] == (uint32_t)idPckg) 209 break; 210 if (i >= cCores) 211 { 212 paidCores[cCores] = (uint32_t)idCore; 213 paidPckgs[cCores] = (uint32_t)idPckg; 214 cCores++; 215 } 205 216 } 206 217 } … … 240 251 if (RTMpIsCpuOnline(idCpu)) 241 252 { 242 uint32_t idCore = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu);243 uint32_t idPckg = (uint32_t)RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/topology/physical_package_id", (int)idCpu);244 uint32_t i; 245 for (i = 0; i < cCores; i++)246 if ( paidCores[i] == idCore247 && paidPckgs[i] == idPckg)248 break; 249 if ( i >= cCores)253 int64_t idCore = 0; 254 int64_t idPckg = 0; 255 256 int rc = RTLinuxSysFsReadIntFile(0, &idCore, "devices/system/cpu/cpu%d/topology/core_id", (int)idCpu); 257 if (RT_SUCCESS(rc)) 258 rc = RTLinuxSysFsReadIntFile(0, &idPckg, "devices/system/cpu/cpu%d/topology/physical_package_id", (int)idCpu); 259 260 if (RT_SUCCESS(rc)) 250 261 { 251 paidCores[cCores] = idCore; 252 paidPckgs[cCores] = idPckg; 253 cCores++; 262 uint32_t i; 263 264 for (i = 0; i < cCores; i++) 265 if ( paidCores[i] == idCore 266 && paidPckgs[i] == idPckg) 267 break; 268 if (i >= cCores) 269 { 270 paidCores[cCores] = idCore; 271 paidPckgs[cCores] = idPckg; 272 cCores++; 273 } 254 274 } 255 275 } … … 263 283 RTDECL(uint32_t) RTMpGetCurFrequency(RTCPUID idCpu) 264 284 { 265 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", (int)idCpu); 266 if (kHz == -1) 285 int64_t kHz = 0; 286 int rc = RTLinuxSysFsReadIntFile(0, &kHz, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_cur_freq", (int)idCpu); 287 if (RT_FAILURE(rc)) 267 288 { 268 289 /* … … 281 302 RTDECL(uint32_t) RTMpGetMaxFrequency(RTCPUID idCpu) 282 303 { 283 int64_t kHz = RTLinuxSysFsReadIntFile(0, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu); 284 if (kHz == -1) 304 int64_t kHz = 0; 305 int rc = RTLinuxSysFsReadIntFile(0, &kHz, "devices/system/cpu/cpu%d/cpufreq/cpuinfo_max_freq", (int)idCpu); 306 if (RT_FAILURE(rc)) 285 307 { 286 308 /* -
trunk/src/VBox/Runtime/r3/linux/sysfs.cpp
r57358 r60373 5 5 6 6 /* 7 * Copyright (C) 2006-201 5Oracle Corporation7 * Copyright (C) 2006-2016 Oracle Corporation 8 8 * 9 9 * This file is part of VirtualBox Open Source Edition (OSE), as … … 30 30 *********************************************************************************************************************************/ 31 31 #define LOG_GROUP RTLOGGROUP_SYSTEM 32 #include <iprt/linux/sysfs.h>33 32 #include <iprt/assert.h> 34 33 #include <iprt/dir.h> 35 34 #include <iprt/err.h> 35 #include <iprt/file.h> 36 36 #include <iprt/fs.h> 37 37 #include <iprt/param.h> 38 38 #include <iprt/path.h> 39 39 #include <iprt/string.h> 40 #include <iprt/symlink.h> 41 42 #include <iprt/linux/sysfs.h> 40 43 41 44 #include <unistd.h> … … 46 49 #include <errno.h> 47 50 48 /** @todo r=bird: This whole API should be rewritten to use IPRT status codes.49 * using errno was a mistake and only results in horrible code. */50 51 51 52 … … 54 55 * prepending a prefix if the path is relative. 55 56 * 56 * @returns The number of characters returned, or an iprt error code on failure. 57 * 57 * @returns IPRT status code. 58 58 * @param pszPrefix The prefix to prepend if the path is relative. Must end 59 59 * in '/'. … … 65 65 * @param va The format args. 66 66 */ 67 static ssize_t rtLinuxConstructPathV(char *pszBuf, size_t cchBuf,67 static int rtLinuxConstructPathV(char *pszBuf, size_t cchBuf, 68 68 const char *pszPrefix, 69 69 const char *pszFormat, va_list va) … … 85 85 cch += cchPrefix; 86 86 } 87 return cch;87 return VINF_SUCCESS; 88 88 } 89 89 … … 93 93 * prepending a prefix if the path is relative. 94 94 * 95 * @returns The number of characters returned, or an iprt error code on failure. 96 * @note Unused. 97 * 95 * @returns IPRT status code. 98 96 * @param pszPrefix The prefix to prepend if the path is relative. Must end 99 97 * in '/'. … … 104 102 * @param ... The format args. 105 103 */ 106 static ssize_trtLinuxConstructPath(char *pszBuf, size_t cchBuf,107 const char *pszPrefix,108 const char *pszFormat, ...)104 DECLINLINE(int) rtLinuxConstructPath(char *pszBuf, size_t cchBuf, 105 const char *pszPrefix, 106 const char *pszFormat, ...) 109 107 { 110 108 va_list va; … … 120 118 * prepending "/sys/" if the path is relative. 121 119 * 122 * @returns The number of characters returned, or -1 and errno set to ERANGE on 123 * failure. 124 * 120 * @returns IPRT status code. 125 121 * @param pszBuf Where to write the path. Must be at least 126 122 * sizeof("/sys/") characters long … … 129 125 * @param va The format args. 130 126 */ 131 static ssize_t rtLinuxSysFsConstructPath(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va) 132 { 133 ssize_t rc = rtLinuxConstructPathV(pszBuf, cchBuf, "/sys/", pszFormat, va); 134 if (rc >= 0) 135 return rc; 136 errno = ERANGE; 137 return -1; 138 } 139 140 141 RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va) 127 DECLINLINE(int) rtLinuxSysFsConstructPath(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va) 128 { 129 return rtLinuxConstructPathV(pszBuf, cchBuf, "/sys/", pszFormat, va); 130 } 131 132 133 RTDECL(int) RTLinuxSysFsExistsExV(const char *pszFormat, va_list va) 142 134 { 143 135 int iSavedErrno = errno; … … 146 138 * Construct the filename and call stat. 147 139 */ 148 bool fRet = false;149 140 char szFilename[RTPATH_MAX]; 150 ssize_t rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);151 if ( rc != -1)141 int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va); 142 if (RT_SUCCESS(rc)) 152 143 { 153 144 struct stat st; 154 fRet = stat(szFilename, &st) == 0; 145 int rcStat = stat(szFilename, &st); 146 if (rcStat != 0) 147 rc = RTErrConvertFromErrno(errno); 155 148 } 156 149 157 150 errno = iSavedErrno; 151 return rc; 152 } 153 154 155 RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va) 156 { 157 return RT_SUCCESS(RTLinuxSysFsExistsExV(pszFormat, va)); 158 } 159 160 161 RTDECL(int) RTLinuxSysFsExistsEx(const char *pszFormat, ...) 162 { 163 va_list va; 164 va_start(va, pszFormat); 165 int rc = RTLinuxSysFsExistsExV(pszFormat, va); 166 va_end(va); 167 return rc; 168 } 169 170 171 RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...) 172 { 173 va_list va; 174 va_start(va, pszFormat); 175 bool fRet = RTLinuxSysFsExistsV(pszFormat, va); 176 va_end(va); 158 177 return fRet; 159 178 } 160 179 161 180 162 RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...) 163 { 164 va_list va; 165 va_start(va, pszFormat); 166 bool fRet = RTLinuxSysFsExistsV(pszFormat, va); 167 va_end(va); 168 return fRet; 169 } 170 171 172 RTDECL(int) RTLinuxSysFsOpenV(const char *pszFormat, va_list va) 181 RTDECL(int) RTLinuxSysFsOpenV(PRTFILE phFile, const char *pszFormat, va_list va) 173 182 { 174 183 /* … … 176 185 */ 177 186 char szFilename[RTPATH_MAX]; 178 ssize_t rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va); 179 if (rc != -1) 180 rc = open(szFilename, O_RDONLY, 0); 181 return rc; 182 } 183 184 185 RTDECL(int) RTLinuxSysFsOpen(const char *pszFormat, ...) 186 { 187 va_list va; 188 va_start(va, pszFormat); 189 int fd = RTLinuxSysFsOpenV(pszFormat, va); 190 va_end(va); 191 return fd; 192 } 193 194 195 RTDECL(void) RTLinuxSysFsClose(int fd) 196 { 197 int iSavedErrno = errno; 198 close(fd); 199 errno = iSavedErrno; 200 } 201 202 203 RTDECL(ssize_t) RTLinuxSysFsReadStr(int fd, char *pszBuf, size_t cchBuf) 187 int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va); 188 if (RT_SUCCESS(rc)) 189 rc = RTFileOpen(phFile, szFilename, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE); 190 return rc; 191 } 192 193 194 RTDECL(int) RTLinuxSysFsOpen(PRTFILE phFile, const char *pszFormat, ...) 195 { 196 va_list va; 197 va_start(va, pszFormat); 198 int rc = RTLinuxSysFsOpenV(phFile, pszFormat, va); 199 va_end(va); 200 return rc; 201 } 202 203 204 RTDECL(int) RTLinuxSysFsReadStr(RTFILE hFile, char *pszBuf, size_t cchBuf, size_t *pcchRead) 204 205 { 205 206 Assert(cchBuf > 1); 206 ssize_t cchRead = read(fd, pszBuf, cchBuf - 1); 207 pszBuf[cchRead >= 0 ? cchRead : 0] = '\0'; 208 return cchRead; 209 } 210 211 212 RTDECL(int) RTLinuxSysFsReadFile(int fd, void *pvBuf, size_t cbBuf, size_t *pcbRead) 213 { 214 int rc; 215 ssize_t cbRead = read(fd, pvBuf, cbBuf); 216 if (cbRead >= 0) 207 size_t cchRead = 0; 208 int rc = RTFileRead(hFile, pszBuf, cchBuf - 1, &cchRead); 209 pszBuf[RT_SUCCESS(rc) ? cchRead : 0] = '\0'; 210 if ( RT_SUCCESS(rc) 211 && pcchRead) 212 *pcchRead = cchRead; 213 214 return rc; 215 } 216 217 218 RTDECL(int) RTLinuxSysFsReadFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbRead) 219 { 220 int rc; 221 size_t cbRead = 0; 222 223 rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead); 224 if (RT_SUCCESS(rc)) 217 225 { 218 226 if (pcbRead) 219 227 *pcbRead = cbRead; 220 if ( (size_t)cbRead < cbBuf)228 if (cbRead < cbBuf) 221 229 rc = VINF_SUCCESS; 222 230 else 223 231 { 224 232 /* Check for EOF */ 225 char ch; 226 off_t off = lseek(fd, 0, SEEK_CUR); 227 ssize_t cbRead2 = read(fd, &ch, 1); 228 if (cbRead2 == 0) 229 rc = VINF_SUCCESS; 230 else if (cbRead2 > 0) 233 char ch; 234 uint64_t offCur = 0; 235 uint64_t offEnd = 0; 236 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_CURRENT, &offCur); 237 if (RT_SUCCESS(rc)) 238 rc = RTFileSeek(hFile, 0, RTFILE_SEEK_END, &offEnd); 239 if ( RT_SUCCESS(rc) 240 && offEnd > offCur) 241 rc = VERR_BUFFER_OVERFLOW; 242 } 243 } 244 245 return rc; 246 } 247 248 249 RTDECL(int) RTLinuxSysFsReadIntFileV(unsigned uBase, int64_t *pi64, const char *pszFormat, va_list va) 250 { 251 RTFILE hFile; 252 253 AssertPtrReturn(pi64, VERR_INVALID_POINTER); 254 255 int rc = RTLinuxSysFsOpenV(&hFile, pszFormat, va); 256 if (RT_SUCCESS(rc)) 257 { 258 char szNum[128]; 259 size_t cchNum; 260 rc = RTLinuxSysFsReadStr(hFile, szNum, sizeof(szNum), &cchNum); 261 if (RT_SUCCESS(rc)) 262 { 263 if (cchNum > 0) 231 264 { 232 lseek(fd, off, SEEK_SET); 233 rc = VERR_BUFFER_OVERFLOW; 265 int64_t i64Ret = -1; 266 rc = RTStrToInt64Ex(szNum, NULL, uBase, &i64Ret); 267 if (RT_SUCCESS(rc)) 268 *pi64 = i64Ret; 234 269 } 235 270 else 236 rc = RTErrConvertFromErrno(errno); 237 } 238 } 239 else 240 rc = RTErrConvertFromErrno(errno); 241 return rc; 242 } 243 244 245 RTDECL(int64_t) RTLinuxSysFsReadIntFileV(unsigned uBase, const char *pszFormat, va_list va) 246 { 247 int fd = RTLinuxSysFsOpenV(pszFormat, va); 248 if (fd == -1) 249 return -1; 250 251 int64_t i64Ret = -1; 252 char szNum[128]; 253 ssize_t cchNum = RTLinuxSysFsReadStr(fd, szNum, sizeof(szNum)); 254 if (cchNum > 0) 255 { 256 int rc = RTStrToInt64Ex(szNum, NULL, uBase, &i64Ret); 257 if (RT_FAILURE(rc)) 258 { 259 i64Ret = -1; 260 errno = -ETXTBSY; /* just something that won't happen at read / open. */ 261 } 262 } 263 else if (cchNum == 0) 264 errno = -ETXTBSY; /* just something that won't happen at read / open. */ 265 266 RTLinuxSysFsClose(fd); 267 return i64Ret; 268 } 269 270 271 RTDECL(int64_t) RTLinuxSysFsReadIntFile(unsigned uBase, const char *pszFormat, ...) 272 { 273 va_list va; 274 va_start(va, pszFormat); 275 int64_t i64Ret = RTLinuxSysFsReadIntFileV(uBase, pszFormat, va); 276 va_end(va); 277 return i64Ret; 278 } 279 280 281 RTDECL(dev_t) RTLinuxSysFsReadDevNumFileV(const char *pszFormat, va_list va) 282 { 283 int fd = RTLinuxSysFsOpenV(pszFormat, va); 284 if (fd == -1) 285 return 0; 286 287 dev_t DevNum = 0; 288 char szNum[128]; 289 ssize_t cchNum = RTLinuxSysFsReadStr(fd, szNum, sizeof(szNum)); 290 if (cchNum > 0) 291 { 292 uint32_t u32Maj = 0; 293 uint32_t u32Min = 0; 294 char *pszNext = NULL; 295 int rc = RTStrToUInt32Ex(szNum, &pszNext, 10, &u32Maj); 296 if (RT_FAILURE(rc) || (rc != VWRN_TRAILING_CHARS) || (*pszNext != ':')) 297 errno = EINVAL; 298 else 299 { 300 rc = RTStrToUInt32Ex(pszNext + 1, NULL, 10, &u32Min); 301 if ( rc != VINF_SUCCESS 302 && rc != VWRN_TRAILING_CHARS 303 && rc != VWRN_TRAILING_SPACES) 304 errno = EINVAL; 271 rc = VERR_INVALID_PARAMETER; 272 } 273 274 RTFileClose(hFile); 275 } 276 277 return rc; 278 } 279 280 281 RTDECL(int) RTLinuxSysFsReadIntFile(unsigned uBase, int64_t *pi64, const char *pszFormat, ...) 282 { 283 va_list va; 284 va_start(va, pszFormat); 285 int rc = RTLinuxSysFsReadIntFileV(uBase, pi64, pszFormat, va); 286 va_end(va); 287 return rc; 288 } 289 290 291 RTDECL(int) RTLinuxSysFsReadDevNumFileV(dev_t *pDevNum, const char *pszFormat, va_list va) 292 { 293 RTFILE hFile; 294 295 AssertPtrReturn(pDevNum, VERR_INVALID_POINTER); 296 297 int rc = RTLinuxSysFsOpenV(&hFile, pszFormat, va); 298 if (RT_SUCCESS(rc)) 299 { 300 size_t cchNum = 0; 301 char szNum[128]; 302 rc = RTLinuxSysFsReadStr(hFile, szNum, sizeof(szNum), &cchNum); 303 if (RT_SUCCESS(rc)) 304 { 305 if (cchNum > 0) 306 { 307 uint32_t u32Maj = 0; 308 uint32_t u32Min = 0; 309 char *pszNext = NULL; 310 rc = RTStrToUInt32Ex(szNum, &pszNext, 10, &u32Maj); 311 if (RT_FAILURE(rc) || (rc != VWRN_TRAILING_CHARS) || (*pszNext != ':')) 312 rc = VERR_INVALID_PARAMETER; 313 else 314 { 315 rc = RTStrToUInt32Ex(pszNext + 1, NULL, 10, &u32Min); 316 if ( rc != VINF_SUCCESS 317 && rc != VWRN_TRAILING_CHARS 318 && rc != VWRN_TRAILING_SPACES) 319 rc = VERR_INVALID_PARAMETER; 320 else 321 *pDevNum = makedev(u32Maj, u32Min); 322 } 323 } 305 324 else 306 { 307 errno = 0; 308 DevNum = makedev(u32Maj, u32Min); 309 } 310 } 311 } 312 else if (cchNum == 0) 313 errno = EINVAL; 314 315 RTLinuxSysFsClose(fd); 316 return DevNum; 317 } 318 319 320 RTDECL(dev_t) RTLinuxSysFsReadDevNumFile(const char *pszFormat, ...) 321 { 322 va_list va; 323 va_start(va, pszFormat); 324 dev_t DevNum = RTLinuxSysFsReadDevNumFileV(pszFormat, va); 325 va_end(va); 326 return DevNum; 327 } 328 329 330 RTDECL(ssize_t) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va) 331 { 332 int fd = RTLinuxSysFsOpenV(pszFormat, va); 333 if (fd == -1) 334 return -1; 335 336 ssize_t cchRet = RTLinuxSysFsReadStr(fd, pszBuf, cchBuf); 337 RTLinuxSysFsClose(fd); 338 if (cchRet > 0) 339 { 340 char *pchNewLine = (char *)memchr(pszBuf, '\n', cchRet); 341 if (pchNewLine) 342 *pchNewLine = '\0'; 343 } 344 return cchRet; 345 } 346 347 348 RTDECL(ssize_t) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, const char *pszFormat, ...) 349 { 350 va_list va; 351 va_start(va, pszFormat); 352 ssize_t cchRet = RTLinuxSysFsReadStrFileV(pszBuf, cchBuf, pszFormat, va); 353 va_end(va); 354 return cchRet; 355 } 356 357 358 RTDECL(ssize_t) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va) 359 { 360 AssertReturnStmt(cchBuf >= 2, errno = EINVAL, -1); 325 rc = VERR_INVALID_PARAMETER; 326 } 327 328 RTFileClose(hFile); 329 } 330 331 return rc; 332 } 333 334 335 RTDECL(int) RTLinuxSysFsReadDevNumFile(dev_t *pDevNum, const char *pszFormat, ...) 336 { 337 va_list va; 338 va_start(va, pszFormat); 339 int rc = RTLinuxSysFsReadDevNumFileV(pDevNum, pszFormat, va); 340 va_end(va); 341 return rc; 342 } 343 344 345 RTDECL(int) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, va_list va) 346 { 347 RTFILE hFile; 348 349 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER); 350 351 int rc = RTLinuxSysFsOpenV(&hFile, pszFormat, va); 352 if (RT_SUCCESS(rc)) 353 { 354 size_t cchRead = 0; 355 rc = RTLinuxSysFsReadStr(hFile, pszBuf, cchBuf, &cchRead); 356 RTFileClose(hFile); 357 if ( RT_SUCCESS(rc) 358 && cchRead > 0) 359 { 360 char *pchNewLine = (char *)memchr(pszBuf, '\n', cchRead); 361 if (pchNewLine) 362 *pchNewLine = '\0'; 363 } 364 365 if (pcchRead) 366 *pcchRead = cchRead; 367 } 368 return rc; 369 } 370 371 372 RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, ...) 373 { 374 va_list va; 375 va_start(va, pszFormat); 376 int rc = RTLinuxSysFsReadStrFileV(pszBuf, cchBuf, pcchRead, pszFormat, va); 377 va_end(va); 378 return rc; 379 } 380 381 382 RTDECL(int) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, va_list va) 383 { 384 AssertReturn(cchBuf >= 2, VERR_INVALID_PARAMETER); 361 385 362 386 /* … … 365 389 char szFilename[RTPATH_MAX]; 366 390 int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va); 367 if (rc == -1) 368 return -1; 369 370 char szLink[RTPATH_MAX]; 371 rc = readlink(szFilename, szLink, sizeof(szLink)); 372 if (rc == -1) 373 return -1; 374 if ((size_t)rc > sizeof(szLink) - 1) 375 { 376 errno = ERANGE; 377 return -1; 378 } 379 szLink[rc] = '\0'; /* readlink fun. */ 380 381 /* 382 * Extract the file name component and copy it into the return buffer. 383 */ 384 size_t cchName; 385 const char *pszName = RTPathFilename(szLink); 386 if (pszName) 387 { 388 cchName = strlen(pszName); /* = &szLink[rc] - pszName; */ 389 if (cchName >= cchBuf) 390 { 391 errno = ERANGE; 392 return -1; 393 } 394 memcpy(pszBuf, pszName, cchName + 1); 395 } 396 else 397 { 398 *pszBuf = '\0'; 399 cchName = 0; 400 } 401 return cchName; 402 } 403 404 405 RTDECL(ssize_t) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, const char *pszFormat, ...) 406 { 407 va_list va; 408 va_start(va, pszFormat); 409 int rc = RTLinuxSysFsGetLinkDestV(pszBuf, cchBuf, pszFormat, va); 410 va_end(va); 411 return rc; 412 } 413 414 415 RTDECL(ssize_t) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, 416 size_t cchBuf, const char *pszPattern, 417 va_list va) 418 { 419 char szFilename[RTPATH_MAX]; 420 int rc = VINF_TRY_AGAIN; 421 391 if (RT_SUCCESS(rc)) 392 { 393 char szLink[RTPATH_MAX]; 394 rc = RTSymlinkRead(szFilename, szLink, sizeof(szLink), 0); 395 if (RT_SUCCESS(rc)) 396 { 397 /* 398 * Extract the file name component and copy it into the return buffer. 399 */ 400 size_t cchName; 401 const char *pszName = RTPathFilename(szLink); 402 if (pszName) 403 { 404 cchName = strlen(pszName); 405 if (cchName < cchBuf) 406 memcpy(pszBuf, pszName, cchName + 1); 407 else 408 rc = VERR_BUFFER_OVERFLOW; 409 } 410 else 411 { 412 *pszBuf = '\0'; 413 cchName = 0; 414 } 415 416 if (pchBuf) 417 *pchBuf = cchName; 418 } 419 } 420 421 return rc; 422 } 423 424 425 RTDECL(int) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, ...) 426 { 427 va_list va; 428 va_start(va, pszFormat); 429 int rc = RTLinuxSysFsGetLinkDestV(pszBuf, cchBuf, pchBuf, pszFormat, va); 430 va_end(va); 431 return rc; 432 } 433 434 435 RTDECL(int) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, 436 size_t cchBuf, const char *pszPattern, 437 va_list va) 438 { 422 439 AssertReturn(cchBuf >= 2, VERR_INVALID_PARAMETER); 423 440 AssertReturn( fMode == RTFS_TYPE_DEV_CHAR 424 441 || fMode == RTFS_TYPE_DEV_BLOCK, 425 442 VERR_INVALID_PARAMETER); 426 if (pszPattern) 427 { 428 /* 429 * Construct the filename and read the link. 430 */ 431 rc = rtLinuxConstructPathV(szFilename, sizeof(szFilename), "/dev/", 443 AssertPtrReturn(pszPattern, VERR_INVALID_PARAMETER); 444 445 /* 446 * Construct the filename and read the link. 447 */ 448 char szFilename[RTPATH_MAX]; 449 int rc = rtLinuxConstructPathV(szFilename, sizeof(szFilename), "/dev/", 432 450 pszPattern, va); 433 if (rc > 0) 434 { 435 RTFSOBJINFO Info; 436 rc = RTPathQueryInfo(szFilename, &Info, RTFSOBJATTRADD_UNIX); 437 if ( rc == VERR_PATH_NOT_FOUND 438 || ( RT_SUCCESS(rc) 439 && ( Info.Attr.u.Unix.Device != DevNum 440 || (Info.Attr.fMode & RTFS_TYPE_MASK) != fMode))) 441 rc = VERR_FILE_NOT_FOUND; 442 } 443 } 444 445 if (RT_SUCCESS(rc)) 446 { 447 size_t cchPath = strlen(szFilename); 448 if (cchPath >= cchBuf) 449 return VERR_BUFFER_OVERFLOW; 450 memcpy(pszBuf, szFilename, cchPath + 1); 451 return cchPath; 452 } 453 return rc; 454 } 455 456 457 /** @todo Do we really need to return the string length? If the caller is 458 * interested (the current ones aren't) they can check themselves. */ 459 RTDECL(ssize_t) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, 460 size_t cchBuf, const char *pszPattern, 461 ...) 451 if (RT_SUCCESS(rc)) 452 { 453 RTFSOBJINFO Info; 454 rc = RTPathQueryInfo(szFilename, &Info, RTFSOBJATTRADD_UNIX); 455 if ( rc == VERR_PATH_NOT_FOUND 456 || ( RT_SUCCESS(rc) 457 && ( Info.Attr.u.Unix.Device != DevNum 458 || (Info.Attr.fMode & RTFS_TYPE_MASK) != fMode))) 459 rc = VERR_FILE_NOT_FOUND; 460 461 if (RT_SUCCESS(rc)) 462 { 463 size_t cchPath = strlen(szFilename); 464 if (cchPath < cchBuf) 465 memcpy(pszBuf, szFilename, cchPath + 1); 466 else 467 rc = VERR_BUFFER_OVERFLOW; 468 } 469 } 470 471 return rc; 472 } 473 474 475 RTDECL(int) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, 476 size_t cchBuf, const char *pszPattern, 477 ...) 462 478 { 463 479 va_list va;
Note:
See TracChangeset
for help on using the changeset viewer.