chiark / gitweb /
9d73996bcf19c95fa946caddf9de4eaaed73a43c
[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 <signal.h>
28 #include <syslog.h>
29 #include <getopt.h>
30
31 #include "udev.h"
32 #include "udev_rules.h"
33
34
35 #ifdef USE_LOG
36 void log_message (int priority, const char *format, ...)
37 {
38         va_list args;
39
40         if (priority > udev_log_priority)
41                 return;
42
43         va_start(args, format);
44         vprintf(format, args);
45         va_end(args);
46         if (format[strlen(format)-1] != '\n')
47                 printf("\n");
48 }
49 #endif
50
51 int main(int argc, char *argv[], char *envp[])
52 {
53         int force = 0;
54         char *action = "add";
55         struct udev_rules rules = {};
56         char *devpath = NULL;
57         struct udevice *udev;
58         struct sysfs_device *dev;
59         int retval;
60         int rc = 0;
61
62         static const struct option options[] = {
63                 { "action", 1, NULL, 'a' },
64                 { "force", 0, NULL, 'f' },
65                 { "help", 0, NULL, 'h' },
66                 {}
67         };
68
69         info("version %s", UDEV_VERSION);
70         udev_config_init();
71         if (udev_log_priority < LOG_INFO) {
72                 char priority[32];
73
74                 udev_log_priority = LOG_INFO;
75                 sprintf(priority, "%i", udev_log_priority);
76                 setenv("UDEV_LOG", priority, 1);
77         }
78
79         while (1) {
80                 int option;
81
82                 option = getopt_long(argc, argv, "a:fh", options, NULL);
83                 if (option == -1)
84                         break;
85
86                 dbg("option '%c'", option);
87                 switch (option) {
88                 case 'a':
89                         action = optarg;
90                         break;
91                 case 'f':
92                         force = 1;
93                         break;
94                 case 'h':
95                         printf("Usage: udevtest [--action=<string>] [--force] [--help] <devpath>\n"
96                                "  --action=<string>   set action string\n"
97                                "  --force             don't skip node/link creation\n"
98                                "  --help              print this help text\n\n");
99                         exit(0);
100                 default:
101                         exit(1);
102                 }
103         }
104         devpath = argv[optind];
105
106         if (devpath == NULL) {
107                 fprintf(stderr, "devpath parameter missing\n");
108                 rc = 1;
109                 goto exit;
110         }
111
112         sysfs_init();
113         udev_rules_init(&rules, 0);
114
115         /* remove /sys if given */
116         if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
117                 devpath = &devpath[strlen(sysfs_path)];
118
119         dev = sysfs_device_get(devpath);
120         if (dev == NULL) {
121                 fprintf(stderr, "unable to open device '%s'\n", devpath);
122                 rc = 2;
123                 goto exit;
124         }
125
126         udev = udev_device_init(NULL);
127         if (udev == NULL) {
128                 fprintf(stderr, "error initializing device\n");
129                 rc = 3;
130                 goto exit;
131         }
132
133         /* override built-in sysfs device */
134         udev->dev = dev;
135         strcpy(udev->action, action);
136         udev->devt = udev_device_get_devt(udev);
137
138         /* simulate node creation with test flag */
139         if (!force)
140                 udev->test_run = 1;
141
142         setenv("DEVPATH", udev->dev->devpath, 1);
143         setenv("SUBSYSTEM", udev->dev->subsystem, 1);
144         setenv("ACTION", udev->action, 1);
145
146         printf("This program is for debugging only, it does not run any program,\n"
147                "specified by a RUN key. It may show incorrect results, if rules\n"
148                "match against subsystem specfic kernel event variables.\n"
149                "\n");
150
151         info("looking at device '%s' from subsystem '%s'", udev->dev->devpath, udev->dev->subsystem);
152         retval = udev_device_event(&rules, udev);
153         if (retval == 0 && !udev->ignore_device && udev_run) {
154                 struct name_entry *name_loop;
155
156                 list_for_each_entry(name_loop, &udev->run_list, node) {
157                         char program[PATH_SIZE];
158
159                         strlcpy(program, name_loop->name, sizeof(program));
160                         udev_rules_apply_format(udev, program, sizeof(program));
161                         info("run: '%s'", program);
162                 }
163         }
164
165 exit:
166         udev_rules_cleanup(&rules);
167         sysfs_cleanup();
168         return rc;
169 }