team-ljyzipelo

공개
181줄6.1 KB
1#define _GNU_SOURCE
2#include <dlfcn.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <stdarg.h>
6#include <stdatomic.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/stat.h>
11#include <sys/statvfs.h>
12#include <time.h>
13#include <unistd.h>
14
15static atomic_long elapsed;
16static atomic_int directories;
17static atomic_int existing_files;
18static int (*real_openat)(int, const char *, int, ...);
19static ssize_t (*real_read)(int, void *, size_t);
20static ssize_t (*real_write)(int, const void *, size_t);
21static int (*real_mkdirat)(int, const char *, mode_t);
22static int (*real_linkat)(int, const char *, int, const char *, int);
23static int (*real_renameat)(int, const char *, int, const char *);
24static int (*real_unlinkat)(int, const char *, int);
25static int (*real_fsync)(int);
26static int (*real_fstatvfs)(int, struct statvfs *);
27
28__attribute__((constructor)) static void initialize(void) {
29 real_openat = dlsym(RTLD_NEXT, "openat");
30 real_read = dlsym(RTLD_NEXT, "read");
31 real_write = dlsym(RTLD_NEXT, "write");
32 real_mkdirat = dlsym(RTLD_NEXT, "mkdirat");
33 real_linkat = dlsym(RTLD_NEXT, "linkat");
34 real_renameat = dlsym(RTLD_NEXT, "renameat");
35 real_unlinkat = dlsym(RTLD_NEXT, "unlinkat");
36 real_fsync = dlsym(RTLD_NEXT, "fsync");
37 real_fstatvfs = dlsym(RTLD_NEXT, "fstatvfs");
38}
39
40static void tick(const char *stage) {
41 const char *mode = getenv("ZIPELO_TEST_FAULT");
42 char expected[80];
43 snprintf(expected, sizeof(expected), "phase-%s", stage);
44 size_t n = strlen(expected);
45 if (!mode || (strcmp(mode, "phase-all") && strcmp(mode, "phase-all-stall")
46 && (strncmp(mode, expected, n) || (mode[n] && strcmp(mode + n, "-stall"))))) return;
47 int saved = errno;
48 atomic_fetch_add(&elapsed, strstr(mode, "-stall") ? 60 : 10);
49 const char *marker = getenv("ZIPELO_TEST_MARKER");
50 int fd = real_openat(AT_FDCWD, marker, O_CREAT | O_APPEND | O_WRONLY, 0600);
51 if (fd >= 0) {
52 real_write(fd, stage, strlen(stage));
53 real_write(fd, "\n", 1);
54 close(fd);
55 }
56 errno = saved;
57}
58
59static int output_fd(int fd) {
60 char link[64], path[4096];
61 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
62 int saved = errno;
63 ssize_t n = readlink(link, path, sizeof(path) - 1);
64 errno = saved;
65 if (n < 0) return 0;
66 path[n] = 0;
67 return strstr(path, "/output") != NULL;
68}
69
70int clock_gettime(clockid_t id, struct timespec *value) {
71 int (*original)(clockid_t, struct timespec *) = dlsym(RTLD_NEXT, "clock_gettime");
72 int result = original(id, value);
73 if (!result && id == CLOCK_MONOTONIC) value->tv_sec += atomic_load(&elapsed);
74 return result;
75}
76
77int openat(int fd, const char *path, int flags, ...) {
78 mode_t mode = 0;
79 if (flags & O_CREAT) {
80 va_list args;
81 va_start(args, flags);
82 mode = va_arg(args, unsigned int);
83 va_end(args);
84 }
85 int result = real_openat(fd, path, flags, mode);
86 if (!strncmp(path, "entry-", 6) && (flags & O_DIRECTORY) && result >= 0) {
87 int n = atomic_fetch_add(&directories, 1);
88 tick(n < 8 ? "preflight" : "directory");
89 }
90 if (strstr(path, " (") && (flags & O_PATH)) tick("rename");
91 if (!strncmp(path, "entry-", 6) && (flags & O_PATH)) {
92 if (output_fd(fd)) {
93 if (result >= 0 && atomic_fetch_add(&existing_files, 1) >= 8) tick("overwrite");
94 tick("lookup");
95 } else if (result >= 0) {
96 tick("source");
97 }
98 }
99 if (result >= 0 && (flags & O_CREAT) && !strncmp(path, ".zipelo-", 8)) tick("create");
100 return result;
101}
102
103int mkdirat(int fd, const char *path, mode_t mode) {
104 int result = real_mkdirat(fd, path, mode);
105 if (!result && !strncmp(path, "entry-", 6)) tick("mkdir");
106 return result;
107}
108
109ssize_t read(int fd, void *buffer, size_t count) {
110 ssize_t result = real_read(fd, buffer, count);
111 if (result >= 30 && (count == 30 || count == 46)
112 && (!memcmp(buffer, "PK\003\004", 4) || !memcmp(buffer, "PK\001\002", 4))) tick("catalog");
113 return result;
114}
115
116static void written_frame(const unsigned char *bytes, size_t count) {
117 static unsigned char header[32];
118 static size_t used, remaining;
119 static unsigned int kind;
120 while (count) {
121 if (used < sizeof(header)) {
122 size_t n = sizeof(header) - used;
123 if (n > count) n = count;
124 memcpy(header + used, bytes, n);
125 used += n;
126 bytes += n;
127 count -= n;
128 if (used != sizeof(header)) continue;
129 if (memcmp(header, "ZAW1", 4)) abort();
130 kind = header[6] | (header[7] << 8);
131 remaining = header[8] | (header[9] << 8) | (header[10] << 16) | ((size_t)header[11] << 24);
132 }
133 size_t n = remaining < count ? remaining : count;
134 remaining -= n;
135 bytes += n;
136 count -= n;
137 if (!remaining) {
138 if (kind == 101) tick("list");
139 used = 0;
140 }
141 }
142}
143
144ssize_t write(int fd, const void *buffer, size_t count) {
145 ssize_t result = real_write(fd, buffer, count);
146 if (result >= 30 && count >= 30 && !memcmp(buffer, "PK\003\004", 4)) tick("header");
147 if (fd == STDOUT_FILENO && result > 0) written_frame(buffer, result);
148 if (result > 0 && output_fd(fd)) tick("write");
149 return result;
150}
151
152int linkat(int fd, const char *from, int newfd, const char *to, int flags) {
153 int result = real_linkat(fd, from, newfd, to, flags);
154 if (!result) tick("publish");
155 return result;
156}
157
158int renameat(int fd, const char *from, int newfd, const char *to) {
159 int result = real_renameat(fd, from, newfd, to);
160 if (!result) tick("publish");
161 return result;
162}
163
164int unlinkat(int fd, const char *path, int flags) {
165 int result = real_unlinkat(fd, path, flags);
166 if (!result && !strncmp(path, ".zipelo-", 8)) tick("cleanup");
167 return result;
168}
169
170int fsync(int fd) {
171 int result = real_fsync(fd);
172 if (!result && output_fd(fd)) tick("sync");
173 return result;
174}
175
176int fstatvfs(int fd, struct statvfs *value) {
177 int result = real_fstatvfs(fd, value);
178 struct stat metadata;
179 if (!result && !fstat(fd, &metadata) && S_ISDIR(metadata.st_mode) && output_fd(fd)) tick("space");
180 return result;
181}