diff options
Diffstat (limited to 'tools/lib/api/fs/fs.c')
-rw-r--r-- | tools/lib/api/fs/fs.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c index 459599d1b6c4..ef78c22ff44d 100644 --- a/tools/lib/api/fs/fs.c +++ b/tools/lib/api/fs/fs.c | |||
@@ -13,6 +13,7 @@ | |||
13 | #include <sys/mount.h> | 13 | #include <sys/mount.h> |
14 | 14 | ||
15 | #include "fs.h" | 15 | #include "fs.h" |
16 | #include "debug-internal.h" | ||
16 | 17 | ||
17 | #define _STR(x) #x | 18 | #define _STR(x) #x |
18 | #define STR(x) _STR(x) | 19 | #define STR(x) _STR(x) |
@@ -300,6 +301,56 @@ int filename__read_ull(const char *filename, unsigned long long *value) | |||
300 | return err; | 301 | return err; |
301 | } | 302 | } |
302 | 303 | ||
304 | #define STRERR_BUFSIZE 128 /* For the buffer size of strerror_r */ | ||
305 | |||
306 | int filename__read_str(const char *filename, char **buf, size_t *sizep) | ||
307 | { | ||
308 | size_t size = 0, alloc_size = 0; | ||
309 | void *bf = NULL, *nbf; | ||
310 | int fd, n, err = 0; | ||
311 | char sbuf[STRERR_BUFSIZE]; | ||
312 | |||
313 | fd = open(filename, O_RDONLY); | ||
314 | if (fd < 0) | ||
315 | return -errno; | ||
316 | |||
317 | do { | ||
318 | if (size == alloc_size) { | ||
319 | alloc_size += BUFSIZ; | ||
320 | nbf = realloc(bf, alloc_size); | ||
321 | if (!nbf) { | ||
322 | err = -ENOMEM; | ||
323 | break; | ||
324 | } | ||
325 | |||
326 | bf = nbf; | ||
327 | } | ||
328 | |||
329 | n = read(fd, bf + size, alloc_size - size); | ||
330 | if (n < 0) { | ||
331 | if (size) { | ||
332 | pr_warning("read failed %d: %s\n", errno, | ||
333 | strerror_r(errno, sbuf, sizeof(sbuf))); | ||
334 | err = 0; | ||
335 | } else | ||
336 | err = -errno; | ||
337 | |||
338 | break; | ||
339 | } | ||
340 | |||
341 | size += n; | ||
342 | } while (n > 0); | ||
343 | |||
344 | if (!err) { | ||
345 | *sizep = size; | ||
346 | *buf = bf; | ||
347 | } else | ||
348 | free(bf); | ||
349 | |||
350 | close(fd); | ||
351 | return err; | ||
352 | } | ||
353 | |||
303 | int sysfs__read_ull(const char *entry, unsigned long long *value) | 354 | int sysfs__read_ull(const char *entry, unsigned long long *value) |
304 | { | 355 | { |
305 | char path[PATH_MAX]; | 356 | char path[PATH_MAX]; |
@@ -326,6 +377,19 @@ int sysfs__read_int(const char *entry, int *value) | |||
326 | return filename__read_int(path, value); | 377 | return filename__read_int(path, value); |
327 | } | 378 | } |
328 | 379 | ||
380 | int sysfs__read_str(const char *entry, char **buf, size_t *sizep) | ||
381 | { | ||
382 | char path[PATH_MAX]; | ||
383 | const char *sysfs = sysfs__mountpoint(); | ||
384 | |||
385 | if (!sysfs) | ||
386 | return -1; | ||
387 | |||
388 | snprintf(path, sizeof(path), "%s/%s", sysfs, entry); | ||
389 | |||
390 | return filename__read_str(path, buf, sizep); | ||
391 | } | ||
392 | |||
329 | int sysctl__read_int(const char *sysctl, int *value) | 393 | int sysctl__read_int(const char *sysctl, int *value) |
330 | { | 394 | { |
331 | char path[PATH_MAX]; | 395 | char path[PATH_MAX]; |