]>
git.ipfire.org Git - thirdparty/systemd.git/blob - udev/udevadm-test.c
c53c27329556e8a8e659262f6d9c36ab7979b183
2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 int udevadm_test(struct udev
*udev
, int argc
, char *argv
[])
35 char filename
[UTIL_PATH_SIZE
];
36 const char *action
= "add";
37 const char *syspath
= NULL
;
38 struct udev_event
*event
;
39 struct udev_device
*dev
;
40 struct udev_rules
*rules
= NULL
;
44 static const struct option options
[] = {
45 { "action", required_argument
, NULL
, 'a' },
46 { "help", no_argument
, NULL
, 'h' },
50 info(udev
, "version %s\n", VERSION
);
55 option
= getopt_long(argc
, argv
, "a:s:fh", options
, NULL
);
59 dbg(udev
, "option '%c'\n", option
);
65 printf("Usage: udevadm test OPTIONS <syspath>\n"
66 " --action=<string> set action string\n"
73 syspath
= argv
[optind
];
75 if (syspath
== NULL
) {
76 fprintf(stderr
, "syspath parameter missing\n");
81 printf("This program is for debugging only, it does not run any program,\n"
82 "specified by a RUN key. It may show incorrect results, because\n"
83 "some values may be different, or not available at a simulation run.\n"
86 rules
= udev_rules_new(udev
, 1);
88 fprintf(stderr
, "error reading rules\n");
93 /* add /sys if needed */
94 if (strncmp(syspath
, udev_get_sys_path(udev
), strlen(udev_get_sys_path(udev
))) != 0) {
95 util_strlcpy(filename
, udev_get_sys_path(udev
), sizeof(filename
));
96 util_strlcat(filename
, syspath
, sizeof(filename
));
98 util_strlcpy(filename
, syspath
, sizeof(filename
));
100 util_remove_trailing_chars(filename
, '/');
102 dev
= udev_device_new_from_syspath(udev
, filename
);
104 fprintf(stderr
, "unable to open device '%s'\n", filename
);
109 /* skip reading of db, but read kernel parameters */
110 udev_device_set_info_loaded(dev
);
111 udev_device_read_uevent_file(dev
);
113 udev_device_set_action(dev
, action
);
114 event
= udev_event_new(dev
);
115 err
= udev_event_execute_rules(event
, rules
);
117 if (udev_device_get_event_timeout(dev
) >= 0)
118 info(udev
, "custom event timeout: %i\n", udev_device_get_event_timeout(dev
));
120 if (err
== 0 && !event
->ignore_device
&& udev_get_run(udev
)) {
121 struct udev_list_entry
*entry
;
123 udev_list_entry_foreach(entry
, udev_list_get_entry(&event
->run_list
)) {
124 char program
[UTIL_PATH_SIZE
];
126 util_strlcpy(program
, udev_list_entry_get_name(entry
), sizeof(program
));
127 udev_event_apply_format(event
, program
, sizeof(program
));
128 info(udev
, "run: '%s'\n", program
);
131 udev_event_unref(event
);
132 udev_device_unref(dev
);
134 udev_rules_unref(rules
);