forked from numactl/numactl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sysfs.c
69 lines (62 loc) · 1.11 KB
/
sysfs.c
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
/* Utility functions for reading sysfs values */
#define _GNU_SOURCE 1
#include <stdio.h>
#include <sys/fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdarg.h>
#include <ctype.h>
#include "numa.h"
#include "numaint.h"
#define SYSFS_BLOCK 4096
hidden char *sysfs_read(char *name)
{
char *buf;
int n;
int fd;
fd = open(name, O_RDONLY);
buf = malloc(SYSFS_BLOCK);
if (!buf)
return NULL;
n = read(fd, buf, SYSFS_BLOCK - 1);
close(fd);
if (n <= 0) {
free(buf);
return NULL;
}
buf[n] = 0;
return buf;
}
hidden int sysfs_node_read(struct bitmask *mask, char *fmt, ...)
{
int n;
va_list ap;
char *p, *fn, *m, *end;
int num;
va_start(ap, fmt);
n = vasprintf(&fn, fmt, ap);
va_end(ap);
if (n < 0)
return -1;
p = sysfs_read(fn);
free(fn);
if (!p)
return -1;
m = p;
do {
num = strtol(m, &end, 0);
if (m == end)
return -1;
if (num < 0)
return -2;
if (num >= numa_num_task_nodes())
return -1;
numa_bitmask_setbit(mask, num);
/* Continuation not supported by kernel yet. */
m = end;
while (isspace(*m) || *m == ',')
m++;
} while (isdigit(*m));
free(p);
return 0;
}