AtlatestRepositorysigil-ui

sigil-ui / tree / test / waylanddmabuf-fence-peer.c

1/* Owned pipes establish nonblocking scheduling and descriptor ownership.
2 * They are not GPU sync-file or hardware import qualification. */
3#define main import_ownership_main
4#include "dmabuf-import-peer.c"
5#undef main
6
7int main(int argc, char **argv)
8{
9 assert(argc == 2);
10 const char *mode = argv[1];
11 struct rlimit no_core = {0, 0};
12 assert(setrlimit(RLIMIT_CORE, &no_core) == 0);
13 signal(SIGPIPE, SIG_IGN);
14 ImportStats *stats = mmap(NULL, sizeof *stats, PROT_READ | PROT_WRITE,
15 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
16 assert(stats != MAP_FAILED);
17 int sockets[2];
18 assert(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);
19 pid_t peer = fork();
20 assert(peer >= 0);
21 if (!peer) {
22 close(sockets[0]);
23 int status = import_serve(sockets[1], "fence", stats);
24 if (status == 2 && (errno == EPIPE || errno == ECONNRESET)) status = 0;
25 _exit(status);
26 }
27 close(sockets[1]);
28 char name[32], error[256];
29 snprintf(name, sizeof name, "%d", sockets[0]);
30 assert(setenv("WAYLAND_SOCKET", name, 1) == 0 && load_libraries(error, sizeof error));
31 Wl *root = calloc(1, sizeof *root);
32 assert(root);
33 root->references = root->configured = root->scale = 1;
34 root->width = 640;
35 root->height = 480;
36 for (unsigned i = 0; i < CLIPBOARD_WRITES; i++) root->writes[i].fd = -1;
37 root->display = wl_display_connect(NULL);
38 assert(root->display);
39 root->registry = wl_display_get_registry(root->display);
40 assert(wl_registry_add_listener(root->registry, &registry_listener, root) == 0);
41 assert(wl_display_roundtrip(root->display) >= 0);
42 root->surface = wl_compositor_create_surface(root->compositor);
43 Wl *child = sui_wayland_child(root, 0, 0, 16, 16, 0);
44 assert(child && wl_display_roundtrip(root->display) >= 0);
45 int plane = memfd_create("fence-fixture", MFD_CLOEXEC), pipes[2];
46 uint32_t marker = 0x22334455;
47 assert(plane >= 0 && ftruncate(plane, 4096) == 0 && pwrite(plane, &marker, 4, 0) == 4);
48 assert(pipe2(pipes, O_CLOEXEC | O_NONBLOCK) == 0);
49 unsigned descriptors = import_fd_count(), count = !strcmp(mode, "many") ? DMABUF_IMPORT_LIMIT : 1;
50 SuiWaylandDmabufSpec spec = {.width = 16, .height = 16, .fourcc = 0x34325241,
51 .n_planes = 1, .modifier = 0x0100000000000001, .planes = {{plane, 0, 64}}};
52 SuiWaylandDmabufImport *buffers[DMABUF_IMPORT_LIMIT] = {0};
53 for (unsigned i = 0; i < count; i++) assert((buffers[i] = sui_wayland_dmabuf_import(child, &spec)));
54 if (!strcmp(mode, "pending-import"))
55 assert(!sui_wayland_dmabuf_import_fence(buffers[0], pipes[0], 1000) && errno == EINVAL);
56 assert(wl_display_roundtrip(root->display) >= 0);
57 if (!strcmp(mode, "invalid")) {
58 assert(!sui_wayland_dmabuf_import_fence(NULL, pipes[0], 1000) && errno == EINVAL);
59 assert(!sui_wayland_dmabuf_import_fence(buffers[0], pipes[0], 0) && errno == EINVAL);
60 assert(!sui_wayland_dmabuf_import_fence(buffers[0], pipes[0], 5001) && errno == EINVAL);
61 assert(!sui_wayland_dmabuf_import_fence(buffers[0], -1, 1000) && errno == EINVAL);
62 int stale = dup(pipes[0]);
63 assert(stale >= 0 && close(stale) == 0);
64 assert(!sui_wayland_dmabuf_import_fence(buffers[0], stale, 1000) && errno == EBADF);
65 assert(import_fd_count() == descriptors);
66 }
67 unsigned timeout = !strcmp(mode, "timeout") ? 25 : 1000;
68 for (unsigned i = 0; i < count; i++) {
69 assert(sui_wayland_dmabuf_import_fence(buffers[i], pipes[0], timeout));
70 assert(sui_wayland_dmabuf_import_status(buffers[i]) == SUI_DMABUF_IMPORT_FENCE_PENDING);
71 assert(!sui_wayland_present_dmabuf(child, buffers[i], NULL, NULL, NULL));
72 }
73 assert(import_fd_count() == descriptors + count && fcntl(pipes[0], F_GETFD) == FD_CLOEXEC);
74 if (!strcmp(mode, "double")) {
75 assert(!sui_wayland_dmabuf_import_fence(buffers[0], pipes[0], 1000) && errno == EINVAL);
76 assert(import_fd_count() == descriptors + count);
77 }
78 int write_closed = 0;
79 pid_t writer = -1;
80 if (!strcmp(mode, "delayed")) {
81 writer = fork();
82 assert(writer >= 0);
83 if (!writer) { usleep(30000); _exit(write(pipes[1], "x", 1) == 1 ? 0 : 2); }
84 /* Protocol work stays responsive before the producer is ready. */
85 assert(wl_display_roundtrip(root->display) >= 0);
86 assert(!stats->attaches);
87 } else if (!strcmp(mode, "error")) {
88 assert(close(pipes[1]) == 0);
89 write_closed = 1;
90 } else if (strcmp(mode, "timeout") && strcmp(mode, "cancel") && strcmp(mode, "unref") &&
91 strcmp(mode, "parent-close") && strcmp(mode, "disconnect")) {
92 assert(write(pipes[1], "x", 1) == 1);
93 }
94 if (!strcmp(mode, "cancel")) {
95 sui_wayland_dmabuf_import_cancel(buffers[0]);
96 assert(sui_wayland_dmabuf_import_status(buffers[0]) == SUI_DMABUF_IMPORT_CANCELED);
97 assert(import_fd_count() == descriptors);
98 } else if (!strcmp(mode, "unref")) {
99 sui_wayland_dmabuf_import_unref(buffers[0]);
100 buffers[0] = NULL;
101 assert(import_fd_count() == descriptors);
102 } else if (!strcmp(mode, "parent-close")) {
103 sui_wayland_close(root);
104 assert(sui_wayland_dmabuf_import_status(buffers[0]) == SUI_DMABUF_IMPORT_CANCELED);
105 assert(import_fd_count() + 1 == descriptors);
106 } else if (!strcmp(mode, "disconnect")) {
107 assert(shutdown(wl_display_get_fd(root->display), SHUT_RDWR) == 0);
108 root->count = root->wake = 0;
109 assert(dispatch(root, 0) < 0);
110 assert(sui_wayland_dmabuf_import_status(buffers[0]) == SUI_DMABUF_IMPORT_CANCELED);
111 /* Failed status still requires normal cleanup to close owned FDs. */
112 sui_wayland_close(root);
113 assert(import_fd_count() + 1 == descriptors);
114 } else {
115 if (!strcmp(mode, "child-close")) sui_wayland_close(child);
116 int64_t start = now_ms();
117 do {
118 root->count = root->head = root->wake = 0;
119 assert(dispatch(root, 200) >= 0);
120 } while (sui_wayland_dmabuf_import_status(buffers[0]) == SUI_DMABUF_IMPORT_FENCE_PENDING &&
121 now_ms() - start < 1500);
122 int expected = !strcmp(mode, "timeout") ? SUI_DMABUF_IMPORT_FENCE_TIMED_OUT :
123 !strcmp(mode, "error") ? SUI_DMABUF_IMPORT_FENCE_FAILED : SUI_DMABUF_IMPORT_READY;
124 for (unsigned i = 0; i < count; i++) assert(sui_wayland_dmabuf_import_status(buffers[i]) == expected);
125 assert(import_fd_count() + write_closed == descriptors);
126 if (!strcmp(mode, "delayed")) assert(now_ms() - start < 1000);
127 if (expected != SUI_DMABUF_IMPORT_READY || !strcmp(mode, "child-close"))
128 assert(!sui_wayland_present_dmabuf(child, buffers[0], NULL, NULL, NULL));
129 else {
130 assert(sui_wayland_present_dmabuf(child, buffers[0], NULL, NULL, NULL));
131 assert(!sui_wayland_dmabuf_import_fence(buffers[0], pipes[0], 1000) && errno == EINVAL);
132 assert(wl_display_roundtrip(root->display) >= 0 && wl_display_roundtrip(root->display) >= 0);
133 }
134 }
135 if (writer >= 0) {
136 int status;
137 assert(waitpid(writer, &status, 0) == writer && WIFEXITED(status) && WEXITSTATUS(status) == 0);
138 }
139 sui_wayland_close(root);
140 sui_wayland_unref(child);
141 sui_wayland_unref(root);
142 for (unsigned i = 0; i < count; i++) sui_wayland_dmabuf_import_unref(buffers[i]);
143 assert(fcntl(pipes[0], F_GETFD) == FD_CLOEXEC && import_fd_count() + 1 + write_closed == descriptors);
144 close(plane);
145 close(pipes[0]);
146 if (!write_closed) close(pipes[1]);
147 unsetenv("WAYLAND_SOCKET");
148 int status;
149 assert(waitpid(peer, &status, 0) == peer && WIFEXITED(status) && WEXITSTATUS(status) == 0);
150 munmap(stats, sizeof *stats);
151 printf("PASS: fence wire %s, bounded dispatch, refusal and FD ownership\n", mode);
152 return 0;