const char *name;
} dmi_type_name_t;
-static char *g_cap_string = NULL;
+static char *g_cap_json = NULL;
static char *httpd_host = NULL;
static unsigned short httpd_port = 9104;
static struct MHD_Daemon *httpd;
DEBUG(" %s:%s", reader.name, reader.value);
attributes = NULL;
if (entries == NULL) {
- ERROR(CAP_PLUGIN ": unexpected dmi output format");
+ ERROR(CAP_PLUGIN ": unexpected dmi output format.");
dmi_reader_clean(&reader);
return -1;
}
if (json_object_set_new(entries, reader.name,
json_string(reader.value))) {
- ERROR(CAP_PLUGIN ": Failed to set json entry.");
+ ERROR(CAP_PLUGIN ": Failed to set json object for entries.");
dmi_reader_clean(&reader);
return -1;
}
case DMI_ENTRY_LIST_NAME:
DEBUG(" %s:", reader.name);
if (entries == NULL) {
- ERROR(CAP_PLUGIN ": unexpected dmi output format");
+ ERROR(CAP_PLUGIN ": unexpected dmi output format.");
dmi_reader_clean(&reader);
return -1;
}
return -1;
}
if (json_object_set_new(entries, reader.name, attributes)) {
- ERROR(CAP_PLUGIN ": Failed to set json entry.");
+ ERROR(CAP_PLUGIN ": Failed to set json object for entry %s.",
+ reader.name);
dmi_reader_clean(&reader);
return -1;
}
*connection_state = &(int){44};
return MHD_YES;
}
- DEBUG(CAP_PLUGIN ": formatted response: %s", g_cap_string);
+ DEBUG(CAP_PLUGIN ": formatted response: %s", g_cap_json);
#if defined(MHD_VERSION) && MHD_VERSION >= 0x00090500
struct MHD_Response *res = MHD_create_response_from_buffer(
- strlen(g_cap_string), g_cap_string, MHD_RESPMEM_PERSISTENT);
+ strlen(g_cap_json), g_cap_json, MHD_RESPMEM_PERSISTENT);
#else
struct MHD_Response *res =
- MHD_create_response_from_data(strlen(g_cap_string), g_cap_string, 0, 0);
+ MHD_create_response_from_data(strlen(g_cap_json), g_cap_json, 0, 0);
#endif
+ if (res == NULL) {
+ ERROR(CAP_PLUGIN ": MHD create response failed.");
+ return MHD_NO;
+ }
MHD_add_response_header(res, MHD_HTTP_HEADER_CONTENT_TYPE, CONTENT_TYPE_JSON);
int status = MHD_queue_response(connection, MHD_HTTP_OK, res);
}
if (bind(fd, ai->ai_addr, ai->ai_addrlen) != 0) {
+ INFO(CAP_PLUGIN ": bind failed: %s", STRERRNO);
close(fd);
fd = -1;
continue;
}
if (listen(fd, LISTEN_BACKLOG) != 0) {
+ INFO(CAP_PLUGIN ": listen failed: %s", STRERRNO);
close(fd);
fd = -1;
continue;
#endif
MHD_OPTION_EXTERNAL_LOGGER, cap_logger, NULL, MHD_OPTION_END);
- if (d == NULL) {
+ if (d == NULL)
ERROR(CAP_PLUGIN ": MHD_start_daemon() failed.");
- return NULL;
- }
return d;
}
}
sfree(httpd_host);
- sfree(g_cap_string);
+ sfree(g_cap_json);
return 0;
}
return -1;
}
- g_cap_string = json_dumps(root, JSON_COMPACT);
+ g_cap_json = json_dumps(root, JSON_COMPACT);
json_decref(root);
- if (g_cap_string == NULL) {
+ if (g_cap_json == NULL) {
ERROR(CAP_PLUGIN ": json_dumps() failed.");
cap_shutdown();
return -1;
DMI_ENTRY_MAP, DMI_ENTRY_END};
static size_t len = STATIC_ARRAY_SIZE(entry);
+static struct MHD_Response *mhd_res = NULL;
+
/* mock functions */
int dmi_reader_init(dmi_reader_t *reader, const dmi_type type) {
reader->current_type = DMI_ENTRY_NONE;
struct MHD_Response *
MHD_create_response_from_buffer(size_t size, void *data,
enum MHD_ResponseMemoryMode mode) {
- return NULL;
+ return mhd_res;
}
struct MHD_Response *MHD_create_response_from_data(size_t size, void *data,
int must_free,
int must_copy) {
- return NULL;
+ return mhd_res;
}
int MHD_add_response_header(struct MHD_Response *response, const char *header,
DEF_TEST(http_handler) {
void *state = NULL;
- g_cap_string = "TEST";
+ g_cap_json = "TEST";
int ret = cap_http_handler(NULL, NULL, NULL, MHD_HTTP_METHOD_PUT, NULL, NULL,
NULL, &state);
EXPECT_EQ_INT(MHD_NO, ret);
EXPECT_EQ_INT(MHD_YES, ret);
CHECK_NOT_NULL(state);
+ ret = cap_http_handler(NULL, NULL, NULL, MHD_HTTP_METHOD_GET, NULL, NULL,
+ NULL, &state);
+ EXPECT_EQ_INT(MHD_NO, ret);
+ CHECK_NOT_NULL(state);
+
+ /* mock not NULL pointer */
+ mhd_res = (struct MHD_Response *)&(int){0};
ret = cap_http_handler(NULL, NULL, NULL, MHD_HTTP_METHOD_GET, NULL, NULL,
NULL, &state);
EXPECT_EQ_INT(MHD_HTTP_OK, ret);
CHECK_NOT_NULL(state);
- g_cap_string = NULL;
+ g_cap_json = NULL;
+ mhd_res = NULL;
return 0;
}
B<Synopsis:>
<Plugin capabilities>
- Host "<hostname>"
+ Host "localhost"
Port "9104"
</Plugin>
#include "utils/common/common.h"
#include "utils/dmi/dmi.h"
-#define UTIL_NAME "DMI_READER"
+#define UTIL_NAME "dmi_reader"
#define DMIDECODE_CMD_FMT_LEN DMI_MAX_LEN
buff++;
if (*buff == '\0') {
r->current_type = DMI_ENTRY_NONE;
- r->_read_callback = dmi_look_for_handle;
+ r->_read_next = dmi_look_for_handle;
return DMI_OK;
}
r->name = r->_buff;
r->current_type = DMI_ENTRY_NAME;
- r->_read_callback = dmi_read_entry;
+ r->_read_next = dmi_read_entry;
return DMI_OK;
}
if (strncmp(handle, r->_buff, strlen(handle)) != 0)
continue;
- r->_read_callback = dmi_read_type_name;
+ r->_read_next = dmi_read_type_name;
return DMI_OK;
}
reader->name = NULL;
reader->value = NULL;
- reader->_read_callback = dmi_look_for_handle;
reader->current_type = DMI_ENTRY_NONE;
+ reader->_read_next = dmi_look_for_handle;
return DMI_OK;
}
return;
}
- if (reader->_fd)
+ if (reader->_fd) {
pclose(reader->_fd);
+ reader->_fd = NULL;
+ }
- reader->_fd = NULL;
- reader->_read_callback = NULL;
+ reader->_read_next = NULL;
}
int dmi_read_next(dmi_reader_t *reader) {
- if (reader == NULL || reader->_read_callback == NULL || reader->_fd == NULL) {
+ if (reader == NULL || reader->_read_next == NULL || reader->_fd == NULL) {
ERROR(UTIL_NAME ".%s: NULL pointer.", __func__);
return DMI_ERROR;
}
- int ret = reader->_read_callback(reader);
+ int ret = reader->_read_next(reader);
if (reader->current_type == DMI_ENTRY_END || ret == DMI_ERROR) {
pclose(reader->_fd);
- reader->_read_callback = NULL;
+ reader->_fd = NULL;
+ reader->_read_next = NULL;
DEBUG(UTIL_NAME ": dmidecode reader finished, status=%d.", ret);
}
typedef struct dmi_reader_s {
FILE *_fd;
char _buff[DMI_MAX_LEN];
- int (*_read_callback)(struct dmi_reader_s *reader);
+ int (*_read_next)(struct dmi_reader_s *reader);
/* Type of current entry */
entry_type current_type;
/* Entry name, the pointer changes after every read. */