]> git.ipfire.org Git - thirdparty/systemd.git/blob - udevtest.c
[PATCH] make the udev object available to more processing stages
[thirdparty/systemd.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_lib.h"
33 #include "udev_version.h"
34 #include "logging.h"
35 #include "namedev.h"
36
37
38 #ifdef LOG
39 unsigned char logname[LOGNAME_SIZE];
40 void log_message (int level, const char *format, ...)
41 {
42 va_list args;
43
44 va_start(args, format);
45 vprintf(format, args);
46 va_end(args);
47 if (format[strlen(format)-1] != '\n')
48 printf("\n");
49 }
50 #endif
51
52 int main(int argc, char *argv[], char *envp[])
53 {
54 struct sysfs_class_device *class_dev;
55 char *devpath;
56 char path[SYSFS_PATH_MAX];
57 char temp[NAME_SIZE];
58 char *subsystem = "";
59 struct udevice udev;
60
61 info("version %s", UDEV_VERSION);
62
63 if (argv[1] == NULL) {
64 info("udevinfo expects the DEVPATH of the sysfs device as a argument");
65 return 1;
66 }
67
68 /* remove sysfs_path if given */
69 if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0)
70 devpath = argv[1] + strlen(sysfs_path);
71 else
72 if (argv[1][0] != '/') {
73 /* prepend '/' if missing */
74 strfieldcpy(temp, "/");
75 strfieldcat(temp, argv[1]);
76 devpath = temp;
77 } else {
78 devpath = argv[1];
79 }
80
81 info("looking at '%s'", devpath);
82
83 /* we only care about class devices and block stuff */
84 if (!strstr(devpath, "class") && !strstr(devpath, "block")) {
85 dbg("not a block or class device");
86 return 2;
87 }
88
89 /* initialize our configuration */
90 udev_init_config();
91
92 /* initialize the naming deamon */
93 namedev_init();
94
95 if (argv[2] != NULL)
96 subsystem = argv[2];
97
98 /* fill in values and test_run flag*/
99 udev_set_values(&udev, devpath, subsystem, "add");
100
101 /* open the device */
102 snprintf(path, SYSFS_PATH_MAX, "%s%s", sysfs_path, udev.devpath);
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
109 dbg("opened class_dev->name='%s'", class_dev->name);
110
111 /* simulate node creation with test flag */
112 udev.test_run = 1;
113 udev_add_device(&udev, class_dev);
114
115 sysfs_close_class_device(class_dev);
116
117 return 0;
118 }