]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/rfkill/rfkill.c
trivial: rfkill - don't include util.h twice
[thirdparty/systemd.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
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_name = NULL, *escaped_path_id = NULL;
32 const char *name, *path_id;
33 int r;
34
35 if (argc != 3) {
36 log_error("This program requires two arguments.");
37 return EXIT_FAILURE;
38 }
39
40 log_set_target(LOG_TARGET_AUTO);
41 log_parse_environment();
42 log_open();
43
44 umask(0022);
45
46 r = mkdir_p("/var/lib/systemd/rfkill", 0755);
47 if (r < 0) {
48 log_error("Failed to create rfkill directory: %s", strerror(-r));
49 return EXIT_FAILURE;
50 }
51
52 udev = udev_new();
53 if (!udev) {
54 log_oom();
55 return EXIT_FAILURE;
56 }
57
58 errno = 0;
59 device = udev_device_new_from_subsystem_sysname(udev, "rfkill", argv[2]);
60 if (!device) {
61 if (errno != 0)
62 log_error("Failed to get rfkill device '%s': %m", argv[2]);
63 else
64 log_oom();
65
66 return EXIT_FAILURE;
67 }
68
69 name = udev_device_get_sysattr_value(device, "name");
70 if (!name) {
71 log_error("rfkill device has no name?");
72 return EXIT_FAILURE;
73 }
74
75 escaped_name = cescape(name);
76 if (!escaped_name) {
77 log_oom();
78 return EXIT_FAILURE;
79 }
80
81 path_id = udev_device_get_property_value(device, "ID_PATH");
82 if (path_id) {
83 escaped_path_id = cescape(path_id);
84 if (!escaped_path_id) {
85 log_oom();
86 return EXIT_FAILURE;
87 }
88
89 saved = strjoin("/var/lib/systemd/rfkill/", escaped_path_id, ":", escaped_name, NULL);
90 } else
91 saved = strjoin("/var/lib/systemd/rfkill/", escaped_name, NULL);
92
93 if (!saved) {
94 log_oom();
95 return EXIT_FAILURE;
96 }
97
98 if (streq(argv[1], "load") && restore_state()) {
99 _cleanup_free_ char *value = NULL;
100
101 r = read_one_line_file(saved, &value);
102 if (r < 0) {
103
104 if (r == -ENOENT)
105 return EXIT_SUCCESS;
106
107 log_error("Failed to read %s: %s", saved, strerror(-r));
108 return EXIT_FAILURE;
109 }
110
111 r = udev_device_set_sysattr_value(device, "soft", value);
112 if (r < 0) {
113 log_error("Failed to write system attribute: %s", strerror(-r));
114 return EXIT_FAILURE;
115 }
116
117 } else if (streq(argv[1], "save")) {
118 const char *value;
119
120 value = udev_device_get_sysattr_value(device, "soft");
121 if (!value) {
122 log_error("Failed to read system attribute: %s", strerror(-r));
123 return EXIT_FAILURE;
124 }
125
126 r = write_string_file(saved, value);
127 if (r < 0) {
128 log_error("Failed to write %s: %s", saved, strerror(-r));
129 return EXIT_FAILURE;
130 }
131
132 } else {
133 log_error("Unknown verb %s.", argv[1]);
134 return EXIT_FAILURE;
135 }
136
137 return EXIT_SUCCESS;
138 }