1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-28 02:50:16 +03:00

udev-dump: also show written sysfs attributes and sysctl entries (#36091)

Split-out of #35968.
This commit is contained in:
Luca Boccassi 2025-01-22 20:56:31 +00:00 committed by GitHub
commit 338813351e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 66 additions and 6 deletions

View File

@ -10,12 +10,46 @@
#include "udev-event.h"
#include "user-util.h"
static void event_cache_written_value(Hashmap **values, const char *attr, const char *value) {
assert(values);
_unused_ _cleanup_free_ void *key = NULL;
free(hashmap_remove2(*values, attr, &key));
if (hashmap_put_strdup_full(values, &path_hash_ops_free_free, attr, value) < 0)
log_oom_debug();
}
void event_cache_written_sysattr(UdevEvent *event, const char *attr, const char *value) {
event_cache_written_value(&event->written_sysattrs, attr, value);
}
void event_cache_written_sysctl(UdevEvent *event, const char *attr, const char *value) {
event_cache_written_value(&event->written_sysctls, attr, value);
}
void dump_event(UdevEvent *event, FILE *f) {
sd_device *dev = ASSERT_PTR(ASSERT_PTR(event)->dev);
if (!f)
f = stdout;
if (!hashmap_isempty(event->written_sysattrs)) {
const char *key, *value;
fprintf(f, "%sWritten sysfs attributes:%s\n", ansi_highlight(), ansi_normal());
HASHMAP_FOREACH_KEY(value, key, event->written_sysattrs)
fprintf(f, " %s : %s\n", key, value);
}
if (!hashmap_isempty(event->written_sysctls)) {
const char *key, *value;
fprintf(f, "%sWritten sysctl entries:%s\n", ansi_highlight(), ansi_normal());
HASHMAP_FOREACH_KEY(value, key, event->written_sysctls)
fprintf(f, " %s : %s\n", key, value);
}
fprintf(f, "%sProperties:%s\n", ansi_highlight(), ansi_normal());
FOREACH_DEVICE_PROPERTY(dev, key, value)
fprintf(f, " %s=%s\n", key, value);
@ -29,8 +63,8 @@ void dump_event(UdevEvent *event, FILE *f) {
if (sd_device_get_devnum(dev, NULL) >= 0) {
if (sd_device_get_devlink_first(dev)) {
int prio;
device_get_devlink_priority(dev, &prio);
int prio = 0;
(void) device_get_devlink_priority(dev, &prio);
fprintf(f, "%sDevice node symlinks:%s (priority=%i)\n", ansi_highlight(), ansi_normal(), prio);
FOREACH_DEVICE_DEVLINK(dev, devlink)
fprintf(f, " %s\n", devlink);

View File

@ -5,4 +5,6 @@
typedef struct UdevEvent UdevEvent;
void event_cache_written_sysattr(UdevEvent *event, const char *attr, const char *value);
void event_cache_written_sysctl(UdevEvent *event, const char *attr, const char *value);
void dump_event(UdevEvent *event, FILE *f);

View File

@ -54,6 +54,8 @@ static UdevEvent* udev_event_free(UdevEvent *event) {
sd_netlink_unref(event->rtnl);
ordered_hashmap_free_free_key(event->run_list);
ordered_hashmap_free_free_free(event->seclabel_list);
hashmap_free(event->written_sysattrs);
hashmap_free(event->written_sysctls);
free(event->program_result);
free(event->name);
strv_free(event->altnames);

View File

@ -36,6 +36,8 @@ typedef struct UdevEvent {
gid_t gid;
OrderedHashmap *seclabel_list;
OrderedHashmap *run_list;
Hashmap *written_sysattrs;
Hashmap *written_sysctls;
usec_t birth_usec;
unsigned builtin_run;
unsigned builtin_ret;

View File

@ -2959,11 +2959,19 @@ static int udev_rule_apply_token_to_event(
WRITE_STRING_FILE_VERIFY_IGNORE_NEWLINE);
if (r < 0)
log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysfs attribute \"%s\", ignoring: %m", value, buf);
else
else {
event_cache_written_sysattr(event, buf, value);
log_event_done(event, token);
} else
}
} else {
log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysfs attribute \"%s\".", value, buf);
r = verify_regular_at(AT_FDCWD, buf, /* follow = */ false);
if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
log_event_error_errno(event, token, r, "Failed to verify sysfs attribute \"%s\" is a regular file: %m", buf);
else
event_cache_written_sysattr(event, buf, value);
}
return true;
}
case TK_A_SYSCTL: {
@ -2982,11 +2990,23 @@ static int udev_rule_apply_token_to_event(
r = sysctl_write(buf, value);
if (r < 0)
log_event_error_errno(event, token, r, "Failed to write \"%s\" to sysctl entry \"%s\", ignoring: %m", value, buf);
else
else {
event_cache_written_sysctl(event, buf, value);
log_event_done(event, token);
} else
}
} else {
log_event_debug(event, token, "Running in test mode, skipping writing \"%s\" to sysctl entry \"%s\".", value, buf);
_cleanup_free_ char *path = path_join("/proc/sys/", buf);
if (!path)
return log_oom();
r = verify_regular_at(AT_FDCWD, path, /* follow = */ true);
if (r < 0 && !ERRNO_IS_NEG_PRIVILEGE(r))
log_event_error_errno(event, token, r, "Failed to verify sysctl entry \"%s\" is a regular file: %m", buf);
else
event_cache_written_sysctl(event, buf, value);
}
return true;
}
case TK_A_RUN_BUILTIN: