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