tarina

git clone https://git.tarina.org/tarina
Log | Files | Refs | README | LICENSE

init_sysfs.c (4334B)


      1 /*
      2  * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
      3  *	              2008 Jaroslav Kysela <perex@perex.cz>
      4  *
      5  *	This program is free software; you can redistribute it and/or modify it
      6  *	under the terms of the GNU General Public License as published by the
      7  *	Free Software Foundation version 2 of the License.
      8  * 
      9  *	This program is distributed in the hope that it will be useful, but
     10  *	WITHOUT ANY WARRANTY; without even the implied warranty of
     11  *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  *	General Public License for more details.
     13  * 
     14  *	You should have received a copy of the GNU General Public License along
     15  *	with this program; if not, write to the Free Software Foundation, Inc.,
     16  *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     17  *
     18  */
     19 
     20 
     21 static char sysfs_path[PATH_SIZE];
     22 
     23 /* attribute value cache */
     24 static LIST_HEAD(attr_list);
     25 struct sysfs_attr {
     26 	struct list_head node;
     27 	char path[PATH_SIZE];
     28 	char *value;			/* points to value_local if value is cached */
     29 	char value_local[NAME_SIZE];
     30 };
     31 
     32 static int sysfs_init(void)
     33 {
     34 	const char *env;
     35 	char sysfs_test[PATH_SIZE];
     36 
     37 	env = getenv("SYSFS_PATH");
     38 	if (env) {
     39 		strlcpy(sysfs_path, env, sizeof(sysfs_path));
     40 		remove_trailing_chars(sysfs_path, '/');
     41 	} else
     42 		strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
     43 	dbg("sysfs_path='%s'", sysfs_path);
     44 
     45 	strlcpy(sysfs_test, sysfs_path, sizeof(sysfs_test));
     46 	strlcat(sysfs_test, "/kernel/uevent_helper", sizeof(sysfs_test));
     47 	if (access(sysfs_test, F_OK)) {
     48 		error("sysfs path '%s' is invalid\n", sysfs_path);
     49 		return -errno;
     50 	}
     51 
     52 	INIT_LIST_HEAD(&attr_list);
     53 	return 0;
     54 }
     55 
     56 static void sysfs_cleanup(void)
     57 {
     58 	struct sysfs_attr *attr_loop;
     59 	struct sysfs_attr *attr_temp;
     60 
     61 	list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
     62 		list_del(&attr_loop->node);
     63 		free(attr_loop);
     64 	}
     65 }
     66 
     67 static char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
     68 {
     69 	char path_full[PATH_SIZE];
     70 	const char *path;
     71 	char value[NAME_SIZE];
     72 	struct sysfs_attr *attr_loop;
     73 	struct sysfs_attr *attr;
     74 	struct stat statbuf;
     75 	int fd;
     76 	ssize_t size;
     77 	size_t sysfs_len;
     78 
     79 	dbg("open '%s'/'%s'", devpath, attr_name);
     80 	sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
     81 	path = &path_full[sysfs_len];
     82 	strlcat(path_full, devpath, sizeof(path_full));
     83 	strlcat(path_full, "/", sizeof(path_full));
     84 	strlcat(path_full, attr_name, sizeof(path_full));
     85 
     86 	/* look for attribute in cache */
     87 	list_for_each_entry(attr_loop, &attr_list, node) {
     88 		if (strcmp(attr_loop->path, path) == 0) {
     89 			dbg("found in cache '%s'", attr_loop->path);
     90 			return attr_loop->value;
     91 		}
     92 	}
     93 
     94 	/* store attribute in cache (also negatives are kept in cache) */
     95 	dbg("new uncached attribute '%s'", path_full);
     96 	attr = malloc(sizeof(struct sysfs_attr));
     97 	if (attr == NULL)
     98 		return NULL;
     99 	memset(attr, 0x00, sizeof(struct sysfs_attr));
    100 	strlcpy(attr->path, path, sizeof(attr->path));
    101 	dbg("add to cache '%s'", path_full);
    102 	list_add(&attr->node, &attr_list);
    103 
    104 	if (lstat(path_full, &statbuf) != 0) {
    105 		dbg("stat '%s' failed: %s", path_full, strerror(errno));
    106 		goto out;
    107 	}
    108 
    109 	if (S_ISLNK(statbuf.st_mode)) {
    110 		/* links return the last element of the target path */
    111 		char link_target[PATH_SIZE + 1];
    112 		int len;
    113 		const char *pos;
    114 
    115 		len = readlink(path_full, link_target, sizeof(link_target) - 1);
    116 		if (len > 0) {
    117 			link_target[len] = '\0';
    118 			pos = strrchr(link_target, '/');
    119 			if (pos != NULL) {
    120 				dbg("cache '%s' with link value '%s'", path_full, pos+1);
    121 				strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
    122 				attr->value = attr->value_local;
    123 			}
    124 		}
    125 		goto out;
    126 	}
    127 
    128 	/* skip directories */
    129 	if (S_ISDIR(statbuf.st_mode))
    130 		goto out;
    131 
    132 	/* skip non-readable files */
    133 	if ((statbuf.st_mode & S_IRUSR) == 0)
    134 		goto out;
    135 
    136 	/* read attribute value */
    137 	fd = open(path_full, O_RDONLY);
    138 	if (fd < 0) {
    139 		dbg("attribute '%s' does not exist", path_full);
    140 		goto out;
    141 	}
    142 	size = read(fd, value, sizeof(value));
    143 	close(fd);
    144 	if (size < 0)
    145 		goto out;
    146 	if (size == sizeof(value))
    147 		goto out;
    148 
    149 	/* got a valid value, store and return it */
    150 	value[size] = '\0';
    151 	remove_trailing_chars(value, '\n');
    152 	dbg("cache '%s' with attribute value '%s'", path_full, value);
    153 	strlcpy(attr->value_local, value, sizeof(attr->value_local));
    154 	attr->value = attr->value_local;
    155 
    156 out:
    157 	return attr->value;
    158 }