-
Notifications
You must be signed in to change notification settings - Fork 0
/
libwatchit.c
110 lines (86 loc) · 2.41 KB
/
libwatchit.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
/*
* libwatchit.c - LD_PRELOAD library that traces successful open() calls
*/
#define __USE_GNU
#include <stdarg.h>
#include <fcntl.h>
#include <dlfcn.h>
// #define DEBUG
#include "watchit.h"
static int sock;
int open(const char *, int, ...) __attribute__ ((weak, alias("wrap_open")));
int __open(const char *, int, ...) __attribute__ ((weak, alias("wrap_open")));
int open64(const char *, int, ...) __attribute__ ((weak, alias("wrap_open64")));
int __open64(const char *, int, ...) __attribute__ ((weak, alias("wrap_open64")));
static int (*orig_open)(const char *, int, ...) = NULL;
static int (*orig_open64)(const char *, int, ...) = NULL;
static int
write_sock(int sock, const char *name)
{
/*
* Maybe use:
* open:<name>
* or
* <pid>:open:<name>
*/
DPRINTF("writing %s\n", name);
if (write(sock, name, strlen(name)) < 0)
return -1;
if (write(sock, "\n", 1) < 0)
return -1;
return 0;
}
int
wrap_open(const char *name, int flags, ...)
{
va_list args;
mode_t mode;
int rc;
va_start(args, flags);
mode = va_arg(args, mode_t);
va_end(args);
DPRINTF("calling libc open(%s, 0x%x, 0x%x)\n", name, flags, mode);
rc = orig_open(name, flags, mode);
if (rc >= 0)
(void)write_sock(sock, name); // XXX
return rc;
}
int
wrap_open64(const char *name, int flags, ...)
{
va_list args;
mode_t mode;
int rc;
va_start(args, flags);
mode = va_arg(args, mode_t);
va_end(args);
DPRINTF("calling libc open64(%s, 0x%x, 0x%x)\n", name, flags, mode);
rc = orig_open64(name, flags, mode);
if (rc >= 0)
(void)write_sock(sock, name); // XXX
return rc;
}
void _init(void)
{
char *p;
p = getenv("SOCK_PATH");
if (p == NULL)
handle_error("SOCK_PATH not set");
DPRINTF("sock_path=%s\n", p);
struct sockaddr_un my_addr;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
handle_error("socket");
memset(&my_addr, 0, sizeof(struct sockaddr_un));
my_addr.sun_family = AF_UNIX;
strncpy(my_addr.sun_path, p,
sizeof(my_addr.sun_path) - 1);
if (connect(sock, (const struct sockaddr *)&my_addr, sizeof(my_addr.sun_path) - 1) < 0)
handle_error("connect");
orig_open = dlsym( ((void *) -1l), "open");
if (orig_open < 0)
handle_error("error: missing symbol open!");
orig_open64 = dlsym( ((void *) -1l), "open64");
if (orig_open64 < 0)
handle_error("error: missing symbol open64!");
}