xref: /aosp_15_r20/external/liburing/test/eventfd-reg.c (revision 25da2bea747f3a93b4c30fd9708b0618ef55a0e6)
1 /* SPDX-License-Identifier: MIT */
2 /*
3  * Description: test eventfd registration+unregistration
4  *
5  */
6 #include <errno.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <poll.h>
13 #include <sys/eventfd.h>
14 
15 #include "liburing.h"
16 
main(int argc,char * argv[])17 int main(int argc, char *argv[])
18 {
19 	struct io_uring_params p = {};
20 	struct io_uring ring;
21 	int ret, evfd[2], i;
22 
23 	if (argc > 1)
24 		return 0;
25 
26 	ret = io_uring_queue_init_params(8, &ring, &p);
27 	if (ret) {
28 		fprintf(stderr, "ring setup failed: %d\n", ret);
29 		return 1;
30 	}
31 
32 	evfd[0] = eventfd(0, EFD_CLOEXEC);
33 	evfd[1] = eventfd(0, EFD_CLOEXEC);
34 	if (evfd[0] < 0 || evfd[1] < 0) {
35 		perror("eventfd");
36 		return 1;
37 	}
38 
39 	ret = io_uring_register_eventfd(&ring, evfd[0]);
40 	if (ret) {
41 		fprintf(stderr, "failed to register evfd: %d\n", ret);
42 		return 1;
43 	}
44 
45 	/* Check that registrering again will get -EBUSY */
46 	ret = io_uring_register_eventfd(&ring, evfd[1]);
47 	if (ret != -EBUSY) {
48 		fprintf(stderr, "unexpected 2nd register: %d\n", ret);
49 		return 1;
50 	}
51 	close(evfd[1]);
52 
53 	ret = io_uring_unregister_eventfd(&ring);
54 	if (ret) {
55 		fprintf(stderr, "unexpected unregister: %d\n", ret);
56 		return 1;
57 	}
58 
59 	/* loop 100 registers/unregister */
60 	for (i = 0; i < 100; i++) {
61 		ret = io_uring_register_eventfd(&ring, evfd[0]);
62 		if (ret) {
63 			fprintf(stderr, "failed to register evfd: %d\n", ret);
64 			return 1;
65 		}
66 
67 		ret = io_uring_unregister_eventfd(&ring);
68 		if (ret) {
69 			fprintf(stderr, "unexpected unregister: %d\n", ret);
70 			return 1;
71 		}
72 	}
73 
74 	close(evfd[0]);
75 	return 0;
76 }
77