123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- #include "../config-host.h"
- /* SPDX-License-Identifier: MIT */
- /*
- * Description: Test IORING_ASYNC_CANCEL_{ALL,FD}
- *
- */
- #include <errno.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <string.h>
- #include <poll.h>
- #include "liburing.h"
- static int no_cancel_flags;
- static int test1(struct io_uring *ring, int *fd)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- int ret, i;
- for (i = 0; i < 8; i++) {
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- return 1;
- }
- io_uring_prep_poll_add(sqe, fd[0], POLLIN);
- sqe->user_data = i + 1;
- }
- ret = io_uring_submit(ring);
- if (ret < 8) {
- fprintf(stderr, "sqe submit failed: %d\n", ret);
- return 1;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- return 1;
- }
- /*
- * Mark CANCEL_ALL to cancel all matching the key, and use
- * CANCEL_FD to cancel requests matching the specified fd.
- * This should cancel all the pending poll requests on the pipe
- * input.
- */
- io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
- sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD;
- sqe->fd = fd[0];
- sqe->user_data = 100;
- ret = io_uring_submit(ring);
- if (ret < 1) {
- fprintf(stderr, "child: sqe submit failed: %d\n", ret);
- return 1;
- }
- for (i = 0; i < 9; i++) {
- if (no_cancel_flags)
- break;
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait=%d\n", ret);
- return 1;
- }
- switch (cqe->user_data) {
- case 100:
- if (cqe->res == -EINVAL) {
- no_cancel_flags = 1;
- break;
- }
- if (cqe->res != 8) {
- fprintf(stderr, "canceled %d\n", cqe->res);
- return 1;
- }
- break;
- case 1 ... 8:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "poll res %d\n", cqe->res);
- return 1;
- }
- break;
- default:
- fprintf(stderr, "invalid user_data %lu\n",
- (unsigned long) cqe->user_data);
- return 1;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- }
- static int test2(struct io_uring *ring, int *fd)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- int ret, i, fd2[2];
- if (pipe(fd2) < 0) {
- perror("pipe");
- return 1;
- }
- for (i = 0; i < 8; i++) {
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- if (!(i & 1))
- io_uring_prep_poll_add(sqe, fd[0], POLLIN);
- else
- io_uring_prep_poll_add(sqe, fd2[0], POLLIN);
- sqe->user_data = i & 1;
- }
- ret = io_uring_submit(ring);
- if (ret < 8) {
- fprintf(stderr, "sqe submit failed: %d\n", ret);
- goto err;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- /*
- * Mark CANCEL_ALL to cancel all matching the key, and use
- * CANCEL_FD to cancel requests matching the specified fd.
- * This should cancel all the pending poll requests on the pipe
- * input.
- */
- io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
- sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD;
- sqe->fd = fd[0];
- sqe->user_data = 100;
- ret = io_uring_submit(ring);
- if (ret < 1) {
- fprintf(stderr, "sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 5; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait=%d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 100:
- if (cqe->res != 4) {
- fprintf(stderr, "canceled %d\n", cqe->res);
- goto err;
- }
- break;
- case 0:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "poll res %d\n", cqe->res);
- goto err;
- }
- break;
- default:
- fprintf(stderr, "invalid user_data %lu\n",
- (unsigned long) cqe->user_data);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- usleep(1000);
- /*
- * Should not have any pending CQEs now
- */
- ret = io_uring_peek_cqe(ring, &cqe);
- if (!ret) {
- fprintf(stderr, "Unexpected extra cancel cqe\n");
- goto err;
- }
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- /*
- * Mark CANCEL_ALL to cancel all matching the key, and use
- * CANCEL_FD to cancel requests matching the specified fd.
- * This should cancel all the pending poll requests on the pipe
- * input.
- */
- io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
- sqe->cancel_flags |= IORING_ASYNC_CANCEL_FD;
- sqe->fd = fd2[0];
- sqe->user_data = 100;
- ret = io_uring_submit(ring);
- if (ret < 1) {
- fprintf(stderr, "sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 5; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait=%d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 100:
- if (cqe->res != 4) {
- fprintf(stderr, "canceled %d\n", cqe->res);
- goto err;
- }
- break;
- case 1:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "poll res %d\n", cqe->res);
- goto err;
- }
- break;
- default:
- fprintf(stderr, "invalid user_data %lu\n",
- (unsigned long) cqe->user_data);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- close(fd2[0]);
- close(fd2[1]);
- return 0;
- err:
- close(fd2[0]);
- close(fd2[1]);
- return 1;
- }
- static int test3(struct io_uring *ring, int *fd)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- int ret, i, fd2[2];
- if (pipe(fd2) < 0) {
- perror("pipe");
- return 1;
- }
- for (i = 0; i < 8; i++) {
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- if (!(i & 1)) {
- io_uring_prep_poll_add(sqe, fd[0], POLLIN);
- sqe->flags |= IOSQE_ASYNC;
- } else
- io_uring_prep_poll_add(sqe, fd2[0], POLLIN);
- sqe->user_data = i & 1;
- }
- ret = io_uring_submit(ring);
- if (ret < 8) {
- fprintf(stderr, "child: sqe submit failed: %d\n", ret);
- goto err;
- }
- usleep(10000);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- /*
- * Mark CANCEL_ALL to cancel all matching the key, and use
- * CANCEL_FD to cancel requests matching the specified fd.
- * This should cancel all the pending poll requests on the pipe
- * input.
- */
- io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
- sqe->cancel_flags |= IORING_ASYNC_CANCEL_ANY;
- sqe->fd = 0;
- sqe->user_data = 100;
- ret = io_uring_submit(ring);
- if (ret < 1) {
- fprintf(stderr, "child: sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 9; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait=%d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 100:
- if (cqe->res != 8) {
- fprintf(stderr, "canceled %d\n", cqe->res);
- goto err;
- }
- break;
- case 0:
- case 1:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "poll res %d\n", cqe->res);
- goto err;
- }
- break;
- default:
- fprintf(stderr, "invalid user_data %lu\n",
- (unsigned long) cqe->user_data);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- close(fd2[0]);
- close(fd2[1]);
- return 0;
- err:
- close(fd2[0]);
- close(fd2[1]);
- return 1;
- }
- static int test4(struct io_uring *ring, int *fd)
- {
- struct io_uring_sqe *sqe;
- struct io_uring_cqe *cqe;
- char buffer[32];
- int ret, i;
- for (i = 0; i < 8; i++) {
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- io_uring_prep_read(sqe, fd[0], &buffer, sizeof(buffer), 0);
- sqe->flags |= IOSQE_ASYNC;
- sqe->user_data = i + 1;
- }
- ret = io_uring_submit(ring);
- if (ret < 8) {
- fprintf(stderr, "child: sqe submit failed: %d\n", ret);
- goto err;
- }
- usleep(10000);
- sqe = io_uring_get_sqe(ring);
- if (!sqe) {
- fprintf(stderr, "get sqe failed\n");
- goto err;
- }
- /*
- * Mark CANCEL_ALL to cancel all matching the key, and use
- * CANCEL_FD to cancel requests matching the specified fd.
- * This should cancel all the pending poll requests on the pipe
- * input.
- */
- io_uring_prep_cancel(sqe, 0, IORING_ASYNC_CANCEL_ALL);
- sqe->cancel_flags |= IORING_ASYNC_CANCEL_ANY;
- sqe->fd = 0;
- sqe->user_data = 100;
- ret = io_uring_submit(ring);
- if (ret < 1) {
- fprintf(stderr, "child: sqe submit failed: %d\n", ret);
- goto err;
- }
- for (i = 0; i < 9; i++) {
- ret = io_uring_wait_cqe(ring, &cqe);
- if (ret) {
- fprintf(stderr, "wait=%d\n", ret);
- goto err;
- }
- switch (cqe->user_data) {
- case 100:
- if (cqe->res != 8) {
- fprintf(stderr, "canceled %d\n", cqe->res);
- goto err;
- }
- break;
- case 1 ... 8:
- if (cqe->res != -ECANCELED) {
- fprintf(stderr, "poll res %d\n", cqe->res);
- goto err;
- }
- break;
- default:
- fprintf(stderr, "invalid user_data %lu\n",
- (unsigned long) cqe->user_data);
- goto err;
- }
- io_uring_cqe_seen(ring, cqe);
- }
- return 0;
- err:
- return 1;
- }
- int main(int argc, char *argv[])
- {
- struct io_uring ring;
- int ret, fd[2];
- if (argc > 1)
- return 0;
- if (pipe(fd) < 0) {
- perror("pipe");
- return 1;
- }
- ret = io_uring_queue_init(8, &ring, 0);
- if (ret) {
- fprintf(stderr, "ring setup failed: %d\n", ret);
- return 1;
- }
- ret = test1(&ring, fd);
- if (ret) {
- fprintf(stderr, "test1 failed\n");
- return ret;
- }
- if (no_cancel_flags)
- return 0;
- ret = test2(&ring, fd);
- if (ret) {
- fprintf(stderr, "test2 failed\n");
- return ret;
- }
- ret = test3(&ring, fd);
- if (ret) {
- fprintf(stderr, "test3 failed\n");
- return ret;
- }
- ret = test4(&ring, fd);
- if (ret) {
- fprintf(stderr, "test4 failed\n");
- return ret;
- }
- return 0;
- }
|