varnish-cache/bin/varnishd/cache/cache_vrt_vmod.c
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_varnishd.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
        int                     funclen;
64
        const char              *abi;
65
        unsigned                vrt_major;
66
        unsigned                vrt_minor;
67
};
68
69
static VTAILQ_HEAD(,vmod)       vmods = VTAILQ_HEAD_INITIALIZER(vmods);
70
71
static unsigned
72 8600
vmod_abi_mismatch(const struct vmod_data *d)
73
{
74
75 8600
        if (d->vrt_major == 0 && d->vrt_minor == 0)
76 6950
                return (d->abi == NULL || strcmp(d->abi, VMOD_ABI_Version));
77
78 1650
        return (d->vrt_major != VRT_MAJOR_VERSION ||
79 1650
            d->vrt_minor > VRT_MINOR_VERSION);
80 8600
}
81
82
int
83 11950
VPI_Vmod_Init(VRT_CTX, struct vmod **hdl, unsigned nbr, void *ptr, int len,
84
    const char *nm, const char *path, const char *file_id, const char *backup)
85
{
86
        struct vmod *v;
87
        const struct vmod_data *d;
88
        char buf[256];
89
        void *dlhdl;
90
91 11950
        ASSERT_CLI();
92 11950
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
93 11950
        AN(ctx->msg);
94 11950
        AN(hdl);
95 11950
        AZ(*hdl);
96
97 11950
        dlhdl = dlopen(backup, RTLD_NOW | RTLD_LOCAL);
98 11950
        if (dlhdl == NULL) {
99 0
                VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
100 0
                    nm, backup, path);
101 0
                VSB_printf(ctx->msg, "dlopen() failed: %s\n", dlerror());
102 0
                return (1);
103
        }
104
105 15525
        VTAILQ_FOREACH(v, &vmods, list)
106 6925
                if (v->hdl == dlhdl)
107 3350
                        break;
108 11950
        if (v == NULL) {
109 8600
                ALLOC_OBJ(v, VMOD_MAGIC);
110 8600
                AN(v);
111 8600
                REPLACE(v->backup, backup);
112
113 8600
                v->hdl = dlhdl;
114
115 8600
                bprintf(buf, "Vmod_%s_Data", nm);
116 8600
                d = dlsym(v->hdl, buf);
117 17200
                if (d == NULL ||
118 8600
                    d->file_id == NULL ||
119 8600
                    strcmp(d->file_id, file_id)) {
120 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
121 0
                            nm, backup, path);
122 0
                        VSB_cat(ctx->msg,
123
                            "This is no longer the same file seen by"
124
                            " the VCL-compiler.\n");
125 0
                        (void)dlclose(v->hdl);
126 0
                        FREE_OBJ(v);
127 0
                        return (1);
128
                }
129 17200
                if (vmod_abi_mismatch(d) ||
130 8600
                    d->name == NULL ||
131 8600
                    strcmp(d->name, nm) ||
132 8600
                    d->func == NULL ||
133 8600
                    d->func_len <= 0 ||
134 8600
                    d->proto != NULL ||
135 8600
                    d->json == NULL) {
136 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
137 0
                            nm, backup, path);
138 0
                        VSB_cat(ctx->msg, "VMOD data is mangled.\n");
139 0
                        (void)dlclose(v->hdl);
140 0
                        FREE_OBJ(v);
141 0
                        return (1);
142
                }
143
144 8600
                v->nbr = nbr;
145 8600
                v->funclen = d->func_len;
146 8600
                v->funcs = d->func;
147 8600
                v->abi = d->abi;
148 8600
                v->vrt_major = d->vrt_major;
149 8600
                v->vrt_minor = d->vrt_minor;
150
151 8600
                REPLACE(v->nm, nm);
152 8600
                REPLACE(v->path, path);
153
154 8600
                VSC_C_main->vmods++;
155 8600
                VTAILQ_INSERT_TAIL(&vmods, v, list);
156 8600
        }
157
158 11950
        assert(len == v->funclen);
159 11950
        memcpy(ptr, v->funcs, v->funclen);
160 11950
        v->ref++;
161
162 11950
        *hdl = v;
163 11950
        return (0);
164 11950
}
165
166
void
167 1962
VPI_Vmod_Unload(VRT_CTX, struct vmod **hdl)
168
{
169
        struct vmod *v;
170
171 1962
        ASSERT_CLI();
172
173 1962
        TAKE_OBJ_NOTNULL(v, hdl, VMOD_MAGIC);
174
175 1962
        VCL_TaskLeave(ctx, cli_task_privs);
176 1962
        VCL_TaskEnter(cli_task_privs);
177
178
#ifndef DONT_DLCLOSE_VMODS
179
        /*
180
         * atexit(3) handlers are not called during dlclose(3).  We don't
181
         * normally use them, but we do when running GCOV.  This option
182
         * enables us to do that.
183
         */
184
        AZ(dlclose(v->hdl));
185
#endif
186 1962
        if (--v->ref != 0)
187 1400
                return;
188 562
        free(v->nm);
189 562
        free(v->path);
190 562
        free(v->backup);
191 562
        VTAILQ_REMOVE(&vmods, v, list);
192 562
        VSC_C_main->vmods--;
193 562
        FREE_OBJ(v);
194 1962
}
195
196
void
197 297
VMOD_Panic(struct vsb *vsb)
198
{
199
        struct vmod *v;
200
201 297
        VSB_cat(vsb, "vmods = {\n");
202 297
        VSB_indent(vsb, 2);
203 472
        VTAILQ_FOREACH(v, &vmods, list)
204 350
                VSB_printf(vsb, "%s = {%p, %s, %u.%u},\n",
205 175
                    v->nm, v, v->abi, v->vrt_major, v->vrt_minor);
206 297
        VSB_indent(vsb, -2);
207 297
        VSB_cat(vsb, "},\n");
208 297
}
209
210
/*---------------------------------------------------------------------*/
211
212
static void v_matchproto_(cli_func_t)
213 100
ccf_debug_vmod(struct cli *cli, const char * const *av, void *priv)
214
{
215
        struct vmod *v;
216
217 100
        (void)av;
218 100
        (void)priv;
219 100
        ASSERT_CLI();
220 198
        VTAILQ_FOREACH(v, &vmods, list)
221 98
                VCLI_Out(cli, "%5d %s (%s)\n", v->ref, v->nm, v->path);
222 100
}
223
224
static struct cli_proto vcl_cmds[] = {
225
        { CLICMD_DEBUG_VMOD,                    "d", ccf_debug_vmod },
226
        { NULL }
227
};
228
229
void
230 22144
VMOD_Init(void)
231
{
232
233 22144
        CLI_AddFuncs(vcl_cmds);
234 22144
}