]>
Commit | Line | Data |
---|---|---|
3731acf1 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 <libudev.h> | |
23 | ||
24 | #include "util.h" | |
25 | #include "mkdir.h" | |
26 | #include "fileio.h" | |
27 | ||
28 | int main(int argc, char *argv[]) { | |
29 | struct udev *udev = NULL; | |
30 | struct udev_device *device = NULL; | |
31 | _cleanup_free_ char *saved = NULL; | |
32 | int r; | |
33 | ||
34 | if (argc != 3) { | |
35 | log_error("This program requires two arguments."); | |
36 | return EXIT_FAILURE; | |
37 | } | |
38 | ||
39 | log_set_target(LOG_TARGET_AUTO); | |
40 | log_parse_environment(); | |
41 | log_open(); | |
42 | ||
43 | umask(0022); | |
44 | ||
45 | r = mkdir_p("/var/lib/backlight", 0755); | |
46 | if (r < 0) { | |
47 | log_error("Failed to create backlight directory: %s", strerror(-r)); | |
48 | goto finish; | |
49 | } | |
50 | ||
51 | udev = udev_new(); | |
52 | if (!udev) { | |
53 | r = log_oom(); | |
54 | goto finish; | |
55 | } | |
56 | ||
57 | device = udev_device_new_from_syspath(udev, argv[2]); | |
58 | if (!device) { | |
59 | r = log_oom(); | |
60 | goto finish; | |
61 | } | |
62 | ||
63 | if (!streq_ptr(udev_device_get_subsystem(device), "backlight")) { | |
64 | log_error("Not a backlight device: %s", argv[2]); | |
65 | r = -ENODEV; | |
66 | goto finish; | |
67 | } | |
68 | ||
69 | saved = strappend("/var/lib/backlight/", udev_device_get_sysname(device)); | |
70 | if (!saved) { | |
71 | r = log_oom(); | |
72 | goto finish; | |
73 | } | |
74 | ||
75 | if (streq(argv[1], "load")) { | |
76 | _cleanup_free_ char *value = NULL; | |
77 | ||
78 | r = read_one_line_file(saved, &value); | |
79 | if (r < 0) { | |
80 | ||
81 | if (r == -ENOENT) { | |
82 | r = 0; | |
83 | goto finish; | |
84 | } | |
85 | ||
86 | log_error("Failed to read %s: %s", saved, strerror(-r)); | |
87 | goto finish; | |
88 | } | |
89 | ||
90 | r = udev_device_set_sysattr_value(device, "brightness", value); | |
91 | if (r < 0) { | |
92 | log_error("Failed to write system attribute: %s", strerror(-r)); | |
93 | goto finish; | |
94 | } | |
95 | ||
96 | } else if (streq(argv[1], "save")) { | |
97 | const char *value; | |
98 | ||
99 | value = udev_device_get_sysattr_value(device, "brightness"); | |
100 | if (!value) { | |
101 | log_error("Failed to read system attribute: %s", strerror(-r)); | |
102 | goto finish; | |
103 | } | |
104 | ||
105 | r = write_string_file(saved, value); | |
106 | if (r < 0) { | |
107 | log_error("Failed to write %s: %s", saved, strerror(-r)); | |
108 | goto finish; | |
109 | } | |
110 | ||
111 | } else { | |
112 | log_error("Unknown verb %s.", argv[1]); | |
113 | r = -EINVAL; | |
114 | goto finish; | |
115 | } | |
116 | ||
117 | finish: | |
118 | if (device) | |
119 | udev_device_unref(device); | |
120 | ||
121 | if (udev) | |
122 | udev_unref(udev); | |
123 | ||
124 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; | |
125 | ||
126 | } |