VirtualBox

source: vbox/trunk/include/iprt/linux/sysfs.h@ 53420

Last change on this file since 53420 was 50783, checked in by vboxsync, 11 years ago

Main and Runtime/Linux: rip out all code for recursively walking /dev, as it is not needed on modern Linux systems and partly visibly broken.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.9 KB
Line 
1/* $Id: sysfs.h 50783 2014-03-14 09:57:47Z vboxsync $ */
2/** @file
3 * IPRT - Linux sysfs access.
4 */
5
6/*
7 * Copyright (C) 2008-2010 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#ifndef ___iprt_linux_sysfs_h
28#define ___iprt_linux_sysfs_h
29
30#include <iprt/cdefs.h>
31#include <iprt/types.h>
32#include <iprt/stdarg.h>
33
34#include <sys/types.h> /* for dev_t */
35
36RT_C_DECLS_BEGIN
37
38/** @defgroup grp_rt_linux_sysfs RTLinuxSysfs - Linux sysfs
39 * @ingroup grp_rt
40 * @{
41 */
42
43/**
44 * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
45 *
46 * @returns true / false, errno is preserved.
47 * @param pszFormat The name format, either absolute or relative to "/sys/".
48 * @param va The format args.
49 */
50RTDECL(bool) RTLinuxSysFsExistsV(const char *pszFormat, va_list va);
51
52/**
53 * Checks if a sysfs file (or directory, device, symlink, whatever) exists.
54 *
55 * @returns true / false, errno is preserved.
56 * @param pszFormat The name format, either absolute or relative to "/sys/".
57 * @param ... The format args.
58 */
59RTDECL(bool) RTLinuxSysFsExists(const char *pszFormat, ...);
60
61/**
62 * Opens a sysfs file.
63 *
64 * @returns The file descriptor. -1 and errno on failure.
65 * @param pszFormat The name format, either absolute or relative to "/sys/".
66 * @param va The format args.
67 */
68RTDECL(int) RTLinuxSysFsOpenV(const char *pszFormat, va_list va);
69
70/**
71 * Opens a sysfs file.
72 *
73 * @returns The file descriptor. -1 and errno on failure.
74 * @param pszFormat The name format, either absolute or relative to "/sys/".
75 * @param ... The format args.
76 */
77RTDECL(int) RTLinuxSysFsOpen(const char *pszFormat, ...);
78
79/**
80 * Closes a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
81 *
82 * @param fd File descriptor returned by RTLinuxSysFsOpen or
83 * RTLinuxSysFsOpenV.
84 */
85RTDECL(void) RTLinuxSysFsClose(int fd);
86
87/**
88 * Reads a string from a file opened with RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
89 *
90 * @returns The number of bytes read. -1 and errno on failure.
91 * @param fd The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
92 * @param pszBuf Where to store the string.
93 * @param cchBuf The size of the buffer. Must be at least 2 bytes.
94 */
95RTDECL(ssize_t) RTLinuxSysFsReadStr(int fd, char *pszBuf, size_t cchBuf);
96
97/**
98 * Reads the remainder of a file opened with RTLinuxSysFsOpen or
99 * RTLinuxSysFsOpenV.
100 *
101 * @returns IPRT status code.
102 * @param fd The file descriptor returned by RTLinuxSysFsOpen or RTLinuxSysFsOpenV.
103 * @param pvBuf Where to store the bits from the file.
104 * @param cbBuf The size of the buffer.
105 * @param pcbRead Where to return the number of bytes read. Optional.
106 */
107RTDECL(int) RTLinuxSysFsReadFile(int fd, void *pvBuf, size_t cbBuf, size_t *pcbRead);
108
109/**
110 * Reads a number from a sysfs file.
111 *
112 * @returns 64-bit signed value on success, -1 and errno on failure.
113 * @param uBase The number base, 0 for autodetect.
114 * @param pszFormat The filename format, either absolute or relative to "/sys/".
115 * @param va Format args.
116 */
117RTDECL(int64_t) RTLinuxSysFsReadIntFileV(unsigned uBase, const char *pszFormat, va_list va);
118
119/**
120 * Reads a number from a sysfs file.
121 *
122 * @returns 64-bit signed value on success, -1 and errno on failure.
123 * @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 */
127RTDECL(int64_t) RTLinuxSysFsReadIntFile(unsigned uBase, const char *pszFormat, ...);
128
129/**
130 * Reads a device number from a sysfs file.
131 *
132 * @returns device number on success, 0 and errno on failure.
133 * @param pszFormat The filename format, either absolute or relative to "/sys/".
134 * @param va Format args.
135 */
136RTDECL(dev_t) RTLinuxSysFsReadDevNumFileV(const char *pszFormat, va_list va);
137
138/**
139 * Reads a device number from a sysfs file.
140 *
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 */
145RTDECL(dev_t) RTLinuxSysFsReadDevNumFile(const char *pszFormat, ...);
146
147/**
148 * Reads a string from a sysfs file. If the file contains a newline, we only
149 * return the text up until there.
150 *
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.
155 * @param pszFormat The filename format, either absolute or relative to "/sys/".
156 * @param va Format args.
157 */
158RTDECL(ssize_t) RTLinuxSysFsReadStrFileV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va);
159
160/**
161 * Reads a string from a sysfs file. If the file contains a newline, we only
162 * return the text up until there.
163 *
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 */
171RTDECL(ssize_t) RTLinuxSysFsReadStrFile(char *pszBuf, size_t cchBuf, const char *pszFormat, ...);
172
173/**
174 * Reads the last element of the path of the file pointed to by the symbolic
175 * link specified.
176 *
177 * This is needed at least to get the name of the driver associated with a
178 * device, where pszFormat should be the "driver" link in the devices sysfs
179 * directory.
180 *
181 * @returns The length of the returned string on success, -1 and errno on
182 * failure.
183 * @param pszBuf Where to store the path element. Must be at least two
184 * characters, but a longer buffer would be advisable.
185 * @param cchBuf The size of the buffer pointed to by @a pszBuf.
186 * @param pszFormat The filename format, either absolute or relative to "/sys/".
187 * @param va Format args.
188 */
189RTDECL(ssize_t) RTLinuxSysFsGetLinkDestV(char *pszBuf, size_t cchBuf, const char *pszFormat, va_list va);
190
191/**
192 * Reads the last element of the path of the file pointed to by the symbolic
193 * link specified.
194 *
195 * This is needed at least to get the name of the driver associated with a
196 * device, where pszFormat should be the "driver" link in the devices sysfs
197 * directory.
198 *
199 * @returns The length of the returned string on success, -1 and errno on
200 * failure.
201 * @param pszBuf Where to store the path element. Must be at least two
202 * 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 */
207RTDECL(ssize_t) RTLinuxSysFsGetLinkDest(char *pszBuf, size_t cchBuf, const char *pszFormat, ...);
208
209/**
210 * Check the path of a device node under /dev, given the device number and a
211 * pattern and store the path into @a pszBuf.
212 *
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.
216 * @param DevNum The device number to search for.
217 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
218 * RTFS_TYPE_DEV_BLOCK are valid values.
219 * @param pszBuf Where to store the path.
220 * @param cchBuf The size of the buffer.
221 * @param pszPattern The expected path format of the device node, either
222 * absolute or relative to "/dev".
223 * @param va Format args.
224 */
225RTDECL(ssize_t) RTLinuxCheckDevicePathV(dev_t DevNum, RTFMODE fMode,
226 char *pszBuf, size_t cchBuf,
227 const char *pszPattern, va_list va);
228
229/**
230 * Check the path of a device node under /dev, given the device number and a
231 * pattern and store the path into @a pszBuf.
232 *
233 * @returns The length of the returned string on success, -1 and errno on
234 * failure.
235 * @returns -1 and ENOENT if no matching device node could be found.
236 * @param DevNum The device number to search for
237 * @param fMode The type of device - only RTFS_TYPE_DEV_CHAR and
238 * RTFS_TYPE_DEV_BLOCK are valid values
239 * @param pszBuf Where to store the path.
240 * @param cchBuf The size of the buffer.
241 * @param pszPattern The expected path format of the device node, either
242 * absolute or relative to "/dev".
243 * @param ... Format args.
244 */
245RTDECL(ssize_t) RTLinuxCheckDevicePath(dev_t DevNum, RTFMODE fMode,
246 char *pszBuf, size_t cchBuf,
247 const char *pszPattern, ...);
248
249/** @} */
250
251RT_C_DECLS_END
252
253#endif
254
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