chiark / gitweb /
Docs: udev.xml: Offset daemon name with commas
[elogind.git] / udev / udevadm-test.c
1 /*
2  * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
3  * Copyright (C) 2004-2008 Kay Sievers <kay.sievers@vrfy.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <signal.h>
28 #include <syslog.h>
29 #include <getopt.h>
30
31 #include "udev.h"
32
33 int udevadm_test(struct udev *udev, int argc, char *argv[])
34 {
35         char filename[UTIL_PATH_SIZE];
36         const char *action = "add";
37         const char *syspath = NULL;
38         struct udev_event *event;
39         struct udev_device *dev;
40         struct udev_rules *rules = NULL;
41         struct udev_list_entry *entry;
42         int err;
43         int rc = 0;
44
45         static const struct option options[] = {
46                 { "action", required_argument, NULL, 'a' },
47                 { "help", no_argument, NULL, 'h' },
48                 {}
49         };
50
51         info(udev, "version %s\n", VERSION);
52
53         for (;;) {
54                 int option;
55
56                 option = getopt_long(argc, argv, "a:s:fh", options, NULL);
57                 if (option == -1)
58                         break;
59
60                 dbg(udev, "option '%c'\n", option);
61                 switch (option) {
62                 case 'a':
63                         action = optarg;
64                         break;
65                 case 'h':
66                         printf("Usage: udevadm test OPTIONS <syspath>\n"
67                                "  --action=<string>     set action string\n"
68                                "  --help\n\n");
69                         exit(0);
70                 default:
71                         exit(1);
72                 }
73         }
74         syspath = argv[optind];
75
76         if (syspath == NULL) {
77                 fprintf(stderr, "syspath parameter missing\n");
78                 rc = 1;
79                 goto exit;
80         }
81
82         printf("This program is for debugging only, it does not run any program,\n"
83                "specified by a RUN key. It may show incorrect results, because\n"
84                "some values may be different, or not available at a simulation run.\n"
85                "\n");
86
87         rules = udev_rules_new(udev, 1);
88         if (rules == NULL) {
89                 fprintf(stderr, "error reading rules\n");
90                 rc = 1;
91                 goto exit;
92         }
93
94         /* add /sys if needed */
95         if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
96                 util_strscpyl(filename, sizeof(filename), udev_get_sys_path(udev), syspath, NULL);
97         else
98                 util_strscpy(filename, sizeof(filename), syspath);
99         util_remove_trailing_chars(filename, '/');
100
101         dev = udev_device_new_from_syspath(udev, filename);
102         if (dev == NULL) {
103                 fprintf(stderr, "unable to open device '%s'\n", filename);
104                 rc = 2;
105                 goto exit;
106         }
107
108         /* skip reading of db, but read kernel parameters */
109         udev_device_set_info_loaded(dev);
110         udev_device_read_uevent_file(dev);
111
112         udev_device_set_action(dev, action);
113         event = udev_event_new(dev);
114         err = udev_event_execute_rules(event, rules);
115
116         if (udev_device_get_event_timeout(dev) >= 0)
117                 info(udev, "custom event timeout: %i\n", udev_device_get_event_timeout(dev));
118
119         udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev))
120                 info(udev, "%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry));
121
122         if (err == 0)
123                 udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) {
124                         char program[UTIL_PATH_SIZE];
125
126                         udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program));
127                         info(udev, "run: '%s'\n", program);
128                 }
129         udev_event_unref(event);
130         udev_device_unref(dev);
131 exit:
132         udev_rules_unref(rules);
133         return rc;
134 }