test-udev: use _cleanup_ and modernize style

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-09-20 13:27:04 +02:00
parent c745d2bfa0
commit 465815aaa7

View file

@ -17,6 +17,7 @@
#include "selinux-util.h"
#include "signal-util.h"
#include "string-util.h"
#include "tests.h"
#include "udev.h"
static int fake_filesystems(void) {
@ -26,27 +27,26 @@ static int fake_filesystems(void) {
const char *error;
bool ignore_mount_error;
} fakefss[] = {
{ "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 },
{ "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 },
};
unsigned int i;
unsigned i;
if (unshare(CLONE_NEWNS) < 0)
return log_error_errno(errno, "failed to call unshare(): %m");
return log_error_errno(errno, "Failed to call unshare(): %m");
if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
return log_error_errno(errno, "failed to mount / as private: %m");
return log_error_errno(errno, "Failed to mount / as private: %m");
for (i = 0; i < ELEMENTSOF(fakefss); i++) {
for (i = 0; i < ELEMENTSOF(fakefss); i++)
if (mount(fakefss[i].src, fakefss[i].target, NULL, MS_BIND, NULL) < 0) {
log_full_errno(fakefss[i].ignore_mount_error ? LOG_DEBUG : LOG_ERR, errno, "%s: %m", fakefss[i].error);
if (!fakefss[i].ignore_mount_error)
return -errno;
}
}
return 0;
}
@ -55,48 +55,46 @@ int main(int argc, char *argv[]) {
_cleanup_(udev_event_unrefp) struct udev_event *event = NULL;
_cleanup_(udev_device_unrefp) struct udev_device *dev = NULL;
_cleanup_(udev_rules_unrefp) struct udev_rules *rules = NULL;
char syspath[UTIL_PATH_SIZE];
const char *devpath;
const char *action;
int err;
const char *devpath, *action;
int r;
log_parse_environment();
log_open();
test_setup_logging(LOG_INFO);
err = fake_filesystems();
if (err < 0)
r = fake_filesystems();
if (r < 0)
return EXIT_FAILURE;
log_debug("version %s", PACKAGE_VERSION);
mac_selinux_init();
action = argv[1];
if (action == NULL) {
if (!action) {
log_error("action missing");
goto out;
}
devpath = argv[2];
if (devpath == NULL) {
if (!devpath) {
log_error("devpath missing");
goto out;
}
rules = udev_rules_new(1);
strscpyl(syspath, sizeof(syspath), "/sys", devpath, NULL);
const char *syspath = strjoina("/sys", devpath);
dev = udev_device_new_from_synthetic_event(NULL, syspath, action);
if (dev == NULL) {
if (!dev) {
log_debug("unknown device '%s'", devpath);
goto out;
}
event = udev_event_new(dev);
assert_se(event);
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGTERM, SIGINT, SIGHUP, SIGCHLD, -1) >= 0);
/* do what devtmpfs usually provides us */
if (udev_device_get_devnode(dev) != NULL) {
if (udev_device_get_devnode(dev)) {
mode_t mode = 0600;
if (streq(udev_device_get_subsystem(dev), "block"))
@ -122,5 +120,5 @@ int main(int argc, char *argv[]) {
out:
mac_selinux_finish();
return err ? EXIT_FAILURE : EXIT_SUCCESS;
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}