1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-19 22:50:17 +03:00

udev-builtin: several trivial cleanups (#36239)

- introduces UDEV_BUILTIN_DESTRUCTOR macro,
- normalize log messages on initialize/finalize,
- explicitly initialize global variable,
- drop using _UDEV_BUILTIN_INVALID.
This commit is contained in:
Yu Watanabe 2025-02-06 09:38:39 +09:00 committed by GitHub
commit 2b6890d92f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 47 deletions

View File

@ -15,7 +15,7 @@
#include "string-util.h"
#include "udev-builtin.h"
static sd_hwdb *hwdb;
static sd_hwdb *hwdb = NULL;
int udev_builtin_hwdb_lookup(
UdevEvent *event,
@ -199,12 +199,14 @@ static int builtin_hwdb_init(void) {
if (r < 0)
return r;
log_debug("Loaded hardware database.");
return 0;
}
/* called on udev shutdown and reload request */
static void builtin_hwdb_exit(void) {
hwdb = sd_hwdb_unref(hwdb);
log_debug("Unloaded hardware database.");
}
/* called every couple of seconds during event activity; 'true' if config has changed */

View File

@ -57,12 +57,11 @@ static int builtin_kmod_init(void) {
if (ctx)
return 0;
log_debug("Loading kernel module index.");
r = module_setup_context(&ctx);
if (r < 0)
return log_error_errno(r, "Failed to initialize libkmod context: %m");
log_debug("Loaded kernel module index.");
return 0;
}
@ -71,8 +70,8 @@ static void builtin_kmod_exit(void) {
if (!ctx)
return;
log_debug("Unload kernel module index.");
ctx = sym_kmod_unref(ctx);
log_debug("Unloaded kernel module index.");
}
/* called every couple of seconds during event activity; 'true' if config has changed */

View File

@ -82,7 +82,7 @@ static int builtin_net_setup_link_init(void) {
if (r < 0)
return r;
log_debug("Created link configuration context.");
log_debug("Loaded link configuration context.");
return 0;
}

View File

@ -54,6 +54,15 @@ extern const UdevBuiltin udev_builtin_usb_id;
void udev_builtin_init(void);
void udev_builtin_exit(void);
static inline void udev_builtin_exitp(bool *p) {
if (*ASSERT_PTR(p))
udev_builtin_exit();
}
#define _UDEV_BUILTIN_DESTRUCTOR(u) \
_unused_ _cleanup_(udev_builtin_exitp) bool v = true;
#define UDEV_BUILTIN_DESTRUCTOR \
_UDEV_BUILTIN_DESTRUCTOR(UNIQ_T(builtin_destructor, UNIQ))
UdevBuiltinCommand udev_builtin_lookup(const char *command);
const char* udev_builtin_name(UdevBuiltinCommand cmd);
bool udev_builtin_run_once(UdevBuiltinCommand cmd);

View File

@ -121,7 +121,7 @@ void dump_event(UdevEvent *event, FILE *f) {
ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list) {
UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val);
if (builtin_cmd != _UDEV_BUILTIN_INVALID)
if (builtin_cmd >= 0)
fprintf(f, " RUN{builtin} : %s\n", command);
else
fprintf(f, " RUN{program} : %s\n", command);

View File

@ -343,7 +343,7 @@ void udev_event_execute_run(UdevEvent *event) {
ORDERED_HASHMAP_FOREACH_KEY(val, command, event->run_list) {
UdevBuiltinCommand builtin_cmd = PTR_TO_UDEV_BUILTIN_CMD(val);
if (builtin_cmd != _UDEV_BUILTIN_INVALID) {
if (builtin_cmd >= 0) {
log_device_debug(event->dev, "Running built-in command \"%s\"", command);
r = udev_builtin_run(event, builtin_cmd, command);
if (r < 0)

View File

@ -87,42 +87,30 @@ int builtin_main(int argc, char *argv[], void *userdata) {
return r;
udev_builtin_init();
UDEV_BUILTIN_DESTRUCTOR;
cmd = udev_builtin_lookup(arg_command);
if (cmd < 0) {
r = log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command '%s'", arg_command);
goto finish;
}
if (cmd < 0)
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown command '%s'", arg_command);
r = find_device_with_action(arg_syspath, arg_action, &dev);
if (r < 0) {
log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
goto finish;
}
if (r < 0)
return log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
event = udev_event_new(dev, NULL, EVENT_UDEVADM_TEST_BUILTIN);
if (!event) {
r = log_oom();
goto finish;
}
if (!event)
return log_oom();
if (arg_action != SD_DEVICE_REMOVE) {
/* For net_setup_link */
r = device_clone_with_db(dev, &event->dev_db_clone);
if (r < 0) {
log_device_error_errno(dev, r, "Failed to clone device: %m");
goto finish;
}
if (r < 0)
return log_device_error_errno(dev, r, "Failed to clone device: %m");
}
r = udev_builtin_run(event, cmd, arg_command);
if (r < 0) {
log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
goto finish;
}
if (r < 0)
return log_debug_errno(r, "Builtin command '%s' fails: %m", arg_command);
r = 0;
finish:
udev_builtin_exit();
return r;
return 0;
}

View File

@ -125,30 +125,25 @@ int test_main(int argc, char *argv[], void *userdata) {
puts("\nLoading builtins...");
udev_builtin_init();
UDEV_BUILTIN_DESTRUCTOR;
puts("Loading builtins done.");
puts("\nLoading udev rules files...");
r = udev_rules_load(&rules, arg_resolve_name_timing, arg_extra_rules_dir);
if (r < 0) {
log_error_errno(r, "Failed to read udev rules: %m");
goto out;
}
if (r < 0)
return log_error_errno(r, "Failed to read udev rules: %m");
puts("Loading udev rules files done.");
r = find_device_with_action(arg_syspath, arg_action, &dev);
if (r < 0) {
log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
goto out;
}
if (r < 0)
return log_error_errno(r, "Failed to open device '%s': %m", arg_syspath);
/* don't read info from the db */
device_seal(dev);
event = udev_event_new(dev, NULL, EVENT_UDEVADM_TEST);
if (!event) {
log_oom();
goto out;
}
if (!event)
return log_oom();
event->trace = arg_verbose;
assert_se(sigfillset(&mask) >= 0);
@ -161,8 +156,5 @@ int test_main(int argc, char *argv[], void *userdata) {
puts("");
dump_event(event, NULL);
r = 0;
out:
udev_builtin_exit();
return r;
return 0;
}