]>
Commit | Line | Data |
---|---|---|
3990f247 LP |
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; | |
f6f738db LP |
31 | _cleanup_free_ char *saved = NULL, *escaped_name = NULL, *escaped_path_id = NULL; |
32 | const char *name, *path_id; | |
3990f247 LP |
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) { | |
da927ba9 | 48 | log_error_errno(r, "Failed to create rfkill directory: %m"); |
3990f247 LP |
49 | return EXIT_FAILURE; |
50 | } | |
51 | ||
52 | udev = udev_new(); | |
53 | if (!udev) { | |
54 | log_oom(); | |
55 | return EXIT_FAILURE; | |
56 | } | |
57 | ||
3990f247 | 58 | errno = 0; |
f6f738db | 59 | device = udev_device_new_from_subsystem_sysname(udev, "rfkill", argv[2]); |
3990f247 LP |
60 | if (!device) { |
61 | if (errno != 0) | |
56f64d95 | 62 | log_error_errno(errno, "Failed to get rfkill device '%s': %m", argv[2]); |
3990f247 LP |
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 | ||
f6f738db LP |
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 | ||
be3f52f4 | 89 | saved = strjoin("/var/lib/systemd/rfkill/", escaped_path_id, ":", escaped_name, NULL); |
f6f738db LP |
90 | } else |
91 | saved = strjoin("/var/lib/systemd/rfkill/", escaped_name, NULL); | |
92 | ||
3990f247 LP |
93 | if (!saved) { |
94 | log_oom(); | |
95 | return EXIT_FAILURE; | |
96 | } | |
97 | ||
354806fb | 98 | if (streq(argv[1], "load")) { |
3990f247 LP |
99 | _cleanup_free_ char *value = NULL; |
100 | ||
354806fb BN |
101 | if (!shall_restore_state()) |
102 | return EXIT_SUCCESS; | |
103 | ||
3990f247 LP |
104 | r = read_one_line_file(saved, &value); |
105 | if (r < 0) { | |
106 | ||
107 | if (r == -ENOENT) | |
108 | return EXIT_SUCCESS; | |
109 | ||
da927ba9 | 110 | log_error_errno(r, "Failed to read %s: %m", saved); |
3990f247 LP |
111 | return EXIT_FAILURE; |
112 | } | |
113 | ||
114 | r = udev_device_set_sysattr_value(device, "soft", value); | |
115 | if (r < 0) { | |
da927ba9 | 116 | log_error_errno(r, "Failed to write system attribute: %m"); |
3990f247 LP |
117 | return EXIT_FAILURE; |
118 | } | |
119 | ||
120 | } else if (streq(argv[1], "save")) { | |
121 | const char *value; | |
122 | ||
123 | value = udev_device_get_sysattr_value(device, "soft"); | |
124 | if (!value) { | |
da927ba9 | 125 | log_error_errno(r, "Failed to read system attribute: %m"); |
3990f247 LP |
126 | return EXIT_FAILURE; |
127 | } | |
128 | ||
129 | r = write_string_file(saved, value); | |
130 | if (r < 0) { | |
da927ba9 | 131 | log_error_errno(r, "Failed to write %s: %m", saved); |
3990f247 LP |
132 | return EXIT_FAILURE; |
133 | } | |
134 | ||
135 | } else { | |
136 | log_error("Unknown verb %s.", argv[1]); | |
137 | return EXIT_FAILURE; | |
138 | } | |
139 | ||
140 | return EXIT_SUCCESS; | |
141 | } |