1 /*
2 * Copyright (c) 2010-2018 Linux Test Project
3 * Copyright (c) 2011-2015 Cyril Hrubis <chrubis@suse.cz>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #ifndef TST_SAFE_MACROS_H__
20 #define TST_SAFE_MACROS_H__
21
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <sys/time.h>
25 #include <sys/resource.h>
26 #include <sys/stat.h>
27 #include <sys/vfs.h>
28 #include <fcntl.h>
29 #include <libgen.h>
30 #include <signal.h>
31 #include <stdarg.h>
32 #include <unistd.h>
33 #include <dirent.h>
34 #include <grp.h>
35
36 #include "safe_macros_fn.h"
37
38 #define SAFE_BASENAME(path) \
39 safe_basename(__FILE__, __LINE__, NULL, (path))
40
41 #define SAFE_CHDIR(path) \
42 safe_chdir(__FILE__, __LINE__, NULL, (path))
43
44 #define SAFE_CLOSE(fd) do { \
45 safe_close(__FILE__, __LINE__, NULL, (fd)); \
46 fd = -1; \
47 } while (0)
48
49 #define SAFE_CREAT(pathname, mode) \
50 safe_creat(__FILE__, __LINE__, NULL, (pathname), (mode))
51
52 #define SAFE_CHROOT(path) \
53 safe_chroot(__FILE__, __LINE__, (path))
54 int safe_chroot(const char *file, const int lineno, const char *path);
55
56 #define SAFE_DIRNAME(path) \
57 safe_dirname(__FILE__, __LINE__, NULL, (path))
58
safe_dup(const char * file,const int lineno,int oldfd)59 static inline int safe_dup(const char *file, const int lineno,
60 int oldfd)
61 {
62 int rval;
63
64 rval = dup(oldfd);
65 if (rval == -1) {
66 tst_brk_(file, lineno, TBROK | TERRNO,
67 "dup(%i) failed", oldfd);
68 }
69
70 return rval;
71 }
72 #define SAFE_DUP(oldfd) \
73 safe_dup(__FILE__, __LINE__, (oldfd))
74
75 #define SAFE_GETCWD(buf, size) \
76 safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size))
77
78 #define SAFE_GETPWNAM(name) \
79 safe_getpwnam(__FILE__, __LINE__, NULL, (name))
80
81 #define SAFE_GETRUSAGE(who, usage) \
82 safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage))
83
84 #define SAFE_MALLOC(size) \
85 safe_malloc(__FILE__, __LINE__, NULL, (size))
86
87 #define SAFE_MKDIR(pathname, mode) \
88 safe_mkdir(__FILE__, __LINE__, NULL, (pathname), (mode))
89
90 #define SAFE_RMDIR(pathname) \
91 safe_rmdir(__FILE__, __LINE__, NULL, (pathname))
92
93 #define SAFE_MUNMAP(addr, length) \
94 safe_munmap(__FILE__, __LINE__, NULL, (addr), (length))
95
96 #define SAFE_OPEN(pathname, oflags, ...) \
97 safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), \
98 ##__VA_ARGS__)
99
100 #define SAFE_PIPE(fildes) \
101 safe_pipe(__FILE__, __LINE__, NULL, (fildes))
102
103 #define SAFE_READ(len_strict, fildes, buf, nbyte) \
104 safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
105
106 #define SAFE_SETEGID(egid) \
107 safe_setegid(__FILE__, __LINE__, NULL, (egid))
108
109 #define SAFE_SETEUID(euid) \
110 safe_seteuid(__FILE__, __LINE__, NULL, (euid))
111
112 #define SAFE_SETGID(gid) \
113 safe_setgid(__FILE__, __LINE__, NULL, (gid))
114
115 #define SAFE_SETUID(uid) \
116 safe_setuid(__FILE__, __LINE__, NULL, (uid))
117
118 int safe_setregid(const char *file, const int lineno,
119 gid_t rgid, gid_t egid);
120
121 #define SAFE_SETREGID(rgid, egid) \
122 safe_setregid(__FILE__, __LINE__, (rgid), (egid))
123
124 int safe_setreuid(const char *file, const int lineno,
125 uid_t ruid, uid_t euid);
126
127 #define SAFE_SETREUID(ruid, euid) \
128 safe_setreuid(__FILE__, __LINE__, (ruid), (euid))
129
130 #define SAFE_GETRESUID(ruid, euid, suid) \
131 safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid))
132
133 #define SAFE_GETRESGID(rgid, egid, sgid) \
134 safe_getresgid(__FILE__, __LINE__, NULL, (rgid), (egid), (sgid))
135
136 int safe_setpgid(const char *file, const int lineno, pid_t pid, pid_t pgid);
137
138 #define SAFE_SETPGID(pid, pgid) \
139 safe_setpgid(__FILE__, __LINE__, (pid), (pgid));
140
141 pid_t safe_getpgid(const char *file, const int lineno, pid_t pid);
142
143 #define SAFE_GETPGID(pid) \
144 safe_getpgid(__FILE__, __LINE__, (pid))
145
146 #define SAFE_UNLINK(pathname) \
147 safe_unlink(__FILE__, __LINE__, NULL, (pathname))
148
149 #define SAFE_LINK(oldpath, newpath) \
150 safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath))
151
152 #define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \
153 safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \
154 (newdirfd), (newpath), (flags))
155
156 #define SAFE_READLINK(path, buf, bufsize) \
157 safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize))
158
159 #define SAFE_SYMLINK(oldpath, newpath) \
160 safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath))
161
162 #define SAFE_WRITE(len_strict, fildes, buf, nbyte) \
163 safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte))
164
165 #define SAFE_STRTOL(str, min, max) \
166 safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max))
167
168 #define SAFE_STRTOUL(str, min, max) \
169 safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max))
170
171 #define SAFE_SYSCONF(name) \
172 safe_sysconf(__FILE__, __LINE__, NULL, name)
173
174 #define SAFE_CHMOD(path, mode) \
175 safe_chmod(__FILE__, __LINE__, NULL, (path), (mode))
176
177 #define SAFE_FCHMOD(fd, mode) \
178 safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode))
179
180 #define SAFE_CHOWN(path, owner, group) \
181 safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group))
182
183 #define SAFE_FCHOWN(fd, owner, group) \
184 safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group))
185
186 #define SAFE_WAIT(status) \
187 safe_wait(__FILE__, __LINE__, NULL, (status))
188
189 #define SAFE_WAITPID(pid, status, opts) \
190 safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts))
191
192 #define SAFE_KILL(pid, sig) \
193 safe_kill(__FILE__, __LINE__, NULL, (pid), (sig))
194
195 #define SAFE_MEMALIGN(alignment, size) \
196 safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size))
197
198 #define SAFE_MKFIFO(pathname, mode) \
199 safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode))
200
201 #define SAFE_RENAME(oldpath, newpath) \
202 safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath))
203
204 #define SAFE_MOUNT(source, target, filesystemtype, \
205 mountflags, data) \
206 safe_mount(__FILE__, __LINE__, NULL, (source), (target), \
207 (filesystemtype), (mountflags), (data))
208
209 #define SAFE_UMOUNT(target) \
210 safe_umount(__FILE__, __LINE__, NULL, (target))
211
212 #define SAFE_OPENDIR(name) \
213 safe_opendir(__FILE__, __LINE__, NULL, (name))
214
215 #define SAFE_CLOSEDIR(dirp) \
216 safe_closedir(__FILE__, __LINE__, NULL, (dirp))
217
218 #define SAFE_READDIR(dirp) \
219 safe_readdir(__FILE__, __LINE__, NULL, (dirp))
220
221 #define SAFE_IOCTL(fd, request, ...) \
222 ({int tst_ret_ = ioctl(fd, request, ##__VA_ARGS__); \
223 tst_ret_ < 0 ? \
224 tst_brk(TBROK | TERRNO, \
225 "ioctl(%i,%s,...) failed", fd, #request), 0 \
226 : tst_ret_;})
227
228 #define SAFE_FCNTL(fd, cmd, ...) \
229 ({int tst_ret_ = fcntl(fd, cmd, ##__VA_ARGS__); \
230 tst_ret_ == -1 ? \
231 tst_brk(TBROK | TERRNO, \
232 "fcntl(%i,%s,...) failed", fd, #cmd), 0 \
233 : tst_ret_;})
234
235 /*
236 * following functions are inline because the behaviour may depend on
237 * -D_FILE_OFFSET_BITS=64 -DOFF_T=off64_t compile flags
238 */
239
safe_mmap(const char * file,const int lineno,void * addr,size_t length,int prot,int flags,int fd,off_t offset)240 static inline void *safe_mmap(const char *file, const int lineno,
241 void *addr, size_t length,
242 int prot, int flags, int fd, off_t offset)
243 {
244 void *rval;
245
246 rval = mmap(addr, length, prot, flags, fd, offset);
247 if (rval == MAP_FAILED) {
248 tst_brk_(file, lineno, TBROK | TERRNO,
249 "mmap(%p,%zu,%d,%d,%d,%ld) failed",
250 addr, length, prot, flags, fd, (long) offset);
251 }
252
253 return rval;
254 }
255 #define SAFE_MMAP(addr, length, prot, flags, fd, offset) \
256 safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \
257 (flags), (fd), (offset))
258
safe_ftruncate(const char * file,const int lineno,int fd,off_t length)259 static inline int safe_ftruncate(const char *file, const int lineno,
260 int fd, off_t length)
261 {
262 int rval;
263
264 rval = ftruncate(fd, length);
265 if (rval == -1) {
266 tst_brk_(file, lineno, TBROK | TERRNO,
267 "ftruncate(%d,%ld) failed",
268 fd, (long)length);
269 }
270
271 return rval;
272 }
273 #define SAFE_FTRUNCATE(fd, length) \
274 safe_ftruncate(__FILE__, __LINE__, (fd), (length))
275
safe_truncate(const char * file,const int lineno,const char * path,off_t length)276 static inline int safe_truncate(const char *file, const int lineno,
277 const char *path, off_t length)
278 {
279 int rval;
280
281 rval = truncate(path, length);
282 if (rval == -1) {
283 tst_brk_(file, lineno, TBROK | TERRNO,
284 "truncate(%s,%ld) failed",
285 path, (long)length);
286 }
287
288 return rval;
289 }
290 #define SAFE_TRUNCATE(path, length) \
291 safe_truncate(__FILE__, __LINE__, (path), (length))
292
safe_stat(const char * file,const int lineno,const char * path,struct stat * buf)293 static inline int safe_stat(const char *file, const int lineno,
294 const char *path, struct stat *buf)
295 {
296 int rval;
297
298 rval = stat(path, buf);
299
300 if (rval == -1) {
301 tst_brk_(file, lineno, TBROK | TERRNO,
302 "stat(%s,%p) failed", path, buf);
303 }
304
305 return rval;
306 }
307 #define SAFE_STAT(path, buf) \
308 safe_stat(__FILE__, __LINE__, (path), (buf))
309
safe_fstat(const char * file,const int lineno,int fd,struct stat * buf)310 static inline int safe_fstat(const char *file, const int lineno,
311 int fd, struct stat *buf)
312 {
313 int rval;
314
315 rval = fstat(fd, buf);
316
317 if (rval == -1) {
318 tst_brk_(file, lineno, TBROK | TERRNO,
319 "fstat(%d,%p) failed", fd, buf);
320 }
321
322 return rval;
323 }
324 #define SAFE_FSTAT(fd, buf) \
325 safe_fstat(__FILE__, __LINE__, (fd), (buf))
326
safe_lstat(const char * file,const int lineno,const char * path,struct stat * buf)327 static inline int safe_lstat(const char *file, const int lineno,
328 const char *path, struct stat *buf)
329 {
330 int rval;
331
332 rval = lstat(path, buf);
333
334 if (rval == -1) {
335 tst_brk_(file, lineno, TBROK | TERRNO,
336 "lstat(%s,%p) failed", path, buf);
337 }
338
339 return rval;
340 }
341 #define SAFE_LSTAT(path, buf) \
342 safe_lstat(__FILE__, __LINE__, (path), (buf))
343
safe_statfs(const char * file,const int lineno,const char * path,struct statfs * buf)344 static inline int safe_statfs(const char *file, const int lineno,
345 const char *path, struct statfs *buf)
346 {
347 int rval;
348
349 rval = statfs(path, buf);
350
351 if (rval == -1) {
352 tst_brk_(file, lineno, TBROK | TERRNO,
353 "statfs(%s,%p) failed", path, buf);
354 }
355
356 return rval;
357 }
358 #define SAFE_STATFS(path, buf) \
359 safe_statfs(__FILE__, __LINE__, (path), (buf))
360
safe_lseek(const char * file,const int lineno,int fd,off_t offset,int whence)361 static inline off_t safe_lseek(const char *file, const int lineno,
362 int fd, off_t offset, int whence)
363 {
364 off_t rval;
365
366 rval = lseek(fd, offset, whence);
367
368 if (rval == (off_t) -1) {
369 tst_brk_(file, lineno, TBROK | TERRNO,
370 "lseek(%d,%ld,%d) failed",
371 fd, (long)offset, whence);
372 }
373
374 return rval;
375 }
376 #define SAFE_LSEEK(fd, offset, whence) \
377 safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence))
378
safe_getrlimit(const char * file,const int lineno,int resource,struct rlimit * rlim)379 static inline int safe_getrlimit(const char *file, const int lineno,
380 int resource, struct rlimit *rlim)
381 {
382 int rval;
383
384 rval = getrlimit(resource, rlim);
385
386 if (rval == -1) {
387 tst_brk_(file, lineno, TBROK | TERRNO,
388 "getrlimit(%d,%p) failed",
389 resource, rlim);
390 }
391
392 return rval;
393 }
394 #define SAFE_GETRLIMIT(resource, rlim) \
395 safe_getrlimit(__FILE__, __LINE__, (resource), (rlim))
396
safe_setrlimit(const char * file,const int lineno,int resource,const struct rlimit * rlim)397 static inline int safe_setrlimit(const char *file, const int lineno,
398 int resource, const struct rlimit *rlim)
399 {
400 int rval;
401
402 rval = setrlimit(resource, rlim);
403
404 if (rval == -1) {
405 tst_brk_(file, lineno, TBROK | TERRNO,
406 "setrlimit(%d,%p) failed",
407 resource, rlim);
408 }
409
410 return rval;
411 }
412 #define SAFE_SETRLIMIT(resource, rlim) \
413 safe_setrlimit(__FILE__, __LINE__, (resource), (rlim))
414
415 typedef void (*sighandler_t)(int);
safe_signal(const char * file,const int lineno,int signum,sighandler_t handler)416 static inline sighandler_t safe_signal(const char *file, const int lineno,
417 int signum, sighandler_t handler)
418 {
419 sighandler_t rval;
420
421 rval = signal(signum, handler);
422
423 if (rval == SIG_ERR) {
424 tst_brk_(file, lineno, TBROK | TERRNO,
425 "signal(%d,%p) failed",
426 signum, handler);
427 }
428
429 return rval;
430 }
431
432 #define SAFE_SIGNAL(signum, handler) \
433 safe_signal(__FILE__, __LINE__, (signum), (handler))
434
435 int safe_sigaction(const char *file, const int lineno,
436 int signum, const struct sigaction *act,
437 struct sigaction *oldact);
438 #define SAFE_SIGACTION(signum, act, oldact) \
439 safe_sigaction(__FILE__, __LINE__, (signum), (act), (oldact))
440
441 #define SAFE_EXECLP(file, arg, ...) do { \
442 execlp((file), (arg), ##__VA_ARGS__); \
443 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
444 "execlp(%s, %s, ...) failed", file, arg); \
445 } while (0)
446
447 #define SAFE_EXECL(file, arg, ...) do { \
448 execl((file), (arg), ##__VA_ARGS__); \
449 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
450 "execl(%s, %s, ...) failed", file, arg); \
451 } while (0)
452
453 int safe_getpriority(const char *file, const int lineno, int which, id_t who);
454 #define SAFE_GETPRIORITY(which, who) \
455 safe_getpriority(__FILE__, __LINE__, (which), (who))
456
457 struct group *safe_getgrnam(const char *file, const int lineno,
458 const char *name);
459 #define SAFE_GETGRNAM(name) \
460 safe_getgrnam(__FILE__, __LINE__, (name))
461
462 struct group *safe_getgrnam_fallback(const char *file, const int lineno,
463 const char *name, const char *fallback);
464 #define SAFE_GETGRNAM_FALLBACK(name, fallback) \
465 safe_getgrnam_fallback(__FILE__, __LINE__, (name), (fallback))
466
467 struct group *safe_getgrgid(const char *file, const int lineno, gid_t gid);
468 #define SAFE_GETGRGID(gid) \
469 safe_getgrgid(__FILE__, __LINE__, (gid))
470
471 ssize_t safe_getxattr(const char *file, const int lineno, const char *path,
472 const char *name, void *value, size_t size);
473 #define SAFE_GETXATTR(path, name, value, size) \
474 safe_getxattr(__FILE__, __LINE__, (path), (name), (value), (size))
475
476 int safe_setxattr(const char *file, const int lineno, const char *path,
477 const char *name, const void *value, size_t size, int flags);
478 #define SAFE_SETXATTR(path, name, value, size, flags) \
479 safe_setxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
480
481 int safe_lsetxattr(const char *file, const int lineno, const char *path,
482 const char *name, const void *value, size_t size, int flags);
483 #define SAFE_LSETXATTR(path, name, value, size, flags) \
484 safe_lsetxattr(__FILE__, __LINE__, (path), (name), (value), (size), (flags))
485
486 int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
487 const void *value, size_t size, int flags);
488 #define SAFE_FSETXATTR(fd, name, value, size, flags) \
489 safe_fsetxattr(__FILE__, __LINE__, (fd), (name), (value), (size), (flags))
490
491 int safe_removexattr(const char *file, const int lineno, const char *path,
492 const char *name);
493 #define SAFE_REMOVEXATTR(path, name) \
494 safe_removexattr(__FILE__, __LINE__, (path), (name))
495
496 int safe_lremovexattr(const char *file, const int lineno, const char *path,
497 const char *name);
498 #define SAFE_LREMOVEXATTR(path, name) \
499 safe_lremovexattr(__FILE__, __LINE__, (path), (name))
500
501 int safe_fremovexattr(const char *file, const int lineno, int fd,
502 const char *name);
503 #define SAFE_FREMOVEXATTR(fd, name) \
504 safe_fremovexattr(__FILE__, __LINE__, (fd), (name))
505
506 int safe_fsync(const char *file, const int lineno, int fd);
507 #define SAFE_FSYNC(fd) safe_fsync(__FILE__, __LINE__, (fd))
508
509 int safe_setsid(const char *file, const int lineno);
510 #define SAFE_SETSID() safe_setsid(__FILE__, __LINE__)
511
512 int safe_mknod(const char *file, const int lineno, const char *pathname,
513 mode_t mode, dev_t dev);
514 #define SAFE_MKNOD(pathname, mode, dev) \
515 safe_mknod(__FILE__, __LINE__, (pathname), (mode), (dev))
516
517 int safe_mlock(const char *file, const int lineno, const char *addr,
518 size_t len);
519 #define SAFE_MLOCK(addr, len) safe_mlock(__FILE__, __LINE__, (addr), (len))
520
521 int safe_munlock(const char *file, const int lineno, const char *addr,
522 size_t len);
523 #define SAFE_MUNLOCK(addr, len) safe_munlock(__FILE__, __LINE__, (addr), (len))
524
525 int safe_mincore(const char *file, const int lineno, void *start,
526 size_t length, unsigned char *vec);
527 #define SAFE_MINCORE(start, length, vec) \
528 safe_mincore(__FILE__, __LINE__, (start), (length), (vec))
529
530 int safe_fanotify_init(const char *file, const int lineno,
531 unsigned int flags, unsigned int event_f_flags);
532 #define SAFE_FANOTIFY_INIT(fan, mode) \
533 safe_fanotify_init(__FILE__, __LINE__, (fan), (mode))
534
535 int safe_personality(const char *filename, unsigned int lineno,
536 unsigned long persona);
537 #define SAFE_PERSONALITY(persona) safe_personality(__FILE__, __LINE__, persona)
538
539 #define SAFE_SETENV(name, value, overwrite) do { \
540 if (setenv(name, value, overwrite)) { \
541 tst_brk_(__FILE__, __LINE__, TBROK | TERRNO, \
542 "setenv(%s, %s, %d) failed", \
543 name, value, overwrite); \
544 } \
545 } while (0)
546
547 void safe_unshare(const char *file, const int lineno, int flags);
548 #define SAFE_UNSHARE(flags) safe_unshare(__FILE__, __LINE__, (flags))
549
550 #endif /* SAFE_MACROS_H__ */
551