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