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