From 036b72b593fade3afff55d507fcebad16e9d06a8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 14 Nov 2022 02:49:19 +0900 Subject: [PATCH] sleep: introduce siphash24_compress_id128() Also, rename get_battery_identifier() to siphash24_compress_device_sysattr(). This also makes any errors in sd_id128_get_machine() or id128_get_product() ignored. For the machine ID, the failure should not be significant unless the file stored in the discharge level is reused by another system, which is quite unusual. For the product ID, if the firmware provides useless ID (all zero or all 0xFF), then loading/storing the discharge rate becomes completely broken, that should be avoided. Note, now sysattrs are used instead of properties in uevent files, but both provide the same information, hence no functionality should be changed. (cherry picked from commit a7795a4ecfe5c51fc837d0cb2f27d892c83df7a4) --- src/shared/sleep-config.c | 62 ++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 34 deletions(-) diff --git a/src/shared/sleep-config.c b/src/shared/sleep-config.c index ffac67e977..3a8822f89b 100644 --- a/src/shared/sleep-config.c +++ b/src/shared/sleep-config.c @@ -254,61 +254,55 @@ int fetch_batteries_capacity_by_name(Hashmap **ret) { return 0; } -/* Read file path and return hash of value in that file */ -static int get_battery_identifier(sd_device *dev, const char *property, struct siphash *state) { +static int siphash24_compress_device_sysattr(sd_device *dev, const char *attr, struct siphash *state) { const char *x; int r; assert(dev); - assert(property); + assert(attr); assert(state); - r = sd_device_get_property_value(dev, property, &x); - if (r == -ENOENT) - log_device_debug_errno(dev, r, "Battery device property %s is unavailable, ignoring: %m", property); - else if (r < 0) - return log_device_debug_errno(dev, r, "Failed to get battery device property %s: %m", property); - else if (isempty(x)) - log_device_debug(dev, "Battery device property '%s' is empty.", property); - else - siphash24_compress_string(x, state); + r = sd_device_get_sysattr_value(dev, attr, &x); + if (r < 0) + return log_device_debug_errno(dev, r, "Failed to read '%s' attribute: %m", attr); + if (!isempty(x)) + siphash24_compress_string(x, state); + + return 0; +} + +static int siphash24_compress_id128(int (*getter)(sd_id128_t*), const char *name, struct siphash *state) { + sd_id128_t id; + int r; + + assert(getter); + assert(state); + + r = getter(&id); + if (r < 0) + return log_debug_errno(r, "Failed to get %s ID: %m", name); + + siphash24_compress(&id, sizeof(sd_id128_t), state); return 0; } /* Read system and battery identifier from specific location and generate hash of it */ static int get_system_battery_identifier_hash(sd_device *dev, uint64_t *ret) { struct siphash state; - sd_id128_t machine_id, product_id; - int r; assert(ret); assert(dev); siphash24_init(&state, BATTERY_DISCHARGE_RATE_HASH_KEY.bytes); - get_battery_identifier(dev, "POWER_SUPPLY_MANUFACTURER", &state); - get_battery_identifier(dev, "POWER_SUPPLY_MODEL_NAME", &state); - get_battery_identifier(dev, "POWER_SUPPLY_SERIAL_NUMBER", &state); - - r = sd_id128_get_machine(&machine_id); - if (r == -ENOENT) - log_debug_errno(r, "machine ID is unavailable: %m"); - else if (r < 0) - return log_debug_errno(r, "Failed to get machine ID: %m"); - else - siphash24_compress(&machine_id, sizeof(sd_id128_t), &state); - - r = id128_get_product(&product_id); - if (r == -ENOENT) - log_debug_errno(r, "product_id does not exist: %m"); - else if (r < 0) - return log_debug_errno(r, "Failed to get product ID: %m"); - else - siphash24_compress(&product_id, sizeof(sd_id128_t), &state); + (void) siphash24_compress_device_sysattr(dev, "manufacturer", &state); + (void) siphash24_compress_device_sysattr(dev, "model_name", &state); + (void) siphash24_compress_device_sysattr(dev, "serial_number", &state); + (void) siphash24_compress_id128(sd_id128_get_machine, "machine", &state); + (void) siphash24_compress_id128(id128_get_product, "product", &state); *ret = siphash24_finalize(&state); - return 0; }