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 4
pan_privs(struct vsb *vsb, const struct vrt_privs *privs)
56
{
57
        struct vrt_priv *vp;
58
59 4
        VSB_printf(vsb, "privs = %p {\n", privs);
60 4
        if (PAN_already(vsb, privs))
61 0
                return;
62 4
        VSB_indent(vsb, 2);
63 4
        PAN_CheckMagic(vsb, privs, VRT_PRIVS_MAGIC);
64 4
        if (privs->magic == VRT_PRIVS_MAGIC) {
65 4
                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 4
        VSB_indent(vsb, -2);
79 4
        VSB_printf(vsb, "},\n");
80
}
81
82
83
/*--------------------------------------------------------------------
84
 */
85
86
void
87 10142
VRTPRIV_init(struct vrt_privs *privs)
88
{
89
90 10142
        INIT_OBJ(privs, VRT_PRIVS_MAGIC);
91 10142
        VTAILQ_INIT(&privs->privs);
92 10142
}
93
94
static struct vmod_priv *
95 426
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 426
        CHECK_OBJ_NOTNULL(vps, VRT_PRIVS_MAGIC);
101 426
        AN(vmod_id);
102
103 766
        VTAILQ_FOREACH(vp, &vps->privs, list) {
104 644
                CHECK_OBJ_NOTNULL(vp, VRT_PRIV_MAGIC);
105 1288
                if (vp->vcl == vcl && vp->id == id &&
106 644
                    vp->vmod_id == vmod_id)
107 304
                        return (vp->priv);
108
        }
109 122
        vp = WS_Alloc(ws, sizeof *vp);
110 122
        if (vp == NULL)
111 0
                return (NULL);
112 122
        INIT_OBJ(vp, VRT_PRIV_MAGIC);
113 122
        vp->vcl = vcl;
114 122
        vp->id = id;
115 122
        vp->vmod_id = vmod_id;
116 122
        VTAILQ_INSERT_TAIL(&vps->privs, vp, list);
117 122
        return (vp->priv);
118
}
119
120
void
121 13339
VRTPRIV_dynamic_kill(struct vrt_privs *privs, uintptr_t id)
122
{
123
        struct vrt_priv *vp, *vp1;
124
125 13339
        CHECK_OBJ_NOTNULL(privs, VRT_PRIVS_MAGIC);
126 13339
        AN(id);
127
128 13463
        VTAILQ_FOREACH_SAFE(vp, &privs->privs, list, vp1) {
129 124
                CHECK_OBJ_NOTNULL(vp, VRT_PRIV_MAGIC);
130 124
                if (id == vp->id) {
131 122
                        VTAILQ_REMOVE(&privs->privs, vp, list);
132 122
                        VRT_priv_fini(vp->priv);
133
                }
134
        }
135 13339
}
136
137
struct vmod_priv *
138 410
VRT_priv_task(VRT_CTX, const void *vmod_id)
139
{
140
        uintptr_t id;
141
        struct vrt_privs *vps;
142
143 410
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
144 410
        if (ctx->req) {
145 75
                CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
146 75
                id = (uintptr_t)ctx->req;
147 75
                CAST_OBJ_NOTNULL(vps, ctx->req->privs, VRT_PRIVS_MAGIC);
148 335
        } else if (ctx->bo) {
149 229
                CHECK_OBJ_NOTNULL(ctx->bo, BUSYOBJ_MAGIC);
150 229
                id = (uintptr_t)ctx->bo;
151 229
                CAST_OBJ_NOTNULL(vps, ctx->bo->privs, VRT_PRIVS_MAGIC);
152
        } else {
153 106
                ASSERT_CLI();
154 106
                id = (uintptr_t)cli_task_privs;
155 106
                CAST_OBJ_NOTNULL(vps, cli_task_privs, VRT_PRIVS_MAGIC);
156
        }
157 410
        return (vrt_priv_dynamic(ctx->vcl, ctx->ws,
158
                                 vps, id, (uintptr_t)vmod_id));
159
}
160
161
struct vmod_priv *
162 16
VRT_priv_top(VRT_CTX, const void *vmod_id)
163
{
164
        uintptr_t id;
165
        struct vrt_privs *vps;
166
167 16
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
168 16
        if (ctx->req) {
169 16
                CHECK_OBJ_NOTNULL(ctx->req, REQ_MAGIC);
170 16
                CHECK_OBJ_NOTNULL(ctx->req->top, REQ_MAGIC);
171 16
                id = (uintptr_t)&ctx->req->top->top;
172 16
                CAST_OBJ_NOTNULL(vps, ctx->req->top->privs, VRT_PRIVS_MAGIC);
173 16
                return (vrt_priv_dynamic(ctx->vcl, ctx->req->top->ws,
174
                                         vps, id, (uintptr_t)vmod_id));
175
        } else
176 0
                WRONG("PRIV_TOP is only accessible in client VCL context");
177
        NEEDLESS(return NULL);
178
}
179
180
/*--------------------------------------------------------------------
181
 */
182
183
void
184 191
VRT_priv_fini(const struct vmod_priv *p)
185
{
186
187 191
        if (p->priv != NULL && p->free != NULL)
188 29
                p->free(p->priv);
189 191
}