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