1 /* 2 * Safe macros for commonly used syscalls to reduce code duplication in LTP 3 * testcases, and to ensure all errors are caught in said testcases as 4 * gracefully as possible. 5 * 6 * Also satiates some versions of gcc/glibc when the warn_unused_result 7 * attribute is applied to the function call. 8 * 9 * Licensed under the GPLv2. 10 */ 11 12 #ifndef SAFE_MACROS_FN_H__ 13 #define SAFE_MACROS_FN_H__ 14 15 #include <sys/mman.h> 16 #include <sys/types.h> 17 #include <sys/time.h> 18 #include <sys/resource.h> 19 #include <sys/stat.h> 20 #include <sys/ioctl.h> 21 #include <fcntl.h> 22 #include <libgen.h> 23 #include <stdarg.h> 24 #include <unistd.h> 25 #include <dirent.h> 26 27 char* safe_basename(const char *file, const int lineno, 28 void (*cleanup_fn)(void), char *path); 29 30 int safe_chdir(const char *file, const int lineno, 31 void (*cleanup_fn)(void), const char *path); 32 33 int safe_close(const char *file, const int lineno, 34 void (*cleanup_fn)(void), int fildes); 35 36 int safe_creat(const char *file, const int lineno, 37 void (*cleanup_fn)(void), const char *pathname, mode_t mode); 38 39 char* safe_dirname(const char *file, const int lineno, 40 void (*cleanup_fn)(void), char *path); 41 42 char* safe_getcwd(const char *file, const int lineno, 43 void (*cleanup_fn)(void), char *buf, size_t size); 44 45 struct passwd* safe_getpwnam(const char *file, const int lineno, 46 void (*cleanup_fn)(void), const char *name); 47 48 int safe_getrusage(const char *file, const int lineno, 49 void (*cleanup_fn)(void), int who, struct rusage *usage); 50 51 void* safe_malloc(const char *file, const int lineno, 52 void (*cleanup_fn)(void), size_t size); 53 54 int safe_mkdir(const char *file, const int lineno, 55 void (*cleanup_fn)(void), const char *pathname, mode_t mode); 56 57 int safe_rmdir(const char *file, const int lineno, 58 void (*cleanup_fn)(void), const char *pathname); 59 60 61 int safe_munmap(const char *file, const int lineno, 62 void (*cleanup_fn)(void), void *addr, size_t length); 63 64 int safe_open(const char *file, const int lineno, 65 void (*cleanup_fn)(void), const char *pathname, int oflags, ...); 66 67 int safe_pipe(const char *file, const int lineno, 68 void (*cleanup_fn)(void), int fildes[2]); 69 70 ssize_t safe_read(const char *file, const int lineno, 71 void (*cleanup_fn)(void), char len_strict, int fildes, 72 void *buf, size_t nbyte); 73 74 int safe_setegid(const char *file, const int lineno, 75 void (*cleanup_fn)(void), gid_t egid); 76 77 int safe_seteuid(const char *file, const int lineno, 78 void (*cleanup_fn)(void), uid_t euid); 79 80 int safe_setgid(const char *file, const int lineno, 81 void (*cleanup_fn)(void), gid_t gid); 82 83 int safe_setuid(const char *file, const int lineno, 84 void (*cleanup_fn)(void), uid_t uid); 85 86 int safe_getresuid(const char *file, const int lineno, 87 void (*cleanup_fn)(void), 88 uid_t *ruid, uid_t *euid, uid_t *suid); 89 90 int safe_getresgid(const char *file, const int lineno, 91 void (*cleanup_fn)(void), 92 gid_t *rgid, gid_t *egid, gid_t *sgid); 93 94 int safe_unlink(const char *file, const int lineno, 95 void (*cleanup_fn)(void), const char *pathname); 96 97 int safe_link(const char *file, const int lineno, 98 void (cleanup_fn)(void), const char *oldpath, 99 const char *newpath); 100 101 int safe_linkat(const char *file, const int lineno, 102 void (cleanup_fn)(void), int olddirfd, const char *oldpath, 103 int newdirfd, const char *newpath, int flags); 104 105 ssize_t safe_readlink(const char *file, const int lineno, 106 void (cleanup_fn)(void), const char *path, 107 char *buf, size_t bufsize); 108 109 int safe_symlink(const char *file, const int lineno, 110 void (cleanup_fn)(void), const char *oldpath, 111 const char *newpath); 112 113 ssize_t safe_write(const char *file, const int lineno, 114 void (cleanup_fn)(void), char len_strict, int fildes, 115 const void *buf, size_t nbyte); 116 117 long safe_strtol(const char *file, const int lineno, 118 void (cleanup_fn)(void), char *str, long min, long max); 119 120 unsigned long safe_strtoul(const char *file, const int lineno, 121 void (cleanup_fn)(void), 122 char *str, unsigned long min, unsigned long max); 123 124 long safe_sysconf(const char *file, const int lineno, 125 void (cleanup_fn)(void), int name); 126 127 int safe_chmod(const char *file, const int lineno, void (cleanup_fn)(void), 128 const char *path, mode_t mode); 129 130 int safe_fchmod(const char *file, const int lineno, void (cleanup_fn)(void), 131 int fd, mode_t mode); 132 133 int safe_chown(const char *file, const int lineno, void (cleanup_fn)(void), 134 const char *path, uid_t owner, gid_t group); 135 136 int safe_fchown(const char *file, const int lineno, void (cleanup_fn)(void), 137 int fd, uid_t owner, gid_t group); 138 139 pid_t safe_wait(const char *file, const int lineno, void (cleanup_fn)(void), 140 int *status); 141 142 pid_t safe_waitpid(const char *file, const int lineno, void (cleanup_fn)(void), 143 pid_t pid, int *status, int opts); 144 145 int safe_kill(const char *file, const int lineno, void (cleanup_fn)(void), 146 pid_t pid, int sig); 147 148 void *safe_memalign(const char *file, const int lineno, 149 void (*cleanup_fn)(void), size_t alignment, size_t size); 150 151 int safe_mkfifo(const char *file, const int lineno, 152 void (*cleanup_fn)(void), const char *pathname, mode_t mode); 153 154 int safe_rename(const char *file, const int lineno, void (*cleanup_fn)(void), 155 const char *oldpath, const char *newpath); 156 157 int safe_mount(const char *file, const int lineno, void (*cleanup_fn)(void), 158 const char *source, const char *target, 159 const char *filesystemtype, unsigned long mountflags, 160 const void *data); 161 162 int safe_umount(const char *file, const int lineno, void (*cleanup_fn)(void), 163 const char *target); 164 165 DIR* safe_opendir(const char *file, const int lineno, void (cleanup_fn)(void), 166 const char *name); 167 168 int safe_closedir(const char *file, const int lineno, void (cleanup_fn)(void), 169 DIR *dirp); 170 171 struct dirent *safe_readdir(const char *file, const int lineno, 172 void (cleanup_fn)(void), 173 DIR *dirp); 174 175 DIR* safe_opendir(const char *file, const int lineno, 176 void (cleanup_fn)(void), 177 const char *name); 178 179 struct dirent *safe_readdir(const char *file, const int lineno, 180 void (cleanup_fn)(void), 181 DIR *dirp); 182 183 int safe_closedir(const char *file, const int lineno, 184 void (cleanup_fn)(void), 185 DIR *dirp); 186 187 #endif /* SAFE_MACROS_FN_H__ */ 188