Merge pull request #17020 from yuwata/backlight
backlight: validate sysattr value
This commit is contained in:
commit
f7c87baeca
|
@ -261,17 +261,13 @@ static int get_max_brightness(sd_device *device, unsigned *ret) {
|
||||||
* max_brightness in case of 'backlight' subsystem. This avoids preserving
|
* max_brightness in case of 'backlight' subsystem. This avoids preserving
|
||||||
* an unreadably dim screen, which would otherwise force the user to
|
* an unreadably dim screen, which would otherwise force the user to
|
||||||
* disable state restoration. */
|
* disable state restoration. */
|
||||||
static int clamp_brightness(sd_device *device, char **value, unsigned max_brightness) {
|
static int clamp_brightness(sd_device *device, bool saved, unsigned max_brightness, unsigned *brightness) {
|
||||||
unsigned brightness, new_brightness, min_brightness;
|
unsigned new_brightness, min_brightness;
|
||||||
const char *subsystem;
|
const char *subsystem;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(value);
|
assert(device);
|
||||||
assert(*value);
|
assert(brightness);
|
||||||
|
|
||||||
r = safe_atou(*value, &brightness);
|
|
||||||
if (r < 0)
|
|
||||||
return log_device_warning_errno(device, r, "Failed to parse brightness \"%s\": %m", *value);
|
|
||||||
|
|
||||||
r = sd_device_get_subsystem(device, &subsystem);
|
r = sd_device_get_subsystem(device, &subsystem);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
|
@ -282,22 +278,16 @@ static int clamp_brightness(sd_device *device, char **value, unsigned max_bright
|
||||||
else
|
else
|
||||||
min_brightness = 0;
|
min_brightness = 0;
|
||||||
|
|
||||||
new_brightness = CLAMP(brightness, min_brightness, max_brightness);
|
new_brightness = CLAMP(*brightness, min_brightness, max_brightness);
|
||||||
if (new_brightness != brightness) {
|
if (new_brightness != *brightness)
|
||||||
char *new_value;
|
log_device_info(device, "%s brightness %u is %s to %u.",
|
||||||
|
saved ? "Saved" : "Current",
|
||||||
r = asprintf(&new_value, "%u", new_brightness);
|
*brightness,
|
||||||
if (r < 0)
|
new_brightness > *brightness ?
|
||||||
return log_oom();
|
|
||||||
|
|
||||||
log_device_info(device, "Saved brightness %s %s to %s.", *value,
|
|
||||||
new_brightness > brightness ?
|
|
||||||
"too low; increasing" : "too high; decreasing",
|
"too low; increasing" : "too high; decreasing",
|
||||||
new_value);
|
new_brightness);
|
||||||
|
|
||||||
free_and_replace(*value, new_value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
*brightness = new_brightness;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -309,7 +299,8 @@ static bool shall_clamp(sd_device *d) {
|
||||||
|
|
||||||
r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &s);
|
r = sd_device_get_property_value(d, "ID_BACKLIGHT_CLAMP", &s);
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
log_device_debug_errno(d, r, "Failed to get ID_BACKLIGHT_CLAMP property, ignoring: %m");
|
if (r != -ENOENT)
|
||||||
|
log_device_debug_errno(d, r, "Failed to get ID_BACKLIGHT_CLAMP property, ignoring: %m");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -322,31 +313,60 @@ static bool shall_clamp(sd_device *d) {
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int read_brightness(sd_device *device, const char **ret) {
|
static int read_brightness(sd_device *device, unsigned max_brightness, unsigned *ret_brightness) {
|
||||||
const char *subsystem;
|
const char *subsystem, *value;
|
||||||
|
unsigned brightness;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(device);
|
assert(device);
|
||||||
assert(ret);
|
assert(ret_brightness);
|
||||||
|
|
||||||
r = sd_device_get_subsystem(device, &subsystem);
|
r = sd_device_get_subsystem(device, &subsystem);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_device_debug_errno(device, r, "Failed to get subsystem: %m");
|
return log_device_debug_errno(device, r, "Failed to get subsystem: %m");
|
||||||
|
|
||||||
if (streq(subsystem, "backlight")) {
|
if (streq(subsystem, "backlight")) {
|
||||||
r = sd_device_get_sysattr_value(device, "actual_brightness", ret);
|
r = sd_device_get_sysattr_value(device, "actual_brightness", &value);
|
||||||
if (r >= 0)
|
if (r == -ENOENT) {
|
||||||
return 0;
|
log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute, "
|
||||||
if (r != -ENOENT)
|
"fall back to use 'brightness' attribute: %m");
|
||||||
|
goto use_brightness;
|
||||||
|
}
|
||||||
|
if (r < 0)
|
||||||
return log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute: %m");
|
return log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute: %m");
|
||||||
|
|
||||||
log_device_debug_errno(device, r, "Failed to read 'actual_brightness' attribute, fall back to use 'brightness' attribute: %m");
|
r = safe_atou(value, &brightness);
|
||||||
|
if (r < 0) {
|
||||||
|
log_device_debug_errno(device, r, "Failed to parse 'actual_brightness' attribute, "
|
||||||
|
"fall back to use 'brightness' attribute: %s", value);
|
||||||
|
goto use_brightness;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (brightness > max_brightness) {
|
||||||
|
log_device_debug(device, "actual_brightness=%u is larger than max_brightness=%u, "
|
||||||
|
"fall back to use 'brightness' attribute", brightness, max_brightness);
|
||||||
|
goto use_brightness;
|
||||||
|
}
|
||||||
|
|
||||||
|
*ret_brightness = brightness;
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = sd_device_get_sysattr_value(device, "brightness", ret);
|
use_brightness:
|
||||||
|
r = sd_device_get_sysattr_value(device, "brightness", &value);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_device_debug_errno(device, r, "Failed to read 'brightness' attribute: %m");
|
return log_device_debug_errno(device, r, "Failed to read 'brightness' attribute: %m");
|
||||||
|
|
||||||
|
r = safe_atou(value, &brightness);
|
||||||
|
if (r < 0)
|
||||||
|
return log_device_debug_errno(device, r, "Failed to parse 'brightness' attribute: %s", value);
|
||||||
|
|
||||||
|
if (brightness > max_brightness)
|
||||||
|
return log_device_debug_errno(device, SYNTHETIC_ERRNO(EINVAL),
|
||||||
|
"brightness=%u is larger than max_brightness=%u",
|
||||||
|
brightness, max_brightness);
|
||||||
|
|
||||||
|
*ret_brightness = brightness;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,7 +374,7 @@ static int run(int argc, char *argv[]) {
|
||||||
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
|
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
|
||||||
_cleanup_free_ char *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
|
_cleanup_free_ char *escaped_ss = NULL, *escaped_sysname = NULL, *escaped_path_id = NULL;
|
||||||
const char *sysname, *path_id, *ss, *saved;
|
const char *sysname, *path_id, *ss, *saved;
|
||||||
unsigned max_brightness;
|
unsigned max_brightness, brightness;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
log_setup_service();
|
log_setup_service();
|
||||||
|
@ -431,44 +451,50 @@ static int run(int argc, char *argv[]) {
|
||||||
clamp = shall_clamp(device);
|
clamp = shall_clamp(device);
|
||||||
|
|
||||||
r = read_one_line_file(saved, &value);
|
r = read_one_line_file(saved, &value);
|
||||||
if (IN_SET(r, -ENOENT, 0)) {
|
if (r < 0 && r != -ENOENT)
|
||||||
const char *curval;
|
return log_error_errno(r, "Failed to read %s: %m", saved);
|
||||||
|
if (r > 0) {
|
||||||
|
r = safe_atou(value, &brightness);
|
||||||
|
if (r < 0) {
|
||||||
|
log_error_errno(r, "Failed to parse saved brightness '%s', removing %s.",
|
||||||
|
value, saved);
|
||||||
|
(void) unlink(saved);
|
||||||
|
} else {
|
||||||
|
if (clamp)
|
||||||
|
(void) clamp_brightness(device, true, max_brightness, &brightness);
|
||||||
|
|
||||||
/* Fallback to clamping current brightness or exit early if
|
/* Do not fall back to read current brightness below. */
|
||||||
* clamping is not supported/enabled. */
|
r = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (r <= 0) {
|
||||||
|
/* Fallback to clamping current brightness or exit early if clamping is not
|
||||||
|
* supported/enabled. */
|
||||||
if (!clamp)
|
if (!clamp)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
r = read_brightness(device, &curval);
|
r = read_brightness(device, max_brightness, &brightness);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_device_error_errno(device, r, "Failed to read current brightness: %m");
|
return log_device_error_errno(device, r, "Failed to read current brightness: %m");
|
||||||
|
|
||||||
value = strdup(curval);
|
(void) clamp_brightness(device, false, max_brightness, &brightness);
|
||||||
if (!value)
|
}
|
||||||
return log_oom();
|
|
||||||
} else if (r < 0)
|
|
||||||
return log_error_errno(r, "Failed to read %s: %m", saved);
|
|
||||||
|
|
||||||
if (clamp)
|
r = sd_device_set_sysattr_valuef(device, "brightness", "%u", brightness);
|
||||||
(void) clamp_brightness(device, &value, max_brightness);
|
|
||||||
|
|
||||||
r = sd_device_set_sysattr_value(device, "brightness", value);
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_device_error_errno(device, r, "Failed to write system 'brightness' attribute: %m");
|
return log_device_error_errno(device, r, "Failed to write system 'brightness' attribute: %m");
|
||||||
|
|
||||||
} else if (streq(argv[1], "save")) {
|
} else if (streq(argv[1], "save")) {
|
||||||
const char *value;
|
|
||||||
|
|
||||||
if (validate_device(device) == 0) {
|
if (validate_device(device) == 0) {
|
||||||
(void) unlink(saved);
|
(void) unlink(saved);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
r = read_brightness(device, &value);
|
r = read_brightness(device, max_brightness, &brightness);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_device_error_errno(device, r, "Failed to read current brightness: %m");
|
return log_device_error_errno(device, r, "Failed to read current brightness: %m");
|
||||||
|
|
||||||
r = write_string_file(saved, value, WRITE_STRING_FILE_CREATE);
|
r = write_string_filef(saved, WRITE_STRING_FILE_CREATE, "%u", brightness);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return log_device_error_errno(device, r, "Failed to write %s: %m", saved);
|
return log_device_error_errno(device, r, "Failed to write %s: %m", saved);
|
||||||
|
|
||||||
|
|
|
@ -732,4 +732,5 @@ global:
|
||||||
sd_device_get_current_tag_first;
|
sd_device_get_current_tag_first;
|
||||||
sd_device_get_current_tag_next;
|
sd_device_get_current_tag_next;
|
||||||
sd_device_has_current_tag;
|
sd_device_has_current_tag;
|
||||||
|
sd_device_set_sysattr_valuef;
|
||||||
} LIBSYSTEMD_246;
|
} LIBSYSTEMD_246;
|
||||||
|
|
|
@ -1979,3 +1979,26 @@ _public_ int sd_device_set_sysattr_value(sd_device *device, const char *sysattr,
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_public_ int sd_device_set_sysattr_valuef(sd_device *device, const char *sysattr, const char *format, ...) {
|
||||||
|
_cleanup_free_ char *value = NULL;
|
||||||
|
va_list ap;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
assert_return(device, -EINVAL);
|
||||||
|
assert_return(sysattr, -EINVAL);
|
||||||
|
|
||||||
|
if (!format) {
|
||||||
|
device_remove_sysattr_value(device, sysattr);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
va_start(ap, format);
|
||||||
|
r = vasprintf(&value, format, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
if (r < 0)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
return sd_device_set_sysattr_value(device, sysattr, value);
|
||||||
|
}
|
||||||
|
|
|
@ -79,6 +79,7 @@ int sd_device_get_property_value(sd_device *device, const char *key, const char
|
||||||
int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **_value);
|
int sd_device_get_sysattr_value(sd_device *device, const char *sysattr, const char **_value);
|
||||||
|
|
||||||
int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, const char *value);
|
int sd_device_set_sysattr_value(sd_device *device, const char *sysattr, const char *value);
|
||||||
|
int sd_device_set_sysattr_valuef(sd_device *device, const char *sysattr, const char *format, ...) _sd_printf_(3, 4);
|
||||||
|
|
||||||
/* device enumerator */
|
/* device enumerator */
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue