From b955821b18f7f5a9ee4a5d417fab8b2757f8a2fa Mon Sep 17 00:00:00 2001 From: Burt P Date: Fri, 25 Aug 2017 11:12:39 -0500 Subject: dmi: requested changes Change requested by lpereira in https://github.com/lpereira/hardinfo/pull/160 Signed-off-by: Burt P --- hardinfo/dmi_util.c | 128 ++++++++++++++++++++++++++-------------------------- includes/dmi_util.h | 23 +++++++++- modules/computer.c | 4 +- 3 files changed, 88 insertions(+), 67 deletions(-) diff --git a/hardinfo/dmi_util.c b/hardinfo/dmi_util.c index f87e3546..5083c3c8 100644 --- a/hardinfo/dmi_util.c +++ b/hardinfo/dmi_util.c @@ -21,7 +21,7 @@ #include "hardinfo.h" #include "dmi_util.h" -const char *dmi_sysfs_root() { +static const char *dmi_sysfs_root(void) { char *candidates[] = { "/sys/devices/virtual/dmi", "/sys/class/dmi", @@ -37,77 +37,79 @@ const char *dmi_sysfs_root() { } char *dmi_get_str(const char *id_str) { - static struct { - char *id; - char *path; - } tab_dmi_sysfs[] = { - /* dmidecode -> sysfs */ - { "bios-release-date", "id/bios_date" }, - { "bios-vendor", "id/bios_vendor" }, - { "bios-version", "id/bios_version" }, - { "baseboard-product-name", "id/board_name" }, - { "baseboard-manufacturer", "id/board_vendor" }, - { "baseboard-version", "id/board_version" }, - { "baseboard-serial-number", "id/board_serial" }, - { "baseboard-asset-tag", "id/board_asset_tag" }, - { "system-product-name", "id/product_name" }, - { "system-manufacturer", "id/sys_vendor" }, - { "system-serial-number", "id/product_serial" }, - { "system-product-family", "id/product_family" }, - { "system-version", "id/product_version" }, - { "system-uuid", "product_uuid" }, - { "chassis-type", "id/chassis_type" }, - { "chassis-serial-number", "id/chassis_serial" }, - { "chassis-manufacturer", "id/chassis_vendor" }, - { "chassis-version", "id/chassis_version" }, - { "chassis-asset-tag", "id/chassis_asset_tag" }, - { NULL, NULL } - }; - const gchar *dmi_root = dmi_sysfs_root(); - gchar *full_path = NULL, *ret = NULL; - gboolean spawned; - gchar *out, *err; + static struct { + char *id; + char *path; + } tab_dmi_sysfs[] = { + /* dmidecode -> sysfs */ + { "bios-release-date", "id/bios_date" }, + { "bios-vendor", "id/bios_vendor" }, + { "bios-version", "id/bios_version" }, + { "baseboard-product-name", "id/board_name" }, + { "baseboard-manufacturer", "id/board_vendor" }, + { "baseboard-version", "id/board_version" }, + { "baseboard-serial-number", "id/board_serial" }, + { "baseboard-asset-tag", "id/board_asset_tag" }, + { "system-product-name", "id/product_name" }, + { "system-manufacturer", "id/sys_vendor" }, + { "system-serial-number", "id/product_serial" }, + { "system-product-family", "id/product_family" }, + { "system-version", "id/product_version" }, + { "system-uuid", "product_uuid" }, + { "chassis-type", "id/chassis_type" }, + { "chassis-serial-number", "id/chassis_serial" }, + { "chassis-manufacturer", "id/chassis_vendor" }, + { "chassis-version", "id/chassis_version" }, + { "chassis-asset-tag", "id/chassis_asset_tag" }, + { NULL, NULL } + }; + const gchar *dmi_root = dmi_sysfs_root(); + gchar *ret = NULL; + gchar full_path[PATH_MAX]; + gboolean spawned; + gchar *out, *err; - int i = 0; + int i = 0; - /* try sysfs first */ - if (dmi_root) { - while (tab_dmi_sysfs[i].id != NULL) { - if (strcmp(id_str, tab_dmi_sysfs[i].id) == 0) { - full_path = g_strdup_printf("%s/%s", dmi_root, tab_dmi_sysfs[i].path); - if (g_file_get_contents(full_path, &ret, NULL, NULL) ) - goto dmi_str_done; + /* try sysfs first */ + if (dmi_root) { + while (tab_dmi_sysfs[i].id != NULL) { + if (strcmp(id_str, tab_dmi_sysfs[i].id) == 0) { + snprintf(full_path, PATH_MAX, "%s/%s", dmi_root, tab_dmi_sysfs[i].path); + if (g_file_get_contents(full_path, &ret, NULL, NULL) ) + goto dmi_str_done; + } + i++; } - i++; } - } - /* try dmidecode, but may require root */ - full_path = g_strconcat("dmidecode -s ", id_str, NULL); - spawned = g_spawn_command_line_sync(full_path, + /* try dmidecode, but may require root */ + snprintf(full_path, PATH_MAX, "dmidecode -s %s", id_str); + spawned = g_spawn_command_line_sync(full_path, &out, &err, &i, NULL); - if (spawned) { - if (i == 0) - ret = out; - else - g_free(out); - g_free(err); - } + if (spawned) { + if (i == 0) + ret = out; + else + g_free(out); + + g_free(err); + } dmi_str_done: - if (ret != NULL) { - ret = strend(ret, '\n'); - ret = g_strstrip(ret); - if (strlen(ret) == 0) { - g_free(ret); - ret = NULL; + if (ret != NULL) { + ret = strend(ret, '\n'); + ret = g_strstrip(ret); + /* return NULL on empty */ + if (*ret == 0) { + g_free(ret); + ret = NULL; + } } - } - g_free(full_path); - return ret; + return ret; } -char *dmi_chassis_type_str(int chassis_type, int with_val) { +char *dmi_chassis_type_str(int chassis_type, gboolean with_val) { static const char *types[] = { N_("Invalid chassis type (0)"), N_("Unknown chassis type"), /* 1 is "Other", but not helpful in HardInfo */ @@ -145,8 +147,8 @@ char *dmi_chassis_type_str(int chassis_type, int with_val) { if (chassis_type >= 0 && chassis_type < G_N_ELEMENTS(types)) { if (with_val) return g_strdup_printf("[%d] %s", chassis_type, _(types[chassis_type])); - else - return g_strdup(_(types[chassis_type])); + + return g_strdup(_(types[chassis_type])); } return NULL; } diff --git a/includes/dmi_util.h b/includes/dmi_util.h index 965bee91..aecda739 100644 --- a/includes/dmi_util.h +++ b/includes/dmi_util.h @@ -1,12 +1,31 @@ +/* + * HardInfo - Displays System Information + * Copyright (C) 2003-2017 Leandro A. F. Pereira + * This file + * Copyright (C) 2017 Burt P. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + #ifndef __DMI_UTIL_H__ #define __DMI_UTIL_H__ -const char *dmi_sysfs_root(void); char *dmi_get_str(const char *id_str); /* if chassis_type is <=0 it will be fetched from DMI. * with_val = true, will return a string like "[3] Desktop" instead of just * "Desktop". */ -char *dmi_chassis_type_str(int chassis_type, int with_val); +char *dmi_chassis_type_str(int chassis_type, gboolean with_val); #endif diff --git a/modules/computer.c b/modules/computer.c index 388c305a..c7581be4 100644 --- a/modules/computer.c +++ b/modules/computer.c @@ -308,14 +308,14 @@ static gchar *detect_machine_type(void) gchar *chassis; chassis = dmi_chassis_type_str(-1, 0); - if (chassis != NULL) + if (chassis) return chassis; chassis = dtr_get_string("/model", 0); if (chassis) { if (strstr(chassis, "Raspberry Pi") != NULL || strstr(chassis, "ODROID") != NULL - /* etc */ ) { + /* FIXME: consider making a table when adding more models */ ) { g_free(chassis); return g_strdup(_("Single-board computer")); } -- cgit v1.2.3