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