udevadm-monitor: use hash ops with destructor
This also make it use STATIC_DESTRUCTOR_REGISTER() macro, and logs OOM error.
This commit is contained in:
+21
-30
@@ -15,10 +15,11 @@
|
||||
#include "hashmap.h"
|
||||
#include "set.h"
|
||||
#include "signal-util.h"
|
||||
#include "static-destruct.h"
|
||||
#include "string-util.h"
|
||||
#include "time-util.h"
|
||||
#include "udevadm.h"
|
||||
#include "virt.h"
|
||||
#include "time-util.h"
|
||||
|
||||
static bool arg_show_property = false;
|
||||
static bool arg_print_kernel = false;
|
||||
@@ -26,6 +27,9 @@ static bool arg_print_udev = false;
|
||||
static Set *arg_tag_filter = NULL;
|
||||
static Hashmap *arg_subsystem_filter = NULL;
|
||||
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_tag_filter, set_freep);
|
||||
STATIC_DESTRUCTOR_REGISTER(arg_subsystem_filter, hashmap_freep);
|
||||
|
||||
static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
|
||||
sd_device_action_t action = _SD_DEVICE_ACTION_INVALID;
|
||||
const char *devpath = NULL, *subsystem = NULL;
|
||||
@@ -143,28 +147,27 @@ static int parse_argv(int argc, char *argv[]) {
|
||||
if (slash) {
|
||||
devtype = strdup(slash + 1);
|
||||
if (!devtype)
|
||||
return -ENOMEM;
|
||||
return log_oom();
|
||||
|
||||
subsystem = strndup(optarg, slash - optarg);
|
||||
} else
|
||||
subsystem = strdup(optarg);
|
||||
|
||||
if (!subsystem)
|
||||
return -ENOMEM;
|
||||
return log_oom();
|
||||
|
||||
r = hashmap_ensure_put(&arg_subsystem_filter, NULL, subsystem, devtype);
|
||||
r = hashmap_ensure_put(&arg_subsystem_filter, &trivial_hash_ops_free_free, subsystem, devtype);
|
||||
if (r < 0)
|
||||
return r;
|
||||
return log_oom();
|
||||
|
||||
TAKE_PTR(subsystem);
|
||||
TAKE_PTR(devtype);
|
||||
break;
|
||||
}
|
||||
case 't':
|
||||
/* optarg is stored in argv[], so we don't need to copy it */
|
||||
r = set_ensure_put(&arg_tag_filter, &string_hash_ops, optarg);
|
||||
r = set_put_strdup(&arg_tag_filter, optarg);
|
||||
if (r < 0)
|
||||
return r;
|
||||
return log_oom();
|
||||
break;
|
||||
|
||||
case 'V':
|
||||
@@ -192,7 +195,7 @@ int monitor_main(int argc, char *argv[], void *userdata) {
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
if (r <= 0)
|
||||
goto finalize;
|
||||
return r;
|
||||
|
||||
if (running_in_chroot() > 0) {
|
||||
log_info("Running in chroot, ignoring request.");
|
||||
@@ -203,22 +206,18 @@ int monitor_main(int argc, char *argv[], void *userdata) {
|
||||
setlinebuf(stdout);
|
||||
|
||||
r = sd_event_default(&event);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to initialize event: %m");
|
||||
goto finalize;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to initialize event: %m");
|
||||
|
||||
r = sd_event_set_signal_exit(event, true);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to install SIGINT/SIGTERM handling: %m");
|
||||
goto finalize;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to install SIGINT/SIGTERM handling: %m");
|
||||
|
||||
printf("monitor will print the received events for:\n");
|
||||
if (arg_print_udev) {
|
||||
r = setup_monitor(MONITOR_GROUP_UDEV, event, &udev_monitor);
|
||||
if (r < 0)
|
||||
goto finalize;
|
||||
return r;
|
||||
|
||||
printf("UDEV - the event which udev sends out after rule processing\n");
|
||||
}
|
||||
@@ -226,23 +225,15 @@ int monitor_main(int argc, char *argv[], void *userdata) {
|
||||
if (arg_print_kernel) {
|
||||
r = setup_monitor(MONITOR_GROUP_KERNEL, event, &kernel_monitor);
|
||||
if (r < 0)
|
||||
goto finalize;
|
||||
return r;
|
||||
|
||||
printf("KERNEL - the kernel uevent\n");
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
r = sd_event_loop(event);
|
||||
if (r < 0) {
|
||||
log_error_errno(r, "Failed to run event loop: %m");
|
||||
goto finalize;
|
||||
}
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Failed to run event loop: %m");
|
||||
|
||||
r = 0;
|
||||
|
||||
finalize:
|
||||
hashmap_free_free_free(arg_subsystem_filter);
|
||||
set_free(arg_tag_filter);
|
||||
|
||||
return r;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user