]>
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.
30 #include "libsysfs/sysfs/libsysfs.h"
31 #include "libsysfs/dlist.h"
33 #include "udev_version.h"
38 # define SYSFS_VALUE_MAX 200
44 unsigned char logname
[42];
45 void log_message (int level
, const char *format
, ...)
49 va_start(args
, format
);
50 vsyslog(level
, format
, args
);
55 static int print_all_attributes(const char *path
)
57 struct dlist
*attributes
;
58 struct sysfs_attribute
*attr
;
59 struct sysfs_directory
*sysfs_dir
;
60 char value
[SYSFS_VALUE_MAX
];
64 sysfs_dir
= sysfs_open_directory(path
);
65 if (sysfs_dir
== NULL
)
68 attributes
= sysfs_get_dir_attributes(sysfs_dir
);
69 if (attributes
== NULL
) {
74 dlist_for_each_data(attributes
, attr
, struct sysfs_attribute
) {
75 if (attr
->value
!= NULL
) {
76 strfieldcpy(value
, attr
->value
);
81 /* remove trailing newline */
82 if (value
[len
-1] == '\n') {
87 /* skip nonprintable values */
89 if (isprint(value
[len
-1]) == 0)
94 printf(" SYSFS{%s}=\"%s\"\n", attr
->name
, value
);
100 sysfs_close_directory(sysfs_dir
);
105 /* callback for database dump */
106 static int print_record(char *path
, struct udevice
*dev
)
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
);
128 static int print_device_chain(const char *path
)
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
;
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");
144 /* read the 'dev' file for major/minor*/
145 attr
= sysfs_get_classdev_attr(class_dev
, "dev");
147 printf("couldn't get the \"dev\" file\n");
151 printf("\ndevice '%s' has major:minor %s", class_dev
->path
, attr
->value
);
152 sysfs_close_attribute(attr
);
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");
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
;
168 sysfs_dev
= sysfs_get_classdev_device(class_dev
);
169 if (sysfs_dev
!= NULL
)
170 printf("follow the class device's \"device\"\n");
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
);
178 /* open sysfs device directory and print all attributes */
179 print_all_attributes(sysfs_dev
->path
);
181 sysfs_dev_parent
= sysfs_get_device_parent(sysfs_dev
);
182 if (sysfs_dev_parent
== NULL
)
185 //sysfs_close_device(sysfs_dev);
186 sysfs_dev
= sysfs_dev_parent
;
188 sysfs_close_device(sysfs_dev
);
191 //sysfs_close_class_device(class_dev);
195 static int process_options(void)
197 static const char short_options
[] = "adn:p:q:rVh";
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
];
210 /* get command line options */
212 option
= getopt(main_argc
, main_argv
, short_options
);
216 dbg("option '%c'", option
);
219 dbg("udev name: %s\n", optarg
);
220 strfieldcpy(name
, optarg
);
224 dbg("udev path: %s\n", optarg
);
225 strfieldcpy(path
, optarg
);
229 dbg("udev query: %s\n", optarg
);
231 if (strcmp(optarg
, "name") == 0) {
236 if (strcmp(optarg
, "symlink") == 0) {
241 if (strcmp(optarg
, "mode") == 0) {
246 if (strcmp(optarg
, "owner") == 0) {
251 if (strcmp(optarg
, "group") == 0) {
256 if (strcmp(optarg
, "path") == 0) {
261 printf("unknown query type\n");
273 retval
= udevdb_open_ro();
275 printf("unable to open udev database\n");
278 udevdb_call_foreach(print_record
);
283 printf("udevinfo, version %s\n", UDEV_VERSION
);
294 /* process options */
296 retval
= udevdb_open_ro();
298 printf("unable to open udev database\n");
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
);
307 if (path
[0] != '/') {
308 /* prepend '/' if missing */
309 strfieldcat(temp
, "/");
310 strfieldcat(temp
, path
);
316 retval
= udevdb_get_dev(pos
, &dev
);
318 printf("device not found in database\n");
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
);
330 retval
= udevdb_get_dev_byname(pos
, path
, &dev
);
332 printf("device not found in database\n");
338 printf("query needs device path(-p) or node name(-n) specified\n");
345 strfieldcpy(result
, udev_root
);
346 strfieldcat(result
, dev
.name
);
350 strfieldcpy(result
, dev
.symlink
);
354 sprintf(result
, "%#o", dev
.mode
);
358 strfieldcpy(result
, dev
.group
);
362 strfieldcpy(result
, dev
.owner
);
366 strfieldcpy(result
, path
);
372 printf("%s\n", result
);
380 if (path
[0] == '\0') {
381 printf("attribute walk on device chain needs path(-p) specified\n");
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
);
390 print_device_chain(path
);
396 printf("%s\n", udev_root
);
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"
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"
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"
421 int main(int argc
, char *argv
[], char *envp
[])
428 init_logging("udevinfo");
430 /* initialize our configuration */
433 retval
= process_options();