chiark / gitweb /
[PATCH] no error on enoent
[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 };
127
128 static int print_device_chain(const char *path)
129 {
130         struct sysfs_class_device *class_dev;
131         struct sysfs_class_device *class_dev_parent;
132         struct sysfs_attribute *attr;
133         struct sysfs_device *sysfs_dev;
134         struct sysfs_device *sysfs_dev_parent;
135         int retval = 0;
136
137         /*  get the class dev */
138         class_dev = sysfs_open_class_device_path(path);
139         if (class_dev == NULL) {
140                 printf("couldn't get the class device\n");
141                 return -1;
142         }
143
144         /* read the 'dev' file for major/minor*/
145         attr = sysfs_get_classdev_attr(class_dev, "dev");
146         if (attr == NULL) {
147                 printf("couldn't get the \"dev\" file\n");
148                 retval = -1;
149                 goto exit;
150         }
151         printf("\ndevice '%s' has major:minor %s", class_dev->path, attr->value);
152         sysfs_close_attribute(attr);
153
154         /* open sysfs class device directory and print all attributes */
155         printf("  looking at class device '%s':\n", class_dev->path);
156         if (print_all_attributes(class_dev->path) != 0) {
157                 printf("couldn't open class device directory\n");
158                 retval = -1;
159                 goto exit;
160         }
161
162         /* get the device link (if parent exists look here) */
163         class_dev_parent = sysfs_get_classdev_parent(class_dev);
164         if (class_dev_parent != NULL) {
165                 //sysfs_close_class_device(class_dev);
166                 class_dev = class_dev_parent;
167         }
168         sysfs_dev = sysfs_get_classdev_device(class_dev);
169         if (sysfs_dev != NULL)
170                 printf("follow the class device's \"device\"\n");
171
172         /* look the device chain upwards */
173         while (sysfs_dev != NULL) {
174                 printf("  looking at the device chain at '%s':\n", sysfs_dev->path);
175                 printf("    BUS=\"%s\"\n", sysfs_dev->bus);
176                 printf("    ID=\"%s\"\n", sysfs_dev->bus_id);
177
178                 /* open sysfs device directory and print all attributes */
179                 print_all_attributes(sysfs_dev->path);
180
181                 sysfs_dev_parent = sysfs_get_device_parent(sysfs_dev);
182                 if (sysfs_dev_parent == NULL)
183                         break;
184
185                 //sysfs_close_device(sysfs_dev);
186                 sysfs_dev = sysfs_dev_parent;
187         }
188         sysfs_close_device(sysfs_dev);
189
190 exit:
191         //sysfs_close_class_device(class_dev);
192         return retval;
193 }
194
195 static int process_options(void)
196 {
197         static const char short_options[] = "adn:p:q:rVh";
198         int option;
199         int retval = 1;
200         struct udevice dev;
201         int root = 0;
202         int attributes = 0;
203         enum query_type query = NONE;
204         char result[NAME_SIZE] = "";
205         char path[NAME_SIZE] = "";
206         char name[NAME_SIZE] = "";
207         char temp[NAME_SIZE];
208         char *pos;
209
210         /* get command line options */
211         while (1) {
212                 option = getopt(main_argc, main_argv, short_options);
213                 if (option == -1)
214                         break;
215
216                 dbg("option '%c'", option);
217                 switch (option) {
218                 case 'n':
219                         dbg("udev name: %s\n", optarg);
220                         strfieldcpy(name, optarg);
221                         break;
222
223                 case 'p':
224                         dbg("udev path: %s\n", optarg);
225                         strfieldcpy(path, optarg);
226                         break;
227
228                 case 'q':
229                         dbg("udev query: %s\n", optarg);
230
231                         if (strcmp(optarg, "name") == 0) {
232                                 query = NAME;
233                                 break;
234                         }
235
236                         if (strcmp(optarg, "symlink") == 0) {
237                                 query = SYMLINK;
238                                 break;
239                         }
240
241                         if (strcmp(optarg, "mode") == 0) {
242                                 query = MODE;
243                                 break;
244                         }
245
246                         if (strcmp(optarg, "owner") == 0) {
247                                 query = OWNER;
248                                 break;
249                         }
250
251                         if (strcmp(optarg, "group") == 0) {
252                                 query = GROUP;
253                                 break;
254                         }
255
256                         if (strcmp(optarg, "path") == 0) {
257                                 query = PATH;
258                                 break;
259                         }
260
261                         printf("unknown query type\n");
262                         exit(1);
263
264                 case 'r':
265                         root = 1;
266                         break;
267
268                 case 'a':
269                         attributes = 1;
270                         break;
271
272                 case 'd':
273                         retval = udevdb_open_ro();
274                         if (retval != 0) {
275                                 printf("unable to open udev database\n");
276                                 exit(2);
277                         }
278                         udevdb_call_foreach(print_record);
279                         udevdb_exit();
280                         exit(0);
281
282                 case 'V':
283                         printf("udevinfo, version %s\n", UDEV_VERSION);
284                         exit(0);
285
286                 case 'h':
287                         retval = 0;
288                 case '?':
289                 default:
290                         goto help;
291                 }
292         }
293
294         /* process options */
295         if (query != NONE) {
296                 retval = udevdb_open_ro();
297                 if (retval != 0) {
298                         printf("unable to open udev database\n");
299                         return -EACCES;
300                 }
301
302                 if (path[0] != '\0') {
303                         /* remove sysfs_path if given */
304                         if (strncmp(path, sysfs_path, strlen(sysfs_path)) == 0) {
305                                 pos = path + strlen(sysfs_path);
306                         } else {
307                                 if (path[0] != '/') {
308                                         /* prepend '/' if missing */
309                                         strfieldcat(temp, "/");
310                                         strfieldcat(temp, path);
311                                         pos = temp;
312                                 } else {
313                                         pos = path;
314                                 }
315                         }
316                         retval = udevdb_get_dev(pos, &dev);
317                         if (retval != 0) {
318                                 printf("device not found in database\n");
319                                 goto exit;
320                         }
321                         goto print;
322                 }
323
324                 if (name[0] != '\0') {
325                         /* remove udev_root if given */
326                         if (strncmp(name, udev_root, strlen(udev_root)) == 0) {
327                                 pos = name + strlen(udev_root);
328                         } else
329                                 pos = name;
330                         retval = udevdb_get_dev_byname(pos, path, &dev);
331                         if (retval != 0) {
332                                 printf("device not found in database\n");
333                                 goto exit;
334                         }
335                         goto print;
336                 }
337
338                 printf("query needs device path(-p) or node name(-n) specified\n");
339                 goto exit;
340
341 print:
342                 switch(query) {
343                 case NAME:
344                         if (root)
345                                 strfieldcpy(result, udev_root);
346                         strfieldcat(result, dev.name);
347                         break;
348
349                 case SYMLINK:
350                         strfieldcpy(result, dev.symlink);
351                         break;
352
353                 case MODE:
354                         sprintf(result, "%#o", dev.mode);
355                         break;
356
357                 case GROUP:
358                         strfieldcpy(result, dev.group);
359                         break;
360
361                 case OWNER:
362                         strfieldcpy(result, dev.owner);
363                         break;
364
365                 case PATH:
366                         strfieldcpy(result, path);
367                         break;
368
369                 default:
370                         goto exit;
371                 }
372                 printf("%s\n", result);
373
374 exit:
375                 udevdb_exit();
376                 return retval;
377         }
378
379         if (attributes) {
380                 if (path[0] == '\0') {
381                         printf("attribute walk on device chain needs path(-p) specified\n");
382                         return -EINVAL;
383                 } else {
384                         if (strncmp(path, sysfs_path, strlen(sysfs_path)) != 0) {
385                                 /* prepend sysfs mountpoint if not given */
386                                 strfieldcpy(temp, path);
387                                 strfieldcpy(path, sysfs_path);
388                                 strfieldcat(path, temp);
389                         }
390                         print_device_chain(path);
391                         return 0;
392                 }
393         }
394
395         if (root) {
396                 printf("%s\n", udev_root);
397                 return 0;
398         }
399
400 help:
401         printf("Usage: [-anpqrdVh]\n"
402                "  -q TYPE  query database for the specified value:\n"
403                "             'name'    name of device node\n"
404                "             'symlink' pointing to node\n"
405                "             'mode'    permissions of node\n"
406                "             'owner'   of node\n"
407                "             'group'   of node\n"
408                "             'path'    sysfs device path\n"
409                "  -p PATH  sysfs device path used for query or chain\n"
410                "  -n NAME  node name used for query\n"
411                "\n"
412                "  -r       print udev root\n"
413                "  -a       print all SYSFS_attributes along the device chain\n"
414                "  -d       dump whole database\n"
415                "  -V       print udev version\n"
416                "  -h       print this help text\n"
417                "\n");
418         return retval;
419 }
420
421 int main(int argc, char *argv[], char *envp[])
422 {
423         int retval;
424
425         main_argv = argv;
426         main_argc = argc;
427
428         init_logging("udevinfo");
429
430         /* initialize our configuration */
431         udev_init_config();
432
433         retval = process_options();
434         if (retval != 0)
435                 exit(1);
436         exit(0);
437 }