team-ljyzipelo

공개
207줄8.4 KB
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
13static ssize_t (*real_write)(int, const void *, size_t);
14static int (*real_openat)(int, const char *, int, ...);
15static int (*real_open64)(const char *, int, ...);
16static int (*real_fsync)(int);
17static int (*real_fstatvfs)(int, struct statvfs *);
18static int started;
19static 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
29static int output_file(int fd) {
30 char link[64], path[4096];
31 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
32 ssize_t count = readlink(link, path, sizeof(path) - 1);
33 if (count < 0) return 0;
34 path[count] = 0;
35 const char *fault = getenv("ZIPELO_TEST_FAULT");
36 if (fault && (!strcmp(fault, "stage-full") || !strcmp(fault, "codec-full"))) return strstr(path, "/#") && strstr(path, "(deleted)");
37 return strstr(path, "/.zipelo-") != NULL;
38}
39
40int openat(int fd, const char *path, int flags, ...) {
41 mode_t mode = 0;
42 if (flags & O_CREAT) {
43 va_list args;
44 va_start(args, flags);
45 mode = va_arg(args, unsigned int);
46 va_end(args);
47 }
48 const char *fault = getenv("ZIPELO_TEST_FAULT");
49 if (fault && (!strcmp(fault, "create-full") || !strcmp(fault, "create-denied"))
50 && !strncmp(path, ".zipelo-", 8)) {
51 errno = !strcmp(fault, "create-full") ? ENOSPC : EACCES;
52 return -1;
53 }
54 return real_openat(fd, path, flags, mode);
55}
56
57int open64(const char *path, int flags, ...) {
58 mode_t mode = 0;
59 if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) {
60 va_list args;
61 va_start(args, flags);
62 mode = va_arg(args, unsigned int);
63 va_end(args);
64 }
65 const char *fault = getenv("ZIPELO_TEST_FAULT");
66 const char *name = strrchr(path, '/');
67 name = name ? name + 1 : path;
68 int named_temp = (flags & O_CREAT) && !strncmp(name, "zipelo-", 7);
69 if (fault && !strcmp(fault, "temp-full")
70 && (((flags & O_TMPFILE) == O_TMPFILE) || named_temp)) {
71 errno = ENOSPC;
72 return -1;
73 }
74 if (fault && !strcmp(fault, "fallback") && ((flags & O_TMPFILE) == O_TMPFILE)) {
75 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
76 if (marker >= 0) close(marker);
77 errno = EOPNOTSUPP;
78 return -1;
79 }
80 return real_open64(path, flags, mode);
81}
82
83ssize_t write(int fd, const void *bytes, size_t count) {
84 const char *fault = getenv("ZIPELO_TEST_FAULT");
85 if (!fault || !output_file(fd)) return real_write(fd, bytes, count);
86 if (!strcmp(fault, "codec-full")) {
87 if (snapshot_fd == -1) snapshot_fd = fd;
88 if (fd == snapshot_fd) return real_write(fd, bytes, count);
89 if (!started++) return real_write(fd, bytes, count < 7 ? count : 7);
90 errno = ENOSPC;
91 return -1;
92 }
93 if (!strcmp(fault, "write-io")) { errno = EIO; return -1; }
94 if (!strcmp(fault, "full") || !strcmp(fault, "stage-full") || !strcmp(fault, "full-cleanup-fail")) {
95 if (!started++) return real_write(fd, bytes, count < 7 ? count : 7);
96 errno = ENOSPC;
97 return -1;
98 }
99 if (!strcmp(fault, "pause") && !started++) {
100 const char *marker = getenv("ZIPELO_TEST_MARKER");
101 const char *release = getenv("ZIPELO_TEST_RELEASE");
102 int signal_fd = real_openat(AT_FDCWD, marker, O_CREAT | O_EXCL | O_WRONLY, 0600);
103 if (signal_fd < 0) { errno = EIO; return -1; }
104 close(signal_fd);
105 for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000);
106 if (access(release, F_OK)) { errno = EIO; return -1; }
107 }
108 return real_write(fd, bytes, count);
109}
110
111int fstatvfs(int fd, struct statvfs *value) {
112 int result = real_fstatvfs(fd, value);
113 const char *fault = getenv("ZIPELO_TEST_FAULT");
114 if (result || !fault || strncmp(fault, "space-", 6)) return result;
115 struct stat st;
116 if (fstat(fd, &st)) return -1;
117 int affected = !strcmp(fault, "space-error") || !strcmp(fault, "space-combined")
118 || (!strcmp(fault, "space-stage") && S_ISREG(st.st_mode))
119 || ((!strcmp(fault, "space-destination") || !strcmp(fault, "space-one-output")) && S_ISDIR(st.st_mode));
120 if (!affected) return result;
121 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
122 if (marker >= 0) close(marker);
123 if (!strcmp(fault, "space-error")) { errno = EIO; return -1; }
124 if (!strcmp(fault, "space-one-output")) {
125 value->f_frsize = 1;
126 value->f_bavail = 64;
127 return 0;
128 }
129 value->f_bavail = !strcmp(fault, "space-combined") ? (1536 * 1024) / value->f_frsize : 0;
130 return 0;
131}
132
133int fsync(int fd) {
134 const char *fault = getenv("ZIPELO_TEST_FAULT");
135 struct stat st;
136 if (fault && !strcmp(fault, "move-on-sync") && output_file(fd)) {
137 char link[64], path[4096], moved[4112];
138 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
139 ssize_t count = readlink(link, path, sizeof(path) - 1);
140 if (count < 0) { errno = EIO; return -1; }
141 path[count] = 0;
142 snprintf(moved, sizeof(moved), "%s.owned", path);
143 if (rename(path, moved)) { errno = EIO; return -1; }
144 return real_fsync(fd);
145 }
146 int directory_full = fault && !strcmp(fault, "dir-sync-full")
147 && !fstat(fd, &st) && S_ISDIR(st.st_mode);
148 if (directory_full || (fault && !strcmp(fault, "sync-full") && output_file(fd))) {
149 errno = ENOSPC;
150 return -1;
151 }
152 if (fault && output_file(fd) && (!strcmp(fault, "sync-io") || !strcmp(fault, "sync-cleanup-fail") || !strcmp(fault, "swap-sync") || !strcmp(fault, "swap-publish"))) {
153 if (!strcmp(fault, "swap-sync") || !strcmp(fault, "swap-publish")) {
154 char link[64], path[4096], moved[4112];
155 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
156 ssize_t count = readlink(link, path, sizeof(path) - 1);
157 if (count < 0) { errno = EIO; return -1; }
158 path[count] = 0;
159 snprintf(moved, sizeof(moved), "%s.owned", path);
160 if (rename(path, moved)) { errno = EIO; return -1; }
161 int replacement = real_openat(AT_FDCWD, path, O_CREAT | O_EXCL | O_WRONLY, 0600);
162 if (replacement < 0) { errno = EIO; return -1; }
163 real_write(replacement, "unrelated replacement", 21);
164 close(replacement);
165 if (!strcmp(fault, "swap-publish")) return real_fsync(fd);
166 }
167 errno = EIO;
168 return -1;
169 }
170 if (fault && !strcmp(fault, "dir-sync-io") && !fstat(fd, &st) && S_ISDIR(st.st_mode)) {
171 errno = EIO;
172 return -1;
173 }
174 return real_fsync(fd);
175}
176
177int unlinkat(int fd, const char *path, int flags) {
178 const char *fault = getenv("ZIPELO_TEST_FAULT");
179 if (fault && strstr(fault, "cleanup-fail") && !strncmp(path, ".zipelo-", 8)) {
180 errno = EACCES;
181 return -1;
182 }
183 int (*real_unlinkat)(int, const char *, int) = dlsym(RTLD_NEXT, "unlinkat");
184 return real_unlinkat(fd, path, flags);
185}
186
187int linkat(int oldfd, const char *oldname, int newfd, const char *newname, int flags) {
188 int (*real_linkat)(int, const char *, int, const char *, int) = dlsym(RTLD_NEXT, "linkat");
189 const char *fault = getenv("ZIPELO_TEST_FAULT");
190 if (fault && !strcmp(fault, "swap-on-link") && !strncmp(oldname, ".zipelo-", 8)) {
191 char moved[512];
192 snprintf(moved, sizeof(moved), "%s.owned", oldname);
193 if (renameat(oldfd, oldname, oldfd, moved)) return -1;
194 int replacement = real_openat(oldfd, oldname, O_CREAT | O_EXCL | O_WRONLY, 0600);
195 if (replacement < 0) return -1;
196 real_write(replacement, "unrelated replacement", 21);
197 close(replacement);
198 }
199 return real_linkat(oldfd, oldname, newfd, newname, flags);
200}
201
202int renameat(int oldfd, const char *oldname, int newfd, const char *newname) {
203 const char *fault = getenv("ZIPELO_TEST_FAULT");
204 if (fault && !strcmp(fault, "replace-fail")) { errno = EIO; return -1; }
205 int (*real_renameat)(int, const char *, int, const char *) = dlsym(RTLD_NEXT, "renameat");
206 return real_renameat(oldfd, oldname, newfd, newname);
207}