1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "udev-util.h"
28 int main(int argc, char *argv[]) {
29 _cleanup_udev_unref_ struct udev *udev = NULL;
30 _cleanup_udev_device_unref_ struct udev_device *device = NULL;
31 _cleanup_free_ char *saved = NULL, *escaped_type = NULL, *escaped_path_id = NULL;
32 const char *name, *type, *path_id;
36 log_error("This program requires two arguments.");
40 log_set_target(LOG_TARGET_AUTO);
41 log_parse_environment();
46 r = mkdir_p("/var/lib/systemd/rfkill", 0755);
48 log_error_errno(r, "Failed to create rfkill directory: %m");
58 device = udev_device_new_from_subsystem_sysname(udev, "rfkill", argv[2]);
60 log_debug_errno(errno, "Failed to get rfkill device '%s', ignoring: %m", argv[2]);
64 name = udev_device_get_sysattr_value(device, "name");
66 log_error("rfkill device has no name? Ignoring device.");
70 log_debug("Operating on rfkill device '%s'.", name);
72 type = udev_device_get_sysattr_value(device, "type");
74 log_error("rfkill device has no type? Ignoring device.");
78 escaped_type = cescape(type);
84 path_id = udev_device_get_property_value(device, "ID_PATH");
86 escaped_path_id = cescape(path_id);
87 if (!escaped_path_id) {
92 saved = strjoin("/var/lib/systemd/rfkill/", escaped_path_id, ":", escaped_type, NULL);
94 saved = strjoin("/var/lib/systemd/rfkill/", escaped_type, NULL);
101 if (streq(argv[1], "load")) {
102 _cleanup_free_ char *value = NULL;
104 if (!shall_restore_state())
107 r = read_one_line_file(saved, &value);
111 log_error_errno(r, "Failed to read %s: %m", saved);
115 r = udev_device_set_sysattr_value(device, "soft", value);
117 log_debug_errno(r, "Failed to write 'soft' attribute on rfkill device, ignoring: %m");
121 } else if (streq(argv[1], "save")) {
124 value = udev_device_get_sysattr_value(device, "soft");
126 log_debug_errno(r, "Failed to read system attribute, ignoring device: %m");
130 r = write_string_file(saved, value);
132 log_error_errno(r, "Failed to write %s: %m", saved);
137 log_error("Unknown verb %s.", argv[1]);