chiark / gitweb /
[PATCH] include missing header to udevtest.c
[elogind.git] / udevtest.c
1 /*
2  * udev.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
30 #include "libsysfs/sysfs/libsysfs.h"
31 #include "udev.h"
32 #include "udev_sysfs.h"
33 #include "udev_utils.h"
34 #include "udev_version.h"
35 #include "namedev.h"
36 #include "logging.h"
37
38
39 #ifdef LOG
40 unsigned char logname[LOGNAME_SIZE];
41 void log_message (int level, const char *format, ...)
42 {
43         va_list args;
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 sysfs_class_device *class_dev;
56         char *devpath;
57         char path[SYSFS_PATH_MAX];
58         char temp[NAME_SIZE];
59         struct udevice udev;
60         char *subsystem = NULL;
61
62         info("version %s", UDEV_VERSION);
63
64         if (argc < 2 || argc > 3) {
65                 info("Usage: udevtest <devpath> [subsystem]");
66                 return 1;
67         }
68
69         /* initialize our configuration */
70         udev_init_config();
71
72         /* remove sysfs_path if given */
73         if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0) {
74                 devpath = &argv[1][strlen(sysfs_path)] ;
75         }
76         else
77                 if (argv[1][0] != '/') {
78                         /* prepend '/' if missing */
79                         strfieldcpy(temp, "/");
80                         strfieldcat(temp, argv[1]);
81                         devpath = temp;
82                 } else {
83                         devpath = argv[1];
84                 }
85
86         info("looking at '%s'", devpath);
87
88         /* initialize the naming deamon */
89         namedev_init();
90
91         if (argc == 3)
92                 subsystem = argv[2];
93
94         /* fill in values and test_run flag*/
95         udev_init_device(&udev, devpath, subsystem);
96
97         /* skip subsystems without "dev", but handle net devices */
98         if (udev.type != 'n' && subsystem_expect_no_dev(udev.subsystem)) {
99                 info("don't care about '%s' devices", udev.subsystem);
100                 return 2;
101         }
102
103         /* open the device */
104         snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
105         class_dev = sysfs_open_class_device_path(path);
106         if (class_dev == NULL) {
107                 info("sysfs_open_class_device_path failed");
108                 return 1;
109         }
110
111         info("opened class_dev->name='%s'", class_dev->name);
112
113         /* simulate node creation with test flag */
114         udev.test_run = 1;
115         udev_add_device(&udev, class_dev);
116
117         sysfs_close_class_device(class_dev);
118
119         return 0;
120 }