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