-
Notifications
You must be signed in to change notification settings - Fork 9
/
lib.c
155 lines (138 loc) · 4.64 KB
/
lib.c
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
#include "common.c"
static void getProfileName(char *p, size_t size)
{
readlink("/proc/self/exe", p, size);
char *end = p + size;
// Remove leading '/', replace remaining '/' to '\'
for (; *p && p<end; p++)
p[0] = p[1] == '/' ? '\\' : p[1];
}
#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;
}