2017-11-18 17:14:42 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0+ */
|
2006-03-17 01:52:15 +01:00
|
|
|
/*
|
2012-11-12 19:36:23 +01:00
|
|
|
* Copyright (C) 2008-2009 Kay Sievers <kay@vrfy.org>
|
2006-03-17 01:52:15 +01:00
|
|
|
*
|
2008-09-10 02:40:42 +02:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2006-03-17 01:52:15 +01:00
|
|
|
*
|
2008-09-10 02:40:42 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2006-03-17 01:52:15 +01:00
|
|
|
*/
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <getopt.h>
|
2006-03-17 01:52:15 +01:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <string.h>
|
2006-03-17 01:52:15 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2013-12-18 03:49:07 +01:00
|
|
|
#include "udev-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "udev.h"
|
2014-11-26 01:39:19 +01:00
|
|
|
#include "udevadm-util.h"
|
2013-11-03 12:50:58 +01:00
|
|
|
#include "util.h"
|
2006-03-17 01:52:15 +01:00
|
|
|
|
2006-09-19 17:51:31 +02:00
|
|
|
static int verbose;
|
|
|
|
static int dry_run;
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) {
|
2012-01-10 01:34:15 +01:00
|
|
|
struct udev_list_entry *entry;
|
|
|
|
|
|
|
|
udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
|
|
|
|
char filename[UTIL_PATH_SIZE];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf("%s\n", udev_list_entry_get_name(entry));
|
|
|
|
if (dry_run)
|
|
|
|
continue;
|
2013-01-09 19:06:46 +01:00
|
|
|
strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
|
2014-02-13 14:59:56 +01:00
|
|
|
fd = open(filename, O_WRONLY|O_CLOEXEC);
|
2012-04-08 16:06:20 +02:00
|
|
|
if (fd < 0)
|
2012-01-10 01:34:15 +01:00
|
|
|
continue;
|
|
|
|
if (write(fd, action, strlen(action)) < 0)
|
2014-11-28 19:29:59 +01:00
|
|
|
log_debug_errno(errno, "error writing '%s' to '%s': %m", action, filename);
|
2012-01-10 01:34:15 +01:00
|
|
|
close(fd);
|
|
|
|
}
|
2006-03-17 01:52:15 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static const char *keyval(const char *str, const char **val, char *buf, size_t size) {
|
2012-01-10 01:34:15 +01:00
|
|
|
char *pos;
|
|
|
|
|
2013-01-09 19:06:46 +01:00
|
|
|
strscpy(buf, size,str);
|
2012-01-10 01:34:15 +01:00
|
|
|
pos = strchr(buf, '=');
|
|
|
|
if (pos != NULL) {
|
|
|
|
pos[0] = 0;
|
|
|
|
pos++;
|
|
|
|
}
|
|
|
|
*val = pos;
|
|
|
|
return buf;
|
2009-06-07 02:07:54 +02:00
|
|
|
}
|
|
|
|
|
2013-12-18 03:48:14 +01:00
|
|
|
static void help(void) {
|
2015-01-05 13:19:55 +01:00
|
|
|
printf("%s trigger OPTIONS\n\n"
|
|
|
|
"Request events from the kernel.\n\n"
|
|
|
|
" -h --help Show this help\n"
|
|
|
|
" --version Show package version\n"
|
|
|
|
" -v --verbose Print the list of devices while running\n"
|
|
|
|
" -n --dry-run Do not actually trigger the events\n"
|
|
|
|
" -t --type= Type of events to trigger\n"
|
|
|
|
" devices sysfs devices (default)\n"
|
|
|
|
" subsystems sysfs subsystems and drivers\n"
|
|
|
|
" -c --action=ACTION Event action value, default is \"change\"\n"
|
|
|
|
" -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
|
|
|
|
" -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
|
|
|
|
" -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
|
|
|
|
" -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
|
|
|
|
" -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
|
|
|
|
" -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
|
|
|
|
" -y --sysname-match=NAME Trigger devices with this /sys path\n"
|
|
|
|
" --name-match=NAME Trigger devices with this /dev name\n"
|
|
|
|
" -b --parent-match=NAME Trigger devices with that parent device\n"
|
|
|
|
, program_invocation_short_name);
|
2013-12-18 03:48:14 +01:00
|
|
|
}
|
|
|
|
|
2014-07-29 15:47:41 +02:00
|
|
|
static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
|
2014-11-26 02:32:03 +01:00
|
|
|
enum {
|
|
|
|
ARG_NAME = 0x100,
|
|
|
|
};
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
static const struct option options[] = {
|
2014-11-26 02:32:03 +01:00
|
|
|
{ "verbose", no_argument, NULL, 'v' },
|
|
|
|
{ "dry-run", no_argument, NULL, 'n' },
|
|
|
|
{ "type", required_argument, NULL, 't' },
|
|
|
|
{ "action", required_argument, NULL, 'c' },
|
|
|
|
{ "subsystem-match", required_argument, NULL, 's' },
|
|
|
|
{ "subsystem-nomatch", required_argument, NULL, 'S' },
|
|
|
|
{ "attr-match", required_argument, NULL, 'a' },
|
|
|
|
{ "attr-nomatch", required_argument, NULL, 'A' },
|
|
|
|
{ "property-match", required_argument, NULL, 'p' },
|
|
|
|
{ "tag-match", required_argument, NULL, 'g' },
|
|
|
|
{ "sysname-match", required_argument, NULL, 'y' },
|
|
|
|
{ "name-match", required_argument, NULL, ARG_NAME },
|
|
|
|
{ "parent-match", required_argument, NULL, 'b' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
2012-01-10 01:34:15 +01:00
|
|
|
{}
|
|
|
|
};
|
|
|
|
enum {
|
|
|
|
TYPE_DEVICES,
|
|
|
|
TYPE_SUBSYSTEMS,
|
|
|
|
} device_type = TYPE_DEVICES;
|
|
|
|
const char *action = "change";
|
2013-12-18 03:49:07 +01:00
|
|
|
_cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL;
|
2015-06-23 17:16:28 +02:00
|
|
|
int c, r;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
udev_enumerate = udev_enumerate_new(udev);
|
2013-12-18 03:49:07 +01:00
|
|
|
if (udev_enumerate == NULL)
|
|
|
|
return 1;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2013-12-18 03:48:14 +01:00
|
|
|
while ((c = getopt_long(argc, argv, "vno:t:c:s:S:a:A:p:g:y:b:h", options, NULL)) >= 0) {
|
2012-01-10 01:34:15 +01:00
|
|
|
const char *key;
|
|
|
|
const char *val;
|
|
|
|
char buf[UTIL_PATH_SIZE];
|
|
|
|
|
2013-12-18 03:48:14 +01:00
|
|
|
switch (c) {
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
dry_run = 1;
|
|
|
|
break;
|
|
|
|
case 't':
|
2013-12-18 03:49:07 +01:00
|
|
|
if (streq(optarg, "devices"))
|
2012-01-10 01:34:15 +01:00
|
|
|
device_type = TYPE_DEVICES;
|
2013-12-18 03:49:07 +01:00
|
|
|
else if (streq(optarg, "subsystems"))
|
2012-01-10 01:34:15 +01:00
|
|
|
device_type = TYPE_SUBSYSTEMS;
|
2013-12-18 03:49:07 +01:00
|
|
|
else {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("unknown type --type=%s", optarg);
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'c':
|
2017-10-04 19:32:12 +02:00
|
|
|
if (!STR_IN_SET(optarg, "add", "remove", "change")) {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("unknown action '%s'", optarg);
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
|
|
|
} else
|
2013-11-03 12:50:58 +01:00
|
|
|
action = optarg;
|
2013-12-18 03:49:07 +01:00
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 's':
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add subsystem match '%s': %m", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'S':
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add negative subsystem match '%s': %m", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
key = keyval(optarg, &val, buf, sizeof(buf));
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add sysattr match '%s=%s': %m", key, val);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'A':
|
|
|
|
key = keyval(optarg, &val, buf, sizeof(buf));
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add negative sysattr match '%s=%s': %m", key, val);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
key = keyval(optarg, &val, buf, sizeof(buf));
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_property(udev_enumerate, key, val);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add property match '%s=%s': %m", key, val);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'g':
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_tag(udev_enumerate, optarg);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add tag match '%s': %m", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'y':
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_sysname(udev_enumerate, optarg);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add sysname match '%s': %m", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
case 'b': {
|
2014-11-26 01:39:19 +01:00
|
|
|
_cleanup_udev_device_unref_ struct udev_device *dev;
|
|
|
|
|
|
|
|
dev = find_device(udev, optarg, "/sys");
|
2012-01-10 01:34:15 +01:00
|
|
|
if (dev == NULL) {
|
2013-12-24 16:39:37 +01:00
|
|
|
log_error("unable to open the device '%s'", optarg);
|
2013-12-18 03:49:07 +01:00
|
|
|
return 2;
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2014-11-26 01:39:19 +01:00
|
|
|
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_parent(udev_enumerate, dev);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add parent match '%s': %m", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
2012-01-10 01:34:15 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-11-26 01:39:19 +01:00
|
|
|
|
2014-11-26 02:32:03 +01:00
|
|
|
case ARG_NAME: {
|
|
|
|
_cleanup_udev_device_unref_ struct udev_device *dev;
|
|
|
|
|
|
|
|
dev = find_device(udev, optarg, "/dev/");
|
|
|
|
if (dev == NULL) {
|
|
|
|
log_error("unable to open the device '%s'", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_parent(udev_enumerate, dev);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add parent match '%s': %m", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
2014-11-26 02:32:03 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
case 'h':
|
2013-12-18 03:48:14 +01:00
|
|
|
help();
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2013-12-18 03:48:14 +01:00
|
|
|
case '?':
|
2013-12-18 03:49:07 +01:00
|
|
|
return 1;
|
2013-12-18 03:48:14 +01:00
|
|
|
default:
|
|
|
|
assert_not_reached("Unknown option");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 02:32:03 +01:00
|
|
|
for (; optind < argc; optind++) {
|
|
|
|
_cleanup_udev_device_unref_ struct udev_device *dev;
|
|
|
|
|
|
|
|
dev = find_device(udev, argv[optind], NULL);
|
|
|
|
if (dev == NULL) {
|
|
|
|
log_error("unable to open the device '%s'", argv[optind]);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2015-06-23 17:16:28 +02:00
|
|
|
r = udev_enumerate_add_match_parent(udev_enumerate, dev);
|
|
|
|
if (r < 0) {
|
|
|
|
log_error_errno(r, "could not add tag match '%s': %m", optarg);
|
|
|
|
return 2;
|
|
|
|
}
|
2013-12-18 03:48:14 +01:00
|
|
|
}
|
|
|
|
|
2012-01-10 01:34:15 +01:00
|
|
|
switch (device_type) {
|
|
|
|
case TYPE_SUBSYSTEMS:
|
|
|
|
udev_enumerate_scan_subsystems(udev_enumerate);
|
|
|
|
exec_list(udev_enumerate, action);
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
case TYPE_DEVICES:
|
|
|
|
udev_enumerate_scan_devices(udev_enumerate);
|
|
|
|
exec_list(udev_enumerate, action);
|
2013-12-18 03:49:07 +01:00
|
|
|
return 0;
|
2012-01-10 01:34:15 +01:00
|
|
|
default:
|
2013-03-30 06:38:36 +01:00
|
|
|
assert_not_reached("device_type");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
2006-03-17 01:52:15 +01:00
|
|
|
}
|
2011-07-14 01:53:23 +02:00
|
|
|
|
|
|
|
const struct udevadm_cmd udevadm_trigger = {
|
2012-01-10 01:34:15 +01:00
|
|
|
.name = "trigger",
|
|
|
|
.cmd = adm_trigger,
|
2015-01-05 13:19:55 +01:00
|
|
|
.help = "Request events from the kernel",
|
2011-07-14 01:53:23 +02:00
|
|
|
};
|