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 #define SAFE_GETCWD(buf, size) \ 54 safe_getcwd(__FILE__, __LINE__, NULL, (buf), (size)) 55 56 #define SAFE_GETPWNAM(name) \ 57 safe_getpwnam(__FILE__, __LINE__, NULL, (name)) 58 59 #define SAFE_GETRUSAGE(who, usage) \ 60 safe_getrusage(__FILE__, __LINE__, NULL, (who), (usage)) 61 62 #define SAFE_MALLOC(size) \ 63 safe_malloc(__FILE__, __LINE__, NULL, (size)) 64 65 #define SAFE_MKDIR(pathname, mode) \ 66 safe_mkdir(__FILE__, __LINE__, NULL, (pathname), (mode)) 67 68 #define SAFE_RMDIR(pathname) \ 69 safe_rmdir(__FILE__, __LINE__, NULL, (pathname)) 70 71 #define SAFE_MUNMAP(addr, length) \ 72 safe_munmap(__FILE__, __LINE__, NULL, (addr), (length)) 73 74 #define SAFE_OPEN(pathname, oflags, ...) \ 75 safe_open(__FILE__, __LINE__, NULL, (pathname), (oflags), \ 76 ##__VA_ARGS__) 77 78 #define SAFE_PIPE(fildes) \ 79 safe_pipe(__FILE__, __LINE__, NULL, (fildes)) 80 81 #define SAFE_READ(len_strict, fildes, buf, nbyte) \ 82 safe_read(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte)) 83 84 #define SAFE_PREAD(len_strict, fildes, buf, nbyte, offset) \ 85 safe_pread(__FILE__, __LINE__, NULL, (len_strict), (fildes), \ 86 (buf), (nbyte), (offset)) 87 88 #define SAFE_SETEGID(egid) \ 89 safe_setegid(__FILE__, __LINE__, NULL, (egid)) 90 91 #define SAFE_SETEUID(euid) \ 92 safe_seteuid(__FILE__, __LINE__, NULL, (euid)) 93 94 #define SAFE_SETGID(gid) \ 95 safe_setgid(__FILE__, __LINE__, NULL, (gid)) 96 97 #define SAFE_SETUID(uid) \ 98 safe_setuid(__FILE__, __LINE__, NULL, (uid)) 99 100 #define SAFE_GETRESUID(ruid, euid, suid) \ 101 safe_getresuid(__FILE__, __LINE__, NULL, (ruid), (euid), (suid)) 102 103 #define SAFE_GETRESGID(rgid, egid, sgid) \ 104 safe_getresgid(__FILE__, __LINE__, NULL, (rgid), (egid), (sgid)) 105 106 static inline int safe_setpgid(const char *file, const int lineno, 107 pid_t pid, pid_t pgid) 108 { 109 int rval; 110 111 rval = setpgid(pid, pgid); 112 if (rval) { 113 tst_brk_(file, lineno, TBROK | TERRNO, 114 "setpgid(%i, %i) failed", pid, pgid); 115 } 116 117 return rval; 118 } 119 #define SAFE_SETPGID(pid, pgid) \ 120 safe_setpgid(__FILE__, __LINE__, (pid), (pgid)); 121 122 #define SAFE_UNLINK(pathname) \ 123 safe_unlink(__FILE__, __LINE__, NULL, (pathname)) 124 125 #define SAFE_LINK(oldpath, newpath) \ 126 safe_link(__FILE__, __LINE__, NULL, (oldpath), (newpath)) 127 128 #define SAFE_LINKAT(olddirfd, oldpath, newdirfd, newpath, flags) \ 129 safe_linkat(__FILE__, __LINE__, NULL, (olddirfd), (oldpath), \ 130 (newdirfd), (newpath), (flags)) 131 132 #define SAFE_READLINK(path, buf, bufsize) \ 133 safe_readlink(__FILE__, __LINE__, NULL, (path), (buf), (bufsize)) 134 135 #define SAFE_SYMLINK(oldpath, newpath) \ 136 safe_symlink(__FILE__, __LINE__, NULL, (oldpath), (newpath)) 137 138 #define SAFE_WRITE(len_strict, fildes, buf, nbyte) \ 139 safe_write(__FILE__, __LINE__, NULL, (len_strict), (fildes), (buf), (nbyte)) 140 141 #define SAFE_PWRITE(len_strict, fildes, buf, nbyte, offset) \ 142 safe_pwrite(__FILE__, __LINE__, NULL, (len_strict), (fildes), \ 143 (buf), (nbyte), (offset)) 144 145 #define SAFE_STRTOL(str, min, max) \ 146 safe_strtol(__FILE__, __LINE__, NULL, (str), (min), (max)) 147 148 #define SAFE_STRTOUL(str, min, max) \ 149 safe_strtoul(__FILE__, __LINE__, NULL, (str), (min), (max)) 150 151 #define SAFE_SYSCONF(name) \ 152 safe_sysconf(__FILE__, __LINE__, NULL, name) 153 154 #define SAFE_CHMOD(path, mode) \ 155 safe_chmod(__FILE__, __LINE__, NULL, (path), (mode)) 156 157 #define SAFE_FCHMOD(fd, mode) \ 158 safe_fchmod(__FILE__, __LINE__, NULL, (fd), (mode)) 159 160 #define SAFE_CHOWN(path, owner, group) \ 161 safe_chown(__FILE__, __LINE__, NULL, (path), (owner), (group)) 162 163 #define SAFE_FCHOWN(fd, owner, group) \ 164 safe_fchown(__FILE__, __LINE__, NULL, (fd), (owner), (group)) 165 166 #define SAFE_WAIT(status) \ 167 safe_wait(__FILE__, __LINE__, NULL, (status)) 168 169 #define SAFE_WAITPID(pid, status, opts) \ 170 safe_waitpid(__FILE__, __LINE__, NULL, (pid), (status), (opts)) 171 172 #define SAFE_KILL(pid, sig) \ 173 safe_kill(__FILE__, __LINE__, NULL, (pid), (sig)) 174 175 #define SAFE_MEMALIGN(alignment, size) \ 176 safe_memalign(__FILE__, __LINE__, NULL, (alignment), (size)) 177 178 #define SAFE_MKFIFO(pathname, mode) \ 179 safe_mkfifo(__FILE__, __LINE__, NULL, (pathname), (mode)) 180 181 #define SAFE_RENAME(oldpath, newpath) \ 182 safe_rename(__FILE__, __LINE__, NULL, (oldpath), (newpath)) 183 184 #define SAFE_MOUNT(source, target, filesystemtype, \ 185 mountflags, data) \ 186 safe_mount(__FILE__, __LINE__, NULL, (source), (target), \ 187 (filesystemtype), (mountflags), (data)) 188 189 #define SAFE_UMOUNT(target) \ 190 safe_umount(__FILE__, __LINE__, NULL, (target)) 191 192 #define SAFE_OPENDIR(name) \ 193 safe_opendir(__FILE__, __LINE__, NULL, (name)) 194 195 #define SAFE_CLOSEDIR(dirp) \ 196 safe_closedir(__FILE__, __LINE__, NULL, (dirp)) 197 198 #define SAFE_READDIR(dirp) \ 199 safe_readdir(__FILE__, __LINE__, NULL, (dirp)) 200 201 #define SAFE_IOCTL(fd, request, ...) \ 202 ({int ret = ioctl(fd, request, ##__VA_ARGS__); \ 203 ret < 0 ? \ 204 tst_brk(TBROK | TERRNO, \ 205 "ioctl(%i,%s,...) failed", fd, #request) \ 206 : ret;}) 207 208 #define SAFE_FCNTL(fd, cmd, ...) \ 209 ({int ret = fcntl(fd, cmd, ##__VA_ARGS__); \ 210 ret == -1 ? \ 211 tst_brk(TBROK | TERRNO, \ 212 "fcntl(%i,%s,...) failed", fd, #cmd), 0 \ 213 : ret;}) 214 215 /* 216 * following functions are inline because the behaviour may depend on 217 * -D_FILE_OFFSET_BITS=64 -DOFF_T=off64_t compile flags 218 */ 219 220 static inline void *safe_mmap(const char *file, const int lineno, 221 void *addr, size_t length, 222 int prot, int flags, int fd, off_t offset) 223 { 224 void *rval; 225 226 rval = mmap(addr, length, prot, flags, fd, offset); 227 if (rval == MAP_FAILED) { 228 tst_brk_(file, lineno, TBROK | TERRNO, 229 "mmap(%p,%zu,%d,%d,%d,%ld) failed", 230 addr, length, prot, flags, fd, (long) offset); 231 } 232 233 return rval; 234 } 235 #define SAFE_MMAP(addr, length, prot, flags, fd, offset) \ 236 safe_mmap(__FILE__, __LINE__, (addr), (length), (prot), \ 237 (flags), (fd), (offset)) 238 239 static inline int safe_ftruncate(const char *file, const int lineno, 240 int fd, off_t length) 241 { 242 int rval; 243 244 rval = ftruncate(fd, length); 245 if (rval == -1) { 246 tst_brk_(file, lineno, TBROK | TERRNO, 247 "ftruncate(%d,%ld) failed", 248 fd, (long)length); 249 } 250 251 return rval; 252 } 253 #define SAFE_FTRUNCATE(fd, length) \ 254 safe_ftruncate(__FILE__, __LINE__, (fd), (length)) 255 256 static inline int safe_truncate(const char *file, const int lineno, 257 const char *path, off_t length) 258 { 259 int rval; 260 261 rval = truncate(path, length); 262 if (rval == -1) { 263 tst_brk_(file, lineno, TBROK | TERRNO, 264 "truncate(%s,%ld) failed", 265 path, (long)length); 266 } 267 268 return rval; 269 } 270 #define SAFE_TRUNCATE(path, length) \ 271 safe_truncate(__FILE__, __LINE__, (path), (length)) 272 273 static inline int safe_stat(const char *file, const int lineno, 274 const char *path, struct stat *buf) 275 { 276 int rval; 277 278 rval = stat(path, buf); 279 280 if (rval == -1) { 281 tst_brk_(file, lineno, TBROK | TERRNO, 282 "stat(%s,%p) failed", path, buf); 283 } 284 285 return rval; 286 } 287 #define SAFE_STAT(path, buf) \ 288 safe_stat(__FILE__, __LINE__, (path), (buf)) 289 290 static inline int safe_fstat(const char *file, const int lineno, 291 int fd, struct stat *buf) 292 { 293 int rval; 294 295 rval = fstat(fd, buf); 296 297 if (rval == -1) { 298 tst_brk_(file, lineno, TBROK | TERRNO, 299 "fstat(%d,%p) failed", fd, buf); 300 } 301 302 return rval; 303 } 304 #define SAFE_FSTAT(fd, buf) \ 305 safe_fstat(__FILE__, __LINE__, (fd), (buf)) 306 307 static inline int safe_lstat(const char *file, const int lineno, 308 const char *path, struct stat *buf) 309 { 310 int rval; 311 312 rval = lstat(path, buf); 313 314 if (rval == -1) { 315 tst_brk_(file, lineno, TBROK | TERRNO, 316 "lstat(%s,%p) failed", path, buf); 317 } 318 319 return rval; 320 } 321 #define SAFE_LSTAT(path, buf) \ 322 safe_lstat(__FILE__, __LINE__, (path), (buf)) 323 324 static inline off_t safe_lseek(const char *file, const int lineno, 325 int fd, off_t offset, int whence) 326 { 327 off_t rval; 328 329 rval = lseek(fd, offset, whence); 330 331 if (rval == (off_t) -1) { 332 tst_brk_(file, lineno, TBROK | TERRNO, 333 "lseek(%d,%ld,%d) failed", 334 fd, (long)offset, whence); 335 } 336 337 return rval; 338 } 339 #define SAFE_LSEEK(fd, offset, whence) \ 340 safe_lseek(__FILE__, __LINE__, (fd), (offset), (whence)) 341 342 static inline int safe_getrlimit(const char *file, const int lineno, 343 int resource, struct rlimit *rlim) 344 { 345 int rval; 346 347 rval = getrlimit(resource, rlim); 348 349 if (rval == -1) { 350 tst_brk_(file, lineno, TBROK | TERRNO, 351 "getrlimit(%d,%p) failed", 352 resource, rlim); 353 } 354 355 return rval; 356 } 357 #define SAFE_GETRLIMIT(resource, rlim) \ 358 safe_getrlimit(__FILE__, __LINE__, (resource), (rlim)) 359 360 static inline int safe_setrlimit(const char *file, const int lineno, 361 int resource, const struct rlimit *rlim) 362 { 363 int rval; 364 365 rval = setrlimit(resource, rlim); 366 367 if (rval == -1) { 368 tst_brk_(file, lineno, TBROK | TERRNO, 369 "setrlimit(%d,%p) failed", 370 resource, rlim); 371 } 372 373 return rval; 374 } 375 #define SAFE_SETRLIMIT(resource, rlim) \ 376 safe_setrlimit(__FILE__, __LINE__, (resource), (rlim)) 377 378 typedef void (*sighandler_t)(int); 379 static inline sighandler_t safe_signal(const char *file, const int lineno, 380 int signum, sighandler_t handler) 381 { 382 sighandler_t rval; 383 384 rval = signal(signum, handler); 385 386 if (rval == SIG_ERR) { 387 tst_brk_(file, lineno, TBROK | TERRNO, 388 "signal(%d,%p) failed", 389 signum, handler); 390 } 391 392 return rval; 393 } 394 395 #define SAFE_SIGNAL(signum, handler) \ 396 safe_signal(__FILE__, __LINE__, (signum), (handler)) 397 398 #endif /* SAFE_MACROS_H__ */ 399