Jens Axboe | e502435 | 2020-02-11 20:34:12 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Description: test wq sharing |
| 4 | */ |
| 5 | #include <errno.h> |
| 6 | #include <stdio.h> |
| 7 | #include <unistd.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <string.h> |
| 10 | #include <fcntl.h> |
| 11 | |
| 12 | #include "liburing.h" |
| 13 | |
Jens Axboe | 3f78c17 | 2020-01-27 17:29:55 -0700 | [diff] [blame] | 14 | static int test_attach_invalid(int ringfd) |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 15 | { |
| 16 | struct io_uring_params p; |
| 17 | struct io_uring ring; |
| 18 | int ret; |
| 19 | |
| 20 | memset(&p, 0, sizeof(p)); |
| 21 | p.flags = IORING_SETUP_ATTACH_WQ; |
Jens Axboe | 3f78c17 | 2020-01-27 17:29:55 -0700 | [diff] [blame] | 22 | p.wq_fd = ringfd; |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 23 | ret = io_uring_queue_init_params(1, &ring, &p); |
| 24 | if (ret != -EINVAL) { |
| 25 | fprintf(stderr, "Attach to zero: %d\n", ret); |
| 26 | goto err; |
| 27 | } |
| 28 | return 0; |
| 29 | err: |
| 30 | return 1; |
| 31 | } |
| 32 | |
Jens Axboe | 3f78c17 | 2020-01-27 17:29:55 -0700 | [diff] [blame] | 33 | static int test_attach(int ringfd) |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 34 | { |
| 35 | struct io_uring_params p; |
| 36 | struct io_uring ring2; |
| 37 | int ret; |
| 38 | |
| 39 | memset(&p, 0, sizeof(p)); |
| 40 | p.flags = IORING_SETUP_ATTACH_WQ; |
Jens Axboe | 3f78c17 | 2020-01-27 17:29:55 -0700 | [diff] [blame] | 41 | p.wq_fd = ringfd; |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 42 | ret = io_uring_queue_init_params(1, &ring2, &p); |
| 43 | if (ret == -EINVAL) { |
| 44 | fprintf(stdout, "Sharing not supported, skipping\n"); |
| 45 | return 0; |
| 46 | } else if (ret) { |
| 47 | fprintf(stderr, "Attach to id: %d\n", ret); |
| 48 | goto err; |
| 49 | } |
| 50 | io_uring_queue_exit(&ring2); |
| 51 | return 0; |
| 52 | err: |
| 53 | return 1; |
| 54 | } |
| 55 | |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 56 | int main(int argc, char *argv[]) |
| 57 | { |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 58 | struct io_uring ring; |
| 59 | int ret; |
| 60 | |
Jens Axboe | a2141fc | 2020-05-19 17:36:19 -0600 | [diff] [blame^] | 61 | if (argc > 1) |
| 62 | return 0; |
| 63 | |
Jens Axboe | 34901a7 | 2020-01-28 12:51:58 -0700 | [diff] [blame] | 64 | ret = io_uring_queue_init(8, &ring, 0); |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 65 | if (ret) { |
| 66 | fprintf(stderr, "ring setup failed\n"); |
| 67 | return 1; |
| 68 | } |
| 69 | |
Jens Axboe | 3f78c17 | 2020-01-27 17:29:55 -0700 | [diff] [blame] | 70 | /* stdout is definitely not an io_uring descriptor */ |
| 71 | ret = test_attach_invalid(2); |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 72 | if (ret) { |
Jens Axboe | 3f78c17 | 2020-01-27 17:29:55 -0700 | [diff] [blame] | 73 | fprintf(stderr, "test_attach_invalid failed\n"); |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 74 | return ret; |
| 75 | } |
| 76 | |
Jens Axboe | 3f78c17 | 2020-01-27 17:29:55 -0700 | [diff] [blame] | 77 | ret = test_attach(ring.ring_fd); |
Jens Axboe | b769750 | 2020-01-24 10:17:31 -0700 | [diff] [blame] | 78 | if (ret) { |
| 79 | fprintf(stderr, "test_attach failed\n"); |
| 80 | return ret; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |