1/* 2 * Copyright (C) 2003-2004 Greg Kroah-Hartman <greg@kroah.com> 3 * Copyright (C) 2004-2008 Kay Sievers <kay@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 <getopt.h> 29#include <sys/signalfd.h> 30 31#include "udev.h" 32#include "udev-util.h" 33 34static void help(void) { 35 36 printf("%s test OPTIONS <syspath>\n\n" 37 "Test an event run.\n" 38 " -h --help Show this help\n" 39 " --version Show package version\n" 40 " -a --action=ACTION Set action string\n" 41 " -N --resolve-names=early|late|never When to resolve names\n" 42 , program_invocation_short_name); 43} 44 45static int adm_test(struct udev *udev, int argc, char *argv[]) { 46 int resolve_names = 1; 47 char filename[UTIL_PATH_SIZE]; 48 const char *action = "add"; 49 const char *syspath = NULL; 50 struct udev_list_entry *entry; 51 _cleanup_udev_rules_unref_ struct udev_rules *rules = NULL; 52 _cleanup_udev_device_unref_ struct udev_device *dev = NULL; 53 _cleanup_udev_event_unref_ struct udev_event *event = NULL; 54 sigset_t mask, sigmask_orig; 55 int rc = 0, c; 56 57 static const struct option options[] = { 58 { "action", required_argument, NULL, 'a' }, 59 { "resolve-names", required_argument, NULL, 'N' }, 60 { "help", no_argument, NULL, 'h' }, 61 {} 62 }; 63 64 log_debug("version %s", VERSION); 65 66 while((c = getopt_long(argc, argv, "a:N:h", options, NULL)) >= 0) 67 switch (c) { 68 case 'a': 69 action = optarg; 70 break; 71 case 'N': 72 if (streq (optarg, "early")) { 73 resolve_names = 1; 74 } else if (streq (optarg, "late")) { 75 resolve_names = 0; 76 } else if (streq (optarg, "never")) { 77 resolve_names = -1; 78 } else { 79 fprintf(stderr, "resolve-names must be early, late or never\n"); 80 log_error("resolve-names must be early, late or never"); 81 exit(EXIT_FAILURE); 82 } 83 break; 84 case 'h': 85 help(); 86 exit(EXIT_SUCCESS); 87 case '?': 88 exit(EXIT_FAILURE); 89 default: 90 assert_not_reached("Unknown option"); 91 } 92 93 syspath = argv[optind]; 94 if (syspath == NULL) { 95 fprintf(stderr, "syspath parameter missing\n"); 96 rc = 2; 97 goto out; 98 } 99 100 printf("This program is for debugging only, it does not run any program\n" 101 "specified by a RUN key. It may show incorrect results, because\n" 102 "some values may be different, or not available at a simulation run.\n" 103 "\n"); 104 105 sigprocmask(SIG_SETMASK, NULL, &sigmask_orig); 106 107 udev_builtin_init(udev); 108 109 rules = udev_rules_new(udev, resolve_names); 110 if (rules == NULL) { 111 fprintf(stderr, "error reading rules\n"); 112 rc = 3; 113 goto out; 114 } 115 116 /* add /sys if needed */ 117 if (!startswith(syspath, "/sys")) 118 strscpyl(filename, sizeof(filename), "/sys", syspath, NULL); 119 else 120 strscpy(filename, sizeof(filename), syspath); 121 util_remove_trailing_chars(filename, '/'); 122 123 dev = udev_device_new_from_synthetic_event(udev, filename, action); 124 if (dev == NULL) { 125 fprintf(stderr, "unable to open device '%s'\n", filename); 126 rc = 4; 127 goto out; 128 } 129 130 /* don't read info from the db */ 131 udev_device_set_info_loaded(dev); 132 133 event = udev_event_new(dev); 134 135 sigfillset(&mask); 136 sigprocmask(SIG_SETMASK, &mask, &sigmask_orig); 137 event->fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC); 138 if (event->fd_signal < 0) { 139 fprintf(stderr, "error creating signalfd\n"); 140 rc = 5; 141 goto out; 142 } 143 144 udev_event_execute_rules(event, 145 60 * USEC_PER_SEC, 20 * USEC_PER_SEC, 146 NULL, 147 rules, 148 &sigmask_orig); 149 150 udev_list_entry_foreach(entry, udev_device_get_properties_list_entry(dev)) 151 printf("%s=%s\n", udev_list_entry_get_name(entry), udev_list_entry_get_value(entry)); 152 153 udev_list_entry_foreach(entry, udev_list_get_entry(&event->run_list)) { 154 char program[UTIL_PATH_SIZE]; 155 156 udev_event_apply_format(event, udev_list_entry_get_name(entry), program, sizeof(program), false); 157 printf("run: '%s'\n", program); 158 } 159out: 160 if (event != NULL && event->fd_signal >= 0) 161 close(event->fd_signal); 162 udev_builtin_exit(udev); 163 return rc; 164} 165 166const struct udevadm_cmd udevadm_test = { 167 .name = "test", 168 .cmd = adm_test, 169 .help = "Test an event run", 170 .debug = true, 171}; 172