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