]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev/udevadm-test.c
merge udev-rules.c and udev-rules-parse.c
[thirdparty/systemd.git] / udev / udevadm-test.c
CommitLineData
bb051f66 1/*
e8d569b4 2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
55e9959b 3 * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
bb051f66 4 *
55e9959b
KS
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.
bb051f66 9 *
55e9959b
KS
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.
14 *
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/>.
bb051f66
GKH
17 */
18
19#include <stdlib.h>
20#include <string.h>
21#include <stdio.h>
1aa1e248
KS
22#include <stddef.h>
23#include <unistd.h>
bb051f66
GKH
24#include <errno.h>
25#include <ctype.h>
68d4af11 26#include <fcntl.h>
bb051f66 27#include <signal.h>
6b493a20 28#include <syslog.h>
eff4a673 29#include <getopt.h>
bb051f66
GKH
30
31#include "udev.h"
bb051f66 32
7d563a17 33int udevadm_test(struct udev *udev, int argc, char *argv[])
bb051f66 34{
aa8734ff 35 char filename[UTIL_PATH_SIZE];
eff4a673 36 int force = 0;
285e2a24 37 const char *action = "add";
aa8734ff
KS
38 const char *syspath = NULL;
39 struct udev_event *event;
40 struct udev_device *dev;
df97701e 41 struct udev_rules rules = {};
aa8734ff 42 int err;
1aa1e248 43 int rc = 0;
8a0acf85 44
eff4a673 45 static const struct option options[] = {
033e9f8c 46 { "action", required_argument, NULL, 'a' },
033e9f8c
KS
47 { "force", no_argument, NULL, 'f' },
48 { "help", no_argument, NULL, 'h' },
eff4a673
KS
49 {}
50 };
51
7d563a17
KS
52 info(udev, "version %s\n", VERSION);
53
eff4a673
KS
54 while (1) {
55 int option;
56
df97701e 57 option = getopt_long(argc, argv, "a:s:fh", options, NULL);
eff4a673
KS
58 if (option == -1)
59 break;
60
7d563a17 61 dbg(udev, "option '%c'\n", option);
eff4a673
KS
62 switch (option) {
63 case 'a':
64 action = optarg;
65 break;
66 case 'f':
67 force = 1;
68 break;
69 case 'h':
aa8734ff 70 printf("Usage: udevadm test OPTIONS <syspath>\n"
df97701e 71 " --action=<string> set action string\n"
df97701e
KS
72 " --force don't skip node/link creation\n"
73 " --help print this help text\n\n");
eff4a673
KS
74 exit(0);
75 default:
76 exit(1);
77 }
e3396a2d 78 }
aa8734ff 79 syspath = argv[optind];
e3396a2d 80
aa8734ff
KS
81 if (syspath == NULL) {
82 fprintf(stderr, "syspath parameter missing\n");
e3396a2d
KS
83 rc = 1;
84 goto exit;
e03a196a
KS
85 }
86
86fd6b4f
KS
87 printf("This program is for debugging only, it does not run any program,\n"
88 "specified by a RUN key. It may show incorrect results, because\n"
89 "some values may be different, or not available at a simulation run.\n"
90 "\n");
91
7d563a17 92 udev_rules_init(udev, &rules, 0);
2362eea6 93
aa8734ff
KS
94 /* add /sys if needed */
95 if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) {
96 util_strlcpy(filename, udev_get_sys_path(udev), sizeof(filename));
97 util_strlcat(filename, syspath, sizeof(filename));
98 syspath = filename;
99 }
bb051f66 100
aa8734ff 101 dev = udev_device_new_from_syspath(udev, syspath);
1aa1e248 102 if (dev == NULL) {
aa8734ff 103 fprintf(stderr, "unable to open device '%s'\n", syspath);
1aa1e248
KS
104 rc = 2;
105 goto exit;
106 }
7a947ce5 107
aa8734ff
KS
108 /* skip reading of db, but read kernel parameters */
109 udev_device_set_info_loaded(dev);
110 udev_device_read_uevent_file(dev);
7a947ce5 111
aa8734ff
KS
112 udev_device_set_action(dev, action);
113 event = udev_event_new(dev);
d27d3bb0 114
5d24c6ca 115 /* simulate node creation with test flag */
eff4a673 116 if (!force)
aa8734ff 117 event->test = 1;
1aa1e248 118
aa8734ff 119 err = udev_event_run(event, &rules);
bf50425b 120
aa8734ff
KS
121 if (udev_device_get_event_timeout(dev) >= 0)
122 info(udev, "custom event timeout: %i\n", udev_device_get_event_timeout(dev));
bf50425b 123
aa8734ff
KS
124 if (err == 0 && !event->ignore_device && udev_get_run(udev)) {
125 struct udev_list_entry *entry;
1aa1e248 126
aa8734ff 127 udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
17fcfb59 128 char program[UTIL_PATH_SIZE];
ad27f5b3 129
aa8734ff
KS
130 util_strlcpy(program, udev_list_entry_get_name(entry), sizeof(program));
131 udev_rules_apply_format(event, program, sizeof(program));
7d563a17 132 info(udev, "run: '%s'\n", program);
ad27f5b3 133 }
1aa1e248 134 }
aa8734ff
KS
135 udev_event_unref(event);
136 udev_device_unref(dev);
1aa1e248
KS
137exit:
138 udev_rules_cleanup(&rules);
1aa1e248 139 return rc;
bb051f66 140}