X Tutup
Skip to content

Commit 1383150

Browse files
committed
rfkill: replace udev_monitor by sd_device_monitor
1 parent eb96839 commit 1383150

File tree

1 file changed

+47
-39
lines changed

1 file changed

+47
-39
lines changed

src/rfkill/rfkill.c

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include "fd-util.h"
1212
#include "fileio.h"
1313
#include "io-util.h"
14-
#include "libudev-private.h"
1514
#include "mkdir.h"
1615
#include "parse-util.h"
1716
#include "proc-cmdline.h"
@@ -82,14 +81,36 @@ static int find_device(
8281
return 0;
8382
}
8483

84+
struct DeviceMonitorData {
85+
const char *sysname;
86+
sd_device *device;
87+
};
88+
89+
static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) {
90+
struct DeviceMonitorData *data = userdata;
91+
const char *sysname;
92+
93+
assert(device);
94+
assert(data);
95+
assert(data->sysname);
96+
97+
if (sd_device_get_sysname(device, &sysname) >= 0 && streq(sysname, data->sysname)) {
98+
data->device = sd_device_ref(device);
99+
return sd_event_exit(sd_device_monitor_get_event(monitor), 0);
100+
}
101+
102+
return 0;
103+
}
104+
85105
static int wait_for_initialized(
86106
sd_device *device,
87107
sd_device **ret) {
88108

89-
_cleanup_(udev_monitor_unrefp) struct udev_monitor *monitor = NULL;
109+
_cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL;
110+
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
90111
_cleanup_(sd_device_unrefp) sd_device *d = NULL;
91-
int initialized, watch_fd, r;
92-
const char *sysname;
112+
struct DeviceMonitorData data = {};
113+
int initialized, r;
93114

94115
assert(device);
95116
assert(ret);
@@ -99,61 +120,48 @@ static int wait_for_initialized(
99120
return 0;
100121
}
101122

102-
assert_se(sd_device_get_sysname(device, &sysname) >= 0);
123+
assert_se(sd_device_get_sysname(device, &data.sysname) >= 0);
103124

104125
/* Wait until the device is initialized, so that we can get
105126
* access to the ID_PATH property */
106127

107-
monitor = udev_monitor_new_from_netlink(NULL, "udev");
108-
if (!monitor)
109-
return log_error_errno(errno, "Failed to acquire monitor: %m");
128+
r = sd_event_default(&event);
129+
if (r < 0)
130+
return log_error_errno(r, "Failed to get default event: %m");
131+
132+
r = sd_device_monitor_new(&monitor);
133+
if (r < 0)
134+
return log_error_errno(r, "Failed to acquire monitor: %m");
110135

111-
r = udev_monitor_filter_add_match_subsystem_devtype(monitor, "rfkill", NULL);
136+
r = sd_device_monitor_filter_add_match_subsystem_devtype(monitor, "rfkill", NULL);
112137
if (r < 0)
113-
return log_error_errno(r, "Failed to add rfkill udev match to monitor: %m");
138+
return log_error_errno(r, "Failed to add rfkill device match to monitor: %m");
114139

115-
r = udev_monitor_enable_receiving(monitor);
140+
r = sd_device_monitor_attach_event(monitor, event, 0);
116141
if (r < 0)
117-
return log_error_errno(r, "Failed to enable udev receiving: %m");
142+
return log_error_errno(r, "Failed to attach event to device monitor: %m");
118143

119-
watch_fd = udev_monitor_get_fd(monitor);
120-
if (watch_fd < 0)
121-
return log_error_errno(watch_fd, "Failed to get watch fd: %m");
144+
r = sd_device_monitor_start(monitor, device_monitor_handler, &data, "rfkill-device-monitor");
145+
if (r < 0)
146+
return log_error_errno(r, "Failed to start device monitor: %m");
122147

123148
/* Check again, maybe things changed */
124-
r = sd_device_new_from_subsystem_sysname(&d, "rfkill", sysname);
149+
r = sd_device_new_from_subsystem_sysname(&d, "rfkill", data.sysname);
125150
if (r < 0)
126151
return log_full_errno(IN_SET(r, -ENOENT, -ENXIO, -ENODEV) ? LOG_DEBUG : LOG_ERR, r,
127-
"Failed to open device '%s': %m", sysname);
152+
"Failed to open device '%s': %m", data.sysname);
128153

129154
if (sd_device_get_is_initialized(d, &initialized) >= 0 && initialized) {
130155
*ret = TAKE_PTR(d);
131156
return 0;
132157
}
133158

134-
for (;;) {
135-
_cleanup_(sd_device_unrefp) sd_device *t = NULL;
136-
const char *name;
137-
138-
r = fd_wait_for_event(watch_fd, POLLIN, EXIT_USEC);
139-
if (r == -EINTR)
140-
continue;
141-
if (r < 0)
142-
return log_error_errno(r, "Failed to watch udev monitor: %m");
143-
if (r == 0) {
144-
log_error("Timed out waiting for udev monitor.");
145-
return -ETIMEDOUT;
146-
}
147-
148-
r = udev_monitor_receive_sd_device(monitor, &t);
149-
if (r < 0)
150-
continue;
159+
r = sd_event_loop(event);
160+
if (r < 0)
161+
return log_error_errno(r, "Event loop failed: %m");
151162

152-
if (sd_device_get_sysname(t, &name) >= 0 && streq(name, sysname)) {
153-
*ret = TAKE_PTR(t);
154-
return 0;
155-
}
156-
}
163+
*ret = TAKE_PTR(data.device);
164+
return 0;
157165
}
158166

159167
static int determine_state_file(

0 commit comments

Comments
 (0)
X Tutup