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); |
Garrett Cooper | ac0d5eb | 2012-05-20 14:05:35 -0700 | [diff] [blame] | 220 | if (rval == -1 || (len_strict && 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 | |
| 315 | int safe_symlink(const char *file, const int lineno, |
| 316 | void (cleanup_fn)(void), const char *oldpath, |
| 317 | const char *newpath) |
| 318 | { |
| 319 | int rval; |
| 320 | |
| 321 | rval = symlink(oldpath, newpath); |
| 322 | |
| 323 | if (rval == -1) { |
| 324 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 325 | "link(%s, %s) failed at %s:%d", |
| 326 | oldpath, newpath, file, lineno); |
| 327 | } |
| 328 | |
| 329 | return rval; |
| 330 | } |
| 331 | |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 332 | ssize_t |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 333 | safe_write(const char *file, const int lineno, void (cleanup_fn) (void), |
| 334 | char len_strict, int fildes, const void *buf, size_t nbyte) |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 335 | { |
| 336 | ssize_t rval; |
| 337 | |
| 338 | rval = write(fildes, buf, nbyte); |
| 339 | if ((len_strict == 0 && rval == -1) || rval != nbyte) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 340 | tst_brkm(TBROK | TERRNO, cleanup_fn, "write failed at %s:%d", |
| 341 | file, lineno); |
Garrett Cooper | dd3f47e | 2010-12-20 05:40:52 -0800 | [diff] [blame] | 342 | |
| 343 | return (rval); |
| 344 | } |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 345 | |
| 346 | int safe_ftruncate(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 347 | void (cleanup_fn) (void), int fd, off_t length) |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 348 | { |
| 349 | int rval; |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 350 | |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 351 | rval = ftruncate(fd, length); |
| 352 | if (rval == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 353 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 354 | "ftruncate failed at %s:%d", file, lineno); |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | return rval; |
| 358 | } |
| 359 | |
| 360 | int safe_truncate(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 361 | void (cleanup_fn) (void), const char *path, off_t length) |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 362 | { |
| 363 | int rval; |
| 364 | |
| 365 | rval = truncate(path, length); |
| 366 | if (rval == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 367 | tst_brkm(TBROK | TERRNO, cleanup_fn, "truncate failed at %s:%d", |
| 368 | file, lineno); |
Cyril Hrubis | 3f75fe4 | 2011-12-28 15:33:12 +0100 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | return rval; |
| 372 | } |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 373 | |
| 374 | long safe_strtol(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 375 | void (cleanup_fn) (void), char *str, long min, long max) |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 376 | { |
| 377 | long rval; |
| 378 | char *endptr; |
| 379 | |
| 380 | errno = 0; |
| 381 | rval = strtol(str, &endptr, 10); |
| 382 | if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN)) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 383 | || (errno != 0 && rval == 0)) |
| 384 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Caspar Zhang | d6a1f25 | 2012-02-09 15:58:28 +0800 | [diff] [blame] | 385 | "strtol failed at %s:%d", file, lineno); |
| 386 | if (rval > max || rval < min) |
| 387 | tst_brkm(TBROK, cleanup_fn, |
| 388 | "converted value out of range (%ld - %ld) at %s:%d", |
| 389 | min, max, file, lineno); |
| 390 | if (endptr == str || (*endptr != '\0' && *endptr != '\n')) |
| 391 | tst_brkm(TBROK, cleanup_fn, |
| 392 | "Invalid value: '%s' at %s:%d", str, file, lineno); |
| 393 | |
| 394 | return rval; |
| 395 | } |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 396 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 397 | unsigned long safe_strtoul(const char *file, const int lineno, |
| 398 | void (cleanup_fn) (void), char *str, |
| 399 | unsigned long min, unsigned long max) |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 400 | { |
| 401 | unsigned long rval; |
| 402 | char *endptr; |
| 403 | |
| 404 | errno = 0; |
| 405 | rval = strtoul(str, &endptr, 10); |
| 406 | if ((errno == ERANGE && rval == ULONG_MAX) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 407 | || (errno != 0 && rval == 0)) |
| 408 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
| 409 | "strtol failed at %s:%d", file, lineno); |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 410 | if (rval > max || rval < min) |
| 411 | tst_brkm(TBROK, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 412 | "converted value out of range (%lu - %lu at %s:%d", |
| 413 | min, max, file, lineno); |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 414 | if (endptr == str || (*endptr != '\0' && *endptr != '\n')) |
| 415 | tst_brkm(TBROK, cleanup_fn, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 416 | "Invalid value: '%s' at %s:%d", str, file, lineno); |
Zhouping Liu | 2b73a15 | 2012-07-07 23:14:39 +0800 | [diff] [blame] | 417 | |
| 418 | return rval; |
| 419 | } |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 420 | |
| 421 | long safe_sysconf(const char *file, const int lineno, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 422 | void (cleanup_fn) (void), int name) |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 423 | { |
| 424 | long rval; |
| 425 | errno = 0; |
| 426 | |
| 427 | rval = sysconf(name); |
| 428 | |
| 429 | if (rval == -1) { |
| 430 | if (errno == EINVAL) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 431 | tst_brkm(TBROK | TERRNO, cleanup_fn, |
Wanlong Gao | 3b535a8 | 2012-10-18 16:35:14 +0800 | [diff] [blame] | 432 | "sysconf failed at %s:%d", file, lineno); |
| 433 | else |
| 434 | tst_resm(TINFO, "queried option is not available" |
| 435 | " or thers is no definite limit at %s:%d", |
| 436 | file, lineno); |
| 437 | } |
| 438 | |
| 439 | return rval; |
| 440 | } |