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 14360
vmod_abi_mismatch(const struct vmod_data *d)
73
{
74
75 14360
        if (d->vrt_major == 0 && d->vrt_minor == 0)
76 11520
                return (d->abi == NULL || strcmp(d->abi, VMOD_ABI_Version));
77
78 2840
        return (d->vrt_major != VRT_MAJOR_VERSION ||
79 2840
            d->vrt_minor > VRT_MINOR_VERSION);
80 14360
}
81
82
int
83 19840
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 19840
        ASSERT_CLI();
92 19840
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
93 19840
        AN(ctx->msg);
94 19840
        AN(hdl);
95 19840
        AZ(*hdl);
96
97 19840
        dlhdl = dlopen(backup, RTLD_NOW | RTLD_LOCAL);
98 19840
        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 25800
        VTAILQ_FOREACH(v, &vmods, list)
106 11440
                if (v->hdl == dlhdl)
107 5480
                        break;
108 19840
        if (v == NULL) {
109 14360
                ALLOC_OBJ(v, VMOD_MAGIC);
110 14360
                AN(v);
111
112 14360
                v->hdl = dlhdl;
113
114 14360
                bprintf(buf, "Vmod_%s_Data", nm);
115 14360
                d = dlsym(v->hdl, buf);
116 28720
                if (d == NULL ||
117 14360
                    d->file_id == NULL ||
118 14360
                    strcmp(d->file_id, file_id)) {
119 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
120 0
                            nm, backup, path);
121 0
                        VSB_cat(ctx->msg,
122
                            "This is no longer the same file seen by"
123
                            " the VCL-compiler.\n");
124 0
                        (void)dlclose(v->hdl);
125 0
                        FREE_OBJ(v);
126 0
                        return (1);
127
                }
128 28720
                if (vmod_abi_mismatch(d) ||
129 14360
                    d->name == NULL ||
130 14360
                    strcmp(d->name, nm) ||
131 14360
                    d->func == NULL ||
132 14360
                    d->func_len <= 0 ||
133 14360
                    d->proto != NULL ||
134 14360
                    d->json == NULL) {
135 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
136 0
                            nm, backup, path);
137 0
                        VSB_cat(ctx->msg, "VMOD data is mangled.\n");
138 0
                        (void)dlclose(v->hdl);
139 0
                        FREE_OBJ(v);
140 0
                        return (1);
141
                }
142
143 14360
                v->nbr = nbr;
144 14360
                v->funclen = d->func_len;
145 14360
                v->funcs = d->func;
146 14360
                v->abi = d->abi;
147 14360
                v->vrt_major = d->vrt_major;
148 14360
                v->vrt_minor = d->vrt_minor;
149
150 14360
                REPLACE(v->nm, nm);
151 14360
                REPLACE(v->path, path);
152 14360
                REPLACE(v->backup, backup);
153
154 14360
                VSC_C_main->vmods++;
155 14360
                VTAILQ_INSERT_TAIL(&vmods, v, list);
156 14360
        }
157
158 19840
        assert(len == v->funclen);
159 19840
        memcpy(ptr, v->funcs, v->funclen);
160 19840
        v->ref++;
161
162 19840
        *hdl = v;
163 19840
        return (0);
164 19840
}
165
166
void
167 3158
VPI_Vmod_Unload(VRT_CTX, struct vmod **hdl)
168
{
169
        struct vmod *v;
170
171 3158
        ASSERT_CLI();
172
173 3158
        TAKE_OBJ_NOTNULL(v, hdl, VMOD_MAGIC);
174
175 3158
        VCL_TaskLeave(ctx, cli_task_privs);
176 3158
        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 3158
        if (--v->ref != 0)
187 2240
                return;
188 918
        free(v->nm);
189 918
        free(v->path);
190 918
        free(v->backup);
191 918
        VTAILQ_REMOVE(&vmods, v, list);
192 918
        VSC_C_main->vmods--;
193 918
        FREE_OBJ(v);
194 3158
}
195
196
void
197 510
VMOD_Panic(struct vsb *vsb)
198
{
199
        struct vmod *v;
200
201 510
        VSB_cat(vsb, "vmods = {\n");
202 510
        VSB_indent(vsb, 2);
203 910
        VTAILQ_FOREACH(v, &vmods, list)
204 800
                VSB_printf(vsb, "%s = {%p, %s, %u.%u},\n",
205 400
                    v->nm, v, v->abi, v->vrt_major, v->vrt_minor);
206 510
        VSB_indent(vsb, -2);
207 510
        VSB_cat(vsb, "},\n");
208 510
}
209
210
/*---------------------------------------------------------------------*/
211
212
static void v_matchproto_(cli_func_t)
213 160
ccf_debug_vmod(struct cli *cli, const char * const *av, void *priv)
214
{
215
        struct vmod *v;
216
217 160
        (void)av;
218 160
        (void)priv;
219 160
        ASSERT_CLI();
220 318
        VTAILQ_FOREACH(v, &vmods, list)
221 158
                VCLI_Out(cli, "%5d %s (%s)\n", v->ref, v->nm, v->path);
222 160
}
223
224
static struct cli_proto vcl_cmds[] = {
225
        { CLICMD_DEBUG_VMOD,                    "d", ccf_debug_vmod },
226
        { NULL }
227
};
228
229
void
230 36540
VMOD_Init(void)
231
{
232
233 36540
        CLI_AddFuncs(vcl_cmds);
234 36540
}