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