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
sysfs_init(void)32 static int sysfs_init(void)
33 {
34 const char *env;
35 char sysfs_test[PATH_SIZE];
36
37 INIT_LIST_HEAD(&attr_list);
38
39 env = getenv("SYSFS_PATH");
40 if (env) {
41 strlcpy(sysfs_path, env, sizeof(sysfs_path));
42 remove_trailing_chars(sysfs_path, '/');
43 } else
44 strlcpy(sysfs_path, "/sys", sizeof(sysfs_path));
45 dbg("sysfs_path='%s'", sysfs_path);
46
47 strlcpy(sysfs_test, sysfs_path, sizeof(sysfs_test));
48 strlcat(sysfs_test, "/kernel/uevent_seqnum", sizeof(sysfs_test));
49 if (access(sysfs_test, F_OK)) {
50 strlcpy(sysfs_test, sysfs_path, sizeof(sysfs_test));
51 strlcat(sysfs_test, "/kernel/uevent_helper", sizeof(sysfs_test));
52 if (access(sysfs_test, F_OK)) {
53 error("sysfs path '%s' is invalid", sysfs_path);
54 return -errno;
55 }
56 }
57
58 return 0;
59 }
60
sysfs_cleanup(void)61 static void sysfs_cleanup(void)
62 {
63 struct sysfs_attr *attr_loop;
64 struct sysfs_attr *attr_temp;
65
66 list_for_each_entry_safe(attr_loop, attr_temp, &attr_list, node) {
67 list_del(&attr_loop->node);
68 free(attr_loop);
69 }
70 }
71
sysfs_attr_get_value(const char * devpath,const char * attr_name)72 static char *sysfs_attr_get_value(const char *devpath, const char *attr_name)
73 {
74 char path_full[PATH_SIZE];
75 const char *path;
76 char value[NAME_SIZE];
77 struct sysfs_attr *attr_loop;
78 struct sysfs_attr *attr;
79 struct stat statbuf;
80 int fd;
81 ssize_t size;
82 size_t sysfs_len;
83
84 dbg("open '%s'/'%s'", devpath, attr_name);
85 sysfs_len = strlcpy(path_full, sysfs_path, sizeof(path_full));
86 path = &path_full[sysfs_len];
87 strlcat(path_full, devpath, sizeof(path_full));
88 strlcat(path_full, "/", sizeof(path_full));
89 strlcat(path_full, attr_name, sizeof(path_full));
90
91 /* look for attribute in cache */
92 list_for_each_entry(attr_loop, &attr_list, node) {
93 if (strcmp(attr_loop->path, path) == 0) {
94 dbg("found in cache '%s'", attr_loop->path);
95 return attr_loop->value;
96 }
97 }
98
99 /* store attribute in cache (also negatives are kept in cache) */
100 dbg("new uncached attribute '%s'", path_full);
101 attr = malloc(sizeof(struct sysfs_attr));
102 if (attr == NULL)
103 return NULL;
104 memset(attr, 0x00, sizeof(struct sysfs_attr));
105 strlcpy(attr->path, path, sizeof(attr->path));
106 dbg("add to cache '%s'", path_full);
107 list_add(&attr->node, &attr_list);
108
109 if (lstat(path_full, &statbuf) != 0) {
110 dbg("stat '%s' failed: %s", path_full, strerror(errno));
111 goto out;
112 }
113
114 if (S_ISLNK(statbuf.st_mode)) {
115 /* links return the last element of the target path */
116 char link_target[PATH_SIZE + 1];
117 int len;
118 const char *pos;
119
120 len = readlink(path_full, link_target, sizeof(link_target) - 1);
121 if (len > 0) {
122 link_target[len] = '\0';
123 pos = strrchr(link_target, '/');
124 if (pos != NULL) {
125 dbg("cache '%s' with link value '%s'", path_full, pos+1);
126 strlcpy(attr->value_local, &pos[1], sizeof(attr->value_local));
127 attr->value = attr->value_local;
128 }
129 }
130 goto out;
131 }
132
133 /* skip directories */
134 if (S_ISDIR(statbuf.st_mode))
135 goto out;
136
137 /* skip non-readable files */
138 if ((statbuf.st_mode & S_IRUSR) == 0)
139 goto out;
140
141 /* read attribute value */
142 fd = open(path_full, O_RDONLY);
143 if (fd < 0) {
144 dbg("attribute '%s' does not exist", path_full);
145 goto out;
146 }
147 size = read(fd, value, sizeof(value));
148 close(fd);
149 if (size < 0)
150 goto out;
151 if (size == sizeof(value))
152 goto out;
153
154 /* got a valid value, store and return it */
155 value[size] = '\0';
156 remove_trailing_chars(value, '\n');
157 dbg("cache '%s' with attribute value '%s'", path_full, value);
158 strlcpy(attr->value_local, value, sizeof(attr->value_local));
159 attr->value = attr->value_local;
160
161 out:
162 return attr->value;
163 }
164