From ece1e280ee8b332185dd17b68032eafb7ed17cbc Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Thu, 1 Aug 2024 12:26:44 +0900 Subject: [PATCH] sd-device-monitor: introduce sd_device_monitor_is_running() --- src/libsystemd/libsystemd.sym | 1 + src/libsystemd/sd-device/device-monitor.c | 7 +++++++ src/libsystemd/sd-device/test-sd-device-monitor.c | 13 +++++++++++++ src/systemd/sd-device.h | 1 + 4 files changed, 22 insertions(+) diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index af2ea430f5a..fa3c0e9933f 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -1044,4 +1044,5 @@ global: sd_varlink_take_fd; sd_varlink_unref; sd_varlink_wait; + sd_device_monitor_is_running; } LIBSYSTEMD_256; diff --git a/src/libsystemd/sd-device/device-monitor.c b/src/libsystemd/sd-device/device-monitor.c index 69d6864f005..5881d02540d 100644 --- a/src/libsystemd/sd-device/device-monitor.c +++ b/src/libsystemd/sd-device/device-monitor.c @@ -246,6 +246,13 @@ _public_ int sd_device_monitor_new(sd_device_monitor **ret) { return device_monitor_new_full(ret, MONITOR_GROUP_UDEV, -EBADF); } +_public_ int sd_device_monitor_is_running(sd_device_monitor *m) { + if (!m) + return 0; + + return sd_event_source_get_enabled(m->event_source, NULL); +} + _public_ int sd_device_monitor_stop(sd_device_monitor *m) { assert_return(m, -EINVAL); diff --git a/src/libsystemd/sd-device/test-sd-device-monitor.c b/src/libsystemd/sd-device/test-sd-device-monitor.c index 613e17a68ac..50505a5b121 100644 --- a/src/libsystemd/sd-device/test-sd-device-monitor.c +++ b/src/libsystemd/sd-device/test-sd-device-monitor.c @@ -99,6 +99,19 @@ static void send_by_enumerator( } } +TEST(sd_device_monitor_is_running) { + _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL; + + ASSERT_EQ(sd_device_monitor_is_running(NULL), 0); + + ASSERT_OK(device_monitor_new_full(&m, MONITOR_GROUP_NONE, -1)); + ASSERT_EQ(sd_device_monitor_is_running(m), 0); + ASSERT_OK(sd_device_monitor_start(m, NULL, NULL)); + ASSERT_EQ(sd_device_monitor_is_running(m), 1); + ASSERT_OK(sd_device_monitor_stop(m)); + ASSERT_EQ(sd_device_monitor_is_running(m), 0); +} + TEST(refuse_invalid_device) { _cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor_server = NULL, *monitor_client = NULL; _cleanup_(sd_device_unrefp) sd_device *loopback = NULL; diff --git a/src/systemd/sd-device.h b/src/systemd/sd-device.h index 4a14c394573..40786fc1cdc 100644 --- a/src/systemd/sd-device.h +++ b/src/systemd/sd-device.h @@ -149,6 +149,7 @@ sd_event *sd_device_monitor_get_event(sd_device_monitor *m); sd_event_source *sd_device_monitor_get_event_source(sd_device_monitor *m); int sd_device_monitor_set_description(sd_device_monitor *m, const char *description); int sd_device_monitor_get_description(sd_device_monitor *m, const char **ret); +int sd_device_monitor_is_running(sd_device_monitor *m); int sd_device_monitor_start(sd_device_monitor *m, sd_device_monitor_handler_t callback, void *userdata); int sd_device_monitor_stop(sd_device_monitor *m); -- 2.47.3