team-ljyzipelo

공개
167줄5.9 KB
1#define _GNU_SOURCE
2#include <dlfcn.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <stdarg.h>
6#include <stdio.h>
7#include <stdlib.h>
8#include <string.h>
9#include <sys/statvfs.h>
10#include <sys/stat.h>
11#include <sys/resource.h>
12#include <signal.h>
13#include <unistd.h>
14
15static int (*original_openat)(int, const char *, int, ...);
16static int (*original_open64)(const char *, int, ...);
17static ssize_t (*original_write)(int, const void *, size_t);
18static int written[3];
19static int snapshot_fd = -1;
20static int source_changed;
21static unsigned long long source_read;
22
23__attribute__((constructor)) static void initialize(void) {
24 original_openat = dlsym(RTLD_NEXT, "openat");
25 original_open64 = dlsym(RTLD_NEXT, "open64");
26 original_write = dlsym(RTLD_NEXT, "write");
27 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
28 if (phase && !strcmp(phase, "quota")) {
29 struct rlimit limit = {65536, 65536};
30 signal(SIGXFSZ, SIG_IGN);
31 if (setrlimit(RLIMIT_FSIZE, &limit)) _exit(99);
32 }
33}
34static int location(const char *path) {
35 if (strstr(path, "/cache/")) return 0;
36 if (strstr(path, "/archive/")) return 1;
37 if (strstr(path, "/system/")) return 2;
38 return -1;
39}
40static int descriptor(int fd, char *path, size_t size) {
41 char link[64];
42 path[0] = 0;
43 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
44 ssize_t length = readlink(link, path, size - 1);
45 if (length < 0) return -1;
46 path[length] = 0;
47 return location(path);
48}
49static int affected(int at) {
50 const char *mask = getenv("ZIPELO_SPOOL_MASK");
51 return at >= 0 && mask && (atoi(mask) & (1 << at));
52}
53static void trace(int at) {
54 const char *path = getenv("ZIPELO_SPOOL_TRACE");
55 if (!path || at < 0) return;
56 int fd = original_openat(AT_FDCWD, path, O_CREAT | O_WRONLY | O_APPEND, 0600);
57 if (fd >= 0) {
58 char value = '0' + at;
59 original_write(fd, &value, 1);
60 close(fd);
61 }
62}
63static int reject(int at, int flags) {
64 if ((flags & O_TMPFILE) != O_TMPFILE && !(flags & O_CREAT)) return 0;
65 trace(at);
66 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
67 if (affected(at) && phase && (!strcmp(phase, "create") || !strcmp(phase, "denied"))) {
68 errno = !strcmp(phase, "denied") ? EACCES : EDQUOT;
69 return 1;
70 }
71 if (getenv("ZIPELO_SPOOL_NAMED") && (flags & O_TMPFILE) == O_TMPFILE) {
72 errno = EOPNOTSUPP;
73 return 1;
74 }
75 return 0;
76}
77int openat(int fd, const char *path, int flags, ...) {
78 mode_t mode = 0;
79 if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
80 va_list args;
81 va_start(args, flags);
82 mode = va_arg(args, unsigned int);
83 va_end(args);
84 }
85 char parent[4096], full[8192];
86 descriptor(fd, parent, sizeof(parent));
87 snprintf(full, sizeof(full), "%s/%s", parent, path);
88 if (reject(location(full), flags)) return -1;
89 int result = original_openat(fd, path, flags, mode);
90 if (result >= 0 && snapshot_fd < 0 && location(full) >= 0
91 && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) {
92 snapshot_fd = result;
93 }
94 return result;
95}
96int open64(const char *path, int flags, ...) {
97 mode_t mode = 0;
98 if ((flags & O_CREAT) || (flags & O_TMPFILE) == O_TMPFILE) {
99 va_list args;
100 va_start(args, flags);
101 mode = va_arg(args, unsigned int);
102 va_end(args);
103 }
104 char full[8192];
105 snprintf(full, sizeof(full), "%s/", path);
106 if (reject(location(full), flags)) return -1;
107 int result = original_open64(path, flags, mode);
108 if (result >= 0 && snapshot_fd < 0 && location(full) >= 0
109 && ((flags & O_TMPFILE) == O_TMPFILE || (flags & O_CREAT))) {
110 snapshot_fd = result;
111 }
112 return result;
113}
114ssize_t write(int fd, const void *bytes, size_t count) {
115 char path[4096];
116 int at = descriptor(fd, path, sizeof(path));
117 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
118 int stage = phase && !strcmp(phase, "stage-write") && fd != snapshot_fd;
119 if (affected(at) && phase && (stage || !strcmp(phase, "write")
120 || !strcmp(phase, "changed") || !strcmp(phase, "io"))) {
121 if (!strcmp(phase, "io")) { errno = EIO; return -1; }
122 if (!written[at]++) return original_write(fd, bytes, count < 7 ? count : 7);
123 errno = EDQUOT;
124 return -1;
125 }
126 return original_write(fd, bytes, count);
127}
128int fstatvfs(int fd, struct statvfs *value) {
129 int (*original)(int, struct statvfs *) = dlsym(RTLD_NEXT, "fstatvfs");
130 int result = original(fd, value);
131 char path[4096];
132 int at = descriptor(fd, path, sizeof(path));
133 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
134 int stage = phase && !strcmp(phase, "stage-space") && fd != snapshot_fd;
135 if (!result && affected(at) && phase && (stage || !strcmp(phase, "space"))) {
136 value->f_frsize = 1;
137 value->f_bavail = stage ? 0 : 1024;
138 }
139 if (!result && affected(at) && phase && !strcmp(phase, "quota")) {
140 value->f_frsize = 1;
141 value->f_bavail = 65536;
142 }
143 return result;
144}
145
146ssize_t read(int fd, void *bytes, size_t count) {
147 ssize_t (*original)(int, void *, size_t) = dlsym(RTLD_NEXT, "read");
148 ssize_t length = original(fd, bytes, count);
149 const char *phase = getenv("ZIPELO_SPOOL_PHASE");
150 const char *source = getenv("ZIPELO_SPOOL_SOURCE");
151 char path[4096];
152 descriptor(fd, path, sizeof(path));
153 struct stat data;
154 if (length > 0 && source && !strcmp(path, source)) source_read += length;
155 int late = phase && !strcmp(phase, "late-changed") && !fstat(fd, &data)
156 && source_read > (unsigned long long)data.st_size;
157 if (length > 0 && !source_changed && phase && (!strcmp(phase, "changed") || late)
158 && source && !strcmp(path, source)) {
159 source_changed = 1;
160 int output = original_openat(AT_FDCWD, source, O_WRONLY | O_APPEND);
161 if (output >= 0) {
162 original_write(output, "x", 1);
163 close(output);
164 }
165 }
166 return length;
167}