chiark / gitweb /
allow to overwrite the configured udev_root by exporting UDEV_ROOT
[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 <errno.h>
25 #include <ctype.h>
26 #include <signal.h>
27 #include <syslog.h>
28
29 #include "libsysfs/sysfs/libsysfs.h"
30 #include "udev.h"
31 #include "udev_utils.h"
32 #include "udev_version.h"
33 #include "udev_rules.h"
34 #include "logging.h"
35
36
37 #ifdef USE_LOG
38 void log_message (int priority, const char *format, ...)
39 {
40         va_list args;
41
42         if (priority > udev_log_priority)
43                 return;
44
45         va_start(args, format);
46         vprintf(format, args);
47         va_end(args);
48         if (format[strlen(format)-1] != '\n')
49                 printf("\n");
50 }
51 #endif
52
53 int main(int argc, char *argv[], char *envp[])
54 {
55         struct udev_rules rules;
56         struct sysfs_class_device *class_dev;
57         char *devpath;
58         char path[PATH_SIZE];
59         char temp[PATH_SIZE];
60         struct udevice udev;
61         char *subsystem = NULL;
62
63         info("version %s", UDEV_VERSION);
64
65         /* initialize our configuration */
66         udev_init_config();
67         if (udev_log_priority < LOG_INFO)
68                 udev_log_priority = LOG_INFO;
69
70         if (argc != 3) {
71                 info("Usage: udevtest <devpath> <subsystem>");
72                 return 1;
73         }
74
75         /* remove sysfs_path if given */
76         if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0)
77                 devpath = &argv[1][strlen(sysfs_path)] ;
78         else
79                 if (argv[1][0] != '/') {
80                         /* prepend '/' if missing */
81                         snprintf(temp, sizeof(temp), "/%s", argv[1]);
82                         temp[sizeof(temp)-1] = '\0';
83                         devpath = temp;
84                 } else
85                         devpath = argv[1];
86
87         subsystem = argv[2];
88         setenv("DEVPATH", devpath, 1);
89         setenv("SUBSYSTEM", subsystem, 1);
90         setenv("ACTION", "add", 1);
91         info("looking at device '%s' from subsystem '%s'", devpath, subsystem);
92
93         /* initialize the naming deamon */
94         udev_rules_init(&rules, 0);
95
96         /* fill in values and test_run flag*/
97         udev_init_device(&udev, devpath, subsystem, "add");
98
99         /* open the device */
100         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
101         path[sizeof(path)-1] = '\0';
102         class_dev = sysfs_open_class_device_path(path);
103         if (class_dev == NULL) {
104                 info("sysfs_open_class_device_path failed");
105                 return 1;
106         }
107         info("opened class_dev->name='%s'", class_dev->name);
108
109         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS)
110                 udev.devt = get_devt(class_dev);
111
112         /* simulate node creation with test flag */
113         udev.test_run = 1;
114         if (udev.type == DEV_NET || udev.devt) {
115                 udev_rules_get_name(&rules, &udev, class_dev);
116                 udev_add_device(&udev, class_dev);
117         } else
118                 info("only char and block devices with a dev-file are supported by this test program");
119         sysfs_close_class_device(class_dev);
120
121         return 0;
122 }