| | varnish-cache/lib/libvcc/vcc_vmod.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2010-2015 Varnish Software AS |
2 |
|
* All rights reserved. |
3 |
|
* |
4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
5 |
|
* |
6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
7 |
|
* |
8 |
|
* Redistribution and use in source and binary forms, with or without |
9 |
|
* modification, are permitted provided that the following conditions |
10 |
|
* are met: |
11 |
|
* 1. Redistributions of source code must retain the above copyright |
12 |
|
* notice, this list of conditions and the following disclaimer. |
13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
14 |
|
* notice, this list of conditions and the following disclaimer in the |
15 |
|
* documentation and/or other materials provided with the distribution. |
16 |
|
* |
17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
27 |
|
* SUCH DAMAGE. |
28 |
|
* |
29 |
|
* Parse `import`, check metadata and versioning. |
30 |
|
* |
31 |
|
*/ |
32 |
|
|
33 |
|
#include "config.h" |
34 |
|
|
35 |
|
#include <stdlib.h> |
36 |
|
#include <string.h> |
37 |
|
|
38 |
|
#include "vcc_compile.h" |
39 |
|
|
40 |
|
#include "libvcc.h" |
41 |
|
#include "vfil.h" |
42 |
|
#include "vjsn.h" |
43 |
|
#include "vmod_abi.h" |
44 |
|
|
45 |
|
#include "vcc_vmod.h" |
46 |
|
|
47 |
|
struct vmod_import { |
48 |
|
unsigned magic; |
49 |
|
#define VMOD_IMPORT_MAGIC 0x31803a5d |
50 |
|
const char *err; |
51 |
|
struct vsb *json; |
52 |
|
char *path; |
53 |
|
VTAILQ_ENTRY(vmod_import) list; |
54 |
|
int from_vext; |
55 |
|
int unimported_vext; |
56 |
|
|
57 |
|
// From $VMOD |
58 |
|
double vmod_syntax; |
59 |
|
char *name; |
60 |
|
char *func_name; |
61 |
|
char *file_id; |
62 |
|
char *abi; |
63 |
|
unsigned major; |
64 |
|
unsigned minor; |
65 |
|
|
66 |
|
struct symbol *sym; |
67 |
|
const struct token *t_mod; |
68 |
|
struct vjsn *vj; |
69 |
|
#define STANZA(UU, ll, ss) int n_##ll; |
70 |
|
STANZA_TBL |
71 |
|
#undef STANZA |
72 |
|
}; |
73 |
|
|
74 |
|
static VTAILQ_HEAD(,vmod_import) imports = VTAILQ_HEAD_INITIALIZER(imports); |
75 |
|
|
76 |
|
typedef void vcc_do_stanza_f(struct vcc *tl, const struct vmod_import *vim, |
77 |
|
const struct vjsn_val *vv); |
78 |
|
|
79 |
|
static int |
80 |
23440 |
vcc_Extract_JSON(struct vmod_import *vim, const char *filename) |
81 |
|
{ |
82 |
23440 |
const char *magic = "VMOD_JSON_SPEC\x02", *p; |
83 |
|
int c; |
84 |
|
FILE *f; |
85 |
|
|
86 |
23440 |
CHECK_OBJ_NOTNULL(vim, VMOD_IMPORT_MAGIC); |
87 |
23440 |
AN(filename); |
88 |
|
|
89 |
23440 |
f = fopen(filename, "rb"); |
90 |
23440 |
if (f == NULL) { |
91 |
40 |
vim->err = strerror(errno); |
92 |
40 |
return (-1); |
93 |
|
} |
94 |
|
|
95 |
23400 |
p = magic; |
96 |
23400 |
vim->err = "No VMOD JSON found"; |
97 |
1799840 |
while (1) { |
98 |
453324000 |
c = getc(f); |
99 |
453324000 |
if (c == EOF) { |
100 |
40 |
AZ(fclose(f)); |
101 |
40 |
vim->err = "No VMOD JSON found"; |
102 |
40 |
return (-1); |
103 |
|
} |
104 |
453323960 |
if (c != *p) { |
105 |
451524160 |
p = magic; |
106 |
451524160 |
continue; |
107 |
|
} |
108 |
1799800 |
p++; |
109 |
1799800 |
if (*p == '\0') |
110 |
23360 |
break; |
111 |
|
} |
112 |
|
|
113 |
23360 |
vim->json = VSB_new_auto(); |
114 |
23360 |
AN(vim->json); |
115 |
|
|
116 |
431395800 |
while (1) { |
117 |
431395800 |
c = getc(f); |
118 |
431395800 |
if (c == EOF) { |
119 |
40 |
AZ(fclose(f)); |
120 |
40 |
vim->err = "Truncated VMOD JSON"; |
121 |
40 |
VSB_destroy(&vim->json); |
122 |
40 |
return (-1); |
123 |
|
} |
124 |
431395760 |
if (c == '\x03') |
125 |
23320 |
break; |
126 |
431372440 |
VSB_putc(vim->json, c); |
127 |
|
} |
128 |
23320 |
AZ(fclose(f)); |
129 |
23320 |
AZ(VSB_finish(vim->json)); |
130 |
23320 |
return (0); |
131 |
23440 |
} |
132 |
|
|
133 |
|
static const char * |
134 |
23320 |
vcc_ParseJSON(const struct vcc *tl, const char *jsn, struct vmod_import *vim) |
135 |
|
{ |
136 |
|
const struct vjsn_val *vv, *vv2, *vv3; |
137 |
|
const char *err; |
138 |
|
char *p; |
139 |
|
|
140 |
23320 |
vim->vj = vjsn_parse(jsn, &err); |
141 |
23320 |
if (err != NULL) |
142 |
40 |
return (err); |
143 |
23280 |
AN(vim->vj); |
144 |
|
|
145 |
23280 |
vv = vim->vj->value; |
146 |
23280 |
if (!vjsn_is_array(vv)) |
147 |
40 |
return ("Not array[0]"); |
148 |
|
|
149 |
23240 |
vv2 = VTAILQ_FIRST(&vv->children); |
150 |
23240 |
AN(vv2); |
151 |
23240 |
if (!vjsn_is_array(vv2)) |
152 |
40 |
return ("Not array[1]"); |
153 |
23200 |
vv3 = VTAILQ_FIRST(&vv2->children); |
154 |
23200 |
AN(vv3); |
155 |
23200 |
if (!vjsn_is_string(vv3)) |
156 |
40 |
return ("Not string[2]"); |
157 |
23160 |
if (strcmp(vv3->value, "$VMOD")) |
158 |
40 |
return ("Not $VMOD[3]"); |
159 |
|
|
160 |
23120 |
vv3 = VTAILQ_NEXT(vv3, list); |
161 |
23120 |
AN(vv3); |
162 |
23120 |
assert(vjsn_is_string(vv3)); |
163 |
23120 |
vim->vmod_syntax = strtod(vv3->value, NULL); |
164 |
23120 |
assert (vim->vmod_syntax == 1.0); |
165 |
|
|
166 |
23120 |
vv3 = VTAILQ_NEXT(vv3, list); |
167 |
23120 |
AN(vv3); |
168 |
23120 |
assert(vjsn_is_string(vv3)); |
169 |
23120 |
vim->name = vv3->value; |
170 |
|
|
171 |
23120 |
vv3 = VTAILQ_NEXT(vv3, list); |
172 |
23120 |
AN(vv3); |
173 |
23120 |
assert(vjsn_is_string(vv3)); |
174 |
23120 |
vim->func_name = vv3->value; |
175 |
|
|
176 |
23120 |
vv3 = VTAILQ_NEXT(vv3, list); |
177 |
23120 |
AN(vv3); |
178 |
23120 |
assert(vjsn_is_string(vv3)); |
179 |
23120 |
vim->file_id = vv3->value; |
180 |
|
|
181 |
23120 |
vv3 = VTAILQ_NEXT(vv3, list); |
182 |
23120 |
AN(vv3); |
183 |
23120 |
assert(vjsn_is_string(vv3)); |
184 |
23120 |
vim->abi = vv3->value; |
185 |
|
|
186 |
23120 |
vv3 = VTAILQ_NEXT(vv3, list); |
187 |
23120 |
AN(vv3); |
188 |
23120 |
assert(vjsn_is_string(vv3)); |
189 |
23120 |
vim->major = strtoul(vv3->value, &p, 10); |
190 |
23120 |
assert(p == NULL || *p == '\0' || *p == 'U'); |
191 |
|
|
192 |
23120 |
vv3 = VTAILQ_NEXT(vv3, list); |
193 |
23120 |
AN(vv3); |
194 |
23120 |
assert(vjsn_is_string(vv3)); |
195 |
23120 |
vim->minor = strtoul(vv3->value, &p, 10); |
196 |
23120 |
assert(p == NULL || *p == '\0' || *p == 'U'); |
197 |
|
|
198 |
|
|
199 |
23120 |
if (vim->major == 0 && vim->minor == 0 && |
200 |
17800 |
strcmp(vim->abi, VMOD_ABI_Version)) { |
201 |
40 |
VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(vim->t_mod)); |
202 |
40 |
VSB_printf(tl->sb, "\tFile name: %s\n", vim->path); |
203 |
80 |
VSB_printf(tl->sb, "\tABI mismatch, expected <%s>, got <%s>\n", |
204 |
40 |
VMOD_ABI_Version, vim->abi); |
205 |
40 |
return (""); |
206 |
|
} |
207 |
28360 |
if (vim->major != 0 && |
208 |
5320 |
(vim->major != VRT_MAJOR_VERSION || |
209 |
5280 |
vim->minor > VRT_MINOR_VERSION)) { |
210 |
40 |
VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(vim->t_mod)); |
211 |
40 |
VSB_printf(tl->sb, "\tFile name: %s\n", vim->path); |
212 |
80 |
VSB_printf(tl->sb, "\tVMOD wants ABI version %u.%u\n", |
213 |
40 |
vim->major, vim->minor); |
214 |
40 |
VSB_printf(tl->sb, "\tvarnishd provides ABI version %u.%u\n", |
215 |
|
VRT_MAJOR_VERSION, VRT_MINOR_VERSION); |
216 |
40 |
return (""); |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
853240 |
VTAILQ_FOREACH(vv2, &vv->children, list) { |
221 |
830240 |
assert (vjsn_is_array(vv2)); |
222 |
830240 |
vv3 = VTAILQ_FIRST(&vv2->children); |
223 |
830240 |
assert(vjsn_is_string(vv3)); |
224 |
830240 |
assert(vv3->value[0] == '$'); |
225 |
|
#define STANZA(UU, ll, ss) \ |
226 |
|
if (!strcmp(vv3->value, "$" #UU)) {vim->n_##ll++; continue;} |
227 |
830240 |
STANZA_TBL |
228 |
|
#undef STANZA |
229 |
40 |
return ("Unknown metadata stanza."); |
230 |
|
} |
231 |
23000 |
if (vim->n_cproto != 1) |
232 |
40 |
return ("Bad cproto stanza(s)"); |
233 |
22960 |
if (vim->n_vmod != 1) |
234 |
40 |
return ("Bad vmod stanza(s)"); |
235 |
22920 |
return (NULL); |
236 |
23320 |
} |
237 |
|
|
238 |
|
/* |
239 |
|
* Load and check the metadata from the objectfile containing the vmod |
240 |
|
*/ |
241 |
|
|
242 |
|
static int |
243 |
23320 |
vcc_VmodLoad(struct vcc *tl, struct vmod_import *vim) |
244 |
|
{ |
245 |
|
static const char *err; |
246 |
|
struct vmod_import *vim2; |
247 |
|
|
248 |
23320 |
CHECK_OBJ_NOTNULL(vim, VMOD_IMPORT_MAGIC); |
249 |
|
|
250 |
23320 |
err = vcc_ParseJSON(tl, VSB_data(vim->json), vim); |
251 |
23320 |
if (err != NULL && *err != '\0') { |
252 |
640 |
VSB_printf(tl->sb, |
253 |
320 |
"VMOD %.*s: bad metadata\n", PF(vim->t_mod)); |
254 |
320 |
VSB_printf(tl->sb, "\t(%s)\n", err); |
255 |
320 |
VSB_printf(tl->sb, "\tFile name: %s\n", vim->path); |
256 |
320 |
} |
257 |
|
|
258 |
23320 |
if (err != NULL) |
259 |
400 |
return (-1); |
260 |
|
|
261 |
28680 |
VTAILQ_FOREACH(vim2, &imports, list) { |
262 |
5920 |
if (strcmp(vim->name, vim2->name)) |
263 |
5760 |
continue; |
264 |
160 |
if (!strcmp(vim->file_id, vim2->file_id)) { |
265 |
|
// (Truly) duplicate imports are OK |
266 |
120 |
return (0); |
267 |
|
} |
268 |
80 |
VSB_printf(tl->sb, |
269 |
|
"Different version of VMOD %.*s already loaded\n", |
270 |
40 |
PF(vim->t_mod)); |
271 |
40 |
vcc_ErrWhere(tl, vim->t_mod); |
272 |
40 |
VSB_cat(tl->sb, "Previous import at:\n"); |
273 |
40 |
vcc_ErrWhere(tl, vim2->t_mod); |
274 |
40 |
vcc_Warn(tl); |
275 |
40 |
break; |
276 |
|
} |
277 |
22800 |
VTAILQ_INSERT_TAIL(&imports, vim, list); |
278 |
|
|
279 |
22800 |
return (0); |
280 |
23320 |
} |
281 |
|
|
282 |
|
static void v_matchproto_(vcc_do_stanza_f) |
283 |
4880 |
vcc_do_event(struct vcc *tl, const struct vmod_import *vim, |
284 |
|
const struct vjsn_val *vv) |
285 |
|
{ |
286 |
|
struct inifin *ifp; |
287 |
|
|
288 |
4880 |
ifp = New_IniFin(tl); |
289 |
9760 |
VSB_printf(ifp->ini, |
290 |
|
"\tif (%s(ctx, &vmod_priv_%s, VCL_EVENT_LOAD))\n" |
291 |
|
"\t\treturn(1);", |
292 |
4880 |
vv->value, vim->sym->vmod_name); |
293 |
9760 |
VSB_printf(ifp->fin, |
294 |
|
"\t\t(void)%s(ctx, &vmod_priv_%s,\n" |
295 |
|
"\t\t\t VCL_EVENT_DISCARD);", |
296 |
4880 |
vv->value, vim->sym->vmod_name); |
297 |
9760 |
VSB_printf(ifp->event, "%s(ctx, &vmod_priv_%s, ev)", |
298 |
4880 |
vv->value, vim->sym->vmod_name); |
299 |
4880 |
} |
300 |
|
|
301 |
|
static void v_matchproto_(vcc_do_stanza_f) |
302 |
22640 |
vcc_do_cproto(struct vcc *tl, const struct vmod_import *vim, |
303 |
|
const struct vjsn_val *vv) |
304 |
|
{ |
305 |
22640 |
(void)vim; |
306 |
22640 |
do { |
307 |
4296960 |
assert (vjsn_is_string(vv)); |
308 |
4296960 |
Fh(tl, 0, "%s\n", vv->value); |
309 |
4296960 |
vv = VTAILQ_NEXT(vv, list); |
310 |
4296960 |
} while(vv != NULL); |
311 |
22640 |
} |
312 |
|
|
313 |
|
static void |
314 |
45280 |
vcc_vj_foreach(struct vcc *tl, const struct vmod_import *vim, |
315 |
|
const char *stanza, vcc_do_stanza_f *func) |
316 |
|
{ |
317 |
|
const struct vjsn_val *vv, *vv2, *vv3; |
318 |
|
|
319 |
45280 |
vv = vim->vj->value; |
320 |
45280 |
assert (vjsn_is_array(vv)); |
321 |
1674560 |
VTAILQ_FOREACH(vv2, &vv->children, list) { |
322 |
1629280 |
assert (vjsn_is_array(vv2)); |
323 |
1629280 |
vv3 = VTAILQ_FIRST(&vv2->children); |
324 |
1629280 |
assert (vjsn_is_string(vv3)); |
325 |
1629280 |
if (!strcmp(vv3->value, stanza)) |
326 |
27520 |
func(tl, vim, VTAILQ_NEXT(vv3, list)); |
327 |
1629280 |
} |
328 |
45280 |
} |
329 |
|
|
330 |
|
static void |
331 |
22640 |
vcc_emit_setup(struct vcc *tl, const struct vmod_import *vim) |
332 |
|
{ |
333 |
|
struct inifin *ifp; |
334 |
22640 |
const struct token *mod = vim->t_mod; |
335 |
|
|
336 |
22640 |
ifp = New_IniFin(tl); |
337 |
|
|
338 |
22640 |
VSB_cat(ifp->ini, "\tif (VPI_Vmod_Init(ctx,\n"); |
339 |
22640 |
VSB_printf(ifp->ini, "\t &VGC_vmod_%.*s,\n", PF(mod)); |
340 |
22640 |
VSB_printf(ifp->ini, "\t %u,\n", tl->vmod_count++); |
341 |
22640 |
VSB_printf(ifp->ini, "\t &%s,\n", vim->func_name); |
342 |
22640 |
VSB_printf(ifp->ini, "\t sizeof(%s),\n", vim->func_name); |
343 |
22640 |
VSB_printf(ifp->ini, "\t \"%.*s\",\n", PF(mod)); |
344 |
22640 |
VSB_cat(ifp->ini, "\t "); |
345 |
22640 |
VSB_quote(ifp->ini, vim->path, -1, VSB_QUOTE_CSTR); |
346 |
22640 |
VSB_cat(ifp->ini, ",\n"); |
347 |
22640 |
AN(vim->file_id); |
348 |
22640 |
VSB_printf(ifp->ini, "\t \"%s\",\n", vim->file_id); |
349 |
22640 |
if (vim->from_vext) { |
350 |
40 |
VSB_cat(ifp->ini, "\t "); |
351 |
40 |
VSB_quote(ifp->ini, vim->path, -1, VSB_QUOTE_CSTR); |
352 |
40 |
VSB_cat(ifp->ini, "\n"); |
353 |
40 |
} else { |
354 |
45200 |
VSB_printf(ifp->ini, "\t \"./vmod_cache/_vmod_%.*s.%s\"\n", |
355 |
22600 |
PF(mod), vim->file_id); |
356 |
|
} |
357 |
22640 |
VSB_cat(ifp->ini, "\t ))\n"); |
358 |
22640 |
VSB_cat(ifp->ini, "\t\treturn(1);"); |
359 |
|
|
360 |
22640 |
VSB_cat(tl->symtab, ",\n {\n"); |
361 |
22640 |
VSB_cat(tl->symtab, "\t\"dir\": \"import\",\n"); |
362 |
22640 |
VSB_cat(tl->symtab, "\t\"type\": \"$VMOD\",\n"); |
363 |
22640 |
VSB_printf(tl->symtab, "\t\"name\": \"%.*s\",\n", PF(mod)); |
364 |
22640 |
if (vim->from_vext) |
365 |
40 |
VSB_cat(tl->symtab, "\t\"vext\": true,\n"); |
366 |
|
else |
367 |
22600 |
VSB_cat(tl->symtab, "\t\"vext\": false,\n"); |
368 |
22640 |
VSB_printf(tl->symtab, "\t\"file\": \"%s\",\n", vim->path); |
369 |
45280 |
VSB_printf(tl->symtab, "\t\"dst\": \"./vmod_cache/_vmod_%.*s.%s\"\n", |
370 |
22640 |
PF(mod), vim->file_id); |
371 |
22640 |
VSB_cat(tl->symtab, " }"); |
372 |
|
|
373 |
|
/* XXX: zero the function pointer structure ?*/ |
374 |
45280 |
VSB_printf(ifp->fin, "\t\tVRT_priv_fini(ctx, &vmod_priv_%.*s);", |
375 |
22640 |
PF(mod)); |
376 |
45280 |
VSB_printf(ifp->final, "\t\tVPI_Vmod_Unload(ctx, &VGC_vmod_%.*s);", |
377 |
22640 |
PF(mod)); |
378 |
|
|
379 |
22640 |
vcc_vj_foreach(tl, vim, "$EVENT", vcc_do_event); |
380 |
|
|
381 |
22640 |
Fh(tl, 0, "\n/* --- BEGIN VMOD %.*s --- */\n\n", PF(mod)); |
382 |
22640 |
Fh(tl, 0, "static struct vmod *VGC_vmod_%.*s;\n", PF(mod)); |
383 |
22640 |
Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod)); |
384 |
|
|
385 |
22640 |
vcc_vj_foreach(tl, vim, "$CPROTO", vcc_do_cproto); |
386 |
|
|
387 |
22640 |
Fh(tl, 0, "\n/* --- END VMOD %.*s --- */\n\n", PF(mod)); |
388 |
22640 |
} |
389 |
|
|
390 |
|
static void |
391 |
800 |
vcc_vim_destroy(struct vmod_import **vimp) |
392 |
|
{ |
393 |
|
struct vmod_import *vim; |
394 |
|
|
395 |
800 |
TAKE_OBJ_NOTNULL(vim, vimp, VMOD_IMPORT_MAGIC); |
396 |
800 |
if (vim->path) |
397 |
760 |
free(vim->path); |
398 |
800 |
if (vim->vj) |
399 |
600 |
vjsn_delete(&vim->vj); |
400 |
800 |
if (vim->json) |
401 |
640 |
VSB_destroy(&vim->json); |
402 |
800 |
FREE_OBJ(vim); |
403 |
800 |
} |
404 |
|
|
405 |
|
static int |
406 |
23400 |
vcc_path_open(void *priv, const char *fn) |
407 |
|
{ |
408 |
|
struct vmod_import *vim; |
409 |
|
|
410 |
23400 |
CAST_OBJ_NOTNULL(vim, priv, VMOD_IMPORT_MAGIC); |
411 |
23400 |
AN(fn); |
412 |
|
|
413 |
23400 |
return (vcc_Extract_JSON(vim, fn)); |
414 |
|
} |
415 |
|
|
416 |
|
void |
417 |
23600 |
vcc_ParseImport(struct vcc *tl) |
418 |
|
{ |
419 |
|
char fn[1024]; |
420 |
|
const char *p; |
421 |
|
struct token *mod, *tmod, *t1; |
422 |
|
struct symbol *msym, *vsym; |
423 |
23600 |
struct vmod_import *vim = NULL; |
424 |
|
const struct vmod_import *vimold; |
425 |
|
|
426 |
23600 |
t1 = tl->t; |
427 |
23600 |
SkipToken(tl, ID); /* "import" */ |
428 |
|
|
429 |
23600 |
ExpectErr(tl, ID); /* "vmod_name" */ |
430 |
23600 |
mod = tl->t; |
431 |
23600 |
tmod = vcc_PeekTokenFrom(tl, mod); |
432 |
23600 |
AN(tmod); |
433 |
23600 |
if (tmod->tok == ID && vcc_IdIs(tmod, "as")) { |
434 |
200 |
vcc_NextToken(tl); /* "vmod_name" */ |
435 |
200 |
vcc_NextToken(tl); /* "as" */ |
436 |
200 |
ExpectErr(tl, ID); /* "vcl_name" */ |
437 |
200 |
} |
438 |
23600 |
tmod = tl->t; |
439 |
|
|
440 |
23600 |
msym = VCC_SymbolGet(tl, SYM_MAIN, SYM_VMOD, SYMTAB_CREATE, XREF_NONE); |
441 |
23600 |
ERRCHK(tl); |
442 |
23560 |
AN(msym); |
443 |
|
|
444 |
23560 |
bprintf(fn, "libvmod_%.*s.so", PF(mod)); |
445 |
23560 |
if (tl->t->tok == ID) { |
446 |
200 |
if (!vcc_IdIs(tl->t, "from")) { |
447 |
40 |
VSB_cat(tl->sb, "Expected 'from path ...'\n"); |
448 |
40 |
vcc_ErrWhere(tl, tl->t); |
449 |
40 |
return; |
450 |
|
} |
451 |
160 |
vcc_NextToken(tl); |
452 |
160 |
if (!tl->unsafe_path && strchr(tl->t->dec, '/')) { |
453 |
40 |
VSB_cat(tl->sb, |
454 |
|
"'import ... from path ...' is unsafe.\nAt:"); |
455 |
40 |
vcc_ErrToken(tl, tl->t); |
456 |
40 |
vcc_ErrWhere(tl, tl->t); |
457 |
40 |
return; |
458 |
|
} |
459 |
120 |
ExpectErr(tl, CSTR); |
460 |
120 |
p = strrchr(tl->t->dec, '/'); |
461 |
120 |
if (p != NULL && p[1] == '\0') |
462 |
40 |
bprintf(fn, "%slibvmod_%.*s.so", tl->t->dec, PF(mod)); |
463 |
|
else |
464 |
80 |
bprintf(fn, "%s", tl->t->dec); |
465 |
120 |
vcc_NextToken(tl); |
466 |
120 |
} else { |
467 |
29360 |
VTAILQ_FOREACH(vim, &imports, list) { |
468 |
6040 |
if (!vcc_IdIs(mod, vim->name)) |
469 |
5880 |
continue; |
470 |
160 |
if (!vim->unimported_vext) |
471 |
120 |
continue; |
472 |
40 |
fprintf(stderr, "IMPORT %s from VEXT\n", vim->name); |
473 |
40 |
vim->unimported_vext = 0; |
474 |
40 |
vim->t_mod = mod; |
475 |
40 |
vim->sym = msym; |
476 |
40 |
break; |
477 |
|
} |
478 |
|
} |
479 |
|
|
480 |
23480 |
SkipToken(tl, ';'); |
481 |
|
|
482 |
23480 |
if (vim == NULL) { |
483 |
23440 |
ALLOC_OBJ(vim, VMOD_IMPORT_MAGIC); |
484 |
23440 |
AN(vim); |
485 |
23440 |
vim->t_mod = mod; |
486 |
23440 |
vim->sym = msym; |
487 |
|
|
488 |
23440 |
if (VFIL_searchpath(tl->vmod_path, vcc_path_open, vim, fn, &vim->path)) { |
489 |
160 |
if (vim->err == NULL) { |
490 |
80 |
VSB_printf(tl->sb, |
491 |
40 |
"Could not find VMOD %.*s\n", PF(mod)); |
492 |
40 |
} else { |
493 |
240 |
VSB_printf(tl->sb, |
494 |
120 |
"Could not open VMOD %.*s\n", PF(mod)); |
495 |
240 |
VSB_printf(tl->sb, "\tFile name: %s\n", |
496 |
120 |
vim->path != NULL ? vim->path : fn); |
497 |
120 |
VSB_printf(tl->sb, "\tError: %s\n", vim->err); |
498 |
|
} |
499 |
160 |
vcc_ErrWhere(tl, mod); |
500 |
160 |
vcc_vim_destroy(&vim); |
501 |
160 |
return; |
502 |
|
} |
503 |
|
|
504 |
23280 |
if (vcc_VmodLoad(tl, vim) < 0 || tl->err) { |
505 |
400 |
vcc_ErrWhere(tl, vim->t_mod); |
506 |
400 |
vcc_vim_destroy(&vim); |
507 |
400 |
return; |
508 |
|
} |
509 |
22880 |
} |
510 |
|
|
511 |
22920 |
if (!vcc_IdIs(vim->t_mod, vim->name)) { |
512 |
80 |
vcc_ErrWhere(tl, vim->t_mod); |
513 |
160 |
VSB_printf(tl->sb, "Wrong file for VMOD %.*s\n", |
514 |
80 |
PF(vim->t_mod)); |
515 |
80 |
VSB_printf(tl->sb, "\tFile name: %s\n", vim->path); |
516 |
80 |
VSB_printf(tl->sb, "\tContains vmod \"%s\"\n", vim->name); |
517 |
80 |
vcc_vim_destroy(&vim); |
518 |
80 |
return; |
519 |
|
} |
520 |
|
|
521 |
22840 |
vimold = msym->import; |
522 |
22840 |
if (vimold != NULL) { |
523 |
120 |
CHECK_OBJ(vimold, VMOD_IMPORT_MAGIC); |
524 |
120 |
if (!strcmp(vimold->file_id, vim->file_id)) { |
525 |
|
/* Identical import is OK */ |
526 |
80 |
} else { |
527 |
80 |
VSB_printf(tl->sb, |
528 |
|
"Another module already imported as %.*s.\n", |
529 |
40 |
PF(tmod)); |
530 |
40 |
vcc_ErrWhere2(tl, t1, tl->t); |
531 |
|
} |
532 |
120 |
vcc_vim_destroy(&vim); |
533 |
120 |
return; |
534 |
|
} |
535 |
22720 |
msym->def_b = t1; |
536 |
22720 |
msym->def_e = tl->t; |
537 |
|
|
538 |
28400 |
VTAILQ_FOREACH(vsym, &tl->sym_vmods, sideways) { |
539 |
5720 |
assert(vsym->kind == SYM_VMOD); |
540 |
5720 |
vimold = vsym->import; |
541 |
5720 |
CHECK_OBJ_NOTNULL(vimold, VMOD_IMPORT_MAGIC); |
542 |
5720 |
if (!strcmp(vimold->file_id, vim->file_id)) { |
543 |
|
/* Already loaded under different name */ |
544 |
40 |
msym->eval_priv = vsym->eval_priv; |
545 |
40 |
msym->import = vsym->import; |
546 |
40 |
msym->vmod_name = vsym->vmod_name; |
547 |
40 |
vcc_VmodSymbols(tl, msym); |
548 |
40 |
AZ(tl->err); |
549 |
|
// XXX: insert msym in sideways ? |
550 |
40 |
vcc_vim_destroy(&vim); |
551 |
40 |
return; |
552 |
|
} |
553 |
5680 |
} |
554 |
|
|
555 |
22680 |
VTAILQ_INSERT_TAIL(&tl->sym_vmods, msym, sideways); |
556 |
|
|
557 |
22680 |
msym->eval_priv = vim->vj; |
558 |
22680 |
msym->import = vim; |
559 |
22680 |
msym->vmod_name = TlDup(tl, vim->name); |
560 |
22680 |
vcc_VmodSymbols(tl, msym); |
561 |
22680 |
ERRCHK(tl); |
562 |
|
|
563 |
22640 |
vcc_emit_setup(tl, vim); |
564 |
23600 |
} |
565 |
|
|
566 |
|
void |
567 |
40 |
vcc_ImportVext(struct vcc *tl, const char *filename) |
568 |
|
{ |
569 |
|
struct vmod_import *vim; |
570 |
|
|
571 |
40 |
ALLOC_OBJ(vim, VMOD_IMPORT_MAGIC); |
572 |
40 |
AN(vim); |
573 |
|
|
574 |
40 |
if (vcc_Extract_JSON(vim, filename)) { |
575 |
0 |
FREE_OBJ(vim); |
576 |
0 |
return; |
577 |
|
} |
578 |
40 |
fprintf(stderr, "FOUND VMOD in VEXT %s\n", filename); |
579 |
40 |
if (vcc_VmodLoad(tl, vim) < 0 || tl->err) { |
580 |
|
// vcc_ErrWhere(tl, vim->t_mod); |
581 |
0 |
vcc_vim_destroy(&vim); |
582 |
0 |
return; |
583 |
|
} |
584 |
40 |
vim->from_vext = 1; |
585 |
40 |
vim->unimported_vext = 1; |
586 |
40 |
vim->path = strdup(filename); |
587 |
40 |
vim->path += 1; |
588 |
40 |
AN(vim->path); |
589 |
40 |
fprintf(stderr, "GOOD VMOD %s in VEXT %s\n", vim->name, filename); |
590 |
40 |
} |