chiark / gitweb /
0f7d5dbe73563a36a2e0addfa11c17b1fed9465c
[elogind.git] / udev / udevadm-test.c
1 /*
2  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include <stddef.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <syslog.h>
30 #include <getopt.h>
31
32 #include "udev.h"
33 #include "udev_rules.h"
34
35 static int import_uevent_var(struct udev *udev, const char *devpath)
36 {
37         char path[PATH_SIZE];
38         static char value[4096]; /* must stay, used with putenv */
39         ssize_t size;
40         int fd;
41         char *key;
42         char *next;
43         int rc = -1;
44
45         /* read uevent file */
46         strlcpy(path, udev_get_sys_path(udev), sizeof(path));
47         strlcat(path, devpath, sizeof(path));
48         strlcat(path, "/uevent", sizeof(path));
49         fd = open(path, O_RDONLY);
50         if (fd < 0)
51                 goto out;
52         size = read(fd, value, sizeof(value));
53         close(fd);
54         if (size < 0)
55                 goto out;
56         value[size] = '\0';
57
58         /* import keys into environment */
59         key = value;
60         while (key[0] != '\0') {
61                 next = strchr(key, '\n');
62                 if (next == NULL)
63                         goto out;
64                 next[0] = '\0';
65                 info(udev, "import into environment: '%s'\n", key);
66                 putenv(key);
67                 key = &next[1];
68         }
69         rc = 0;
70 out:
71         return rc;
72 }
73
74 int udevadm_test(struct udev *udev, int argc, char *argv[])
75 {
76         int force = 0;
77         const char *action = "add";
78         const char *subsystem = NULL;
79         const char *devpath = NULL;
80         struct udevice *udevice;
81         struct sysfs_device *dev;
82         struct udev_rules rules = {};
83         int retval;
84         int rc = 0;
85
86         static const struct option options[] = {
87                 { "action", 1, NULL, 'a' },
88                 { "subsystem", 1, NULL, 's' },
89                 { "force", 0, NULL, 'f' },
90                 { "help", 0, NULL, 'h' },
91                 {}
92         };
93
94         info(udev, "version %s\n", VERSION);
95
96         /* export log priority to executed programs */
97         if (udev_get_log_priority(udev) > 0) {
98                 char priority[32];
99
100                 sprintf(priority, "%i", udev_get_log_priority(udev));
101                 setenv("UDEV_LOG", priority, 1);
102         }
103
104         while (1) {
105                 int option;
106
107                 option = getopt_long(argc, argv, "a:s:fh", options, NULL);
108                 if (option == -1)
109                         break;
110
111                 dbg(udev, "option '%c'\n", option);
112                 switch (option) {
113                 case 'a':
114                         action = optarg;
115                         break;
116                 case 's':
117                         subsystem = optarg;
118                         break;
119                 case 'f':
120                         force = 1;
121                         break;
122                 case 'h':
123                         printf("Usage: udevadm test OPTIONS <devpath>\n"
124                                "  --action=<string>     set action string\n"
125                                "  --subsystem=<string>  set subsystem string\n"
126                                "  --force               don't skip node/link creation\n"
127                                "  --help                print this help text\n\n");
128                         exit(0);
129                 default:
130                         exit(1);
131                 }
132         }
133         devpath = argv[optind];
134
135         if (devpath == NULL) {
136                 fprintf(stderr, "devpath parameter missing\n");
137                 rc = 1;
138                 goto exit;
139         }
140
141         printf("This program is for debugging only, it does not run any program,\n"
142                "specified by a RUN key. It may show incorrect results, because\n"
143                "some values may be different, or not available at a simulation run.\n"
144                "\n");
145
146         udev_rules_init(udev, &rules, 0);
147
148         /* remove /sys if given */
149         if (strncmp(devpath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) == 0)
150                 devpath = &devpath[strlen(udev_get_sys_path(udev))];
151
152         dev = sysfs_device_get(udev, devpath);
153         if (dev == NULL) {
154                 fprintf(stderr, "unable to open device '%s'\n", devpath);
155                 rc = 2;
156                 goto exit;
157         }
158
159         udevice = udev_device_init(udev);
160         if (udevice == NULL) {
161                 fprintf(stderr, "error initializing device\n");
162                 rc = 3;
163                 goto exit;
164         }
165
166         if (subsystem != NULL)
167                 strlcpy(dev->subsystem, subsystem, sizeof(dev->subsystem));
168
169         /* override built-in sysfs device */
170         udevice->dev = dev;
171         strlcpy(udevice->action, action, sizeof(udevice->action));
172         udevice->devt = udev_device_get_devt(udevice);
173
174         /* simulate node creation with test flag */
175         if (!force)
176                 udevice->test_run = 1;
177
178         setenv("DEVPATH", udevice->dev->devpath, 1);
179         setenv("SUBSYSTEM", udevice->dev->subsystem, 1);
180         setenv("ACTION", udevice->action, 1);
181         import_uevent_var(udev, udevice->dev->devpath);
182
183         info(udev, "looking at device '%s' from subsystem '%s'\n", udevice->dev->devpath, udevice->dev->subsystem);
184         retval = udev_device_event(&rules, udevice);
185
186         if (udevice->event_timeout >= 0)
187                 info(udev, "custom event timeout: %i\n", udevice->event_timeout);
188
189         if (retval == 0 && !udevice->ignore_device && udev_get_run(udev)) {
190                 struct name_entry *name_loop;
191
192                 list_for_each_entry(name_loop, &udevice->run_list, node) {
193                         char program[PATH_SIZE];
194
195                         strlcpy(program, name_loop->name, sizeof(program));
196                         udev_rules_apply_format(udevice, program, sizeof(program));
197                         info(udev, "run: '%s'\n", program);
198                 }
199         }
200         udev_device_cleanup(udevice);
201
202 exit:
203         udev_rules_cleanup(&rules);
204         return rc;
205 }