static int send_req_and_recv_resp_sk(int fd, CriuReq *req, CriuResp **resp) { int ret = 0; if (send_req(fd, req) < 0) { ret = -ECOMM; goto exit; } again: *resp = recv_resp(fd); if (!*resp) { perror("Can't receive response"); ret = -ECOMM; goto exit; } if ((*resp)->type == CRIU_REQ_TYPE__NOTIFY) { if (notify) ret = notify((*resp)->notify->script, (*resp)->notify); ret = send_notify_ack(fd, ret); if (!ret) goto again; else goto exit; } if ((*resp)->type != req->type) { if ((*resp)->type == CRIU_REQ_TYPE__EMPTY && (*resp)->success == false) ret = -EINVAL; else { perror("Unexpected response type"); ret = -EBADMSG; } } exit: return ret; }
int DeRegNode(struct storage_node *node) { struct isns_cmd *cmd; struct isns_pdu *pdu; struct isns_resp *resp = NULL; char *buf; if (!node) { log_error("Null storage node to deregister.\n"); return -EINVAL; } if (!(cmd = allocate_isns_cmd())) { log_error("allocate isns cmd fail\n"); return -ENOMEM; } pdu = &cmd->pdu; init_isns_hdr(pdu->hdr, FUNC_DevDeReg, NO_REPLACE); buf = pdu->pay_load; buf = append_tlv(buf, &node->iscsi_name); buf = append_tlv(buf, &delimiter); /* iscsi name can be as an operating attr for dereg */ //buf = append_tlv(buf, &iet_entity->eid); buf = append_tlv(buf, &node->iscsi_name); cmd->cmd_size = buf - (char *)pdu->hdr; pdu->hdr->pdu_length = htons((u16)(buf - (char *)pdu->pay_load)); if (send_cmd(cmd)) { log_error("fail to send isns cmd\n"); return -EIO; } if (!(resp = allocate_isns_resp())) { log_error("allocate isns resp fail\n"); return -ENOMEM; } recv_resp(resp, FUNC_DevDeRegRsp); free_isns_resp(resp); return 0; }
int DeRegPortal(struct portal *p, struct tag_len_val *name) { struct isns_cmd *cmd; struct isns_pdu *pdu; struct isns_resp *resp = NULL; char *buf; if (!p) { log_error("Null portal to deregister.\n"); return -EINVAL; } if (!(cmd = allocate_isns_cmd())) { log_error("allocate isns cmd fail\n"); return -ENOMEM; } pdu = &cmd->pdu; init_isns_hdr(pdu->hdr, FUNC_DevDeReg, NO_REPLACE); buf = pdu->pay_load; buf = append_tlv(buf, name); buf = append_tlv(buf, &delimiter); buf = append_tlv(buf, &p->portal_ip_addr); buf = append_tlv(buf, &p->portal_port); cmd->cmd_size = buf - (char *)pdu->hdr; pdu->hdr->pdu_length = htons((u16)(buf - (char *)pdu->pay_load)); if (send_cmd(cmd)) { log_error("fail to send isns cmd\n"); return -EIO; } if (!(resp = allocate_isns_resp())) { log_error("allocate isns resp fail\n"); return -ENOMEM; } recv_resp(resp, FUNC_DevDeRegRsp); free_isns_resp(resp); return 0; }
int main(int argc, char *argv[]) { CriuReq req = CRIU_REQ__INIT; CriuResp *resp = NULL; int fd, dir_fd; int ret = 0; struct sockaddr_un addr; socklen_t addr_len; if (argc != 3) { fprintf(stderr, "Usage: test-c criu-service.socket imgs_dir"); return -1; } /* * Open a directory, in which criu will * put images */ puts(argv[2]); dir_fd = open(argv[2], O_DIRECTORY); if (dir_fd == -1) { perror("Can't open imgs dir"); return -1; } /* * Set "DUMP" type of request. * Allocate CriuDumpReq. */ req.type = CRIU_REQ_TYPE__DUMP; req.opts = malloc(sizeof(CriuOpts)); if (!req.opts) { perror("Can't allocate memory for dump request"); return -1; } criu_opts__init(req.opts); /* * Set dump options. * Checkout more in protobuf/rpc.proto. */ req.opts->has_leave_running = true; req.opts->leave_running = true; req.opts->images_dir_fd = dir_fd; req.opts->has_log_level = true; req.opts->log_level = 4; /* * Connect to service socket */ fd = socket(AF_LOCAL, SOCK_SEQPACKET, 0); if (fd == -1) { perror("Can't create socket"); return -1; } memset(&addr, 0, sizeof(addr)); addr.sun_family = AF_LOCAL; strcpy(addr.sun_path, argv[1]); addr_len = strlen(addr.sun_path) + sizeof(addr.sun_family); ret = connect(fd, (struct sockaddr *) &addr, addr_len); if (ret == -1) { perror("Cant connect to socket"); goto exit; } /* * Send request */ ret = send_req(fd, &req); if (ret == -1) { perror("Can't send request"); goto exit; } /* * Recv response */ resp = recv_resp(fd); if (!resp) { perror("Can't recv response"); ret = -1; goto exit; } if (resp->type != CRIU_REQ_TYPE__DUMP) { perror("Unexpected response type"); ret = -1; goto exit; } /* * Check response. */ if (resp->success) puts("Success"); else { puts("Fail"); ret = -1; goto exit; } if (resp->dump->has_restored && resp->dump->restored) puts("Restored"); exit: close(fd); close(dir_fd); if (resp) criu_resp__free_unpacked(resp, NULL); return ret; }
int RegNode(struct storage_node *node) { struct isns_cmd *cmd; struct isns_pdu *pdu; struct isns_resp *resp = NULL; char *buf; struct portal *p; if (!node) { log_error("Null storage node to register.\n"); return -EINVAL; } if (!(cmd = allocate_isns_cmd())) { log_error("allocate isns cmd fail\n"); return -ENOMEM; } pdu = &cmd->pdu; init_isns_hdr(pdu->hdr, FUNC_DevAttrReg, NO_REPLACE); buf = pdu->pay_load; buf = append_tlv(buf, &node->iscsi_name); buf = append_tlv(buf, &delimiter); /* FIXME: assume one entity now */ buf = append_tlv(buf, &iet_entity->eid); buf = append_tlv(buf, &iet_entity->entity_proto); for (p = iet_portal; p ; p = p->next) { buf = append_tlv(buf, &p->portal_ip_addr); buf = append_tlv(buf, &p->portal_port); } buf = append_tlv(buf, &node->iscsi_name); buf = append_tlv(buf, &node->iscsi_node_type); buf = append_tlv(buf, &node->iscsi_alias); buf = append_tlv(buf, &node->iscsi_auth_method); cmd->cmd_size = buf - (char *)pdu->hdr; pdu->hdr->pdu_length = htons((u16)(buf - (char *)pdu->pay_load)); if (send_cmd(cmd)) { log_error("fail to send isns cmd\n"); free_isns_cmd(cmd); return -EIO; } free_isns_cmd(cmd); if (!(resp = allocate_isns_resp())) { log_error("allocate isns resp fail\n"); return -ENOMEM; } if (recv_resp(resp, FUNC_DevAttrRegRsp)) { free_isns_resp(resp); return -EIO; } if ((ntohl(*((u32 *)resp->pdu.pay_load + 1)) == ATTR_TAG_EID) && !iet_entity->eid.attr_len) { // get assigned EID from iSNS server iet_entity->eid.attr_len = ntohl(*((u32 *)resp->pdu.pay_load + 2)); memcpy(iet_entity->eid.attr_val, resp->pdu.pay_load + 8, ntohl(*((u32 *)resp->pdu.pay_load + 2))); log_debug(1, "new eid info, %ld, %s\n", iet_entity->eid.attr_len, iet_entity->eid.attr_val); } free_isns_resp(resp); return 0; }