1 | /* $Id: sysfs.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Linux sysfs access.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 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 an iprt error code on failure.
|
---|
57 | *
|
---|
58 | * @param pszPrefix The prefix to prepend if the path is relative. Must end
|
---|
59 | * in '/'.
|
---|
60 | * @param pszBuf Where to write the path. Must be at least
|
---|
61 | * sizeof(@a pszPrefix) characters long
|
---|
62 | * @param cchBuf The size of the buffer pointed to by @a pszBuf.
|
---|
63 | * @param pszFormat The name format, either absolute or relative to the
|
---|
64 | * prefix specified by @a pszPrefix.
|
---|
65 | * @param va The format args.
|
---|
66 | */
|
---|
67 | static ssize_t rtLinuxConstructPathV(char *pszBuf, size_t cchBuf,
|
---|
68 | const char *pszPrefix,
|
---|
69 | const char *pszFormat, va_list va)
|
---|
70 | {
|
---|
71 | size_t cchPrefix = strlen(pszPrefix);
|
---|
72 | AssertReturn(pszPrefix[cchPrefix - 1] == '/', VERR_INVALID_PARAMETER);
|
---|
73 | AssertReturn(cchBuf > cchPrefix + 1, VERR_INVALID_PARAMETER);
|
---|
74 |
|
---|
75 | /** @todo While RTStrPrintfV prevents overflows, it doesn't make it easy to
|
---|
76 | * check for truncations. RTPath should provide some formatters and
|
---|
77 | * joiners which can take over this rather common task that is
|
---|
78 | * performed here. */
|
---|
79 | size_t cch = RTStrPrintfV(pszBuf, cchBuf, pszFormat, va);
|
---|
80 | if (*pszBuf != '/')
|
---|
81 | {
|
---|
82 | AssertReturn(cchBuf >= cch + cchPrefix + 1, VERR_BUFFER_OVERFLOW);
|
---|
83 | memmove(pszBuf + cchPrefix, pszBuf, cch + 1);
|
---|
84 | memcpy(pszBuf, pszPrefix, cchPrefix);
|
---|
85 | cch += cchPrefix;
|
---|
86 | }
|
---|
87 | return cch;
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Constructs the path of a sysfs file from the format parameters passed,
|
---|
93 | * prepending a prefix if the path is relative.
|
---|
94 | *
|
---|
95 | * @returns The number of characters returned, or an iprt error code on failure.
|
---|
96 | * @note Unused.
|
---|
97 | *
|
---|
98 | * @param pszPrefix The prefix to prepend if the path is relative. Must end
|
---|
99 | * in '/'.
|
---|
100 | * @param pszBuf Where to write the path. Must be at least
|
---|
101 | * sizeof(@a pszPrefix) characters long
|
---|
102 | * @param cchBuf The size of the buffer pointed to by @a pszBuf.
|
---|
103 | * @param pszFormat The name format, either absolute or relative to "/sys/".
|
---|
104 | * @param ... The format args.
|
---|
105 | */
|
---|
106 | static ssize_t rtLinuxConstructPath(char *pszBuf, size_t cchBuf,
|
---|
107 | const char *pszPrefix,
|
---|
108 | const char *pszFormat, ...)
|
---|
109 | {
|
---|
110 | va_list va;
|
---|
111 | va_start(va, pszFormat);
|
---|
112 | int rc = rtLinuxConstructPathV(pszBuf, cchBuf, pszPrefix, pszFormat, va);
|
---|
113 | va_end(va);
|
---|
114 | return rc;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Constructs the path of a sysfs file from the format parameters passed,
|
---|
120 | * prepending "/sys/" if the path is relative.
|
---|
121 | *
|
---|
122 | * @returns The number of characters returned, or -1 and errno set to ERANGE on
|
---|
123 | * failure.
|
---|
124 | *
|
---|
125 | * @param pszBuf Where to write the path. Must be at least
|
---|
126 | * sizeof("/sys/") characters long
|
---|
127 | * @param cchBuf The size of the buffer pointed to by @a pszBuf.
|
---|
128 | * @param pszFormat The name format, either absolute or relative to "/sys/".
|
---|
129 | * @param va The format args.
|
---|
130 | */
|
---|
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)
|
---|
142 | {
|
---|
143 | int iSavedErrno = errno;
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Construct the filename and call stat.
|
---|
147 | */
|
---|
148 | bool fRet = false;
|
---|
149 | char szFilename[RTPATH_MAX];
|
---|
150 | ssize_t rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
|
---|
151 | if (rc != -1)
|
---|
152 | {
|
---|
153 | struct stat st;
|
---|
154 | fRet = stat(szFilename, &st) == 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | errno = iSavedErrno;
|
---|
158 | return fRet;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
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)
|
---|
173 | {
|
---|
174 | /*
|
---|
175 | * Construct the filename and call open.
|
---|
176 | */
|
---|
177 | 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)
|
---|
204 | {
|
---|
205 | 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)
|
---|
217 | {
|
---|
218 | if (pcbRead)
|
---|
219 | *pcbRead = cbRead;
|
---|
220 | if ((size_t)cbRead < cbBuf)
|
---|
221 | rc = VINF_SUCCESS;
|
---|
222 | else
|
---|
223 | {
|
---|
224 | /* 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)
|
---|
231 | {
|
---|
232 | lseek(fd, off, SEEK_SET);
|
---|
233 | rc = VERR_BUFFER_OVERFLOW;
|
---|
234 | }
|
---|
235 | 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;
|
---|
305 | 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);
|
---|
361 |
|
---|
362 | /*
|
---|
363 | * Construct the filename and read the link.
|
---|
364 | */
|
---|
365 | char szFilename[RTPATH_MAX];
|
---|
366 | 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 |
|
---|
422 | AssertReturn(cchBuf >= 2, VERR_INVALID_PARAMETER);
|
---|
423 | AssertReturn( fMode == RTFS_TYPE_DEV_CHAR
|
---|
424 | || fMode == RTFS_TYPE_DEV_BLOCK,
|
---|
425 | VERR_INVALID_PARAMETER);
|
---|
426 | if (pszPattern)
|
---|
427 | {
|
---|
428 | /*
|
---|
429 | * Construct the filename and read the link.
|
---|
430 | */
|
---|
431 | rc = rtLinuxConstructPathV(szFilename, sizeof(szFilename), "/dev/",
|
---|
432 | 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 | ...)
|
---|
462 | {
|
---|
463 | va_list va;
|
---|
464 | va_start(va, pszPattern);
|
---|
465 | int rc = RTLinuxCheckDevicePathV(DevNum, fMode, pszBuf, cchBuf,
|
---|
466 | pszPattern, va);
|
---|
467 | va_end(va);
|
---|
468 | return rc;
|
---|
469 | }
|
---|
470 |
|
---|