2 This file is part of systemd.
4 Copyright 2013 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 #include <linux/rfkill.h>
24 #include "sd-daemon.h"
26 #include "alloc-util.h"
32 #include "parse-util.h"
33 #include "proc-cmdline.h"
34 #include "string-table.h"
35 #include "string-util.h"
36 #include "udev-util.h"
40 /* Note that any write is delayed until exit and the rfkill state will not be
41 * stored for rfkill indices that disappear after a change. */
42 #define EXIT_USEC (5 * USEC_PER_SEC)
44 typedef struct write_queue_item
{
45 LIST_FIELDS(struct write_queue_item
, queue
);
51 static void write_queue_item_free(struct write_queue_item
*item
)
59 static const char* const rfkill_type_table
[NUM_RFKILL_TYPES
] = {
60 [RFKILL_TYPE_ALL
] = "all",
61 [RFKILL_TYPE_WLAN
] = "wlan",
62 [RFKILL_TYPE_BLUETOOTH
] = "bluetooth",
63 [RFKILL_TYPE_UWB
] = "uwb",
64 [RFKILL_TYPE_WIMAX
] = "wimax",
65 [RFKILL_TYPE_WWAN
] = "wwan",
66 [RFKILL_TYPE_GPS
] = "gps",
67 [RFKILL_TYPE_FM
] = "fm",
68 [RFKILL_TYPE_NFC
] = "nfc",
71 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(rfkill_type
, int);
73 static int find_device(
75 const struct rfkill_event
*event
,
76 struct udev_device
**ret
) {
78 _cleanup_free_
char *sysname
= NULL
;
79 struct udev_device
*device
;
86 if (asprintf(&sysname
, "rfkill%i", event
->idx
) < 0)
89 device
= udev_device_new_from_subsystem_sysname(udev
, "rfkill", sysname
);
91 return log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
, "Failed to open device: %m");
93 name
= udev_device_get_sysattr_value(device
, "name");
95 log_debug("Device has no name, ignoring.");
96 udev_device_unref(device
);
100 log_debug("Operating on rfkill device '%s'.", name
);
106 static int wait_for_initialized(
108 struct udev_device
*device
,
109 struct udev_device
**ret
) {
111 _cleanup_udev_monitor_unref_
struct udev_monitor
*monitor
= NULL
;
112 struct udev_device
*d
;
120 if (udev_device_get_is_initialized(device
) != 0) {
121 *ret
= udev_device_ref(device
);
125 assert_se(sysname
= udev_device_get_sysname(device
));
127 /* Wait until the device is initialized, so that we can get
128 * access to the ID_PATH property */
130 monitor
= udev_monitor_new_from_netlink(udev
, "udev");
132 return log_error_errno(errno
, "Failed to acquire monitor: %m");
134 r
= udev_monitor_filter_add_match_subsystem_devtype(monitor
, "rfkill", NULL
);
136 return log_error_errno(r
, "Failed to add rfkill udev match to monitor: %m");
138 r
= udev_monitor_enable_receiving(monitor
);
140 return log_error_errno(r
, "Failed to enable udev receiving: %m");
142 watch_fd
= udev_monitor_get_fd(monitor
);
144 return log_error_errno(watch_fd
, "Failed to get watch fd: %m");
146 /* Check again, maybe things changed */
147 d
= udev_device_new_from_subsystem_sysname(udev
, "rfkill", sysname
);
149 return log_full_errno(errno
== ENOENT
? LOG_DEBUG
: LOG_ERR
, errno
, "Failed to open device: %m");
151 if (udev_device_get_is_initialized(d
) != 0) {
157 _cleanup_udev_device_unref_
struct udev_device
*t
= NULL
;
159 r
= fd_wait_for_event(watch_fd
, POLLIN
, EXIT_USEC
);
163 return log_error_errno(r
, "Failed to watch udev monitor: %m");
165 log_error("Timed out waiting for udev monitor.");
169 t
= udev_monitor_receive_device(monitor
);
173 if (streq_ptr(udev_device_get_sysname(t
), sysname
)) {
174 *ret
= udev_device_ref(t
);
180 static int determine_state_file(
182 const struct rfkill_event
*event
,
185 _cleanup_udev_device_unref_
struct udev_device
*d
= NULL
;
186 _cleanup_udev_device_unref_
struct udev_device
*device
= NULL
;
187 const char *path_id
, *type
;
194 r
= find_device(udev
, event
, &d
);
198 r
= wait_for_initialized(udev
, d
, &device
);
202 assert_se(type
= rfkill_type_to_string(event
->type
));
204 path_id
= udev_device_get_property_value(device
, "ID_PATH");
206 _cleanup_free_
char *escaped_path_id
= NULL
;
208 escaped_path_id
= cescape(path_id
);
209 if (!escaped_path_id
)
212 state_file
= strjoin("/var/lib/systemd/rfkill/", escaped_path_id
, ":", type
);
214 state_file
= strjoin("/var/lib/systemd/rfkill/", type
);
223 static int load_state(
226 const struct rfkill_event
*event
) {
228 _cleanup_free_
char *state_file
= NULL
, *value
= NULL
;
229 struct rfkill_event we
;
233 assert(rfkill_fd
>= 0);
237 if (shall_restore_state() == 0)
240 r
= determine_state_file(udev
, event
, &state_file
);
244 r
= read_one_line_file(state_file
, &value
);
246 /* No state file? Then save the current state */
248 r
= write_string_file(state_file
, one_zero(event
->soft
), WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
250 return log_error_errno(r
, "Failed to write state file %s: %m", state_file
);
252 log_debug("Saved state '%s' to %s.", one_zero(event
->soft
), state_file
);
256 return log_error_errno(r
, "Failed to read state file %s: %m", state_file
);
258 b
= parse_boolean(value
);
260 return log_error_errno(b
, "Failed to parse state file %s: %m", state_file
);
262 we
= (struct rfkill_event
) {
263 .op
= RFKILL_OP_CHANGE
,
268 l
= write(rfkill_fd
, &we
, sizeof(we
));
270 return log_error_errno(errno
, "Failed to restore rfkill state for %i: %m", event
->idx
);
271 if (l
!= sizeof(we
)) {
272 log_error("Couldn't write rfkill event structure, too short.");
276 log_debug("Loaded state '%s' from %s.", one_zero(b
), state_file
);
280 static void save_state_queue_remove(
281 struct write_queue_item
**write_queue
,
285 struct write_queue_item
*item
, *tmp
;
287 LIST_FOREACH_SAFE(queue
, item
, tmp
, *write_queue
) {
288 if ((state_file
&& streq(item
->file
, state_file
)) || idx
== item
->rfkill_idx
) {
289 log_debug("Canceled previous save state of '%s' to %s.", one_zero(item
->state
), item
->file
);
290 LIST_REMOVE(queue
, *write_queue
, item
);
291 write_queue_item_free(item
);
296 static int save_state_queue(
297 struct write_queue_item
**write_queue
,
300 const struct rfkill_event
*event
) {
302 _cleanup_free_
char *state_file
= NULL
;
303 struct write_queue_item
*item
;
306 assert(rfkill_fd
>= 0);
310 r
= determine_state_file(udev
, event
, &state_file
);
313 save_state_queue_remove(write_queue
, event
->idx
, state_file
);
315 item
= new0(struct write_queue_item
, 1);
319 item
->file
= state_file
;
320 item
->rfkill_idx
= event
->idx
;
321 item
->state
= event
->soft
;
324 LIST_APPEND(queue
, *write_queue
, item
);
329 static int save_state_cancel(
330 struct write_queue_item
**write_queue
,
333 const struct rfkill_event
*event
) {
335 _cleanup_free_
char *state_file
= NULL
;
338 assert(rfkill_fd
>= 0);
342 r
= determine_state_file(udev
, event
, &state_file
);
343 save_state_queue_remove(write_queue
, event
->idx
, state_file
);
350 static int save_state_write(struct write_queue_item
**write_queue
) {
351 struct write_queue_item
*item
, *tmp
;
353 bool error_logged
= false;
356 LIST_FOREACH_SAFE(queue
, item
, tmp
, *write_queue
) {
357 r
= write_string_file(item
->file
, one_zero(item
->state
), WRITE_STRING_FILE_CREATE
|WRITE_STRING_FILE_ATOMIC
);
361 log_error_errno(r
, "Failed to write state file %s: %m", item
->file
);
364 log_warning_errno(r
, "Failed to write state file %s: %m", item
->file
);
366 log_debug("Saved state '%s' to %s.", one_zero(item
->state
), item
->file
);
368 LIST_REMOVE(queue
, *write_queue
, item
);
369 write_queue_item_free(item
);
374 int main(int argc
, char *argv
[]) {
375 LIST_HEAD(write_queue_item
, write_queue
);
376 _cleanup_udev_unref_
struct udev
*udev
= NULL
;
377 _cleanup_close_
int rfkill_fd
= -1;
382 log_error("This program requires no arguments.");
386 LIST_HEAD_INIT(write_queue
);
388 log_set_target(LOG_TARGET_AUTO
);
389 log_parse_environment();
400 r
= mkdir_p("/var/lib/systemd/rfkill", 0755);
402 log_error_errno(r
, "Failed to create rfkill directory: %m");
406 n
= sd_listen_fds(false);
408 r
= log_error_errno(n
, "Failed to determine whether we got any file descriptors passed: %m");
412 log_error("Got too many file descriptors.");
418 rfkill_fd
= open("/dev/rfkill", O_RDWR
|O_CLOEXEC
|O_NOCTTY
|O_NONBLOCK
);
420 if (errno
== ENOENT
) {
421 log_debug_errno(errno
, "Missing rfkill subsystem, or no device present, exiting.");
426 r
= log_error_errno(errno
, "Failed to open /dev/rfkill: %m");
430 rfkill_fd
= SD_LISTEN_FDS_START
;
432 r
= fd_nonblock(rfkill_fd
, 1);
434 log_error_errno(r
, "Failed to make /dev/rfkill socket non-blocking: %m");
440 struct rfkill_event event
;
444 l
= read(rfkill_fd
, &event
, sizeof(event
));
446 if (errno
== EAGAIN
) {
449 /* Notify manager that we are
451 * processing whatever was
453 (void) sd_notify(false, "READY=1");
457 /* Hang around for a bit, maybe there's more coming */
459 r
= fd_wait_for_event(rfkill_fd
, POLLIN
, EXIT_USEC
);
463 log_error_errno(r
, "Failed to poll() on device: %m");
469 log_debug("All events read and idle, exiting.");
473 log_error_errno(errno
, "Failed to read from /dev/rfkill: %m");
476 if (l
!= RFKILL_EVENT_SIZE_V1
) {
477 log_error("Read event structure of invalid size.");
482 type
= rfkill_type_to_string(event
.type
);
484 log_debug("An rfkill device of unknown type %i discovered, ignoring.", event
.type
);
491 log_debug("A new rfkill device has been added with index %i and type %s.", event
.idx
, type
);
492 (void) load_state(rfkill_fd
, udev
, &event
);
496 log_debug("An rfkill device has been removed with index %i and type %s", event
.idx
, type
);
497 (void) save_state_cancel(&write_queue
, rfkill_fd
, udev
, &event
);
500 case RFKILL_OP_CHANGE
:
501 log_debug("An rfkill device has changed state with index %i and type %s", event
.idx
, type
);
502 (void) save_state_queue(&write_queue
, rfkill_fd
, udev
, &event
);
506 log_debug("Unknown event %i from /dev/rfkill for index %i and type %s, ignoring.", event
.op
, event
.idx
, type
);
514 (void) save_state_write(&write_queue
);
516 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;