static void after_read(uv_stream_t* handle, ssize_t nread, uv_buf_t buf) { uv_shutdown_t* req; if (nread < 0) { /* Error or EOF */ ASSERT (uv_last_error().code == UV_EOF); if (buf.base) { free(buf.base); } req = malloc(sizeof *req); uv_shutdown(req, (uv_network_stream_t*)handle, after_shutdown); return; } if (nread == 0) { /* Everything OK, but nothing read. */ free(buf.base); return; } /* process requests and send responses */ process_req(handle, nread, buf); }
void invoke_consensus_req(node *cur, call type, data, data_size) { vs; // Local preparation. spin_lock(); vs = idx++; spin_unlock(); call_entry *entry = build_entry(cur,vs,data,data_size); mem_write(cur_log_offset, entry); store_entry(entry); // RDMA write. for (i = 0; i < group_size; i++) { RDMA_write(i, remote_log_offset[i], entry); } // Wait for quorum. recheck: for (i = 0; i < ; i++) { bitmap += ack[i].reply; } if (reach_quorum()) { process_req(); } else goto recheck; }
static void main_loop(int fd) { int ret_code; char cmd_string[JSONRPC_MAX_STRING_LEN] = {0}; fd_set fds; struct sockaddr_in addr; socklen_t addrlen = sizeof(addr); jrpc_req_pkg_t req; json_t *rsp = NULL; int func_id = 0; int reg_connection = -1; char *rsp_str = malloc(JSONRPC_MAX_STRING_LEN); if (rsp_str == NULL) goto err; for (;;) { FD_ZERO(&fds); FD_SET(fd, &fds); ret_code = select(fd + 1, &fds, NULL, NULL, NULL); if (ret_code < 0) continue; memset(cmd_string, 0, sizeof(cmd_string)); ret_code = recvfrom(fd, cmd_string, sizeof(cmd_string), 0, (struct sockaddr *)&addr, &addrlen); if (ret_code < 0) continue; rsp = json_object(); if (rsp == NULL) goto err; rmm_log(INFO, "receive: %s.\n", cmd_string); parse_req(cmd_string, &req, &func_id); ret_code = process_req(func_id, &req, rsp); jrpc_req_pkg_free(&req); if (ret_code < 0) { continue; } memset(rsp_str, 0, JSONRPC_MAX_STRING_LEN); ret_code = jrpc_create_result_rsp_string(req.id, JSONRPC_ID_TYPE_NORMAL, rsp, JSON_OBJECT, rsp_str); if (ret_code < 0) continue; rmm_log(INFO, "send: %s.\n", rsp_str); sendto(fd, rsp_str, strnlen_s(rsp_str, JSONRPC_MAX_STRING_LEN)+1, 0, (struct sockaddr *)&addr, addrlen); } free(rsp_str); err: close(fd); return; }
void* thr1(void* arg){ while(1) switch(__VERIFIER_nondet_int()){ case 0: rdma_addr_unregister_client(__VERIFIER_nondet_int()); break; case 1: queue_req(); break; case 2: process_req(); break; case 3: rdma_resolve_ip(); break; case 4: rdma_addr_cancel(); break; } return 0; }
void* serve(void* arg) { struct list_entry* ent = (struct list_entry*) arg; struct sbpfs_head head; char* rec_buf; u64_t rec_len; //printf("start receive\n"); if (sbp_recv(ent->skt, &rec_buf, &rec_len) == -1) { printf("failed to receive data\n"); return NULL; } if (strlen(rec_buf) == 0) { goto err_exit; } if (decode_head(rec_buf, rec_len, &head) == -1) { printf("decode head failed\n"); goto err_exit; }/*head need free*/ char* method; if ((method = get_head_entry_value(&head, METHOD)) == NULL) { printf("get head entry failed\n"); goto err_exit2; } if (strcmp(method, "READ") == 0) { ent->req = 1; } else if (strcmp(method, "WRITE") == 0) { ent->req = 2; } else if (strcmp(method, "Copy") == 0) { ent->req = 3; } else { printf("invalid request\n"); goto err_exit2; } char* block_num_s; char* offset_s; char* length_s; char* content_len_s; if ((block_num_s = get_head_entry_value(&head, "Arg0")) == NULL) { printf("get head entry Arg0 failed\n"); goto err_exit2; } if ((offset_s = get_head_entry_value(&head, "Arg1")) == NULL) { printf("get head entry Arg1 failed\n"); goto err_exit2; } if ((length_s = get_head_entry_value(&head, "Arg2")) == NULL) { printf("get head entry Arg2 failed\n"); goto err_exit2; } if ((content_len_s = get_head_entry_value(&head, "Content-Length")) == NULL) { printf("get head entry Content-Length failed\n"); goto err_exit2; } ent->block_num = atoll(block_num_s); ent->offset = atoi(offset_s); ent->length = atoi(length_s); if (ent->req == 2 || ent->req == 3) { ent->data_length = atoll(content_len_s); ent->data = rec_buf + head.head_len; } //send_err(ent->skt, "a", "b"); process_req(ent); err_exit2: free_head(&head); err_exit: free(rec_buf); close(ent->skt); free(ent); return NULL; }
/** * Main request handling loop * * @return 0 on success, err code on failure */ int go(void) { int i; int res; uint32_t tmp = 0; uint32_t *seed; char *resend; size_t tosend; size_t offset; uint32_t *f; init_fs(); debug("fs init complete\n"); if (!READDATATIMEOUT(conf)) { debug("failed to recv conf\n"); res = 0; SENDDATA(res); return 3; } if (conf.min_ver > VERSION || conf.max_ver < VERSION) { debug("version mismatch\n"); res = 0; SENDDATA(res); return 5; } //send success ack res = 1; SENDDATA(res); //seed our PRNG //because of how it works, seeding with 0's == no encryption seed = (uint32_t*)(FLAG_PAGE + (8*sizeof(uint32_t))); f = malloc(8*sizeof(uint32_t)); if (!f) return 1; cgc_memcpy(f, seed, 8*sizeof(uint32_t)); if (!conf.encrypt) seed = calloc(8*sizeof(uint32_t)); srand(seed); //inform client of seed for (i = 0; i < 8; i+=2) { if (conf.encrypt) tmp = f[i]^f[i+1]; SENDDATA(tmp); } #ifdef PATCHED_1 cgc_memset(f, '\0', 8*sizeof(uint32_t)); #endif debug("flag data: @h\n",f); free(f); while (1) { res = process_req(); if (res == ERR || res == EXIT) { break; } else if (res == DISCON && conf.allow_reconnect && last) { debug("disconnected, retrying\n"); if (!READDATATIMEOUT(conf)) { debug("failed to recv conf\n"); return 3; } if (!last) continue; debug("Allocating resend buffer of @h bytes.\n",conf.buf_size+sizeof(resp_t)); #ifdef PATCHED_1 resend = calloc(conf.buf_size+sizeof(resp_t)); #else resend = malloc(conf.buf_size+sizeof(resp_t)); #endif debug("resend data: @h\n",resend); if (!READDATA(offset)) { debug("failed to recv offset\n"); return 4; } tosend = sizeof(resp_t)-offset; tosend = tosend > sizeof(resp_t) ? sizeof(resp_t) : tosend; if (offset > conf.buf_size || offset > sizeof(resp_t) #ifdef PATCHED_1 || tosend > (conf.buf_size+sizeof(resp_t)) #endif ) { debug("failed to validate offset.\n"); return 1; } else { debug("copying last out\n"); cgc_memcpy(resend, last+offset, sizeof(resp_t)-offset); } #ifdef PATCHED_1 if (tosend != esendall(stdout, resend, tosend)) #else if (tosend != esendall(stdout, resend, sizeof(resp_t))) #endif return 4; debug("Resent all, should be back!\n"); free(resend); free(last); last = NULL; } } return 0; }
/* * Takes the command line parameters and communicates appropriately * with the route6 daemon. */ int main(int argc, char *argv[]) { int command; int pid; struct sigaction sig; bzero((void *)&sig, sizeof(sig)); sig.sa_handler = (void *)cleanup; sig.sa_flags = 0; if (sigaction(SIGALRM, &sig, (struct sigaction *)NULL) < 0) { perror("sigaction"); exit(1); } sig.sa_flags = 0; sig.sa_handler = (void *)cleanup; if (sigaction(SIGTERM, &sig, (struct sigaction *)NULL) < 0) { perror("sigaction"); exit(1); } if ((command = parse(argc, argv)) < 0) { usage(); exit(1); } switch (command) { case ADM_STAT: case ADM_TABLE: process_req(); break; case ADM_SIGNAL: /* superuser permissions reqd. to send signals to route6d */ if (getuid() != 0) { fprintf(stderr, "Permission denied\n"); exit(1); } if ((pid = getroute6dpid()) == 0) { fprintf(stderr, "%s is not runnig.", PROGNAME); exit(1); } /* Send requested signal to route6d */ if (kill(pid, sigval) < 0) perror("kill"); break; case ADM_EXEC: if (getuid() != 0) { fprintf(stderr, "Permission denied\n"); exit(1); } if ((pid = getroute6dpid()) != 0) { fprintf(stderr, "maybe %s is still alive\n", PROGNAME); exit(1); } execl(RT6_PATH, RT6_PATH, NULL); perror("start failed"); break; } cleanup(); exit(0); }