-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_cache.c
166 lines (141 loc) · 3.58 KB
/
map_cache.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
/*
* map_cache.c
*
* Copyright 2007 Jean-Marc Saffroy <[email protected]>
* This file is part of the Driller library.
* Driller is free software, distributed under the terms of the
* GNU Lesser General Public License version 2.1.
*
* map, unmap and keep track of memory maps to foreign processes,
* avoiding the overhead of additional system calls when the same map
* is used for more than one transaction
*/
#include <unistd.h>
#include <string.h>
#include <search.h>
#include <assert.h>
#include "log.h"
#include "fdproxy.h"
#include "driller.h"
#include "map_cache.h"
#include "search.h"
#define MAP_CACHE_HSIZE_INIT 32
static struct hsearch_data map_cache;
static int map_cache_hsize = MAP_CACHE_HSIZE_INIT;
/*
* record a (key, map_cache) pair
*/
static void map_cache_hash(struct map_cache *mc, struct fdkey *key) {
char *buf;
ENTRY e, *ep;
int rc;
buf = fdproxy_keystr(key);
dbg("add <%s> = %p", buf, (mc ? mc->mc_addr : NULL));
e.key = buf;
rc = hsearch_r(e, FIND, &ep, &map_cache);
if(ep != NULL) {
ep->data = mc;
return;
}
e.key = strdup(buf);
assert(e.key != NULL);
e.data = mc;
rc = hsearch_r(e, ENTER, &ep, &map_cache);
if(rc == 0) {
/* retry with larger htable */
map_cache_hsize += map_cache_hsize/2;
if(hcreate_r(map_cache_hsize, &map_cache) == 0)
err("cannot grow htable (size=%d)",
map_cache_hsize);
rc = hsearch_r(e, ENTER, &ep, &map_cache);
if(rc == 0)
err("cannot insert into htable (size=%d)",
map_cache_hsize);
}
}
/*
* remove record of (key, map_cache) pair
*/
struct map_cache *map_cache_unhash(struct fdkey *key) {
char *buf;
ENTRY e, *ep;
int rc;
struct map_cache *mc;
buf = fdproxy_keystr(key);
e.key = buf;
rc = hsearch_r(e, FIND, &ep, &map_cache);
if(ep != NULL) {
mc = ep->data;
ep->data = NULL;
} else {
dbg("cannot find '%s' in htable", buf);
mc = NULL;
}
dbg("unhash <%s> = %p", buf, (mc ? mc->mc_addr : NULL));
return mc;
}
/*
* find and return map_cache matching key
*/
struct map_cache *map_cache_lookup(struct fdkey *key) {
char *buf;
ENTRY e, *ep;
int rc;
struct map_cache *mc;
buf = fdproxy_keystr(key);
e.key = buf;
rc = hsearch_r(e, FIND, &ep, &map_cache);
if(ep != NULL) {
mc = ep->data;
} else {
dbg2("cannot find '%s' in htable", buf);
mc = NULL;
}
dbg2("lookup <%s> = %p", buf, (mc ? mc->mc_addr : NULL));
return mc;
}
/*
* record new (key, map_cache) pair and establish memory map
*/
struct map_cache *map_cache_install(struct map_rec *map,
struct fdkey *key) {
struct map_cache *mc;
assert(map_cache_lookup(key) == NULL);
mc = malloc(sizeof(*mc));
assert(mc != NULL);
memcpy(&mc->mc_map, map, sizeof(*map));
mc->mc_addr = driller_install_map(map);
map_cache_hash(mc, key);
dbg("install <%s> @ %p", fdproxy_keystr(key), mc->mc_addr);
return mc;
}
/*
* refresh and remap map_cache for the given key
*/
void map_cache_update(struct map_rec *map, struct fdkey *key,
struct map_cache *mc) {
driller_remove_map(&mc->mc_map, mc->mc_addr);
memcpy(&mc->mc_map, map, sizeof(*map));
mc->mc_addr = driller_install_map(map);
dbg("update <%s> @ %p", fdproxy_keystr(key), mc->mc_addr);
}
/*
* unhash, unmap and close fd for the given key
*/
void map_cache_remove(struct fdkey *key) {
struct map_cache *mc;
mc = map_cache_unhash(key);
if(mc != NULL) {
dbg("remove <%s> = %p", fdproxy_keystr(key), mc->mc_addr);
driller_remove_map(&mc->mc_map, mc->mc_addr);
if(close(mc->mc_map.fd) != 0)
perr("close");
memset(mc, 0xf0, sizeof(mc));
free(mc);
}
}
void map_cache_init(void) {
int rc;
rc = hcreate_r(map_cache_hsize, &map_cache);
assert(rc != 0);
}