summaryrefslogtreecommitdiffstats
path: root/src/hardware/dmidecode.c
diff options
context:
space:
mode:
authorPeter Schiffer <pschiffe@redhat.com>2013-06-06 20:39:51 +0200
committerPeter Schiffer <pschiffe@redhat.com>2013-06-06 20:39:51 +0200
commitccb095d49a35b001439c254beebf5ba4e57e89e0 (patch)
treeeb2aed06949a2d2d8464d30a13f837234626fbd3 /src/hardware/dmidecode.c
parent91e01015001072515ba308e347b8997af36d8953 (diff)
downloadopenlmi-providers-ccb095d49a35b001439c254beebf5ba4e57e89e0.tar.gz
openlmi-providers-ccb095d49a35b001439c254beebf5ba4e57e89e0.tar.xz
openlmi-providers-ccb095d49a35b001439c254beebf5ba4e57e89e0.zip
Hardware: Added system slot provider
New providers: * LMI_SystemSlotProvider * LMI_SystemSlotContainerProvider
Diffstat (limited to 'src/hardware/dmidecode.c')
-rw-r--r--src/hardware/dmidecode.c210
1 files changed, 210 insertions, 0 deletions
diff --git a/src/hardware/dmidecode.c b/src/hardware/dmidecode.c
index cbee376..f29596f 100644
--- a/src/hardware/dmidecode.c
+++ b/src/hardware/dmidecode.c
@@ -1649,3 +1649,213 @@ void dmi_free_ports(DmiPort **ports, unsigned *ports_nb)
*ports_nb = 0;
*ports = NULL;
}
+
+
+/******************************************************************************
+ * DmiSystemSlot
+ */
+
+/*
+ * Initialize DmiSystemSlot attributes.
+ * @param slot
+ */
+void init_dmislot_struct(DmiSystemSlot *slot)
+{
+ slot->name = NULL;
+ slot->number = 0;
+ slot->type = NULL;
+ slot->data_width = 0;
+ slot->link_width = NULL;
+ slot->supports_hotplug = 0;
+}
+
+/*
+ * Check attributes of slot structure and fill in defaults if needed.
+ * @param slot
+ * @return 0 if success, negative value otherwise
+ */
+short check_dmislot_attributes(DmiSystemSlot *slot)
+{
+ short ret = -1;
+
+ if (!slot->name) {
+ if (!(slot->name = strdup("System slot"))) {
+ ret = -2;
+ goto done;
+ }
+ }
+ if (!slot->type) {
+ if (!(slot->type = strdup("Unknown"))) {
+ ret = -3;
+ goto done;
+ }
+ }
+ if (!slot->link_width) {
+ if (!(slot->link_width = strdup("Unknown"))) {
+ ret = -4;
+ goto done;
+ }
+ }
+
+ ret = 0;
+
+done:
+ if (ret != 0) {
+ warn("Failed to allocate memory.");
+ }
+
+ return ret;
+}
+
+short dmi_get_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
+{
+ short ret = -1;
+ int curr_slot = -1;
+ unsigned i, buffer_size = 0;
+ char **buffer = NULL, *buf;
+
+ *slots_nb = 0;
+
+ /* get dmidecode output */
+ if (run_command("dmidecode -t 9", &buffer, &buffer_size) != 0) {
+ ret = -2;
+ goto done;
+ }
+
+ /* count slots */
+ for (i = 0; i < buffer_size; i++) {
+ if (strncmp(buffer[i], "Handle 0x", 9) == 0) {
+ (*slots_nb)++;
+ }
+ }
+
+ /* if no slot was found */
+ if (*slots_nb < 1) {
+ warn("Dmidecode didn't recognize any system slot.");
+ ret = -3;
+ goto done;
+ }
+
+ /* allocate memory for slots */
+ *slots = (DmiSystemSlot *)calloc(*slots_nb, sizeof(DmiSystemSlot));
+ if (!(*slots)) {
+ warn("Failed to allocate memory.");
+ ret = -4;
+ goto done;
+ }
+
+ /* parse information about slots */
+ for (i = 0; i < buffer_size; i++) {
+ if (strncmp(buffer[i], "Handle 0x", 9) == 0) {
+ curr_slot++;
+ init_dmislot_struct(&(*slots)[curr_slot]);
+ (*slots)[curr_slot].number = curr_slot;
+ continue;
+ }
+ /* ignore first useless lines */
+ if (curr_slot < 0) {
+ continue;
+ }
+ /* Name */
+ buf = copy_string_part_after_delim(buffer[i], "Designation: ");
+ if (buf) {
+ (*slots)[curr_slot].name = buf;
+ buf = NULL;
+ continue;
+ }
+ /* Type, data width, link width */
+ buf = copy_string_part_after_delim(buffer[i], "Type: ");
+ if (buf) {
+ char **exploded_buf = NULL;
+ unsigned j, exploded_buf_size = 0, from = 0;
+
+ /* Dmi type consists of 2 information. First is data width (NUM-bit)
+ or link width (xNUM) or nothing, second is slot type. */
+ if (explode(buf, NULL, &exploded_buf, &exploded_buf_size) != 0) {
+ ret = -5;
+ goto done;
+ }
+ if (exploded_buf_size < 1) {
+ continue;
+ }
+
+ if (exploded_buf[0][0] == 'x') {
+ (*slots)[curr_slot].link_width = strdup(exploded_buf[0]);
+ if (!(*slots)[curr_slot].link_width) {
+ free_2d_buffer(&exploded_buf, &exploded_buf_size);
+ warn("Failed to allocate memory.");
+ ret = -6;
+ goto done;
+ }
+ from = 1;
+ } else if (strstr(exploded_buf[0], "-bit")) {
+ sscanf(buf, "%u-bit ", &(*slots)[curr_slot].data_width);
+ from = 1;
+ }
+
+ for (j = from; j < exploded_buf_size; j++) {
+ /* If type is not NULL, we need to add space before another string. */
+ if (!(*slots)[curr_slot].type) {
+ (*slots)[curr_slot].type = append_str(
+ (*slots)[curr_slot].type, exploded_buf[j], NULL);
+ } else {
+ (*slots)[curr_slot].type = append_str(
+ (*slots)[curr_slot].type, " ", exploded_buf[j], NULL);
+ }
+ /* If type is NULL here, append_str failed. */
+ if (!(*slots)[curr_slot].type) {
+ free_2d_buffer(&exploded_buf, &exploded_buf_size);
+ ret = -7;
+ goto done;
+ }
+ }
+
+ free_2d_buffer(&exploded_buf, &exploded_buf_size);
+ free(buf);
+ buf = NULL;
+ continue;
+ }
+ if (strcmp(buffer[i], "Hot-plug devices are supported") == 0) {
+ (*slots)[curr_slot].supports_hotplug = 1;
+ }
+ }
+
+ /* fill in default attributes if needed */
+ for (i = 0; i < *slots_nb; i++) {
+ if (check_dmislot_attributes(&(*slots)[i]) != 0) {
+ ret = -8;
+ goto done;
+ }
+ }
+
+ ret = 0;
+
+done:
+ free_2d_buffer(&buffer, &buffer_size);
+
+ if (ret != 0) {
+ dmi_free_system_slots(slots, slots_nb);
+ }
+
+ return ret;
+}
+
+void dmi_free_system_slots(DmiSystemSlot **slots, unsigned *slots_nb)
+{
+ unsigned i;
+
+ if (*slots_nb > 0) {
+ for (i = 0; i < *slots_nb; i++) {
+ free((*slots)[i].name);
+ (*slots)[i].name = NULL;
+ free((*slots)[i].type);
+ (*slots)[i].type = NULL;
+ free((*slots)[i].link_width);
+ (*slots)[i].link_width = NULL;
+ }
+ free(*slots);
+ }
+
+ *slots_nb = 0;
+ *slots = NULL;
+}