Repository navigation
Expand file tree
/
Copy pathlib.c
More file actions
217 lines (192 loc) · 6.15 KB
/
Copy pathlib.c
File metadata and controls
217 lines (192 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "common.c"
static void getProfileName(char *p, size_t size)
{
char *env = getenv("HAX11_PROFILE");
if (env) {
strncpy(p, env, size);
p[size - 1] = '\0';
return;
}
ssize_t nread = readlink("/proc/self/exe", p, size - 1);
if (nread == -1) {
log_error("readlink failed: %s\n", strerror(errno));
strncpy(p, "readlink-error", size - 1);
return;
}
if (nread == 0 || p[0] != '/') {
log_error("readlink got unexpected result: %s\n", p);
strncpy(p, "weird-readlink-result", size - 1);
return;
}
char *end = p + nread;
// Remove leading '/', replace remaining '/' to '\'
for (; *p && p<end; p++)
p[0] = p[1] == '/' ? '\\' : p[1];
end[-1] = '\0';
}
#include <sys/un.h>
#include <pthread.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <errno.h>
#define NEXT(handle, path, func) ({ \
static typeof(&func) pfunc = NULL; \
if (!pfunc) \
pfunc = (typeof(&func))dlsym(RTLD_NEXT, #func); \
if (!pfunc) { \
if (!handle) { \
handle = dlopen(path, RTLD_LAZY); \
} \
if (!handle) { \
needConfig(); \
log_error("Ack, dlopen of '%s' failed: %s\n", \
path, dlerror()); \
} \
pfunc = (typeof(&func))dlsym(handle, #func); \
} \
if (!pfunc) { \
needConfig(); \
log_error("Ack, dlsym of '%s' (from '%s') failed: %s\n", \
#func, path, dlerror()); \
} \
pfunc; \
})
static void fail(int err, const char* func)
{
log_error("%s failed (%d / %s)!\n",
func, err, strerror(err));
exit(1);
}
#define CHECKRET(expr, cond, err, func) ({ \
int ret = (expr); \
if (!(cond)) \
fail((err), (func)); \
ret; \
})
static void* libc = NULL;
static void* pthread = NULL;
int connect(int socket, const struct sockaddr *address,
socklen_t address_len)
{
int connect_result = NEXT(libc, LIBC_SO, connect)
(socket, address, address_len);
if (connect_result == 0)
{
if (address->sa_family == AF_UNIX)
{
struct sockaddr_un *ua = (struct sockaddr_un*)address;
const char* path = ua->sun_path;
if (!*path)
path++;
//log_debug("connect(%s,%d)\n", path, address_len);
if (!strncmp(path, "/tmp/.X11-unix/X", 16))
{
needConfig();
log_debug("Found X connection!\n");
if (config.enable)
{
log_debug("Intercepting X connection!\n");
int pair[2];
CHECKRET(socketpair(AF_UNIX, SOCK_STREAM, 0, pair),
ret == 0, ret, "socketpair");
X11ConnData* data = calloc(1, sizeof(X11ConnData));
static int index = 0;
data->index = index++;
data->server = dup(socket);
data->client = pair[0];
CHECKRET(dup2(pair[1], socket),
ret >= 0, errno, "dup2");
CHECKRET(close(pair[1]),
ret == 0, errno, "close");
if (config.fork)
{
log_debug("Forking...\n");
pid_t pid = fork();
CHECKRET(pid, ret >= 0, errno, "fork");
if (pid == 0)
{
pid = getpid();
log_debug("In child (%d), forking again\n", pid);
pid_t pid2 = fork();
CHECKRET(pid2, ret >= 0, errno, "fork");
if (pid2)
{
log_debug("Second fork is %d\n", pid2);
exit(0);
}
log_debug("In child, double-fork OK\n");
struct rlimit r;
CHECKRET(getrlimit(RLIMIT_NOFILE, &r),
ret >= 0, errno, "getrlimit");
for (int n=3; n<(int)r.rlim_cur; n++)
if (n != data->server && n != data->client)
close(n); // Ignore error
log_debug("Running main loop.\n");
workThreadProc(data);
log_debug("Fork is exiting.\n");
exit(0);
}
else
{
close(data->server);
close(data->client);
free(data);
log_debug("In parent! Child is %d\n", pid);
CHECKRET(waitpid(pid, NULL, 0),
ret >= 0, errno, "waitpid");
return connect_result;
}
}
pthread_attr_t attr = {};
CHECKRET(NEXT(pthread, LIBPTHREAD_SO, pthread_attr_init)(&attr),
ret == 0, ret, "pthread_attr_init");
CHECKRET(NEXT(pthread, LIBPTHREAD_SO, pthread_attr_setdetachstate)(&attr, PTHREAD_CREATE_DETACHED),
ret == 0, ret, "pthread_attr_setdetachstate");
pthread_t workThread;
CHECKRET(NEXT(pthread, LIBPTHREAD_SO, pthread_create)
(&workThread, &attr, workThreadProc, data),
ret == 0, ret, "pthread_create");
NEXT(pthread, LIBPTHREAD_SO, pthread_attr_destroy)(&attr);
}
}
}
}
return connect_result;
}
// Work around https://github.com/ValveSoftware/steam-for-linux/issues/8542
__attribute__ ((visibility("default")))
char *realpath(
const char *restrict path,
char *restrict resolved_path
)
{
char* res = NEXT(libc, LIBC_SO, realpath)(path, resolved_path);
if (resolved_path == NULL && res != NULL)
{
needConfig();
if (config.enable && config.padRealpath)
{
char* copy = (char*)malloc(strlen(res) + /*PATH_MAX*/4096);
strcpy(copy, res);
free(res);
res = copy;
}
}
return res;
}
#define LIBGL_SO "libGL.so.1"
static void* libGL = NULL;
static void glXSwapIntervalEXT(
__attribute__((unused)) /*Display*/void *dpy,
__attribute__((unused)) /*GLXDrawable*/XID drawable,
__attribute__((unused)) int interval)
{
log_debug("Stubbed: glXSwapIntervalEXT(%p, 0x%lx, %d)\n", dpy, drawable, interval);
}
void *glXGetProcAddressARB(const /*GLubyte*/char *procName)
{
log_debug("glXGetProcAddressARB(%s)\n", procName);
if (strcmp(procName, "glXSwapIntervalEXT") == 0 && config.openGLForceVSyncOff)
return &glXSwapIntervalEXT;
return NEXT(libGL, LIBGL_SO, glXGetProcAddressARB)(procName);
}