Browse Source

Merge pull request #15531 from felipeborges/add-device-model-field-to-hostnamed

hostnamed: Add "Model" field
master
Lennart Poettering 2 years ago committed by GitHub
parent
commit
9bd6ee8d5d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      hwdb.d/20-dmi-id.hwdb
  2. 1
      hwdb.d/meson.build
  3. 3
      rules.d/50-udev-default.rules.in
  4. 9
      src/hostname/hostnamectl.c
  5. 55
      src/hostname/hostnamed.c

6
hwdb.d/20-dmi-id.hwdb

@ -0,0 +1,6 @@
# This file is part of systemd
# Fix "Lenovo" capitalization in /sys/class/dmi/id/sys_vendor
dmi:bvnLENOVO*
ID_SYSFS_ATTRIBUTE_MODEL=product_version
ID_VENDOR_FROM_DATABASE=Lenovo

1
hwdb.d/meson.build

@ -4,6 +4,7 @@
# they are very long but quite repetitive and the parser is not very fast.
# So we don't "test" them.
hwdb_files_notest = files('''
20-dmi-id.hwdb
20-pci-vendor-model.hwdb
20-pci-classes.hwdb
20-usb-vendor-model.hwdb

3
rules.d/50-udev-default.rules.in

@ -89,4 +89,7 @@ KERNEL=="udmabuf", GROUP="kvm"
SUBSYSTEM=="ptp", ATTR{clock_name}=="KVM virtual PTP", SYMLINK += "ptp_kvm"
SUBSYSTEM=="dmi", ENV{ID_SYSFS_ATTRIBUTE_MODEL}=="", ENV{ID_VENDOR}="$attr{sys_vendor}", ENV{ID_MODEL}="$attr{product_name}"
SUBSYSTEM=="dmi", ENV{ID_SYSFS_ATTRIBUTE_MODEL}=="product_version", ENV{ID_VENDOR}="$attr{sys_vendor}", ENV{ID_MODEL}="$attr{product_version}"
LABEL="default_end"

9
src/hostname/hostnamectl.c

@ -43,6 +43,8 @@ typedef struct StatusInfo {
const char *virtualization;
const char *architecture;
const char *home_url;
const char *hardware_vendor;
const char *hardware_model;
} StatusInfo;
static void print_status_info(StatusInfo *i) {
@ -107,6 +109,11 @@ static void print_status_info(StatusInfo *i) {
if (!isempty(i->architecture))
printf(" Architecture: %s\n", i->architecture);
if (!isempty(i->hardware_vendor))
printf(" Hardware Vendor: %s\n", i->hardware_vendor);
if (!isempty(i->hardware_model))
printf(" Hardware Model: %s\n", i->hardware_model);
}
static int show_one_name(sd_bus *bus, const char* attr) {
@ -150,6 +157,8 @@ static int show_all_names(sd_bus *bus, sd_bus_error *error) {
{ "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) },
{}
};

55
src/hostname/hostnamed.c

@ -20,6 +20,7 @@
#include "hostname-setup.h"
#include "hostname-util.h"
#include "id128-util.h"
#include "libudev.h"
#include "main-func.h"
#include "missing_capability.h"
#include "nscd-flush.h"
@ -27,6 +28,7 @@
#include "os-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "sd-device.h"
#include "selinux-util.h"
#include "service-util.h"
#include "signal-util.h"
@ -50,6 +52,9 @@ enum {
PROP_DEPLOYMENT,
PROP_LOCATION,
PROP_HARDWARE_VENDOR,
PROP_HARDWARE_MODEL,
/* Read from /etc/os-release (or /usr/lib/os-release) */
PROP_OS_PRETTY_NAME,
PROP_OS_CPE_NAME,
@ -426,6 +431,54 @@ static int context_write_data_machine_info(Context *c) {
return write_env_file_label("/etc/machine-info", l);
}
static int property_get_hardware_vendor(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
const char *hardware_vendor = NULL;
int r;
r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
if (r < 0) {
log_warning_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
return sd_bus_message_append(reply, "s", NULL);
}
if (sd_device_get_property_value(device, "ID_VENDOR_FROM_DATABASE", &hardware_vendor) < 0)
(void) sd_device_get_property_value(device, "ID_VENDOR", &hardware_vendor);
return sd_bus_message_append(reply, "s", hardware_vendor);
}
static int property_get_hardware_model(
sd_bus *bus,
const char *path,
const char *interface,
const char *property,
sd_bus_message *reply,
void *userdata,
sd_bus_error *error) {
_cleanup_(sd_device_unrefp) sd_device *device = NULL;
const char *hardware_model = NULL;
int r;
r = sd_device_new_from_syspath(&device, "/sys/class/dmi/id");
if (r < 0) {
log_warning_errno(r, "Failed to open /sys/class/dmi/id device, ignoring: %m");
return sd_bus_message_append(reply, "s", NULL);
}
if (sd_device_get_property_value(device, "ID_MODEL_FROM_DATABASE", &hardware_model) < 0)
(void) sd_device_get_property_value(device, "ID_MODEL", &hardware_model);
return sd_bus_message_append(reply, "s", hardware_model);
}
static int property_get_hostname(
sd_bus *bus,
const char *path,
@ -903,6 +956,8 @@ static const sd_bus_vtable hostname_vtable[] = {
SD_BUS_PROPERTY("OperatingSystemPrettyName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_PRETTY_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
SD_BUS_PROPERTY("OperatingSystemCPEName", "s", property_get_os_release_field, offsetof(Context, data) + sizeof(char*) * PROP_OS_CPE_NAME, SD_BUS_VTABLE_PROPERTY_CONST),
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_METHOD_WITH_NAMES("SetHostname",
"sb",

Loading…
Cancel
Save