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