VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/linux/sysfs.cpp@ 34244

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

*: spelling fixes, thanks Timeless!

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 15.7 KB
Line 
1/* $Id: sysfs.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
2/** @file
3 * IPRT - Linux sysfs access.
4 */
5
6/*
7 * Copyright (C) 2006-2008 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#define LOG_GROUP RTLOGGROUP_SYSTEM
32#include <iprt/linux/sysfs.h>
33#include <iprt/assert.h>
34#include <iprt/dir.h>
35#include <iprt/err.h>
36#include <iprt/fs.h>
37#include <iprt/param.h>
38#include <iprt/path.h>
39#include <iprt/string.h>
40
41#include <unistd.h>
42#include <stdio.h>
43#include <sys/sysctl.h>
44#include <sys/stat.h>
45#include <sys/fcntl.h>
46#include <errno.h>
47
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
52/**
53 * Constructs the path of a sysfs file from the format parameters passed,
54 * prepending a prefix if the path is relative.
55 *
56 * @returns The number of characters returned, or -1 and errno set to ERANGE on
57 * failure.
58 *
59 * @param pszPrefix The prefix to prepend if the path is relative. Must end
60 * in '/'.
61 * @param pszBuf Where to write the path. Must be at least
62 * sizeof(@a pszPrefix) characters long
63 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
64 * @param pszFormat The name format, either absolute or relative to the
65 * prefix specified by @a pszPrefix.
66 * @param va The format args.
67 */
68static ssize_t rtLinuxConstructPathV(char *pszBuf, size_t cchBuf,
69 const char *pszPrefix,
70 const char *pszFormat, va_list va)
71{
72 size_t cchPrefix = strlen(pszPrefix);
73 AssertReturnStmt(pszPrefix[cchPrefix - 1] == '/', errno = ERANGE, -1);
74 AssertReturnStmt(cchBuf > cchPrefix + 1, errno = ERANGE, -1);
75
76 /** @todo While RTStrPrintfV prevents overflows, it doesn't make it easy to
77 * check for truncations. RTPath should provide some formatters and
78 * joiners which can take over this rather common task that is
79 * performed here. */
80 size_t cch = RTStrPrintfV(pszBuf, cchBuf, pszFormat, va);
81 if (*pszBuf != '/')
82 {
83 AssertReturnStmt(cchBuf >= cch + cchPrefix + 1, errno = ERANGE, -1);
84 memmove(pszBuf + cchPrefix, pszBuf, cch + 1);
85 memcpy(pszBuf, pszPrefix, cchPrefix);
86 cch += cchPrefix;
87 }
88 return cch;
89}
90
91
92/**
93 * Constructs the path of a sysfs file from the format parameters passed,
94 * prepending a prefix if the path is relative.
95 *
96 * @returns The number of characters returned, or -1 and errno set to ERANGE on
97 * failure.
98 *
99 * @param pszPrefix The prefix to prepend if the path is relative. Must end
100 * in '/'.
101 * @param pszBuf Where to write the path. Must be at least
102 * sizeof(@a pszPrefix) characters long
103 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
104 * @param pszFormat The name format, either absolute or relative to "/sys/".
105 * @param ... The format args.
106 */
107static ssize_t rtLinuxConstructPath(char *pszBuf, size_t cchBuf,
108 const char *pszPrefix,
109 const char *pszFormat, ...)
110{
111 va_list va;
112 va_start(va, pszFormat);
113 int rc = rtLinuxConstructPathV(pszBuf, cchBuf, pszPrefix, pszFormat, va);
114 va_end(va);
115 return rc;
116}
117
118
119/**
120 * Constructs the path of a sysfs file from the format parameters passed,
121 * prepending "/sys/" if the path is relative.
122 *
123 * @returns The number of characters returned, or -1 and errno set to ERANGE on
124 * failure.
125 *
126 * @param pszBuf Where to write the path. Must be at least
127 * sizeof("/sys/") characters long
128 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
129 * @param pszFormat The name format, either absolute or relative to "/sys/".
130 * @param va The format args.
131 */
132static ssize_t rtLinuxSysFsConstructPath(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va)
133{
134 return rtLinuxConstructPathV(pszBuf, cchBuf, "/sys/", pszFormat, va);
135}
136
137
138RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va)
139{
140 int iSavedErrno = errno;
141
142 /*
143 * Construct the filename and call stat.
144 */
145 bool fRet = false;
146 char szFilename[RTPATH_MAX];
147 ssize_t rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
148 if (rc != -1)
149 {
150 struct stat st;
151 fRet = stat(szFilename, &st) == 0;
152 }
153
154 errno = iSavedErrno;
155 return fRet;
156}
157
158
159RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...)
160{
161 va_list va;
162 va_start(va, pszFormat);
163 bool fRet = RTLinuxSysFsExistsV(pszFormat, va);
164 va_end(va);
165 return fRet;
166}
167
168
169RTDECL(int) RTLinuxSysFsOpenV(const char *pszFormat, va_list va)
170{
171 /*
172 * Construct the filename and call open.
173 */
174 char szFilename[RTPATH_MAX];
175 ssize_t rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
176 if (rc != -1)
177 rc = open(szFilename, O_RDONLY, 0);
178 return rc;
179}
180
181
182RTDECL(int) RTLinuxSysFsOpen(const char *pszFormat, ...)
183{
184 va_list va;
185 va_start(va, pszFormat);
186 int fd = RTLinuxSysFsOpenV(pszFormat, va);
187 va_end(va);
188 return fd;
189}
190
191
192RTDECL(void) RTLinuxSysFsClose(int fd)
193{
194 int iSavedErrno = errno;
195 close(fd);
196 errno = iSavedErrno;
197}
198
199
200RTDECL(ssize_t) RTLinuxSysFsReadStr(int fd, char *pszBuf, size_t cchBuf)
201{
202 Assert(cchBuf > 1);
203 ssize_t cchRead = read(fd, pszBuf, cchBuf - 1);
204 pszBuf[cchRead >= 0 ? cchRead : 0] = '\0';
205 return cchRead;
206}
207
208
209RTDECL(int) RTLinuxSysFsReadFile(int fd, void *pvBuf, size_t cbBuf, size_t *pcbRead)
210{
211 int rc;
212 ssize_t cbRead = read(fd, pvBuf, cbBuf);
213 if (cbRead >= 0)
214 {
215 if (pcbRead)
216 *pcbRead = cbRead;
217 if ((size_t)cbRead < cbBuf)
218 rc = VINF_SUCCESS;
219 else
220 {
221 /* Check for EOF */
222 char ch;
223 off_t off = lseek(fd, 0, SEEK_CUR);
224 ssize_t cbRead2 = read(fd, &ch, 1);
225 if (cbRead2 == 0)
226 rc = VINF_SUCCESS;
227 else if (cbRead2 > 0)
228 {
229 lseek(fd, off, SEEK_SET);
230 rc = VERR_BUFFER_OVERFLOW;
231 }
232 else
233 rc = RTErrConvertFromErrno(errno);
234 }
235 }
236 else
237 rc = RTErrConvertFromErrno(errno);
238 return rc;
239}
240
241
242RTDECL(int64_t) RTLinuxSysFsReadIntFileV(unsigned uBase, const char *pszFormat, va_list va)
243{
244 int fd = RTLinuxSysFsOpenV(pszFormat, va);
245 if (fd == -1)
246 return -1;
247
248 int64_t i64Ret = -1;
249 char szNum[128];
250 ssize_t cchNum = RTLinuxSysFsReadStr(fd, szNum, sizeof(szNum));
251 if (cchNum > 0)
252 {
253 int rc = RTStrToInt64Ex(szNum, NULL, uBase, &i64Ret);
254 if (RT_FAILURE(rc))
255 {
256 i64Ret = -1;
257 errno = -ETXTBSY; /* just something that won't happen at read / open. */
258 }
259 }
260 else if (cchNum == 0)
261 errno = -ETXTBSY; /* just something that won't happen at read / open. */
262
263 RTLinuxSysFsClose(fd);
264 return i64Ret;
265}
266
267
268RTDECL(int64_t) RTLinuxSysFsReadIntFile(unsigned uBase, const char *pszFormat, ...)
269{
270 va_list va;
271 va_start(va, pszFormat);
272 int64_t i64Ret = RTLinuxSysFsReadIntFileV(uBase, pszFormat, va);
273 va_end(va);
274 return i64Ret;
275}
276
277
278RTDECL(dev_t) RTLinuxSysFsReadDevNumFileV(const char *pszFormat, va_list va)
279{
280 int fd = RTLinuxSysFsOpenV(pszFormat, va);
281 if (fd == -1)
282 return 0;
283
284 dev_t DevNum = 0;
285 char szNum[128];
286 ssize_t cchNum = RTLinuxSysFsReadStr(fd, szNum, sizeof(szNum));
287 if (cchNum > 0)
288 {
289 uint32_t u32Maj = 0;
290 uint32_t u32Min = 0;
291 char *pszNext = NULL;
292 int rc = RTStrToUInt32Ex(szNum, &pszNext, 10, &u32Maj);
293 if (RT_FAILURE(rc) || (rc != VWRN_TRAILING_CHARS) || (*pszNext != ':'))
294 errno = EINVAL;
295 else
296 {
297 rc = RTStrToUInt32Ex(pszNext + 1, NULL, 10, &u32Min);
298 if ( rc != VINF_SUCCESS
299 && rc != VWRN_TRAILING_CHARS
300 && rc != VWRN_TRAILING_SPACES)
301 errno = EINVAL;
302 else
303 {
304 errno = 0;
305 DevNum = makedev(u32Maj, u32Min);
306 }
307 }
308 }
309 else if (cchNum == 0)
310 errno = EINVAL;
311
312 RTLinuxSysFsClose(fd);
313 return DevNum;
314}
315
316
317RTDECL(dev_t) RTLinuxSysFsReadDevNumFile(const char *pszFormat, ...)
318{
319 va_list va;
320 va_start(va, pszFormat);
321 dev_t DevNum = RTLinuxSysFsReadDevNumFileV(pszFormat, va);
322 va_end(va);
323 return DevNum;
324}
325
326
327RTDECL(ssize_t) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va)
328{
329 int fd = RTLinuxSysFsOpenV(pszFormat, va);
330 if (fd == -1)
331 return -1;
332
333 ssize_t cchRet = RTLinuxSysFsReadStr(fd, pszBuf, cchBuf);
334 RTLinuxSysFsClose(fd);
335 if (cchRet > 0)
336 {
337 char *pchNewLine = (char *)memchr(pszBuf, '\n', cchRet);
338 if (pchNewLine)
339 *pchNewLine = '\0';
340 }
341 return cchRet;
342}
343
344
345RTDECL(ssize_t) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, const char *pszFormat, ...)
346{
347 va_list va;
348 va_start(va, pszFormat);
349 ssize_t cchRet = RTLinuxSysFsReadStrFileV(pszBuf, cchBuf, pszFormat, va);
350 va_end(va);
351 return cchRet;
352}
353
354
355RTDECL(ssize_t) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va)
356{
357 AssertReturnStmt(cchBuf >= 2, errno = EINVAL, -1);
358
359 /*
360 * Construct the filename and read the link.
361 */
362 char szFilename[RTPATH_MAX];
363 int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
364 if (rc == -1)
365 return -1;
366
367 char szLink[RTPATH_MAX];
368 rc = readlink(szFilename, szLink, sizeof(szLink));
369 if (rc == -1)
370 return -1;
371 if ((size_t)rc > sizeof(szLink) - 1)
372 {
373 errno = ERANGE;
374 return -1;
375 }
376 szLink[rc] = '\0'; /* readlink fun. */
377
378 /*
379 * Extract the file name component and copy it into the return buffer.
380 */
381 size_t cchName;
382 const char *pszName = RTPathFilename(szLink);
383 if (pszName)
384 {
385 cchName = strlen(pszName); /* = &szLink[rc] - pszName; */
386 if (cchName >= cchBuf)
387 {
388 errno = ERANGE;
389 return -1;
390 }
391 memcpy(pszBuf, pszName, cchName + 1);
392 }
393 else
394 {
395 *pszBuf = '\0';
396 cchName = 0;
397 }
398 return cchName;
399}
400
401
402RTDECL(ssize_t) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, const char *pszFormat, ...)
403{
404 va_list va;
405 va_start(va, pszFormat);
406 int rc = RTLinuxSysFsGetLinkDestV(pszBuf, cchBuf, pszFormat, va);
407 va_end(va);
408 return rc;
409}
410
411
412static ssize_t rtLinuxFindDevicePathRecursive(dev_t DevNum, RTFMODE fMode, const char *pszBasePath,
413 char *pszBuf, size_t cchBuf)
414{
415 /*
416 * Check assumptions made by the code below.
417 */
418 size_t const cchBasePath = strlen(pszBasePath);
419 AssertReturnStmt(cchBasePath < RTPATH_MAX - 10U, errno = ENAMETOOLONG, -1);
420
421 ssize_t rcRet;
422 PRTDIR pDir;
423 int rc = RTDirOpen(&pDir, pszBasePath);
424 if (RT_SUCCESS(rc))
425 {
426 char szPath[RTPATH_MAX]; /** @todo 4K per recursion - can easily be optimized away by passing it along pszBasePath
427 and only remember the length. */
428 memcpy(szPath, pszBasePath, cchBasePath + 1);
429
430 for (;;)
431 {
432 RTDIRENTRYEX Entry;
433 rc = RTDirReadEx(pDir, &Entry, NULL, RTFSOBJATTRADD_UNIX, RTPATH_F_ON_LINK);
434 if (RT_FAILURE(rc))
435 {
436 errno = rc == VERR_NO_MORE_FILES
437 ? ENOENT
438 : rc == VERR_BUFFER_OVERFLOW
439 ? EOVERFLOW
440 : EIO;
441 rcRet = -1;
442 break;
443 }
444 if (RTFS_IS_SYMLINK(Entry.Info.Attr.fMode))
445 continue;
446
447 /* Do the matching. */
448 if ( Entry.Info.Attr.u.Unix.Device == DevNum
449 && (Entry.Info.Attr.fMode & RTFS_TYPE_MASK) == fMode)
450 {
451 rcRet = rtLinuxConstructPath(pszBuf, cchBuf, pszBasePath, "%s", Entry.szName);
452 break;
453 }
454
455 /* Recurse into subdirectories. */
456 if (!RTFS_IS_DIRECTORY(Entry.Info.Attr.fMode))
457 continue;
458 if (Entry.szName[0] == '.')
459 continue;
460
461 szPath[cchBasePath] = '\0';
462 rc = RTPathAppend(szPath, sizeof(szPath) - 1, Entry.szName); /* -1: for slash */
463 if (RT_FAILURE(rc))
464 {
465 errno = ENAMETOOLONG;
466 rcRet = -1;
467 break;
468 }
469 strcat(&szPath[cchBasePath], "/");
470 rcRet = rtLinuxFindDevicePathRecursive(DevNum, fMode, szPath, pszBuf, cchBuf);
471 if (rcRet >= 0 || errno != ENOENT)
472 break;
473 }
474 RTDirClose(pDir);
475 }
476 else
477 {
478 rcRet = -1;
479 errno = RTErrConvertToErrno(rc);
480 }
481 return rcRet;
482}
483
484
485RTDECL(ssize_t) RTLinuxFindDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
486 const char *pszSuggestion, va_list va)
487{
488 AssertReturnStmt(cchBuf >= 2, errno = EINVAL, -1);
489 AssertReturnStmt( fMode == RTFS_TYPE_DEV_CHAR
490 || fMode == RTFS_TYPE_DEV_BLOCK,
491 errno = EINVAL, -1);
492
493 if (pszSuggestion)
494 {
495 /*
496 * Construct the filename and read the link.
497 */
498 char szFilename[RTPATH_MAX];
499 int rc = rtLinuxConstructPathV(szFilename, sizeof(szFilename), "/dev/", pszSuggestion, va);
500 if (rc == -1)
501 return -1;
502
503 /*
504 * Check whether the caller's suggestion was right.
505 */
506 RTFSOBJINFO Info;
507 rc = RTPathQueryInfo(szFilename, &Info, RTFSOBJATTRADD_UNIX);
508 if ( RT_SUCCESS(rc)
509 && Info.Attr.u.Unix.Device == DevNum
510 && (Info.Attr.fMode & RTFS_TYPE_MASK) == fMode)
511 {
512 size_t cchPath = strlen(szFilename);
513 if (cchPath >= cchBuf)
514 {
515 errno = EOVERFLOW;
516 return -1;
517 }
518 memcpy(pszBuf, szFilename, cchPath + 1);
519 return cchPath;
520 }
521
522 /* The suggestion was wrong, fall back on the brute force attack. */
523 }
524
525 return rtLinuxFindDevicePathRecursive(DevNum, fMode, "/dev/", pszBuf, cchBuf);
526}
527
528
529RTDECL(ssize_t) RTLinuxFindDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf, size_t cchBuf,
530 const char *pszSuggestion, ...)
531{
532 va_list va;
533 va_start(va, pszSuggestion);
534 int rc = RTLinuxFindDevicePathV(DevNum, fMode, pszBuf, cchBuf, pszSuggestion, va);
535 va_end(va);
536 return rc;
537}
538
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