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