Blame alsactl/init_sysfs.c

Packit 229ac0
/*
Packit 229ac0
 * Copyright (C) 2005-2006 Kay Sievers <kay.sievers@vrfy.org>
Packit 229ac0
 *	              2008 Jaroslav Kysela <perex@perex.cz>
Packit 229ac0
 *
Packit 229ac0
 *	This program is free software; you can redistribute it and/or modify it
Packit 229ac0
 *	under the terms of the GNU General Public License as published by the
Packit 229ac0
 *	Free Software Foundation version 2 of the License.
Packit 229ac0
 * 
Packit 229ac0
 *	This program is distributed in the hope that it will be useful, but
Packit 229ac0
 *	WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 229ac0
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 229ac0
 *	General Public License for more details.
Packit 229ac0
 * 
Packit 229ac0
 *	You should have received a copy of the GNU General Public License along
Packit 229ac0
 *	with this program; if not, write to the Free Software Foundation, Inc.,
Packit 229ac0
 *	51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Packit 229ac0
 *
Packit 229ac0
 */
Packit 229ac0
Packit 229ac0
Packit 229ac0
static char sysfs_path[PATH_SIZE];
Packit 229ac0
Packit 229ac0
/* attribute value cache */
Packit 229ac0
static LIST_HEAD(attr_list);
Packit 229ac0
struct sysfs_attr {
Packit 229ac0
	struct list_head node;
Packit 229ac0
	char path[PATH_SIZE];
Packit 229ac0
	char *value;			/* points to value_local if value is cached */
Packit 229ac0
	char value_local[NAME_SIZE];
Packit 229ac0
};
Packit 229ac0
Packit 229ac0
static int sysfs_init(void)
Packit 229ac0
{
Packit 229ac0
	const char *env;
Packit 229ac0
	char sysfs_test[PATH_SIZE];
Packit 229ac0
Packit 229ac0
	env = getenv("SYSFS_PATH");
Packit 229ac0
	if (env) {
Packit 229ac0
		strlcpy(sysfs_path, env, sizeof(sysfs_path));
Packit 229ac0
		remove_trailing_chars(sysfs_path, '/');
Packit 229ac0
	} else
Packit 229ac0
		strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
Packit 229ac0
	dbg("sysfs_path='%s'", sysfs_path);
Packit 229ac0
Packit 229ac0
	strlcpy(sysfs_test, sysfs_path, sizeof(sysfs_test));
Packit 229ac0
	strlcat(sysfs_test, "/kernel/uevent_helper", sizeof(sysfs_test));
Packit 229ac0
	if (access(sysfs_test, F_OK)) {
Packit 229ac0
		error("sysfs path '%s' is invalid\n", sysfs_path);
Packit 229ac0
		return -errno;
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	INIT_LIST_HEAD(&attr_list);
Packit 229ac0
	return 0;
Packit 229ac0
}
Packit 229ac0
Packit 229ac0
static void sysfs_cleanup(void)
Packit 229ac0
{
Packit 229ac0
	struct sysfs_attr *attr_loop;
Packit 229ac0
	struct sysfs_attr *attr_temp;
Packit 229ac0
Packit 229ac0
	list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
Packit 229ac0
		list_del(&attr_loop->node);
Packit 229ac0
		free(attr_loop);
Packit 229ac0
	}
Packit 229ac0
}
Packit 229ac0
Packit 229ac0
static char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
Packit 229ac0
{
Packit 229ac0
	char path_full[PATH_SIZE];
Packit 229ac0
	const char *path;
Packit 229ac0
	char value[NAME_SIZE];
Packit 229ac0
	struct sysfs_attr *attr_loop;
Packit 229ac0
	struct sysfs_attr *attr;
Packit 229ac0
	struct stat statbuf;
Packit 229ac0
	int fd;
Packit 229ac0
	ssize_t size;
Packit 229ac0
	size_t sysfs_len;
Packit 229ac0
Packit 229ac0
	dbg("open '%s'/'%s'", devpath, attr_name);
Packit 229ac0
	sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
Packit 229ac0
	path = &path_full[sysfs_len];
Packit 229ac0
	strlcat(path_full, devpath, sizeof(path_full));
Packit 229ac0
	strlcat(path_full, "/", sizeof(path_full));
Packit 229ac0
	strlcat(path_full, attr_name, sizeof(path_full));
Packit 229ac0
Packit 229ac0
	/* look for attribute in cache */
Packit 229ac0
	list_for_each_entry(attr_loop, &attr_list, node) {
Packit 229ac0
		if (strcmp(attr_loop->path, path) == 0) {
Packit 229ac0
			dbg("found in cache '%s'", attr_loop->path);
Packit 229ac0
			return attr_loop->value;
Packit 229ac0
		}
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	/* store attribute in cache (also negatives are kept in cache) */
Packit 229ac0
	dbg("new uncached attribute '%s'", path_full);
Packit 229ac0
	attr = malloc(sizeof(struct sysfs_attr));
Packit 229ac0
	if (attr == NULL)
Packit 229ac0
		return NULL;
Packit 229ac0
	memset(attr, 0x00, sizeof(struct sysfs_attr));
Packit 229ac0
	strlcpy(attr->path, path, sizeof(attr->path));
Packit 229ac0
	dbg("add to cache '%s'", path_full);
Packit 229ac0
	list_add(&attr->node, &attr_list);
Packit 229ac0
Packit 229ac0
	if (lstat(path_full, &statbuf) != 0) {
Packit 229ac0
		dbg("stat '%s' failed: %s", path_full, strerror(errno));
Packit 229ac0
		goto out;
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	if (S_ISLNK(statbuf.st_mode)) {
Packit 229ac0
		/* links return the last element of the target path */
Packit 229ac0
		char link_target[PATH_SIZE + 1];
Packit 229ac0
		int len;
Packit 229ac0
		const char *pos;
Packit 229ac0
Packit 229ac0
		len = readlink(path_full, link_target, sizeof(link_target) - 1);
Packit 229ac0
		if (len > 0) {
Packit 229ac0
			link_target[len] = '\0';
Packit 229ac0
			pos = strrchr(link_target, '/');
Packit 229ac0
			if (pos != NULL) {
Packit 229ac0
				dbg("cache '%s' with link value '%s'", path_full, pos+1);
Packit 229ac0
				strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
Packit 229ac0
				attr->value = attr->value_local;
Packit 229ac0
			}
Packit 229ac0
		}
Packit 229ac0
		goto out;
Packit 229ac0
	}
Packit 229ac0
Packit 229ac0
	/* skip directories */
Packit 229ac0
	if (S_ISDIR(statbuf.st_mode))
Packit 229ac0
		goto out;
Packit 229ac0
Packit 229ac0
	/* skip non-readable files */
Packit 229ac0
	if ((statbuf.st_mode & S_IRUSR) == 0)
Packit 229ac0
		goto out;
Packit 229ac0
Packit 229ac0
	/* read attribute value */
Packit 229ac0
	fd = open(path_full, O_RDONLY);
Packit 229ac0
	if (fd < 0) {
Packit 229ac0
		dbg("attribute '%s' does not exist", path_full);
Packit 229ac0
		goto out;
Packit 229ac0
	}
Packit 229ac0
	size = read(fd, value, sizeof(value));
Packit 229ac0
	close(fd);
Packit 229ac0
	if (size < 0)
Packit 229ac0
		goto out;
Packit 229ac0
	if (size == sizeof(value))
Packit 229ac0
		goto out;
Packit 229ac0
Packit 229ac0
	/* got a valid value, store and return it */
Packit 229ac0
	value[size] = '\0';
Packit 229ac0
	remove_trailing_chars(value, '\n');
Packit 229ac0
	dbg("cache '%s' with attribute value '%s'", path_full, value);
Packit 229ac0
	strlcpy(attr->value_local, value, sizeof(attr->value_local));
Packit 229ac0
	attr->value = attr->value_local;
Packit 229ac0
Packit 229ac0
out:
Packit 229ac0
	return attr->value;
Packit 229ac0
}