-
Notifications
You must be signed in to change notification settings - Fork 8
/
sock_events.c
230 lines (197 loc) · 5.42 KB
/
sock_events.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
#define _GNU_SOURCE
#include "sock_events.h"
#include "utils.h"
#include <dlfcn.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#ifdef __ANDROID__
#define MUTEX_ERRORCHECK PTHREAD_ERRORCHECK_MUTEX_INITIALIZER
#else
#define MUTEX_ERRORCHECK PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
#endif
#define IS_OUT true
#define IS_IN false
static void trace_iov(int fd, bool is_out, const char *fn, int ret,
const struct iovec *iovec, int iovec_count)
{
int i, remain;
if (ret <= 0)
return;
remain = ret;
for (i = 0; i < iovec_count && remain > 0; i++) {
const struct iovec *vec = &iovec[i];
int written_len = vec->iov_len;
if (remain < vec->iov_len)
written_len = remain;
udtrace_data(fd, is_out, fn, vec->iov_base, written_len);
remain -= written_len;
}
}
/***********************************************************************
* Actual socket call handler functions
***********************************************************************/
void sock_ev_socket(int fd, int domain, int type, int protocol)
{
if (domain != AF_UNIX)
return;
udtrace_add_fd(fd);
}
void sock_ev_bind(int fd, int ret, int err, const struct sockaddr *addr, socklen_t len)
{
const struct sockaddr_un *sun = (const struct sockaddr_un *) addr;
if (ret < 0)
return;
LOG("bind(%d, \"%s\")\n", fd, sun->sun_path);
udtrace_fd_set_path(fd, sun->sun_path);
}
void sock_ev_connect(int fd, int ret, int err, const struct sockaddr *addr,
socklen_t len)
{
const struct sockaddr_un *sun = (const struct sockaddr_un *) addr;
if (ret < 0)
return;
LOG("connect(%d, \"%s\")\n", fd, sun->sun_path);
udtrace_fd_set_path(fd, sun->sun_path);
}
void sock_ev_listen(int fd, int ret, int err, int backlog)
{
}
void sock_ev_accept(int fd, int ret, int err, struct sockaddr *addr,
socklen_t *addr_len)
{
if (ret < 0)
return;
LOG("accept(fd=%d on parent fd=%d)\n", ret, fd);
udtrace_add_fd_child(fd, ret);
}
void sock_ev_accept4(int fd, int ret, int err, struct sockaddr *addr,
socklen_t *addr_len, int flags)
{
if (ret < 0)
return;
LOG("accept(fd=%d on parent fd=%d)\n", ret, fd);
udtrace_add_fd_child(fd, ret);
}
void sock_ev_send(int fd, int ret, int err, const void *buf, size_t bytes,
int flags)
{
if (ret <= 0)
return;
udtrace_data(fd, IS_OUT, "send", buf, ret);
}
void sock_ev_recv(int fd, int ret, int err, void *buf, size_t bytes, int flags)
{
if (ret <= 0)
return;
udtrace_data(fd, IS_IN, "recv", buf, ret);
}
void sock_ev_sendto(int fd, int ret, int err, const void *buf, size_t bytes,
int flags, const struct sockaddr *addr, socklen_t len)
{
if (ret <= 0)
return;
udtrace_data(fd, IS_OUT, "sendto", buf, ret);
}
void sock_ev_recvfrom(int fd, int ret, int err, void *buf, size_t bytes,
int flags, const struct sockaddr *addr, socklen_t *len)
{
if (ret <= 0)
return;
udtrace_data(fd, IS_IN, "recvfrom", buf, ret);
}
void sock_ev_sendmsg(int fd, int ret, int err, const struct msghdr *msg,
int flags)
{
trace_iov(fd, IS_OUT, "sendmsg", ret, msg->msg_iov, msg->msg_iovlen);
}
void sock_ev_recvmsg(int fd, int ret, int err, const struct msghdr *msg,
int flags)
{
trace_iov(fd, IS_IN, "recvmsg", ret, msg->msg_iov, msg->msg_iovlen);
}
#if !defined(__ANDROID__) || __ANDROID_API__ >= 21
void sock_ev_sendmmsg(int fd, int ret, int err, const struct mmsghdr *vmessages,
unsigned int vlen, int flags)
{
int i;
if (ret <= 0)
return;
for (i = 0; i < vlen; i++) {
const struct mmsghdr *mm = &vmessages[i];
trace_iov(fd, IS_OUT, "sendmmsg", mm->msg_len,
mm->msg_hdr.msg_iov, mm->msg_hdr.msg_iovlen);
}
}
void sock_ev_recvmmsg(int fd, int ret, int err, const struct mmsghdr *vmessages,
unsigned int vlen, int flags, const struct timespec *tmo)
{
int i;
if (ret <= 0)
return;
for (i = 0; i < vlen; i++) {
const struct mmsghdr *mm = &vmessages[i];
trace_iov(fd, IS_IN, "recvmmsg", mm->msg_len,
mm->msg_hdr.msg_iov, mm->msg_hdr.msg_iovlen);
}
}
#endif
void sock_ev_write(int fd, int ret, int err, const void *buf, size_t bytes)
{
if (ret <= 0)
return;
udtrace_data(fd, IS_OUT, "write", buf, ret);
}
void sock_ev_read(int fd, int ret, int err, void *buf, size_t bytes)
{
if (ret <= 0)
return;
udtrace_data(fd, IS_IN, "read", buf, ret);
}
void sock_ev_close(int fd, int ret, int err)
{
udtrace_del_fd(fd);
}
void sock_ev_dup(int fd, int ret, int err)
{
if (ret >= 0)
udtrace_add_fd(ret);
}
void sock_ev_dup2(int fd, int ret, int err, int newfd)
{
if (ret >= 0) {
udtrace_del_fd(newfd);
udtrace_add_fd(ret);
}
}
void sock_ev_dup3(int fd, int ret, int err, int newfd, int flags)
{
if (ret >= 0) {
udtrace_del_fd(newfd);
udtrace_add_fd(ret);
}
}
void sock_ev_writev(int fd, int ret, int err, const struct iovec *iovec,
int iovec_count)
{
trace_iov(fd, IS_OUT, "writev", ret, iovec, iovec_count);
}
void sock_ev_readv(int fd, int ret, int err, const struct iovec *iovec,
int iovec_count)
{
trace_iov(fd, IS_IN, "readv", ret, iovec, iovec_count);
}
void sock_ev_sendfile(int fd, int ret, int err, int in_fd, off_t *offset,
size_t bytes)
{
fprintf(stderr, "Cnnot trace sendmsg!");
}