Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 1 | #include <sys/types.h> |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 2 | #include <sys/mman.h> |
Caspar Zhang | 817c782 | 2011-06-30 01:50:33 +0800 | [diff] [blame] | 3 | #include <sys/resource.h> |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 4 | #include <sys/stat.h> |
Vinson Lee | 9af0bd7 | 2012-02-09 15:05:50 -0800 | [diff] [blame] | 5 | #include <errno.h> |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 6 | #include <fcntl.h> |
| 7 | #include <libgen.h> |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 8 | #include <limits.h> |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 9 | #include <pwd.h> |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 10 | #include <stdarg.h> |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 11 | #include <stdlib.h> |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 12 | #include <unistd.h> |
| 13 | #include "test.h" |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 14 | #include "safe_macros.h" |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 15 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 16 | char *safe_basename(const char *file, const int lineno, |
| 17 | void (*cleanup_fn) (void), char *path) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 18 | { |
| 19 | char *rval; |
| 20 | |
| 21 | rval = basename(path); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 22 | if (rval == NULL) { |
| 23 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 24 | "%s:%d: basename(%s) failed", |
| 25 | file, lineno, path); |
| 26 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 27 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 28 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | int |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 32 | safe_chdir(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 33 | const char *path) |
Garrett Cooper | 4a3c658 | 2010-12-21 07:07:01 -0800 | [diff] [blame] | 34 | { |
| 35 | int rval; |
| 36 | |
| 37 | rval = chdir(path); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 38 | if (rval == -1) { |
| 39 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 40 | "%s:%d: chdir(%s) failed", |
| 41 | file, lineno, path); |
| 42 | } |
Garrett Cooper | 4a3c658 | 2010-12-21 07:07:01 -0800 | [diff] [blame] | 43 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 44 | return rval; |
Garrett Cooper | 4a3c658 | 2010-12-21 07:07:01 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | int |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 48 | safe_close(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 49 | int fildes) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 50 | { |
| 51 | int rval; |
| 52 | |
| 53 | rval = close(fildes); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 54 | if (rval == -1) { |
| 55 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 56 | "%s:%d: close(%d) failed", |
| 57 | file, lineno, fildes); |
| 58 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 59 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 60 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | int |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 64 | safe_creat(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 65 | char *pathname, mode_t mode) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 66 | { |
| 67 | int rval; |
| 68 | |
| 69 | rval = creat(pathname, mode); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 70 | if (rval == -1) { |
| 71 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 72 | "%s:%d: creat(%s,0%o) failed", |
| 73 | file, lineno, pathname, mode); |
| 74 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 75 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 76 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 79 | char *safe_dirname(const char *file, const int lineno, |
| 80 | void (*cleanup_fn) (void), char *path) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 81 | { |
| 82 | char *rval; |
| 83 | |
| 84 | rval = dirname(path); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 85 | if (rval == NULL) { |
| 86 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 87 | "%s:%d: dirname(%s) failed", |
| 88 | file, lineno, path); |
| 89 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 90 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 91 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 94 | char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 95 | char *buf, size_t size) |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 96 | { |
| 97 | char *rval; |
| 98 | |
| 99 | rval = getcwd(buf, size); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 100 | if (rval == NULL) { |
| 101 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 102 | "%s:%d: getcwd(%p,%zu) failed", |
| 103 | file, lineno, buf, size); |
| 104 | } |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 105 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 106 | return rval; |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | struct passwd *safe_getpwnam(const char *file, const int lineno, |
| 110 | void (*cleanup_fn) (void), const char *name) |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 111 | { |
| 112 | struct passwd *rval; |
| 113 | |
| 114 | rval = getpwnam(name); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 115 | if (rval == NULL) { |
| 116 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 117 | "%s:%d: getpwnam(%s) failed", |
| 118 | file, lineno, name); |
| 119 | } |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 120 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 121 | return rval; |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Caspar Zhang | 817c782 | 2011-06-30 01:50:33 +0800 | [diff] [blame] | 124 | int |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 125 | safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 126 | int who, struct rusage *usage) |
Caspar Zhang | 817c782 | 2011-06-30 01:50:33 +0800 | [diff] [blame] | 127 | { |
| 128 | int rval; |
| 129 | |
| 130 | rval = getrusage(who, usage); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 131 | if (rval == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 132 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 133 | "%s:%d: getrusage(%d,%p) failed", |
| 134 | file, lineno, who, usage); |
| 135 | } |
Caspar Zhang | 817c782 | 2011-06-30 01:50:33 +0800 | [diff] [blame] | 136 | |
| 137 | return rval; |
| 138 | } |
| 139 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 140 | void *safe_malloc(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 141 | size_t size) |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 142 | { |
| 143 | void *rval; |
| 144 | |
| 145 | rval = malloc(size); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 146 | if (rval == NULL) { |
| 147 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 148 | "%s:%d: malloc(%zu) failed", |
| 149 | file, lineno, size); |
| 150 | } |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 151 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 152 | return rval; |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 155 | int safe_mkdir(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 156 | const char *pathname, mode_t mode) |
Garrett Cooper | 4a3c658 | 2010-12-21 07:07:01 -0800 | [diff] [blame] | 157 | { |
| 158 | int rval; |
| 159 | |
| 160 | rval = mkdir(pathname, mode); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 161 | if (rval == -1) { |
| 162 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 163 | "%s:%d: mkdir(%s,0%o) failed", |
| 164 | file, lineno, pathname, mode); |
| 165 | } |
Garrett Cooper | 4a3c658 | 2010-12-21 07:07:01 -0800 | [diff] [blame] | 166 | |
| 167 | return (rval); |
| 168 | } |
| 169 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 170 | void *safe_mmap(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 171 | void *addr, size_t length, int prot, int flags, int fd, |
| 172 | off_t offset) |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 173 | { |
| 174 | void *rval; |
| 175 | |
| 176 | rval = mmap(addr, length, prot, flags, fd, offset); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 177 | if (rval == MAP_FAILED) { |
| 178 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 179 | "%s:%d: mmap(%p,%zu,%d,%d,%d,%ld) failed", |
| 180 | file, lineno, addr, length, prot, flags, fd, |
| 181 | (long) offset); |
| 182 | } |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 183 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 184 | return rval; |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 185 | } |
| 186 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 187 | int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 188 | void *addr, size_t length) |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 189 | { |
| 190 | int rval; |
| 191 | |
| 192 | rval = munmap(addr, length); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 193 | if (rval == -1) { |
| 194 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 195 | "%s:%d: munmap(%p,%zu) failed", |
| 196 | file, lineno, addr, length); |
| 197 | } |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 198 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 199 | return rval; |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 202 | int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 203 | const char *pathname, int oflags, ...) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 204 | { |
| 205 | va_list ap; |
| 206 | int rval; |
| 207 | mode_t mode; |
| 208 | |
| 209 | va_start(ap, oflags); |
| 210 | mode = va_arg(ap, mode_t); |
| 211 | va_end(ap); |
| 212 | |
| 213 | rval = open(pathname, oflags, mode); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 214 | if (rval == -1) { |
| 215 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 216 | "%s:%d: open(%s,%d,0%o) failed", |
| 217 | file, lineno, pathname, oflags, mode); |
| 218 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 219 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 220 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 223 | int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 224 | int fildes[2]) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 225 | { |
| 226 | int rval; |
| 227 | |
| 228 | rval = pipe(fildes); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 229 | if (rval == -1) { |
| 230 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 231 | "%s:%d: pipe({%d,%d}) failed", |
| 232 | file, lineno, fildes[0], fildes[1]); |
| 233 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 234 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 235 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 236 | } |
| 237 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 238 | ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 239 | char len_strict, int fildes, void *buf, size_t nbyte) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 240 | { |
| 241 | ssize_t rval; |
| 242 | |
| 243 | rval = read(fildes, buf, nbyte); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 244 | if (rval == -1 || (len_strict && (size_t)rval != nbyte)) { |
| 245 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 246 | "%s:%d: read(%d,%p,%zu) failed, returned %zd", |
| 247 | file, lineno, fildes, buf, nbyte, rval); |
| 248 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 249 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 250 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 253 | int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 254 | gid_t egid) |
Garrett Cooper | 400c836 | 2010-12-20 20:02:01 -0800 | [diff] [blame] | 255 | { |
| 256 | int rval; |
| 257 | |
| 258 | rval = setegid(egid); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 259 | if (rval == -1) { |
| 260 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 261 | "%s:%d: setegid(%u) failed", |
| 262 | file, lineno, (unsigned) egid); |
| 263 | } |
Garrett Cooper | 400c836 | 2010-12-20 20:02:01 -0800 | [diff] [blame] | 264 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 265 | return rval; |
Garrett Cooper | 400c836 | 2010-12-20 20:02:01 -0800 | [diff] [blame] | 266 | } |
| 267 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 268 | int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 269 | uid_t euid) |
Garrett Cooper | 400c836 | 2010-12-20 20:02:01 -0800 | [diff] [blame] | 270 | { |
| 271 | int rval; |
| 272 | |
| 273 | rval = seteuid(euid); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 274 | if (rval == -1) { |
| 275 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 276 | "%s:%d: seteuid(%u) failed", |
| 277 | file, lineno, (unsigned) euid); |
| 278 | } |
Garrett Cooper | 400c836 | 2010-12-20 20:02:01 -0800 | [diff] [blame] | 279 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 280 | return rval; |
Garrett Cooper | 400c836 | 2010-12-20 20:02:01 -0800 | [diff] [blame] | 281 | } |
| 282 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 283 | int safe_setgid(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 284 | gid_t gid) |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 285 | { |
| 286 | int rval; |
| 287 | |
| 288 | rval = setgid(gid); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 289 | if (rval == -1) { |
| 290 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 291 | "%s:%d: setgid(%u) failed", |
| 292 | file, lineno, (unsigned) gid); |
| 293 | } |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 294 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 295 | return rval; |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 298 | int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 299 | uid_t uid) |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 300 | { |
| 301 | int rval; |
| 302 | |
| 303 | rval = setuid(uid); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 304 | if (rval == -1) { |
| 305 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 306 | "%s:%d: setuid(%u) failed", |
| 307 | file, lineno, (unsigned) uid); |
| 308 | } |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 309 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 310 | return rval; |
Garrett Cooper | ca43592 | 2010-12-20 12:26:32 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 313 | int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void), |
| 314 | const char *pathname) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 315 | { |
| 316 | int rval; |
| 317 | |
| 318 | rval = unlink(pathname); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 319 | if (rval == -1) { |
| 320 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 321 | "%s:%d: unlink(%s) failed", |
| 322 | file, lineno, pathname); |
| 323 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 324 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 325 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Cyril Hrubis | aede40b | 2013-04-15 19:33:23 +0200 | [diff] [blame] | 328 | |
| 329 | int safe_link(const char *file, const int lineno, |
| 330 | void (cleanup_fn)(void), const char *oldpath, |
| 331 | const char *newpath) |
| 332 | { |
| 333 | int rval; |
| 334 | |
| 335 | rval = link(oldpath, newpath); |
| 336 | |
| 337 | if (rval == -1) { |
| 338 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 339 | "%s:%d: link(%s,%s) failed", |
| 340 | file, lineno, oldpath, newpath); |
Cyril Hrubis | aede40b | 2013-04-15 19:33:23 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | return rval; |
| 344 | } |
| 345 | |
Cyril Hrubis | f095ccb | 2013-11-27 19:55:59 +0100 | [diff] [blame] | 346 | off_t safe_lseek(const char *file, const int lineno, |
| 347 | void (cleanup_fn)(void), int fd, |
| 348 | off_t offset, int whence) |
| 349 | { |
| 350 | off_t rval; |
| 351 | |
| 352 | rval = lseek(fd, offset, whence); |
| 353 | |
| 354 | if (rval == (off_t) -1) { |
| 355 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 356 | "%s:%d: lseek(%d,%ld,%d) failed", |
| 357 | file, lineno, fd, (long)offset, whence); |
Cyril Hrubis | f095ccb | 2013-11-27 19:55:59 +0100 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | return rval; |
| 361 | } |
| 362 | |
Cyril Hrubis | aede40b | 2013-04-15 19:33:23 +0200 | [diff] [blame] | 363 | int safe_symlink(const char *file, const int lineno, |
| 364 | void (cleanup_fn)(void), const char *oldpath, |
| 365 | const char *newpath) |
| 366 | { |
| 367 | int rval; |
| 368 | |
| 369 | rval = symlink(oldpath, newpath); |
| 370 | |
| 371 | if (rval == -1) { |
| 372 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 373 | "%s:%d: symlink(%s,%s) failed", |
| 374 | file, lineno, oldpath, newpath); |
Cyril Hrubis | aede40b | 2013-04-15 19:33:23 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | return rval; |
| 378 | } |
| 379 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 380 | ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void), |
| 381 | char len_strict, int fildes, const void *buf, size_t nbyte) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 382 | { |
| 383 | ssize_t rval; |
| 384 | |
| 385 | rval = write(fildes, buf, nbyte); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 386 | if ((len_strict == 0 && rval == -1) || (size_t)rval != nbyte) { |
| 387 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 388 | "%s:%d: write(%d,%p,%zu) failed", |
| 389 | file, lineno, fildes, buf, rval); |
| 390 | } |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 391 | |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 392 | return rval; |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 393 | } |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 394 | |
| 395 | int safe_ftruncate(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 396 | void (cleanup_fn) (void), int fd, off_t length) |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 397 | { |
| 398 | int rval; |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 399 | |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 400 | rval = ftruncate(fd, length); |
| 401 | if (rval == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 402 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 403 | "%s:%d: ftruncate(%d,%ld) failed", |
| 404 | file, lineno, fd, (long)length); |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | return rval; |
| 408 | } |
| 409 | |
| 410 | int safe_truncate(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 411 | void (cleanup_fn) (void), const char *path, off_t length) |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 412 | { |
| 413 | int rval; |
| 414 | |
| 415 | rval = truncate(path, length); |
| 416 | if (rval == -1) { |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 417 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 418 | "%s:%d: truncate(%s,%ld) failed", |
| 419 | file, lineno, path, (long)length); |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return rval; |
| 423 | } |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 424 | |
| 425 | long safe_strtol(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 426 | void (cleanup_fn) (void), char *str, long min, long max) |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 427 | { |
| 428 | long rval; |
| 429 | char *endptr; |
| 430 | |
| 431 | errno = 0; |
| 432 | rval = strtol(str, &endptr, 10); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 433 | |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 434 | if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN)) |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 435 | || (errno != 0 && rval == 0)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 436 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 437 | "%s:%d: strtol(%s) failed", file, lineno, str); |
| 438 | } |
| 439 | |
| 440 | if (endptr == str || (*endptr != '\0' && *endptr != '\n')) { |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 441 | tst_brkm(TBROK, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 442 | "%s:%d: strtol(%s): Invalid value", file, lineno, str); |
| 443 | } |
| 444 | |
| 445 | if (rval > max || rval < min) { |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 446 | tst_brkm(TBROK, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 447 | "%s:%d: strtol(%s): %ld is out of range %ld - %ld", |
| 448 | file, lineno, str, rval, min, max); |
| 449 | } |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 450 | |
| 451 | return rval; |
| 452 | } |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 453 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 454 | unsigned long safe_strtoul(const char *file, const int lineno, |
| 455 | void (cleanup_fn) (void), char *str, |
| 456 | unsigned long min, unsigned long max) |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 457 | { |
| 458 | unsigned long rval; |
| 459 | char *endptr; |
| 460 | |
| 461 | errno = 0; |
| 462 | rval = strtoul(str, &endptr, 10); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 463 | |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 464 | if ((errno == ERANGE && rval == ULONG_MAX) |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 465 | || (errno != 0 && rval == 0)) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 466 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 467 | "%s:%d: strtoul(%s) failed", file, lineno, str); |
| 468 | } |
| 469 | |
| 470 | if (rval > max || rval < min) { |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 471 | tst_brkm(TBROK, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 472 | "%s:%d: strtoul(%s): %lu is out of range %lu - %lu", |
| 473 | file, lineno, str, rval, min, max); |
| 474 | } |
| 475 | |
| 476 | if (endptr == str || (*endptr != '\0' && *endptr != '\n')) { |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 477 | tst_brkm(TBROK, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 478 | "Invalid value: '%s' at %s:%d", str, file, lineno); |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 479 | } |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 480 | |
| 481 | return rval; |
| 482 | } |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 483 | |
| 484 | long safe_sysconf(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 485 | void (cleanup_fn) (void), int name) |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 486 | { |
| 487 | long rval; |
| 488 | errno = 0; |
| 489 | |
| 490 | rval = sysconf(name); |
| 491 | |
| 492 | if (rval == -1) { |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 493 | if (errno) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 494 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 495 | "%s:%d: sysconf(%d) failed", |
| 496 | file, lineno, name); |
| 497 | } else { |
| 498 | tst_resm(TINFO, "%s:%d: sysconf(%d): " |
| 499 | "queried option is not available" |
| 500 | " or there is no definite limit", |
| 501 | file, lineno, name); |
| 502 | } |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | return rval; |
| 506 | } |
Zeng Linggang | 1030c9d | 2014-01-22 13:58:55 +0800 | [diff] [blame] | 507 | |
Cyril Hrubis | 7ee6327 | 2014-02-25 16:38:24 +0100 | [diff] [blame] | 508 | int safe_stat(const char *file, const int lineno, |
| 509 | void (cleanup_fn)(void), const char *path, struct stat *buf) |
| 510 | { |
| 511 | int rval; |
| 512 | |
| 513 | rval = stat(path, buf); |
| 514 | |
| 515 | if (rval == -1) { |
| 516 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 517 | "%s:%d: stat(%s,%p) failed", file, lineno, path, buf); |
Cyril Hrubis | 7ee6327 | 2014-02-25 16:38:24 +0100 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | return rval; |
| 521 | } |
| 522 | |
Zeng Linggang | 1030c9d | 2014-01-22 13:58:55 +0800 | [diff] [blame] | 523 | int safe_fstat(const char *file, const int lineno, |
| 524 | void (cleanup_fn)(void), int fd, struct stat *buf) |
| 525 | { |
| 526 | int rval; |
| 527 | |
| 528 | rval = fstat(fd, buf); |
| 529 | |
| 530 | if (rval == -1) { |
| 531 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 532 | "%s:%d: fstat(%d,%p) failed", file, lineno, fd, buf); |
Cyril Hrubis | 7ee6327 | 2014-02-25 16:38:24 +0100 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | return rval; |
| 536 | } |
| 537 | |
| 538 | int safe_lstat(const char *file, const int lineno, |
| 539 | void (cleanup_fn)(void), const char *path, struct stat *buf) |
| 540 | { |
| 541 | int rval; |
| 542 | |
| 543 | rval = lstat(path, buf); |
| 544 | |
| 545 | if (rval == -1) { |
| 546 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 547 | "%s:%d: lstat(%s,%p) failed", file, lineno, path, buf); |
Zeng Linggang | 1030c9d | 2014-01-22 13:58:55 +0800 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | return rval; |
| 551 | } |
Zeng Linggang | ef9aac6 | 2014-03-03 19:22:55 +0800 | [diff] [blame] | 552 | |
| 553 | int safe_getrlimit(const char *file, const int lineno, |
| 554 | void (cleanup_fn)(void), int resource, struct rlimit *rlim) |
| 555 | { |
| 556 | int rval; |
| 557 | |
| 558 | rval = getrlimit(resource, rlim); |
| 559 | |
| 560 | if (rval == -1) { |
| 561 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 562 | "%s:%d: getrlimit(%d,%p) failed", |
| 563 | file, lineno, resource, rlim); |
Zeng Linggang | ef9aac6 | 2014-03-03 19:22:55 +0800 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | return rval; |
| 567 | } |
| 568 | |
| 569 | int safe_setrlimit(const char *file, const int lineno, void (cleanup_fn)(void), |
| 570 | int resource, const struct rlimit *rlim) |
| 571 | { |
| 572 | int rval; |
| 573 | |
| 574 | rval = setrlimit(resource, rlim); |
| 575 | |
| 576 | if (rval == -1) { |
| 577 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 578 | "%s:%d: setrlimit(%d,%p) failed", |
| 579 | file, lineno, resource, rlim); |
Zeng Linggang | ef9aac6 | 2014-03-03 19:22:55 +0800 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | return rval; |
| 583 | } |
Cyril Hrubis | c459235 | 2014-03-04 16:41:02 +0100 | [diff] [blame] | 584 | |
| 585 | int safe_chmod(const char *file, const int lineno, |
| 586 | void (cleanup_fn)(void), const char *path, mode_t mode) |
| 587 | { |
| 588 | int rval; |
| 589 | |
| 590 | rval = chmod(path, mode); |
| 591 | |
| 592 | if (rval == -1) { |
| 593 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 594 | "%s:%d: chmod(%s,0%o) failed", |
| 595 | file, lineno, path, mode); |
Cyril Hrubis | c459235 | 2014-03-04 16:41:02 +0100 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | return rval; |
| 599 | } |
| 600 | |
| 601 | int safe_fchmod(const char *file, const int lineno, |
| 602 | void (cleanup_fn)(void), int fd, mode_t mode) |
| 603 | { |
| 604 | int rval; |
| 605 | |
| 606 | rval = fchmod(fd, mode); |
| 607 | |
| 608 | if (rval == -1) { |
| 609 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Mats Liljegren | 49e8615 | 2014-04-16 19:27:58 +0200 | [diff] [blame^] | 610 | "%s:%d: fchmod(%d,0%o) failed", |
| 611 | file, lineno, fd, mode); |
Cyril Hrubis | c459235 | 2014-03-04 16:41:02 +0100 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | return rval; |
| 615 | } |