1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

device-monitor: Add extra fields to the log context before callback

This commit is contained in:
Daan De Meyer 2022-12-01 11:48:34 +01:00
parent 1773292745
commit e808a1d12e
3 changed files with 82 additions and 0 deletions

View File

@ -242,11 +242,15 @@ _public_ int sd_device_monitor_stop(sd_device_monitor *m) {
static int device_monitor_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) { static int device_monitor_event_handler(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL; _cleanup_(sd_device_unrefp) sd_device *device = NULL;
_unused_ _cleanup_(log_context_freep) LogContext *c = NULL;
sd_device_monitor *m = ASSERT_PTR(userdata); sd_device_monitor *m = ASSERT_PTR(userdata);
if (device_monitor_receive_device(m, &device) <= 0) if (device_monitor_receive_device(m, &device) <= 0)
return 0; return 0;
if (log_context_enabled())
c = log_context_new_consume(device_make_log_fields(device));
if (m->callback) if (m->callback)
return m->callback(m, device, m->userdata); return m->callback(m, device, m->userdata);

View File

@ -5,6 +5,7 @@
#include "devnum-util.h" #include "devnum-util.h"
#include "fd-util.h" #include "fd-util.h"
#include "string-util.h" #include "string-util.h"
#include "strv.h"
int devname_from_devnum(mode_t mode, dev_t devnum, char **ret) { int devname_from_devnum(mode_t mode, dev_t devnum, char **ret) {
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
@ -63,3 +64,78 @@ int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret) {
return TAKE_FD(fd); return TAKE_FD(fd);
} }
static int add_string_field(
sd_device *device,
const char *field,
int (*func)(sd_device *dev, const char **s),
char ***strv) {
const char *s;
int r;
assert(device);
assert(field);
assert(func);
assert(strv);
r = func(device, &s);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"%s\" property, ignoring: %m", field);
if (r >= 0)
(void) strv_extend_assignment(strv, field, s);
return 0;
}
char** device_make_log_fields(sd_device *device) {
_cleanup_strv_free_ char **strv = NULL;
dev_t devnum;
int ifindex;
sd_device_action_t action;
uint64_t seqnum, diskseq;
int r;
assert(device);
(void) add_string_field(device, "SYSPATH", sd_device_get_syspath, &strv);
(void) add_string_field(device, "SUBSYSTEM", sd_device_get_subsystem, &strv);
(void) add_string_field(device, "DEVTYPE", sd_device_get_devtype, &strv);
(void) add_string_field(device, "DRIVER", sd_device_get_driver, &strv);
(void) add_string_field(device, "DEVPATH", sd_device_get_devpath, &strv);
(void) add_string_field(device, "DEVNAME", sd_device_get_devname, &strv);
(void) add_string_field(device, "SYSNAME", sd_device_get_sysname, &strv);
(void) add_string_field(device, "SYSNUM", sd_device_get_sysnum, &strv);
r = sd_device_get_devnum(device, &devnum);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"DEVNUM\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "DEVNUM="DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(devnum));
r = sd_device_get_ifindex(device, &ifindex);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"IFINDEX\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "IFINDEX=%i", ifindex);
r = sd_device_get_action(device, &action);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"ACTION\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "ACTION=%s", device_action_to_string(action));
r = sd_device_get_seqnum(device, &seqnum);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"SEQNUM\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "SEQNUM=%"PRIu64, seqnum);
r = sd_device_get_diskseq(device, &diskseq);
if (r < 0 && r != -ENOENT)
log_device_debug_errno(device, r, "Failed to get device \"DISKSEQ\" property, ignoring: %m");
if (r >= 0)
(void) strv_extendf(&strv, "DISKSEQ=%"PRIu64, diskseq);
return TAKE_PTR(strv);
}

View File

@ -99,3 +99,5 @@ static inline int devname_from_stat_rdev(const struct stat *st, char **ret) {
return devname_from_devnum(st->st_mode, st->st_rdev, ret); return devname_from_devnum(st->st_mode, st->st_rdev, ret);
} }
int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret); int device_open_from_devnum(mode_t mode, dev_t devnum, int flags, char **ret);
char** device_make_log_fields(sd_device *device);