void vcc_ParseImport(struct vcc *tl) { void *hdl; char fn[1024]; char buf[256]; struct token *mod, *t1; const char *modname; const char *proto; const char *abi; const char **spec; struct symbol *sym; const struct symbol *osym; const char *p; // int *modlen; t1 = tl->t; SkipToken(tl, ID); /* "import" */ ExpectErr(tl, ID); mod = tl->t; vcc_NextToken(tl); osym = VCC_FindSymbol(tl, mod, SYM_NONE); if (osym != NULL && osym->kind != SYM_VMOD) { VSB_printf(tl->sb, "Module %.*s conflics with other symbol.\n", PF(mod)); vcc_ErrWhere2(tl, t1, tl->t); return; } if (osym != NULL) { VSB_printf(tl->sb, "Module %.*s already imported.\n", PF(mod)); vcc_ErrWhere2(tl, t1, tl->t); VSB_printf(tl->sb, "Previous import was here:\n"); vcc_ErrWhere2(tl, osym->def_b, osym->def_e); return; } bprintf(fn, "%.*s", PF(mod)); sym = VCC_AddSymbolStr(tl, fn, SYM_VMOD); ERRCHK(tl); AN(sym); sym->def_b = t1; sym->def_e = tl->t; if (tl->t->tok == ID) { if (!tl->unsafe_path) { VSB_printf(tl->sb, "'import ... from path...'" " not allowed.\nAt:"); vcc_ErrToken(tl, tl->t); vcc_ErrWhere(tl, tl->t); return; } if (!vcc_IdIs(tl->t, "from")) { VSB_printf(tl->sb, "Expected 'from path...'\n"); vcc_ErrWhere(tl, tl->t); return; } vcc_NextToken(tl); ExpectErr(tl, CSTR); bprintf(fn, "%s", tl->t->dec); vcc_NextToken(tl); } else { bprintf(fn, "%s/libvmod_%.*s.so", tl->vmod_dir, PF(mod)); } Fh(tl, 0, "static void *VGC_vmod_%.*s;\n", PF(mod)); Fi(tl, 0, "\tif (VRT_Vmod_Init(&VGC_vmod_%.*s,\n", PF(mod)); Fi(tl, 0, "\t &Vmod_%.*s_Func,\n", PF(mod)); Fi(tl, 0, "\t sizeof(Vmod_%.*s_Func),\n", PF(mod)); Fi(tl, 0, "\t \"%.*s\",\n", PF(mod)); Fi(tl, 0, "\t "); EncString(tl->fi, fn, NULL, 0); Fi(tl, 0, ",\n\t "); Fi(tl, 0, "cli))\n"); Fi(tl, 0, "\t\treturn(1);\n"); SkipToken(tl, ';'); hdl = dlopen(fn, RTLD_NOW | RTLD_LOCAL); if (hdl == NULL) { VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, dlerror()); vcc_ErrWhere(tl, mod); return; } bprintf(buf, "Vmod_%.*s_Name", PF(mod)); modname = dlsym(hdl, buf); if (modname == NULL) { VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, "Symbol Vmod_Name not found"); vcc_ErrWhere(tl, mod); return; } if (!vcc_IdIs(mod, modname)) { VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n", PF(mod), fn); VSB_printf(tl->sb, "\tModule has wrong name: <%s>\n", modname); vcc_ErrWhere(tl, mod); return; } bprintf(buf, "Vmod_%.*s_ABI", PF(mod)); abi = dlsym(hdl, buf); if (abi == NULL || strcmp(abi, VMOD_ABI_Version) != 0) { VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n", PF(mod), fn); VSB_printf(tl->sb, "\tABI mismatch, expected <%s>, got <%s>\n", VMOD_ABI_Version, abi); vcc_ErrWhere(tl, mod); return; } bprintf(buf, "Vmod_%.*s_Proto", PF(mod)); proto = dlsym(hdl, buf); if (proto == NULL) { VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, "Symbol Vmod_Proto not found"); vcc_ErrWhere(tl, mod); return; } bprintf(buf, "Vmod_%.*s_Spec", PF(mod)); spec = dlsym(hdl, buf); if (spec == NULL) { VSB_printf(tl->sb, "Could not load module %.*s\n\t%s\n\t%s\n", PF(mod), fn, "Symbol Vmod_Spec not found"); vcc_ErrWhere(tl, mod); return; } Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod)); for (; *spec != NULL; spec++) { p = *spec; if (!strcmp(p, "OBJ")) { p += strlen(p) + 1; sym = VCC_AddSymbolStr(tl, p, SYM_OBJECT); XXXAN(sym); sym->args = p; } else if (!strcmp(p, "INIT")) { p += strlen(p) + 1; Fi(tl, 0, "\t%s(&vmod_priv_%.*s, &VCL_conf);\n", p, PF(mod)); } else { sym = VCC_AddSymbolStr(tl, p, SYM_FUNC); ERRCHK(tl); AN(sym); sym->eval = vcc_Eval_SymFunc; p += strlen(p) + 1; sym->cfunc = p; p += strlen(p) + 1; sym->args = p; /* Functions which return VOID are procedures */ if (!memcmp(p, "VOID\0", 5)) sym->kind = SYM_PROC; } } Fh(tl, 0, "\n%s\n", proto); /* XXX: zero the function pointer structure ?*/ Ff(tl, 0, "\tvmod_priv_fini(&vmod_priv_%.*s);\n", PF(mod)); Ff(tl, 0, "\tVRT_Vmod_Fini(&VGC_vmod_%.*s);\n", PF(mod)); }
void vcc_ParseImport(struct vcc *tl) { void *hdl; char fn[1024], *fnp; char buf[256]; struct token *mod, *t1; struct inifin *ifp; const char * const *spec; struct symbol *sym; const struct symbol *osym; const char *p; // int *modlen; const struct vmod_data *vmd; t1 = tl->t; SkipToken(tl, ID); /* "import" */ ExpectErr(tl, ID); mod = tl->t; vcc_NextToken(tl); osym = VCC_FindSymbol(tl, mod, SYM_NONE); if (osym != NULL && osym->kind != SYM_VMOD) { VSB_printf(tl->sb, "Module %.*s conflicts with other symbol.\n", PF(mod)); vcc_ErrWhere2(tl, t1, tl->t); return; } if (osym != NULL) { VSB_printf(tl->sb, "Module %.*s already imported.\n", PF(mod)); vcc_ErrWhere2(tl, t1, tl->t); VSB_printf(tl->sb, "Previous import was here:\n"); vcc_ErrWhere2(tl, osym->def_b, osym->def_e); return; } bprintf(fn, "%.*s", PF(mod)); sym = VCC_AddSymbolStr(tl, fn, SYM_VMOD); ERRCHK(tl); AN(sym); sym->def_b = t1; sym->def_e = tl->t; if (tl->t->tok == ID) { if (!vcc_IdIs(tl->t, "from")) { VSB_printf(tl->sb, "Expected 'from path ...'\n"); vcc_ErrWhere(tl, tl->t); return; } vcc_NextToken(tl); if (!tl->param->unsafe_path && strchr(tl->t->dec, '/')) { VSB_printf(tl->sb, "'import ... from path ...' is unsafe.\nAt:"); vcc_ErrToken(tl, tl->t); vcc_ErrWhere(tl, tl->t); return; } ExpectErr(tl, CSTR); p = strrchr(tl->t->dec, '/'); if (p != NULL && p[1] == '\0') bprintf(fn, "%slibvmod_%.*s.so", tl->t->dec, PF(mod)); else bprintf(fn, "%s", tl->t->dec); vcc_NextToken(tl); } else { bprintf(fn, "libvmod_%.*s.so", PF(mod)); } SkipToken(tl, ';'); if (VFIL_searchpath(tl->param->vmod_path, vcc_path_dlopen, &hdl, fn, &fnp)) { VSB_printf(tl->sb, "Could not load VMOD %.*s\n", PF(mod)); VSB_printf(tl->sb, "\tFile name: %s\n", fnp != NULL ? fnp : fn); VSB_printf(tl->sb, "\tdlerror: %s\n", dlerror()); vcc_ErrWhere(tl, mod); return; } bprintf(buf, "Vmod_%.*s_Data", PF(mod)); vmd = dlsym(hdl, buf); if (vmd == NULL) { VSB_printf(tl->sb, "Malformed VMOD %.*s\n", PF(mod)); VSB_printf(tl->sb, "\tFile name: %s\n", fnp); VSB_printf(tl->sb, "\t(no Vmod_Data symbol)\n"); vcc_ErrWhere(tl, mod); return; } if (strcmp(VCS_Branch, "master") == 0 && strcmp(vmd->abi, VMOD_ABI_Version) != 0) { VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(mod)); VSB_printf(tl->sb, "\tFile name: %s\n", fnp); VSB_printf(tl->sb, "\tABI mismatch, expected <%s>, got <%s>\n", VMOD_ABI_Version, vmd->abi); vcc_ErrWhere(tl, mod); return; } if (vmd->vrt_major != VRT_MAJOR_VERSION || vmd->vrt_minor > VRT_MINOR_VERSION) { VSB_printf(tl->sb, "Incompatible VMOD %.*s\n", PF(mod)); VSB_printf(tl->sb, "\tFile name: %s\n", fnp); VSB_printf(tl->sb, "\tVMOD version %u.%u\n", vmd->vrt_major, vmd->vrt_minor); VSB_printf(tl->sb, "\tvarnishd version %u.%u\n", VRT_MAJOR_VERSION, VRT_MINOR_VERSION); vcc_ErrWhere(tl, mod); return; } if (vmd->name == NULL || vmd->func == NULL || vmd->func_len <= 0 || vmd->proto == NULL || vmd->abi == NULL) { VSB_printf(tl->sb, "Mangled VMOD %.*s\n", PF(mod)); VSB_printf(tl->sb, "\tFile name: %s\n", fnp); VSB_printf(tl->sb, "\tInconsistent metadata\n"); vcc_ErrWhere(tl, mod); return; } if (!vcc_IdIs(mod, vmd->name)) { VSB_printf(tl->sb, "Wrong VMOD file %.*s\n", PF(mod)); VSB_printf(tl->sb, "\tFile name: %s\n", fnp); VSB_printf(tl->sb, "\tContains vmod \"%s\"\n", vmd->name); vcc_ErrWhere(tl, mod); return; } ifp = New_IniFin(tl); VSB_printf(ifp->ini, "\tif (VRT_Vmod_Init(&VGC_vmod_%.*s,\n", PF(mod)); VSB_printf(ifp->ini, "\t &Vmod_%.*s_Func,\n", PF(mod)); VSB_printf(ifp->ini, "\t sizeof(Vmod_%.*s_Func),\n", PF(mod)); VSB_printf(ifp->ini, "\t \"%.*s\",\n", PF(mod)); VSB_printf(ifp->ini, "\t "); EncString(ifp->ini, fnp, NULL, 0); free(fnp); VSB_printf(ifp->ini, ",\n"); AN(vmd); AN(vmd->file_id); VSB_printf(ifp->ini, "\t \"%s\",\n", vmd->file_id); VSB_printf(ifp->ini, "\t ctx))\n"); VSB_printf(ifp->ini, "\t\treturn(1);"); /* XXX: zero the function pointer structure ?*/ VSB_printf(ifp->fin, "\t\tVRT_priv_fini(&vmod_priv_%.*s);", PF(mod)); VSB_printf(ifp->fin, "\n\t\tVRT_Vmod_Fini(&VGC_vmod_%.*s);", PF(mod)); ifp = NULL; spec = vmd->spec; for (; *spec != NULL; spec++) { p = *spec; if (!strcmp(p, "$OBJ")) { p += strlen(p) + 1; sym = VCC_AddSymbolStr(tl, p, SYM_OBJECT); XXXAN(sym); sym->args = p; } else if (!strcmp(p, "$EVENT")) { p += strlen(p) + 1; if (ifp == NULL) ifp = New_IniFin(tl); VSB_printf(ifp->ini, "\tif (%s(ctx, &vmod_priv_%.*s, VCL_EVENT_LOAD))\n" "\t\treturn(1);", p, PF(mod)); VSB_printf(ifp->fin, "\t\t(void)%s(ctx, &vmod_priv_%.*s,\n" "\t\t VCL_EVENT_DISCARD);\n", p, PF(mod)); VSB_printf(ifp->event, "\t(void)%s(ctx, &vmod_priv_%.*s, ev);\n", p, PF(mod)); } else { sym = VCC_AddSymbolStr(tl, p, SYM_FUNC); ERRCHK(tl); AN(sym); sym->eval = vcc_Eval_SymFunc; p += strlen(p) + 1; sym->cfunc = p; p += strlen(p) + 1; sym->args = p; /* Functions which return VOID are procedures */ if (!memcmp(p, "VOID\0", 5)) sym->kind = SYM_PROC; } } Fh(tl, 0, "\n/* --- BEGIN VMOD %.*s --- */\n\n", PF(mod)); Fh(tl, 0, "static struct vmod *VGC_vmod_%.*s;\n", PF(mod)); Fh(tl, 0, "static struct vmod_priv vmod_priv_%.*s;\n", PF(mod)); Fh(tl, 0, "\n%s\n", vmd->proto); Fh(tl, 0, "\n/* --- END VMOD %.*s --- */\n\n", PF(mod)); }