public inbox for [email protected]
 help / color / mirror / Atom feed
From: Pavel Begunkov <[email protected]>
To: Jens Axboe <[email protected]>, [email protected]
Subject: [PATCH liburing 1/1] tests: test open into taken fixed slot
Date: Tue, 14 Sep 2021 16:20:31 +0100	[thread overview]
Message-ID: <d87d67a95dc816556e3e38440bf34163fba3861f.1631632805.git.asml.silence@gmail.com> (raw)

Add a test case verifying opening into an already teaken fixed file
slot.

Signed-off-by: Pavel Begunkov <[email protected]>
---
 test/openat2.c | 84 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 76 insertions(+), 8 deletions(-)

diff --git a/test/openat2.c b/test/openat2.c
index 7838c05..379c61e 100644
--- a/test/openat2.c
+++ b/test/openat2.c
@@ -9,6 +9,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
+#include <sys/uio.h>
 
 #include "helpers.h"
 #include "liburing.h"
@@ -117,11 +118,6 @@ static int test_open_fixed(const char *path, int dfd)
 		return -1;
 	}
 
-	ret = test_openat2(&ring, path, dfd, true, 0);
-	if (ret != -EBADF) {
-		fprintf(stderr, "bogus double register %d\n", ret);
-		return -1;
-	}
 	io_uring_queue_exit(&ring);
 	return 0;
 }
@@ -152,25 +148,90 @@ static int test_open_fixed_fail(const char *path, int dfd)
 	ret = test_openat2(&ring, path, dfd, true, 1);
 	if (ret != -EINVAL) {
 		fprintf(stderr, "install out of bounds, %i\n", ret);
-		return 1;
+		return -1;
 	}
 
 	ret = test_openat2(&ring, path, dfd, true, (1u << 16));
 	if (ret != -EINVAL) {
 		fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
-		return 1;
+		return -1;
 	}
 
 	ret = test_openat2(&ring, path, dfd, true, (1u << 16) + 1);
 	if (ret != -EINVAL) {
 		fprintf(stderr, "install out of bounds or u16 overflow, %i\n", ret);
-		return 1;
+		return -1;
 	}
 
 	io_uring_queue_exit(&ring);
 	return 0;
 }
 
+static int test_direct_reinstall(const char *path, int dfd)
+{
+	struct io_uring_cqe *cqe;
+	struct io_uring_sqe *sqe;
+	char buf[1] = { 0xfa };
+	struct io_uring ring;
+	int ret, pipe_fds[2];
+	ssize_t ret2;
+
+	if (pipe2(pipe_fds, O_NONBLOCK)) {
+		fprintf(stderr, "pipe() failed\n");
+		return -1;
+	}
+	ret = io_uring_queue_init(8, &ring, 0);
+	if (ret) {
+		fprintf(stderr, "ring setup failed\n");
+		return -1;
+	}
+	ret = io_uring_register_files(&ring, pipe_fds, 2);
+	if (ret) {
+		fprintf(stderr, "%s: register ret=%d\n", __FUNCTION__, ret);
+		return -1;
+	}
+
+	/* reinstall into the second slot */
+	ret = test_openat2(&ring, path, dfd, true, 1);
+	if (ret != 0) {
+		fprintf(stderr, "reinstall failed, %i\n", ret);
+		return -1;
+	}
+
+	/* verify it's reinstalled, first write into the slot... */
+	sqe = io_uring_get_sqe(&ring);
+	io_uring_prep_write(sqe, 1, buf, sizeof(buf), 0);
+	sqe->flags |= IOSQE_FIXED_FILE;
+
+	ret = io_uring_submit(&ring);
+	if (ret != 1) {
+		fprintf(stderr, "sqe submit failed: %d\n", ret);
+		return -1;
+	}
+	ret = io_uring_wait_cqe(&ring, &cqe);
+	if (ret < 0) {
+		fprintf(stderr, "wait completion %d\n", ret);
+		return ret;
+	}
+	ret = cqe->res;
+	io_uring_cqe_seen(&ring, cqe);
+	if (ret != 1) {
+		fprintf(stderr, "invalid write %i\n", ret);
+		return -1;
+	}
+
+	/* ... and make sure nothing has been written to the pipe */
+	ret2 = read(pipe_fds[0], buf, 1);
+	if (ret2 != 0 && !(ret2 < 0 && errno == EAGAIN)) {
+		fprintf(stderr, "invalid pipe read, %d %d\n", errno, (int)ret2);
+		return -1;
+	}
+
+	close(pipe_fds[0]);
+	close(pipe_fds[1]);
+	io_uring_queue_exit(&ring);
+	return 0;
+}
 
 int main(int argc, char *argv[])
 {
@@ -227,6 +288,13 @@ int main(int argc, char *argv[])
 		fprintf(stderr, "test_open_fixed_fail failed\n");
 		goto err;
 	}
+
+	ret = test_direct_reinstall(path, -1);
+	if (ret) {
+		fprintf(stderr, "test_direct_reinstall failed\n");
+		goto err;
+	}
+
 done:
 	unlink(path);
 	if (do_unlink)
-- 
2.33.0


             reply	other threads:[~2021-09-14 15:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-14 15:20 Pavel Begunkov [this message]
2021-09-14 17:21 ` [PATCH liburing 1/1] tests: test open into taken fixed slot Jens Axboe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=d87d67a95dc816556e3e38440bf34163fba3861f.1631632805.git.asml.silence@gmail.com \
    [email protected] \
    [email protected] \
    [email protected] \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox