summaryrefslogtreecommitdiff
path: root/src/hostname
diff options
context:
space:
mode:
authorSonali Srivastava <srivastava.sonali1@gmail.com>2022-04-19 00:17:50 +0530
committerLuca Boccassi <luca.boccassi@gmail.com>2022-04-19 13:54:16 +0200
commitc52950c292be639aec38f9a5db1681005203c2cd (patch)
treeecb663220c22609ae60bd874af366d904b759236 /src/hostname
parente4b5226d63350cfe2eacacfa343d0323f05e3ebd (diff)
downloadsystemd-c52950c292be639aec38f9a5db1681005203c2cd.tar.gz
hostnamed: display firmware version
Diffstat (limited to 'src/hostname')
-rw-r--r--src/hostname/hostnamectl.c38
-rw-r--r--src/hostname/hostnamed.c45
2 files changed, 62 insertions, 21 deletions
diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c
index d94194d212..a96241c42e 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 b855b41112..ce2230c93a 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",