static void test_exec_sensor_cb(const char *cjson_sensor, void (*msg_cb)(void *opaque, rb_message_list *msgs, size_t i), void *opaque, size_t n) { const size_t aux_mem_wrap_fail_in = mem_wrap_fail_in; // Exclude this // code mem_wrap_fail_in = 0; struct _worker_info worker_info; memset(&worker_info, 0, sizeof(worker_info)); snmp_sess_init(&worker_info.default_session); struct json_object *json_sensor = json_tokener_parse(cjson_sensor); rb_sensor_t *sensor = parse_rb_sensor(json_sensor, &worker_info); json_object_put(json_sensor); mem_wrap_fail_in = aux_mem_wrap_fail_in; for (size_t i = 0; i < n; ++i) { rb_message_list messages; rb_message_list_init(&messages); process_rb_sensor(&worker_info, sensor, &messages); msg_cb(opaque, &messages, i); } rb_sensor_put(sensor); }
int wpa_ctrl_request(struct wpa_ctrl *ctrl, char *cmd, size_t cmd_len, char *reply, size_t *reply_len, void (*msg_cb)(char *msg, size_t len)) { struct timeval tv; int res; fd_set rfds; if (send(ctrl->s, cmd, cmd_len, 0) < 0) return -1; for (;;) { tv.tv_sec = 2; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(ctrl->s, &rfds); res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); if (FD_ISSET(ctrl->s, &rfds)) { res = recv(ctrl->s, reply, *reply_len, 0); if (res < 0) return res; if (res > 0 && reply[0] == '<') { /* This is an unsolicited message from * wpa_supplicant, not the reply to the * request. Use msg_cb to report this to the * caller. */ if (msg_cb) { /* Make sure the message is nul * terminated. */ if (res == *reply_len) res = (*reply_len) - 1; reply[res] = '\0'; msg_cb(reply, res); } continue; } *reply_len = res; break; } else { return -2; } } return 0; }
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len, void (*msg_cb)(char *msg, size_t len)) { struct timeval tv; struct os_reltime started_at; int res; fd_set rfds; const char *_cmd; char *cmd_buf = NULL; size_t _cmd_len; #ifdef CONFIG_CTRL_IFACE_UDP if (ctrl->cookie) { char *pos; _cmd_len = os_strlen(ctrl->cookie) + 1 + cmd_len; cmd_buf = os_malloc(_cmd_len); if (cmd_buf == NULL) return -1; _cmd = cmd_buf; pos = cmd_buf; os_strlcpy(pos, ctrl->cookie, _cmd_len); pos += os_strlen(ctrl->cookie); *pos++ = ' '; os_memcpy(pos, cmd, cmd_len); } else #endif /* CONFIG_CTRL_IFACE_UDP */ { _cmd = cmd; _cmd_len = cmd_len; } errno = 0; started_at.sec = 0; started_at.usec = 0; retry_send: if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) { if (errno == EAGAIN || errno == EBUSY || errno == EWOULDBLOCK) { /* * Must be a non-blocking socket... Try for a bit * longer before giving up. */ if (started_at.sec == 0) os_get_reltime(&started_at); else { struct os_reltime n; os_get_reltime(&n); /* Try for a few seconds. */ if (os_reltime_expired(&n, &started_at, 5)) goto send_err; } os_sleep(1, 0); goto retry_send; } send_err: os_free(cmd_buf); return -1; } os_free(cmd_buf); for (;;) { tv.tv_sec = 10; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(ctrl->s, &rfds); res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); if (res < 0 && errno == EINTR) continue; if (res < 0) return res; if (FD_ISSET(ctrl->s, &rfds)) { res = recv(ctrl->s, reply, *reply_len, 0); if (res < 0) return res; if (res > 0 && reply[0] == '<') { /* This is an unsolicited message from * wpa_supplicant, not the reply to the * request. Use msg_cb to report this to the * caller. */ if (msg_cb) { /* Make sure the message is nul * terminated. */ if ((size_t) res == *reply_len) res = (*reply_len) - 1; reply[res] = '\0'; msg_cb(reply, res); } continue; } *reply_len = res; break; } else { return -2; } } return 0; }
int wpa_ctrl_request(struct wpa_ctrl *ctrl, const char *cmd, size_t cmd_len, char *reply, size_t *reply_len, void (*msg_cb)(char *msg, size_t len)) { struct timeval tv; int res; fd_set rfds; const char *_cmd; char *cmd_buf = NULL; size_t _cmd_len; #ifdef CONFIG_CTRL_IFACE_UDP if (ctrl->cookie) { char *pos; _cmd_len = strlen(ctrl->cookie) + 1 + cmd_len; cmd_buf = os_malloc(_cmd_len ); if (cmd_buf == NULL) return -1; _cmd = cmd_buf; pos = cmd_buf; strcpy(pos, ctrl->cookie); pos += strlen(ctrl->cookie); *pos++ = ' '; memcpy(pos, cmd, cmd_len); } else #endif /* CONFIG_CTRL_IFACE_UDP */ { _cmd = cmd; _cmd_len = cmd_len; } if (send(ctrl->s, _cmd, _cmd_len, 0) < 0) { os_free(cmd_buf); return -1; } os_free(cmd_buf); for (;;) { tv.tv_sec = 2; tv.tv_usec = 0; FD_ZERO(&rfds); FD_SET(ctrl->s, &rfds); res = select(ctrl->s + 1, &rfds, NULL, NULL, &tv); if (FD_ISSET(ctrl->s, &rfds)) { res = recv(ctrl->s, reply, *reply_len, 0); if (res < 0) return res; if (res > 0 && reply[0] == '<') { /* This is an unsolicited message from * wpa_supplicant, not the reply to the * request. Use msg_cb to report this to the * caller. */ if (msg_cb) { /* Make sure the message is nul * terminated. */ if ((size_t) res == *reply_len) res = (*reply_len) - 1; reply[res] = '\0'; msg_cb(reply, res); } continue; } *reply_len = res; break; } else { return -2; } } return 0; }