]>
Commit | Line | Data |
---|---|---|
be9b51f6 KS |
1 | /* |
2 | * udevinfo - fetches attributes for a device | |
3 | * | |
4 | * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org> | |
5 | * | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation version 2 of the License. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; if not, write to the Free Software Foundation, Inc., | |
18 | * 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | * | |
20 | */ | |
21 | ||
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <stdio.h> | |
034f35d7 | 25 | #include <ctype.h> |
87171e46 KS |
26 | #include <stdarg.h> |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
be9b51f6 | 29 | |
869fc2f1 GKH |
30 | #include "udev.h" |
31 | #include "udev_version.h" | |
32 | #include "logging.h" | |
33 | #include "udevdb.h" | |
34 | #include "libsysfs/libsysfs.h" | |
be9b51f6 KS |
35 | |
36 | ||
87171e46 | 37 | # define SYSFS_VALUE_MAX 200 |
be9b51f6 KS |
38 | |
39 | char **main_argv; | |
87171e46 | 40 | int main_argc; |
be9b51f6 | 41 | |
d026a35d GKH |
42 | #ifdef LOG |
43 | unsigned char logname[42]; | |
44 | void log_message (int level, const char *format, ...) | |
51a8bb2f | 45 | { |
d026a35d GKH |
46 | va_list args; |
47 | ||
48 | va_start(args, format); | |
49 | vsyslog(level, format, args); | |
50 | va_end(args); | |
51a8bb2f | 51 | } |
d026a35d | 52 | #endif |
51a8bb2f | 53 | |
87171e46 | 54 | static int print_all_attributes(const char *path) |
be9b51f6 KS |
55 | { |
56 | struct dlist *attributes; | |
57 | struct sysfs_attribute *attr; | |
58 | struct sysfs_directory *sysfs_dir; | |
87171e46 | 59 | char value[SYSFS_VALUE_MAX]; |
be9b51f6 KS |
60 | int len; |
61 | int retval = 0; | |
62 | ||
63 | sysfs_dir = sysfs_open_directory(path); | |
64 | if (sysfs_dir == NULL) | |
65 | return -1; | |
66 | ||
67 | attributes = sysfs_get_dir_attributes(sysfs_dir); | |
68 | if (attributes == NULL) { | |
69 | retval = -1; | |
70 | goto exit; | |
71 | } | |
72 | ||
73 | dlist_for_each_data(attributes, attr, struct sysfs_attribute) { | |
74 | if (attr->value != NULL) { | |
87171e46 | 75 | strncpy(value, attr->value, SYSFS_VALUE_MAX); |
be9b51f6 | 76 | len = strlen(value); |
034f35d7 KS |
77 | if (len == 0) |
78 | continue; | |
79 | ||
80 | /* remove trailing newline */ | |
81 | if (value[len-1] == '\n') { | |
be9b51f6 | 82 | value[len-1] = '\0'; |
034f35d7 KS |
83 | len--; |
84 | } | |
85 | ||
86 | /* skip nonprintable values */ | |
87 | while (len) { | |
88 | if (isprint(value[len-1]) == 0) | |
89 | break; | |
90 | len--; | |
91 | } | |
92 | if (len == 0) | |
93 | printf(" SYSFS_%s=\"%s\"\n", attr->name, value); | |
be9b51f6 KS |
94 | } |
95 | } | |
96 | printf("\n"); | |
97 | ||
98 | exit: | |
99 | sysfs_close_directory(sysfs_dir); | |
100 | ||
101 | return retval; | |
102 | } | |
103 | ||
87171e46 KS |
104 | /* callback for database dump */ |
105 | static int print_record(char *path, struct udevice *dev) | |
106 | { | |
107 | printf("P: %s\n", path); | |
108 | printf("N: %s\n", dev->name); | |
109 | printf("M: %#o\n", dev->mode); | |
110 | printf("S: %s\n", dev->symlink); | |
111 | printf("O: %s\n", dev->owner); | |
112 | printf("G: %s\n", dev->group); | |
113 | printf("\n"); | |
114 | return 0; | |
115 | } | |
116 | ||
117 | enum query_type { | |
118 | NONE, | |
119 | NAME, | |
120 | PATH, | |
121 | SYMLINK, | |
122 | MODE, | |
123 | OWNER, | |
124 | GROUP | |
125 | }; | |
126 | ||
127 | static int print_device_chain(const char *path) | |
be9b51f6 | 128 | { |
be9b51f6 KS |
129 | struct sysfs_class_device *class_dev; |
130 | struct sysfs_class_device *class_dev_parent; | |
131 | struct sysfs_attribute *attr; | |
034f35d7 KS |
132 | struct sysfs_device *sysfs_dev; |
133 | struct sysfs_device *sysfs_dev_parent; | |
be9b51f6 KS |
134 | int retval = 0; |
135 | ||
be9b51f6 KS |
136 | /* get the class dev */ |
137 | class_dev = sysfs_open_class_device_path(path); | |
138 | if (class_dev == NULL) { | |
139 | printf("couldn't get the class device\n"); | |
140 | return -1; | |
141 | } | |
142 | ||
143 | /* read the 'dev' file for major/minor*/ | |
144 | attr = sysfs_get_classdev_attr(class_dev, "dev"); | |
145 | if (attr == NULL) { | |
146 | printf("couldn't get the \"dev\" file\n"); | |
147 | retval = -1; | |
148 | goto exit; | |
149 | } | |
034f35d7 | 150 | printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value); |
be9b51f6 KS |
151 | sysfs_close_attribute(attr); |
152 | ||
153 | /* open sysfs class device directory and print all attributes */ | |
034f35d7 | 154 | printf(" looking at class device '%s':\n", class_dev->path); |
be9b51f6 KS |
155 | if (print_all_attributes(class_dev->path) != 0) { |
156 | printf("couldn't open class device directory\n"); | |
157 | retval = -1; | |
158 | goto exit; | |
159 | } | |
160 | ||
034f35d7 | 161 | /* get the device link (if parent exists look here) */ |
be9b51f6 KS |
162 | class_dev_parent = sysfs_get_classdev_parent(class_dev); |
163 | if (class_dev_parent != NULL) { | |
164 | //sysfs_close_class_device(class_dev); | |
165 | class_dev = class_dev_parent; | |
166 | } | |
034f35d7 KS |
167 | sysfs_dev = sysfs_get_classdev_device(class_dev); |
168 | if (sysfs_dev != NULL) | |
169 | printf("follow the class device's \"device\"\n"); | |
170 | ||
171 | /* look the device chain upwards */ | |
172 | while (sysfs_dev != NULL) { | |
173 | printf(" looking at the device chain at '%s':\n", sysfs_dev->path); | |
174 | printf(" BUS=\"%s\"\n", sysfs_dev->bus); | |
175 | printf(" ID=\"%s\"\n", sysfs_dev->bus_id); | |
be9b51f6 KS |
176 | |
177 | /* open sysfs device directory and print all attributes */ | |
034f35d7 KS |
178 | print_all_attributes(sysfs_dev->path); |
179 | ||
180 | sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev); | |
181 | if (sysfs_dev_parent == NULL) | |
182 | break; | |
183 | ||
184 | //sysfs_close_device(sysfs_dev); | |
185 | sysfs_dev = sysfs_dev_parent; | |
be9b51f6 | 186 | } |
034f35d7 | 187 | sysfs_close_device(sysfs_dev); |
be9b51f6 KS |
188 | |
189 | exit: | |
190 | //sysfs_close_class_device(class_dev); | |
191 | return retval; | |
192 | } | |
87171e46 KS |
193 | |
194 | static int process_options(void) | |
195 | { | |
196 | static const char short_options[] = "adn:p:q:rVh"; | |
197 | int option; | |
198 | int retval = 1; | |
199 | struct udevice dev; | |
200 | int root = 0; | |
201 | int attributes = 0; | |
202 | enum query_type query = NONE; | |
203 | char result[NAME_SIZE] = ""; | |
204 | char path[NAME_SIZE] = ""; | |
205 | char name[NAME_SIZE] = ""; | |
206 | char temp[NAME_SIZE]; | |
207 | char *pos; | |
208 | ||
209 | /* get command line options */ | |
210 | while (1) { | |
211 | option = getopt(main_argc, main_argv, short_options); | |
212 | if (option == -1) | |
213 | break; | |
214 | ||
215 | dbg("option '%c'", option); | |
216 | switch (option) { | |
217 | case 'n': | |
218 | dbg("udev name: %s\n", optarg); | |
219 | strfieldcpy(name, optarg); | |
220 | break; | |
221 | ||
222 | case 'p': | |
223 | dbg("udev path: %s\n", optarg); | |
224 | strfieldcpy(path, optarg); | |
225 | break; | |
226 | ||
227 | case 'q': | |
228 | dbg("udev query: %s\n", optarg); | |
229 | ||
230 | if (strcmp(optarg, "name") == 0) { | |
231 | query = NAME; | |
232 | break; | |
233 | } | |
234 | ||
235 | if (strcmp(optarg, "symlink") == 0) { | |
236 | query = SYMLINK; | |
237 | break; | |
238 | } | |
239 | ||
240 | if (strcmp(optarg, "mode") == 0) { | |
241 | query = MODE; | |
242 | break; | |
243 | } | |
244 | ||
245 | if (strcmp(optarg, "owner") == 0) { | |
246 | query = OWNER; | |
247 | break; | |
248 | } | |
249 | ||
250 | if (strcmp(optarg, "group") == 0) { | |
251 | query = GROUP; | |
252 | break; | |
253 | } | |
254 | ||
255 | if (strcmp(optarg, "path") == 0) { | |
256 | query = PATH; | |
257 | break; | |
258 | } | |
259 | ||
260 | printf("unknown query type\n"); | |
261 | exit(1); | |
262 | ||
263 | case 'r': | |
264 | root = 1; | |
265 | break; | |
266 | ||
267 | case 'a': | |
268 | attributes = 1; | |
269 | break; | |
270 | ||
271 | case 'd': | |
272 | retval = udevdb_open_ro(); | |
273 | if (retval != 0) { | |
274 | printf("unable to open udev database\n"); | |
275 | exit(2); | |
276 | } | |
277 | udevdb_call_foreach(print_record); | |
278 | udevdb_exit(); | |
279 | exit(0); | |
280 | ||
281 | case 'V': | |
869fc2f1 | 282 | printf("udevinfo, version %s\n", UDEV_VERSION); |
87171e46 KS |
283 | exit(0); |
284 | ||
285 | case 'h': | |
286 | retval = 0; | |
287 | case '?': | |
288 | default: | |
289 | goto help; | |
290 | } | |
291 | } | |
292 | ||
293 | /* process options */ | |
294 | if (query != NONE) { | |
295 | retval = udevdb_open_ro(); | |
296 | if (retval != 0) { | |
297 | printf("unable to open udev database\n"); | |
298 | return -EACCES; | |
299 | } | |
300 | ||
301 | if (path[0] != '\0') { | |
302 | /* remove sysfs_path if given */ | |
303 | if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) { | |
304 | pos = path + strlen(sysfs_path); | |
305 | } else { | |
306 | if (path[0] != '/') { | |
307 | /* prepend '/' if missing */ | |
308 | strcat(temp, "/"); | |
309 | strncat(temp, path, sizeof(path)); | |
310 | pos = temp; | |
311 | } else { | |
312 | pos = path; | |
313 | } | |
314 | } | |
315 | retval = udevdb_get_dev(pos, &dev); | |
316 | if (retval != 0) { | |
317 | printf("device not found in database\n"); | |
318 | goto exit; | |
319 | } | |
320 | goto print; | |
321 | } | |
322 | ||
323 | if (name[0] != '\0') { | |
324 | /* remove udev_root if given */ | |
325 | if (strncmp(name, udev_root, strlen(udev_root)) == 0) { | |
326 | pos = name + strlen(udev_root); | |
327 | } else | |
328 | pos = name; | |
329 | retval = udevdb_get_dev_byname(pos, path, &dev); | |
330 | if (retval != 0) { | |
331 | printf("device not found in database\n"); | |
332 | goto exit; | |
333 | } | |
334 | goto print; | |
335 | } | |
336 | ||
337 | printf("query needs device path(-p) or node name(-n) specified\n"); | |
338 | goto exit; | |
339 | ||
340 | print: | |
341 | switch(query) { | |
342 | case NAME: | |
343 | if (root) | |
344 | strfieldcpy(result, udev_root); | |
345 | strncat(result, dev.name, sizeof(result)); | |
346 | break; | |
347 | ||
348 | case SYMLINK: | |
349 | strfieldcpy(result, dev.symlink); | |
350 | break; | |
351 | ||
352 | case MODE: | |
353 | sprintf(result, "%#o", dev.mode); | |
354 | break; | |
355 | ||
356 | case GROUP: | |
357 | strfieldcpy(result, dev.group); | |
358 | break; | |
359 | ||
360 | case OWNER: | |
361 | strfieldcpy(result, dev.owner); | |
362 | break; | |
363 | ||
364 | case PATH: | |
365 | strfieldcpy(result, path); | |
366 | break; | |
367 | ||
368 | default: | |
369 | goto exit; | |
370 | } | |
371 | printf("%s\n", result); | |
372 | ||
373 | exit: | |
374 | udevdb_exit(); | |
375 | return retval; | |
376 | } | |
377 | ||
378 | if (attributes) { | |
379 | if (path[0] == '\0') { | |
380 | printf("attribute walk on device chain needs path(-p) specified\n"); | |
381 | return -EINVAL; | |
382 | } else { | |
383 | if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) { | |
384 | /* prepend sysfs mountpoint if not given */ | |
385 | strfieldcpy(temp, path); | |
386 | strfieldcpy(path, sysfs_path); | |
387 | strncat(path, temp, sizeof(path)); | |
388 | } | |
389 | print_device_chain(path); | |
390 | return 0; | |
391 | } | |
392 | } | |
393 | ||
394 | if (root) { | |
395 | printf("%s\n", udev_root); | |
396 | return 0; | |
397 | } | |
398 | ||
399 | help: | |
400 | printf("Usage: [-anpqrdVh]\n" | |
401 | " -q TYPE query database for the specified value:\n" | |
402 | " 'name' name of device node\n" | |
403 | " 'symlink' pointing to node\n" | |
404 | " 'mode' permissions of node\n" | |
405 | " 'owner' of node\n" | |
406 | " 'group' of node\n" | |
407 | " 'path' sysfs device path\n" | |
408 | " -p PATH sysfs device path used for query or chain\n" | |
409 | " -n NAME node name used for query\n" | |
410 | "\n" | |
411 | " -r print udev root\n" | |
2a25816f | 412 | " -a print all SYSFS_attributes along the device chain\n" |
87171e46 KS |
413 | " -d dump whole database\n" |
414 | " -V print udev version\n" | |
415 | " -h print this help text\n" | |
416 | "\n"); | |
417 | return retval; | |
418 | } | |
419 | ||
420 | int main(int argc, char *argv[], char *envp[]) | |
421 | { | |
422 | int retval; | |
423 | ||
424 | main_argv = argv; | |
425 | main_argc = argc; | |
87171e46 | 426 | |
95a6f4c8 GKH |
427 | init_logging("udevinfo"); |
428 | ||
87171e46 KS |
429 | /* initialize our configuration */ |
430 | udev_init_config(); | |
431 | ||
432 | retval = process_options(); | |
433 | if (retval != 0) | |
434 | exit(1); | |
435 | exit(0); | |
436 | } |