team-ljyzipelo

공개
228줄8.1 KB
1#define _GNU_SOURCE
2#include <dlfcn.h>
3#include <dirent.h>
4#include <errno.h>
5#include <fcntl.h>
6#include <stdarg.h>
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include <sys/statvfs.h>
11#include <sys/stat.h>
12#include <sys/resource.h>
13#include <signal.h>
14#include <unistd.h>
15
16static int (*original_openat)(int, const char *, int, ...);
17static int (*original_open64)(const char *, int, ...);
18static ssize_t (*original_write)(int, const void *, size_t);
19static int written[3];
20static int snapshot_fd = -1;
21static int source_changed;
22static unsigned long long source_read;
23
24__attribute__((constructor)) static void initialize(void) {
25 original_openat = dlsym(RTLD_NEXT, "openat");
26 original_open64 = dlsym(RTLD_NEXT, "open64");
27 original_write = dlsym(RTLD_NEXT, "write");
28 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
29 if (phase && !strcmp(phase, "quota")) {
30 struct rlimit limit = {65536, 65536};
31 signal(SIGXFSZ, SIG_IGN);
32 if (setrlimit(RLIMIT_FSIZE, &limit)) _exit(99);
33 }
34}
35static int location(const char *path) {
36 if (strstr(path, "/cache/")) return 0;
37 if (strstr(path, "/archive/")) return 1;
38 if (strstr(path, "/system/")) return 2;
39 return -1;
40}
41static int descriptor(int fd, char *path, size_t size) {
42 char link[64];
43 path[0] = 0;
44 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
45 ssize_t length = readlink(link, path, size - 1);
46 if (length < 0) return -1;
47 path[length] = 0;
48 return location(path);
49}
50static int affected(int at) {
51 const char *mask = getenv("ZIPELO_SPOOL_MASK");
52 return at >= 0 && mask && (atoi(mask) & (1 << at));
53}
54static void trace(int at) {
55 const char *path = getenv("ZIPELO_SPOOL_TRACE");
56 if (!path || at < 0) return;
57 int fd = original_openat(AT_FDCWD, path, O_CREAT | O_WRONLY | O_APPEND, 0600);
58 if (fd >= 0) {
59 char value = '0' + at;
60 original_write(fd, &value, 1);
61 close(fd);
62 }
63}
64static unsigned long long temporary_usage(void) {
65 DIR *directory = opendir("/proc/self/fd");
66 if (!directory) _exit(98);
67 struct dirent *entry;
68 struct stat seen[256];
69 size_t count = 0;
70 unsigned long long used = 0;
71 while ((entry = readdir(directory))) {
72 if (entry->d_name[0] == '.') continue;
73 int fd = atoi(entry->d_name);
74 char path[4096];
75 struct stat value;
76 if (descriptor(fd, path, sizeof(path)) != 2 || fstat(fd, &value)
77 || !S_ISREG(value.st_mode)) continue;
78 size_t i;
79 for (i = 0; i < count; i++) {
80 if (seen[i].st_dev == value.st_dev && seen[i].st_ino == value.st_ino) break;
81 }
82 if (i < count) continue;
83 if (count == 256) _exit(97);
84 seen[count++] = value;
85 used += value.st_size;
86 }
87 closedir(directory);
88 return used;
89}
90static void exhausted(void) {
91 const char *path = getenv("ZIPELO_SPOOL_TRACE");
92 if (!path) return;
93 int fd = original_openat(AT_FDCWD, path, O_CREAT | O_WRONLY | O_APPEND, 0600);
94 if (fd >= 0) {
95 original_write(fd, "F", 1);
96 close(fd);
97 }
98}
99#define TEMPORARY_CAPACITY (2ULL * 1024 * 1024)
100static int reject(int at, int flags) {
101 if ((flags & O_TMPFILE) != O_TMPFILE && !(flags & O_CREAT)) return 0;
102 trace(at);
103 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
104 if (affected(at) && at < 2 && phase && !strcmp(phase, "tiny-tmpfs")) {
105 errno = EDQUOT;
106 return 1;
107 }
108 if (affected(at) && phase && (!strcmp(phase, "create") || !strcmp(phase, "denied"))) {
109 errno = !strcmp(phase, "denied") ? EACCES : EDQUOT;
110 return 1;
111 }
112 if (getenv("ZIPELO_SPOOL_NAMED") && (flags & O_TMPFILE) == O_TMPFILE) {
113 errno = EOPNOTSUPP;
114 return 1;
115 }
116 return 0;
117}
118int openat(int fd, const char *path, int flags, ...) {
119 mode_t mode = 0;
120 if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
121 va_list args;
122 va_start(args, flags);
123 mode = va_arg(args, unsigned int);
124 va_end(args);
125 }
126 char parent[4096], full[8192];
127 descriptor(fd, parent, sizeof(parent));
128 snprintf(full, sizeof(full), "%s/%s", parent, path);
129 if (reject(location(full), flags)) return -1;
130 int result = original_openat(fd, path, flags, mode);
131 if (result >= 0 && snapshot_fd < 0 && location(full) >= 0
132 && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) {
133 snapshot_fd = result;
134 }
135 return result;
136}
137int open64(const char *path, int flags, ...) {
138 mode_t mode = 0;
139 if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
140 va_list args;
141 va_start(args, flags);
142 mode = va_arg(args, unsigned int);
143 va_end(args);
144 }
145 char full[8192];
146 snprintf(full, sizeof(full), "%s/", path);
147 if (reject(location(full), flags)) return -1;
148 int result = original_open64(path, flags, mode);
149 if (result >= 0 && snapshot_fd < 0 && location(full) >= 0
150 && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) {
151 snapshot_fd = result;
152 }
153 return result;
154}
155ssize_t write(int fd, const void *bytes, size_t count) {
156 char path[4096];
157 int at = descriptor(fd, path, sizeof(path));
158 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
159 if (phase && !strcmp(phase, "tiny-tmpfs") && at == 2) {
160 struct stat value;
161 off_t position = lseek(fd, 0, SEEK_CUR);
162 if (position < 0 || fstat(fd, &value)) _exit(96);
163 unsigned long long used = temporary_usage();
164 unsigned long long room = used < TEMPORARY_CAPACITY ? TEMPORARY_CAPACITY - used : 0;
165 unsigned long long end = (unsigned long long)value.st_size + room;
166 if ((unsigned long long)position >= end) {
167 exhausted();
168 errno = ENOSPC;
169 return -1;
170 }
171 if (count > end - position) count = end - position;
172 }
173 int stage = phase && !strcmp(phase, "stage-write") && fd != snapshot_fd;
174 if (affected(at) && phase && (stage || !strcmp(phase, "write")
175 || !strcmp(phase, "changed") || !strcmp(phase, "io"))) {
176 if (!strcmp(phase, "io")) { errno = EIO; return -1; }
177 if (!written[at]++) return original_write(fd, bytes, count < 7 ? count : 7);
178 errno = EDQUOT;
179 return -1;
180 }
181 return original_write(fd, bytes, count);
182}
183int fstatvfs(int fd, struct statvfs *value) {
184 int (*original)(int, struct statvfs *) = dlsym(RTLD_NEXT, "fstatvfs");
185 int result = original(fd, value);
186 char path[4096];
187 int at = descriptor(fd, path, sizeof(path));
188 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
189 int stage = phase && !strcmp(phase, "stage-space") && fd != snapshot_fd;
190 if (!result && affected(at) && phase && (stage || !strcmp(phase, "space"))) {
191 value->f_frsize = 1;
192 value->f_bavail = stage ? 0 : 1024;
193 }
194 if (!result && affected(at) && phase && !strcmp(phase, "quota")) {
195 value->f_frsize = 1;
196 value->f_bavail = 65536;
197 }
198 if (!result && at == 2 && phase && !strcmp(phase, "tiny-tmpfs")) {
199 unsigned long long used = temporary_usage();
200 value->f_frsize = 1;
201 value->f_blocks = TEMPORARY_CAPACITY;
202 value->f_bavail = value->f_bfree = used < TEMPORARY_CAPACITY ? TEMPORARY_CAPACITY - used : 0;
203 }
204 return result;
205}
206
207ssize_t read(int fd, void *bytes, size_t count) {
208 ssize_t (*original)(int, void *, size_t) = dlsym(RTLD_NEXT, "read");
209 ssize_t length = original(fd, bytes, count);
210 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
211 const char *source = getenv("ZIPELO_SPOOL_SOURCE");
212 char path[4096];
213 descriptor(fd, path, sizeof(path));
214 struct stat data;
215 if (length > 0 && source && !strcmp(path, source)) source_read += length;
216 int late = phase && !strcmp(phase, "late-changed") && !fstat(fd, &data)
217 && source_read > (unsigned long long)data.st_size;
218 if (length > 0 && !source_changed && phase && (!strcmp(phase, "changed") || late)
219 && source && !strcmp(path, source)) {
220 source_changed = 1;
221 int output = original_openat(AT_FDCWD, source, O_WRONLY | O_APPEND);
222 if (output >= 0) {
223 original_write(output, "x", 1);
224 close(output);
225 }
226 }
227 return length;
228}