| 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 | |
| 11 | static ssize_t (*real_write)(int, const void *, size_t); |
| 12 | static int (*real_openat)(int, const char *, int, ...); |
| 13 | static int (*real_open64)(const char *, int, ...); |
| 14 | static int started; |
| 15 | |
| 16 | __attribute__((constructor)) static void initialize(void) { |
| 17 | real_write = dlsym(RTLD_NEXT, "write"); |
| 18 | real_openat = dlsym(RTLD_NEXT, "openat"); |
| 19 | real_open64 = dlsym(RTLD_NEXT, "open64"); |
| 20 | } |
| 21 | |
| 22 | static int output_file(int fd) { |
| 23 | char link[64], path[4096]; |
| 24 | snprintf(link, sizeof(link), "/proc/self/fd/%d", fd); |
| 25 | ssize_t count = readlink(link, path, sizeof(path) - 1); |
| 26 | if (count < 0) return 0; |
| 27 | path[count] = 0; |
| 28 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 29 | if (fault && !strcmp(fault, "stage-full")) return strstr(path, "/#") && strstr(path, "(deleted)"); |
| 30 | return strstr(path, "/.zipelo-") != NULL; |
| 31 | } |
| 32 | |
| 33 | int openat(int fd, const char *path, int flags, ...) { |
| 34 | mode_t mode = 0; |
| 35 | if (flags & O_CREAT) { |
| 36 | va_list args; |
| 37 | va_start(args, flags); |
| 38 | mode = va_arg(args, unsigned int); |
| 39 | va_end(args); |
| 40 | } |
| 41 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 42 | if (fault && !strcmp(fault, "create-full") && !strncmp(path, ".zipelo-", 8)) { |
| 43 | errno = ENOSPC; |
| 44 | return -1; |
| 45 | } |
| 46 | return real_openat(fd, path, flags, mode); |
| 47 | } |
| 48 | |
| 49 | int open64(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 && !strcmp(fault, "fallback") && ((flags & O_TMPFILE) == O_TMPFILE)) { |
| 59 | int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600); |
| 60 | if (marker >= 0) close(marker); |
| 61 | errno = EOPNOTSUPP; |
| 62 | return -1; |
| 63 | } |
| 64 | return real_open64(path, flags, mode); |
| 65 | } |
| 66 | |
| 67 | ssize_t write(int fd, const void *bytes, size_t count) { |
| 68 | const char *fault = getenv("ZIPELO_TEST_FAULT"); |
| 69 | if (!fault || !output_file(fd)) return real_write(fd, bytes, count); |
| 70 | if (!strcmp(fault, "full") || !strcmp(fault, "stage-full")) { |
| 71 | if (!started++) return real_write(fd, bytes, count < 7 ? count : 7); |
| 72 | errno = ENOSPC; |
| 73 | return -1; |
| 74 | } |
| 75 | if (!strcmp(fault, "pause") && !started++) { |
| 76 | const char *marker = getenv("ZIPELO_TEST_MARKER"); |
| 77 | const char *release = getenv("ZIPELO_TEST_RELEASE"); |
| 78 | int signal_fd = real_openat(AT_FDCWD, marker, O_CREAT | O_EXCL | O_WRONLY, 0600); |
| 79 | if (signal_fd < 0) { errno = EIO; return -1; } |
| 80 | close(signal_fd); |
| 81 | for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000); |
| 82 | if (access(release, F_OK)) { errno = EIO; return -1; } |
| 83 | } |
| 84 | return real_write(fd, bytes, count); |
| 85 | } |