| 1 |
/* |
| 2 |
* MIT License |
| 3 |
* |
| 4 |
* Copyright(c) 2019 Intel Corporation. All rights reserved. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and associated documentation files (the "Software"), to deal |
| 8 |
* in the Software without restriction, including without limitation the rights |
| 9 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
* copies of the Software, and to permit persons to whom the Software is |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 |
* SOFTWARE. |
| 23 |
* |
| 24 |
* Authors: |
| 25 |
* Kamil Wiatrowski <[email protected]> |
| 26 |
* |
| 27 |
*/ |
| 28 |
|
| 29 |
/* |
| 30 |
* The util helps to parse the output from dmidecode command. |
| 31 |
* It expects that the output follows the given format, the size of |
| 32 |
* tabulations does not matter: |
| 33 |
* |
| 34 |
* Handle 1(...) |
| 35 |
* SMBIOS type name |
| 36 |
* item1: value |
| 37 |
* item2: value |
| 38 |
* list name: |
| 39 |
* list elem1 |
| 40 |
* list elem2 |
| 41 |
* (...) |
| 42 |
* item3: value |
| 43 |
* (...) |
| 44 |
* |
| 45 |
* Handle 2(...) |
| 46 |
* SMBIOS type name |
| 47 |
* (and so on ...) |
| 48 |
* |
| 49 |
*/ |
| 50 |
|
| 51 |
#include "collectd.h" |
| 52 |
|
| 53 |
#include "utils/common/common.h" |
| 54 |
#include "utils/dmi/dmi.h" |
| 55 |
|
| 56 |
#define UTIL_NAME "dmi_reader" |
| 57 |
|
| 58 |
#define DMIDECODE_CMD_FMT_LEN DMI_MAX_LEN |
| 59 |
|
| 60 |
static int dmi_look_for_handle(dmi_reader_t *r); |
| 61 |
|
| 62 |
static int dmi_read_entry(dmi_reader_t *r) { |
| 63 |
while (fgets(r->_buff, STATIC_ARRAY_SIZE(r->_buff), r->_fd) != NULL) { |
| 64 |
char *buff = r->_buff; |
| 65 |
while (isspace(*buff)) |
| 66 |
buff++; |
| 67 |
if (*buff == '\0') { |
| 68 |
r->current_type = DMI_ENTRY_NONE; |
| 69 |
r->_read_next = dmi_look_for_handle; |
| 70 |
return DMI_OK; |
| 71 |
} |
| 72 |
|
| 73 |
strstripnewline(buff); |
| 74 |
char *sep = strchr(buff, ':'); |
| 75 |
if (sep == NULL) { |
| 76 |
r->value = buff; |
| 77 |
r->current_type = DMI_ENTRY_LIST_VALUE; |
| 78 |
return DMI_OK; |
| 79 |
} |
| 80 |
|
| 81 |
sep[0] = '\0'; |
| 82 |
r->name = buff; |
| 83 |
if (sep[1] == '\0') { |
| 84 |
r->current_type = DMI_ENTRY_LIST_NAME; |
| 85 |
} else { |
| 86 |
sep++; |
| 87 |
while (isspace(*sep)) |
| 88 |
sep++; |
| 89 |
if (*sep == '\0') |
| 90 |
INFO(UTIL_NAME ": value is empty for: \'%s\'.", r->_buff); |
| 91 |
r->value = sep; |
| 92 |
r->current_type = DMI_ENTRY_MAP; |
| 93 |
} |
| 94 |
return DMI_OK; |
| 95 |
} |
| 96 |
|
| 97 |
r->current_type = DMI_ENTRY_END; |
| 98 |
return DMI_OK; |
| 99 |
} |
| 100 |
|
| 101 |
static int dmi_read_type_name(dmi_reader_t *r) { |
| 102 |
while (fgets(r->_buff, STATIC_ARRAY_SIZE(r->_buff), r->_fd) != NULL) { |
| 103 |
strstripnewline(r->_buff); |
| 104 |
if (strlen(r->_buff) == 0) { |
| 105 |
ERROR(UTIL_NAME ": unexpected format of dmidecode output."); |
| 106 |
return DMI_ERROR; |
| 107 |
} |
| 108 |
|
| 109 |
r->name = r->_buff; |
| 110 |
r->current_type = DMI_ENTRY_NAME; |
| 111 |
r->_read_next = dmi_read_entry; |
| 112 |
|
| 113 |
return DMI_OK; |
| 114 |
} |
| 115 |
|
| 116 |
r->current_type = DMI_ENTRY_END; |
| 117 |
return DMI_OK; |
| 118 |
} |
| 119 |
|
| 120 |
static int dmi_look_for_handle(dmi_reader_t *r) { |
| 121 |
while (fgets(r->_buff, STATIC_ARRAY_SIZE(r->_buff), r->_fd) != NULL) { |
| 122 |
const char *handle = "Handle"; |
| 123 |
if (strncmp(handle, r->_buff, strlen(handle)) != 0) |
| 124 |
continue; |
| 125 |
|
| 126 |
r->_read_next = dmi_read_type_name; |
| 127 |
return DMI_OK; |
| 128 |
} |
| 129 |
|
| 130 |
r->current_type = DMI_ENTRY_END; |
| 131 |
return DMI_OK; |
| 132 |
} |
| 133 |
|
| 134 |
int dmi_reader_init(dmi_reader_t *reader, const dmi_type type) { |
| 135 |
if (reader == NULL) { |
| 136 |
ERROR(UTIL_NAME ".%s: NULL pointer.", __func__); |
| 137 |
return DMI_ERROR; |
| 138 |
} |
| 139 |
char cmd[DMIDECODE_CMD_FMT_LEN]; |
| 140 |
|
| 141 |
if (type == DMI_TYPE_ALL) |
| 142 |
strncpy(cmd, "dmidecode 2>/dev/null", STATIC_ARRAY_SIZE(cmd)); |
| 143 |
else |
| 144 |
snprintf(cmd, STATIC_ARRAY_SIZE(cmd), "dmidecode -t %d 2>/dev/null", type); |
| 145 |
|
| 146 |
DEBUG(UTIL_NAME ": dmidecode cmd=\'%s\'.", cmd); |
| 147 |
|
| 148 |
reader->_fd = popen(cmd, "r"); |
| 149 |
if (reader->_fd == NULL) { |
| 150 |
ERROR(UTIL_NAME ": popen failed."); |
| 151 |
return DMI_ERROR; |
| 152 |
} |
| 153 |
|
| 154 |
reader->name = NULL; |
| 155 |
reader->value = NULL; |
| 156 |
reader->current_type = DMI_ENTRY_NONE; |
| 157 |
reader->_read_next = dmi_look_for_handle; |
| 158 |
|
| 159 |
return DMI_OK; |
| 160 |
} |
| 161 |
|
| 162 |
void dmi_reader_clean(dmi_reader_t *reader) { |
| 163 |
if (reader == NULL) { |
| 164 |
WARNING(UTIL_NAME ".%s: NULL pointer.", __func__); |
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
if (reader->_fd) { |
| 169 |
pclose(reader->_fd); |
| 170 |
reader->_fd = NULL; |
| 171 |
} |
| 172 |
|
| 173 |
reader->_read_next = NULL; |
| 174 |
} |
| 175 |
|
| 176 |
int dmi_read_next(dmi_reader_t *reader) { |
| 177 |
if (reader == NULL || reader->_read_next == NULL || reader->_fd == NULL) { |
| 178 |
ERROR(UTIL_NAME ".%s: NULL pointer.", __func__); |
| 179 |
return DMI_ERROR; |
| 180 |
} |
| 181 |
int ret = reader->_read_next(reader); |
| 182 |
|
| 183 |
if (reader->current_type == DMI_ENTRY_END || ret == DMI_ERROR) { |
| 184 |
pclose(reader->_fd); |
| 185 |
reader->_fd = NULL; |
| 186 |
reader->_read_next = NULL; |
| 187 |
DEBUG(UTIL_NAME ": dmidecode reader finished, status=%d.", ret); |
| 188 |
} |
| 189 |
|
| 190 |
return ret; |
| 191 |
} |