xref: /aosp_15_r20/external/ltp/lib/tst_epoll.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2022 SUSE LLC <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
6*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
9*49cdfc7eSAndroid Build Coastguard Worker #include "tst_epoll.h"
10*49cdfc7eSAndroid Build Coastguard Worker 
safe_epoll_create1(const char * const file,const int lineno,const int flags)11*49cdfc7eSAndroid Build Coastguard Worker int safe_epoll_create1(const char *const file, const int lineno,
12*49cdfc7eSAndroid Build Coastguard Worker 		       const int flags)
13*49cdfc7eSAndroid Build Coastguard Worker {
14*49cdfc7eSAndroid Build Coastguard Worker 	const char *flags_str;
15*49cdfc7eSAndroid Build Coastguard Worker 	int ret = epoll_create1(flags);
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker 	switch (flags) {
18*49cdfc7eSAndroid Build Coastguard Worker 	case EPOLL_CLOEXEC:
19*49cdfc7eSAndroid Build Coastguard Worker 		flags_str = "EPOLL_CLOEXEC";
20*49cdfc7eSAndroid Build Coastguard Worker 		break;
21*49cdfc7eSAndroid Build Coastguard Worker 	case 0:
22*49cdfc7eSAndroid Build Coastguard Worker 		flags_str = "";
23*49cdfc7eSAndroid Build Coastguard Worker 		break;
24*49cdfc7eSAndroid Build Coastguard Worker 	default:
25*49cdfc7eSAndroid Build Coastguard Worker 		flags_str = "???";
26*49cdfc7eSAndroid Build Coastguard Worker 	}
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1) {
29*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(file, lineno,
30*49cdfc7eSAndroid Build Coastguard Worker 			 TBROK | TERRNO, "epoll_create1(%s)", flags_str);
31*49cdfc7eSAndroid Build Coastguard Worker 	}
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
34*49cdfc7eSAndroid Build Coastguard Worker }
35*49cdfc7eSAndroid Build Coastguard Worker 
safe_epoll_ctl(const char * const file,const int lineno,int epfd,int op,int fd,struct epoll_event * ev)36*49cdfc7eSAndroid Build Coastguard Worker int safe_epoll_ctl(const char *const file, const int lineno,
37*49cdfc7eSAndroid Build Coastguard Worker 		   int epfd, int op, int fd, struct epoll_event *ev)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker 	const char *op_str;
40*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker 	switch (op) {
43*49cdfc7eSAndroid Build Coastguard Worker 	case EPOLL_CTL_ADD:
44*49cdfc7eSAndroid Build Coastguard Worker 		op_str = "EPOLL_CTL_ADD";
45*49cdfc7eSAndroid Build Coastguard Worker 		break;
46*49cdfc7eSAndroid Build Coastguard Worker 	case EPOLL_CTL_DEL:
47*49cdfc7eSAndroid Build Coastguard Worker 		op_str = "EPOLL_CTL_DEL";
48*49cdfc7eSAndroid Build Coastguard Worker 		break;
49*49cdfc7eSAndroid Build Coastguard Worker 	case EPOLL_CTL_MOD:
50*49cdfc7eSAndroid Build Coastguard Worker 		op_str = "EPOLL_CTL_MOD";
51*49cdfc7eSAndroid Build Coastguard Worker 		break;
52*49cdfc7eSAndroid Build Coastguard Worker 	default:
53*49cdfc7eSAndroid Build Coastguard Worker 		op_str = "???";
54*49cdfc7eSAndroid Build Coastguard Worker 	}
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	ret = epoll_ctl(epfd, op, fd, ev);
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1) {
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(file, lineno,
60*49cdfc7eSAndroid Build Coastguard Worker 			 TBROK | TERRNO,
61*49cdfc7eSAndroid Build Coastguard Worker 			 "epoll_ctl(%d, %s, %d, ...", epfd, op_str, fd);
62*49cdfc7eSAndroid Build Coastguard Worker 	}
63*49cdfc7eSAndroid Build Coastguard Worker 
64*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
safe_epoll_wait(const char * const file,const int lineno,int epfd,struct epoll_event * events,int maxevents,int timeout)67*49cdfc7eSAndroid Build Coastguard Worker int safe_epoll_wait(const char *const file, const int lineno,
68*49cdfc7eSAndroid Build Coastguard Worker 		    int epfd, struct epoll_event *events,
69*49cdfc7eSAndroid Build Coastguard Worker 		    int maxevents, int timeout)
70*49cdfc7eSAndroid Build Coastguard Worker {
71*49cdfc7eSAndroid Build Coastguard Worker 	int ret = epoll_wait(epfd, events, maxevents, timeout);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	if (ret == -1) {
74*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk_(file, lineno, TBROK | TERRNO,
75*49cdfc7eSAndroid Build Coastguard Worker 			 "epoll_wait(%d, ..., %d, %d)",
76*49cdfc7eSAndroid Build Coastguard Worker 			 epfd, maxevents, timeout);
77*49cdfc7eSAndroid Build Coastguard Worker 	}
78*49cdfc7eSAndroid Build Coastguard Worker 
79*49cdfc7eSAndroid Build Coastguard Worker 	return ret;
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
82