chiark / gitweb /
udevadm: move init from commands to udevadm
[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(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, sysfs_path, 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("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(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 *udev;
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("version %s\n", VERSION);
95         if (udev_log_priority < LOG_INFO) {
96                 char priority[32];
97
98                 udev_log_priority = LOG_INFO;
99                 sprintf(priority, "%i", udev_log_priority);
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("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(&rules, 0);
146
147         /* remove /sys if given */
148         if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
149                 devpath = &devpath[strlen(sysfs_path)];
150
151         dev = sysfs_device_get(devpath);
152         if (dev == NULL) {
153                 fprintf(stderr, "unable to open device '%s'\n", devpath);
154                 rc = 2;
155                 goto exit;
156         }
157
158         udev = udev_device_init();
159         if (udev == 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         udev->dev = dev;
170         strlcpy(udev->action, action, sizeof(udev->action));
171         udev->devt = udev_device_get_devt(udev);
172
173         /* simulate node creation with test flag */
174         if (!force)
175                 udev->test_run = 1;
176
177         setenv("DEVPATH", udev->dev->devpath, 1);
178         setenv("SUBSYSTEM", udev->dev->subsystem, 1);
179         setenv("ACTION", udev->action, 1);
180         import_uevent_var(udev->dev->devpath);
181
182         info("looking at device '%s' from subsystem '%s'\n", udev->dev->devpath, udev->dev->subsystem);
183         retval = udev_device_event(&rules, udev);
184
185         if (udev->event_timeout >= 0)
186                 info("custom event timeout: %i\n", udev->event_timeout);
187
188         if (retval == 0 && !udev->ignore_device && udev_run) {
189                 struct name_entry *name_loop;
190
191                 list_for_each_entry(name_loop, &udev->run_list, node) {
192                         char program[PATH_SIZE];
193
194                         strlcpy(program, name_loop->name, sizeof(program));
195                         udev_rules_apply_format(udev, program, sizeof(program));
196                         info("run: '%s'\n", program);
197                 }
198         }
199         udev_device_cleanup(udev);
200
201 exit:
202         udev_rules_cleanup(&rules);
203         return rc;
204 }