• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Safe macros for commonly used syscalls to reduce code duplication in LTP
3  * testcases, and to ensure all errors are caught in said testcases as
4  * gracefully as possible.
5  *
6  * Also satiates some versions of gcc/glibc when the warn_unused_result
7  * attribute is applied to the function call.
8  *
9  * Licensed under the GPLv2.
10  */
11 
12 #ifndef __TEST_H__
13 #error "you must include test.h before this file"
14 #else
15 
16 #ifndef __SAFE_MACROS_H__
17 #define __SAFE_MACROS_H__
18 
19 #include "safe_macros_fn.h"
20 #include "old_safe_stdio.h"
21 #include "old_safe_net.h"
22 
23 #define SAFE_BASENAME(cleanup_fn, path)	\
24 	safe_basename(__FILE__, __LINE__, (cleanup_fn), (path))
25 
26 #define SAFE_CHDIR(cleanup_fn, path)	\
27 	safe_chdir(__FILE__, __LINE__, (cleanup_fn), (path))
28 
29 #define SAFE_CLOSE(cleanup_fn, fd) ({ \
30 		int ret = safe_close(__FILE__, __LINE__, (cleanup_fn), (fd)); \
31 		fd = -1; \
32 		ret; \
33 	})
34 
35 #define SAFE_CREAT(cleanup_fn, pathname, mode)	\
36 	safe_creat(__FILE__, __LINE__, cleanup_fn, (pathname), (mode))
37 
38 #define SAFE_DIRNAME(cleanup_fn, path)	\
39 	safe_dirname(__FILE__, __LINE__, (cleanup_fn), (path))
40 
41 #define	SAFE_GETCWD(cleanup_fn, buf, size)	\
42 	safe_getcwd(__FILE__, __LINE__, (cleanup_fn), (buf), (size))
43 
44 #define SAFE_GETPWNAM(cleanup_fn, name)	\
45 	safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name))
46 
47 #define SAFE_GETRUSAGE(cleanup_fn, who, usage) \
48 	safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage))
49 
50 #define SAFE_MALLOC(cleanup_fn, size)	\
51 	safe_malloc(__FILE__, __LINE__, (cleanup_fn), (size))
52 
53 #define SAFE_MKDIR(cleanup_fn, pathname, mode)	\
54 	safe_mkdir(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode))
55 
56 #define SAFE_RMDIR(cleanup_fn, pathname) \
57 	safe_rmdir(__FILE__, __LINE__, (cleanup_fn), (pathname))
58 
59 #define SAFE_MUNMAP(cleanup_fn, addr, length)	\
60 	safe_munmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length))
61 
62 #define SAFE_OPEN(cleanup_fn, pathname, oflags, ...)	\
63 	safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), \
64 	    ##__VA_ARGS__)
65 
66 #define SAFE_PIPE(cleanup_fn, fildes)	\
67 	safe_pipe(__FILE__, __LINE__, cleanup_fn, (fildes))
68 
69 #define SAFE_READ(cleanup_fn, len_strict, fildes, buf, nbyte)	\
70 	safe_read(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
71 	    (buf), (nbyte))
72 
73 #define SAFE_SETEGID(cleanup_fn, egid)	\
74 	safe_setegid(__FILE__, __LINE__, cleanup_fn, (egid))
75 
76 #define SAFE_SETEUID(cleanup_fn, euid)	\
77 	safe_seteuid(__FILE__, __LINE__, cleanup_fn, (euid))
78 
79 #define SAFE_SETGID(cleanup_fn, gid)	\
80 	safe_setgid(__FILE__, __LINE__, cleanup_fn, (gid))
81 
82 #define SAFE_SETUID(cleanup_fn, uid) \
83 	safe_setuid(__FILE__, __LINE__, cleanup_fn, (uid))
84 
85 #define SAFE_GETRESUID(cleanup_fn, ruid, euid, suid) \
86 	safe_getresuid(__FILE__, __LINE__, cleanup_fn, (ruid), (euid), (suid))
87 
88 #define SAFE_GETRESGID(cleanup_fn, rgid, egid, sgid) \
89 	safe_getresgid(__FILE__, __LINE__, cleanup_fn, (rgid), (egid), (sgid))
90 
91 #define SAFE_UNLINK(cleanup_fn, pathname) \
92 	safe_unlink(__FILE__, __LINE__, cleanup_fn, (pathname))
93 
94 #define SAFE_LINK(cleanup_fn, oldpath, newpath) \
95         safe_link(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath))
96 
97 #define SAFE_LINKAT(cleanup_fn, olddirfd, oldpath, newdirfd, newpath, flags) \
98 	safe_linkat(__FILE__, __LINE__, cleanup_fn, (olddirfd), (oldpath), \
99 		    (newdirfd), (newpath), (flags))
100 
101 #define SAFE_READLINK(cleanup_fn, path, buf, bufsize) \
102 	safe_readlink(__FILE__, __LINE__, cleanup_fn, (path), (buf), (bufsize))
103 
104 #define SAFE_SYMLINK(cleanup_fn, oldpath, newpath) \
105         safe_symlink(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath))
106 
107 #define SAFE_WRITE(cleanup_fn, len_strict, fildes, buf, nbyte)	\
108 	safe_write(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \
109 	    (buf), (nbyte))
110 
111 #define SAFE_STRTOL(cleanup_fn, str, min, max) \
112 	safe_strtol(__FILE__, __LINE__, cleanup_fn, (str), (min), (max))
113 
114 #define SAFE_STRTOUL(cleanup_fn, str, min, max) \
115 	safe_strtoul(__FILE__, __LINE__, cleanup_fn, (str), (min), (max))
116 
117 #define SAFE_SYSCONF(cleanup_fn, name) \
118 	safe_sysconf(__FILE__, __LINE__, cleanup_fn, name)
119 
120 #define SAFE_CHMOD(cleanup_fn, path, mode) \
121 	safe_chmod(__FILE__, __LINE__, (cleanup_fn), (path), (mode))
122 
123 #define SAFE_FCHMOD(cleanup_fn, fd, mode) \
124 	safe_fchmod(__FILE__, __LINE__, (cleanup_fn), (fd), (mode))
125 
126 #define SAFE_CHOWN(cleanup_fn, path, owner, group) \
127 	safe_chown(__FILE__, __LINE__, (cleanup_fn), (path), (owner), (group))
128 
129 #define SAFE_FCHOWN(cleanup_fn, fd, owner, group) \
130 	safe_fchown(__FILE__, __LINE__, (cleanup_fn), (fd), (owner), (group))
131 
132 #define SAFE_WAIT(cleanup_fn, status) \
133         safe_wait(__FILE__, __LINE__, (cleanup_fn), (status))
134 
135 #define SAFE_WAITPID(cleanup_fn, pid, status, opts) \
136         safe_waitpid(__FILE__, __LINE__, (cleanup_fn), (pid), (status), (opts))
137 
138 #define SAFE_KILL(cleanup_fn, pid, sig) \
139 	safe_kill(__FILE__, __LINE__, (cleanup_fn), (pid), (sig))
140 
141 #define SAFE_MEMALIGN(cleanup_fn, alignment, size) \
142 	safe_memalign(__FILE__, __LINE__, (cleanup_fn), (alignment), (size))
143 
144 #define SAFE_MKFIFO(cleanup_fn, pathname, mode) \
145 	safe_mkfifo(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode))
146 
147 #define SAFE_RENAME(cleanup_fn, oldpath, newpath) \
148 	safe_rename(__FILE__, __LINE__, (cleanup_fn), (oldpath), (newpath))
149 
150 #define SAFE_MOUNT(cleanup_fn, source, target, filesystemtype, \
151 		   mountflags, data) \
152 	safe_mount(__FILE__, __LINE__, (cleanup_fn), (source), (target), \
153 		   (filesystemtype), (mountflags), (data))
154 
155 #define SAFE_UMOUNT(cleanup_fn, target) \
156 	safe_umount(__FILE__, __LINE__, (cleanup_fn), (target))
157 
158 /*
159  * following functions are inline because the behaviour may depend on
160  * -D_FILE_OFFSET_BITS=64 -DOFF_T=__off64_t compile flags
161  */
162 
safe_mmap(const char * file,const int lineno,void (* cleanup_fn)(void),void * addr,size_t length,int prot,int flags,int fd,off_t offset)163 static inline void *safe_mmap(const char *file, const int lineno,
164 	void (*cleanup_fn)(void), void *addr, size_t length,
165 	int prot, int flags, int fd, off_t offset)
166 {
167 	void *rval;
168 
169 	rval = mmap(addr, length, prot, flags, fd, offset);
170 	if (rval == MAP_FAILED) {
171 		tst_brkm(TBROK | TERRNO, cleanup_fn,
172 			 "%s:%d: mmap(%p,%zu,%d,%d,%d,%ld) failed",
173 			 file, lineno, addr, length, prot, flags, fd,
174 			 (long) offset);
175 	}
176 
177 	return rval;
178 }
179 #define SAFE_MMAP(cleanup_fn, addr, length, prot, flags, fd, offset) \
180 	safe_mmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length), (prot), \
181 	(flags), (fd), (offset))
182 
safe_ftruncate(const char * file,const int lineno,void (cleanup_fn)(void),int fd,off_t length)183 static inline int safe_ftruncate(const char *file, const int lineno,
184 	void (cleanup_fn) (void), int fd, off_t length)
185 {
186 	int rval;
187 
188 	rval = ftruncate(fd, length);
189 	if (rval == -1) {
190 		tst_brkm(TBROK | TERRNO, cleanup_fn,
191 			 "%s:%d: ftruncate(%d,%ld) failed",
192 			 file, lineno, fd, (long)length);
193 	}
194 
195 	return rval;
196 }
197 #define SAFE_FTRUNCATE(cleanup_fn, fd, length) \
198 	safe_ftruncate(__FILE__, __LINE__, cleanup_fn, (fd), (length))
199 
safe_truncate(const char * file,const int lineno,void (cleanup_fn)(void),const char * path,off_t length)200 static inline int safe_truncate(const char *file, const int lineno,
201 	void (cleanup_fn) (void), const char *path, off_t length)
202 {
203 	int rval;
204 
205 	rval = truncate(path, length);
206 	if (rval == -1) {
207 		tst_brkm(TBROK | TERRNO, cleanup_fn,
208 			 "%s:%d: truncate(%s,%ld) failed",
209 			 file, lineno, path, (long)length);
210 	}
211 
212 	return rval;
213 }
214 #define SAFE_TRUNCATE(cleanup_fn, path, length) \
215 	safe_truncate(__FILE__, __LINE__, cleanup_fn, (path), (length))
216 
safe_stat(const char * file,const int lineno,void (cleanup_fn)(void),const char * path,struct stat * buf)217 static inline int safe_stat(const char *file, const int lineno,
218 	void (cleanup_fn)(void), const char *path, struct stat *buf)
219 {
220 	int rval;
221 
222 	rval = stat(path, buf);
223 
224 	if (rval == -1) {
225 		tst_brkm(TBROK | TERRNO, cleanup_fn,
226 			 "%s:%d: stat(%s,%p) failed", file, lineno, path, buf);
227 	}
228 
229 	return rval;
230 }
231 #define SAFE_STAT(cleanup_fn, path, buf) \
232 	safe_stat(__FILE__, __LINE__, (cleanup_fn), (path), (buf))
233 
safe_fstat(const char * file,const int lineno,void (cleanup_fn)(void),int fd,struct stat * buf)234 static inline int safe_fstat(const char *file, const int lineno,
235 	void (cleanup_fn)(void), int fd, struct stat *buf)
236 {
237 	int rval;
238 
239 	rval = fstat(fd, buf);
240 
241 	if (rval == -1) {
242 		tst_brkm(TBROK | TERRNO, cleanup_fn,
243 			 "%s:%d: fstat(%d,%p) failed", file, lineno, fd, buf);
244 	}
245 
246 	return rval;
247 }
248 #define SAFE_FSTAT(cleanup_fn, fd, buf) \
249 	safe_fstat(__FILE__, __LINE__, (cleanup_fn), (fd), (buf))
250 
safe_lstat(const char * file,const int lineno,void (cleanup_fn)(void),const char * path,struct stat * buf)251 static inline int safe_lstat(const char *file, const int lineno,
252 	void (cleanup_fn)(void), const char *path, struct stat *buf)
253 {
254 	int rval;
255 
256 	rval = lstat(path, buf);
257 
258 	if (rval == -1) {
259 		tst_brkm(TBROK | TERRNO, cleanup_fn,
260 			 "%s:%d: lstat(%s,%p) failed", file, lineno, path, buf);
261 	}
262 
263 	return rval;
264 }
265 #define SAFE_LSTAT(cleanup_fn, path, buf) \
266 	safe_lstat(__FILE__, __LINE__, (cleanup_fn), (path), (buf))
267 
safe_lseek(const char * file,const int lineno,void (cleanup_fn)(void),int fd,off_t offset,int whence)268 static inline off_t safe_lseek(const char *file, const int lineno,
269 	void (cleanup_fn)(void), int fd, off_t offset, int whence)
270 {
271 	off_t rval;
272 
273 	rval = lseek(fd, offset, whence);
274 
275 	if (rval == (off_t) -1) {
276 		tst_brkm(TBROK | TERRNO, cleanup_fn,
277 			"%s:%d: lseek(%d,%ld,%d) failed",
278 			file, lineno, fd, (long)offset, whence);
279 	}
280 
281 	return rval;
282 }
283 #define SAFE_LSEEK(cleanup_fn, fd, offset, whence) \
284 	safe_lseek(__FILE__, __LINE__, cleanup_fn, (fd), (offset), (whence))
285 
safe_getrlimit(const char * file,const int lineno,void (cleanup_fn)(void),int resource,struct rlimit * rlim)286 static inline int safe_getrlimit(const char *file, const int lineno,
287 	void (cleanup_fn)(void), int resource, struct rlimit *rlim)
288 {
289 	int rval;
290 
291 	rval = getrlimit(resource, rlim);
292 
293 	if (rval == -1) {
294 		tst_brkm(TBROK | TERRNO, cleanup_fn,
295 			 "%s:%d: getrlimit(%d,%p) failed",
296 			 file, lineno, resource, rlim);
297 	}
298 
299 	return rval;
300 }
301 #define SAFE_GETRLIMIT(cleanup_fn, resource, rlim) \
302 	safe_getrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
303 
safe_setrlimit(const char * file,const int lineno,void (cleanup_fn)(void),int resource,const struct rlimit * rlim)304 static inline int safe_setrlimit(const char *file, const int lineno,
305 	void (cleanup_fn)(void), int resource, const struct rlimit *rlim)
306 {
307 	int rval;
308 
309 	rval = setrlimit(resource, rlim);
310 
311 	if (rval == -1) {
312 		tst_brkm(TBROK | TERRNO, cleanup_fn,
313 			 "%s:%d: setrlimit(%d,%p) failed",
314 			 file, lineno, resource, rlim);
315 	}
316 
317 	return rval;
318 }
319 #define SAFE_SETRLIMIT(cleanup_fn, resource, rlim) \
320 	safe_setrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim))
321 
322 #define SAFE_OPENDIR(cleanup_fn, name) \
323 	safe_opendir(__FILE__, __LINE__, (cleanup_fn), (name))
324 
325 #define SAFE_CLOSEDIR(cleanup_fn, dirp) \
326 	safe_closedir(__FILE__, __LINE__, (cleanup_fn), (dirp))
327 
328 #define SAFE_READDIR(cleanup_fn, dirp) \
329 	safe_readdir(__FILE__, __LINE__, (cleanup_fn), (dirp))
330 
331 
332 #define SAFE_IOCTL(cleanup_fn, fd, request, ...)                   \
333 	({int ret = ioctl(fd, request, __VA_ARGS__);               \
334 	  if (ret < 0)                                             \
335 		tst_brkm(TBROK | TERRNO, cleanup_fn,               \
336 		         "ioctl(%i,%s,...) failed", fd, #request); \
337 	  ret;})
338 
339 #endif /* __SAFE_MACROS_H__ */
340 #endif /* __TEST_H__ */
341