/* * ut_sigaction -- a sigaction that cannot return < 0 */ int ut_sigaction(const char *file, int line, const char *func, int signum, struct sigaction *act, struct sigaction *oldact) { #ifndef _WIN32 int retval = sigaction(signum, act, oldact); if (retval != 0) ut_fatal(file, line, func, "!sigaction: %s", strsignal(signum)); return retval; #else if (signum == SIGABRT) { DWORD dwMode = GetErrorMode(); SetErrorMode(dwMode | SEM_NOGPFAULTERRORBOX | SEM_FAILCRITICALERRORS); _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT); } if (signum == SIGSEGV) { Sa_handler = act->sa_handler; Signum = signum; AddVectoredExceptionHandler(0, exception_handler); } _crt_signal_t retval = signal(signum, act->sa_handler); if (retval == SIG_ERR) ut_fatal(file, line, func, "!signal: %d", signum); if (oldact != NULL) { oldact->sa_handler = retval; } return 0; #endif }
int ut_fcntl(const char *file, int line, const char *func, int fd, int cmd, int num, ...) { int retval; va_list args; uint64_t arg = 0; /* * In the case of fcntl, num is always 0 or 1 */ if (num != 0) { va_start(args, num); arg = va_arg(args, uint64_t); retval = fcntl(fd, cmd, arg); va_end(args); } else retval = fcntl(fd, cmd); if (retval < 0) ut_fatal(file, line, func, "!fcntl: %d", fd); return retval; }
/* * ut_close -- a close that cannot return -1 */ int ut_close(const char *file, int line, const char *func, int fd) { int retval = close(fd); if (retval != 0) ut_fatal(file, line, func, "!close: %d", fd); return retval; }
/* * ut_umount -- a umount that cannot return -1 */ int ut_umount(const char *file, int line, const char *func, const char *tar) { int retval = umount(tar); if (retval < 0) ut_fatal(file, line, func, "!umount: %s", tar); return retval; }
/* * ut_closedir -- a closedir that cannot return -1 */ int ut_closedir(const char *file, int line, const char *func, DIR *dirp) { int retval = closedir(dirp); if (retval < 0) ut_fatal(file, line, func, "!closedir: %p", dirp); return retval; }
/* * ut_unlink -- an unlink that cannot return -1 */ int ut_unlink(const char *file, int line, const char *func, const char *path) { int retval = unlink(path); if (retval != 0) ut_fatal(file, line, func, "!unlink: %s", path); return retval; }
/* * ut_malloc -- a malloc that cannot return NULL */ void * ut_malloc(const char *file, int line, const char *func, size_t size) { void *retval = malloc(size); if (retval == NULL) ut_fatal(file, line, func, "cannot malloc %zu bytes", size); return retval; }
/* * ut_flock -- a flock that cannot return -1 */ int ut_flock(const char *file, int line, const char *func, int fd, int op) { int retval = flock(fd, op); if (retval != 0) ut_fatal(file, line, func, "!flock: %d", fd); return retval; }
/* * ut_symlink -- a symlink that cannot return -1 */ int ut_symlink(const char *file, int line, const char *func, const char *oldpath, const char *newpath) { int retval = symlink(oldpath, newpath); if (retval < 0) ut_fatal(file, line, func, "!symlink: %s %s", oldpath, newpath); return retval; }
/* * ut_mknod -- a mknod that cannot return -1 */ int ut_mknod(const char *file, int line, const char *func, const char *pathname, mode_t mode, dev_t dev) { int retval = mknod(pathname, mode, dev); if (retval < 0) ut_fatal(file, line, func, "!mknod: %s", pathname); return retval; }
/* * ut_chmod -- a chmod that cannot return -1 */ int ut_chmod(const char *file, int line, const char *func, const char *path, mode_t mode) { int retval = chmod(path, mode); if (retval < 0) ut_fatal(file, line, func, "!mode: %s %o", path, mode); return retval; }
/* * ut_write -- a write that can't return -1 */ size_t ut_write(const char *file, int line, const char *func, int fd, const void *buf, size_t count) { ssize_t retval = write(fd, buf, count); if (retval < 0) ut_fatal(file, line, func, "!write: %d", fd); return (size_t)retval; }
/* * ut_rename -- a rename that cannot return -1 */ int ut_rename(const char *file, int line, const char *func, const char *oldpath, const char *newpath) { int retval = rename(oldpath, newpath); if (retval < 0) ut_fatal(file, line, func, "!rename: %s %s", oldpath, newpath); return retval; }
/* * ut_rmdir -- a rmdir that cannot return -1 */ int ut_rmdir(const char *file, int line, const char *func, const char *pathname) { int retval = rmdir(pathname); if (retval < 0) ut_fatal(file, line, func, "!rmdir: %s", pathname); return retval; }
/* * ut_opendir -- an opendir that cannot return NULL */ DIR * ut_opendir(const char *file, int line, const char *func, const char *name) { DIR *retval = opendir(name); if (retval == NULL) ut_fatal(file, line, func, "!opendir: %s", name); return retval; }
/* * ut_read -- a read that can't return -1 */ size_t ut_read(const char *file, int line, const char *func, int fd, void *buf, size_t count) { ssize_t retval = read(fd, buf, count); if (retval < 0) ut_fatal(file, line, func, "!read: %d", fd); return (size_t)retval; }
/* * ut_stat -- a stat that cannot return -1 */ int ut_stat(const char *file, int line, const char *func, const char *path, struct stat *st_bufp) { int retval = stat(path, st_bufp); if (retval < 0) ut_fatal(file, line, func, "!stat: %s", path); return retval; }
/* * ut_fstat -- a fstat that cannot return -1 */ int ut_fstat(const char *file, int line, const char *func, int fd, struct stat *st_bufp) { int retval = fstat(fd, st_bufp); if (retval < 0) ut_fatal(file, line, func, "!fstat: %d", fd); return retval; }
/* * ut_readlink -- a readlink that can't return -1 */ size_t ut_readlink(const char *file, int line, const char *func, const char *path, void *buf, size_t count) { ssize_t retval = readlink(path, buf, count); if (retval < 0) ut_fatal(file, line, func, "!readlink: %s", path); return (size_t)retval; }
/* * ut_lseek -- an lseek that can't return -1 */ off_t ut_lseek(const char *file, int line, const char *func, int fd, off_t offset, int whence) { off_t retval = lseek(fd, offset, whence); if (retval == -1) ut_fatal(file, line, func, "!lseek: %d", fd); return retval; }
/* * ut_dirfd -- a dirfd that cannot return -1 */ int ut_dirfd(const char *file, int line, const char *func, DIR *dirp) { int retval = dirfd(dirp); if (retval < 0) ut_fatal(file, line, func, "!dirfd: %p", dirp); return retval; }
/* * ut_mkdir -- a mkdir that cannot return -1 */ int ut_mkdir(const char *file, int line, const char *func, const char *pathname, mode_t mode) { int retval = mkdir(pathname, mode); if (retval < 0) ut_fatal(file, line, func, "!mkdir: %s", pathname); return retval; }
/* * ut_access -- an access that cannot return -1 */ int ut_access(const char *file, int line, const char *func, const char *path, int mode) { int retval = access(path, mode); if (retval != 0) ut_fatal(file, line, func, "!access: %s: %d", path, mode); return retval; }
/* * ut_sigaction -- a sigaction that cannot return < 0 */ int ut_sigaction(const char *file, int line, const char *func, int signum, struct sigaction *act, struct sigaction *oldact) { int retval = sigaction(signum, act, oldact); if (retval != 0) ut_fatal(file, line, func, "!sigaction: %s", strsignal(signum)); return retval; }
/* * ut_memalign -- like malloc but page-aligned memory */ void * ut_memalign(const char *file, int line, const char *func, size_t alignment, size_t size) { void *retval; if ((errno = posix_memalign(&retval, alignment, size)) != 0) ut_fatal(file, line, func, "!memalign %zu bytes (%zu alignment)", size, alignment); return retval; }
/* * ut_munmap -- a munmap call that cannot return -1 */ int ut_munmap(const char *file, int line, const char *func, void *addr, size_t length) { int retval = munmap(addr, length); if (retval < 0) ut_fatal(file, line, func, "!munmap: addr=0x%llx length=0x%zx", (unsigned long long)addr, length); return retval; }
/* * ut_ftruncate -- a ftruncate that cannot return -1 */ int ut_ftruncate(const char *file, int line, const char *func, int fd, off_t length) { int retval = ftruncate(fd, length); if (retval < 0) ut_fatal(file, line, func, "!ftruncate: %d %llu", fd, (unsigned long long)length); return retval; }
/* * ut_pselect -- a pselect that cannot return -1 */ int ut_pselect(const char *file, int line, const char *func, int nfds, fd_set *rfds, fd_set *wfds, fd_set *efds, const struct timespec *tv, const sigset_t *sigmask) { int retval = pselect(nfds, rfds, wfds, efds, tv, sigmask); if (retval < 0) ut_fatal(file, line, func, "!pselect"); return retval; }
/* * ut_strdup -- a strdup that cannot return NULL */ char * ut_strdup(const char *file, int line, const char *func, const char *str) { char *retval = strdup(str); if (retval == NULL) ut_fatal(file, line, func, "cannot strdup %zu bytes", strlen(str)); return retval; }
/* * ut_truncate -- a truncate that cannot return -1 */ int ut_truncate(const char *file, int line, const char *func, const char *path, off_t length) { int retval = truncate(path, length); if (retval < 0) ut_fatal(file, line, func, "!truncate: %s %llu", path, (unsigned long long)length); return retval; }