-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg.c
265 lines (229 loc) · 6.55 KB
/
pkg.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
* This file is ugly. It needs cleaned up and probably rewritten.
*
* Things are a bit intense in here as we try to do a bit of
* caching of packages.
* * File is accessed causing package to be opened
* * ref count is increased (initially to 1)
* * File is closed
* * ref count is reduced
* * Another file in package is opened before the
* garbage collect time out
* * Initial handle to package is reused
* * ref count increased
* ....
* This should help with things like compiling using several
* headers from a package or starting programs which read
* a bunch of config files.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/file.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "atomicio.h"
#include "balloc.h"
#include "conf.h"
#include "db.h"
#include "evt.h"
#include "logger.h"
#include "memory.h"
#include "pkg.h"
#include "str.h"
#include "timestr.h"
/* private module-global stuff */
static BlockHeap *pkg_heap; /* Block allocator heap */
static dlink_list pkg_list; /* List of currently opened packages */
static time_t pkg_lifetime = 0; /* see pkg_init() for initialization */
static dlink_node *pkg_findnode(struct pkg_handle *pkg) {
dlink_node *ptr, *tptr;
DLINK_FOREACH_SAFE(ptr, tptr, pkg_list.head) {
if ((struct pkg_handle *)ptr->data == pkg)
return ptr;
}
return NULL;
}
/*
* release the package handle
*
* Should only be called by the Garbage Collector
* unless we get tight on memory (XXX: Add this)
*/
static void pkg_release(struct pkg_handle *pkg) {
dlink_node *ptr;
/*
* If name is allocated, free it
*/
if (pkg->name) {
mem_free(pkg->name);
pkg->name = NULL;
}
/*
* Unlock the file
*/
flock(pkg->fd, LOCK_UN);
/*
* close handle, if exists
*/
if (pkg->fd) {
close(pkg->fd);
pkg->fd = -1;
}
if ((ptr = pkg_findnode((struct pkg_handle *)pkg)) != NULL) {
dlink_delete(ptr, &pkg_list);
blockheap_free(pkg_heap, ptr->data);
}
}
/*
* Find a package by name
* Used by pkg_open to locate an existing reference
* to a package that hasn't been garbage collected yet
*/
static struct pkg_handle *pkg_handle_byname(const char *path) {
dlink_node *ptr, *tptr;
struct pkg_handle *p;
DLINK_FOREACH_SAFE(ptr, tptr, pkg_list.head) {
p = (struct pkg_handle *)ptr->data;
if (strcmp(p->name, path) == 0)
return p;
}
return NULL;
}
/*
* Scan for packages with refcnt == 0 every once in a while and close them
* This helps to reduce closing and reopening packages unneededly
* Adjust tuning parameters in fs-pkg.cf as needed, based on your memory model.
* if rfcnt == 0 && now > otime + (tuning.time.pkg_gc / 2): release it
*/
static void pkg_gc(int fd, short event, void *arg) {
dlink_node *ptr, *tptr;
struct pkg_handle *p;
DLINK_FOREACH_SAFE(ptr, tptr, pkg_list.head) {
p = (struct pkg_handle *)ptr->data;
if (p->refcnt == 0 && (time(NULL) > p->otime + (pkg_lifetime / 2)))
pkg_release(p);
}
}
/*
* Open a package, so that we can map files within it
*
* First, we try to see if the package already exists
* in the cache. If it does, use it.
*
* Either way, we bump the ref count
*/
struct pkg_handle *pkg_open(const char *path) {
struct pkg_handle *t;
int res = 0;
/*
* try to find an existing handle for the package
* If this fails, create one and cache it...
*/
if ((t = pkg_handle_byname(path)) == NULL) {
t = blockheap_alloc(pkg_heap);
t->name = str_dup(path);
if ((t->fd = open(t->name, O_RDONLY)) < 0) {
Log(LOG_ERROR, "failed opening pkg %s, bailing...", t->name);
pkg_release(t);
return NULL;
}
/*
* Try to acquire an exclusive lock, fail if we cant
*/
if (flock(t->fd, LOCK_EX | LOCK_NB) == -1) {
Log(LOG_ERROR, "failed locking package %s, bailing...", t->name);
pkg_release(t);
return NULL;
}
/*
* Add handle to the cache list
*/
dlink_add_tail_alloc(t, &pkg_list);
}
/*
* Adjust last used time and reference count either way...
*/
t->otime = time(NULL);
t->refcnt++;
return t;
}
/*
* reduce the package's reference count
*
* We no longer free the package as the garbage collector
* will do this for us after a delay, helping to keep packages
* cached for a bit and reducing IO overhead
*/
void pkg_close(struct pkg_handle *pkg) {
pkg->refcnt--;
Log(LOG_DEBUG, "pkg_close: pkg %s refcnt == %d", pkg->name, pkg->refcnt);
}
/* Handle vfs_watch CREATED event */
int pkg_import(const char *path) {
char *tmp = NULL;
int infd, outfd;
int db_id = -1;
/*
* XXX: what do we do when an in-use package is modified?
* XXX: for now I'm going to create a mandatory lock on
* XXX: the package when it is opened, so this shouldn't happen
*/
if (pkg_handle_byname(path)) {
Log(LOG_HACK, "Active package %s was changed, ignoring...");
return EXIT_SUCCESS;
}
Log(LOG_INFO, "importing pkg %s", path);
db_begin();
#if defined(CONFIG_TOC_LIBXML2)
/*
* Try to extract the package TOC
*/
if ((tmp = pkg_toc_extract(path)) == NULL) {
Log(LOG_ERROR, "failed to export toc for %s", path);
return EXIT_FAILURE;
}
Log(LOG_DEBUG, "TOC for %s extracted, processing", path);
#endif
/*
* Process the extracted TOC
*/
if (!pkg_toc_process(path, tmp)) {
Log(LOG_DEBUG, "package %s seems valid, committing...", path);
db_commit();
} else {
Log(LOG_ERROR, "failed adding package %s to database, skipping...", path);
db_rollback();
}
#if defined(CONFIG_TOC_LIBXML2)
/*
* clean up: remove extracted toc and free its name buffer
*/
unlink(tmp);
mem_free(tmp);
#endif
return EXIT_SUCCESS;
}
/* Handle vfs_watch REMOVED event */
int pkg_forget(const char *path) {
db_pkg_remove(path);
return EXIT_SUCCESS;
}
void pkg_init(void) {
if (!
(pkg_heap =
blockheap_create(sizeof(struct pkg_handle),
dconf_get_int("tuning.heap.pkg", 128), "pkg"))) {
Log(LOG_FATAL, "pkg_init(): block allocator failed");
raise(SIGTERM);
}
pkg_lifetime = nn2_timestr_to_time(dconf_get_str("tuning.timer.pkg_gc", NULL), 60);
evt_timer_add_periodic(pkg_gc, "gc.pkg", pkg_lifetime);
#if defined(CONFIG_TOC_LIBXAR)
pkg_toc_zbufsize = dconf_get_int("tuning.toc.gz.buffer", 1310720);
#endif
}