| 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_fstatvfs)(int, struct statvfs *); |
| 17 | static int started; |
| 18 | |
| 19 | __attribute__((constructor)) static void initialize(void) { |
| 20 | real_write = dlsym(RTLD_NEXT, "write"); |
| 21 | real_openat = dlsym(RTLD_NEXT, "openat"); |
| 22 | real_open64 = dlsym(RTLD_NEXT, "open64"); |
| 23 | real_fstatvfs = dlsym(RTLD_NEXT, "fstatvfs"); |
| 24 | } |
| 25 | |
| 26 | static int output_file(int fd) { |
| 27 | char link[64], path[4096]; |
| 28 | snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); |
| 29 | ssize_t count = readlink(link, path, sizeof(path) - 1); |
| 30 | if (count < 0) return 0; |
| 31 | path[count] = 0; |
| 32 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 33 | if (fault && !strcmp(fault, "stage-full")) return strstr(path, "/#") && strstr(path, "(deleted)"); |
| 34 | return strstr(path, "/.zipelo-") != NULL; |
| 35 | } |
| 36 | |
| 37 | int openat(int fd, const char *path, int flags, ...) { |
| 38 | mode_t mode = 0; |
| 39 | if (flags & O_CREAT) { |
| 40 | va_list args; |
| 41 | va_start(args, flags); |
| 42 | mode = va_arg(args, unsigned int); |
| 43 | va_end(args); |
| 44 | } |
| 45 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 46 | if (fault && !strcmp(fault, "create-full") && !strncmp(path, ".zipelo-", 8)) { |
| 47 | errno = ENOSPC; |
| 48 | return -1; |
| 49 | } |
| 50 | return real_openat(fd, path, flags, mode); |
| 51 | } |
| 52 | |
| 53 | int open64(const char *path, int flags, ...) { |
| 54 | mode_t mode = 0; |
| 55 | if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) { |
| 56 | va_list args; |
| 57 | va_start(args, flags); |
| 58 | mode = va_arg(args, unsigned int); |
| 59 | va_end(args); |
| 60 | } |
| 61 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 62 | if (fault && !strcmp(fault, "fallback") && ((flags & O_TMPFILE) == O_TMPFILE)) { |
| 63 | int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600); |
| 64 | if (marker >= 0) close(marker); |
| 65 | errno = EOPNOTSUPP; |
| 66 | return -1; |
| 67 | } |
| 68 | return real_open64(path, flags, mode); |
| 69 | } |
| 70 | |
| 71 | ssize_t write(int fd, const void *bytes, size_t count) { |
| 72 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 73 | if (!fault || !output_file(fd)) return real_write(fd, bytes, count); |
| 74 | if (!strcmp(fault, "full") || !strcmp(fault, "stage-full")) { |
| 75 | if (!started++) return real_write(fd, bytes, count < 7 ? count : 7); |
| 76 | errno = ENOSPC; |
| 77 | return -1; |
| 78 | } |
| 79 | if (!strcmp(fault, "pause") && !started++) { |
| 80 | const char *marker = getenv("ZIPELO_TEST_MARKER"); |
| 81 | const char *release = getenv("ZIPELO_TEST_RELEASE"); |
| 82 | int signal_fd = real_openat(AT_FDCWD, marker, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 83 | if (signal_fd < 0) { errno = EIO; return -1; } |
| 84 | close(signal_fd); |
| 85 | for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000); |
| 86 | if (access(release, F_OK)) { errno = EIO; return -1; } |
| 87 | } |
| 88 | return real_write(fd, bytes, count); |
| 89 | } |
| 90 | |
| 91 | int fstatvfs(int fd, struct statvfs *value) { |
| 92 | int result = real_fstatvfs(fd, value); |
| 93 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 94 | if (result || !fault || strncmp(fault, "space-", 6)) return result; |
| 95 | struct stat st; |
| 96 | if (fstat(fd, &st)) return -1; |
| 97 | int affected = !strcmp(fault, "space-error") || !strcmp(fault, "space-combined") |
| 98 | || (!strcmp(fault, "space-stage") && S_ISREG(st.st_mode)) |
| 99 | || (!strcmp(fault, "space-destination") && S_ISDIR(st.st_mode)); |
| 100 | if (!affected) return result; |
| 101 | int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600); |
| 102 | if (marker >= 0) close(marker); |
| 103 | if (!strcmp(fault, "space-error")) { errno = EIO; return -1; } |
| 104 | value->f_bavail = !strcmp(fault, "space-combined") ? (1536 * 1024) / value->f_frsize : 0; |
| 105 | return 0; |
| 106 | } |