int fio_net_send_cmd(int fd, uint16_t opcode, const void *buf, off_t size, uint64_t *tagptr, struct flist_head *list) { struct fio_net_cmd *cmd = NULL; size_t this_len, cur_len = 0; uint64_t tag; int ret; if (list) { assert(tagptr); tag = *tagptr = alloc_reply(*tagptr, opcode); } else tag = tagptr ? *tagptr : 0; do { this_len = size; if (this_len > FIO_SERVER_MAX_FRAGMENT_PDU) this_len = FIO_SERVER_MAX_FRAGMENT_PDU; if (!cmd || cur_len < sizeof(*cmd) + this_len) { if (cmd) free(cmd); cur_len = sizeof(*cmd) + this_len; cmd = malloc(cur_len); } fio_init_net_cmd(cmd, opcode, buf, this_len, tag); if (this_len < size) cmd->flags = __cpu_to_le32(FIO_NET_CMD_F_MORE); fio_net_cmd_crc(cmd); ret = fio_send_data(fd, cmd, sizeof(*cmd) + this_len); size -= this_len; buf += this_len; } while (!ret && size); if (list) { if (ret) free_reply(tag); else add_reply(tag, list); } if (cmd) free(cmd); return ret; }
/* * If 'list' is non-NULL, then allocate and store the sent command for * later verification. */ int fio_net_send_simple_cmd(int sk, uint16_t opcode, uint64_t tag, struct flist_head *list) { int ret; if (list) tag = alloc_reply(tag, opcode); ret = fio_net_send_simple_stack_cmd(sk, opcode, tag); if (ret) { if (list) free_reply(tag); return ret; } if (list) add_reply(tag, list); return 0; }
static void query_zones(wrt_client_t *c, mrp_json_t *req) { const char *type = RESWRT_QUERY_ZONES; mrp_json_t *reply; const char **zones; size_t nzone; int seq; if (!mrp_json_get_integer(req, "seq", &seq)) { ignore_invalid_request(c, req, "missing 'seq' field"); return; } zones = mrp_zone_get_all_names(0, NULL); if (zones == NULL) { error_reply(c, type, seq, ENOMEM, "failed to query zone names"); return; } reply = alloc_reply(type, seq); if (reply == NULL) mrp_log_error("Failed to allocate WRT resource reply."); nzone = 0; for (nzone = 0; zones[nzone] != NULL; nzone++) ; if (mrp_json_add_integer (reply, "status", 0) && mrp_json_add_string_array(reply, "zones" , zones, nzone)) send_message(c, reply); mrp_json_unref(reply); mrp_free(zones); }
static void query_classes(wrt_client_t *c, mrp_json_t *req) { const char *type = RESWRT_QUERY_CLASSES; mrp_json_t *reply; const char **classes; size_t nclass; int seq; if (!mrp_json_get_integer(req, "seq", &seq)) { ignore_invalid_request(c, req, "missing 'seq' field"); return; } classes = mrp_application_class_get_all_names(0, NULL); if (classes == NULL) { error_reply(c, type, seq, ENOMEM, "failed to query class names"); return; } reply = alloc_reply(type, seq); if (reply == NULL) return; nclass = 0; for (nclass = 0; classes[nclass] != NULL; nclass++) ; if (mrp_json_add_integer (reply, "status" , 0) && mrp_json_add_string_array(reply, "classes", classes, nclass)) send_message(c, reply); mrp_json_unref(reply); mrp_free(classes); }
static void query_resources(wrt_client_t *c, mrp_json_t *req) { const char *type = RESWRT_QUERY_RESOURCES; mrp_json_t *reply, *rarr, *r, *ao; const char **resources; int seq, cnt; mrp_attr_t *attrs, *a; mrp_attr_t buf[ATTRIBUTE_MAX]; uint32_t id; if (!mrp_json_get_integer(req, "seq", &seq)) { ignore_invalid_request(c, req, "missing 'seq' field"); return; } rarr = r = ao = NULL; resources = mrp_resource_definition_get_all_names(0, NULL); if (resources == NULL) { error_reply(c, type, seq, ENOMEM, "failed to query class names"); return; } reply = alloc_reply(type, seq); if (reply == NULL) return; rarr = mrp_json_create(MRP_JSON_ARRAY); if (rarr == NULL) goto fail; for (id = 0; resources[id]; id++) { r = mrp_json_create(MRP_JSON_OBJECT); if (r == NULL) goto fail; if (!mrp_json_add_string (r, "name", resources[id])) goto fail; attrs = mrp_resource_definition_read_all_attributes(id, ATTRIBUTE_MAX, buf); ao = mrp_json_create(MRP_JSON_OBJECT); if (ao == NULL) goto fail; for (a = attrs, cnt = 0; a->name; a++, cnt++) { switch (a->type) { case mqi_string: if (!mrp_json_add_string(ao, a->name, a->value.string)) goto fail; break; case mqi_integer: case mqi_unsignd: if (!mrp_json_add_integer(ao, a->name, a->value.integer)) goto fail; break; case mqi_floating: if (!mrp_json_add_double(ao, a->name, a->value.floating)) goto fail; break; default: mrp_log_error("attribute '%s' of resource '%s' " "has unknown type %d", a->name, resources[id], a->type); break; } } if (cnt > 0) mrp_json_add(r, "attributes", ao); else mrp_json_unref(ao); ao = NULL; if (!mrp_json_array_append(rarr, r)) goto fail; else r = NULL; } if (mrp_json_add_integer(reply, "status" , 0)) { mrp_json_add (reply, "resources", rarr); send_message(c, reply); } mrp_json_unref(reply); mrp_free(resources); return; fail: mrp_json_unref(reply); mrp_json_unref(rarr); mrp_json_unref(r); mrp_json_unref(ao); mrp_free(resources); }