summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Axboe <axboe@kernel.dk>2019-05-24 09:03:11 -0600
committerJens Axboe <axboe@kernel.dk>2019-05-24 09:04:17 -0600
commitd672969583c3226828392771b4a949452c9ef578 (patch)
treefdeb720a8512b4f60808bc33870770b823f86da8
parent5d1e32e44b2f7795266d012288f307c94ae72868 (diff)
test/send_recvmsg: code cleanups and error handling
Signed-off-by: Jens Axboe <axboe@kernel.dk>
-rw-r--r--test/send_recvmsg.c60
1 files changed, 38 insertions, 22 deletions
diff --git a/test/send_recvmsg.c b/test/send_recvmsg.c
index 900b0d9..e6cda69 100644
--- a/test/send_recvmsg.c
+++ b/test/send_recvmsg.c
@@ -1,3 +1,6 @@
+/*
+ * Simple test case showing using sendmsg and recvmsg through io_uring
+ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -17,15 +20,17 @@ static char str[] = "This is a test of sendmsg and recvmsg over io_uring!";
static int do_recvmsg(void)
{
- struct sockaddr_in saddr;
- char buf[MAX_MSG + 1];
- struct msghdr msg;
- struct iovec iov;
+ struct sockaddr_in saddr;
+ char buf[MAX_MSG + 1];
+ struct msghdr msg;
+ struct iovec iov = {
+ .iov_base = buf,
+ .iov_len = sizeof(buf) - 1,
+ };
struct io_uring ring;
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
- char *recv_str;
- int sockfd, ret;
+ int sockfd, ret;
ret = io_uring_queue_init(1, &ring, 0);
if (ret) {
@@ -33,18 +38,25 @@ static int do_recvmsg(void)
return 1;
}
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
- memset(&saddr, 0, sizeof(saddr));
- saddr.sin_family = AF_INET;
- saddr.sin_addr.s_addr = htonl(INADDR_ANY);
- saddr.sin_port = htons(PORT);
- bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
+ memset(&saddr, 0, sizeof(saddr));
+ saddr.sin_family = AF_INET;
+ saddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ saddr.sin_port = htons(PORT);
+
+ sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sockfd < 0) {
+ perror("socket");
+ return 1;
+ }
+
+ ret = bind(sockfd, (struct sockaddr *)&saddr, sizeof(saddr));
+ if (ret < 0) {
+ perror("bind");
+ goto err;
+ }
memset(&msg, 0, sizeof(msg));
msg.msg_namelen = sizeof(struct sockaddr_in);
-
- iov.iov_base = buf;
- iov.iov_len = MAX_MSG;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
@@ -72,8 +84,7 @@ static int do_recvmsg(void)
goto err;
}
- recv_str = msg.msg_iov[0].iov_base;
- if (strcmp(str, recv_str)) {
+ if (strcmp(str, iov.iov_base)) {
printf("string mismatch\n");
goto err;
}
@@ -88,8 +99,11 @@ err:
static int do_sendmsg(void)
{
struct sockaddr_in saddr;
+ struct iovec iov = {
+ .iov_base = str,
+ .iov_len = sizeof(str),
+ };
struct msghdr msg;
- struct iovec iov;
struct io_uring ring;
struct io_uring_cqe *cqe;
struct io_uring_sqe *sqe;
@@ -101,7 +115,6 @@ static int do_sendmsg(void)
return 1;
}
- sockfd = socket(AF_INET, SOCK_DGRAM, 0);
memset(&saddr, 0, sizeof(saddr));
saddr.sin_family = AF_INET;
saddr.sin_port = htons(PORT);
@@ -110,12 +123,15 @@ static int do_sendmsg(void)
memset(&msg, 0, sizeof(msg));
msg.msg_name = &saddr;
msg.msg_namelen = sizeof(struct sockaddr_in);
-
- iov.iov_base = str;
- iov.iov_len = sizeof(str);
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
+ sockfd = socket(AF_INET, SOCK_DGRAM, 0);
+ if (sockfd < 0) {
+ perror("socket");
+ return 1;
+ }
+
sqe = io_uring_get_sqe(&ring);
memset(sqe, 0, sizeof(*sqe));
sqe->opcode = IORING_OP_SENDMSG;