| 1 | #define _GNU_SOURCE |
| 2 | #include <dlfcn.h> |
| 3 | #include <errno.h> |
| 4 | #include <fcntl.h> |
| 5 | #include <stdarg.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <stdio.h> |
| 9 | #include <unistd.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/statvfs.h> |
| 12 | |
| 13 | static ssize_t (*real_write)(int, const void *, size_t); |
| 14 | static int (*real_openat)(int, const char *, int, ...); |
| 15 | static int (*real_open64)(const char *, int, ...); |
| 16 | static int (*real_fsync)(int); |
| 17 | static int (*real_fstatvfs)(int, struct statvfs *); |
| 18 | static int started; |
| 19 | static int snapshot_fd = -1; |
| 20 | |
| 21 | __attribute__((constructor)) static void initialize(void) { |
| 22 | real_write = dlsym(RTLD_NEXT, "write"); |
| 23 | real_openat = dlsym(RTLD_NEXT, "openat"); |
| 24 | real_open64 = dlsym(RTLD_NEXT, "open64"); |
| 25 | real_fsync = dlsym(RTLD_NEXT, "fsync"); |
| 26 | real_fstatvfs = dlsym(RTLD_NEXT, "fstatvfs"); |
| 27 | } |
| 28 | |
| 29 | /* 출력 폴더 임시 파일의 대체 이름(.zipelo-<hex 32>.tmp). 게시 임시 이름(.zipelo-<pid>-<n>.tmp)은 제외함. */ |
| 30 | static int spool_name(const char *path) { |
| 31 | size_t length = strlen(path); |
| 32 | if (length != 44 || strncmp(path, ".zipelo-", 8) || strcmp(path + 40, ".tmp")) return 0; |
| 33 | for (size_t i = 8; i < 40; i++) |
| 34 | if (!strchr("0123456789abcdef", path[i])) return 0; |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | static int output_file(int fd) { |
| 39 | char link[64], path[4096]; |
| 40 | snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); |
| 41 | ssize_t count = readlink(link, path, sizeof(path) - 1); |
| 42 | if (count < 0) return 0; |
| 43 | path[count] = 0; |
| 44 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 45 | if (fault && (!strcmp(fault, "stage-full") || !strcmp(fault, "codec-full"))) return strstr(path, "/#") && strstr(path, "(deleted)"); |
| 46 | return strstr(path, "/.zipelo-") != NULL; |
| 47 | } |
| 48 | |
| 49 | int openat(int fd, const char *path, int flags, ...) { |
| 50 | mode_t mode = 0; |
| 51 | if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) { |
| 52 | va_list args; |
| 53 | va_start(args, flags); |
| 54 | mode = va_arg(args, unsigned int); |
| 55 | va_end(args); |
| 56 | } |
| 57 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 58 | if (fault && !strncmp(fault, "destination-", 12) |
| 59 | && (((flags & O_TMPFILE) == O_TMPFILE) || ((flags & O_CREAT) && spool_name(path)))) { |
| 60 | if (strstr(fault, "named") && (flags & O_TMPFILE) == O_TMPFILE) { |
| 61 | errno = EOPNOTSUPP; |
| 62 | } else { |
| 63 | errno = strstr(fault, "edquot") ? EDQUOT : strstr(fault, "denied") ? EACCES : ENOSPC; |
| 64 | } |
| 65 | return -1; |
| 66 | } |
| 67 | if (fault && (!strcmp(fault, "create-full") || !strcmp(fault, "create-denied")) |
| 68 | && !strncmp(path, ".zipelo-", 8)) { |
| 69 | errno = !strcmp(fault, "create-full") ? ENOSPC : EACCES; |
| 70 | return -1; |
| 71 | } |
| 72 | /* 해제 작업의 원본 사본과 단계 임시 파일은 출력 폴더 핸들 기준 O_TMPFILE로 만듦. */ |
| 73 | if ((flags & O_TMPFILE) == O_TMPFILE) { |
| 74 | if (fault && !strcmp(fault, "temp-full")) { |
| 75 | errno = ENOSPC; |
| 76 | return -1; |
| 77 | } |
| 78 | if (fault && (!strcmp(fault, "fallback") || !strcmp(fault, "fallback-unlink-denied"))) { |
| 79 | int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600); |
| 80 | if (marker >= 0) close(marker); |
| 81 | errno = EOPNOTSUPP; |
| 82 | return -1; |
| 83 | } |
| 84 | } |
| 85 | return real_openat(fd, path, flags, mode); |
| 86 | } |
| 87 | |
| 88 | int open64(const char *path, int flags, ...) { |
| 89 | mode_t mode = 0; |
| 90 | if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) { |
| 91 | va_list args; |
| 92 | va_start(args, flags); |
| 93 | mode = va_arg(args, unsigned int); |
| 94 | va_end(args); |
| 95 | } |
| 96 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 97 | const char *name = strrchr(path, '/'); |
| 98 | name = name ? name + 1 : path; |
| 99 | int named_temp = (flags & O_CREAT) && !strncmp(name, "zipelo-", 7); |
| 100 | if (fault && !strcmp(fault, "temp-full") |
| 101 | && (((flags & O_TMPFILE) == O_TMPFILE) || named_temp)) { |
| 102 | errno = ENOSPC; |
| 103 | return -1; |
| 104 | } |
| 105 | if (fault && !strcmp(fault, "fallback") && ((flags & O_TMPFILE) == O_TMPFILE)) { |
| 106 | int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600); |
| 107 | if (marker >= 0) close(marker); |
| 108 | errno = EOPNOTSUPP; |
| 109 | return -1; |
| 110 | } |
| 111 | return real_open64(path, flags, mode); |
| 112 | } |
| 113 | |
| 114 | ssize_t write(int fd, const void *bytes, size_t count) { |
| 115 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 116 | if (!fault || !output_file(fd)) return real_write(fd, bytes, count); |
| 117 | if (!strcmp(fault, "codec-full")) { |
| 118 | if (snapshot_fd == -1) snapshot_fd = fd; |
| 119 | if (fd == snapshot_fd) return real_write(fd, bytes, count); |
| 120 | if (!started++) return real_write(fd, bytes, count < 7 ? count : 7); |
| 121 | errno = ENOSPC; |
| 122 | return -1; |
| 123 | } |
| 124 | if (!strcmp(fault, "write-io")) { errno = EIO; return -1; } |
| 125 | if (!strcmp(fault, "full") || !strcmp(fault, "stage-full") || !strcmp(fault, "full-cleanup-fail")) { |
| 126 | if (!started++) return real_write(fd, bytes, count < 7 ? count : 7); |
| 127 | errno = ENOSPC; |
| 128 | return -1; |
| 129 | } |
| 130 | if (!strcmp(fault, "pause") && !started++) { |
| 131 | const char *marker = getenv("ZIPELO_TEST_MARKER"); |
| 132 | const char *release = getenv("ZIPELO_TEST_RELEASE"); |
| 133 | int signal_fd = real_openat(AT_FDCWD, marker, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 134 | if (signal_fd < 0) { errno = EIO; return -1; } |
| 135 | close(signal_fd); |
| 136 | for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000); |
| 137 | if (access(release, F_OK)) { errno = EIO; return -1; } |
| 138 | } |
| 139 | return real_write(fd, bytes, count); |
| 140 | } |
| 141 | |
| 142 | int fstatvfs(int fd, struct statvfs *value) { |
| 143 | int result = real_fstatvfs(fd, value); |
| 144 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 145 | if (result || !fault || strncmp(fault, "space-", 6)) return result; |
| 146 | struct stat st; |
| 147 | if (fstat(fd, &st)) return -1; |
| 148 | int affected = !strcmp(fault, "space-error") || !strcmp(fault, "space-combined") |
| 149 | || (!strcmp(fault, "space-stage") && S_ISREG(st.st_mode)) |
| 150 | || ((!strcmp(fault, "space-destination") || !strcmp(fault, "space-one-output")) && S_ISDIR(st.st_mode)); |
| 151 | if (!affected) return result; |
| 152 | int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600); |
| 153 | if (marker >= 0) close(marker); |
| 154 | if (!strcmp(fault, "space-error")) { errno = EIO; return -1; } |
| 155 | if (!strcmp(fault, "space-one-output")) { |
| 156 | value->f_frsize = 1; |
| 157 | value->f_bavail = 64; |
| 158 | return 0; |
| 159 | } |
| 160 | value->f_bavail = !strcmp(fault, "space-combined") ? (1536 * 1024) / value->f_frsize : 0; |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | int fsync(int fd) { |
| 165 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 166 | struct stat st; |
| 167 | if (fault && !strcmp(fault, "move-on-sync") && output_file(fd)) { |
| 168 | char link[64], path[4096], moved[4112]; |
| 169 | snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); |
| 170 | ssize_t count = readlink(link, path, sizeof(path) - 1); |
| 171 | if (count < 0) { errno = EIO; return -1; } |
| 172 | path[count] = 0; |
| 173 | snprintf(moved, sizeof(moved), "%s.owned", path); |
| 174 | if (rename(path, moved)) { errno = EIO; return -1; } |
| 175 | return real_fsync(fd); |
| 176 | } |
| 177 | int directory_full = fault && !strcmp(fault, "dir-sync-full") |
| 178 | && !fstat(fd, &st) && S_ISDIR(st.st_mode); |
| 179 | if (directory_full || (fault && !strcmp(fault, "sync-full") && output_file(fd))) { |
| 180 | errno = ENOSPC; |
| 181 | return -1; |
| 182 | } |
| 183 | if (fault && output_file(fd) && (!strcmp(fault, "sync-io") || !strcmp(fault, "sync-cleanup-fail") || !strcmp(fault, "swap-sync") || !strcmp(fault, "swap-publish"))) { |
| 184 | if (!strcmp(fault, "swap-sync") || !strcmp(fault, "swap-publish")) { |
| 185 | char link[64], path[4096], moved[4112]; |
| 186 | snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); |
| 187 | ssize_t count = readlink(link, path, sizeof(path) - 1); |
| 188 | if (count < 0) { errno = EIO; return -1; } |
| 189 | path[count] = 0; |
| 190 | snprintf(moved, sizeof(moved), "%s.owned", path); |
| 191 | if (rename(path, moved)) { errno = EIO; return -1; } |
| 192 | int replacement = real_openat(AT_FDCWD, path, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 193 | if (replacement < 0) { errno = EIO; return -1; } |
| 194 | real_write(replacement, "unrelated replacement", 21); |
| 195 | close(replacement); |
| 196 | if (!strcmp(fault, "swap-publish")) return real_fsync(fd); |
| 197 | } |
| 198 | errno = EIO; |
| 199 | return -1; |
| 200 | } |
| 201 | if (fault && !strcmp(fault, "dir-sync-io") && !fstat(fd, &st) && S_ISDIR(st.st_mode)) { |
| 202 | errno = EIO; |
| 203 | return -1; |
| 204 | } |
| 205 | return real_fsync(fd); |
| 206 | } |
| 207 | |
| 208 | int unlinkat(int fd, const char *path, int flags) { |
| 209 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 210 | if (fault && strstr(fault, "cleanup-fail") && !strncmp(path, ".zipelo-", 8)) { |
| 211 | errno = EACCES; |
| 212 | return -1; |
| 213 | } |
| 214 | if (fault && !strcmp(fault, "fallback-unlink-denied") && spool_name(path)) { |
| 215 | errno = EACCES; |
| 216 | return -1; |
| 217 | } |
| 218 | int (*real_unlinkat)(int, const char *, int) = dlsym(RTLD_NEXT, "unlinkat"); |
| 219 | return real_unlinkat(fd, path, flags); |
| 220 | } |
| 221 | |
| 222 | int linkat(int oldfd, const char *oldname, int newfd, const char *newname, int flags) { |
| 223 | int (*real_linkat)(int, const char *, int, const char *, int) = dlsym(RTLD_NEXT, "linkat"); |
| 224 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 225 | if (fault && !strcmp(fault, "swap-on-link") && !strncmp(oldname, ".zipelo-", 8)) { |
| 226 | char moved[512]; |
| 227 | snprintf(moved, sizeof(moved), "%s.owned", oldname); |
| 228 | if (renameat(oldfd, oldname, oldfd, moved)) return -1; |
| 229 | int replacement = real_openat(oldfd, oldname, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 230 | if (replacement < 0) return -1; |
| 231 | real_write(replacement, "unrelated replacement", 21); |
| 232 | close(replacement); |
| 233 | } |
| 234 | return real_linkat(oldfd, oldname, newfd, newname, flags); |
| 235 | } |
| 236 | |
| 237 | int renameat(int oldfd, const char *oldname, int newfd, const char *newname) { |
| 238 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 239 | if (fault && !strcmp(fault, "replace-fail")) { errno = EIO; return -1; } |
| 240 | int (*real_renameat)(int, const char *, int, const char *) = dlsym(RTLD_NEXT, "renameat"); |
| 241 | return real_renameat(oldfd, oldname, newfd, newname); |
| 242 | } |