From c52950c292be639aec38f9a5db1681005203c2cd Mon Sep 17 00:00:00 2001 From: Sonali Srivastava Date: Tue, 19 Apr 2022 00:17:50 +0530 Subject: [PATCH] hostnamed: display firmware version --- man/org.freedesktop.hostname1.xml | 6 +++++ src/hostname/hostnamectl.c | 38 ++++++++++++++++---------- src/hostname/hostnamed.c | 45 ++++++++++++++++++++++++++----- 3 files changed, 68 insertions(+), 21 deletions(-) diff --git a/man/org.freedesktop.hostname1.xml b/man/org.freedesktop.hostname1.xml index 94004002ecf..ea9bd263382 100644 --- a/man/org.freedesktop.hostname1.xml +++ b/man/org.freedesktop.hostname1.xml @@ -87,6 +87,8 @@ node /org/freedesktop/hostname1 { readonly s HardwareVendor = '...'; @org.freedesktop.DBus.Property.EmitsChangedSignal("const") readonly s HardwareModel = '...'; + @org.freedesktop.DBus.Property.EmitsChangedSignal("const") + readonly s FirmwareVersion = '...'; }; interface org.freedesktop.DBus.Peer { ... }; interface org.freedesktop.DBus.Introspectable { ... }; @@ -100,6 +102,8 @@ node /org/freedesktop/hostname1 { + + @@ -160,6 +164,8 @@ node /org/freedesktop/hostname1 { + + Whenever the hostname or other metadata is changed via the daemon, diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index d94194d2126..a96241c42e1 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -51,6 +51,7 @@ typedef struct StatusInfo { const char *home_url; const char *hardware_vendor; const char *hardware_model; + const char *firmware_version; } StatusInfo; static const char* chassis_string_to_glyph(const char *chassis) { @@ -235,6 +236,14 @@ static int print_status_info(StatusInfo *i) { return table_log_add_error(r); } + if (!isempty(i->firmware_version)) { + r = table_add_many(table, + TABLE_STRING, "Firmware Version:", + TABLE_STRING, i->firmware_version); + if (r < 0) + return table_log_add_error(r); + } + r = table_print(table, NULL); if (r < 0) return table_log_print_error(r); @@ -285,20 +294,21 @@ static int show_all_names(sd_bus *bus) { StatusInfo info = {}; static const struct bus_properties_map hostname_map[] = { - { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) }, - { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) }, - { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) }, - { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) }, - { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) }, - { "Deployment", "s", NULL, offsetof(StatusInfo, deployment) }, - { "Location", "s", NULL, offsetof(StatusInfo, location) }, - { "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) }, - { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) }, - { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) }, - { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) }, - { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) }, - { "HardwareVendor", "s", NULL, offsetof(StatusInfo, hardware_vendor) }, - { "HardwareModel", "s", NULL, offsetof(StatusInfo, hardware_model) }, + { "Hostname", "s", NULL, offsetof(StatusInfo, hostname) }, + { "StaticHostname", "s", NULL, offsetof(StatusInfo, static_hostname) }, + { "PrettyHostname", "s", NULL, offsetof(StatusInfo, pretty_hostname) }, + { "IconName", "s", NULL, offsetof(StatusInfo, icon_name) }, + { "Chassis", "s", NULL, offsetof(StatusInfo, chassis) }, + { "Deployment", "s", NULL, offsetof(StatusInfo, deployment) }, + { "Location", "s", NULL, offsetof(StatusInfo, location) }, + { "KernelName", "s", NULL, offsetof(StatusInfo, kernel_name) }, + { "KernelRelease", "s", NULL, offsetof(StatusInfo, kernel_release) }, + { "OperatingSystemPrettyName", "s", NULL, offsetof(StatusInfo, os_pretty_name) }, + { "OperatingSystemCPEName", "s", NULL, offsetof(StatusInfo, os_cpe_name) }, + { "HomeURL", "s", NULL, offsetof(StatusInfo, home_url) }, + { "HardwareVendor", "s", NULL, offsetof(StatusInfo, hardware_vendor) }, + { "HardwareModel", "s", NULL, offsetof(StatusInfo, hardware_model) }, + { "FirmwareVersion", "s", NULL, offsetof(StatusInfo, firmware_version) }, {} }; diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index b855b411121..ce2230c93a3 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -208,21 +208,19 @@ static int get_hardware_model(char **ret) { return get_dmi_data("ID_MODEL_FROM_DATABASE", "ID_MODEL", ret); } -static int get_hardware_serial(char **ret) { +static int get_hardware_firmware_data(const char *sysattr, char **ret) { _cleanup_(sd_device_unrefp) sd_device *device = NULL; _cleanup_free_ char *b = NULL; const char *s = NULL; int r; + assert(sysattr); + r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id"); if (r < 0) return log_debug_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m"); - (void) sd_device_get_sysattr_value(device, "product_serial", &s); - if (isempty(s)) - /* Fallback to board serial */ - (void) sd_device_get_sysattr_value(device, "board_serial", &s); - + (void) sd_device_get_sysattr_value(device, sysattr, &s); if (!isempty(s)) { b = strdup(s); if (!b) @@ -235,6 +233,20 @@ static int get_hardware_serial(char **ret) { return !isempty(s); } +static int get_hardware_serial(char **ret) { + int r; + + r = get_hardware_firmware_data("product_serial", ret); + if (r <= 0) + return get_hardware_firmware_data("board_serial", ret); + + return r; +} + +static int get_firmware_version(char **ret) { + return get_hardware_firmware_data("bios_version", ret); +} + static const char* valid_chassis(const char *chassis) { assert(chassis); @@ -601,6 +613,22 @@ static int property_get_hardware_model( return property_get_hardware_property(reply, userdata, PROP_HARDWARE_MODEL, get_hardware_model); } +static int property_get_firmware_version( + sd_bus *bus, + const char *path, + const char *interface, + const char *property, + sd_bus_message *reply, + void *userdata, + sd_bus_error *error) { + + _cleanup_free_ char *firmware_version = NULL; + + (void) get_firmware_version(&firmware_version); + + return sd_bus_message_append(reply, "s", firmware_version); +} + static int property_get_hostname( sd_bus *bus, const char *path, @@ -1128,7 +1156,7 @@ static int method_get_hardware_serial(sd_bus_message *m, void *userdata, sd_bus_ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *error) { _cleanup_free_ char *hn = NULL, *dhn = NULL, *in = NULL, *text = NULL, - *chassis = NULL, *vendor = NULL, *model = NULL, *serial = NULL; + *chassis = NULL, *vendor = NULL, *model = NULL, *serial = NULL, *firmware_version = NULL; _cleanup_(sd_bus_message_unrefp) sd_bus_message *reply = NULL; _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; sd_id128_t product_uuid = SD_ID128_NULL; @@ -1192,6 +1220,7 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro (void) id128_get_product(&product_uuid); (void) get_hardware_serial(&serial); } + (void) get_firmware_version(&firmware_version); r = json_build(&v, JSON_BUILD_OBJECT( JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn)), @@ -1212,6 +1241,7 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro JSON_BUILD_PAIR("HardwareVendor", JSON_BUILD_STRING(vendor ?: c->data[PROP_HARDWARE_VENDOR])), JSON_BUILD_PAIR("HardwareModel", JSON_BUILD_STRING(model ?: c->data[PROP_HARDWARE_MODEL])), JSON_BUILD_PAIR("HardwareSerial", JSON_BUILD_STRING(serial)), + JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version)), JSON_BUILD_PAIR_CONDITION(!sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_ID128(product_uuid)), JSON_BUILD_PAIR_CONDITION(sd_id128_is_null(product_uuid), "ProductUUID", JSON_BUILD_NULL))); @@ -1252,6 +1282,7 @@ static const sd_bus_vtable hostname_vtable[] = { SD_BUS_PROPERTY("HomeURL", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_HOME_URL, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("HardwareVendor", "s", property_get_hardware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_PROPERTY("HardwareModel", "s", property_get_hardware_model, 0, SD_BUS_VTABLE_PROPERTY_CONST), + SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version, 0, SD_BUS_VTABLE_PROPERTY_CONST), SD_BUS_METHOD_WITH_NAMES("SetHostname", "sb",