Home | History | Annotate | Download | only in util
      1 
      2 #include "util.h"
      3 #include "sysfs.h"
      4 
      5 static const char * const sysfs_known_mountpoints[] = {
      6 	"/sys",
      7 	0,
      8 };
      9 
     10 static int sysfs_found;
     11 char sysfs_mountpoint[PATH_MAX + 1];
     12 
     13 static int sysfs_valid_mountpoint(const char *sysfs)
     14 {
     15 #ifndef __APPLE__
     16 	struct statfs st_fs;
     17 
     18 	if (statfs(sysfs, &st_fs) < 0)
     19 		return -ENOENT;
     20 	else if (st_fs.f_type != (long) SYSFS_MAGIC)
     21 		return -ENOENT;
     22 #endif
     23 
     24 	return 0;
     25 }
     26 
     27 const char *sysfs_find_mountpoint(void)
     28 {
     29 	const char * const *ptr;
     30 	char type[100];
     31 	FILE *fp;
     32 
     33 	if (sysfs_found)
     34 		return (const char *) sysfs_mountpoint;
     35 
     36 	ptr = sysfs_known_mountpoints;
     37 	while (*ptr) {
     38 		if (sysfs_valid_mountpoint(*ptr) == 0) {
     39 			sysfs_found = 1;
     40 			strcpy(sysfs_mountpoint, *ptr);
     41 			return sysfs_mountpoint;
     42 		}
     43 		ptr++;
     44 	}
     45 
     46 	/* give up and parse /proc/mounts */
     47 	fp = fopen("/proc/mounts", "r");
     48 	if (fp == NULL)
     49 		return NULL;
     50 
     51 	while (!sysfs_found &&
     52 	       fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
     53 		      sysfs_mountpoint, type) == 2) {
     54 
     55 		if (strcmp(type, "sysfs") == 0)
     56 			sysfs_found = 1;
     57 	}
     58 
     59 	fclose(fp);
     60 
     61 	return sysfs_found ? sysfs_mountpoint : NULL;
     62 }
     63