chiark / gitweb /
kerne-command-line: introduce option 'systemd.restore_state'
[elogind.git] / src / rfkill / rfkill.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include "util.h"
23 #include "mkdir.h"
24 #include "fileio.h"
25 #include "libudev.h"
26 #include "udev-util.h"
27 #include "utils.h"
28
29 int main(int argc, char *argv[]) {
30         _cleanup_udev_unref_ struct udev *udev = NULL;
31         _cleanup_udev_device_unref_ struct udev_device *device = NULL;
32         _cleanup_free_ char *saved = NULL, *escaped_name = NULL, *escaped_path_id = NULL;
33         const char *name, *path_id;
34         int r;
35
36         if (argc != 3) {
37                 log_error("This program requires two arguments.");
38                 return EXIT_FAILURE;
39         }
40
41         log_set_target(LOG_TARGET_AUTO);
42         log_parse_environment();
43         log_open();
44
45         umask(0022);
46
47         r = mkdir_p("/var/lib/systemd/rfkill", 0755);
48         if (r < 0) {
49                 log_error("Failed to create rfkill directory: %s", strerror(-r));
50                 return EXIT_FAILURE;
51         }
52
53         udev = udev_new();
54         if (!udev) {
55                 log_oom();
56                 return EXIT_FAILURE;
57         }
58
59         errno = 0;
60         device = udev_device_new_from_subsystem_sysname(udev, "rfkill", argv[2]);
61         if (!device) {
62                 if (errno != 0)
63                         log_error("Failed to get rfkill device '%s': %m", argv[2]);
64                 else
65                         log_oom();
66
67                 return EXIT_FAILURE;
68         }
69
70         name = udev_device_get_sysattr_value(device, "name");
71         if (!name) {
72                 log_error("rfkill device has no name?");
73                 return EXIT_FAILURE;
74         }
75
76         escaped_name = cescape(name);
77         if (!escaped_name) {
78                 log_oom();
79                 return EXIT_FAILURE;
80         }
81
82         path_id = udev_device_get_property_value(device, "ID_PATH");
83         if (path_id) {
84                 escaped_path_id = cescape(path_id);
85                 if (!escaped_path_id) {
86                         log_oom();
87                         return EXIT_FAILURE;
88                 }
89
90                 saved = strjoin("/var/lib/systemd/rfkill/", escaped_path_id, ":", escaped_name, NULL);
91         } else
92                 saved = strjoin("/var/lib/systemd/rfkill/", escaped_name, NULL);
93
94         if (!saved) {
95                 log_oom();
96                 return EXIT_FAILURE;
97         }
98
99         if (streq(argv[1], "load") && restore_state()) {
100                 _cleanup_free_ char *value = NULL;
101
102                 r = read_one_line_file(saved, &value);
103                 if (r < 0) {
104
105                         if (r == -ENOENT)
106                                 return EXIT_SUCCESS;
107
108                         log_error("Failed to read %s: %s", saved, strerror(-r));
109                         return EXIT_FAILURE;
110                 }
111
112                 r = udev_device_set_sysattr_value(device, "soft", value);
113                 if (r < 0) {
114                         log_error("Failed to write system attribute: %s", strerror(-r));
115                         return EXIT_FAILURE;
116                 }
117
118         } else if (streq(argv[1], "save")) {
119                 const char *value;
120
121                 value = udev_device_get_sysattr_value(device, "soft");
122                 if (!value) {
123                         log_error("Failed to read system attribute: %s", strerror(-r));
124                         return EXIT_FAILURE;
125                 }
126
127                 r = write_string_file(saved, value);
128                 if (r < 0) {
129                         log_error("Failed to write %s: %s", saved, strerror(-r));
130                         return EXIT_FAILURE;
131                 }
132
133         } else {
134                 log_error("Unknown verb %s.", argv[1]);
135                 return EXIT_FAILURE;
136         }
137
138         return EXIT_SUCCESS;
139 }