Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 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 <sys/types.h> |
Caspar Zhang | 1c9037b | 2011-11-15 21:43:59 +0800 | [diff] [blame] | 20 | #include <sys/resource.h> |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <libgen.h> |
| 24 | #include <stdarg.h> |
| 25 | #include <unistd.h> |
Cyril Hrubis | d2085ca | 2013-06-18 14:47:19 +0200 | [diff] [blame^] | 26 | #include <stdio.h> |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 27 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 28 | char* safe_basename(const char *file, const int lineno, |
| 29 | void (*cleanup_fn)(void), char *path); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 30 | #define SAFE_BASENAME(cleanup_fn, path) \ |
| 31 | safe_basename(__FILE__, __LINE__, (cleanup_fn), (path)) |
| 32 | |
Garrett Cooper | 4a3c658 | 2010-12-21 07:07:01 -0800 | [diff] [blame] | 33 | int safe_chdir(const char *file, const int lineno, |
| 34 | void (*cleanup_fn)(void), const char *path); |
| 35 | #define SAFE_CHDIR(cleanup_fn, path) \ |
| 36 | safe_chdir(__FILE__, __LINE__, (cleanup_fn), (path)) |
| 37 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 38 | int safe_close(const char *file, const int lineno, |
| 39 | void (*cleanup_fn)(void), int fildes); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 40 | #define SAFE_CLOSE(cleanup_fn, fildes) \ |
| 41 | safe_close(__FILE__, __LINE__, (cleanup_fn), (fildes)) |
| 42 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 43 | int safe_creat(const char *file, const int lineno, |
| 44 | void (*cleanup_fn)(void), char *pathname, mode_t mode); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 45 | #define SAFE_CREAT(cleanup_fn, pathname, mode) \ |
| 46 | safe_creat(__FILE__, __LINE__, cleanup_fn, (pathname), (mode)) |
| 47 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 48 | char* safe_dirname(const char *file, const int lineno, |
| 49 | void (*cleanup_fn)(void), char *path); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 50 | #define SAFE_DIRNAME(cleanup_fn, path) \ |
| 51 | safe_dirname(__FILE__, __LINE__, (cleanup_fn), (path)) |
| 52 | |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 53 | char* safe_getcwd(const char *file, const int lineno, |
| 54 | void (*cleanup_fn)(void), char *buf, size_t size); |
| 55 | #define SAFE_GETCWD(cleanup_fn, buf, size) \ |
| 56 | safe_getcwd(__FILE__, __LINE__, (cleanup_fn), (buf), (size)) |
| 57 | |
| 58 | struct passwd* safe_getpwnam(const char *file, const int lineno, |
| 59 | void (*cleanup_fn)(void), const char *name); |
| 60 | #define SAFE_GETPWNAM(cleanup_fn, name) \ |
| 61 | safe_getpwnam(__FILE__, __LINE__, cleanup_fn, (name)) |
| 62 | |
Caspar Zhang | 817c782 | 2011-06-30 01:50:33 +0800 | [diff] [blame] | 63 | int safe_getrusage(const char *file, const int lineno, |
| 64 | void (*cleanup_fn)(void), int who, struct rusage *usage); |
| 65 | #define SAFE_GETRUSAGE(cleanup_fn, who, usage) \ |
| 66 | safe_getrusage(__FILE__, __LINE__, (cleanup_fn), (who), (usage)) |
| 67 | |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 68 | void* safe_malloc(const char *file, const int lineno, |
| 69 | void (*cleanup_fn)(void), size_t size); |
| 70 | #define SAFE_MALLOC(cleanup_fn, size) \ |
| 71 | safe_malloc(__FILE__, __LINE__, (cleanup_fn), (size)) |
| 72 | |
Garrett Cooper | 4a3c658 | 2010-12-21 07:07:01 -0800 | [diff] [blame] | 73 | int safe_mkdir(const char *file, const int lineno, |
| 74 | void (*cleanup_fn)(void), const char *pathname, mode_t mode); |
| 75 | #define SAFE_MKDIR(cleanup_fn, pathname, mode) \ |
| 76 | safe_mkdir(__FILE__, __LINE__, (cleanup_fn), (pathname), (mode)) |
| 77 | |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 78 | void* safe_mmap(const char *file, const int lineno, |
| 79 | void (*cleanup_fn)(void), void *addr, size_t length, int prot, |
| 80 | int flags, int fd, off_t offset); |
| 81 | #define SAFE_MMAP(cleanup_fn, addr, length, prot, flags, fd, offset) \ |
| 82 | safe_mmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length), (prot), \ |
| 83 | (flags), (fd), (offset)) |
| 84 | |
| 85 | int safe_munmap(const char *file, const int lineno, |
| 86 | void (*cleanup_fn)(void), void *addr, size_t length); |
| 87 | #define SAFE_MUNMAP(cleanup_fn, addr, length) \ |
| 88 | safe_munmap(__FILE__, __LINE__, (cleanup_fn), (addr), (length)) |
| 89 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 90 | int safe_open(const char *file, const int lineno, |
| 91 | void (*cleanup_fn)(void), const char *pathname, int oflags, ...); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 92 | #define SAFE_OPEN(cleanup_fn, pathname, oflags, ...) \ |
| 93 | safe_open(__FILE__, __LINE__, (cleanup_fn), (pathname), (oflags), \ |
| 94 | ##__VA_ARGS__) |
| 95 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 96 | int safe_pipe(const char *file, const int lineno, |
| 97 | void (*cleanup_fn)(void), int fildes[2]); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 98 | #define SAFE_PIPE(cleanup_fn, fildes) \ |
| 99 | safe_pipe(__FILE__, __LINE__, cleanup_fn, (fildes)) |
| 100 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 101 | ssize_t safe_read(const char *file, const int lineno, |
| 102 | void (*cleanup_fn)(void), char len_strict, int fildes, void *buf, |
| 103 | size_t nbyte); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 104 | #define SAFE_READ(cleanup_fn, len_strict, fildes, buf, nbyte) \ |
| 105 | safe_read(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \ |
| 106 | (buf), (nbyte)) |
| 107 | |
Garrett Cooper | 400c836 | 2010-12-20 20:02:01 -0800 | [diff] [blame] | 108 | int safe_setegid(const char *file, const int lineno, |
| 109 | void (*cleanup_fn)(void), gid_t egid); |
| 110 | #define SAFE_SETEGID(cleanup_fn, egid) \ |
| 111 | safe_setegid(__FILE__, __LINE__, cleanup_fn, (egid)) |
| 112 | |
| 113 | int safe_seteuid(const char *file, const int lineno, |
| 114 | void (*cleanup_fn)(void), uid_t euid); |
| 115 | #define SAFE_SETEUID(cleanup_fn, euid) \ |
| 116 | safe_seteuid(__FILE__, __LINE__, cleanup_fn, (euid)) |
| 117 | |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 118 | int safe_setgid(const char *file, const int lineno, |
| 119 | void (*cleanup_fn)(void), gid_t gid); |
| 120 | #define SAFE_SETGID(cleanup_fn, gid) \ |
| 121 | safe_setgid(__FILE__, __LINE__, cleanup_fn, (gid)) |
| 122 | |
| 123 | int safe_setuid(const char *file, const int lineno, |
| 124 | void (*cleanup_fn)(void), uid_t uid); |
Cyril Hrubis | aede40b | 2013-04-15 19:33:23 +0200 | [diff] [blame] | 125 | #define SAFE_SETUID(cleanup_fn, uid) \ |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 126 | safe_setuid(__FILE__, __LINE__, cleanup_fn, (uid)) |
| 127 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 128 | int safe_unlink(const char *file, const int lineno, |
| 129 | void (*cleanup_fn)(void), const char *pathname); |
Cyril Hrubis | aede40b | 2013-04-15 19:33:23 +0200 | [diff] [blame] | 130 | #define SAFE_UNLINK(cleanup_fn, pathname) \ |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 131 | safe_unlink(__FILE__, __LINE__, cleanup_fn, (pathname)) |
| 132 | |
Cyril Hrubis | aede40b | 2013-04-15 19:33:23 +0200 | [diff] [blame] | 133 | int safe_link(const char *file, const int lineno, |
| 134 | void (cleanup_fn)(void), const char *oldpath, |
| 135 | const char *newpath); |
| 136 | #define SAFE_LINK(cleanup_fn, oldpath, newpath) \ |
| 137 | safe_link(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath)) |
| 138 | |
| 139 | int safe_symlink(const char *file, const int lineno, |
| 140 | void (cleanup_fn)(void), const char *oldpath, |
| 141 | const char *newpath); |
| 142 | #define SAFE_SYMLINK(cleanup_fn, oldpath, newpath) \ |
| 143 | safe_symlink(__FILE__, __LINE__, cleanup_fn, (oldpath), (newpath)) |
| 144 | |
Garrett Cooper | c674396 | 2010-12-20 05:38:46 -0800 | [diff] [blame] | 145 | ssize_t safe_write(const char *file, const int lineno, |
| 146 | void (cleanup_fn)(void), char len_strict, int fildes, |
| 147 | const void *buf, size_t nbyte); |
Garrett Cooper | 1c2939f | 2010-12-20 05:12:53 -0800 | [diff] [blame] | 148 | #define SAFE_WRITE(cleanup_fn, len_strict, fildes, buf, nbyte) \ |
| 149 | safe_write(__FILE__, __LINE__, cleanup_fn, (len_strict), (fildes), \ |
| 150 | (buf), (nbyte)) |
| 151 | |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 152 | int safe_ftruncate(const char *file, const int lineno, |
| 153 | void (cleanup_fn)(void), int fd, off_t length); |
| 154 | #define SAFE_FTRUNCATE(cleanup_fn, fd, length) \ |
| 155 | safe_ftruncate(__FILE__, __LINE__, cleanup_fn, (fd), (length)) |
| 156 | |
| 157 | int safe_truncate(const char *file, const int lineno, |
| 158 | void (cleanup_fn)(void), const char *path, off_t length); |
| 159 | #define SAFE_TRUNCATE(cleanup_fn, fd, length) \ |
| 160 | safe_truncate(__FILE__, __LINE__, cleanup_fn, (path), (length)) |
| 161 | |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 162 | long safe_strtol(const char *file, const int lineno, |
| 163 | void (cleanup_fn)(void), char *str, long min, long max); |
| 164 | #define SAFE_STRTOL(cleanup_fn, str, min, max) \ |
| 165 | safe_strtol(__FILE__, __LINE__, cleanup_fn, (str), (min), (max)) |
| 166 | |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 167 | unsigned long safe_strtoul(const char *file, const int lineno, void (cleanup_fn)(void), |
| 168 | char *str, unsigned long min, unsigned long max); |
| 169 | #define SAFE_STRTOUL(cleanup_fn, str, min, max) \ |
| 170 | safe_strtoul(__FILE__, __LINE__, cleanup_fn, (str), (min), (max)) |
| 171 | |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 172 | long safe_sysconf(const char *file, const int lineno, |
| 173 | void (cleanup_fn)(void), int name); |
| 174 | #define SAFE_SYSCONF(cleanup_fn, name) \ |
Cyril Hrubis | 75cb911 | 2013-04-15 18:56:36 +0200 | [diff] [blame] | 175 | safe_sysconf(__FILE__, __LINE__, cleanup_fn, name) |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 176 | |
Cyril Hrubis | d2085ca | 2013-06-18 14:47:19 +0200 | [diff] [blame^] | 177 | FILE *safe_fopen(const char *file, const int lineno, void (cleanup_fn)(void), |
| 178 | const char *path, const char *mode); |
| 179 | #define SAFE_FOPEN(cleanup_fn, path, mode) \ |
| 180 | safe_fopen(__FILE__, __LINE__, cleanup_fn, path, mode) |
| 181 | |
| 182 | int safe_fclose(const char *file, const int lineno, void (cleanup_fn)(void), |
| 183 | FILE *f); |
| 184 | #define SAFE_FCLOSE(cleanup_fn, f) \ |
| 185 | safe_fclose(__FILE__, __LINE__, cleanup_fn, f) |
| 186 | |
Cyril Hrubis | 75cb911 | 2013-04-15 18:56:36 +0200 | [diff] [blame] | 187 | #endif /* __SAFE_MACROS_H__ */ |
| 188 | #endif /* __TEST_H__ */ |