xref: /aosp_15_r20/external/ltp/include/tst_safe_macros_inline.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright (c) 2010-2024 Linux Test Project
3  * Copyright (c) 2011-2015 Cyril Hrubis <[email protected]>
4  */
5 
6 #ifndef TST_SAFE_MACROS_INLINE_H__
7 #define TST_SAFE_MACROS_INLINE_H__
8 
9 /*
10  * Following functions are inline because the behaviour may depend on
11  * -D_FILE_OFFSET_BITS=64 compile flag (type off_t or structures containing
12  *  off_t fields), see man off_t(3type).
13  *
14  * Do not add other functions here.
15  */
16 
safe_ftruncate(const char * file,const int lineno,int fd,off_t length)17 static inline int safe_ftruncate(const char *file, const int lineno,
18 	int fd, off_t length)
19 {
20 	int rval;
21 
22 	rval = ftruncate(fd, length);
23 
24 	if (rval == -1) {
25 		tst_brk_(file, lineno, TBROK | TERRNO,
26 			"ftruncate(%d,%ld) failed", fd, (long)length);
27 	} else if (rval) {
28 		tst_brk_(file, lineno, TBROK | TERRNO,
29 			"Invalid ftruncate(%d,%ld) return value %d", fd,
30 			(long)length, rval);
31 	}
32 
33 	return rval;
34 }
35 
36 #define SAFE_FTRUNCATE(fd, length) \
37 	safe_ftruncate(__FILE__, __LINE__, (fd), (length))
38 
safe_posix_fadvise(const char * file,const int lineno,int fd,off_t offset,off_t len,int advice)39 static inline int safe_posix_fadvise(const char *file, const int lineno,
40 	int fd, off_t offset, off_t len, int advice)
41 {
42 	int rval;
43 
44 	rval = posix_fadvise(fd, offset, len, advice);
45 
46 	if (rval)
47 		tst_brk_(file, lineno, TBROK,
48 			"posix_fadvise(%d,%ld,%ld,%d) failed: %s",
49 			fd, (long)offset, (long)len, advice, tst_strerrno(rval));
50 
51 	return rval;
52 }
53 
54 #define SAFE_POSIX_FADVISE(fd, offset, len, advice) \
55 	safe_posix_fadvise(__FILE__, __LINE__, (fd), (offset), (len), (advice))
56 
safe_truncate(const char * file,const int lineno,const char * path,off_t length)57 static inline int safe_truncate(const char *file, const int lineno,
58 	const char *path, off_t length)
59 {
60 	int rval;
61 
62 	rval = truncate(path, length);
63 
64 	if (rval == -1) {
65 		tst_brk_(file, lineno, TBROK | TERRNO,
66 			"truncate(%s,%ld) failed", path, (long)length);
67 	} else if (rval) {
68 		tst_brk_(file, lineno, TBROK | TERRNO,
69 			"Invalid truncate(%s,%ld) return value %d", path,
70 			(long)length, rval);
71 	}
72 
73 	return rval;
74 }
75 
76 #define SAFE_TRUNCATE(path, length) \
77 	safe_truncate(__FILE__, __LINE__, (path), (length))
78 
safe_stat(const char * file,const int lineno,const char * path,struct stat * buf)79 static inline int safe_stat(const char *file, const int lineno,
80 	const char *path, struct stat *buf)
81 {
82 	int rval;
83 
84 	rval = stat(path, buf);
85 
86 	if (rval == -1) {
87 		tst_brk_(file, lineno, TBROK | TERRNO,
88 			"stat(%s,%p) failed", path, buf);
89 	} else if (rval) {
90 		tst_brk_(file, lineno, TBROK | TERRNO,
91 			"Invalid stat(%s,%p) return value %d", path, buf,
92 			rval);
93 	}
94 
95 	return rval;
96 }
97 
98 #define SAFE_STAT(path, buf) \
99 	safe_stat(__FILE__, __LINE__, (path), (buf))
100 
safe_fstat(const char * file,const int lineno,int fd,struct stat * buf)101 static inline int safe_fstat(const char *file, const int lineno,
102 	int fd, struct stat *buf)
103 {
104 	int rval;
105 
106 	rval = fstat(fd, buf);
107 
108 	if (rval == -1) {
109 		tst_brk_(file, lineno, TBROK | TERRNO,
110 			"fstat(%d,%p) failed", fd, buf);
111 	} else if (rval) {
112 		tst_brk_(file, lineno, TBROK | TERRNO,
113 			"Invalid fstat(%d,%p) return value %d", fd, buf, rval);
114 	}
115 
116 	return rval;
117 }
118 #define SAFE_FSTAT(fd, buf) \
119 	safe_fstat(__FILE__, __LINE__, (fd), (buf))
120 
safe_lstat(const char * file,const int lineno,const char * path,struct stat * buf)121 static inline int safe_lstat(const char *file, const int lineno,
122 	const char *path, struct stat *buf)
123 {
124 	int rval;
125 
126 	rval = lstat(path, buf);
127 
128 	if (rval == -1) {
129 		tst_brk_(file, lineno, TBROK | TERRNO,
130 			"lstat(%s,%p) failed", path, buf);
131 	} else if (rval) {
132 		tst_brk_(file, lineno, TBROK | TERRNO,
133 			"Invalid lstat(%s,%p) return value %d", path, buf,
134 			rval);
135 	}
136 
137 	return rval;
138 }
139 #define SAFE_LSTAT(path, buf) \
140 	safe_lstat(__FILE__, __LINE__, (path), (buf))
141 
safe_statfs(const char * file,const int lineno,const char * path,struct statfs * buf)142 static inline int safe_statfs(const char *file, const int lineno,
143 	const char *path, struct statfs *buf)
144 {
145 	int rval;
146 
147 	rval = statfs(path, buf);
148 
149 	if (rval == -1) {
150 		tst_brk_(file, lineno, TBROK | TERRNO,
151 			"statfs(%s,%p) failed", path, buf);
152 	} else if (rval) {
153 		tst_brk_(file, lineno, TBROK | TERRNO,
154 			"Invalid statfs(%s,%p) return value %d", path, buf,
155 			rval);
156 	}
157 
158 	return rval;
159 }
160 
161 #define SAFE_STATFS(path, buf) \
162 	safe_statfs(__FILE__, __LINE__, (path), (buf))
163 
safe_lseek(const char * file,const int lineno,int fd,off_t offset,int whence)164 static inline off_t safe_lseek(const char *file, const int lineno,
165 	int fd, off_t offset, int whence)
166 {
167 	off_t rval;
168 
169 	rval = lseek(fd, offset, whence);
170 
171 	if (rval == (off_t) -1) {
172 		tst_brk_(file, lineno, TBROK | TERRNO,
173 			"lseek(%d,%ld,%d) failed", fd, (long)offset, whence);
174 	} else if (rval < 0) {
175 		tst_brk_(file, lineno, TBROK | TERRNO,
176 			"Invalid lseek(%d,%ld,%d) return value %ld", fd,
177 			(long)offset, whence, (long)rval);
178 	}
179 
180 	return rval;
181 }
182 
183 #define SAFE_LSEEK(fd, offset, whence) \
184 	safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
185 
safe_getrlimit(const char * file,const int lineno,int resource,struct rlimit * rlim)186 static inline int safe_getrlimit(const char *file, const int lineno,
187 	int resource, struct rlimit *rlim)
188 {
189 	int rval;
190 
191 	rval = getrlimit(resource, rlim);
192 
193 	if (rval == -1) {
194 		tst_brk_(file, lineno, TBROK | TERRNO,
195 			"getrlimit(%d,%p) failed", resource, rlim);
196 	} else if (rval) {
197 		tst_brk_(file, lineno, TBROK | TERRNO,
198 			"Invalid getrlimit(%d,%p) return value %d", resource,
199 			rlim, rval);
200 	}
201 
202 	return rval;
203 }
204 
205 #define SAFE_GETRLIMIT(resource, rlim) \
206 	safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
207 
safe_setrlimit(const char * file,const int lineno,int resource,const struct rlimit * rlim)208 static inline int safe_setrlimit(const char *file, const int lineno,
209 	int resource, const struct rlimit *rlim)
210 {
211 	int rval;
212 
213 	rval = setrlimit(resource, rlim);
214 
215 	if (rval == -1) {
216 		tst_brk_(file, lineno, TBROK | TERRNO,
217 			"setrlimit(%d,%p) failed", resource, rlim);
218 	} else if (rval) {
219 		tst_brk_(file, lineno, TBROK | TERRNO,
220 			"Invalid setrlimit(%d,%p) return value %d", resource,
221 			rlim, rval);
222 	}
223 
224 	return rval;
225 }
226 
227 #define SAFE_SETRLIMIT(resource, rlim) \
228 	safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
229 
230 #endif /* TST_SAFE_MACROS_INLINE_H__ */
231