1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker
3*49cdfc7eSAndroid Build Coastguard Worker /*
4*49cdfc7eSAndroid Build Coastguard Worker * Copyright (C) 2023 Cyril Hrubis <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker #include <sys/epoll.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <sys/eventfd.h>
11*49cdfc7eSAndroid Build Coastguard Worker #include <sys/signalfd.h>
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/timerfd.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/inotify.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <linux/perf_event.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <linux/fanotify.h>
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_safe_macros.h"
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/pidfd.h"
21*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/io_uring.h"
22*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/bpf.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fsmount.h"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #include "tst_fd.h"
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker struct tst_fd_desc {
28*49cdfc7eSAndroid Build Coastguard Worker void (*open_fd)(struct tst_fd *fd);
29*49cdfc7eSAndroid Build Coastguard Worker void (*destroy)(struct tst_fd *fd);
30*49cdfc7eSAndroid Build Coastguard Worker const char *desc;
31*49cdfc7eSAndroid Build Coastguard Worker };
32*49cdfc7eSAndroid Build Coastguard Worker
open_file(struct tst_fd * fd)33*49cdfc7eSAndroid Build Coastguard Worker static void open_file(struct tst_fd *fd)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker fd->fd = SAFE_OPEN("fd_file", O_RDWR | O_CREAT, 0666);
36*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK("fd_file");
37*49cdfc7eSAndroid Build Coastguard Worker }
38*49cdfc7eSAndroid Build Coastguard Worker
open_path(struct tst_fd * fd)39*49cdfc7eSAndroid Build Coastguard Worker static void open_path(struct tst_fd *fd)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker int tfd;
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker tfd = SAFE_CREAT("fd_file", 0666);
44*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(tfd);
45*49cdfc7eSAndroid Build Coastguard Worker
46*49cdfc7eSAndroid Build Coastguard Worker fd->fd = SAFE_OPEN("fd_file", O_PATH);
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker SAFE_UNLINK("fd_file");
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker
open_dir(struct tst_fd * fd)51*49cdfc7eSAndroid Build Coastguard Worker static void open_dir(struct tst_fd *fd)
52*49cdfc7eSAndroid Build Coastguard Worker {
53*49cdfc7eSAndroid Build Coastguard Worker SAFE_MKDIR("fd_dir", 0700);
54*49cdfc7eSAndroid Build Coastguard Worker fd->fd = SAFE_OPEN("fd_dir", O_DIRECTORY);
55*49cdfc7eSAndroid Build Coastguard Worker SAFE_RMDIR("fd_dir");
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
open_dev_zero(struct tst_fd * fd)58*49cdfc7eSAndroid Build Coastguard Worker static void open_dev_zero(struct tst_fd *fd)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker fd->fd = SAFE_OPEN("/dev/zero", O_RDONLY);
61*49cdfc7eSAndroid Build Coastguard Worker }
62*49cdfc7eSAndroid Build Coastguard Worker
open_proc_self_maps(struct tst_fd * fd)63*49cdfc7eSAndroid Build Coastguard Worker static void open_proc_self_maps(struct tst_fd *fd)
64*49cdfc7eSAndroid Build Coastguard Worker {
65*49cdfc7eSAndroid Build Coastguard Worker fd->fd = SAFE_OPEN("/proc/self/maps", O_RDONLY);
66*49cdfc7eSAndroid Build Coastguard Worker }
67*49cdfc7eSAndroid Build Coastguard Worker
open_pipe_read(struct tst_fd * fd)68*49cdfc7eSAndroid Build Coastguard Worker static void open_pipe_read(struct tst_fd *fd)
69*49cdfc7eSAndroid Build Coastguard Worker {
70*49cdfc7eSAndroid Build Coastguard Worker int pipe[2];
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(pipe);
73*49cdfc7eSAndroid Build Coastguard Worker fd->fd = pipe[0];
74*49cdfc7eSAndroid Build Coastguard Worker fd->priv = pipe[1];
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
open_pipe_write(struct tst_fd * fd)77*49cdfc7eSAndroid Build Coastguard Worker static void open_pipe_write(struct tst_fd *fd)
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker int pipe[2];
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker SAFE_PIPE(pipe);
82*49cdfc7eSAndroid Build Coastguard Worker fd->fd = pipe[1];
83*49cdfc7eSAndroid Build Coastguard Worker fd->priv = pipe[0];
84*49cdfc7eSAndroid Build Coastguard Worker }
85*49cdfc7eSAndroid Build Coastguard Worker
destroy_pipe(struct tst_fd * fd)86*49cdfc7eSAndroid Build Coastguard Worker static void destroy_pipe(struct tst_fd *fd)
87*49cdfc7eSAndroid Build Coastguard Worker {
88*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd->priv);
89*49cdfc7eSAndroid Build Coastguard Worker }
90*49cdfc7eSAndroid Build Coastguard Worker
open_unix_sock(struct tst_fd * fd)91*49cdfc7eSAndroid Build Coastguard Worker static void open_unix_sock(struct tst_fd *fd)
92*49cdfc7eSAndroid Build Coastguard Worker {
93*49cdfc7eSAndroid Build Coastguard Worker fd->fd = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker
open_inet_sock(struct tst_fd * fd)96*49cdfc7eSAndroid Build Coastguard Worker static void open_inet_sock(struct tst_fd *fd)
97*49cdfc7eSAndroid Build Coastguard Worker {
98*49cdfc7eSAndroid Build Coastguard Worker fd->fd = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
open_epoll(struct tst_fd * fd)101*49cdfc7eSAndroid Build Coastguard Worker static void open_epoll(struct tst_fd *fd)
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker fd->fd = epoll_create(1);
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0)
106*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO, "epoll_create()");
107*49cdfc7eSAndroid Build Coastguard Worker }
108*49cdfc7eSAndroid Build Coastguard Worker
open_eventfd(struct tst_fd * fd)109*49cdfc7eSAndroid Build Coastguard Worker static void open_eventfd(struct tst_fd *fd)
110*49cdfc7eSAndroid Build Coastguard Worker {
111*49cdfc7eSAndroid Build Coastguard Worker fd->fd = eventfd(0, 0);
112*49cdfc7eSAndroid Build Coastguard Worker
113*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0)
114*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO, "Skipping %s", tst_fd_desc(fd));
115*49cdfc7eSAndroid Build Coastguard Worker }
116*49cdfc7eSAndroid Build Coastguard Worker
open_signalfd(struct tst_fd * fd)117*49cdfc7eSAndroid Build Coastguard Worker static void open_signalfd(struct tst_fd *fd)
118*49cdfc7eSAndroid Build Coastguard Worker {
119*49cdfc7eSAndroid Build Coastguard Worker sigset_t sfd_mask;
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker sigemptyset(&sfd_mask);
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker fd->fd = signalfd(-1, &sfd_mask, 0);
124*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
125*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
126*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
127*49cdfc7eSAndroid Build Coastguard Worker }
128*49cdfc7eSAndroid Build Coastguard Worker }
129*49cdfc7eSAndroid Build Coastguard Worker
open_timerfd(struct tst_fd * fd)130*49cdfc7eSAndroid Build Coastguard Worker static void open_timerfd(struct tst_fd *fd)
131*49cdfc7eSAndroid Build Coastguard Worker {
132*49cdfc7eSAndroid Build Coastguard Worker fd->fd = timerfd_create(CLOCK_REALTIME, 0);
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
135*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
136*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker }
139*49cdfc7eSAndroid Build Coastguard Worker
open_pidfd(struct tst_fd * fd)140*49cdfc7eSAndroid Build Coastguard Worker static void open_pidfd(struct tst_fd *fd)
141*49cdfc7eSAndroid Build Coastguard Worker {
142*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_pidfd_open, getpid(), 0);
143*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0)
144*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO, "pidfd_open()");
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker
open_fanotify(struct tst_fd * fd)147*49cdfc7eSAndroid Build Coastguard Worker static void open_fanotify(struct tst_fd *fd)
148*49cdfc7eSAndroid Build Coastguard Worker {
149*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_fanotify_init, FAN_CLASS_NOTIF, O_RDONLY);
150*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
151*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
152*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker }
155*49cdfc7eSAndroid Build Coastguard Worker
open_inotify(struct tst_fd * fd)156*49cdfc7eSAndroid Build Coastguard Worker static void open_inotify(struct tst_fd *fd)
157*49cdfc7eSAndroid Build Coastguard Worker {
158*49cdfc7eSAndroid Build Coastguard Worker fd->fd = inotify_init();
159*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
160*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
161*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
162*49cdfc7eSAndroid Build Coastguard Worker }
163*49cdfc7eSAndroid Build Coastguard Worker }
164*49cdfc7eSAndroid Build Coastguard Worker
open_userfaultfd(struct tst_fd * fd)165*49cdfc7eSAndroid Build Coastguard Worker static void open_userfaultfd(struct tst_fd *fd)
166*49cdfc7eSAndroid Build Coastguard Worker {
167*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_userfaultfd, 0);
168*49cdfc7eSAndroid Build Coastguard Worker
169*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
170*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
171*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
172*49cdfc7eSAndroid Build Coastguard Worker }
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker
open_perf_event(struct tst_fd * fd)175*49cdfc7eSAndroid Build Coastguard Worker static void open_perf_event(struct tst_fd *fd)
176*49cdfc7eSAndroid Build Coastguard Worker {
177*49cdfc7eSAndroid Build Coastguard Worker struct perf_event_attr pe_attr = {
178*49cdfc7eSAndroid Build Coastguard Worker .type = PERF_TYPE_SOFTWARE,
179*49cdfc7eSAndroid Build Coastguard Worker .size = sizeof(struct perf_event_attr),
180*49cdfc7eSAndroid Build Coastguard Worker .config = PERF_COUNT_SW_CPU_CLOCK,
181*49cdfc7eSAndroid Build Coastguard Worker .disabled = 1,
182*49cdfc7eSAndroid Build Coastguard Worker .exclude_kernel = 1,
183*49cdfc7eSAndroid Build Coastguard Worker .exclude_hv = 1,
184*49cdfc7eSAndroid Build Coastguard Worker };
185*49cdfc7eSAndroid Build Coastguard Worker
186*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_perf_event_open, &pe_attr, 0, -1, -1, 0);
187*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
188*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
189*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker }
192*49cdfc7eSAndroid Build Coastguard Worker
open_io_uring(struct tst_fd * fd)193*49cdfc7eSAndroid Build Coastguard Worker static void open_io_uring(struct tst_fd *fd)
194*49cdfc7eSAndroid Build Coastguard Worker {
195*49cdfc7eSAndroid Build Coastguard Worker struct io_uring_params uring_params = {};
196*49cdfc7eSAndroid Build Coastguard Worker
197*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_io_uring_setup, 1, &uring_params);
198*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
199*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
200*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
201*49cdfc7eSAndroid Build Coastguard Worker }
202*49cdfc7eSAndroid Build Coastguard Worker }
203*49cdfc7eSAndroid Build Coastguard Worker
open_bpf_map(struct tst_fd * fd)204*49cdfc7eSAndroid Build Coastguard Worker static void open_bpf_map(struct tst_fd *fd)
205*49cdfc7eSAndroid Build Coastguard Worker {
206*49cdfc7eSAndroid Build Coastguard Worker union bpf_attr array_attr = {
207*49cdfc7eSAndroid Build Coastguard Worker .map_type = BPF_MAP_TYPE_ARRAY,
208*49cdfc7eSAndroid Build Coastguard Worker .key_size = 4,
209*49cdfc7eSAndroid Build Coastguard Worker .value_size = 8,
210*49cdfc7eSAndroid Build Coastguard Worker .max_entries = 1,
211*49cdfc7eSAndroid Build Coastguard Worker };
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_bpf, BPF_MAP_CREATE, &array_attr, sizeof(array_attr));
214*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
215*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
216*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
217*49cdfc7eSAndroid Build Coastguard Worker }
218*49cdfc7eSAndroid Build Coastguard Worker }
219*49cdfc7eSAndroid Build Coastguard Worker
open_fsopen(struct tst_fd * fd)220*49cdfc7eSAndroid Build Coastguard Worker static void open_fsopen(struct tst_fd *fd)
221*49cdfc7eSAndroid Build Coastguard Worker {
222*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_fsopen, "ext2", 0);
223*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
224*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
225*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
226*49cdfc7eSAndroid Build Coastguard Worker }
227*49cdfc7eSAndroid Build Coastguard Worker }
228*49cdfc7eSAndroid Build Coastguard Worker
open_fspick(struct tst_fd * fd)229*49cdfc7eSAndroid Build Coastguard Worker static void open_fspick(struct tst_fd *fd)
230*49cdfc7eSAndroid Build Coastguard Worker {
231*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_fspick, AT_FDCWD, "/", 0);
232*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
233*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
234*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
235*49cdfc7eSAndroid Build Coastguard Worker }
236*49cdfc7eSAndroid Build Coastguard Worker }
237*49cdfc7eSAndroid Build Coastguard Worker
open_open_tree(struct tst_fd * fd)238*49cdfc7eSAndroid Build Coastguard Worker static void open_open_tree(struct tst_fd *fd)
239*49cdfc7eSAndroid Build Coastguard Worker {
240*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_open_tree, AT_FDCWD, "/", 0);
241*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
242*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
243*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
244*49cdfc7eSAndroid Build Coastguard Worker }
245*49cdfc7eSAndroid Build Coastguard Worker }
246*49cdfc7eSAndroid Build Coastguard Worker
open_memfd(struct tst_fd * fd)247*49cdfc7eSAndroid Build Coastguard Worker static void open_memfd(struct tst_fd *fd)
248*49cdfc7eSAndroid Build Coastguard Worker {
249*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_memfd_create, "ltp_memfd", 0);
250*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
251*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
252*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
253*49cdfc7eSAndroid Build Coastguard Worker }
254*49cdfc7eSAndroid Build Coastguard Worker }
255*49cdfc7eSAndroid Build Coastguard Worker
open_memfd_secret(struct tst_fd * fd)256*49cdfc7eSAndroid Build Coastguard Worker static void open_memfd_secret(struct tst_fd *fd)
257*49cdfc7eSAndroid Build Coastguard Worker {
258*49cdfc7eSAndroid Build Coastguard Worker fd->fd = syscall(__NR_memfd_secret, 0);
259*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd < 0) {
260*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF | TERRNO,
261*49cdfc7eSAndroid Build Coastguard Worker "Skipping %s", tst_fd_desc(fd));
262*49cdfc7eSAndroid Build Coastguard Worker }
263*49cdfc7eSAndroid Build Coastguard Worker }
264*49cdfc7eSAndroid Build Coastguard Worker
265*49cdfc7eSAndroid Build Coastguard Worker static struct tst_fd_desc fd_desc[] = {
266*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_FILE] = {.open_fd = open_file, .desc = "file"},
267*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_PATH] = {.open_fd = open_path, .desc = "O_PATH file"},
268*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_DIR] = {.open_fd = open_dir, .desc = "directory"},
269*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_DEV_ZERO] = {.open_fd = open_dev_zero, .desc = "/dev/zero"},
270*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_PROC_MAPS] = {.open_fd = open_proc_self_maps, .desc = "/proc/self/maps"},
271*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_PIPE_READ] = {.open_fd = open_pipe_read, .desc = "pipe read end", .destroy = destroy_pipe},
272*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_PIPE_WRITE] = {.open_fd = open_pipe_write, .desc = "pipe write end", .destroy = destroy_pipe},
273*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_UNIX_SOCK] = {.open_fd = open_unix_sock, .desc = "unix socket"},
274*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_INET_SOCK] = {.open_fd = open_inet_sock, .desc = "inet socket"},
275*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_EPOLL] = {.open_fd = open_epoll, .desc = "epoll"},
276*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_EVENTFD] = {.open_fd = open_eventfd, .desc = "eventfd"},
277*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_SIGNALFD] = {.open_fd = open_signalfd, .desc = "signalfd"},
278*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_TIMERFD] = {.open_fd = open_timerfd, .desc = "timerfd"},
279*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_PIDFD] = {.open_fd = open_pidfd, .desc = "pidfd"},
280*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_FANOTIFY] = {.open_fd = open_fanotify, .desc = "fanotify"},
281*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_INOTIFY] = {.open_fd = open_inotify, .desc = "inotify"},
282*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_USERFAULTFD] = {.open_fd = open_userfaultfd, .desc = "userfaultfd"},
283*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_PERF_EVENT] = {.open_fd = open_perf_event, .desc = "perf event"},
284*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_IO_URING] = {.open_fd = open_io_uring, .desc = "io uring"},
285*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_BPF_MAP] = {.open_fd = open_bpf_map, .desc = "bpf map"},
286*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_FSOPEN] = {.open_fd = open_fsopen, .desc = "fsopen"},
287*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_FSPICK] = {.open_fd = open_fspick, .desc = "fspick"},
288*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_OPEN_TREE] = {.open_fd = open_open_tree, .desc = "open_tree"},
289*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_MEMFD] = {.open_fd = open_memfd, .desc = "memfd"},
290*49cdfc7eSAndroid Build Coastguard Worker [TST_FD_MEMFD_SECRET] = {.open_fd = open_memfd_secret, .desc = "memfd secret"},
291*49cdfc7eSAndroid Build Coastguard Worker };
292*49cdfc7eSAndroid Build Coastguard Worker
tst_fd_desc(struct tst_fd * fd)293*49cdfc7eSAndroid Build Coastguard Worker const char *tst_fd_desc(struct tst_fd *fd)
294*49cdfc7eSAndroid Build Coastguard Worker {
295*49cdfc7eSAndroid Build Coastguard Worker if (fd->type >= ARRAY_SIZE(fd_desc))
296*49cdfc7eSAndroid Build Coastguard Worker return "invalid";
297*49cdfc7eSAndroid Build Coastguard Worker
298*49cdfc7eSAndroid Build Coastguard Worker return fd_desc[fd->type].desc;
299*49cdfc7eSAndroid Build Coastguard Worker }
300*49cdfc7eSAndroid Build Coastguard Worker
tst_fd_next(struct tst_fd * fd)301*49cdfc7eSAndroid Build Coastguard Worker int tst_fd_next(struct tst_fd *fd)
302*49cdfc7eSAndroid Build Coastguard Worker {
303*49cdfc7eSAndroid Build Coastguard Worker size_t len = ARRAY_SIZE(fd_desc);
304*49cdfc7eSAndroid Build Coastguard Worker
305*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd >= 0) {
306*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd->fd);
307*49cdfc7eSAndroid Build Coastguard Worker
308*49cdfc7eSAndroid Build Coastguard Worker if (fd_desc[fd->type].destroy)
309*49cdfc7eSAndroid Build Coastguard Worker fd_desc[fd->type].destroy(fd);
310*49cdfc7eSAndroid Build Coastguard Worker
311*49cdfc7eSAndroid Build Coastguard Worker fd->type++;
312*49cdfc7eSAndroid Build Coastguard Worker }
313*49cdfc7eSAndroid Build Coastguard Worker
314*49cdfc7eSAndroid Build Coastguard Worker for (;;) {
315*49cdfc7eSAndroid Build Coastguard Worker if (fd->type >= len)
316*49cdfc7eSAndroid Build Coastguard Worker return 0;
317*49cdfc7eSAndroid Build Coastguard Worker
318*49cdfc7eSAndroid Build Coastguard Worker fd_desc[fd->type].open_fd(fd);
319*49cdfc7eSAndroid Build Coastguard Worker
320*49cdfc7eSAndroid Build Coastguard Worker if (fd->fd >= 0)
321*49cdfc7eSAndroid Build Coastguard Worker return 1;
322*49cdfc7eSAndroid Build Coastguard Worker
323*49cdfc7eSAndroid Build Coastguard Worker fd->type++;
324*49cdfc7eSAndroid Build Coastguard Worker }
325*49cdfc7eSAndroid Build Coastguard Worker }
326