]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udevinfo.c
2 * udevinfo - fetches attributes for a device
4 * Copyright (C) 2004 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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.
31 #include "udev_version.h"
34 #include "libsysfs/libsysfs.h"
37 # define SYSFS_VALUE_MAX 200
42 static int print_all_attributes(const char *path
)
44 struct dlist
*attributes
;
45 struct sysfs_attribute
*attr
;
46 struct sysfs_directory
*sysfs_dir
;
47 char value
[SYSFS_VALUE_MAX
];
51 sysfs_dir
= sysfs_open_directory(path
);
52 if (sysfs_dir
== NULL
)
55 attributes
= sysfs_get_dir_attributes(sysfs_dir
);
56 if (attributes
== NULL
) {
61 dlist_for_each_data(attributes
, attr
, struct sysfs_attribute
) {
62 if (attr
->value
!= NULL
) {
63 strncpy(value
, attr
->value
, SYSFS_VALUE_MAX
);
68 /* remove trailing newline */
69 if (value
[len
-1] == '\n') {
74 /* skip nonprintable values */
76 if (isprint(value
[len
-1]) == 0)
81 printf(" SYSFS_%s=\"%s\"\n", attr
->name
, value
);
87 sysfs_close_directory(sysfs_dir
);
92 /* callback for database dump */
93 static int print_record(char *path
, struct udevice
*dev
)
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
);
115 static int print_device_chain(const char *path
)
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
;
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");
131 /* read the 'dev' file for major/minor*/
132 attr
= sysfs_get_classdev_attr(class_dev
, "dev");
134 printf("couldn't get the \"dev\" file\n");
138 printf("\ndevice '%s' has major:minor %s", class_dev
->path
, attr
->value
);
139 sysfs_close_attribute(attr
);
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");
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
;
155 sysfs_dev
= sysfs_get_classdev_device(class_dev
);
156 if (sysfs_dev
!= NULL
)
157 printf("follow the class device's \"device\"\n");
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
);
165 /* open sysfs device directory and print all attributes */
166 print_all_attributes(sysfs_dev
->path
);
168 sysfs_dev_parent
= sysfs_get_device_parent(sysfs_dev
);
169 if (sysfs_dev_parent
== NULL
)
172 //sysfs_close_device(sysfs_dev);
173 sysfs_dev
= sysfs_dev_parent
;
175 sysfs_close_device(sysfs_dev
);
178 //sysfs_close_class_device(class_dev);
182 static int process_options(void)
184 static const char short_options
[] = "adn:p:q:rVh";
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
];
197 /* get command line options */
199 option
= getopt(main_argc
, main_argv
, short_options
);
203 dbg("option '%c'", option
);
206 dbg("udev name: %s\n", optarg
);
207 strfieldcpy(name
, optarg
);
211 dbg("udev path: %s\n", optarg
);
212 strfieldcpy(path
, optarg
);
216 dbg("udev query: %s\n", optarg
);
218 if (strcmp(optarg
, "name") == 0) {
223 if (strcmp(optarg
, "symlink") == 0) {
228 if (strcmp(optarg
, "mode") == 0) {
233 if (strcmp(optarg
, "owner") == 0) {
238 if (strcmp(optarg
, "group") == 0) {
243 if (strcmp(optarg
, "path") == 0) {
248 printf("unknown query type\n");
260 retval
= udevdb_open_ro();
262 printf("unable to open udev database\n");
265 udevdb_call_foreach(print_record
);
270 printf("udevinfo, version %s\n", UDEV_VERSION
);
281 /* process options */
283 retval
= udevdb_open_ro();
285 printf("unable to open udev database\n");
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
);
294 if (path
[0] != '/') {
295 /* prepend '/' if missing */
297 strncat(temp
, path
, sizeof(path
));
303 retval
= udevdb_get_dev(pos
, &dev
);
305 printf("device not found in database\n");
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
);
317 retval
= udevdb_get_dev_byname(pos
, path
, &dev
);
319 printf("device not found in database\n");
325 printf("query needs device path(-p) or node name(-n) specified\n");
332 strfieldcpy(result
, udev_root
);
333 strncat(result
, dev
.name
, sizeof(result
));
337 strfieldcpy(result
, dev
.symlink
);
341 sprintf(result
, "%#o", dev
.mode
);
345 strfieldcpy(result
, dev
.group
);
349 strfieldcpy(result
, dev
.owner
);
353 strfieldcpy(result
, path
);
359 printf("%s\n", result
);
367 if (path
[0] == '\0') {
368 printf("attribute walk on device chain needs path(-p) specified\n");
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
));
377 print_device_chain(path
);
383 printf("%s\n", udev_root
);
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"
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"
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"
408 int main(int argc
, char *argv
[], char *envp
[])
415 /* initialize our configuration */
418 retval
= process_options();