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

Merge pull request #34139 from yuwata/sd-device-monitor

sd-device-monitor: introduce sd_device_monitor_get_events() and _get_timeout()
This commit is contained in:
Yu Watanabe 2024-08-28 11:32:51 +09:00 committed by GitHub
commit 29d41efd44
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 41 additions and 12 deletions

View File

@ -1048,5 +1048,7 @@ global:
sd_varlink_wait;
sd_device_monitor_is_running;
sd_device_monitor_get_fd;
sd_device_monitor_get_events;
sd_device_monitor_get_timeout;
sd_device_monitor_receive;
} LIBSYSTEMD_256;

View File

@ -131,6 +131,22 @@ _public_ int sd_device_monitor_get_fd(sd_device_monitor *m) {
return m->sock;
}
_public_ int sd_device_monitor_get_events(sd_device_monitor *m) {
assert_return(m, -EINVAL);
assert_return(m->sock >= 0, -ESTALE);
return EPOLLIN;
}
_public_ int sd_device_monitor_get_timeout(sd_device_monitor *m, uint64_t *ret) {
assert_return(m, -EINVAL);
assert_return(m->sock >= 0, -ESTALE);
if (ret)
*ret = USEC_INFINITY;
return 0;
}
int device_monitor_new_full(sd_device_monitor **ret, MonitorNetlinkGroup group, int fd) {
_cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL;
_cleanup_close_ int sock = -EBADF;

View File

@ -48,7 +48,7 @@ static int monitor_handler(sd_device_monitor *m, sd_device *d, void *userdata) {
const char *s, *syspath = userdata;
ASSERT_OK(sd_device_get_syspath(d, &s));
ASSERT_TRUE(streq(s, syspath));
ASSERT_STREQ(s, syspath);
return sd_event_exit(sd_device_monitor_get_event(m), 100);
}
@ -104,14 +104,14 @@ 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_ZERO(sd_device_monitor_is_running(NULL));
ASSERT_OK(device_monitor_new_full(&m, MONITOR_GROUP_NONE, -1));
ASSERT_EQ(sd_device_monitor_is_running(m), 0);
ASSERT_OK_ZERO(sd_device_monitor_is_running(m));
ASSERT_OK(sd_device_monitor_start(m, NULL, NULL));
ASSERT_EQ(sd_device_monitor_is_running(m), 1);
ASSERT_OK_POSITIVE(sd_device_monitor_is_running(m));
ASSERT_OK(sd_device_monitor_stop(m));
ASSERT_EQ(sd_device_monitor_is_running(m), 0);
ASSERT_OK_ZERO(sd_device_monitor_is_running(m));
}
TEST(sd_device_monitor_start_stop) {
@ -377,20 +377,29 @@ TEST(sd_device_monitor_receive) {
ASSERT_OK(device_monitor_send(monitor_server, &sa, device));
ASSERT_OK(fd = sd_device_monitor_get_fd(monitor_client));
for (;;) {
r = fd_wait_for_event(fd, POLLIN, 10 * USEC_PER_SEC);
usec_t timeout;
int events;
ASSERT_OK(events = sd_device_monitor_get_events(monitor_client));
ASSERT_EQ(events, (int) EPOLLIN);
ASSERT_OK(sd_device_monitor_get_timeout(monitor_client, &timeout));
ASSERT_EQ(timeout, USEC_INFINITY);
r = fd_wait_for_event(fd, events, MAX(10 * USEC_PER_SEC, timeout));
if (r == -EINTR)
continue;
ASSERT_GT(r, 0);
ASSERT_OK_POSITIVE(r);
break;
}
_cleanup_(sd_device_unrefp) sd_device *dev = NULL;
ASSERT_GT(sd_device_monitor_receive(monitor_client, &dev), 0);
ASSERT_OK_POSITIVE(sd_device_monitor_receive(monitor_client, &dev));
const char *s;
ASSERT_OK(sd_device_get_syspath(dev, &s));
ASSERT_TRUE(streq(s, syspath));
ASSERT_STREQ(s, syspath);
}
static int intro(void) {

View File

@ -1569,7 +1569,7 @@ static void handle_revents(sd_varlink *v, int revents) {
}
}
_public_ int sd_varlink_wait(sd_varlink *v, usec_t timeout) {
_public_ int sd_varlink_wait(sd_varlink *v, uint64_t timeout) {
int r, events;
usec_t t;
@ -1683,7 +1683,7 @@ _public_ int sd_varlink_get_events(sd_varlink *v) {
return ret;
}
_public_ int sd_varlink_get_timeout(sd_varlink *v, usec_t *ret) {
_public_ int sd_varlink_get_timeout(sd_varlink *v, uint64_t *ret) {
assert_return(v, -EINVAL);
if (v->state == VARLINK_DISCONNECTED)
@ -2817,7 +2817,7 @@ _public_ int sd_varlink_get_peer_pidfd(sd_varlink *v) {
return v->peer_pidfd;
}
_public_ int sd_varlink_set_relative_timeout(sd_varlink *v, usec_t timeout) {
_public_ int sd_varlink_set_relative_timeout(sd_varlink *v, uint64_t timeout) {
assert_return(v, -EINVAL);
assert_return(timeout > 0, -EINVAL);

View File

@ -143,6 +143,8 @@ sd_device_monitor *sd_device_monitor_ref(sd_device_monitor *m);
sd_device_monitor *sd_device_monitor_unref(sd_device_monitor *m);
int sd_device_monitor_get_fd(sd_device_monitor *m);
int sd_device_monitor_get_events(sd_device_monitor *m);
int sd_device_monitor_get_timeout(sd_device_monitor *m, uint64_t *ret);
int sd_device_monitor_set_receive_buffer_size(sd_device_monitor *m, size_t size);
int sd_device_monitor_attach_event(sd_device_monitor *m, sd_event *event);
int sd_device_monitor_detach_event(sd_device_monitor *m);