Code Coverage: vinyl-cache/bin/vinyld/cache/cache_vrt_vmod.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * Runtime support for compiled VCL programs
31
 */
32
33
#include "config.h"
34
35
#include "cache_int.h"
36
37
#include <dlfcn.h>
38
#include <stdio.h>
39
#include <stdlib.h>
40
41
#include "vcli_serve.h"
42
#include "vcc_interface.h"
43
#include "vmod_abi.h"
44
45
/*--------------------------------------------------------------------
46
 * Modules stuff
47
 */
48
49
struct vmod {
50
        unsigned                magic;
51
#define VMOD_MAGIC              0xb750219c
52
53
        VTAILQ_ENTRY(vmod)      list;
54
55
        int                     ref;
56
57
        char                    *nm;
58
        unsigned                nbr;
59
        char                    *path;
60
        char                    *backup;
61
        void                    *hdl;
62
        const void              *funcs;
63
        size_t                  funclen;
64
        const char              *abi;
65
        unsigned                vrt_major;
66
        unsigned                vrt_minor;
67
        const char              *vcs;
68
        const char              *version;
69
};
70
71
static VTAILQ_HEAD(,vmod)       vmods = VTAILQ_HEAD_INITIALIZER(vmods);
72
73
static unsigned
74
vmod_abi_mismatch(const struct vmod_data *d)
75 422
{
76
77 422
        if (d->vrt_major == 0 && d->vrt_minor == 0)
78 339
                return (d->abi == NULL || vstrcmp(d->abi, VMOD_ABI_Version));
79
80 83
        return (d->vrt_major != VRT_MAJOR_VERSION ||
81 83
            d->vrt_minor > VRT_MINOR_VERSION);
82 422
}
83
84
int
85
VPI_Vmod_Init(VRT_CTX, struct vmod **hdl, unsigned nbr, void *ptr, size_t len,
86
    const char *nm, const char *path, const char *file_id, const char *backup)
87 554
{
88 554
        struct vmod *v;
89 554
        const struct vmod_data *d;
90 554
        char buf[256];
91 554
        void *dlhdl;
92
93 554
        ASSERT_CLI();
94 554
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
95 554
        AN(ctx->msg);
96 554
        AN(hdl);
97 554
        AZ(*hdl);
98
99 554
        dlhdl = dlopen(backup, RTLD_NOW | RTLD_LOCAL);
100 554
        if (dlhdl == NULL) {
101 0
                VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
102 0
                    nm, backup, path);
103 0
                VSB_printf(ctx->msg, "dlopen() failed: %s\n", dlerror());
104 0
                return (1);
105 0
        }
106
107 554
        VTAILQ_FOREACH(v, &vmods, list)
108 295
                if (v->hdl == dlhdl)
109 132
                        break;
110 554
        if (v == NULL) {
111 422
                ALLOC_OBJ(v, VMOD_MAGIC);
112 422
                AN(v);
113
114 422
                v->hdl = dlhdl;
115
116 422
                bprintf(buf, "Vmod_%s_Data", nm);
117 422
                d = dlsym(v->hdl, buf);
118 422
                if (d == NULL ||
119 422
                    d->file_id == NULL ||
120 422
                    vstrcmp(d->file_id, file_id)) {
121 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
122 0
                            nm, backup, path);
123 0
                        VSB_cat(ctx->msg,
124 0
                            "This is no longer the same file seen by"
125 0
                            " the VCL-compiler.\n");
126 0
                        (void)dlclose(v->hdl);
127 0
                        FREE_OBJ(v);
128 0
                        return (1);
129 0
                }
130 422
                if (vmod_abi_mismatch(d) ||
131 422
                    d->name == NULL ||
132 422
                    vstrcmp(d->name, nm) ||
133 422
                    d->func == NULL ||
134 422
                    d->func_len <= 0 ||
135 422
                    d->proto != NULL ||
136 422
                    d->json == NULL) {
137 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
138 0
                            nm, backup, path);
139 0
                        VSB_cat(ctx->msg, "VMOD data is mangled.\n");
140 0
                        (void)dlclose(v->hdl);
141 0
                        FREE_OBJ(v);
142 0
                        return (1);
143 0
                }
144
145 422
                v->nbr = nbr;
146 422
                v->funclen = d->func_len;
147 422
                v->funcs = d->func;
148 422
                v->abi = d->abi;
149 422
                v->vrt_major = d->vrt_major;
150 422
                v->vrt_minor = d->vrt_minor;
151 422
                v->vcs = d->vcs;
152 422
                v->version = d->version;
153
154 422
                REPLACE(v->nm, nm);
155 422
                REPLACE(v->path, path);
156 422
                REPLACE(v->backup, backup);
157
158 422
                VSC_C_main->vmods++;
159 422
                VTAILQ_INSERT_TAIL(&vmods, v, list);
160 422
        }
161
162 554
        assert(len == v->funclen);
163 554
        vmemcpy(ptr, v->funcs, v->funclen);
164 554
        v->ref++;
165
166 554
        *hdl = v;
167 554
        return (0);
168 554
}
169
170
void
171
VPI_Vmod_Unload(VRT_CTX, struct vmod **hdl)
172 80
{
173 80
        struct vmod *v;
174
175 80
        ASSERT_CLI();
176
177 80
        TAKE_OBJ_NOTNULL(v, hdl, VMOD_MAGIC);
178
179 80
        VCL_TaskLeave(ctx, cli_task_privs);
180 80
        VCL_TaskEnter(cli_task_privs);
181
182 80
#ifndef DONT_DLCLOSE_VMODS
183
        /*
184
         * atexit(3) handlers are not called during dlclose(3).  We don't
185
         * normally use them, but we do when running GCOV.  This option
186
         * enables us to do that.
187
         */
188 80
        AZ(dlclose(v->hdl));
189 80
#endif
190 80
        if (--v->ref != 0)
191 50
                return;
192 30
        free(v->nm);
193 30
        free(v->path);
194 30
        free(v->backup);
195 30
        VTAILQ_REMOVE(&vmods, v, list);
196 30
        VSC_C_main->vmods--;
197 30
        FREE_OBJ(v);
198 30
}
199
200 22
#define nvl(x, y) ((x) ? (x) : (y))
201
202
void
203
VMOD_Panic(struct vsb *vsb)
204 11
{
205 11
        struct vmod *v;
206
207 11
        VSB_cat(vsb, "vmods = {\n");
208 11
        VSB_indent(vsb, 2);
209 11
        VTAILQ_FOREACH(v, &vmods, list) {
210 11
                VSB_printf(vsb, "%s = {", v->nm);
211 11
                VSB_indent(vsb, 2);
212 11
                VSB_printf(vsb, "p=%p, abi=\"%s\", vrt=%u.%u,\n",
213 11
                           v, v->abi, v->vrt_major, v->vrt_minor);
214 11
                VSB_bcat(vsb, "vcs=", 4);
215 11
                VSB_quote(vsb, nvl(v->vcs, ""), -1, VSB_QUOTE_CSTR);
216 11
                VSB_bcat(vsb, ", version=", 10);
217 11
                VSB_quote(vsb, nvl(v->version, ""), -1, VSB_QUOTE_CSTR);
218 11
                VSB_indent(vsb, -2);
219 11
                VSB_bcat(vsb, "},\n", 3);
220 11
        }
221
222 11
        VSB_indent(vsb, -2);
223 11
        VSB_cat(vsb, "},\n");
224 11
}
225
226
/*---------------------------------------------------------------------*/
227
228
static void v_matchproto_(cli_func_t)
229
ccf_debug_vmod(struct cli *cli, const char * const *av, void *priv)
230 4
{
231 4
        struct vmod *v;
232
233 4
        (void)av;
234 4
        (void)priv;
235 4
        ASSERT_CLI();
236 4
        VTAILQ_FOREACH(v, &vmods, list) {
237 4
                VCLI_Out(cli, "%5d %s (path=\"%s\", version=\"%s\","
238 4
                    " vcs=\"%s\")\n", v->ref, v->nm, v->path, v->version,
239 4
                    v->vcs);
240 4
        }
241 4
}
242
243
static struct cli_proto vcl_cmds[] = {
244
        { CLICMD_DEBUG_VMOD,                    "d", ccf_debug_vmod },
245
        { NULL }
246
};
247
248
void
249
VMOD_Init(void)
250 1024
{
251
252 1024
        CLI_AddFuncs(vcl_cmds);
253 1024
}