Blame alsactl/init_sysfs.c

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