-/*-*- 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;
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
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;
}
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;
}
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;
}
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();
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();
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))
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;
}
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;
}