chiark / gitweb /
use fnmatch() instead of our own pattern match code
[elogind.git] / udevtest.c
1 /*
2  * udevtest.c
3  *
4  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
5  * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
6  *
7  *      This program is free software; you can redistribute it and/or modify it
8  *      under the terms of the GNU General Public License as published by the
9  *      Free Software Foundation version 2 of the License.
10  * 
11  *      This program is distributed in the hope that it will be useful, but
12  *      WITHOUT ANY WARRANTY; without even the implied warranty of
13  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  *      General Public License for more details.
15  * 
16  *      You should have received a copy of the GNU General Public License along
17  *      with this program; if not, write to the Free Software Foundation, Inc.,
18  *      675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <stddef.h>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <ctype.h>
29 #include <signal.h>
30 #include <syslog.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 int main(int argc, char *argv[], char *envp[])
53 {
54         struct udev_rules rules = {};
55         char *devpath = NULL;
56         struct udevice *udev;
57         struct sysfs_device *dev;
58         int i;
59         int retval;
60         int rc = 0;
61
62         info("version %s", UDEV_VERSION);
63         udev_config_init();
64         if (udev_log_priority < LOG_INFO)
65                 udev_log_priority = LOG_INFO;
66
67         for (i = 1 ; i < argc; i++) {
68                 char *arg = argv[i];
69
70                 if (strcmp(arg, "--help") == 0 || strcmp(arg, "-h") == 0) {
71                         printf("Usage: udevtest [--help] <devpath>\n");
72                         goto exit;
73                 } else
74                         devpath = arg;
75         }
76
77         if (devpath == NULL) {
78                 fprintf(stderr, "devpath parameter missing\n");
79                 rc = 1;
80                 goto exit;
81         }
82
83         sysfs_init();
84         udev_rules_init(&rules, 0);
85
86         /* remove /sys if given */
87         if (strncmp(devpath, sysfs_path, strlen(sysfs_path)) == 0)
88                 devpath = &devpath[strlen(sysfs_path)];
89
90         dev = sysfs_device_get(devpath);
91         if (dev == NULL) {
92                 fprintf(stderr, "unable to open device '%s'\n", devpath);
93                 rc = 2;
94                 goto exit;
95         }
96
97         udev = udev_device_init();
98         if (udev == NULL) {
99                 fprintf(stderr, "error initializing device\n");
100                 rc = 3;
101                 goto exit;
102         }
103
104         /* override built-in sysfs device */
105         udev->dev = dev;
106         strcpy(udev->action, "add");
107         udev->devt = udev_device_get_devt(udev);
108
109         /* simulate node creation with test flag */
110         udev->test_run = 1;
111
112         setenv("DEVPATH", udev->dev->devpath, 1);
113         setenv("SUBSYSTEM", udev->dev->subsystem, 1);
114         setenv("ACTION", "add", 1);
115
116         info("looking at device '%s' from subsystem '%s'", udev->dev->devpath, udev->dev->subsystem);
117         retval = udev_device_event(&rules, udev);
118         if (retval == 0 && !udev->ignore_device && udev_run) {
119                 struct name_entry *name_loop;
120
121                 list_for_each_entry(name_loop, &udev->run_list, node) {
122                         char program[PATH_SIZE];
123
124                         strlcpy(program, name_loop->name, sizeof(program));
125                         udev_rules_apply_format(udev, program, sizeof(program));
126                         info("run: '%s'", program);
127                 }
128         }
129
130 exit:
131         udev_rules_cleanup(&rules);
132         sysfs_cleanup();
133         return rc;
134 }