From: Doug Ledford Date: Wed, 18 Jun 2014 17:44:28 +0000 (-0700) Subject: rdma_server: use perror, unwind allocs on failure X-Git-Tag: v1.0.19~20 X-Git-Url: https://openfabrics.org/gitweb/?a=commitdiff_plain;h=2c2e44e144f17c2cef4af052ec91a680c9a81fb9;p=~shefty%2Flibrdmacm.git rdma_server: use perror, unwind allocs on failure Our main test function prints out errno directly, which is hard to read as it's not decoded at all. Instead, use perror() to make failures more readable. Also redo the failure flow so that we can do a simple unwind at the end of the function and just jump to the right unwind spot on error. Signed-off-by: Doug Ledford Signed-off-by: Sean Hefty --- diff --git a/examples/rdma_server.c b/examples/rdma_server.c index 5b9e16d5..54922fc6 100644 --- a/examples/rdma_server.c +++ b/examples/rdma_server.c @@ -55,7 +55,7 @@ static int run(void) hints.ai_port_space = RDMA_PS_TCP; ret = rdma_getaddrinfo(NULL, port, &hints, &res); if (ret) { - printf("rdma_getaddrinfo %d\n", errno); + perror("rdma_getaddrinfo"); return ret; } @@ -65,65 +65,71 @@ static int run(void) attr.cap.max_inline_data = 16; attr.sq_sig_all = 1; ret = rdma_create_ep(&listen_id, res, NULL, &attr); - rdma_freeaddrinfo(res); if (ret) { - printf("rdma_create_ep %d\n", errno); - return ret; + perror("rdma_create_ep"); + goto out_free_addrinfo; } ret = rdma_listen(listen_id, 0); if (ret) { - printf("rdma_listen %d\n", errno); - return ret; + perror("rdma_listen"); + goto out_destroy_listen_ep; } ret = rdma_get_request(listen_id, &id); if (ret) { - printf("rdma_get_request %d\n", errno); - return ret; + perror("rdma_get_request"); + goto out_destroy_listen_ep; } mr = rdma_reg_msgs(id, recv_msg, 16); if (!mr) { - printf("rdma_reg_msgs %d\n", errno); - return ret; + ret = -1; + perror("rdma_reg_msgs"); + goto out_destroy_accept_ep; } ret = rdma_post_recv(id, NULL, recv_msg, 16, mr); if (ret) { - printf("rdma_post_recv %d\n", errno); - return ret; + perror("rdma_post_recv"); + goto out_dereg; } ret = rdma_accept(id, NULL); if (ret) { - printf("rdma_accept %d\n", errno); - return ret; + perror("rdma_accept"); + goto out_dereg; } - ret = rdma_get_recv_comp(id, &wc); - if (ret <= 0) { - printf("rdma_get_recv_comp %d\n", ret); - return ret; + while ((ret = rdma_get_recv_comp(id, &wc)) == 0); + if (ret < 0) { + perror("rdma_get_recv_comp"); + goto out_disconnect; } ret = rdma_post_send(id, NULL, send_msg, 16, NULL, IBV_SEND_INLINE); if (ret) { - printf("rdma_post_send %d\n", errno); - return ret; + perror("rdma_post_send"); + goto out_disconnect; } - ret = rdma_get_send_comp(id, &wc); - if (ret <= 0) { - printf("rdma_get_send_comp %d\n", ret); - return ret; - } + while ((ret = rdma_get_send_comp(id, &wc)) == 0); + if (ret < 0) + perror("rdma_get_send_comp"); + else + ret = 0; +out_disconnect: rdma_disconnect(id); +out_dereg: rdma_dereg_mr(mr); +out_destroy_accept_ep: rdma_destroy_ep(id); +out_destroy_listen_ep: rdma_destroy_ep(listen_id); - return 0; +out_free_addrinfo: + rdma_freeaddrinfo(res); + return ret; } int main(int argc, char **argv)