chiark / gitweb /
91536523b0b146c3eac8a0e747989e8ebbdabb17
[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
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, *ss = NULL, *escaped_name = NULL;
32         const char *sysname, *name;
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         sysname = strchr(argv[2], ':');
59         if (!sysname) {
60                 log_error("Requires pair of subsystem and sysname for specifying rfkill device.");
61                 return EXIT_FAILURE;
62         }
63
64         ss = strndup(argv[2], sysname - argv[2]);
65         if (!ss) {
66                 log_oom();
67                 return EXIT_FAILURE;
68         }
69
70         sysname++;
71
72         if (!streq(ss, "rfkill")) {
73                 log_error("Not a rfkill device: '%s:%s'", ss, sysname);
74                 return EXIT_FAILURE;
75         }
76
77         errno = 0;
78         device = udev_device_new_from_subsystem_sysname(udev, ss, sysname);
79         if (!device) {
80                 if (errno != 0)
81                         log_error("Failed to get rfkill device '%s:%s': %m", ss, sysname);
82                 else
83                         log_oom();
84
85                 return EXIT_FAILURE;
86         }
87
88         name = udev_device_get_sysattr_value(device, "name");
89         if (!name) {
90                 log_error("rfkill device has no name?");
91                 return EXIT_FAILURE;
92         }
93
94         escaped_name = cescape(name);
95         if (!escaped_name) {
96                 log_oom();
97                 return EXIT_FAILURE;
98         }
99
100         saved = strjoin("/var/lib/systemd/rfkill/", escaped_name, NULL);
101         if (!saved) {
102                 log_oom();
103                 return EXIT_FAILURE;
104         }
105
106         if (streq(argv[1], "load")) {
107                 _cleanup_free_ char *value = NULL;
108
109                 r = read_one_line_file(saved, &value);
110                 if (r < 0) {
111
112                         if (r == -ENOENT)
113                                 return EXIT_SUCCESS;
114
115                         log_error("Failed to read %s: %s", saved, strerror(-r));
116                         return EXIT_FAILURE;
117                 }
118
119                 r = udev_device_set_sysattr_value(device, "soft", value);
120                 if (r < 0) {
121                         log_error("Failed to write system attribute: %s", strerror(-r));
122                         return EXIT_FAILURE;
123                 }
124
125         } else if (streq(argv[1], "save")) {
126                 const char *value;
127
128                 value = udev_device_get_sysattr_value(device, "soft");
129                 if (!value) {
130                         log_error("Failed to read system attribute: %s", strerror(-r));
131                         return EXIT_FAILURE;
132                 }
133
134                 r = write_string_file(saved, value);
135                 if (r < 0) {
136                         log_error("Failed to write %s: %s", saved, strerror(-r));
137                         return EXIT_FAILURE;
138                 }
139
140         } else {
141                 log_error("Unknown verb %s.", argv[1]);
142                 return EXIT_FAILURE;
143         }
144
145         return EXIT_SUCCESS;
146 }