varnish-cache/bin/varnishd/cache/cache_vrt_vmod.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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
 * Runtime support for compiled VCL programs
30
 */
31
32
#include "config.h"
33
34
#include "cache_varnishd.h"
35
36
#include <dlfcn.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
40
#include "vcli_serve.h"
41
#include "vmod_abi.h"
42
43
/*--------------------------------------------------------------------
44
 * Modules stuff
45
 */
46
47
struct vmod {
48
        unsigned                magic;
49
#define VMOD_MAGIC              0xb750219c
50
51
        VTAILQ_ENTRY(vmod)      list;
52
53
        int                     ref;
54
55
        char                    *nm;
56
        char                    *path;
57
        char                    *backup;
58
        void                    *hdl;
59
        const void              *funcs;
60
        int                     funclen;
61
        const char              *abi;
62
        unsigned                vrt_major;
63
        unsigned                vrt_minor;
64
};
65
66
static VTAILQ_HEAD(,vmod)       vmods = VTAILQ_HEAD_INITIALIZER(vmods);
67
68
static unsigned
69 218
vmod_abi_mismatch(const struct vmod_data *d)
70
{
71
72 218
        if (d->vrt_major == 0 && d->vrt_minor == 0)
73 218
                return (d->abi == NULL || strcmp(d->abi, VMOD_ABI_Version));
74
75 0
        return (d->vrt_major != VRT_MAJOR_VERSION ||
76 0
            d->vrt_minor > VRT_MINOR_VERSION);
77
}
78
79
int
80 315
VRT_Vmod_Init(VRT_CTX, struct vmod **hdl, void *ptr, int len, const char *nm,
81
    const char *path, const char *file_id, const char *backup)
82
{
83
        struct vmod *v;
84
        const struct vmod_data *d;
85
        char buf[256];
86
        void *dlhdl;
87
88 315
        ASSERT_CLI();
89 315
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
90 315
        AN(ctx->msg);
91 315
        AN(hdl);
92 315
        AZ(*hdl);
93
94 315
        dlhdl = dlopen(backup, RTLD_NOW | RTLD_LOCAL);
95 315
        if (dlhdl == NULL) {
96 0
                VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
97
                    nm, backup, path);
98 0
                VSB_printf(ctx->msg, "dlopen() failed: %s\n", dlerror());
99 0
                return (1);
100
        }
101
102 391
        VTAILQ_FOREACH(v, &vmods, list)
103 173
                if (v->hdl == dlhdl)
104 97
                        break;
105 315
        if (v == NULL) {
106 218
                ALLOC_OBJ(v, VMOD_MAGIC);
107 218
                AN(v);
108 218
                REPLACE(v->backup, backup);
109
110 218
                v->hdl = dlhdl;
111
112 218
                bprintf(buf, "Vmod_%s_Data", nm);
113 218
                d = dlsym(v->hdl, buf);
114 436
                if (d == NULL ||
115 436
                    d->file_id == NULL ||
116 218
                    strcmp(d->file_id, file_id)) {
117 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
118
                            nm, backup, path);
119 0
                        VSB_printf(ctx->msg,
120
                            "This is no longer the same file seen by"
121
                            " the VCL-compiler.\n");
122 0
                        (void)dlclose(v->hdl);
123 0
                        FREE_OBJ(v);
124 0
                        return (1);
125
                }
126 436
                if (vmod_abi_mismatch(d) ||
127 436
                    d->name == NULL ||
128 436
                    strcmp(d->name, nm) ||
129 436
                    d->func == NULL ||
130 436
                    d->func_len <= 0 ||
131 436
                    d->proto == NULL ||
132 218
                    d->json == NULL) {
133 0
                        VSB_printf(ctx->msg, "Loading vmod %s from %s (%s):\n",
134
                            nm, backup, path);
135 0
                        VSB_printf(ctx->msg, "VMOD data is mangled.\n");
136 0
                        (void)dlclose(v->hdl);
137 0
                        FREE_OBJ(v);
138 0
                        return (1);
139
                }
140
141 218
                v->funclen = d->func_len;
142 218
                v->funcs = d->func;
143 218
                v->abi = d->abi;
144 218
                v->vrt_major = d->vrt_major;
145 218
                v->vrt_minor = d->vrt_minor;
146
147 218
                REPLACE(v->nm, nm);
148 218
                REPLACE(v->path, path);
149
150 218
                VSC_C_main->vmods++;
151 218
                VTAILQ_INSERT_TAIL(&vmods, v, list);
152
        }
153
154 315
        assert(len == v->funclen);
155 315
        memcpy(ptr, v->funcs, v->funclen);
156 315
        v->ref++;
157
158 315
        *hdl = v;
159 315
        return (0);
160
}
161
162
void
163 64
VRT_Vmod_Fini(struct vmod **hdl)
164
{
165
        struct vmod *v;
166
167 64
        ASSERT_CLI();
168
169 64
        TAKE_OBJ_NOTNULL(v, hdl, VMOD_MAGIC);
170
171
#ifndef DONT_DLCLOSE_VMODS
172
        /*
173
         * atexit(3) handlers are not called during dlclose(3).  We don't
174
         * normally use them, but we do when running GCOV.  This option
175
         * enables us to do that.
176
         */
177
        AZ(dlclose(v->hdl));
178
#endif
179 64
        if (--v->ref != 0)
180 46
                return;
181 18
        free(v->nm);
182 18
        free(v->path);
183 18
        free(v->backup);
184 18
        VTAILQ_REMOVE(&vmods, v, list);
185 18
        VSC_C_main->vmods--;
186 18
        FREE_OBJ(v);
187
}
188
189
void
190 5
VMOD_Panic(struct vsb *vsb)
191
{
192
        struct vmod *v;
193
194 5
        VSB_printf(vsb, "vmods = {\n");
195 5
        VSB_indent(vsb, 2);
196 8
        VTAILQ_FOREACH(v, &vmods, list)
197 3
                VSB_printf(vsb, "%s = {%s, %u.%u},\n",
198
                    v->nm, v->abi, v->vrt_major, v->vrt_minor);
199 5
        VSB_indent(vsb, -2);
200 5
        VSB_printf(vsb, "},\n");
201 5
}
202
203
/*---------------------------------------------------------------------*/
204
205
static void v_matchproto_(cli_func_t)
206 4
ccf_debug_vmod(struct cli *cli, const char * const *av, void *priv)
207
{
208
        struct vmod *v;
209
210
        (void)av;
211
        (void)priv;
212 4
        ASSERT_CLI();
213 5
        VTAILQ_FOREACH(v, &vmods, list)
214 1
                VCLI_Out(cli, "%5d %s (%s)\n", v->ref, v->nm, v->path);
215 4
}
216
217
static struct cli_proto vcl_cmds[] = {
218
        { CLICMD_DEBUG_VMOD,                    "d", ccf_debug_vmod },
219
        { NULL }
220
};
221
222
void
223 685
VMOD_Init(void)
224
{
225
226 685
        CLI_AddFuncs(vcl_cmds);
227 685
}