-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlibptkl.c
More file actions
210 lines (179 loc) · 4.36 KB
/
libptkl.c
File metadata and controls
210 lines (179 loc) · 4.36 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
#define _GNU_SOURCE
#include <dlfcn.h>
#include <fcntl.h>
#include <pty.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/uio.h>
#define LOG_FD 100
#define LEN(x) ( sizeof (x) / sizeof * (x) )
static struct {
int (*open)(const char *, int);
int (*openat)(int, const char *, int);
int (*close)(int);
int (*getpt)();
int (*openpty)(int *, int *, char *, const struct termios *, const struct winsize *);
pid_t (*forkpty)(int *, char *, const struct termios *, const struct winsize *);
ssize_t (*write)(int, const void *, size_t);
ssize_t (*writev)(int, const struct iovec *, int);
int (*posix_openpt)(int);
} libc;
static struct {
int fds[16];
} ctx;
static
void __attribute__((constructor))
init() {
libc.open = dlsym(RTLD_NEXT, "open");
if (!libc.open) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.openat = dlsym(RTLD_NEXT, "openat");
if (!libc.openat) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.close = dlsym(RTLD_NEXT, "close");
if (!libc.close) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.getpt = dlsym(RTLD_NEXT, "getpt");
if (!libc.getpt) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.openpty = dlsym(RTLD_NEXT, "openpty");
if (!libc.openpty) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.forkpty = dlsym(RTLD_NEXT, "forkpty");
if (!libc.forkpty) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.write = dlsym(RTLD_NEXT, "write");
if (!libc.write) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.writev = dlsym(RTLD_NEXT, "writev");
if (!libc.writev) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
libc.posix_openpt = dlsym(RTLD_NEXT, "posix_openpt");
if (!libc.posix_openpt) {
dprintf(LOG_FD, "Error in dlsym: %s\n", dlerror());
}
for (int i = 0; i < LEN(ctx.fds); i++) {
ctx.fds[i] = -1;
}
}
static
void __attribute__((destructor))
fini() {
}
static
void
append_fd(const int fd) {
for (int i = 0; i < LEN(ctx.fds); i++) {
if (ctx.fds[i] == -1) {
ctx.fds[i] = fd;
break;
}
}
}
static
void
remove_fd(const int fd) {
for (int i = 0; i < LEN(ctx.fds); i++) {
if (fd == ctx.fds[i]) {
ctx.fds[i] = -1;
}
}
}
static
int
is_pts_fd(const int fd) {
for (int i = 0; i < LEN(ctx.fds); i++) {
if (fd == ctx.fds[i]) {
return 1;
}
}
return 0;
}
int
open(const char * name, int flags, ...) {
int ret = libc.open(name, flags);
if (strstr(name, "/ptmx")) {
dprintf(LOG_FD, "%d: open: %s\n", getpid(), name);
append_fd(ret);
}
return ret;
}
int
openat(int dir, const char * name, int flags, ...) {
int ret = libc.openat(dir, name, flags);
if (strstr(name, "/ptmx")) {
dprintf(LOG_FD, "%d: openat: %s\n", getpid(), name);
append_fd(ret);
}
return ret;
}
int
close(int fd) {
if (fd == LOG_FD) {
return 0;
}
remove_fd(fd);
return libc.close(fd);
}
int
getpt() {
int fd = libc.getpt();
dprintf(LOG_FD, "%d: getpt: master: %d\n", getpid(), fd);
append_fd(fd);
return fd;
}
int
openpty(int * master, int * slave, char * name, const struct termios * termp, const struct winsize * winp) {
int ret;
ret = libc.openpty(master, slave, name, termp, winp);
dprintf(LOG_FD, "%d: openpty: master: %d slave: %d name: %s\n", getpid(), *master, *slave, name);
append_fd(*master);
return ret;
}
pid_t
forkpty(int * master, char * name, const struct termios * termp, const struct winsize * winp) {
pid_t ret;
ret = libc.forkpty(master, name, termp, winp);
dprintf(LOG_FD, "%d: forkpty: pid: %u master: %d name: %s\n", getpid(), ret, *master, name);
append_fd(*master);
return ret;
}
int
posix_openpt(int flags) {
int master;
master = libc.posix_openpt(flags);
dprintf(LOG_FD, "%d: posix_openpt: master: %d\n", getpid(), master);
append_fd(master);
return master;
}
ssize_t
write(int fd, const void * buf, size_t count) {
ssize_t ret;
ret = libc.write(fd, buf, count);
if (ret > 0 && is_pts_fd(fd)) {
dprintf(LOG_FD, "%d: write: fd: %d: size: %d: ", getpid(), fd, ret);
libc.writev(LOG_FD, (const struct iovec[]){{ buf, count }, { "\n", 1 }}, 2);
}
return ret;
}
ssize_t
writev(int fd, const struct iovec * iov, int iovcnt) {
ssize_t ret;
ret = libc.writev(fd, iov, iovcnt);
if (ret > 0 && is_pts_fd(fd)) {
dprintf(LOG_FD, "%d: writev: fd: %d: size: %d", getpid(), fd, ret);
libc.writev(LOG_FD, iov, iovcnt);
libc.write(LOG_FD, "\n", 1);
}
return ret;
}