| 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 | |
| 16 | static int (*original_openat)(int, const char *, int, ...); |
| 17 | static int (*original_open64)(const char *, int, ...); |
| 18 | static ssize_t (*original_write)(int, const void *, size_t); |
| 19 | static int written[3]; |
| 20 | static int snapshot_fd = -1; |
| 21 | static int source_changed; |
| 22 | static 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 | } |
| 35 | static 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 | } |
| 41 | static 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 | } |
| 50 | static int affected(int at) { |
| 51 | const char *mask = getenv("ZIPELO_SPOOL_MASK"); |
| 52 | return at >= 0 && mask && (atoi(mask) & (1 << at)); |
| 53 | } |
| 54 | static 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 | } |
| 64 | struct usage { |
| 65 | struct stat seen[256]; |
| 66 | size_t count; |
| 67 | unsigned long long bytes; |
| 68 | }; |
| 69 | static void count_file(struct usage *usage, const struct stat *value) { |
| 70 | if (!S_ISREG(value->st_mode)) return; |
| 71 | for (size_t i = 0; i < usage->count; i++) { |
| 72 | if (usage->seen[i].st_dev == value->st_dev |
| 73 | && usage->seen[i].st_ino == value->st_ino) return; |
| 74 | } |
| 75 | if (usage->count == 256) _exit(97); |
| 76 | usage->seen[usage->count++] = *value; |
| 77 | usage->bytes += value->st_size; |
| 78 | } |
| 79 | static void count_directory(DIR *directory, struct usage *usage, unsigned depth) { |
| 80 | if (depth > 64) _exit(97); |
| 81 | struct dirent *entry; |
| 82 | while ((entry = readdir(directory))) { |
| 83 | if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) continue; |
| 84 | struct stat value; |
| 85 | if (fstatat(dirfd(directory), entry->d_name, &value, AT_SYMLINK_NOFOLLOW)) _exit(98); |
| 86 | count_file(usage, &value); |
| 87 | if (S_ISDIR(value.st_mode)) { |
| 88 | int fd = original_openat(dirfd(directory), entry->d_name, |
| 89 | O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC); |
| 90 | if (fd < 0) _exit(98); |
| 91 | DIR *child = fdopendir(fd); |
| 92 | if (!child) _exit(98); |
| 93 | count_directory(child, usage, depth + 1); |
| 94 | closedir(child); |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | static unsigned long long temporary_usage(void) { |
| 99 | struct usage usage = {0}; |
| 100 | const char *root = getenv("TMPDIR"); |
| 101 | if (!root) _exit(98); |
| 102 | DIR *directory = opendir(root); |
| 103 | if (!directory) _exit(98); |
| 104 | // 닫혀도 이름이 남은 파일은 계속 용량을 사용함. |
| 105 | count_directory(directory, &usage, 0); |
| 106 | closedir(directory); |
| 107 | directory = opendir("/proc/self/fd"); |
| 108 | if (!directory) _exit(98); |
| 109 | struct dirent *entry; |
| 110 | // 이름이 지워진 열린 파일도 마지막 핸들을 닫기 전까지 집계함. |
| 111 | while ((entry = readdir(directory))) { |
| 112 | if (entry->d_name[0] == '.') continue; |
| 113 | int fd = atoi(entry->d_name); |
| 114 | char path[4096]; |
| 115 | struct stat value; |
| 116 | if (descriptor(fd, path, sizeof(path)) == 2 && !fstat(fd, &value)) { |
| 117 | count_file(&usage, &value); |
| 118 | } |
| 119 | } |
| 120 | closedir(directory); |
| 121 | return usage.bytes; |
| 122 | } |
| 123 | static void exhausted(void) { |
| 124 | const char *path = getenv("ZIPELO_SPOOL_TRACE"); |
| 125 | if (!path) return; |
| 126 | int fd = original_openat(AT_FDCWD, path, O_CREAT | O_WRONLY | O_APPEND, 0600); |
| 127 | if (fd >= 0) { |
| 128 | original_write(fd, "F", 1); |
| 129 | close(fd); |
| 130 | } |
| 131 | } |
| 132 | #define TEMPORARY_CAPACITY (2ULL * 1024 * 1024) |
| 133 | static int reject(int at, int flags) { |
| 134 | if ((flags & O_TMPFILE) != O_TMPFILE && !(flags & O_CREAT)) return 0; |
| 135 | trace(at); |
| 136 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 137 | if (affected(at) && at < 2 && phase && !strcmp(phase, "tiny-tmpfs")) { |
| 138 | errno = EDQUOT; |
| 139 | return 1; |
| 140 | } |
| 141 | if (affected(at) && phase && (!strcmp(phase, "create") || !strcmp(phase, "denied"))) { |
| 142 | errno = !strcmp(phase, "denied") ? EACCES : EDQUOT; |
| 143 | return 1; |
| 144 | } |
| 145 | if (getenv("ZIPELO_SPOOL_NAMED") && (flags & O_TMPFILE) == O_TMPFILE) { |
| 146 | errno = EOPNOTSUPP; |
| 147 | return 1; |
| 148 | } |
| 149 | return 0; |
| 150 | } |
| 151 | int openat(int fd, const char *path, int flags, ...) { |
| 152 | mode_t mode = 0; |
| 153 | if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) { |
| 154 | va_list args; |
| 155 | va_start(args, flags); |
| 156 | mode = va_arg(args, unsigned int); |
| 157 | va_end(args); |
| 158 | } |
| 159 | char parent[4096], full[8192]; |
| 160 | descriptor(fd, parent, sizeof(parent)); |
| 161 | snprintf(full, sizeof(full), "%s/%s", parent, path); |
| 162 | if (reject(location(full), flags)) return -1; |
| 163 | int result = original_openat(fd, path, flags, mode); |
| 164 | if (result >= 0 && snapshot_fd < 0 && location(full) >= 0 |
| 165 | && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) { |
| 166 | snapshot_fd = result; |
| 167 | } |
| 168 | return result; |
| 169 | } |
| 170 | int open64(const char *path, int flags, ...) { |
| 171 | mode_t mode = 0; |
| 172 | if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) { |
| 173 | va_list args; |
| 174 | va_start(args, flags); |
| 175 | mode = va_arg(args, unsigned int); |
| 176 | va_end(args); |
| 177 | } |
| 178 | char full[8192]; |
| 179 | snprintf(full, sizeof(full), "%s/", path); |
| 180 | if (reject(location(full), flags)) return -1; |
| 181 | int result = original_open64(path, flags, mode); |
| 182 | if (result >= 0 && snapshot_fd < 0 && location(full) >= 0 |
| 183 | && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) { |
| 184 | snapshot_fd = result; |
| 185 | } |
| 186 | return result; |
| 187 | } |
| 188 | ssize_t write(int fd, const void *bytes, size_t count) { |
| 189 | char path[4096]; |
| 190 | int at = descriptor(fd, path, sizeof(path)); |
| 191 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 192 | if (phase && !strcmp(phase, "tiny-tmpfs") && at == 2) { |
| 193 | struct stat value; |
| 194 | off_t position = lseek(fd, 0, SEEK_CUR); |
| 195 | if (position < 0 || fstat(fd, &value)) _exit(96); |
| 196 | unsigned long long used = temporary_usage(); |
| 197 | unsigned long long room = used < TEMPORARY_CAPACITY ? TEMPORARY_CAPACITY - used : 0; |
| 198 | unsigned long long end = (unsigned long long)value.st_size + room; |
| 199 | if ((unsigned long long)position >= end) { |
| 200 | exhausted(); |
| 201 | errno = ENOSPC; |
| 202 | return -1; |
| 203 | } |
| 204 | if (count > end - position) count = end - position; |
| 205 | } |
| 206 | int stage = phase && !strcmp(phase, "stage-write") && fd != snapshot_fd; |
| 207 | if (affected(at) && phase && (stage || !strcmp(phase, "write") |
| 208 | || !strcmp(phase, "changed") || !strcmp(phase, "io"))) { |
| 209 | if (!strcmp(phase, "io")) { errno = EIO; return -1; } |
| 210 | if (!written[at]++) return original_write(fd, bytes, count < 7 ? count : 7); |
| 211 | errno = EDQUOT; |
| 212 | return -1; |
| 213 | } |
| 214 | return original_write(fd, bytes, count); |
| 215 | } |
| 216 | int fstatvfs(int fd, struct statvfs *value) { |
| 217 | int (*original)(int, struct statvfs *) = dlsym(RTLD_NEXT, "fstatvfs"); |
| 218 | int result = original(fd, value); |
| 219 | char path[4096]; |
| 220 | int at = descriptor(fd, path, sizeof(path)); |
| 221 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 222 | int stage = phase && !strcmp(phase, "stage-space") && fd != snapshot_fd; |
| 223 | if (!result && affected(at) && phase && (stage || !strcmp(phase, "space"))) { |
| 224 | value->f_frsize = 1; |
| 225 | value->f_bavail = stage ? 0 : 1024; |
| 226 | } |
| 227 | if (!result && affected(at) && phase && !strcmp(phase, "quota")) { |
| 228 | value->f_frsize = 1; |
| 229 | value->f_bavail = 65536; |
| 230 | } |
| 231 | if (!result && at == 2 && phase && !strcmp(phase, "tiny-tmpfs")) { |
| 232 | unsigned long long used = temporary_usage(); |
| 233 | value->f_frsize = 1; |
| 234 | value->f_blocks = TEMPORARY_CAPACITY; |
| 235 | value->f_bavail = value->f_bfree = used < TEMPORARY_CAPACITY ? TEMPORARY_CAPACITY - used : 0; |
| 236 | } |
| 237 | return result; |
| 238 | } |
| 239 | |
| 240 | ssize_t read(int fd, void *bytes, size_t count) { |
| 241 | ssize_t (*original)(int, void *, size_t) = dlsym(RTLD_NEXT, "read"); |
| 242 | ssize_t length = original(fd, bytes, count); |
| 243 | const char *phase = getenv("ZIPELO_SPOOL_PHASE"); |
| 244 | const char *source = getenv("ZIPELO_SPOOL_SOURCE"); |
| 245 | char path[4096]; |
| 246 | descriptor(fd, path, sizeof(path)); |
| 247 | struct stat data; |
| 248 | if (length > 0 && source && !strcmp(path, source)) source_read += length; |
| 249 | int late = phase && !strcmp(phase, "late-changed") && !fstat(fd, &data) |
| 250 | && source_read > (unsigned long long)data.st_size; |
| 251 | if (length > 0 && !source_changed && phase && (!strcmp(phase, "changed") || late) |
| 252 | && source && !strcmp(path, source)) { |
| 253 | source_changed = 1; |
| 254 | int output = original_openat(AT_FDCWD, source, O_WRONLY | O_APPEND); |
| 255 | if (output >= 0) { |
| 256 | original_write(output, "x", 1); |
| 257 | close(output); |
| 258 | } |
| 259 | } |
| 260 | return length; |
| 261 | } |