]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/backlight/backlight.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / backlight / backlight.c
index ab05a7d1a7ae3501bcb5259387e8907efdbddfc3..8fedf8ebbc400b47ff0546ed03164a724635e65d 100644 (file)
@@ -1,5 +1,4 @@
-/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
-
+/* SPDX-License-Identifier: LGPL-2.1+ */
 /***
   This file is part of systemd.
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include "util.h"
-#include "mkdir.h"
-#include "fileio.h"
 #include "libudev.h"
-#include "udev-util.h"
+
+#include "alloc-util.h"
 #include "def.h"
+#include "escape.h"
+#include "fileio.h"
+#include "mkdir.h"
+#include "parse-util.h"
+#include "proc-cmdline.h"
+#include "string-util.h"
+#include "udev-util.h"
+#include "util.h"
 
 static struct udev_device *find_pci_or_platform_parent(struct udev_device *device) {
         struct udev_device *parent;
@@ -163,7 +168,7 @@ static bool validate_device(struct udev *udev, struct udev_device *device) {
                         continue;
 
                 v = udev_device_get_sysattr_value(other, "type");
-                if (!streq_ptr(v, "platform") && !streq_ptr(v, "firmware"))
+                if (!STRPTR_IN_SET(v, "platform", "firmware"))
                         continue;
 
                 /* OK, so there's another backlight device, and it's a
@@ -211,7 +216,7 @@ static unsigned get_max_brightness(struct udev_device *device) {
 
         r = safe_atou(max_brightness_str, &max_brightness);
         if (r < 0) {
-                log_warning_errno(-r, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str);
+                log_warning_errno(r, "Failed to parse 'max_brightness' \"%s\": %m", max_brightness_str);
                 return 0;
         }
 
@@ -235,7 +240,7 @@ static void clamp_brightness(struct udev_device *device, char **value, unsigned
 
         r = safe_atou(*value, &brightness);
         if (r < 0) {
-                log_warning_errno(-r, "Failed to parse brightness \"%s\": %m", *value);
+                log_warning_errno(r, "Failed to parse brightness \"%s\": %m", *value);
                 return;
         }
 
@@ -285,8 +290,7 @@ int main(int argc, char *argv[]) {
 
         r = mkdir_p("/var/lib/systemd/backlight", 0755);
         if (r < 0) {
-                log_error("Failed to create backlight directory /var/lib/systemd/backlight: %s",
-                          strerror(-r));
+                log_error_errno(r, "Failed to create backlight directory /var/lib/systemd/backlight: %m");
                 return EXIT_FAILURE;
         }
 
@@ -318,8 +322,8 @@ int main(int argc, char *argv[]) {
         errno = 0;
         device = udev_device_new_from_subsystem_sysname(udev, ss, sysname);
         if (!device) {
-                if (errno != 0)
-                        log_error("Failed to get backlight or LED device '%s:%s': %m", ss, sysname);
+                if (errno > 0)
+                        log_error_errno(errno, "Failed to get backlight or LED device '%s:%s': %m", ss, sysname);
                 else
                         log_oom();
 
@@ -354,9 +358,9 @@ int main(int argc, char *argv[]) {
                         return EXIT_FAILURE;
                 }
 
-                saved = strjoin("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname, NULL);
+                saved = strjoin("/var/lib/systemd/backlight/", escaped_path_id, ":", escaped_ss, ":", escaped_sysname);
         } else
-                saved = strjoin("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname, NULL);
+                saved = strjoin("/var/lib/systemd/backlight/", escaped_ss, ":", escaped_sysname);
 
         if (!saved) {
                 log_oom();
@@ -374,8 +378,9 @@ int main(int argc, char *argv[]) {
 
         if (streq(argv[1], "load")) {
                 _cleanup_free_ char *value = NULL;
+                const char *clamp;
 
-                if (!shall_restore_state())
+                if (shall_restore_state() == 0)
                         return EXIT_SUCCESS;
 
                 if (!validate_device(udev, device))
@@ -387,16 +392,17 @@ int main(int argc, char *argv[]) {
                         if (r == -ENOENT)
                                 return EXIT_SUCCESS;
 
-                        log_error_errno(-r, "Failed to read %s: %m", saved);
+                        log_error_errno(r, "Failed to read %s: %m", saved);
                         return EXIT_FAILURE;
                 }
 
-                clamp_brightness(device, &value, max_brightness);
+                clamp = udev_device_get_property_value(device, "ID_BACKLIGHT_CLAMP");
+                if (!clamp || parse_boolean(clamp) != 0) /* default to clamping */
+                        clamp_brightness(device, &value, max_brightness);
 
                 r = udev_device_set_sysattr_value(device, "brightness", value);
                 if (r < 0) {
-                        log_error("Failed to write system 'brightness' attribute: %s",
-                                  strerror(-r));
+                        log_error_errno(r, "Failed to write system 'brightness' attribute: %m");
                         return EXIT_FAILURE;
                 }
 
@@ -414,9 +420,9 @@ int main(int argc, char *argv[]) {
                         return EXIT_FAILURE;
                 }
 
-                r = write_string_file(saved, value);
+                r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
                 if (r < 0) {
-                        log_error_errno(-r, "Failed to write %s: %m", saved);
+                        log_error_errno(r, "Failed to write %s: %m", saved);
                         return EXIT_FAILURE;
                 }