chiark / gitweb /
994a354960280315a321477f1ba17aa4d28fff6f
[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 static int import_uevent_var(struct udev *udev, const char *devpath)
35 {
36         char path[PATH_SIZE];
37         static char value[4096]; /* must stay, used with putenv */
38         ssize_t size;
39         int fd;
40         char *key;
41         char *next;
42         int rc = -1;
43
44         /* read uevent file */
45         strlcpy(path, udev_get_sys_path(udev), sizeof(path));
46         strlcat(path, devpath, sizeof(path));
47         strlcat(path, "/uevent", sizeof(path));
48         fd = open(path, O_RDONLY);
49         if (fd < 0)
50                 goto out;
51         size = read(fd, value, sizeof(value));
52         close(fd);
53         if (size < 0)
54                 goto out;
55         value[size] = '\0';
56
57         /* import keys into environment */
58         key = value;
59         while (key[0] != '\0') {
60                 next = strchr(key, '\n');
61                 if (next == NULL)
62                         goto out;
63                 next[0] = '\0';
64                 info(udev, "import into environment: '%s'\n", key);
65                 putenv(key);
66                 key = &next[1];
67         }
68         rc = 0;
69 out:
70         return rc;
71 }
72
73 int udevadm_test(struct udev *udev, int argc, char *argv[])
74 {
75         int force = 0;
76         const char *action = "add";
77         const char *subsystem = NULL;
78         const char *devpath = NULL;
79         struct udevice *udevice;
80         struct sysfs_device *dev;
81         struct udev_rules rules = {};
82         int retval;
83         int rc = 0;
84
85         static const struct option options[] = {
86                 { "action", 1, NULL, 'a' },
87                 { "subsystem", 1, NULL, 's' },
88                 { "force", 0, NULL, 'f' },
89                 { "help", 0, NULL, 'h' },
90                 {}
91         };
92
93         info(udev, "version %s\n", VERSION);
94
95         /* export log priority to executed programs */
96         if (udev_get_log_priority(udev) > 0) {
97                 char priority[32];
98
99                 sprintf(priority, "%i", udev_get_log_priority(udev));
100                 setenv("UDEV_LOG", priority, 1);
101         }
102
103         while (1) {
104                 int option;
105
106                 option = getopt_long(argc, argv, "a:s:fh", options, NULL);
107                 if (option == -1)
108                         break;
109
110                 dbg(udev, "option '%c'\n", option);
111                 switch (option) {
112                 case 'a':
113                         action = optarg;
114                         break;
115                 case 's':
116                         subsystem = optarg;
117                         break;
118                 case 'f':
119                         force = 1;
120                         break;
121                 case 'h':
122                         printf("Usage: udevadm test OPTIONS <devpath>\n"
123                                "  --action=<string>     set action string\n"
124                                "  --subsystem=<string>  set subsystem string\n"
125                                "  --force               don't skip node/link creation\n"
126                                "  --help                print this help text\n\n");
127                         exit(0);
128                 default:
129                         exit(1);
130                 }
131         }
132         devpath = argv[optind];
133
134         if (devpath == NULL) {
135                 fprintf(stderr, "devpath parameter missing\n");
136                 rc = 1;
137                 goto exit;
138         }
139
140         printf("This program is for debugging only, it does not run any program,\n"
141                "specified by a RUN key. It may show incorrect results, because\n"
142                "some values may be different, or not available at a simulation run.\n"
143                "\n");
144
145         udev_rules_init(udev, &rules, 0);
146
147         /* remove /sys if given */
148         if (strncmp(devpath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0)
149                 devpath = &devpath[strlen(udev_get_sys_path(udev))];
150
151         dev = sysfs_device_get(udev, devpath);
152         if (dev == NULL) {
153                 fprintf(stderr, "unable to open device '%s'\n", devpath);
154                 rc = 2;
155                 goto exit;
156         }
157
158         udevice = udev_device_init(udev);
159         if (udevice == NULL) {
160                 fprintf(stderr, "error initializing device\n");
161                 rc = 3;
162                 goto exit;
163         }
164
165         if (subsystem != NULL)
166                 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
167
168         /* override built-in sysfs device */
169         udevice->dev = dev;
170         strlcpy(udevice->action, action, sizeof(udevice->action));
171         udevice->devt = udev_device_get_devt(udevice);
172
173         /* simulate node creation with test flag */
174         if (!force)
175                 udevice->test_run = 1;
176
177         setenv("DEVPATH", udevice->dev->devpath, 1);
178         setenv("SUBSYSTEM", udevice->dev->subsystem, 1);
179         setenv("ACTION", udevice->action, 1);
180         import_uevent_var(udev, udevice->dev->devpath);
181
182         info(udev, "looking at device '%s' from subsystem '%s'\n", udevice->dev->devpath, udevice->dev->subsystem);
183         retval = udev_device_event(&rules, udevice);
184
185         if (udevice->event_timeout >= 0)
186                 info(udev, "custom event timeout: %i\n", udevice->event_timeout);
187
188         if (retval == 0 && !udevice->ignore_device && udev_get_run(udev)) {
189                 struct name_entry *name_loop;
190
191                 list_for_each_entry(name_loop, &udevice->run_list, node) {
192                         char program[PATH_SIZE];
193
194                         strlcpy(program, name_loop->name, sizeof(program));
195                         udev_rules_apply_format(udevice, program, sizeof(program));
196                         info(udev, "run: '%s'\n", program);
197                 }
198         }
199         udev_device_cleanup(udevice);
200
201 exit:
202         udev_rules_cleanup(&rules);
203         return rc;
204 }