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