varnish-cache/bin/varnishd/cache/cache_vrt_priv.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: private variables
30
 */
31
32
#include "config.h"
33
34
35
#include <stdlib.h>
36
37
#include "cache_varnishd.h"
38
39
struct vrt_priv {
40
        unsigned                        magic;
41
#define VRT_PRIV_MAGIC                  0x24157a52
42
        VTAILQ_ENTRY(vrt_priv)          list;
43
        struct vmod_priv                priv[1];
44
        const struct vcl                *vcl;
45
        uintptr_t                       id;
46
        uintptr_t                       vmod_id;
47
};
48
49
struct vrt_privs cli_task_privs[1];
50
51
/*--------------------------------------------------------------------
52
 */
53
54
void
55 8
pan_privs(struct vsb *vsb, const struct vrt_privs *privs)
56
{
57
        struct vrt_priv *vp;
58
59 8
        VSB_printf(vsb, "privs = %p {\n", privs);
60 8
        if (PAN_already(vsb, privs))
61 0
                return;
62 8
        VSB_indent(vsb, 2);
63 8
        PAN_CheckMagic(vsb, privs, VRT_PRIVS_MAGIC);
64 8
        if (privs->magic == VRT_PRIVS_MAGIC) {
65 8
                VTAILQ_FOREACH(vp, &privs->privs, list) {
66 0
                        PAN_CheckMagic(vsb, vp, VRT_PRIV_MAGIC);
67 0
                        VSB_printf(vsb,
68
                            "priv {p %p l %d f %p} vcl %p id %jx vmod %jx\n",
69
                            vp->priv->priv,
70
                            vp->priv->len,
71
                            vp->priv->free,
72
                            vp->vcl,
73 0
                            (uintmax_t)vp->id,
74 0
                            (uintmax_t)vp->vmod_id
75
                        );
76
                }
77
        }
78 8
        VSB_indent(vsb, -2);
79 8
        VSB_printf(vsb, "},\n");
80
}
81
82
83
/*--------------------------------------------------------------------
84
 */
85
86
static void
87 12068
VRTPRIV_init(struct vrt_privs *privs)
88
{
89
90 12068
        INIT_OBJ(privs, VRT_PRIVS_MAGIC);
91 12068
        VTAILQ_INIT(&privs->privs);
92 12068
}
93
94
static struct vmod_priv *
95 546
vrt_priv_dynamic(const struct vcl *vcl, struct ws *ws,
96
     struct vrt_privs *vps, uintptr_t id, uintptr_t vmod_id)
97
{
98
        struct vrt_priv *vp;
99
100 546
        CHECK_OBJ_NOTNULL(vps, VRT_PRIVS_MAGIC);
101 546
        AN(vmod_id);
102
103 1220
        VTAILQ_FOREACH(vp, &vps->privs, list) {
104 972
                CHECK_OBJ_NOTNULL(vp, VRT_PRIV_MAGIC);
105 1944
                if (vp->vcl == vcl && vp->id == id &&
106 972
                    vp->vmod_id == vmod_id)
107 298
                        return (vp->priv);
108
        }
109 248
        vp = WS_Alloc(ws, sizeof *vp);
110 248
        if (vp == NULL)
111 0
                return (NULL);
112 248
        INIT_OBJ(vp, VRT_PRIV_MAGIC);
113 248
        vp->vcl = vcl;
114 248
        vp->id = id;
115 248
        vp->vmod_id = vmod_id;
116 248
        VTAILQ_INSERT_TAIL(&vps->privs, vp, list);
117 248
        return (vp->priv);
118
}
119
120
struct vmod_priv *
121 522
VRT_priv_task(VRT_CTX, const void *vmod_id)
122
{
123
        uintptr_t id;
124
        struct vrt_privs *vps;
125
        struct vmod_priv *vp;
126
127 522
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
128 522
        assert(ctx->req == NULL || ctx->bo == NULL);
129 522
        if (ctx->req) {
130 42
                CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
131 42
                id = (uintptr_t)ctx->req;
132 42
                CAST_OBJ_NOTNULL(vps, ctx->req->privs, VRT_PRIVS_MAGIC);
133 480
        } else if (ctx->bo) {
134 450
                CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
135 450
                id = (uintptr_t)ctx->bo;
136 450
                CAST_OBJ_NOTNULL(vps, ctx->bo->privs, VRT_PRIVS_MAGIC);
137
        } else {
138 30
                ASSERT_CLI();
139 30
                id = (uintptr_t)cli_task_privs;
140 30
                CAST_OBJ_NOTNULL(vps, cli_task_privs, VRT_PRIVS_MAGIC);
141
        }
142
143 522
        vp = vrt_priv_dynamic(ctx->vcl, ctx->ws, vps, id, (uintptr_t)vmod_id);
144 522
        return (vp);
145
}
146
147
struct vmod_priv *
148 24
VRT_priv_top(VRT_CTX, const void *vmod_id)
149
{
150
        uintptr_t id;
151
        struct vrt_privs *vps;
152
153 24
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
154 24
        if (ctx->req) {
155 24
                CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
156 24
                CHECK_OBJ_NOTNULL(ctx->req->top, REQ_MAGIC);
157 24
                id = (uintptr_t)&ctx->req->top->top;
158 24
                CAST_OBJ_NOTNULL(vps, ctx->req->top->privs, VRT_PRIVS_MAGIC);
159 24
                return (vrt_priv_dynamic(ctx->vcl, ctx->req->top->ws,
160
                                         vps, id, (uintptr_t)vmod_id));
161
        } else
162 0
                WRONG("PRIV_TOP is only accessible in client VCL context");
163
        NEEDLESS(return NULL);
164
}
165
166
/*--------------------------------------------------------------------
167
 */
168
169
void
170 386
VRT_priv_fini(const struct vmod_priv *p)
171
{
172
173 386
        if (p->priv != NULL && p->free != NULL)
174 60
                p->free(p->priv);
175 386
}
176
177
/*--------------------------------------------------------------------*/
178
179
void
180 12068
VCL_TaskEnter(const struct vcl *vcl, struct vrt_privs *privs)
181
{
182
183 12068
        AN(vcl);
184 12068
        AZ(privs->magic);
185 12068
        VRTPRIV_init(privs);
186 12068
}
187
188
void
189 12056
VCL_TaskLeave(const struct vcl *vcl, struct vrt_privs *privs)
190
{
191
        struct vrt_priv *vp, *vp1;
192
193 12056
        AN(vcl);
194 12056
        CHECK_OBJ_NOTNULL(privs, VRT_PRIVS_MAGIC);
195 12304
        VTAILQ_FOREACH_SAFE(vp, &privs->privs, list, vp1) {
196 248
                VTAILQ_REMOVE(&privs->privs, vp, list);
197 248
                VRT_priv_fini(vp->priv);
198
        }
199 12056
        INIT_OBJ(privs, 0);
200 12056
}
201