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 340
vmod_abi_mismatch(const struct vmod_data *d)
70
{
71
72 340
        if (d->vrt_major == 0 && d->vrt_minor == 0)
73 340
                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 452
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 452
        ASSERT_CLI();
89 452
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
90 452
        AN(ctx->msg);
91 452
        AN(hdl);
92 452
        AZ(*hdl);
93
94 452
        dlhdl = dlopen(backup, RTLD_NOW | RTLD_LOCAL);
95 452
        if (dlhdl == NULL) {
96 0
                VSB_printf(ctx->msg, "Loading vmod %s from %s:\n", nm, backup);
97 0
                VSB_printf(ctx->msg, "dlopen() failed: %s\n", dlerror());
98 0
                return (1);
99
        }
100
101 530
        VTAILQ_FOREACH(v, &vmods, list)
102 190
                if (v->hdl == dlhdl)
103 112
                        break;
104 452
        if (v == NULL) {
105 340
                ALLOC_OBJ(v, VMOD_MAGIC);
106 340
                AN(v);
107 340
                REPLACE(v->backup, backup);
108
109 340
                v->hdl = dlhdl;
110
111 340
                bprintf(buf, "Vmod_%s_Data", nm);
112 340
                d = dlsym(v->hdl, buf);
113 680
                if (d == NULL ||
114 680
                    d->file_id == NULL ||
115 340
                    strcmp(d->file_id, file_id)) {
116 0
                        VSB_printf(ctx->msg,
117
                            "Loading vmod %s from %s:\n", nm, path);
118 0
                        VSB_printf(ctx->msg,
119
                            "This is no longer the same file seen by"
120
                            " the VCL-compiler.\n");
121 0
                        (void)dlclose(v->hdl);
122 0
                        FREE_OBJ(v);
123 0
                        return (1);
124
                }
125 680
                if (vmod_abi_mismatch(d) ||
126 680
                    d->name == NULL ||
127 680
                    strcmp(d->name, nm) ||
128 680
                    d->func == NULL ||
129 680
                    d->func_len <= 0 ||
130 680
                    d->proto == NULL ||
131 340
                    d->spec == NULL) {
132 0
                        VSB_printf(ctx->msg,
133
                            "Loading VMOD %s from %s:\n", nm, path);
134 0
                        VSB_printf(ctx->msg, "VMOD data is mangled.\n");
135 0
                        (void)dlclose(v->hdl);
136 0
                        FREE_OBJ(v);
137 0
                        return (1);
138
                }
139
140 340
                v->funclen = d->func_len;
141 340
                v->funcs = d->func;
142 340
                v->abi = d->abi;
143 340
                v->vrt_major = d->vrt_major;
144 340
                v->vrt_minor = d->vrt_minor;
145
146 340
                REPLACE(v->nm, nm);
147 340
                REPLACE(v->path, path);
148
149 340
                VSC_C_main->vmods++;
150 340
                VTAILQ_INSERT_TAIL(&vmods, v, list);
151
        }
152
153 452
        assert(len == v->funclen);
154 452
        memcpy(ptr, v->funcs, v->funclen);
155 452
        v->ref++;
156
157 452
        *hdl = v;
158 452
        return (0);
159
}
160
161
void
162 56
VRT_Vmod_Fini(struct vmod **hdl)
163
{
164
        struct vmod *v;
165
166 56
        ASSERT_CLI();
167
168 56
        TAKE_OBJ_NOTNULL(v, hdl, VMOD_MAGIC);
169
170
#ifndef DONT_DLCLOSE_VMODS
171
        /*
172
         * atexit(3) handlers are not called during dlclose(3).  We don't
173
         * normally use them, but we do when running GCOV.  This option
174
         * enables us to do that.
175
         */
176
        AZ(dlclose(v->hdl));
177
#endif
178 56
        if (--v->ref != 0)
179 84
                return;
180 28
        free(v->nm);
181 28
        free(v->path);
182 28
        free(v->backup);
183 28
        VTAILQ_REMOVE(&vmods, v, list);
184 28
        VSC_C_main->vmods--;
185 28
        FREE_OBJ(v);
186
}
187
188
void
189 8
VMOD_Panic(struct vsb *vsb)
190
{
191
        struct vmod *v;
192
193 8
        VSB_printf(vsb, "vmods = {\n");
194 8
        VSB_indent(vsb, 2);
195 14
        VTAILQ_FOREACH(v, &vmods, list)
196 6
                VSB_printf(vsb, "%s = {%s, %u.%u},\n",
197
                    v->nm, v->abi, v->vrt_major, v->vrt_minor);
198 8
        VSB_indent(vsb, -2);
199 8
        VSB_printf(vsb, "},\n");
200 8
}
201
202
/*---------------------------------------------------------------------*/
203
204
static void v_matchproto_(cli_func_t)
205 8
ccf_debug_vmod(struct cli *cli, const char * const *av, void *priv)
206
{
207
        struct vmod *v;
208
209
        (void)av;
210
        (void)priv;
211 8
        ASSERT_CLI();
212 10
        VTAILQ_FOREACH(v, &vmods, list)
213 2
                VCLI_Out(cli, "%5d %s (%s)\n", v->ref, v->nm, v->path);
214 8
}
215
216
static struct cli_proto vcl_cmds[] = {
217
        { CLICMD_DEBUG_VMOD,                    "d", ccf_debug_vmod },
218
        { NULL }
219
};
220
221
void
222 1228
VMOD_Init(void)
223
{
224
225 1228
        CLI_AddFuncs(vcl_cmds);
226 1228
}