Back to index

numactl  2.0.8~rc4
sysfs.c
Go to the documentation of this file.
00001 /* Utility functions for reading sysfs values */
00002 #define _GNU_SOURCE 1
00003 #include <stdio.h>
00004 #include <sys/fcntl.h>
00005 #include <stdlib.h>
00006 #include <unistd.h>
00007 #include <stdarg.h>
00008 #include <ctype.h>
00009 #include "numa.h"
00010 #include "numaint.h"
00011 
00012 #define SYSFS_BLOCK 4096
00013 
00014 hidden char *sysfs_read(char *name)
00015 {
00016        char *buf;
00017        int n;
00018        int fd;
00019 
00020        fd = open(name, O_RDONLY);
00021        buf = malloc(SYSFS_BLOCK);
00022        if (!buf)
00023               return NULL;
00024        n = read(fd, buf, SYSFS_BLOCK - 1);
00025        close(fd);
00026        if (n <= 0) {
00027               free(buf);
00028               return NULL;
00029        }
00030        buf[n] = 0;
00031        return buf;
00032 }
00033 
00034 hidden int sysfs_node_read(struct bitmask *mask, char *fmt, ...)
00035 {
00036        int n;
00037        va_list ap;
00038        char *p, *fn, *m, *end;
00039        int num;
00040 
00041        va_start(ap, fmt);
00042        n = vasprintf(&fn, fmt, ap);
00043        va_end(ap);
00044        if (n < 0)
00045               return -1;
00046        p = sysfs_read(fn);
00047        free(fn);
00048        if (!p)
00049               return -1;
00050 
00051        m = p;
00052        do {
00053               num = strtol(m, &end, 0);
00054               if (m == end)
00055                      return -1;
00056               if (num < 0)
00057                      return -2;
00058               if (num >= numa_num_task_nodes())
00059                      return -1;
00060               numa_bitmask_setbit(mask, num);
00061 
00062               /* Continuation not supported by kernel yet. */
00063               m = end;
00064               while (isspace(*m) || *m == ',')
00065                      m++;
00066        } while (isdigit(*m));
00067        free(p);
00068        return 0;
00069 }