]>
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"
15 #include "main-func.h"
17 #include "parse-util.h"
18 #include "proc-cmdline.h"
19 #include "string-table.h"
20 #include "string-util.h"
21 #include "udev-util.h"
25 /* Note that any write is delayed until exit and the rfkill state will not be
26 * stored for rfkill indices that disappear after a change. */
27 #define EXIT_USEC (5 * USEC_PER_SEC)
29 typedef struct write_queue_item
{
30 LIST_FIELDS(struct write_queue_item
, queue
);
36 typedef struct Context
{
37 LIST_HEAD(write_queue_item
, write_queue
);
41 static struct write_queue_item
* write_queue_item_free(struct write_queue_item
*item
) {
49 static const char* const rfkill_type_table
[NUM_RFKILL_TYPES
] = {
50 [RFKILL_TYPE_ALL
] = "all",
51 [RFKILL_TYPE_WLAN
] = "wlan",
52 [RFKILL_TYPE_BLUETOOTH
] = "bluetooth",
53 [RFKILL_TYPE_UWB
] = "uwb",
54 [RFKILL_TYPE_WIMAX
] = "wimax",
55 [RFKILL_TYPE_WWAN
] = "wwan",
56 [RFKILL_TYPE_GPS
] = "gps",
57 [RFKILL_TYPE_FM
] = "fm",
58 [RFKILL_TYPE_NFC
] = "nfc",
61 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(rfkill_type
, int);
63 static int find_device(
64 const struct rfkill_event
*event
,
66 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
67 _cleanup_free_
char *sysname
= NULL
;
74 if (asprintf(&sysname
, "rfkill%i", event
->idx
) < 0)
77 r
= sd_device_new_from_subsystem_sysname(&device
, "rfkill", sysname
);
79 return log_full_errno(IN_SET(r
, -ENOENT
, -ENXIO
, -ENODEV
) ? LOG_DEBUG
: LOG_ERR
, r
,
80 "Failed to open device '%s': %m", sysname
);
82 r
= sd_device_get_sysattr_value(device
, "name", &name
);
84 return log_device_debug_errno(device
, r
, "Device has no name, ignoring: %m");
86 log_device_debug(device
, "Operating on rfkill device '%s'.", name
);
88 *ret
= TAKE_PTR(device
);
92 static int determine_state_file(
93 const struct rfkill_event
*event
,
96 _cleanup_(sd_device_unrefp
) sd_device
*d
= NULL
, *device
= NULL
;
97 const char *path_id
, *type
;
104 r
= find_device(event
, &d
);
108 r
= device_wait_for_initialization(d
, "rfkill", &device
);
112 assert_se(type
= rfkill_type_to_string(event
->type
));
114 if (sd_device_get_property_value(device
, "ID_PATH", &path_id
) >= 0) {
115 _cleanup_free_
char *escaped_path_id
= NULL
;
117 escaped_path_id
= cescape(path_id
);
118 if (!escaped_path_id
)
121 state_file
= strjoin("/var/lib/systemd/rfkill/", escaped_path_id
, ":", type
);
123 state_file
= strjoin("/var/lib/systemd/rfkill/", type
);
132 static int load_state(Context
*c
, const struct rfkill_event
*event
) {
133 _cleanup_free_
char *state_file
= NULL
, *value
= NULL
;
134 struct rfkill_event we
;
139 assert(c
->rfkill_fd
>= 0);
142 if (shall_restore_state() == 0)
145 r
= determine_state_file(event
, &state_file
);
149 r
= read_one_line_file(state_file
, &value
);
151 /* No state file? Then save the current state */
153 r
= write_string_file(state_file
, one_zero(event
->soft
), WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
155 return log_error_errno(r
, "Failed to write state file %s: %m", state_file
);
157 log_debug("Saved state '%s' to %s.", one_zero(event
->soft
), state_file
);
161 return log_error_errno(r
, "Failed to read state file %s: %m", state_file
);
163 b
= parse_boolean(value
);
165 return log_error_errno(b
, "Failed to parse state file %s: %m", state_file
);
167 we
= (struct rfkill_event
) {
168 .op
= RFKILL_OP_CHANGE
,
173 l
= write(c
->rfkill_fd
, &we
, sizeof(we
));
175 return log_error_errno(errno
, "Failed to restore rfkill state for %i: %m", event
->idx
);
177 return log_error_errno(SYNTHETIC_ERRNO(EIO
),
178 "Couldn't write rfkill event structure, too short.");
180 log_debug("Loaded state '%s' from %s.", one_zero(b
), state_file
);
184 static void save_state_queue_remove(Context
*c
, int idx
, const char *state_file
) {
185 struct write_queue_item
*item
, *tmp
;
189 LIST_FOREACH_SAFE(queue
, item
, tmp
, c
->write_queue
) {
190 if ((state_file
&& streq(item
->file
, state_file
)) || idx
== item
->rfkill_idx
) {
191 log_debug("Canceled previous save state of '%s' to %s.", one_zero(item
->state
), item
->file
);
192 LIST_REMOVE(queue
, c
->write_queue
, item
);
193 write_queue_item_free(item
);
198 static int save_state_queue(Context
*c
, const struct rfkill_event
*event
) {
199 _cleanup_free_
char *state_file
= NULL
;
200 struct write_queue_item
*item
;
204 assert(c
->rfkill_fd
>= 0);
207 r
= determine_state_file(event
, &state_file
);
211 save_state_queue_remove(c
, event
->idx
, state_file
);
213 item
= new0(struct write_queue_item
, 1);
217 item
->file
= TAKE_PTR(state_file
);
218 item
->rfkill_idx
= event
->idx
;
219 item
->state
= event
->soft
;
221 LIST_APPEND(queue
, c
->write_queue
, item
);
226 static int save_state_cancel(Context
*c
, const struct rfkill_event
*event
) {
227 _cleanup_free_
char *state_file
= NULL
;
231 assert(c
->rfkill_fd
>= 0);
234 r
= determine_state_file(event
, &state_file
);
235 save_state_queue_remove(c
, event
->idx
, state_file
);
242 static int save_state_write_one(struct write_queue_item
*item
) {
245 r
= write_string_file(item
->file
, one_zero(item
->state
), WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
247 return log_error_errno(r
, "Failed to write state file %s: %m", item
->file
);
249 log_debug("Saved state '%s' to %s.", one_zero(item
->state
), item
->file
);
253 static void context_save_and_clear(Context
*c
) {
254 struct write_queue_item
*i
;
258 while ((i
= c
->write_queue
)) {
259 LIST_REMOVE(queue
, c
->write_queue
, i
);
260 (void) save_state_write_one(i
);
261 write_queue_item_free(i
);
264 safe_close(c
->rfkill_fd
);
267 static int run(int argc
, char *argv
[]) {
268 _cleanup_(context_save_and_clear
) Context c
= { .rfkill_fd
= -1 };
273 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "This program requires no arguments.");
279 r
= mkdir_p("/var/lib/systemd/rfkill", 0755);
281 return log_error_errno(r
, "Failed to create rfkill directory: %m");
283 n
= sd_listen_fds(false);
285 return log_error_errno(n
, "Failed to determine whether we got any file descriptors passed: %m");
287 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Got too many file descriptors.");
290 c
.rfkill_fd
= open("/dev/rfkill", O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
291 if (c
.rfkill_fd
< 0) {
292 if (errno
== ENOENT
) {
293 log_debug_errno(errno
, "Missing rfkill subsystem, or no device present, exiting.");
297 return log_error_errno(errno
, "Failed to open /dev/rfkill: %m");
300 c
.rfkill_fd
= SD_LISTEN_FDS_START
;
302 r
= fd_nonblock(c
.rfkill_fd
, 1);
304 return log_error_errno(r
, "Failed to make /dev/rfkill socket non-blocking: %m");
308 struct rfkill_event event
;
312 l
= read(c
.rfkill_fd
, &event
, sizeof(event
));
314 if (errno
== EAGAIN
) {
317 /* Notify manager that we are
319 * processing whatever was
321 (void) sd_notify(false, "READY=1");
325 /* Hang around for a bit, maybe there's more coming */
327 r
= fd_wait_for_event(c
.rfkill_fd
, POLLIN
, EXIT_USEC
);
331 return log_error_errno(r
, "Failed to poll() on device: %m");
335 log_debug("All events read and idle, exiting.");
339 log_error_errno(errno
, "Failed to read from /dev/rfkill: %m");
342 if (l
!= RFKILL_EVENT_SIZE_V1
)
343 return log_error_errno(SYNTHETIC_ERRNO(EIO
), "Read event structure of invalid size.");
345 type
= rfkill_type_to_string(event
.type
);
347 log_debug("An rfkill device of unknown type %i discovered, ignoring.", event
.type
);
354 log_debug("A new rfkill device has been added with index %i and type %s.", event
.idx
, type
);
355 (void) load_state(&c
, &event
);
359 log_debug("An rfkill device has been removed with index %i and type %s", event
.idx
, type
);
360 (void) save_state_cancel(&c
, &event
);
363 case RFKILL_OP_CHANGE
:
364 log_debug("An rfkill device has changed state with index %i and type %s", event
.idx
, type
);
365 (void) save_state_queue(&c
, &event
);
369 log_debug("Unknown event %i from /dev/rfkill for index %i and type %s, ignoring.", event
.op
, event
.idx
, type
);
377 DEFINE_MAIN_FUNCTION(run
);