]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/rfkill/rfkill.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include <linux/rfkill.h>
9 #include "alloc-util.h"
10 #include "device-util.h"
16 #include "parse-util.h"
17 #include "proc-cmdline.h"
18 #include "string-table.h"
19 #include "string-util.h"
23 /* Note that any write is delayed until exit and the rfkill state will not be
24 * stored for rfkill indices that disappear after a change. */
25 #define EXIT_USEC (5 * USEC_PER_SEC)
27 typedef struct write_queue_item
{
28 LIST_FIELDS(struct write_queue_item
, queue
);
34 typedef struct Context
{
35 LIST_HEAD(write_queue_item
, write_queue
);
39 static struct write_queue_item
* write_queue_item_free(struct write_queue_item
*item
) {
47 static const char* const rfkill_type_table
[NUM_RFKILL_TYPES
] = {
48 [RFKILL_TYPE_ALL
] = "all",
49 [RFKILL_TYPE_WLAN
] = "wlan",
50 [RFKILL_TYPE_BLUETOOTH
] = "bluetooth",
51 [RFKILL_TYPE_UWB
] = "uwb",
52 [RFKILL_TYPE_WIMAX
] = "wimax",
53 [RFKILL_TYPE_WWAN
] = "wwan",
54 [RFKILL_TYPE_GPS
] = "gps",
55 [RFKILL_TYPE_FM
] = "fm",
56 [RFKILL_TYPE_NFC
] = "nfc",
59 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(rfkill_type
, int);
61 static int find_device(
62 const struct rfkill_event
*event
,
64 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
65 _cleanup_free_
char *sysname
= NULL
;
72 if (asprintf(&sysname
, "rfkill%i", event
->idx
) < 0)
75 r
= sd_device_new_from_subsystem_sysname(&device
, "rfkill", sysname
);
77 return log_full_errno(IN_SET(r
, -ENOENT
, -ENXIO
, -ENODEV
) ? LOG_DEBUG
: LOG_ERR
, r
,
78 "Failed to open device '%s': %m", sysname
);
80 r
= sd_device_get_sysattr_value(device
, "name", &name
);
82 return log_device_debug_errno(device
, r
, "Device has no name, ignoring: %m");
84 log_device_debug(device
, "Operating on rfkill device '%s'.", name
);
86 *ret
= TAKE_PTR(device
);
90 struct DeviceMonitorData
{
95 static int device_monitor_handler(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
96 struct DeviceMonitorData
*data
= userdata
;
101 assert(data
->sysname
);
103 if (sd_device_get_sysname(device
, &sysname
) >= 0 && streq(sysname
, data
->sysname
)) {
104 data
->device
= sd_device_ref(device
);
105 return sd_event_exit(sd_device_monitor_get_event(monitor
), 0);
111 static int wait_for_initialized(
115 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*monitor
= NULL
;
116 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
117 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
;
118 struct DeviceMonitorData data
= {};
124 if (sd_device_get_is_initialized(device
) > 0) {
125 *ret
= sd_device_ref(device
);
129 assert_se(sd_device_get_sysname(device
, &data
.sysname
) >= 0);
131 /* Wait until the device is initialized, so that we can get
132 * access to the ID_PATH property */
134 r
= sd_event_default(&event
);
136 return log_error_errno(r
, "Failed to get default event: %m");
138 r
= sd_device_monitor_new(&monitor
);
140 return log_error_errno(r
, "Failed to acquire monitor: %m");
142 r
= sd_device_monitor_filter_add_match_subsystem_devtype(monitor
, "rfkill", NULL
);
144 return log_error_errno(r
, "Failed to add rfkill device match to monitor: %m");
146 r
= sd_device_monitor_attach_event(monitor
, event
);
148 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
150 r
= sd_device_monitor_start(monitor
, device_monitor_handler
, &data
);
152 return log_error_errno(r
, "Failed to start device monitor: %m");
154 /* Check again, maybe things changed */
155 r
= sd_device_new_from_subsystem_sysname(&d
, "rfkill", data
.sysname
);
157 return log_full_errno(IN_SET(r
, -ENOENT
, -ENXIO
, -ENODEV
) ? LOG_DEBUG
: LOG_ERR
, r
,
158 "Failed to open device '%s': %m", data
.sysname
);
160 if (sd_device_get_is_initialized(d
) > 0) {
165 r
= sd_event_loop(event
);
167 return log_error_errno(r
, "Event loop failed: %m");
169 *ret
= TAKE_PTR(data
.device
);
173 static int determine_state_file(
174 const struct rfkill_event
*event
,
177 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
, *device
= NULL
;
178 const char *path_id
, *type
;
185 r
= find_device(event
, &d
);
189 r
= wait_for_initialized(d
, &device
);
193 assert_se(type
= rfkill_type_to_string(event
->type
));
195 if (sd_device_get_property_value(device
, "ID_PATH", &path_id
) >= 0) {
196 _cleanup_free_
char *escaped_path_id
= NULL
;
198 escaped_path_id
= cescape(path_id
);
199 if (!escaped_path_id
)
202 state_file
= strjoin("/var/lib/systemd/rfkill/", escaped_path_id
, ":", type
);
204 state_file
= strjoin("/var/lib/systemd/rfkill/", type
);
213 static int load_state(Context
*c
, const struct rfkill_event
*event
) {
214 _cleanup_free_
char *state_file
= NULL
, *value
= NULL
;
215 struct rfkill_event we
;
220 assert(c
->rfkill_fd
>= 0);
223 if (shall_restore_state() == 0)
226 r
= determine_state_file(event
, &state_file
);
230 r
= read_one_line_file(state_file
, &value
);
232 /* No state file? Then save the current state */
234 r
= write_string_file(state_file
, one_zero(event
->soft
), WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
236 return log_error_errno(r
, "Failed to write state file %s: %m", state_file
);
238 log_debug("Saved state '%s' to %s.", one_zero(event
->soft
), state_file
);
242 return log_error_errno(r
, "Failed to read state file %s: %m", state_file
);
244 b
= parse_boolean(value
);
246 return log_error_errno(b
, "Failed to parse state file %s: %m", state_file
);
248 we
= (struct rfkill_event
) {
249 .op
= RFKILL_OP_CHANGE
,
254 l
= write(c
->rfkill_fd
, &we
, sizeof(we
));
256 return log_error_errno(errno
, "Failed to restore rfkill state for %i: %m", event
->idx
);
258 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
259 "Couldn't write rfkill event structure, too short.");
261 log_debug("Loaded state '%s' from %s.", one_zero(b
), state_file
);
265 static void save_state_queue_remove(Context
*c
, int idx
, const char *state_file
) {
266 struct write_queue_item
*item
, *tmp
;
270 LIST_FOREACH_SAFE(queue
, item
, tmp
, c
->write_queue
) {
271 if ((state_file
&& streq(item
->file
, state_file
)) || idx
== item
->rfkill_idx
) {
272 log_debug("Canceled previous save state of '%s' to %s.", one_zero(item
->state
), item
->file
);
273 LIST_REMOVE(queue
, c
->write_queue
, item
);
274 write_queue_item_free(item
);
279 static int save_state_queue(Context
*c
, const struct rfkill_event
*event
) {
280 _cleanup_free_
char *state_file
= NULL
;
281 struct write_queue_item
*item
;
285 assert(c
->rfkill_fd
>= 0);
288 r
= determine_state_file(event
, &state_file
);
292 save_state_queue_remove(c
, event
->idx
, state_file
);
294 item
= new0(struct write_queue_item
, 1);
298 item
->file
= TAKE_PTR(state_file
);
299 item
->rfkill_idx
= event
->idx
;
300 item
->state
= event
->soft
;
302 LIST_APPEND(queue
, c
->write_queue
, item
);
307 static int save_state_cancel(Context
*c
, const struct rfkill_event
*event
) {
308 _cleanup_free_
char *state_file
= NULL
;
312 assert(c
->rfkill_fd
>= 0);
315 r
= determine_state_file(event
, &state_file
);
316 save_state_queue_remove(c
, event
->idx
, state_file
);
323 static int save_state_write_one(struct write_queue_item
*item
) {
326 r
= write_string_file(item
->file
, one_zero(item
->state
), WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
328 return log_error_errno(r
, "Failed to write state file %s: %m", item
->file
);
330 log_debug("Saved state '%s' to %s.", one_zero(item
->state
), item
->file
);
334 static void context_save_and_clear(Context
*c
) {
335 struct write_queue_item
*i
;
339 while ((i
= c
->write_queue
)) {
340 LIST_REMOVE(queue
, c
->write_queue
, i
);
341 (void) save_state_write_one(i
);
342 write_queue_item_free(i
);
345 safe_close(c
->rfkill_fd
);
348 int main(int argc
, char *argv
[]) {
349 _cleanup_(context_save_and_clear
) Context c
= { .rfkill_fd
= -1 };
354 log_error("This program requires no arguments.");
362 r
= mkdir_p("/var/lib/systemd/rfkill", 0755);
364 log_error_errno(r
, "Failed to create rfkill directory: %m");
368 n
= sd_listen_fds(false);
370 r
= log_error_errno(n
, "Failed to determine whether we got any file descriptors passed: %m");
374 log_error("Got too many file descriptors.");
380 c
.rfkill_fd
= open("/dev/rfkill", O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
381 if (c
.rfkill_fd
< 0) {
382 if (errno
== ENOENT
) {
383 log_debug_errno(errno
, "Missing rfkill subsystem, or no device present, exiting.");
388 r
= log_error_errno(errno
, "Failed to open /dev/rfkill: %m");
392 c
.rfkill_fd
= SD_LISTEN_FDS_START
;
395 log_error_errno(r
, "Failed to make /dev/rfkill socket non-blocking: %m");
398 r
= fd_nonblock(c
.rfkill_fd
, 1);
402 struct rfkill_event event
;
406 l
= read(c
.rfkill_fd
, &event
, sizeof(event
));
408 if (errno
== EAGAIN
) {
411 /* Notify manager that we are
413 * processing whatever was
415 (void) sd_notify(false, "READY=1");
419 /* Hang around for a bit, maybe there's more coming */
421 r
= fd_wait_for_event(c
.rfkill_fd
, POLLIN
, EXIT_USEC
);
425 log_error_errno(r
, "Failed to poll() on device: %m");
431 log_debug("All events read and idle, exiting.");
435 log_error_errno(errno
, "Failed to read from /dev/rfkill: %m");
438 if (l
!= RFKILL_EVENT_SIZE_V1
) {
439 log_error("Read event structure of invalid size.");
444 type
= rfkill_type_to_string(event
.type
);
446 log_debug("An rfkill device of unknown type %i discovered, ignoring.", event
.type
);
453 log_debug("A new rfkill device has been added with index %i and type %s.", event
.idx
, type
);
454 (void) load_state(&c
, &event
);
458 log_debug("An rfkill device has been removed with index %i and type %s", event
.idx
, type
);
459 (void) save_state_cancel(&c
, &event
);
462 case RFKILL_OP_CHANGE
:
463 log_debug("An rfkill device has changed state with index %i and type %s", event
.idx
, type
);
464 (void) save_state_queue(&c
, &event
);
468 log_debug("Unknown event %i from /dev/rfkill for index %i and type %s, ignoring.", event
.op
, event
.idx
, type
);
476 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;