summaryrefslogtreecommitdiff
path: root/src/hostname/hostnamed.c
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/hostnamed.c
parente4b5226d63350cfe2eacacfa343d0323f05e3ebd (diff)
downloadsystemd-c52950c292be639aec38f9a5db1681005203c2cd.tar.gz
hostnamed: display firmware version
Diffstat (limited to 'src/hostname/hostnamed.c')
-rw-r--r--src/hostname/hostnamed.c45
1 files changed, 38 insertions, 7 deletions
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",