]> git.ipfire.org Git - thirdparty/systemd.git/blame - udevtest.c
merge device event handling and make database content available on "remove"
[thirdparty/systemd.git] / udevtest.c
CommitLineData
bb051f66 1/*
63f61c5c 2 * udevtest.c
bb051f66 3 *
e8d569b4 4 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
bb051f66
GKH
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>
1aa1e248
KS
24#include <stddef.h>
25#include <unistd.h>
bb051f66
GKH
26#include <errno.h>
27#include <ctype.h>
28#include <signal.h>
6b493a20 29#include <syslog.h>
bb051f66
GKH
30
31#include "udev.h"
e5e322bc 32#include "udev_rules.h"
bb051f66 33
8a0acf85 34
6c18b1fb 35#ifdef USE_LOG
6b493a20 36void log_message (int priority, const char *format, ...)
bb051f66 37{
d00bd172 38 va_list args;
bb051f66 39
6b493a20
KS
40 if (priority > udev_log_priority)
41 return;
42
bb051f66 43 va_start(args, format);
eb10f97f 44 vprintf(format, args);
bb051f66 45 va_end(args);
eb10f97f
GKH
46 if (format[strlen(format)-1] != '\n')
47 printf("\n");
bb051f66
GKH
48}
49#endif
50
8a0acf85 51int main(int argc, char *argv[], char *envp[])
bb051f66 52{
8bd41f36 53 struct udev_rules rules;
bb051f66 54 char *devpath;
63f61c5c 55 char temp[PATH_SIZE];
1aa1e248
KS
56 struct udevice *udev;
57 struct sysfs_device *dev;
58 int retval;
59 int rc = 0;
8a0acf85 60
8a0acf85 61 info("version %s", UDEV_VERSION);
bb051f66 62
a0294b76 63 /* initialize our configuration */
1aa1e248 64 udev_config_init();
6b493a20
KS
65 if (udev_log_priority < LOG_INFO)
66 udev_log_priority = LOG_INFO;
a0294b76 67
1aa1e248
KS
68 if (argc != 2) {
69 info("Usage: udevtest <devpath>");
e03a196a
KS
70 return 1;
71 }
72
8a0acf85 73 /* remove sysfs_path if given */
63f61c5c 74 if (strncmp(argv[1], sysfs_path, strlen(sysfs_path)) == 0)
1aa1e248 75 devpath = &argv[1][strlen(sysfs_path)];
8a0acf85
KS
76 else
77 if (argv[1][0] != '/') {
78 /* prepend '/' if missing */
63f61c5c
KS
79 snprintf(temp, sizeof(temp), "/%s", argv[1]);
80 temp[sizeof(temp)-1] = '\0';
8a0acf85 81 devpath = temp;
63f61c5c 82 } else
8a0acf85 83 devpath = argv[1];
8a0acf85 84
1aa1e248 85 sysfs_init();
287814b2 86 udev_rules_init(&rules, 0);
bb051f66 87
1aa1e248
KS
88 dev = sysfs_device_get(devpath);
89 if (dev == NULL) {
90 info("unable to open '%s'", devpath);
91 rc = 2;
92 goto exit;
93 }
7a947ce5 94
1aa1e248
KS
95 udev = udev_device_init();
96 if (udev == NULL) {
97 info("can't open device");
98 rc = 3;
99 goto exit;
e5e2ea95 100 }
7a947ce5 101
1aa1e248
KS
102 /* override built-in sysfs device */
103 udev->dev = dev;
104 strcpy(udev->action, "add");
105 udev->devt = udev_device_get_devt(udev);
d27d3bb0 106
5d24c6ca 107 /* simulate node creation with test flag */
1aa1e248
KS
108 udev->test_run = 1;
109
110 setenv("DEVPATH", udev->dev->devpath, 1);
111 setenv("SUBSYSTEM", udev->dev->subsystem, 1);
112 setenv("ACTION", "add", 1);
113
114 info("looking at device '%s' from subsystem '%s'", udev->dev->devpath, udev->dev->subsystem);
115 retval = udev_device_event(&rules, udev);
ad27f5b3 116 if (retval == 0 && !udev->ignore_device && udev_run) {
1aa1e248
KS
117 struct name_entry *name_loop;
118
ad27f5b3
KS
119 list_for_each_entry(name_loop, &udev->run_list, node) {
120 char program[PATH_SIZE];
121
122 strlcpy(program, name_loop->name, sizeof(program));
123 udev_rules_apply_format(udev, program, sizeof(program));
124 info("run: '%s'", program);
125 }
1aa1e248
KS
126 }
127
128exit:
129 udev_rules_cleanup(&rules);
130 sysfs_cleanup();
131 return rc;
bb051f66 132}