chiark / gitweb /
c45b2d0b6b3e007df187343d1c5665f3b4ef76fe
[elogind.git] / src / backlight / backlight.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;
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/systemd/backlight", 0755);
46         if (r < 0) {
47                 log_error("Failed to create backlight directory: %s", strerror(-r));
48                 return EXIT_FAILURE;
49         }
50
51         udev = udev_new();
52         if (!udev) {
53                 log_oom();
54                 return EXIT_FAILURE;
55         }
56
57         errno = 0;
58         device = udev_device_new_from_subsystem_sysname(udev, "backlight", argv[2]);
59         if (!device)
60                 device = udev_device_new_from_subsystem_sysname(udev, "leds", argv[2]);
61         if (!device) {
62                 if (errno != 0)
63                         log_error("Failed to get backlight device '%s': %m", argv[2]);
64                 else
65                         r = log_oom();
66
67                 return EXIT_FAILURE;
68         }
69
70         if (!streq_ptr(udev_device_get_subsystem(device), "backlight") &&
71             !streq_ptr(udev_device_get_subsystem(device), "leds")) {
72                 log_error("Not a backlight device: %s", argv[2]);
73                 return EXIT_FAILURE;
74         }
75
76         saved = strappend("/var/lib/systemd/backlight/", udev_device_get_sysname(device));
77         if (!saved) {
78                 log_oom();
79                 return EXIT_FAILURE;
80         }
81
82         if (streq(argv[1], "load")) {
83                 _cleanup_free_ char *value = NULL;
84
85                 r = read_one_line_file(saved, &value);
86                 if (r < 0) {
87
88                         if (r == -ENOENT)
89                                 return EXIT_SUCCESS;
90
91                         log_error("Failed to read %s: %s", saved, strerror(-r));
92                         return EXIT_FAILURE;
93                 }
94
95                 r = udev_device_set_sysattr_value(device, "brightness", value);
96                 if (r < 0) {
97                         log_error("Failed to write system attribute: %s", strerror(-r));
98                         return EXIT_FAILURE;
99                 }
100
101         } else if (streq(argv[1], "save")) {
102                 const char *value;
103
104                 value = udev_device_get_sysattr_value(device, "brightness");
105                 if (!value) {
106                         log_error("Failed to read system attribute: %s", strerror(-r));
107                         return EXIT_FAILURE;
108                 }
109
110                 r = write_string_file(saved, value);
111                 if (r < 0) {
112                         log_error("Failed to write %s: %s", saved, strerror(-r));
113                         return EXIT_FAILURE;
114                 }
115
116         } else {
117                 log_error("Unknown verb %s.", argv[1]);
118                 return EXIT_FAILURE;
119         }
120
121         return EXIT_SUCCESS;
122 }