1 /* 2 * Safe macros for commonly used syscalls to reduce code duplication in LTP 3 * testcases, and to ensure all errors are caught in said testcases as 4 * gracefully as possible. 5 * 6 * Also satiates some versions of gcc/glibc when the warn_unused_result 7 * attribute is applied to the function call. 8 * 9 * Licensed under the GPLv2. 10 */ 11 12 #ifndef __TEST_H__ 13 #error "you must include test.h before this file" 14 #else 15 16 #ifndef __SAFE_MACROS_H__ 17 #define __SAFE_MACROS_H__ 18 19 #include "safe_macros_fn.h" 20 #include "old_safe_stdio.h" 21 #include "old_safe_net.h" 22 23 #define SAFE_BASENAME(cleanup_fn, path) \ 24 safe_basename(__FILE__, __LINE__, (cleanup_fn), (path)) 25 26 #define SAFE_CHDIR(cleanup_fn, path) \ 27 safe_chdir(__FILE__, __LINE__, (cleanup_fn), (path)) 28 29 #define SAFE_CLOSE(cleanup_fn, fildes) \ 30 safe_close(__FILE__, __LINE__, (cleanup_fn), (fildes)) 31 32 #define SAFE_CREAT(cleanup_fn, pathname, mode) \ 33 safe_creat(__FILE__, __LINE__, cleanup_fn, (pathname), (mode)) 34 35 #define SAFE_DIRNAME(cleanup_fn, path) \ 36 safe_dirname(__FILE__, __LINE__, (cleanup_fn), (path)) 37 38 #define SAFE_GETCWD(cleanup_fn, buf, size) \ 39 safe_getcwd(__FILE__, __LINE__, (cleanup_fn), (buf), (size)) 40 41 #define SAFE_GETPWNAM(cleanup_fn, name) \ 42 safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name)) 43 44 #define SAFE_GETRUSAGE(cleanup_fn, who, usage) \ 45 safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage)) 46 47 #define SAFE_MALLOC(cleanup_fn, size) \ 48 safe_malloc(__FILE__, __LINE__, (cleanup_fn), (size)) 49 50 #define SAFE_MKDIR(cleanup_fn, pathname, mode) \ 51 safe_mkdir(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode)) 52 53 #define SAFE_RMDIR(cleanup_fn, pathname) \ 54 safe_rmdir(__FILE__, __LINE__, (cleanup_fn), (pathname)) 55 56 #define SAFE_MUNMAP(cleanup_fn, addr, length) \ 57 safe_munmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length)) 58 59 #define SAFE_OPEN(cleanup_fn, pathname, oflags, ...) \ 60 safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), \ 61 ##__VA_ARGS__) 62 63 #define SAFE_PIPE(cleanup_fn, fildes) \ 64 safe_pipe(__FILE__, __LINE__, cleanup_fn, (fildes)) 65 66 #define SAFE_READ(cleanup_fn, len_strict, fildes, buf, nbyte) \ 67 safe_read(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \ 68 (buf), (nbyte)) 69 70 #define SAFE_PREAD(cleanup_fn, len_strict, fildes, buf, nbyte, offset) \ 71 safe_pread(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \ 72 (buf), (nbyte), (offset)) 73 74 #define SAFE_SETEGID(cleanup_fn, egid) \ 75 safe_setegid(__FILE__, __LINE__, cleanup_fn, (egid)) 76 77 #define SAFE_SETEUID(cleanup_fn, euid) \ 78 safe_seteuid(__FILE__, __LINE__, cleanup_fn, (euid)) 79 80 #define SAFE_SETGID(cleanup_fn, gid) \ 81 safe_setgid(__FILE__, __LINE__, cleanup_fn, (gid)) 82 83 #define SAFE_SETUID(cleanup_fn, uid) \ 84 safe_setuid(__FILE__, __LINE__, cleanup_fn, (uid)) 85 86 #define SAFE_GETRESUID(cleanup_fn, ruid, euid, suid) \ 87 safe_getresuid(__FILE__, __LINE__, cleanup_fn, (ruid), (euid), (suid)) 88 89 #define SAFE_GETRESGID(cleanup_fn, rgid, egid, sgid) \ 90 safe_getresgid(__FILE__, __LINE__, cleanup_fn, (rgid), (egid), (sgid)) 91 92 #define SAFE_UNLINK(cleanup_fn, pathname) \ 93 safe_unlink(__FILE__, __LINE__, cleanup_fn, (pathname)) 94 95 #define SAFE_LINK(cleanup_fn, oldpath, newpath) \ 96 safe_link(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath)) 97 98 #define SAFE_LINKAT(cleanup_fn, olddirfd, oldpath, newdirfd, newpath, flags) \ 99 safe_linkat(__FILE__, __LINE__, cleanup_fn, (olddirfd), (oldpath), \ 100 (newdirfd), (newpath), (flags)) 101 102 #define SAFE_READLINK(cleanup_fn, path, buf, bufsize) \ 103 safe_readlink(__FILE__, __LINE__, cleanup_fn, (path), (buf), (bufsize)) 104 105 #define SAFE_SYMLINK(cleanup_fn, oldpath, newpath) \ 106 safe_symlink(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath)) 107 108 #define SAFE_WRITE(cleanup_fn, len_strict, fildes, buf, nbyte) \ 109 safe_write(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \ 110 (buf), (nbyte)) 111 112 #define SAFE_PWRITE(cleanup_fn, len_strict, fildes, buf, nbyte, offset) \ 113 safe_pwrite(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \ 114 (buf), (nbyte), (offset)) 115 116 #define SAFE_STRTOL(cleanup_fn, str, min, max) \ 117 safe_strtol(__FILE__, __LINE__, cleanup_fn, (str), (min), (max)) 118 119 #define SAFE_STRTOUL(cleanup_fn, str, min, max) \ 120 safe_strtoul(__FILE__, __LINE__, cleanup_fn, (str), (min), (max)) 121 122 #define SAFE_SYSCONF(cleanup_fn, name) \ 123 safe_sysconf(__FILE__, __LINE__, cleanup_fn, name) 124 125 #define SAFE_CHMOD(cleanup_fn, path, mode) \ 126 safe_chmod(__FILE__, __LINE__, (cleanup_fn), (path), (mode)) 127 128 #define SAFE_FCHMOD(cleanup_fn, fd, mode) \ 129 safe_fchmod(__FILE__, __LINE__, (cleanup_fn), (fd), (mode)) 130 131 #define SAFE_CHOWN(cleanup_fn, path, owner, group) \ 132 safe_chown(__FILE__, __LINE__, (cleanup_fn), (path), (owner), (group)) 133 134 #define SAFE_FCHOWN(cleanup_fn, fd, owner, group) \ 135 safe_fchown(__FILE__, __LINE__, (cleanup_fn), (fd), (owner), (group)) 136 137 #define SAFE_WAIT(cleanup_fn, status) \ 138 safe_wait(__FILE__, __LINE__, (cleanup_fn), (status)) 139 140 #define SAFE_WAITPID(cleanup_fn, pid, status, opts) \ 141 safe_waitpid(__FILE__, __LINE__, (cleanup_fn), (pid), (status), (opts)) 142 143 #define SAFE_KILL(cleanup_fn, pid, sig) \ 144 safe_kill(__FILE__, __LINE__, (cleanup_fn), (pid), (sig)) 145 146 #define SAFE_MEMALIGN(cleanup_fn, alignment, size) \ 147 safe_memalign(__FILE__, __LINE__, (cleanup_fn), (alignment), (size)) 148 149 #define SAFE_MKFIFO(cleanup_fn, pathname, mode) \ 150 safe_mkfifo(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode)) 151 152 #define SAFE_RENAME(cleanup_fn, oldpath, newpath) \ 153 safe_rename(__FILE__, __LINE__, (cleanup_fn), (oldpath), (newpath)) 154 155 #define SAFE_MOUNT(cleanup_fn, source, target, filesystemtype, \ 156 mountflags, data) \ 157 safe_mount(__FILE__, __LINE__, (cleanup_fn), (source), (target), \ 158 (filesystemtype), (mountflags), (data)) 159 160 #define SAFE_UMOUNT(cleanup_fn, target) \ 161 safe_umount(__FILE__, __LINE__, (cleanup_fn), (target)) 162 163 /* 164 * following functions are inline because the behaviour may depend on 165 * -D_FILE_OFFSET_BITS=64 -DOFF_T=__off64_t compile flags 166 */ 167 168 static inline void *safe_mmap(const char *file, const int lineno, 169 void (*cleanup_fn)(void), void *addr, size_t length, 170 int prot, int flags, int fd, off_t offset) 171 { 172 void *rval; 173 174 rval = mmap(addr, length, prot, flags, fd, offset); 175 if (rval == MAP_FAILED) { 176 tst_brkm(TBROK | TERRNO, cleanup_fn, 177 "%s:%d: mmap(%p,%zu,%d,%d,%d,%ld) failed", 178 file, lineno, addr, length, prot, flags, fd, 179 (long) offset); 180 } 181 182 return rval; 183 } 184 #define SAFE_MMAP(cleanup_fn, addr, length, prot, flags, fd, offset) \ 185 safe_mmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length), (prot), \ 186 (flags), (fd), (offset)) 187 188 static inline int safe_ftruncate(const char *file, const int lineno, 189 void (cleanup_fn) (void), int fd, off_t length) 190 { 191 int rval; 192 193 rval = ftruncate(fd, length); 194 if (rval == -1) { 195 tst_brkm(TBROK | TERRNO, cleanup_fn, 196 "%s:%d: ftruncate(%d,%ld) failed", 197 file, lineno, fd, (long)length); 198 } 199 200 return rval; 201 } 202 #define SAFE_FTRUNCATE(cleanup_fn, fd, length) \ 203 safe_ftruncate(__FILE__, __LINE__, cleanup_fn, (fd), (length)) 204 205 static inline int safe_truncate(const char *file, const int lineno, 206 void (cleanup_fn) (void), const char *path, off_t length) 207 { 208 int rval; 209 210 rval = truncate(path, length); 211 if (rval == -1) { 212 tst_brkm(TBROK | TERRNO, cleanup_fn, 213 "%s:%d: truncate(%s,%ld) failed", 214 file, lineno, path, (long)length); 215 } 216 217 return rval; 218 } 219 #define SAFE_TRUNCATE(cleanup_fn, path, length) \ 220 safe_truncate(__FILE__, __LINE__, cleanup_fn, (path), (length)) 221 222 static inline int safe_stat(const char *file, const int lineno, 223 void (cleanup_fn)(void), const char *path, struct stat *buf) 224 { 225 int rval; 226 227 rval = stat(path, buf); 228 229 if (rval == -1) { 230 tst_brkm(TBROK | TERRNO, cleanup_fn, 231 "%s:%d: stat(%s,%p) failed", file, lineno, path, buf); 232 } 233 234 return rval; 235 } 236 #define SAFE_STAT(cleanup_fn, path, buf) \ 237 safe_stat(__FILE__, __LINE__, (cleanup_fn), (path), (buf)) 238 239 static inline int safe_fstat(const char *file, const int lineno, 240 void (cleanup_fn)(void), int fd, struct stat *buf) 241 { 242 int rval; 243 244 rval = fstat(fd, buf); 245 246 if (rval == -1) { 247 tst_brkm(TBROK | TERRNO, cleanup_fn, 248 "%s:%d: fstat(%d,%p) failed", file, lineno, fd, buf); 249 } 250 251 return rval; 252 } 253 #define SAFE_FSTAT(cleanup_fn, fd, buf) \ 254 safe_fstat(__FILE__, __LINE__, (cleanup_fn), (fd), (buf)) 255 256 static inline int safe_lstat(const char *file, const int lineno, 257 void (cleanup_fn)(void), const char *path, struct stat *buf) 258 { 259 int rval; 260 261 rval = lstat(path, buf); 262 263 if (rval == -1) { 264 tst_brkm(TBROK | TERRNO, cleanup_fn, 265 "%s:%d: lstat(%s,%p) failed", file, lineno, path, buf); 266 } 267 268 return rval; 269 } 270 #define SAFE_LSTAT(cleanup_fn, path, buf) \ 271 safe_lstat(__FILE__, __LINE__, (cleanup_fn), (path), (buf)) 272 273 static inline off_t safe_lseek(const char *file, const int lineno, 274 void (cleanup_fn)(void), int fd, off_t offset, int whence) 275 { 276 off_t rval; 277 278 rval = lseek(fd, offset, whence); 279 280 if (rval == (off_t) -1) { 281 tst_brkm(TBROK | TERRNO, cleanup_fn, 282 "%s:%d: lseek(%d,%ld,%d) failed", 283 file, lineno, fd, (long)offset, whence); 284 } 285 286 return rval; 287 } 288 #define SAFE_LSEEK(cleanup_fn, fd, offset, whence) \ 289 safe_lseek(__FILE__, __LINE__, cleanup_fn, (fd), (offset), (whence)) 290 291 static inline int safe_getrlimit(const char *file, const int lineno, 292 void (cleanup_fn)(void), int resource, struct rlimit *rlim) 293 { 294 int rval; 295 296 rval = getrlimit(resource, rlim); 297 298 if (rval == -1) { 299 tst_brkm(TBROK | TERRNO, cleanup_fn, 300 "%s:%d: getrlimit(%d,%p) failed", 301 file, lineno, resource, rlim); 302 } 303 304 return rval; 305 } 306 #define SAFE_GETRLIMIT(cleanup_fn, resource, rlim) \ 307 safe_getrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim)) 308 309 static inline int safe_setrlimit(const char *file, const int lineno, 310 void (cleanup_fn)(void), int resource, const struct rlimit *rlim) 311 { 312 int rval; 313 314 rval = setrlimit(resource, rlim); 315 316 if (rval == -1) { 317 tst_brkm(TBROK | TERRNO, cleanup_fn, 318 "%s:%d: setrlimit(%d,%p) failed", 319 file, lineno, resource, rlim); 320 } 321 322 return rval; 323 } 324 #define SAFE_SETRLIMIT(cleanup_fn, resource, rlim) \ 325 safe_setrlimit(__FILE__, __LINE__, (cleanup_fn), (resource), (rlim)) 326 327 #define SAFE_OPENDIR(cleanup_fn, name) \ 328 safe_opendir(__FILE__, __LINE__, (cleanup_fn), (name)) 329 330 #define SAFE_CLOSEDIR(cleanup_fn, dirp) \ 331 safe_closedir(__FILE__, __LINE__, (cleanup_fn), (dirp)) 332 333 #define SAFE_READDIR(cleanup_fn, dirp) \ 334 safe_readdir(__FILE__, __LINE__, (cleanup_fn), (dirp)) 335 336 337 #define SAFE_IOCTL(cleanup_fn, fd, request, ...) \ 338 ({int ret = ioctl(fd, request, __VA_ARGS__); \ 339 ret < 0 ? \ 340 tst_brkm(TBROK | TERRNO, cleanup_fn, \ 341 "ioctl(%i,%s,...) failed", fd, #request) \ 342 : ret;}) 343 344 #endif /* __SAFE_MACROS_H__ */ 345 #endif /* __TEST_H__ */ 346