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