Home | History | Annotate | Download | only in lib
      1 #define _GNU_SOURCE
      2 #include <sys/types.h>
      3 #include <sys/mman.h>
      4 #include <sys/resource.h>
      5 #include <sys/stat.h>
      6 #include <sys/wait.h>
      7 #include <sys/mount.h>
      8 #include <sys/xattr.h>
      9 #include <errno.h>
     10 #include <fcntl.h>
     11 #include <libgen.h>
     12 #include <limits.h>
     13 #include <pwd.h>
     14 #include <stdarg.h>
     15 #include <stdlib.h>
     16 #include <unistd.h>
     17 #include <malloc.h>
     18 #include "test.h"
     19 #include "safe_macros.h"
     20 
     21 char *safe_basename(const char *file, const int lineno,
     22 		    void (*cleanup_fn) (void), char *path)
     23 {
     24 	char *rval;
     25 
     26 	rval = basename(path);
     27 	if (rval == NULL) {
     28 		tst_brkm(TBROK | TERRNO, cleanup_fn,
     29 			 "%s:%d: basename(%s) failed",
     30 			 file, lineno, path);
     31 	}
     32 
     33 	return rval;
     34 }
     35 
     36 int
     37 safe_chdir(const char *file, const int lineno, void (*cleanup_fn) (void),
     38 	   const char *path)
     39 {
     40 	int rval;
     41 
     42 	rval = chdir(path);
     43 	if (rval == -1) {
     44 		tst_brkm(TBROK | TERRNO, cleanup_fn,
     45 			 "%s:%d: chdir(%s) failed",
     46 			 file, lineno, path);
     47 	}
     48 
     49 	return rval;
     50 }
     51 
     52 int
     53 safe_close(const char *file, const int lineno, void (*cleanup_fn) (void),
     54 	   int fildes)
     55 {
     56 	int rval;
     57 
     58 	rval = close(fildes);
     59 	if (rval == -1) {
     60 		tst_brkm(TBROK | TERRNO, cleanup_fn,
     61 			 "%s:%d: close(%d) failed",
     62 			 file, lineno, fildes);
     63 	}
     64 
     65 	return rval;
     66 }
     67 
     68 int
     69 safe_creat(const char *file, const int lineno, void (*cleanup_fn) (void),
     70 	   const char *pathname, mode_t mode)
     71 {
     72 	int rval;
     73 
     74 	rval = creat(pathname, mode);
     75 	if (rval == -1) {
     76 		tst_brkm(TBROK | TERRNO, cleanup_fn,
     77 			 "%s:%d: creat(%s,0%o) failed",
     78 			 file, lineno, pathname, mode);
     79 	}
     80 
     81 	return rval;
     82 }
     83 
     84 char *safe_dirname(const char *file, const int lineno,
     85 		   void (*cleanup_fn) (void), char *path)
     86 {
     87 	char *rval;
     88 
     89 	rval = dirname(path);
     90 	if (rval == NULL) {
     91 		tst_brkm(TBROK | TERRNO, cleanup_fn,
     92 			 "%s:%d: dirname(%s) failed",
     93 			 file, lineno, path);
     94 	}
     95 
     96 	return rval;
     97 }
     98 
     99 char *safe_getcwd(const char *file, const int lineno, void (*cleanup_fn) (void),
    100 		  char *buf, size_t size)
    101 {
    102 	char *rval;
    103 
    104 	rval = getcwd(buf, size);
    105 	if (rval == NULL) {
    106 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    107 			 "%s:%d: getcwd(%p,%zu) failed",
    108 			 file, lineno, buf, size);
    109 	}
    110 
    111 	return rval;
    112 }
    113 
    114 struct passwd *safe_getpwnam(const char *file, const int lineno,
    115 			     void (*cleanup_fn) (void), const char *name)
    116 {
    117 	struct passwd *rval;
    118 
    119 	rval = getpwnam(name);
    120 	if (rval == NULL) {
    121 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    122 			 "%s:%d: getpwnam(%s) failed",
    123 			 file, lineno, name);
    124 	}
    125 
    126 	return rval;
    127 }
    128 
    129 int
    130 safe_getrusage(const char *file, const int lineno, void (*cleanup_fn) (void),
    131 	       int who, struct rusage *usage)
    132 {
    133 	int rval;
    134 
    135 	rval = getrusage(who, usage);
    136 	if (rval == -1) {
    137 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    138 			 "%s:%d: getrusage(%d,%p) failed",
    139 			 file, lineno, who, usage);
    140 	}
    141 
    142 	return rval;
    143 }
    144 
    145 void *safe_malloc(const char *file, const int lineno, void (*cleanup_fn) (void),
    146 		  size_t size)
    147 {
    148 	void *rval;
    149 
    150 	rval = malloc(size);
    151 	if (rval == NULL) {
    152 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    153 			 "%s:%d: malloc(%zu) failed",
    154 			 file, lineno, size);
    155 	}
    156 
    157 	return rval;
    158 }
    159 
    160 int safe_mkdir(const char *file, const int lineno, void (*cleanup_fn) (void),
    161                const char *pathname, mode_t mode)
    162 {
    163 	int rval;
    164 
    165 	rval = mkdir(pathname, mode);
    166 	if (rval == -1) {
    167 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    168 			 "%s:%d: mkdir(%s,0%o) failed",
    169 			 file, lineno, pathname, mode);
    170 	}
    171 
    172 	return (rval);
    173 }
    174 
    175 int safe_rmdir(const char *file, const int lineno, void (*cleanup_fn) (void),
    176                const char *pathname)
    177 {
    178 	int rval;
    179 
    180 	rval = rmdir(pathname);
    181 	if (rval == -1) {
    182 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    183 			 "%s:%d: rmdir(%s) failed",
    184 			 file, lineno, pathname);
    185 	}
    186 
    187 	return (rval);
    188 }
    189 
    190 int safe_munmap(const char *file, const int lineno, void (*cleanup_fn) (void),
    191                 void *addr, size_t length)
    192 {
    193 	int rval;
    194 
    195 	rval = munmap(addr, length);
    196 	if (rval == -1) {
    197 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    198 			 "%s:%d: munmap(%p,%zu) failed",
    199 			 file, lineno, addr, length);
    200 	}
    201 
    202 	return rval;
    203 }
    204 
    205 int safe_open(const char *file, const int lineno, void (*cleanup_fn) (void),
    206               const char *pathname, int oflags, ...)
    207 {
    208 	va_list ap;
    209 	int rval;
    210 	mode_t mode;
    211 
    212 	va_start(ap, oflags);
    213 
    214 	/* Android's NDK's mode_t is smaller than an int, which results in
    215 	 * SIGILL here when passing the mode_t type.
    216 	 */
    217 	mode = va_arg(ap, int);
    218 
    219 	va_end(ap);
    220 
    221 	rval = open(pathname, oflags, mode);
    222 	if (rval == -1) {
    223 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    224 			 "%s:%d: open(%s,%d,0%o) failed",
    225 			 file, lineno, pathname, oflags, mode);
    226 	}
    227 
    228 	return rval;
    229 }
    230 
    231 int safe_pipe(const char *file, const int lineno, void (*cleanup_fn) (void),
    232               int fildes[2])
    233 {
    234 	int rval;
    235 
    236 	rval = pipe(fildes);
    237 	if (rval == -1) {
    238 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    239 			 "%s:%d: pipe({%d,%d}) failed",
    240 			 file, lineno, fildes[0], fildes[1]);
    241 	}
    242 
    243 	return rval;
    244 }
    245 
    246 ssize_t safe_read(const char *file, const int lineno, void (*cleanup_fn) (void),
    247                   char len_strict, int fildes, void *buf, size_t nbyte)
    248 {
    249 	ssize_t rval;
    250 
    251 	rval = read(fildes, buf, nbyte);
    252 	if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
    253 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    254 			 "%s:%d: read(%d,%p,%zu) failed, returned %zd",
    255 			 file, lineno, fildes, buf, nbyte, rval);
    256 	}
    257 
    258 	return rval;
    259 }
    260 
    261 int safe_setegid(const char *file, const int lineno, void (*cleanup_fn) (void),
    262                  gid_t egid)
    263 {
    264 	int rval;
    265 
    266 	rval = setegid(egid);
    267 	if (rval == -1) {
    268 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    269 			 "%s:%d: setegid(%u) failed",
    270 			 file, lineno, (unsigned) egid);
    271 	}
    272 
    273 	return rval;
    274 }
    275 
    276 int safe_seteuid(const char *file, const int lineno, void (*cleanup_fn) (void),
    277                  uid_t euid)
    278 {
    279 	int rval;
    280 
    281 	rval = seteuid(euid);
    282 	if (rval == -1) {
    283 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    284 			 "%s:%d: seteuid(%u) failed",
    285 			 file, lineno, (unsigned) euid);
    286 	}
    287 
    288 	return rval;
    289 }
    290 
    291 int safe_setgid(const char *file, const int lineno, void (*cleanup_fn) (void),
    292                 gid_t gid)
    293 {
    294 	int rval;
    295 
    296 	rval = setgid(gid);
    297 	if (rval == -1) {
    298 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    299 			 "%s:%d: setgid(%u) failed",
    300 			 file, lineno, (unsigned) gid);
    301 	}
    302 
    303 	return rval;
    304 }
    305 
    306 int safe_setuid(const char *file, const int lineno, void (*cleanup_fn) (void),
    307                 uid_t uid)
    308 {
    309 	int rval;
    310 
    311 	rval = setuid(uid);
    312 	if (rval == -1) {
    313 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    314 			 "%s:%d: setuid(%u) failed",
    315 			 file, lineno, (unsigned) uid);
    316 	}
    317 
    318 	return rval;
    319 }
    320 
    321 int safe_getresuid(const char *file, const int lineno, void (*cleanup_fn)(void),
    322 		   uid_t *ruid, uid_t *euid, uid_t *suid)
    323 {
    324 	int rval;
    325 
    326 	rval = getresuid(ruid, euid, suid);
    327 	if (rval == -1) {
    328 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    329 			 "%s:%d: getresuid(%p, %p, %p) failed",
    330 			 file, lineno, ruid, euid, suid);
    331 	}
    332 
    333 	return rval;
    334 }
    335 
    336 int safe_getresgid(const char *file, const int lineno, void (*cleanup_fn)(void),
    337 		   gid_t *rgid, gid_t *egid, gid_t *sgid)
    338 {
    339 	int rval;
    340 
    341 	rval = getresgid(rgid, egid, sgid);
    342 	if (rval == -1) {
    343 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    344 			 "%s:%d: getresgid(%p, %p, %p) failed",
    345 			 file, lineno, rgid, egid, sgid);
    346 	}
    347 
    348 	return rval;
    349 }
    350 
    351 int safe_unlink(const char *file, const int lineno, void (*cleanup_fn) (void),
    352                 const char *pathname)
    353 {
    354 	int rval;
    355 
    356 	rval = unlink(pathname);
    357 	if (rval == -1) {
    358 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    359 			 "%s:%d: unlink(%s) failed",
    360 			 file, lineno, pathname);
    361 	}
    362 
    363 	return rval;
    364 }
    365 
    366 
    367 int safe_link(const char *file, const int lineno,
    368               void (cleanup_fn)(void), const char *oldpath,
    369               const char *newpath)
    370 {
    371 	int rval;
    372 
    373 	rval = link(oldpath, newpath);
    374 
    375 	if (rval == -1) {
    376 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    377 		         "%s:%d: link(%s,%s) failed",
    378 			 file, lineno, oldpath, newpath);
    379 	}
    380 
    381 	return rval;
    382 }
    383 
    384 int safe_linkat(const char *file, const int lineno,
    385 		void (cleanup_fn)(void), int olddirfd, const char *oldpath,
    386 		int newdirfd, const char *newpath, int flags)
    387 {
    388 	int rval;
    389 
    390 	rval = linkat(olddirfd, oldpath, newdirfd, newpath, flags);
    391 
    392 	if (rval == -1) {
    393 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    394 			 "%s:%d: linkat(%d,%s,%d,%s,%d) failed",
    395 			 file, lineno, olddirfd, oldpath, newdirfd,
    396 			 newpath, flags);
    397 	}
    398 
    399 	return rval;
    400 }
    401 
    402 ssize_t safe_readlink(const char *file, const int lineno,
    403 		  void (cleanup_fn)(void), const char *path,
    404 		  char *buf, size_t bufsize)
    405 {
    406 	ssize_t rval;
    407 
    408 	rval = readlink(path, buf, bufsize);
    409 
    410 	if (rval == -1) {
    411 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    412 			 "%s:%d: readlink(%s,%p,%zu) failed",
    413 			 file, lineno, path, buf, bufsize);
    414 	} else {
    415 		/* readlink does not append a NUL byte to the buffer.
    416 		 * Add it now. */
    417 		if ((size_t) rval < bufsize)
    418 			buf[rval] = '\0';
    419 		else
    420 			buf[bufsize-1] = '\0';
    421 	}
    422 
    423 	return rval;
    424 }
    425 
    426 int safe_symlink(const char *file, const int lineno,
    427                  void (cleanup_fn)(void), const char *oldpath,
    428                  const char *newpath)
    429 {
    430 	int rval;
    431 
    432 	rval = symlink(oldpath, newpath);
    433 
    434 	if (rval == -1) {
    435 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    436 		         "%s:%d: symlink(%s,%s) failed",
    437 			 file, lineno, oldpath, newpath);
    438 	}
    439 
    440 	return rval;
    441 }
    442 
    443 ssize_t safe_write(const char *file, const int lineno, void (cleanup_fn) (void),
    444                    char len_strict, int fildes, const void *buf, size_t nbyte)
    445 {
    446 	ssize_t rval;
    447 
    448 	rval = write(fildes, buf, nbyte);
    449 	if (rval == -1 || (len_strict && (size_t)rval != nbyte)) {
    450 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    451 			 "%s:%d: write(%d,%p,%zu) failed",
    452 		         file, lineno, fildes, buf, rval);
    453 	}
    454 
    455 	return rval;
    456 }
    457 
    458 long safe_strtol(const char *file, const int lineno,
    459 		 void (cleanup_fn) (void), char *str, long min, long max)
    460 {
    461 	long rval;
    462 	char *endptr;
    463 
    464 	errno = 0;
    465 	rval = strtol(str, &endptr, 10);
    466 
    467 	if ((errno == ERANGE && (rval == LONG_MAX || rval == LONG_MIN))
    468 	    || (errno != 0 && rval == 0)) {
    469 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    470 			 "%s:%d: strtol(%s) failed", file, lineno, str);
    471 		return rval;
    472 	}
    473 
    474 	if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
    475 		tst_brkm(TBROK, cleanup_fn,
    476 			 "%s:%d: strtol(%s): Invalid value", file, lineno, str);
    477 		return 0;
    478 	}
    479 
    480 	if (rval > max || rval < min) {
    481 		tst_brkm(TBROK, cleanup_fn,
    482 			 "%s:%d: strtol(%s): %ld is out of range %ld - %ld",
    483 			 file, lineno, str, rval, min, max);
    484 		return 0;
    485 	}
    486 
    487 	return rval;
    488 }
    489 
    490 unsigned long safe_strtoul(const char *file, const int lineno,
    491 			   void (cleanup_fn) (void), char *str,
    492 			   unsigned long min, unsigned long max)
    493 {
    494 	unsigned long rval;
    495 	char *endptr;
    496 
    497 	errno = 0;
    498 	rval = strtoul(str, &endptr, 10);
    499 
    500 	if ((errno == ERANGE && rval == ULONG_MAX)
    501 	    || (errno != 0 && rval == 0)) {
    502 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    503 			 "%s:%d: strtoul(%s) failed", file, lineno, str);
    504 		return rval;
    505 	}
    506 
    507 	if (rval > max || rval < min) {
    508 		tst_brkm(TBROK, cleanup_fn,
    509 			 "%s:%d: strtoul(%s): %lu is out of range %lu - %lu",
    510 			 file, lineno, str, rval, min, max);
    511 		return 0;
    512 	}
    513 
    514 	if (endptr == str || (*endptr != '\0' && *endptr != '\n')) {
    515 		tst_brkm(TBROK, cleanup_fn,
    516 			 "Invalid value: '%s' at %s:%d", str, file, lineno);
    517 		return 0;
    518 	}
    519 
    520 	return rval;
    521 }
    522 
    523 long safe_sysconf(const char *file, const int lineno,
    524 		  void (cleanup_fn) (void), int name)
    525 {
    526 	long rval;
    527 	errno = 0;
    528 
    529 	rval = sysconf(name);
    530 
    531 	if (rval == -1) {
    532 		if (errno) {
    533 			tst_brkm(TBROK | TERRNO, cleanup_fn,
    534 				 "%s:%d: sysconf(%d) failed",
    535 				 file, lineno, name);
    536 		} else {
    537 			tst_resm(TINFO, "%s:%d: sysconf(%d): "
    538 				 "queried option is not available"
    539 				 " or there is no definite limit",
    540 				 file, lineno, name);
    541 		}
    542 	}
    543 
    544 	return rval;
    545 }
    546 
    547 int safe_chmod(const char *file, const int lineno,
    548                void (cleanup_fn)(void), const char *path, mode_t mode)
    549 {
    550 	int rval;
    551 
    552 	rval = chmod(path, mode);
    553 
    554 	if (rval == -1) {
    555 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    556 			 "%s:%d: chmod(%s,0%o) failed",
    557 			 file, lineno, path, mode);
    558 	}
    559 
    560 	return rval;
    561 }
    562 
    563 int safe_fchmod(const char *file, const int lineno,
    564                 void (cleanup_fn)(void), int fd, mode_t mode)
    565 {
    566 	int rval;
    567 
    568 	rval = fchmod(fd, mode);
    569 
    570 	if (rval == -1) {
    571 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    572 			 "%s:%d: fchmod(%d,0%o) failed",
    573 			 file, lineno, fd, mode);
    574 	}
    575 
    576 	return rval;
    577 }
    578 
    579 int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void),
    580 			const char *path, uid_t owner, gid_t group)
    581 {
    582 	int rval;
    583 
    584 	rval = chown(path, owner, group);
    585 
    586 	if (rval == -1) {
    587 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    588 			"%s:%d: chown(%s,%d,%d) failed",
    589 			file, lineno, path, owner, group);
    590 	}
    591 
    592 	return rval;
    593 }
    594 
    595 int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void),
    596                 int fd, uid_t owner, gid_t group)
    597 {
    598 	int rval;
    599 
    600 	rval = fchown(fd, owner, group);
    601 
    602 	if (rval == -1) {
    603 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    604 		         "%s:%d: fchown(%d,%d,%d) failed",
    605 			 file, lineno, fd, owner, group);
    606 	}
    607 
    608 	return rval;
    609 }
    610 
    611 pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void),
    612                 int *status)
    613 {
    614 	pid_t rval;
    615 
    616 	rval = wait(status);
    617 	if (rval == -1) {
    618 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    619 			 "%s:%d: wait(%p) failed",
    620 			 file, lineno, status);
    621 	}
    622 
    623 	return rval;
    624 }
    625 
    626 pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void),
    627                    pid_t pid, int *status, int opts)
    628 {
    629 	pid_t rval;
    630 
    631 	rval = waitpid(pid, status, opts);
    632 	if (rval == -1) {
    633 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    634 			 "%s:%d: waitpid(%d,%p,%d) failed",
    635 			 file, lineno, pid, status, opts);
    636 	}
    637 
    638 	return rval;
    639 }
    640 
    641 void *safe_memalign(const char *file, const int lineno,
    642 		    void (*cleanup_fn) (void), size_t alignment, size_t size)
    643 {
    644 	void *rval;
    645 
    646 	rval = memalign(alignment, size);
    647 	if (rval == NULL)
    648 		tst_brkm(TBROK | TERRNO, cleanup_fn, "memalign failed at %s:%d",
    649 			 file, lineno);
    650 
    651 	return rval;
    652 }
    653 
    654 int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void),
    655 	      pid_t pid, int sig)
    656 {
    657 	int rval;
    658 
    659 	rval = kill(pid, sig);
    660 
    661 	if (rval == -1) {
    662 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    663 			 "%s:%d: kill(%d,%s) failed",
    664 			 file, lineno, pid, tst_strsig(sig));
    665 	}
    666 
    667 	return rval;
    668 }
    669 
    670 int safe_mkfifo(const char *file, const int lineno,
    671                 void (*cleanup_fn)(void), const char *pathname, mode_t mode)
    672 {
    673 	int rval;
    674 
    675 	rval = mkfifo(pathname, mode);
    676 
    677 	if (rval == -1) {
    678 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    679 		         "%s:%d: mkfifo(%s, 0%o) failed",
    680 			 file, lineno, pathname, mode);
    681 	}
    682 
    683 	return rval;
    684 }
    685 
    686 int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void),
    687 		const char *oldpath, const char *newpath)
    688 {
    689 	int rval;
    690 
    691 	rval = rename(oldpath, newpath);
    692 
    693 	if (rval == -1) {
    694 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    695 			 "%s:%d: rename(%s, %s) failed",
    696 			 file, lineno, oldpath, newpath);
    697 	}
    698 
    699 	return rval;
    700 }
    701 
    702 static const char *const fuse_fs_types[] = {
    703 	"exfat",
    704 	"ntfs",
    705 };
    706 
    707 static int is_fuse(const char *fs_type)
    708 {
    709 	unsigned int i;
    710 
    711 	for (i = 0; i < ARRAY_SIZE(fuse_fs_types); i++) {
    712 		if (!strcmp(fuse_fs_types[i], fs_type))
    713 			return 1;
    714 	}
    715 
    716 	return 0;
    717 }
    718 
    719 int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void),
    720 	       const char *source, const char *target,
    721 	       const char *filesystemtype, unsigned long mountflags,
    722 	       const void *data)
    723 {
    724 	int rval;
    725 
    726 	/*
    727 	 * The FUSE filesystem executes mount.fuse helper, which tries to
    728 	 * execute corresponding binary name which is encoded at the start of
    729 	 * the source string and separated by # from the device name.
    730          *
    731 	 * The mount helpers are called mount.$fs_type.
    732 	 */
    733 	if (is_fuse(filesystemtype)) {
    734 		char buf[1024];
    735 
    736 		tst_resm(TINFO, "Trying FUSE...");
    737 		snprintf(buf, sizeof(buf), "mount.%s '%s' '%s'",
    738 			 filesystemtype, source, target);
    739 
    740 		rval = tst_system(buf);
    741 		if (WIFEXITED(rval) && WEXITSTATUS(rval) == 0)
    742 			return 0;
    743 
    744 		tst_brkm(TBROK, cleanup_fn, "mount.%s failed with %i",
    745 			 filesystemtype, rval);
    746 		return -1;
    747 	}
    748 
    749 	rval = mount(source, target, filesystemtype, mountflags, data);
    750 	if (rval == -1) {
    751 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    752 			 "%s:%d: mount(%s, %s, %s, %lu, %p) failed",
    753 			 file, lineno, source, target, filesystemtype,
    754 			 mountflags, data);
    755 	}
    756 
    757 	return rval;
    758 }
    759 
    760 int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void),
    761 		const char *target)
    762 {
    763 	int rval;
    764 
    765 	rval = umount(target);
    766 
    767 	if (rval == -1) {
    768 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    769 			 "%s:%d: umount(%s) failed",
    770 			 file, lineno, target);
    771 	}
    772 
    773 	return rval;
    774 }
    775 
    776 DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void),
    777                   const char *name)
    778 {
    779 	DIR *rval;
    780 
    781 	rval = opendir(name);
    782 
    783 	if (!rval) {
    784 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    785 		         "%s:%d: opendir(%s) failed", file, lineno, name);
    786 	}
    787 
    788 	return rval;
    789 }
    790 
    791 int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void),
    792                   DIR *dirp)
    793 {
    794 	int rval;
    795 
    796 	rval = closedir(dirp);
    797 
    798 	if (rval) {
    799 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    800 		         "%s:%d: closedir(%p) failed", file, lineno, dirp);
    801 	}
    802 
    803 	return rval;
    804 }
    805 
    806 struct dirent *safe_readdir(const char *file, const int lineno, void (cleanup_fn)(void),
    807                             DIR *dirp)
    808 {
    809 	struct dirent *rval;
    810 	int err = errno;
    811 
    812 	errno = 0;
    813 	rval = readdir(dirp);
    814 
    815 	if (!rval && errno) {
    816 		tst_brkm(TBROK | TERRNO, cleanup_fn,
    817 		         "%s:%d: readdir(%p) failed", file, lineno, dirp);
    818 	}
    819 
    820 	errno = err;
    821 	return rval;
    822 }
    823 
    824 int safe_getpriority(const char *file, const int lineno, int which, id_t who)
    825 {
    826 	int rval, err = errno;
    827 
    828 	errno = 0;
    829 	rval = getpriority(which, who);
    830 	if (errno) {
    831 		tst_brkm(TBROK | TERRNO, NULL,
    832 		         "%s:%d getpriority(%i, %i) failed",
    833 			 file, lineno, which, who);
    834 	}
    835 
    836 	errno = err;
    837 	return rval;
    838 }
    839 
    840 int safe_setxattr(const char *file, const int lineno, const char *path,
    841 		  const char *name, const void *value, size_t size, int flags)
    842 {
    843 	int rval;
    844 
    845 	rval = setxattr(path, name, value, size, flags);
    846 
    847 	if (rval) {
    848 		if (errno == ENOTSUP) {
    849 			tst_brkm(TCONF, NULL,
    850 				 "%s:%d: no xattr support in fs or mounted "
    851 				 "without user_xattr option", file, lineno);
    852 		}
    853 
    854 		tst_brkm(TBROK | TERRNO, NULL, "%s:%d: setxattr() failed",
    855 			     file, lineno);
    856 	}
    857 
    858 	return rval;
    859 }
    860 
    861 int safe_lsetxattr(const char *file, const int lineno, const char *path,
    862 		   const char *name, const void *value, size_t size, int flags)
    863 {
    864 	int rval;
    865 
    866 	rval = lsetxattr(path, name, value, size, flags);
    867 
    868 	if (rval) {
    869 		if (errno == ENOTSUP) {
    870 			tst_brkm(TCONF, NULL,
    871 				 "%s:%d: no xattr support in fs or mounted "
    872 				 "without user_xattr option", file, lineno);
    873 		}
    874 
    875 		tst_brkm(TBROK | TERRNO, NULL, "%s:%d: lsetxattr() failed",
    876 			     file, lineno);
    877 	}
    878 
    879 	return rval;
    880 }
    881 
    882 int safe_fsetxattr(const char *file, const int lineno, int fd, const char *name,
    883 		   const void *value, size_t size, int flags)
    884 {
    885 	int rval;
    886 
    887 	rval = fsetxattr(fd, name, value, size, flags);
    888 
    889 	if (rval) {
    890 		if (errno == ENOTSUP) {
    891 			tst_brkm(TCONF, NULL,
    892 				 "%s:%d: no xattr support in fs or mounted "
    893 				 "without user_xattr option", file, lineno);
    894 		}
    895 
    896 		tst_brkm(TBROK | TERRNO, NULL, "%s:%d: fsetxattr() failed",
    897 			     file, lineno);
    898 	}
    899 
    900 	return rval;
    901 }
    902 
    903 int safe_removexattr(const char *file, const int lineno, const char *path,
    904 		const char *name)
    905 {
    906 	int rval;
    907 
    908 	rval = removexattr(path, name);
    909 
    910 	if (rval) {
    911 		if (errno == ENOTSUP) {
    912 			tst_brkm(TCONF, NULL,
    913 				"%s:%d: no xattr support in fs or mounted "
    914 				"without user_xattr option", file, lineno);
    915 		}
    916 
    917 		tst_brkm(TBROK | TERRNO, NULL, "%s:%d: removexattr() failed",
    918 			file, lineno);
    919 	}
    920 
    921 	return rval;
    922 }
    923 
    924 int safe_fsync(const char *file, const int lineno, int fd)
    925 {
    926 	int rval;
    927 
    928 	rval = fsync(fd);
    929 
    930 	if (rval) {
    931 		tst_brkm(TBROK | TERRNO, NULL,
    932 			"%s:%d: fsync(%i) failed", file, lineno, fd);
    933 	}
    934 
    935 	return rval;
    936 }
    937 
    938 pid_t safe_setsid(const char *file, const int lineno)
    939 {
    940 	pid_t rval;
    941 
    942 	rval = setsid();
    943 	if (rval == -1) {
    944 		tst_brkm(TBROK | TERRNO, NULL,
    945 			 "%s:%d: setsid() failed", file, lineno);
    946 	}
    947 
    948 	return rval;
    949 }
    950 
    951 int safe_mknod(const char *file, const int lineno, const char *pathname,
    952 	mode_t mode, dev_t dev)
    953 {
    954 	int rval;
    955 
    956 	rval = mknod(pathname, mode, dev);
    957 	if (rval == -1) {
    958 		tst_brkm(TBROK | TERRNO, NULL,
    959 			 "%s:%d: mknod() failed", file, lineno);
    960 	}
    961 
    962 	return rval;
    963 }
    964