]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/rfkill/rfkill.c
Merge pull request #500 from zonque/fileio
[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_type = NULL, *escaped_path_id = NULL;
32 const char *name, *type, *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_errno(r, "Failed to create rfkill directory: %m");
49 return EXIT_FAILURE;
50 }
51
52 udev = udev_new();
53 if (!udev) {
54 log_oom();
55 return EXIT_FAILURE;
56 }
57
58 device = udev_device_new_from_subsystem_sysname(udev, "rfkill", argv[2]);
59 if (!device) {
60 log_debug_errno(errno, "Failed to get rfkill device '%s', ignoring: %m", argv[2]);
61 return EXIT_SUCCESS;
62 }
63
64 name = udev_device_get_sysattr_value(device, "name");
65 if (!name) {
66 log_error("rfkill device has no name? Ignoring device.");
67 return EXIT_SUCCESS;
68 }
69
70 log_debug("Operating on rfkill device '%s'.", name);
71
72 type = udev_device_get_sysattr_value(device, "type");
73 if (!type) {
74 log_error("rfkill device has no type? Ignoring device.");
75 return EXIT_SUCCESS;
76 }
77
78 escaped_type = cescape(type);
79 if (!escaped_type) {
80 log_oom();
81 return EXIT_FAILURE;
82 }
83
84 path_id = udev_device_get_property_value(device, "ID_PATH");
85 if (path_id) {
86 escaped_path_id = cescape(path_id);
87 if (!escaped_path_id) {
88 log_oom();
89 return EXIT_FAILURE;
90 }
91
92 saved = strjoin("/var/lib/systemd/rfkill/", escaped_path_id, ":", escaped_type, NULL);
93 } else
94 saved = strjoin("/var/lib/systemd/rfkill/", escaped_type, NULL);
95
96 if (!saved) {
97 log_oom();
98 return EXIT_FAILURE;
99 }
100
101 if (streq(argv[1], "load")) {
102 _cleanup_free_ char *value = NULL;
103
104 if (!shall_restore_state())
105 return EXIT_SUCCESS;
106
107 r = read_one_line_file(saved, &value);
108 if (r == -ENOENT)
109 return EXIT_SUCCESS;
110 if (r < 0) {
111 log_error_errno(r, "Failed to read %s: %m", saved);
112 return EXIT_FAILURE;
113 }
114
115 r = udev_device_set_sysattr_value(device, "soft", value);
116 if (r < 0) {
117 log_debug_errno(r, "Failed to write 'soft' attribute on rfkill device, ignoring: %m");
118 return EXIT_SUCCESS;
119 }
120
121 } else if (streq(argv[1], "save")) {
122 const char *value;
123
124 value = udev_device_get_sysattr_value(device, "soft");
125 if (!value) {
126 log_debug_errno(r, "Failed to read system attribute, ignoring device: %m");
127 return EXIT_SUCCESS;
128 }
129
130 r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
131 if (r < 0) {
132 log_error_errno(r, "Failed to write %s: %m", saved);
133 return EXIT_FAILURE;
134 }
135
136 } else {
137 log_error("Unknown verb %s.", argv[1]);
138 return EXIT_FAILURE;
139 }
140
141 return EXIT_SUCCESS;
142 }