chiark / gitweb /
Merge gregkh@ehlo.org:/home/kay/public_html/pub/scm/linux/hotplug/udev-kay
[elogind.git] / udevtest.c
1 /*
2  * udevtest.c
3  *
4  * Userspace devfs
5  *
6  * Copyright (C) 2003,2004 Greg Kroah-Hartman <greg@kroah.com>
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation version 2 of the License.
11  * 
12  *      This program is distributed in the hope that it will be useful, but
13  *      WITHOUT ANY WARRANTY; without even the implied warranty of
14  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *      General Public License for more details.
16  * 
17  *      You should have received a copy of the GNU General Public License along
18  *      with this program; if not, write to the Free Software Foundation, Inc.,
19  *      675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <signal.h>
29 #include <syslog.h>
30
31 #include "libsysfs/sysfs/libsysfs.h"
32 #include "udev.h"
33 #include "udev_sysfs.h"
34 #include "udev_utils.h"
35 #include "udev_version.h"
36 #include "udev_rules.h"
37 #include "logging.h"
38
39
40 #ifdef USE_LOG
41 void log_message (int priority, const char *format, ...)
42 {
43         va_list args;
44
45         if (priority > udev_log_priority)
46                 return;
47
48         va_start(args, format);
49         vprintf(format, args);
50         va_end(args);
51         if (format[strlen(format)-1] != '\n')
52                 printf("\n");
53 }
54 #endif
55
56 int main(int argc, char *argv[], char *envp[])
57 {
58         struct sysfs_class_device *class_dev;
59         char *devpath;
60         char path[PATH_SIZE];
61         char temp[PATH_SIZE];
62         struct udevice udev;
63         char *subsystem = NULL;
64
65         info("version %s", UDEV_VERSION);
66
67         /* initialize our configuration */
68         udev_init_config();
69         if (udev_log_priority < LOG_INFO)
70                 udev_log_priority = LOG_INFO;
71
72         if (argc != 3) {
73                 info("Usage: udevtest <devpath> <subsystem>");
74                 return 1;
75         }
76
77         /* remove sysfs_path if given */
78         if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0)
79                 devpath = &argv[1][strlen(sysfs_path)] ;
80         else
81                 if (argv[1][0] != '/') {
82                         /* prepend '/' if missing */
83                         snprintf(temp, sizeof(temp), "/%s", argv[1]);
84                         temp[sizeof(temp)-1] = '\0';
85                         devpath = temp;
86                 } else
87                         devpath = argv[1];
88
89         subsystem = argv[2];
90         setenv("DEVPATH", devpath, 1);
91         setenv("SUBSYSTEM", subsystem, 1);
92         setenv("ACTION", "add", 1);
93         info("looking at device '%s' from subsystem '%s'", devpath, subsystem);
94
95         /* initialize the naming deamon */
96         udev_rules_init();
97
98         /* fill in values and test_run flag*/
99         udev_init_device(&udev, devpath, subsystem, "add");
100
101         /* open the device */
102         snprintf(path, sizeof(path), "%s%s", sysfs_path, udev.devpath);
103         path[sizeof(path)-1] = '\0';
104         class_dev = sysfs_open_class_device_path(path);
105         if (class_dev == NULL) {
106                 info("sysfs_open_class_device_path failed");
107                 return 1;
108         }
109         info("opened class_dev->name='%s'", class_dev->name);
110
111         if (udev.type == DEV_BLOCK || udev.type == DEV_CLASS)
112                 udev.devt = get_devt(class_dev);
113
114         /* simulate node creation with test flag */
115         udev.test_run = 1;
116         if (udev.type == DEV_NET || udev.devt) {
117                 udev_rules_get_name(&udev, class_dev);
118                 udev_add_device(&udev, class_dev);
119         } else
120                 info("only char and block devices with a dev-file are supported by this test program");
121         sysfs_close_class_device(class_dev);
122
123         return 0;
124 }