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