• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright (c) 2010-2018 Linux Test Project
3  * Copyright (c) 2011-2015 Cyril Hrubis <chrubis@suse.cz>
4  */
5 
6 #ifndef TST_SAFE_MACROS_H__
7 #define TST_SAFE_MACROS_H__
8 
9 #include <sys/mman.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sys/resource.h>
13 #include <sys/stat.h>
14 #include <sys/vfs.h>
15 #include <sys/sysinfo.h>
16 #include <fcntl.h>
17 #include <libgen.h>
18 #include <signal.h>
19 #include <stdarg.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <grp.h>
23 
24 #include "safe_macros_fn.h"
25 #include "tst_cmd.h"
26 
27 int safe_access(const char *filename, const int lineno, const char *pathname,
28 		   int mode);
29 #define SAFE_ACCESS(path, mode) \
30 	safe_access(__FILE__, __LINE__, (path), (mode))
31 
32 #define SAFE_BASENAME(path) \
33 	safe_basename(__FILE__, __LINE__, NULL, (path))
34 
35 #define SAFE_CHDIR(path) \
36 	safe_chdir(__FILE__, __LINE__, NULL, (path))
37 
38 #define SAFE_CLOSE(fd) do { \
39 		safe_close(__FILE__, __LINE__, NULL, (fd)); \
40 		fd = -1; \
41 	} while (0)
42 
43 #define SAFE_CREAT(pathname, mode) \
44 	safe_creat(__FILE__, __LINE__, NULL, (pathname), (mode))
45 
46 #define SAFE_CHROOT(path) \
47 	safe_chroot(__FILE__, __LINE__, (path))
48 int safe_chroot(const char *file, const int lineno, const char *path);
49 
50 #define SAFE_DIRNAME(path) \
51 	safe_dirname(__FILE__, __LINE__, NULL, (path))
52 
53 int safe_dup(const char *file, const int lineno, int oldfd);
54 
55 #define SAFE_DUP(oldfd) \
56 	safe_dup(__FILE__, __LINE__, (oldfd))
57 
58 int safe_dup2(const char *file, const int lineno, int oldfd, int newfd);
59 
60 #define SAFE_DUP2(oldfd, newfd)			\
61 	safe_dup2(__FILE__, __LINE__, (oldfd), (newfd))
62 
63 #define SAFE_GETCWD(buf, size) \
64 	safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
65 
66 #define SAFE_GETPWNAM(name) \
67 	safe_getpwnam(__FILE__, __LINE__, NULL, (name))
68 
69 #define SAFE_GETRUSAGE(who, usage) \
70 	safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
71 
72 #define SAFE_MALLOC(size) \
73 	safe_malloc(__FILE__, __LINE__, NULL, (size))
74 
75 void *safe_realloc(const char *file, const int lineno, void *ptr, size_t size);
76 
77 #define SAFE_REALLOC(ptr, size) \
78 	safe_realloc(__FILE__, __LINE__, (ptr), (size))
79 
80 #define SAFE_MKDIR(pathname, mode) \
81 	safe_mkdir(__FILE__, __LINE__, NULL, (pathname), (mode))
82 
83 #define SAFE_RMDIR(pathname) \
84 	safe_rmdir(__FILE__, __LINE__, NULL, (pathname))
85 
86 #define SAFE_MUNMAP(addr, length) \
87 	safe_munmap(__FILE__, __LINE__, NULL, (addr), (length))
88 
89 #define SAFE_OPEN(pathname, oflags, ...) \
90 	safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), \
91 	    ##__VA_ARGS__)
92 
93 #define SAFE_PIPE(fildes) \
94 	safe_pipe(__FILE__, __LINE__, NULL, (fildes))
95 
96 int safe_pipe2(const char *file, const int lineno, int fildes[2], int flags);
97 
98 #define SAFE_PIPE2(fildes, flags) \
99 	safe_pipe2(__FILE__, __LINE__, (fildes), (flags))
100 
101 #define SAFE_READ(len_strict, fildes, buf, nbyte) \
102 	safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
103 
104 #define SAFE_SETEGID(egid) \
105 	safe_setegid(__FILE__, __LINE__, NULL, (egid))
106 
107 #define SAFE_SETEUID(euid) \
108 	safe_seteuid(__FILE__, __LINE__, NULL, (euid))
109 
110 #define SAFE_SETGID(gid) \
111 	safe_setgid(__FILE__, __LINE__, NULL, (gid))
112 
113 #define SAFE_SETUID(uid) \
114 	safe_setuid(__FILE__, __LINE__, NULL, (uid))
115 
116 int safe_setregid(const char *file, const int lineno,
117 		  gid_t rgid, gid_t egid);
118 
119 #define SAFE_SETREGID(rgid, egid) \
120 	safe_setregid(__FILE__, __LINE__, (rgid), (egid))
121 
122 int safe_setreuid(const char *file, const int lineno,
123 		  uid_t ruid, uid_t euid);
124 
125 #define SAFE_SETREUID(ruid, euid) \
126 	safe_setreuid(__FILE__, __LINE__, (ruid), (euid))
127 
128 int safe_setresgid(const char *file, const int lineno,
129 	gid_t rgid, gid_t egid, gid_t sgid);
130 #define SAFE_SETRESGID(rgid, egid, sgid) \
131 	safe_setresgid(__FILE__, __LINE__, (rgid), (egid), (sgid))
132 
133 int safe_setresuid(const char *file, const int lineno,
134 		  uid_t ruid, uid_t euid, uid_t suid);
135 #define SAFE_SETRESUID(ruid, euid, suid) \
136 	safe_setresuid(__FILE__, __LINE__, (ruid), (euid), (suid))
137 
138 #define SAFE_GETRESUID(ruid, euid, suid) \
139 	safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid))
140 
141 #define SAFE_GETRESGID(rgid, egid, sgid) \
142 	safe_getresgid(__FILE__, __LINE__, NULL, (rgid), (egid), (sgid))
143 
144 int safe_setpgid(const char *file, const int lineno, pid_t pid, pid_t pgid);
145 
146 #define SAFE_SETPGID(pid, pgid) \
147 	safe_setpgid(__FILE__, __LINE__, (pid), (pgid))
148 
149 pid_t safe_getpgid(const char *file, const int lineno, pid_t pid);
150 
151 #define SAFE_GETPGID(pid) \
152 	safe_getpgid(__FILE__, __LINE__, (pid))
153 
154 int safe_setgroups(const char *file, const int lineno, size_t size, const gid_t *list);
155 
156 #define SAFE_SETGROUPS(size, list) \
157 	safe_setgroups(__FILE__, __LINE__, (size), (list))
158 
159 int safe_getgroups(const char *file, const int lineno, int size, gid_t list[]);
160 
161 #define SAFE_GETGROUPS(size, list) \
162 	safe_getgroups(__FILE__, __LINE__, (size), (list))
163 
164 #define SAFE_UNLINK(pathname) \
165 	safe_unlink(__FILE__, __LINE__, NULL, (pathname))
166 
167 #define SAFE_LINK(oldpath, newpath) \
168 	safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath))
169 
170 #define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \
171 	safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \
172 		    (newdirfd), (newpath), (flags))
173 
174 #define SAFE_READLINK(path, buf, bufsize) \
175 	safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize))
176 
177 #define SAFE_SYMLINK(oldpath, newpath) \
178 	safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))
179 
180 #define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
181 	safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
182 
183 #define SAFE_STRTOL(str, min, max) \
184 	safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max))
185 
186 #define SAFE_STRTOUL(str, min, max) \
187 	safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max))
188 
189 #define SAFE_STRTOF(str, min, max) \
190 	safe_strtof(__FILE__, __LINE__, NULL, (str), (min), (max))
191 
192 #define SAFE_SYSCONF(name) \
193 	safe_sysconf(__FILE__, __LINE__, NULL, name)
194 
195 #define SAFE_CHMOD(path, mode) \
196 	safe_chmod(__FILE__, __LINE__, NULL, (path), (mode))
197 
198 #define SAFE_FCHMOD(fd, mode) \
199 	safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode))
200 
201 #define SAFE_CHOWN(path, owner, group) \
202 	safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group))
203 
204 #define SAFE_FCHOWN(fd, owner, group) \
205 	safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
206 
207 #define SAFE_WAIT(status) \
208 	safe_wait(__FILE__, __LINE__, NULL, (status))
209 
210 #define SAFE_WAITPID(pid, status, opts) \
211 	safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts))
212 
213 #define SAFE_KILL(pid, sig) \
214 	safe_kill(__FILE__, __LINE__, NULL, (pid), (sig))
215 
216 #define SAFE_MEMALIGN(alignment, size) \
217 	safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size))
218 
219 #define SAFE_MKFIFO(pathname, mode) \
220 	safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode))
221 
222 #define SAFE_RENAME(oldpath, newpath) \
223 	safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath))
224 
225 #define SAFE_MOUNT(source, target, filesystemtype, \
226 		   mountflags, data) \
227 	safe_mount(__FILE__, __LINE__, NULL, (source), (target), \
228 		   (filesystemtype), (mountflags), (data))
229 
230 #define SAFE_UMOUNT(target) \
231 	safe_umount(__FILE__, __LINE__, NULL, (target))
232 
233 #define SAFE_OPENDIR(name) \
234 	safe_opendir(__FILE__, __LINE__, NULL, (name))
235 
236 #define SAFE_CLOSEDIR(dirp) \
237 	safe_closedir(__FILE__, __LINE__, NULL, (dirp))
238 
239 #define SAFE_READDIR(dirp) \
240 	safe_readdir(__FILE__, __LINE__, NULL, (dirp))
241 
242 #define SAFE_IOCTL_(file, lineno, fd, request, ...)          \
243 	({int tst_ret_ = ioctl(fd, request, ##__VA_ARGS__);  \
244 	  tst_ret_ < 0 ?                                     \
245 	   tst_brk_((file), (lineno), TBROK | TERRNO,        \
246 	            "ioctl(%i,%s,...) failed", fd, #request), 0 \
247 	 : tst_ret_;})
248 
249 #define SAFE_IOCTL(fd, request, ...) \
250 	SAFE_IOCTL_(__FILE__, __LINE__, (fd), (request), ##__VA_ARGS__)
251 
252 #define SAFE_FCNTL(fd, cmd, ...)                            \
253 	({int tst_ret_ = fcntl(fd, cmd, ##__VA_ARGS__);     \
254 	  tst_ret_ == -1 ?                                  \
255 	   tst_brk(TBROK | TERRNO,                          \
256 	            "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
257 	 : tst_ret_;})
258 
259 /*
260  * following functions are inline because the behaviour may depend on
261  * -D_FILE_OFFSET_BITS=64 compile flag
262  */
263 
safe_mmap(const char * file,const int lineno,void * addr,size_t length,int prot,int flags,int fd,off_t offset)264 static inline void *safe_mmap(const char *file, const int lineno,
265                               void *addr, size_t length,
266                               int prot, int flags, int fd, off_t offset)
267 {
268 	void *rval;
269 
270 	rval = mmap(addr, length, prot, flags, fd, offset);
271 	if (rval == MAP_FAILED) {
272 		tst_brk_(file, lineno, TBROK | TERRNO,
273 			"mmap(%p,%zu,%d,%d,%d,%ld) failed",
274 			addr, length, prot, flags, fd, (long) offset);
275 	}
276 
277 	return rval;
278 }
279 #define SAFE_MMAP(addr, length, prot, flags, fd, offset) \
280 	safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \
281 	(flags), (fd), (offset))
282 
safe_ftruncate(const char * file,const int lineno,int fd,off_t length)283 static inline int safe_ftruncate(const char *file, const int lineno,
284                                  int fd, off_t length)
285 {
286 	int rval;
287 
288 	rval = ftruncate(fd, length);
289 
290 	if (rval == -1) {
291 		tst_brk_(file, lineno, TBROK | TERRNO,
292 			"ftruncate(%d,%ld) failed", fd, (long)length);
293 	} else if (rval) {
294 		tst_brk_(file, lineno, TBROK | TERRNO,
295 			"Invalid ftruncate(%d,%ld) return value %d", fd,
296 			(long)length, rval);
297 	}
298 
299 	return rval;
300 }
301 #define SAFE_FTRUNCATE(fd, length) \
302 	safe_ftruncate(__FILE__, __LINE__, (fd), (length))
303 
safe_posix_fadvise(const char * file,const int lineno,int fd,off_t offset,off_t len,int advice)304 static inline int safe_posix_fadvise(const char *file, const int lineno,
305                                 int fd, off_t offset, off_t len, int advice)
306 {
307 	int rval;
308 
309 	rval = posix_fadvise(fd, offset, len, advice);
310 
311 	if (rval)
312 		tst_brk_(file, lineno, TBROK,
313 			"posix_fadvise(%d,%ld,%ld,%d) failed: %s",
314 			fd, (long)offset, (long)len, advice, tst_strerrno(rval));
315 
316 	return rval;
317 }
318 #define SAFE_POSIX_FADVISE(fd, offset, len, advice) \
319 	safe_posix_fadvise(__FILE__, __LINE__, (fd), (offset), (len), (advice))
320 
safe_truncate(const char * file,const int lineno,const char * path,off_t length)321 static inline int safe_truncate(const char *file, const int lineno,
322                                 const char *path, off_t length)
323 {
324 	int rval;
325 
326 	rval = truncate(path, length);
327 
328 	if (rval == -1) {
329 		tst_brk_(file, lineno, TBROK | TERRNO,
330 			"truncate(%s,%ld) failed", path, (long)length);
331 	} else if (rval) {
332 		tst_brk_(file, lineno, TBROK | TERRNO,
333 			"Invalid truncate(%s,%ld) return value %d", path,
334 			(long)length, rval);
335 	}
336 
337 	return rval;
338 }
339 #define SAFE_TRUNCATE(path, length) \
340 	safe_truncate(__FILE__, __LINE__, (path), (length))
341 
safe_stat(const char * file,const int lineno,const char * path,struct stat * buf)342 static inline int safe_stat(const char *file, const int lineno,
343                             const char *path, struct stat *buf)
344 {
345 	int rval;
346 
347 	rval = stat(path, buf);
348 
349 	if (rval == -1) {
350 		tst_brk_(file, lineno, TBROK | TERRNO,
351 			"stat(%s,%p) failed", path, buf);
352 	} else if (rval) {
353 		tst_brk_(file, lineno, TBROK | TERRNO,
354 			"Invalid stat(%s,%p) return value %d", path, buf,
355 			rval);
356 	}
357 
358 	return rval;
359 }
360 #define SAFE_STAT(path, buf) \
361 	safe_stat(__FILE__, __LINE__, (path), (buf))
362 
safe_fstat(const char * file,const int lineno,int fd,struct stat * buf)363 static inline int safe_fstat(const char *file, const int lineno,
364                              int fd, struct stat *buf)
365 {
366 	int rval;
367 
368 	rval = fstat(fd, buf);
369 
370 	if (rval == -1) {
371 		tst_brk_(file, lineno, TBROK | TERRNO,
372 			"fstat(%d,%p) failed", fd, buf);
373 	} else if (rval) {
374 		tst_brk_(file, lineno, TBROK | TERRNO,
375 			"Invalid fstat(%d,%p) return value %d", fd, buf, rval);
376 	}
377 
378 	return rval;
379 }
380 #define SAFE_FSTAT(fd, buf) \
381 	safe_fstat(__FILE__, __LINE__, (fd), (buf))
382 
safe_lstat(const char * file,const int lineno,const char * path,struct stat * buf)383 static inline int safe_lstat(const char *file, const int lineno,
384 	const char *path, struct stat *buf)
385 {
386 	int rval;
387 
388 	rval = lstat(path, buf);
389 
390 	if (rval == -1) {
391 		tst_brk_(file, lineno, TBROK | TERRNO,
392 			"lstat(%s,%p) failed", path, buf);
393 	} else if (rval) {
394 		tst_brk_(file, lineno, TBROK | TERRNO,
395 			"Invalid lstat(%s,%p) return value %d", path, buf,
396 			rval);
397 	}
398 
399 	return rval;
400 }
401 #define SAFE_LSTAT(path, buf) \
402 	safe_lstat(__FILE__, __LINE__, (path), (buf))
403 
safe_statfs(const char * file,const int lineno,const char * path,struct statfs * buf)404 static inline int safe_statfs(const char *file, const int lineno,
405                               const char *path, struct statfs *buf)
406 {
407 	int rval;
408 
409 	rval = statfs(path, buf);
410 
411 	if (rval == -1) {
412 		tst_brk_(file, lineno, TBROK | TERRNO,
413 			"statfs(%s,%p) failed", path, buf);
414 	} else if (rval) {
415 		tst_brk_(file, lineno, TBROK | TERRNO,
416 			"Invalid statfs(%s,%p) return value %d", path, buf,
417 			rval);
418 	}
419 
420 	return rval;
421 }
422 #define SAFE_STATFS(path, buf) \
423 	safe_statfs(__FILE__, __LINE__, (path), (buf))
424 
safe_lseek(const char * file,const int lineno,int fd,off_t offset,int whence)425 static inline off_t safe_lseek(const char *file, const int lineno,
426                                int fd, off_t offset, int whence)
427 {
428 	off_t rval;
429 
430 	rval = lseek(fd, offset, whence);
431 
432 	if (rval == (off_t) -1) {
433 		tst_brk_(file, lineno, TBROK | TERRNO,
434 			"lseek(%d,%ld,%d) failed", fd, (long)offset, whence);
435 	} else if (rval < 0) {
436 		tst_brk_(file, lineno, TBROK | TERRNO,
437 			"Invalid lseek(%d,%ld,%d) return value %ld", fd,
438 			(long)offset, whence, (long)rval);
439 	}
440 
441 	return rval;
442 }
443 #define SAFE_LSEEK(fd, offset, whence) \
444 	safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
445 
safe_getrlimit(const char * file,const int lineno,int resource,struct rlimit * rlim)446 static inline int safe_getrlimit(const char *file, const int lineno,
447                                  int resource, struct rlimit *rlim)
448 {
449 	int rval;
450 
451 	rval = getrlimit(resource, rlim);
452 
453 	if (rval == -1) {
454 		tst_brk_(file, lineno, TBROK | TERRNO,
455 			"getrlimit(%d,%p) failed", resource, rlim);
456 	} else if (rval) {
457 		tst_brk_(file, lineno, TBROK | TERRNO,
458 			"Invalid getrlimit(%d,%p) return value %d", resource,
459 			rlim, rval);
460 	}
461 
462 	return rval;
463 }
464 #define SAFE_GETRLIMIT(resource, rlim) \
465 	safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
466 
safe_setrlimit(const char * file,const int lineno,int resource,const struct rlimit * rlim)467 static inline int safe_setrlimit(const char *file, const int lineno,
468                                  int resource, const struct rlimit *rlim)
469 {
470 	int rval;
471 
472 	rval = setrlimit(resource, rlim);
473 
474 	if (rval == -1) {
475 		tst_brk_(file, lineno, TBROK | TERRNO,
476 			"setrlimit(%d,%p) failed", resource, rlim);
477 	} else if (rval) {
478 		tst_brk_(file, lineno, TBROK | TERRNO,
479 			"Invalid setrlimit(%d,%p) return value %d", resource,
480 			rlim, rval);
481 	}
482 
483 	return rval;
484 }
485 #define SAFE_SETRLIMIT(resource, rlim) \
486 	safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
487 
488 typedef void (*sighandler_t)(int);
489 sighandler_t safe_signal(const char *file, const int lineno,
490 	int signum, sighandler_t handler);
491 
492 #define SAFE_SIGNAL(signum, handler) \
493 	safe_signal(__FILE__, __LINE__, (signum), (handler))
494 
495 int safe_sigaction(const char *file, const int lineno,
496                    int signum, const struct sigaction *act,
497                    struct sigaction *oldact);
498 #define SAFE_SIGACTION(signum, act, oldact) \
499 	safe_sigaction(__FILE__, __LINE__, (signum), (act), (oldact))
500 
501 int safe_sigaddset(const char *file, const int lineno,
502                     sigset_t *sigs, int signo);
503 #define SAFE_SIGADDSET(sigs, signo) \
504 	safe_sigaddset(__FILE__, __LINE__, (sigs), (signo))
505 
506 int safe_sigdelset(const char *file, const int lineno,
507                     sigset_t *sigs, int signo);
508 #define SAFE_SIGDELSET(sigs, signo) \
509 	safe_sigdelset(__FILE__, __LINE__, (sigs), (signo))
510 
511 int safe_sigemptyset(const char *file, const int lineno,
512                       sigset_t *sigs);
513 #define SAFE_SIGEMPTYSET(sigs) \
514 	safe_sigemptyset(__FILE__, __LINE__, (sigs))
515 
516 int safe_sigfillset(const char *file, const int lineno,
517 		     sigset_t *sigs);
518 #define SAFE_SIGFILLSET(sigs) \
519 	safe_sigfillset(__FILE__, __LINE__, (sigs))
520 
521 int safe_sigprocmask(const char *file, const int lineno,
522                       int how, sigset_t *set, sigset_t *oldset);
523 #define SAFE_SIGPROCMASK(how, set, oldset) \
524 	safe_sigprocmask(__FILE__, __LINE__, (how), (set), (oldset))
525 
526 int safe_sigwait(const char *file, const int lineno,
527                   sigset_t *set, int *sig);
528 #define SAFE_SIGWAIT(set, sig) \
529 	safe_sigwait(__FILE__, __LINE__, (set), (sig))
530 
531 #define SAFE_EXECLP(file, arg, ...) do {                   \
532 	execlp((file), (arg), ##__VA_ARGS__);              \
533 	tst_brk_(__FILE__, __LINE__, TBROK | TERRNO,       \
534 	         "execlp(%s, %s, ...) failed", file, arg); \
535 	} while (0)
536 
537 #define SAFE_EXECL(file, arg, ...) do {				\
538        execl((file), (arg), ##__VA_ARGS__);			\
539        tst_brk_(__FILE__, __LINE__, TBROK | TERRNO,		\
540                 "execl(%s, %s, ...) failed", file, arg); 	\
541        } while (0)
542 
543 #define SAFE_EXECVP(file, arg) do {                   \
544 	execvp((file), (arg));              \
545 	tst_brk_(__FILE__, __LINE__, TBROK | TERRNO,       \
546 	         "execvp(%s, %p) failed", file, arg); \
547 	} while (0)
548 
549 int safe_getpriority(const char *file, const int lineno, int which, id_t who);
550 #define SAFE_GETPRIORITY(which, who) \
551 	safe_getpriority(__FILE__, __LINE__, (which), (who))
552 
553 struct group *safe_getgrnam(const char *file, const int lineno,
554 			    const char *name);
555 #define SAFE_GETGRNAM(name) \
556 	safe_getgrnam(__FILE__, __LINE__, (name))
557 
558 struct group *safe_getgrnam_fallback(const char *file, const int lineno,
559 		const char *name, const char *fallback);
560 #define SAFE_GETGRNAM_FALLBACK(name, fallback) \
561 	safe_getgrnam_fallback(__FILE__, __LINE__, (name), (fallback))
562 
563 struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid);
564 #define SAFE_GETGRGID(gid) \
565 	safe_getgrgid(__FILE__, __LINE__, (gid))
566 
567 ssize_t safe_getxattr(const char *file, const int lineno, const char *path,
568 	const char *name, void *value, size_t size);
569 #define SAFE_GETXATTR(path, name, value, size) \
570 	safe_getxattr(__FILE__, __LINE__, (path), (name), (value), (size))
571 
572 int safe_setxattr(const char *file, const int lineno, const char *path,
573             const char *name, const void *value, size_t size, int flags);
574 #define SAFE_SETXATTR(path, name, value, size, flags) \
575 	safe_setxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
576 
577 int safe_lsetxattr(const char *file, const int lineno, const char *path,
578             const char *name, const void *value, size_t size, int flags);
579 #define SAFE_LSETXATTR(path, name, value, size, flags) \
580 	safe_lsetxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
581 
582 int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
583             const void *value, size_t size, int flags);
584 #define SAFE_FSETXATTR(fd, name, value, size, flags) \
585 	safe_fsetxattr(__FILE__, __LINE__, (fd), (name), (value), (size), (flags))
586 
587 int safe_removexattr(const char *file, const int lineno, const char *path,
588 		const char *name);
589 #define SAFE_REMOVEXATTR(path, name) \
590 	safe_removexattr(__FILE__, __LINE__, (path), (name))
591 
592 int safe_lremovexattr(const char *file, const int lineno, const char *path,
593 		const char *name);
594 #define SAFE_LREMOVEXATTR(path, name) \
595 	safe_lremovexattr(__FILE__, __LINE__, (path), (name))
596 
597 int safe_fremovexattr(const char *file, const int lineno, int fd,
598 		const char *name);
599 #define SAFE_FREMOVEXATTR(fd, name) \
600 	safe_fremovexattr(__FILE__, __LINE__, (fd), (name))
601 
602 int safe_fsync(const char *file, const int lineno, int fd);
603 #define SAFE_FSYNC(fd) safe_fsync(__FILE__, __LINE__, (fd))
604 
605 int safe_setsid(const char *file, const int lineno);
606 #define SAFE_SETSID() safe_setsid(__FILE__, __LINE__)
607 
608 int safe_mknod(const char *file, const int lineno, const char *pathname,
609 	mode_t mode, dev_t dev);
610 #define SAFE_MKNOD(pathname, mode, dev) \
611 	safe_mknod(__FILE__, __LINE__, (pathname), (mode), (dev))
612 
613 int safe_mlock(const char *file, const int lineno, const char *addr,
614 	size_t len);
615 #define SAFE_MLOCK(addr, len) safe_mlock(__FILE__, __LINE__, (addr), (len))
616 
617 int safe_munlock(const char *file, const int lineno, const char *addr,
618 	size_t len);
619 #define SAFE_MUNLOCK(addr, len) safe_munlock(__FILE__, __LINE__, (addr), (len))
620 
621 int safe_mincore(const char *file, const int lineno, void *start,
622 	size_t length, unsigned char *vec);
623 #define SAFE_MINCORE(start, length, vec) \
624 	safe_mincore(__FILE__, __LINE__, (start), (length), (vec))
625 
626 int safe_personality(const char *filename, unsigned int lineno,
627 		    unsigned long persona);
628 #define SAFE_PERSONALITY(persona) safe_personality(__FILE__, __LINE__, persona)
629 
630 int safe_pidfd_open(const char *filename, const int lineno, pid_t pid,
631 		   unsigned int flags);
632 #define SAFE_PIDFD_OPEN(pid, flags) \
633 	safe_pidfd_open(__FILE__, __LINE__, (pid), (flags))
634 
635 #define SAFE_SETENV(name, value, overwrite) do {		\
636 	if (setenv(name, value, overwrite)) {			\
637 		tst_brk_(__FILE__, __LINE__, TBROK | TERRNO,	\
638 			"setenv(%s, %s, %d) failed",		\
639 			name, value, overwrite);		\
640 	}							\
641 	} while (0)
642 
643 int safe_unshare(const char *file, const int lineno, int flags);
644 #define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
645 
646 int safe_setns(const char *file, const int lineno, int fd, int nstype);
647 #define SAFE_SETNS(fd, nstype) safe_setns(__FILE__, __LINE__, (fd), (nstype))
648 
649 void safe_cmd(const char *file, const int lineno, const char *const argv[],
650 	const char *stdout_path, const char *stderr_path);
651 
652 #define SAFE_CMD(argv, stdout_path, stderr_path) \
653 	safe_cmd(__FILE__, __LINE__, (argv), (stdout_path), (stderr_path))
654 /*
655  * SAFE_PTRACE() treats any non-zero return value as error. Don't use it
656  * for requests like PTRACE_PEEK* or PTRACE_SECCOMP_GET_FILTER which use
657  * the return value to pass arbitrary data.
658  */
659 long tst_safe_ptrace(const char *file, const int lineno, int req, pid_t pid,
660 	void *addr, void *data);
661 #define SAFE_PTRACE(req, pid, addr, data) \
662 	tst_safe_ptrace(__FILE__, __LINE__, req, pid, addr, data)
663 
664 int safe_sysinfo(const char *file, const int lineno, struct sysinfo *info);
665 #define SAFE_SYSINFO(info) \
666 	safe_sysinfo(__FILE__, __LINE__, (info))
667 
668 #endif /* SAFE_MACROS_H__ */
669