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