2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-11-12 19:36:23 +01:00
|
|
|
/***
|
2018-06-12 19:00:24 +02:00
|
|
|
Copyright © 2003-2004 Greg Kroah-Hartman <greg@kroah.com>
|
2012-11-12 19:36:23 +01:00
|
|
|
***/
|
2005-04-27 05:59:47 +02:00
|
|
|
|
2004-03-23 07:22:20 +01:00
|
|
|
#include <errno.h>
|
2012-07-15 22:10:46 +02:00
|
|
|
#include <sched.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2012-07-15 22:10:46 +02:00
|
|
|
#include <sys/mount.h>
|
2011-04-20 01:53:03 +02:00
|
|
|
#include <sys/signalfd.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <unistd.h>
|
2003-04-10 19:53:46 +02:00
|
|
|
|
2018-12-20 20:35:25 +01:00
|
|
|
#include "build.h"
|
2018-11-01 14:58:42 +01:00
|
|
|
#include "device-private.h"
|
2015-10-26 21:16:26 +01:00
|
|
|
#include "fs-util.h"
|
2016-04-11 21:31:57 +02:00
|
|
|
#include "log.h"
|
2018-11-20 16:40:49 +01:00
|
|
|
#include "main-func.h"
|
2018-11-16 03:44:17 +01:00
|
|
|
#include "mkdir.h"
|
2020-09-03 16:56:07 +02:00
|
|
|
#include "mount-util.h"
|
2020-02-02 18:56:12 +01:00
|
|
|
#include "namespace-util.h"
|
2014-12-27 18:46:36 +01:00
|
|
|
#include "selinux-util.h"
|
2015-06-02 17:07:21 +02:00
|
|
|
#include "signal-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-09-20 13:27:04 +02:00
|
|
|
#include "tests.h"
|
2019-04-25 01:21:11 +02:00
|
|
|
#include "udev-event.h"
|
2005-04-27 05:59:47 +02:00
|
|
|
|
2012-07-15 22:10:46 +02:00
|
|
|
static int fake_filesystems(void) {
|
|
|
|
static const struct fakefs {
|
|
|
|
const char *src;
|
|
|
|
const char *target;
|
|
|
|
const char *error;
|
2016-04-10 21:29:17 +02:00
|
|
|
bool ignore_mount_error;
|
2012-07-15 22:10:46 +02:00
|
|
|
} fakefss[] = {
|
2018-09-20 13:27:04 +02:00
|
|
|
{ "test/tmpfs/sys", "/sys", "Failed to mount test /sys", false },
|
|
|
|
{ "test/tmpfs/dev", "/dev", "Failed to mount test /dev", false },
|
|
|
|
{ "test/run", "/run", "Failed to mount test /run", false },
|
|
|
|
{ "test/run", "/etc/udev/rules.d", "Failed to mount empty /etc/udev/rules.d", true },
|
|
|
|
{ "test/run", UDEVLIBEXECDIR "/rules.d", "Failed to mount empty " UDEVLIBEXECDIR "/rules.d", true },
|
2012-07-15 22:10:46 +02:00
|
|
|
};
|
2020-02-02 18:56:12 +01:00
|
|
|
int r;
|
2012-07-15 22:10:46 +02:00
|
|
|
|
2020-02-02 18:56:12 +01:00
|
|
|
r = detach_mount_namespace();
|
|
|
|
if (r < 0)
|
|
|
|
return log_error_errno(r, "Failed to detach mount namespace: %m");
|
2012-10-08 20:50:20 +02:00
|
|
|
|
2020-09-03 16:56:07 +02:00
|
|
|
for (size_t i = 0; i < ELEMENTSOF(fakefss); i++) {
|
2020-09-22 15:51:17 +02:00
|
|
|
r = mount_nofollow_verbose(fakefss[i].ignore_mount_error ? LOG_NOTICE : LOG_ERR,
|
|
|
|
fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL);
|
2020-09-03 16:56:07 +02:00
|
|
|
if (r < 0 && !fakefss[i].ignore_mount_error)
|
|
|
|
return r;
|
|
|
|
}
|
2016-04-11 21:31:57 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-07-15 22:10:46 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 16:40:49 +01:00
|
|
|
static int run(int argc, char *argv[]) {
|
2018-12-14 10:24:40 +01:00
|
|
|
_cleanup_(udev_rules_freep) UdevRules *rules = NULL;
|
2018-12-14 10:27:24 +01:00
|
|
|
_cleanup_(udev_event_freep) UdevEvent *event = NULL;
|
2018-11-01 14:58:42 +01:00
|
|
|
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
|
|
|
|
const char *devpath, *devname, *action;
|
|
|
|
int r;
|
2012-07-15 22:10:46 +02:00
|
|
|
|
2018-09-20 13:27:04 +02:00
|
|
|
test_setup_logging(LOG_INFO);
|
2016-04-11 21:31:57 +02:00
|
|
|
|
2020-09-03 16:56:30 +02:00
|
|
|
if (!IN_SET(argc, 2, 3))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"This program needs one or two arguments, %d given", argc - 1);
|
2018-10-09 09:23:52 +02:00
|
|
|
|
2018-11-20 16:40:49 +01:00
|
|
|
r = fake_filesystems();
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-09-03 16:53:22 +02:00
|
|
|
/* Let's make sure the test runs with selinux assumed disabled. */
|
|
|
|
#if HAVE_SELINUX
|
|
|
|
fini_selinuxmnt();
|
|
|
|
#endif
|
|
|
|
mac_selinux_retest();
|
|
|
|
|
2018-11-12 05:20:20 +01:00
|
|
|
if (argc == 2) {
|
2020-09-03 16:56:30 +02:00
|
|
|
if (!streq(argv[1], "check"))
|
|
|
|
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
|
|
|
"Unknown argument: %s", argv[1]);
|
2018-11-12 05:20:20 +01:00
|
|
|
|
2018-11-20 16:40:49 +01:00
|
|
|
return 0;
|
2018-11-12 05:20:20 +01:00
|
|
|
}
|
2018-11-09 04:01:15 +01:00
|
|
|
|
2018-12-20 20:35:25 +01:00
|
|
|
log_debug("version %s", GIT_VERSION);
|
2020-06-25 14:36:49 +02:00
|
|
|
|
|
|
|
r = mac_selinux_init();
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
action = argv[1];
|
|
|
|
devpath = argv[2];
|
|
|
|
|
2020-06-23 17:58:33 +02:00
|
|
|
assert_se(udev_rules_load(&rules, RESOLVE_NAME_EARLY) == 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-09-03 16:56:47 +02:00
|
|
|
const char *syspath;
|
|
|
|
syspath = strjoina("/sys", devpath);
|
2018-11-01 14:58:42 +01:00
|
|
|
r = device_new_from_synthetic_event(&dev, syspath, action);
|
2018-11-20 16:40:49 +01:00
|
|
|
if (r < 0)
|
|
|
|
return log_debug_errno(r, "Failed to open device '%s'", devpath);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2020-12-08 14:37:12 +01:00
|
|
|
assert_se(event = udev_event_new(dev, 0, NULL, log_get_max_level()));
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2015-06-15 20:13:23 +02:00
|
|
|
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
|
2012-01-10 01:34:15 +01:00
|
|
|
|
|
|
|
/* do what devtmpfs usually provides us */
|
2018-11-01 14:58:42 +01:00
|
|
|
if (sd_device_get_devname(dev, &devname) >= 0) {
|
|
|
|
const char *subsystem;
|
2012-04-09 16:37:54 +02:00
|
|
|
mode_t mode = 0600;
|
2012-01-10 01:34:15 +01:00
|
|
|
|
2018-11-01 14:58:42 +01:00
|
|
|
if (sd_device_get_subsystem(dev, &subsystem) >= 0 && streq(subsystem, "block"))
|
2012-01-10 01:34:15 +01:00
|
|
|
mode |= S_IFBLK;
|
|
|
|
else
|
|
|
|
mode |= S_IFCHR;
|
|
|
|
|
2013-02-13 18:13:22 +01:00
|
|
|
if (!streq(action, "remove")) {
|
2018-11-01 14:58:42 +01:00
|
|
|
dev_t devnum = makedev(0, 0);
|
|
|
|
|
|
|
|
(void) mkdir_parents_label(devname, 0755);
|
|
|
|
(void) sd_device_get_devnum(dev, &devnum);
|
2018-12-11 14:55:34 +01:00
|
|
|
if (mknod(devname, mode, devnum) < 0)
|
|
|
|
return log_error_errno(errno, "mknod() failed for '%s': %m", devname);
|
2012-01-10 01:34:15 +01:00
|
|
|
} else {
|
2018-12-11 14:55:34 +01:00
|
|
|
if (unlink(devname) < 0)
|
|
|
|
return log_error_errno(errno, "unlink('%s') failed: %m", devname);
|
2018-11-01 14:58:42 +01:00
|
|
|
(void) rmdir_parents(devname, "/");
|
2012-01-10 01:34:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 11:14:25 +02:00
|
|
|
udev_event_execute_rules(event, 3 * USEC_PER_SEC, SIGKILL, NULL, rules);
|
|
|
|
udev_event_execute_run(event, 3 * USEC_PER_SEC, SIGKILL);
|
2013-10-13 02:28:21 +02:00
|
|
|
|
2018-11-20 16:40:49 +01:00
|
|
|
return 0;
|
2005-04-27 05:59:47 +02:00
|
|
|
}
|
2018-11-20 16:40:49 +01:00
|
|
|
|
|
|
|
DEFINE_MAIN_FUNCTION(run);
|