]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Merge pull request #30295 from yuwata/hostnamectl-machine-id
authorLuca Boccassi <bluca@debian.org>
Wed, 6 Dec 2023 21:12:36 +0000 (21:12 +0000)
committerGitHub <noreply@github.com>
Wed, 6 Dec 2023 21:12:36 +0000 (21:12 +0000)
hostnamectl: read machine ID and boot ID through DBus

man/org.freedesktop.hostname1.xml
src/hostname/hostnamectl.c
src/hostname/hostnamed.c

index a079837a67eae611fbd1c44cee79f4b489e62701..0c5b0f2704a132bf40fe4ad8545d1ba61e139a1d 100644 (file)
@@ -95,6 +95,10 @@ node /org/freedesktop/hostname1 {
       readonly s FirmwareVendor = '...';
       @org.freedesktop.DBus.Property.EmitsChangedSignal("const")
       readonly t FirmwareDate = ...;
+      @org.freedesktop.DBus.Property.EmitsChangedSignal("const")
+      readonly ay MachineID = [...];
+      @org.freedesktop.DBus.Property.EmitsChangedSignal("const")
+      readonly ay BootID = [...];
   };
   interface org.freedesktop.DBus.Peer { ... };
   interface org.freedesktop.DBus.Introspectable { ... };
@@ -116,6 +120,10 @@ node /org/freedesktop/hostname1 {
 
     <!--property FirmwareDate is not documented!-->
 
+    <!--property MachineID is not documented!-->
+
+    <!--property BootID is not documented!-->
+
     <!--Autogenerated cross-references for systemd.directives, do not edit-->
 
     <variablelist class="dbus-interface" generated="True" extra-ref="org.freedesktop.hostname1"/>
@@ -184,6 +192,10 @@ node /org/freedesktop/hostname1 {
 
     <variablelist class="dbus-property" generated="True" extra-ref="FirmwareDate"/>
 
+    <variablelist class="dbus-property" generated="True" extra-ref="MachineID"/>
+
+    <variablelist class="dbus-property" generated="True" extra-ref="BootID"/>
+
     <!--End of Autogenerated section-->
 
     <para>Whenever the hostname or other metadata is changed via the daemon,
@@ -428,6 +440,8 @@ node /org/freedesktop/hostname1 {
       <para><varname>OperatingSystemSupportEnd</varname>,
       <varname>FirmwareVendor</varname>, and
       <varname>FirmwareDate</varname> were added in version 253.</para>
+      <para><varname>MachineID</varname>, and
+      <varname>BootID</varname> were added in version 256.</para>
     </refsect2>
   </refsect1>
 </refentry>
index b1cf19205bf299c71798ff1f8b882f196c5f2c4c..14fc16090959549552469a90a59a0349e818bed0 100644 (file)
@@ -56,6 +56,8 @@ typedef struct StatusInfo {
         const char *hardware_model;
         const char *firmware_version;
         usec_t firmware_date;
+        sd_id128_t machine_id;
+        sd_id128_t boot_id;
 } StatusInfo;
 
 static const char* chassis_string_to_glyph(const char *chassis) {
@@ -97,7 +99,6 @@ static const char *os_support_end_color(usec_t n, usec_t eol) {
 
 static int print_status_info(StatusInfo *i) {
         _cleanup_(table_unrefp) Table *table = NULL;
-        sd_id128_t mid = {}, bid = {};
         TableCell *cell;
         int r;
 
@@ -174,20 +175,18 @@ static int print_status_info(StatusInfo *i) {
                         return table_log_add_error(r);
         }
 
-        r = sd_id128_get_machine(&mid);
-        if (r >= 0) {
+        if (!sd_id128_is_null(i->machine_id)) {
                 r = table_add_many(table,
                                    TABLE_FIELD, "Machine ID",
-                                   TABLE_ID128, mid);
+                                   TABLE_ID128, i->machine_id);
                 if (r < 0)
                         return table_log_add_error(r);
         }
 
-        r = sd_id128_get_boot(&bid);
-        if (r >= 0) {
+        if (!sd_id128_is_null(i->boot_id)) {
                 r = table_add_many(table,
                                    TABLE_FIELD, "Boot ID",
-                                   TABLE_ID128, bid);
+                                   TABLE_ID128, i->boot_id);
                 if (r < 0)
                         return table_log_add_error(r);
         }
@@ -336,29 +335,29 @@ 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)      },
-                { "OperatingSystemSupportEnd", "t", NULL, offsetof(StatusInfo, os_support_end)   },
-                { "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) },
-                { "FirmwareDate",              "t", NULL, offsetof(StatusInfo, firmware_date)    },
+                { "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)      },
+                { "OperatingSystemSupportEnd", "t",  NULL,          offsetof(StatusInfo, os_support_end)   },
+                { "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) },
+                { "FirmwareDate",              "t",  NULL,          offsetof(StatusInfo, firmware_date)    },
+                { "MachineID",                 "ay", bus_map_id128, offsetof(StatusInfo, machine_id)       },
+                { "BootID",                    "ay", bus_map_id128, offsetof(StatusInfo, boot_id)          },
                 {}
-        };
-
-        static const struct bus_properties_map manager_map[] = {
-                { "Virtualization",            "s", NULL, offsetof(StatusInfo, virtualization)  },
-                { "Architecture",              "s", NULL, offsetof(StatusInfo, architecture)    },
+        }, manager_map[] = {
+                { "Virtualization",            "s",  NULL,          offsetof(StatusInfo, virtualization)   },
+                { "Architecture",              "s",  NULL,          offsetof(StatusInfo, architecture)     },
                 {}
         };
 
@@ -388,6 +387,14 @@ static int show_all_names(sd_bus *bus) {
         if (r < 0)
                 return log_error_errno(r, "Failed to query system properties: %s", bus_error_message(&error, r));
 
+        /* For older version of hostnamed. */
+        if (!arg_host) {
+                if (sd_id128_is_null(info.machine_id))
+                        (void) sd_id128_get_machine(&info.machine_id);
+                if (sd_id128_is_null(info.boot_id))
+                        (void) sd_id128_get_boot(&info.boot_id);
+        }
+
         return print_status_info(&info);
 }
 
index e1d53f239580b31a7e7bac90d292e4f6abbf55b2..fc7a97fb99b9e5a0c1bffbdceaa7045111ad589b 100644 (file)
@@ -995,6 +995,44 @@ static int property_get_uname_field(
         return sd_bus_message_append(reply, "s", (char*) &u + PTR_TO_SIZE(userdata));
 }
 
+static int property_get_machine_id(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        sd_id128_t id;
+        int r;
+
+        r = sd_id128_get_machine(&id);
+        if (r < 0)
+                return r;
+
+        return bus_property_get_id128(bus, path, interface, property, reply, &id, error);
+}
+
+static int property_get_boot_id(
+                sd_bus *bus,
+                const char *path,
+                const char *interface,
+                const char *property,
+                sd_bus_message *reply,
+                void *userdata,
+                sd_bus_error *error) {
+
+        sd_id128_t id;
+        int r;
+
+        r = sd_id128_get_boot(&id);
+        if (r < 0)
+                return r;
+
+        return bus_property_get_id128(bus, path, interface, property, reply, &id, error);
+}
+
 static int method_set_hostname(sd_bus_message *m, void *userdata, sd_bus_error *error) {
         Context *c = ASSERT_PTR(userdata);
         const char *name;
@@ -1302,7 +1340,7 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro
         usec_t firmware_date = USEC_INFINITY, eol = USEC_INFINITY;
         _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;
+        sd_id128_t machine_id, boot_id, product_uuid = SD_ID128_NULL;
         Context *c = ASSERT_PTR(userdata);
         bool privileged;
         struct utsname u;
@@ -1369,6 +1407,14 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro
         if (c->data[PROP_OS_SUPPORT_END])
                 (void) os_release_support_ended(c->data[PROP_OS_SUPPORT_END], /* quiet= */ false, &eol);
 
+        r = sd_id128_get_machine(&machine_id);
+        if (r < 0)
+                return log_error_errno(r, "Failed to get machine ID: %m");
+
+        r = sd_id128_get_boot(&boot_id);
+        if (r < 0)
+                return log_error_errno(r, "Failed to get boot ID: %m");
+
         r = json_build(&v, JSON_BUILD_OBJECT(
                                        JSON_BUILD_PAIR("Hostname", JSON_BUILD_STRING(hn)),
                                        JSON_BUILD_PAIR("StaticHostname", JSON_BUILD_STRING(c->data[PROP_STATIC_HOSTNAME])),
@@ -1392,6 +1438,8 @@ static int method_describe(sd_bus_message *m, void *userdata, sd_bus_error *erro
                                        JSON_BUILD_PAIR("FirmwareVersion", JSON_BUILD_STRING(firmware_version)),
                                        JSON_BUILD_PAIR("FirmwareVendor", JSON_BUILD_STRING(firmware_vendor)),
                                        JSON_BUILD_PAIR_FINITE_USEC("FirmwareDate", firmware_date),
+                                       JSON_BUILD_PAIR_ID128("MachineID", machine_id),
+                                       JSON_BUILD_PAIR_ID128("BootID", boot_id),
                                        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)));
 
@@ -1436,6 +1484,8 @@ static const sd_bus_vtable hostname_vtable[] = {
         SD_BUS_PROPERTY("FirmwareVersion", "s", property_get_firmware_version, 0, SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("FirmwareVendor", "s", property_get_firmware_vendor, 0, SD_BUS_VTABLE_PROPERTY_CONST),
         SD_BUS_PROPERTY("FirmwareDate", "t", property_get_firmware_date, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("MachineID", "ay", property_get_machine_id, 0, SD_BUS_VTABLE_PROPERTY_CONST),
+        SD_BUS_PROPERTY("BootID", "ay", property_get_boot_id, 0, SD_BUS_VTABLE_PROPERTY_CONST),
 
         SD_BUS_METHOD_WITH_ARGS("SetHostname",
                                 SD_BUS_ARGS("s", hostname, "b", interactive),