1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
|
/* $Id: sysfs.cpp $ */
/** @file
* IPRT - Linux sysfs access.
*/
/*
* Copyright (C) 2006-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE 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.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP RTLOGGROUP_SYSTEM
#include <iprt/assert.h>
#include <iprt/dir.h>
#include <iprt/err.h>
#include <iprt/file.h>
#include <iprt/fs.h>
#include <iprt/param.h>
#include <iprt/path.h>
#include <iprt/string.h>
#include <iprt/symlink.h>
#include <iprt/linux/sysfs.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
#include <sys/fcntl.h>
#include <sys/sysmacros.h>
#include <errno.h>
/**
* Constructs the path of a sysfs file from the format parameters passed,
* prepending a prefix if the path is relative.
*
* @returns IPRT status code.
* @param pszPrefix The prefix to prepend if the path is relative. Must end
* in '/'.
* @param pszBuf Where to write the path. Must be at least
* sizeof(@a pszPrefix) characters long
* @param cchBuf The size of the buffer pointed to by @a pszBuf.
* @param pszFormat The name format, either absolute or relative to the
* prefix specified by @a pszPrefix.
* @param va The format args.
*/
static int rtLinuxConstructPathV(char *pszBuf, size_t cchBuf,
const char *pszPrefix,
const char *pszFormat, va_list va)
{
size_t cchPrefix = strlen(pszPrefix);
AssertReturn(pszPrefix[cchPrefix - 1] == '/', VERR_INVALID_PARAMETER);
AssertReturn(cchBuf > cchPrefix + 1, VERR_INVALID_PARAMETER);
/** @todo While RTStrPrintfV prevents overflows, it doesn't make it easy to
* check for truncations. RTPath should provide some formatters and
* joiners which can take over this rather common task that is
* performed here. */
size_t cch = RTStrPrintfV(pszBuf, cchBuf, pszFormat, va);
if (*pszBuf != '/')
{
AssertReturn(cchBuf >= cch + cchPrefix + 1, VERR_BUFFER_OVERFLOW);
memmove(pszBuf + cchPrefix, pszBuf, cch + 1);
memcpy(pszBuf, pszPrefix, cchPrefix);
cch += cchPrefix;
}
return VINF_SUCCESS;
}
/**
* Constructs the path of a sysfs file from the format parameters passed,
* prepending a prefix if the path is relative.
*
* @returns IPRT status code.
* @param pszPrefix The prefix to prepend if the path is relative. Must end
* in '/'.
* @param pszBuf Where to write the path. Must be at least
* sizeof(@a pszPrefix) characters long
* @param cchBuf The size of the buffer pointed to by @a pszBuf.
* @param pszFormat The name format, either absolute or relative to "/sys/".
* @param ... The format args.
*/
DECLINLINE(int) rtLinuxConstructPath(char *pszBuf, size_t cchBuf,
const char *pszPrefix,
const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = rtLinuxConstructPathV(pszBuf, cchBuf, pszPrefix, pszFormat, va);
va_end(va);
return rc;
}
/**
* Constructs the path of a sysfs file from the format parameters passed,
* prepending "/sys/" if the path is relative.
*
* @returns IPRT status code.
* @param pszBuf Where to write the path. Must be at least
* sizeof("/sys/") characters long
* @param cchBuf The size of the buffer pointed to by @a pszBuf.
* @param pszFormat The name format, either absolute or relative to "/sys/".
* @param va The format args.
*/
DECLINLINE(int) rtLinuxSysFsConstructPath(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va)
{
return rtLinuxConstructPathV(pszBuf, cchBuf, "/sys/", pszFormat, va);
}
RTDECL(int) RTLinuxSysFsExistsExV(const char *pszFormat, va_list va)
{
int iSavedErrno = errno;
/*
* Construct the filename and call stat.
*/
char szFilename[RTPATH_MAX];
int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
if (RT_SUCCESS(rc))
{
struct stat st;
int rcStat = stat(szFilename, &st);
if (rcStat != 0)
rc = RTErrConvertFromErrno(errno);
}
errno = iSavedErrno;
return rc;
}
RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va)
{
return RT_SUCCESS(RTLinuxSysFsExistsExV(pszFormat, va));
}
RTDECL(int) RTLinuxSysFsExistsEx(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsExistsExV(pszFormat, va);
va_end(va);
return rc;
}
RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
bool fRet = RTLinuxSysFsExistsV(pszFormat, va);
va_end(va);
return fRet;
}
RTDECL(int) RTLinuxSysFsOpenV(PRTFILE phFile, const char *pszFormat, va_list va)
{
/*
* Construct the filename and call open.
*/
char szFilename[RTPATH_MAX];
int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
if (RT_SUCCESS(rc))
rc = RTFileOpen(phFile, szFilename, RTFILE_O_OPEN | RTFILE_O_READ | RTFILE_O_DENY_NONE);
return rc;
}
RTDECL(int) RTLinuxSysFsOpenExV(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, va_list va)
{
/*
* Construct the filename and call open.
*/
char szFilename[RTPATH_MAX];
int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
if (RT_SUCCESS(rc))
rc = RTFileOpen(phFile, szFilename, fOpen);
return rc;
}
RTDECL(int) RTLinuxSysFsOpen(PRTFILE phFile, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsOpenV(phFile, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsOpenEx(PRTFILE phFile, uint64_t fOpen, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsOpenExV(phFile, fOpen, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsReadStr(RTFILE hFile, char *pszBuf, size_t cchBuf, size_t *pcchRead)
{
Assert(cchBuf > 1); /* not mandatory */
int rc;
size_t cchRead;
rc = RTFileRead(hFile, pszBuf, cchBuf, &cchRead);
if (RT_SUCCESS(rc))
{
/*
* ASSUME that if we've read less than we asked for, we've reached the
* end of the file. Otherwise, we've been given a buffer too small for
* the entire remainder of the file.
*/
if (cchRead < cchBuf)
pszBuf[cchRead] = '\0';
else if (cchBuf)
{
rc = RTFileSeek(hFile, -1, RTFILE_SEEK_CURRENT, NULL);
if (RT_SUCCESS(rc))
rc = VERR_BUFFER_OVERFLOW;
cchRead = cchBuf - 1;
pszBuf[cchRead] = '\0';
}
else
rc = VERR_BUFFER_OVERFLOW;
}
else
{
if (cchBuf > 0)
*pszBuf = '\0';
cchRead = 0;
}
if (pcchRead)
*pcchRead = cchRead;
return rc;
}
RTDECL(int) RTLinuxSysFsWriteStr(RTFILE hFile, const char *pszBuf, size_t cchBuf, size_t *pcchWritten)
{
if (!cchBuf)
cchBuf = strlen(pszBuf) + 1; /* Include the terminator */
return RTFileWrite(hFile, pszBuf, cchBuf, pcchWritten);
}
RTDECL(int) RTLinuxSysFsReadFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbRead)
{
int rc;
size_t cbRead = 0;
rc = RTFileRead(hFile, pvBuf, cbBuf, &cbRead);
if (RT_SUCCESS(rc))
{
if (pcbRead)
*pcbRead = cbRead;
if (cbRead < cbBuf)
rc = VINF_SUCCESS;
else
{
/* Check for EOF */
uint64_t offCur = 0;
uint8_t bRead;
rc = RTFileSeek(hFile, 0, RTFILE_SEEK_CURRENT, &offCur);
if (RT_SUCCESS(rc))
{
int rc2 = RTFileRead(hFile, &bRead, 1, NULL);
if (RT_SUCCESS(rc2))
{
rc = VERR_BUFFER_OVERFLOW;
rc2 = RTFileSeek(hFile, offCur, RTFILE_SEEK_BEGIN, NULL);
if (RT_FAILURE(rc2))
rc = rc2;
}
else if (rc2 != VERR_EOF)
rc = rc2;
}
}
}
return rc;
}
RTDECL(int) RTLinuxSysFsWriteFile(RTFILE hFile, void *pvBuf, size_t cbBuf, size_t *pcbWritten)
{
return RTFileWrite(hFile, pvBuf, cbBuf, pcbWritten);
}
RTDECL(int) RTLinuxSysFsReadIntFileV(unsigned uBase, int64_t *pi64, const char *pszFormat, va_list va)
{
RTFILE hFile;
AssertPtrReturn(pi64, VERR_INVALID_POINTER);
int rc = RTLinuxSysFsOpenV(&hFile, pszFormat, va);
if (RT_SUCCESS(rc))
{
char szNum[128];
size_t cchNum;
rc = RTLinuxSysFsReadStr(hFile, szNum, sizeof(szNum), &cchNum);
if (RT_SUCCESS(rc))
{
if (cchNum > 0)
{
int64_t i64Ret = -1;
rc = RTStrToInt64Ex(szNum, NULL, uBase, &i64Ret);
if (RT_SUCCESS(rc))
*pi64 = i64Ret;
}
else
rc = VERR_INVALID_PARAMETER;
}
RTFileClose(hFile);
}
return rc;
}
RTDECL(int) RTLinuxSysFsReadIntFile(unsigned uBase, int64_t *pi64, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsReadIntFileV(uBase, pi64, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsWriteU8FileV(unsigned uBase, uint8_t u8, const char *pszFormat, va_list va)
{
return RTLinuxSysFsWriteU64FileV(uBase, u8, pszFormat, va);
}
RTDECL(int) RTLinuxSysFsWriteU8File(unsigned uBase, uint8_t u8, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsWriteU64FileV(uBase, u8, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsWriteU16FileV(unsigned uBase, uint16_t u16, const char *pszFormat, va_list va)
{
return RTLinuxSysFsWriteU64FileV(uBase, u16, pszFormat, va);
}
RTDECL(int) RTLinuxSysFsWriteU16File(unsigned uBase, uint16_t u16, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsWriteU64FileV(uBase, u16, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsWriteU32FileV(unsigned uBase, uint32_t u32, const char *pszFormat, va_list va)
{
return RTLinuxSysFsWriteU64FileV(uBase, u32, pszFormat, va);
}
RTDECL(int) RTLinuxSysFsWriteU32File(unsigned uBase, uint32_t u32, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsWriteU64FileV(uBase, u32, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsWriteU64FileV(unsigned uBase, uint64_t u64, const char *pszFormat, va_list va)
{
RTFILE hFile;
const char *pszFmt = NULL;
switch (uBase)
{
case 8:
pszFmt = "%#llo";
break;
case 10:
pszFmt = "%llu";
break;
case 16:
pszFmt = "%#llx";
break;
default:
return VERR_INVALID_PARAMETER;
}
int rc = RTLinuxSysFsOpenExV(&hFile, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_NONE, pszFormat, va);
if (RT_SUCCESS(rc))
{
char szNum[128];
size_t cchNum = RTStrPrintf(szNum, sizeof(szNum), pszFmt, u64);
if (cchNum > 0)
{
size_t cbWritten = 0;
rc = RTLinuxSysFsWriteStr(hFile, &szNum[0], cchNum, &cbWritten);
if ( RT_SUCCESS(rc)
&& cbWritten != cchNum)
rc = VERR_BUFFER_OVERFLOW;
}
else
rc = VERR_INVALID_PARAMETER;
RTFileClose(hFile);
}
return rc;
}
RTDECL(int) RTLinuxSysFsWriteU64File(unsigned uBase, uint32_t u64, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsWriteU64FileV(uBase, u64, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsReadDevNumFileV(dev_t *pDevNum, const char *pszFormat, va_list va)
{
RTFILE hFile;
AssertPtrReturn(pDevNum, VERR_INVALID_POINTER);
int rc = RTLinuxSysFsOpenV(&hFile, pszFormat, va);
if (RT_SUCCESS(rc))
{
size_t cchNum = 0;
char szNum[128];
rc = RTLinuxSysFsReadStr(hFile, szNum, sizeof(szNum), &cchNum);
if (RT_SUCCESS(rc))
{
if (cchNum > 0)
{
uint32_t u32Maj = 0;
uint32_t u32Min = 0;
char *pszNext = NULL;
rc = RTStrToUInt32Ex(szNum, &pszNext, 10, &u32Maj);
if (RT_FAILURE(rc) || (rc != VWRN_TRAILING_CHARS) || (*pszNext != ':'))
rc = VERR_INVALID_PARAMETER;
else
{
rc = RTStrToUInt32Ex(pszNext + 1, NULL, 10, &u32Min);
if ( rc != VINF_SUCCESS
&& rc != VWRN_TRAILING_CHARS
&& rc != VWRN_TRAILING_SPACES)
rc = VERR_INVALID_PARAMETER;
else
*pDevNum = makedev(u32Maj, u32Min);
}
}
else
rc = VERR_INVALID_PARAMETER;
}
RTFileClose(hFile);
}
return rc;
}
RTDECL(int) RTLinuxSysFsReadDevNumFile(dev_t *pDevNum, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsReadDevNumFileV(pDevNum, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, va_list va)
{
RTFILE hFile;
AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
int rc = RTLinuxSysFsOpenV(&hFile, pszFormat, va);
if (RT_SUCCESS(rc))
{
/*
* Note! We cannot use RTLinuxSysFsReadStr here as it has different
* semantics wrt to newline characters. It is not known why
* the semantics has to differ... Michael, any clues?
*/
size_t cchRead;
rc = RTFileRead(hFile, pszBuf, cchBuf, &cchRead);
if (RT_SUCCESS(rc))
{
char *pchNewLine = (char *)memchr(pszBuf, '\n', cchRead);
if (pchNewLine)
{
*pchNewLine = '\0';
cchRead = pchNewLine - pszBuf;
}
else if (cchRead < cchBuf)
pszBuf[cchRead] = '\0';
else
{
if (cchBuf)
{
cchRead = cchBuf - 1;
pszBuf[cchRead] = '\0';
}
else
cchRead = 0;
rc = VERR_BUFFER_OVERFLOW;
}
}
else
cchRead = 0;
RTFileClose(hFile);
if (pcchRead)
*pcchRead = cchRead;
}
else
{
if (cchBuf)
*pszBuf = '\0';
if (pcchRead)
*pcchRead = 0;
}
return rc;
}
RTDECL(int) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, size_t *pcchRead, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsReadStrFileV(pszBuf, cchBuf, pcchRead, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsWriteStrFileV(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, va_list va)
{
RTFILE hFile;
AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
int rc = RTLinuxSysFsOpenExV(&hFile, RTFILE_O_OPEN | RTFILE_O_WRITE | RTFILE_O_DENY_NONE, pszFormat, va);
if (RT_SUCCESS(rc))
{
rc = RTLinuxSysFsWriteStr(hFile, pszBuf, cchBuf, pcchWritten);
RTFileClose(hFile);
}
return rc;
}
RTDECL(int) RTLinuxSysFsWriteStrFile(const char *pszBuf, size_t cchBuf, size_t *pcchWritten, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsWriteStrFileV(pszBuf, cchBuf, pcchWritten, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, va_list va)
{
AssertReturn(cchBuf >= 2, VERR_INVALID_PARAMETER);
/*
* Construct the filename and read the link.
*/
char szFilename[RTPATH_MAX];
int rc = rtLinuxSysFsConstructPath(szFilename, sizeof(szFilename), pszFormat, va);
if (RT_SUCCESS(rc))
{
char szLink[RTPATH_MAX];
rc = RTSymlinkRead(szFilename, szLink, sizeof(szLink), 0);
if (RT_SUCCESS(rc))
{
/*
* Extract the file name component and copy it into the return buffer.
*/
size_t cchName;
const char *pszName = RTPathFilename(szLink);
if (pszName)
{
cchName = strlen(pszName);
if (cchName < cchBuf)
memcpy(pszBuf, pszName, cchName + 1);
else
rc = VERR_BUFFER_OVERFLOW;
}
else
{
*pszBuf = '\0';
cchName = 0;
}
if (pchBuf)
*pchBuf = cchName;
}
}
return rc;
}
RTDECL(int) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, size_t *pchBuf, const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
int rc = RTLinuxSysFsGetLinkDestV(pszBuf, cchBuf, pchBuf, pszFormat, va);
va_end(va);
return rc;
}
RTDECL(int) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode, char *pszBuf,
size_t cchBuf, const char *pszPattern,
va_list va)
{
AssertReturn(cchBuf >= 2, VERR_INVALID_PARAMETER);
AssertReturn( fMode == RTFS_TYPE_DEV_CHAR
|| fMode == RTFS_TYPE_DEV_BLOCK,
VERR_INVALID_PARAMETER);
AssertPtrReturn(pszPattern, VERR_INVALID_PARAMETER);
/*
* Construct the filename and read the link.
*/
char szFilename[RTPATH_MAX];
int rc = rtLinuxConstructPathV(szFilename, sizeof(szFilename), "/dev/",
pszPattern, va);
if (RT_SUCCESS(rc))
{
RTFSOBJINFO Info;
rc = RTPathQueryInfo(szFilename, &Info, RTFSOBJATTRADD_UNIX);
if ( rc == VERR_PATH_NOT_FOUND
|| ( RT_SUCCESS(rc)
&& ( Info.Attr.u.Unix.Device != DevNum
|| (Info.Attr.fMode & RTFS_TYPE_MASK) != fMode)))
rc = VERR_FILE_NOT_FOUND;
if (RT_SUCCESS(rc))
{
size_t cchPath = strlen(szFilename);
if (cchPath < cchBuf)
memcpy(pszBuf, szFilename, cchPath + 1);
else
rc = VERR_BUFFER_OVERFLOW;
}
}
return rc;
}
RTDECL(int) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode, char *pszBuf,
size_t cchBuf, const char *pszPattern,
...)
{
va_list va;
va_start(va, pszPattern);
int rc = RTLinuxCheckDevicePathV(DevNum, fMode, pszBuf, cchBuf,
pszPattern, va);
va_end(va);
return rc;
}
|