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