varnish-cache/vmod/vmod_debug_dyn.c
0
/*-
1
 * Copyright (c) 2015 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Dridi Boukelmoune <dridi@varnish-software.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
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
30
#include "config.h"
31
32
#include <netdb.h>
33
#include <stdlib.h>
34
#include <string.h>
35
#include <sys/socket.h>
36
#include <sys/stat.h>
37
#include <unistd.h>
38
39
#include "cache/cache.h"
40
41
#include "vsa.h"
42
#include "vss.h"
43
#include "vus.h"
44
#include "vcc_debug_if.h"
45
46
struct xyzzy_debug_dyn {
47
        unsigned                magic;
48
#define VMOD_DEBUG_DYN_MAGIC    0x9b77ccbd
49
        pthread_mutex_t         mtx;
50
        char                    *vcl_name;
51
        VCL_BACKEND             dir;
52
};
53
54
struct xyzzy_debug_dyn_uds {
55
        unsigned                magic;
56
#define VMOD_DEBUG_UDS_MAGIC    0x6c7370e6
57
        pthread_mutex_t         mtx;
58
        char                    *vcl_name;
59
        VCL_BACKEND             dir;
60
};
61
62
static void
63 720
dyn_dir_init(VRT_CTX, struct xyzzy_debug_dyn *dyn,
64
    VCL_STRING addr, VCL_STRING port, VCL_PROBE probe, VCL_BACKEND via)
65
{
66
        const struct suckaddr *sa;
67
        VCL_BACKEND dir, dir2;
68
        struct vrt_endpoint vep;
69
        struct vrt_backend vrt;
70
71 720
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
72 720
        XXXAN(addr);
73 720
        XXXAN(port);
74 720
        CHECK_OBJ_ORNULL(via, DIRECTOR_MAGIC);
75
76 720
        INIT_OBJ(&vep, VRT_ENDPOINT_MAGIC);
77 720
        INIT_OBJ(&vrt, VRT_BACKEND_MAGIC);
78 720
        vrt.endpoint = &vep;
79 720
        vrt.vcl_name = dyn->vcl_name;
80 720
        vrt.hosthdr = addr;
81 720
        vrt.authority = addr;
82 720
        vrt.probe = probe;
83
84 720
        sa = VSS_ResolveOne(NULL, addr, port, AF_UNSPEC, SOCK_STREAM, 0);
85 720
        AN(sa);
86 720
        if (VSA_Get_Proto(sa) == AF_INET)
87 720
                vep.ipv4 = sa;
88 0
        else if (VSA_Get_Proto(sa) == AF_INET6)
89 0
                vep.ipv6 = sa;
90
        else
91 0
                WRONG("Wrong proto family");
92
93 720
        dir = VRT_new_backend(ctx, &vrt, via);
94 720
        AN(dir);
95
96
        /*
97
         * NB: A real dynamic backend should not replace the previous
98
         * instance if the new one is identical.  We do it here because
99
         * the d* tests requires a replacement.
100
         */
101 720
        PTOK(pthread_mutex_lock(&dyn->mtx));
102 720
        dir2 = dyn->dir;
103 720
        dyn->dir = dir;
104 720
        PTOK(pthread_mutex_unlock(&dyn->mtx));
105
106 720
        if (dir2 != NULL)
107 360
                VRT_delete_backend(ctx, &dir2);
108
109 720
        VSA_free(&sa);
110 720
}
111
112
VCL_VOID
113 400
xyzzy_dyn__init(VRT_CTX, struct xyzzy_debug_dyn **dynp,
114
    const char *vcl_name, VCL_STRING addr, VCL_STRING port, VCL_PROBE probe,
115
    VCL_BACKEND via)
116
{
117
        struct xyzzy_debug_dyn *dyn;
118
119 400
        ASSERT_CLI();
120 400
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
121 400
        AN(dynp);
122 400
        AZ(*dynp);
123 400
        AN(vcl_name);
124
125 400
        if (*addr == '\0' || *port == '\0') {
126 40
                AZ(VRT_handled(ctx));
127 40
                VRT_fail(ctx, "Missing dynamic backend address or port");
128 40
                return;
129
        }
130
131 360
        ALLOC_OBJ(dyn, VMOD_DEBUG_DYN_MAGIC);
132 360
        AN(dyn);
133 360
        REPLACE(dyn->vcl_name, vcl_name);
134
135 360
        PTOK(pthread_mutex_init(&dyn->mtx, NULL));
136
137 360
        dyn_dir_init(ctx, dyn, addr, port, probe, via);
138 360
        XXXAN(dyn->dir);
139 360
        *dynp = dyn;
140 400
}
141
142
VCL_VOID v_matchproto_(td_xyzzy_debug_dyn__fini)
143 40
xyzzy_dyn__fini(struct xyzzy_debug_dyn **dynp)
144
{
145
        struct xyzzy_debug_dyn *dyn;
146
147 40
        TAKE_OBJ_NOTNULL(dyn, dynp, VMOD_DEBUG_DYN_MAGIC);
148
        /* at this point all backends will be deleted by the vcl */
149 40
        free(dyn->vcl_name);
150 40
        PTOK(pthread_mutex_destroy(&dyn->mtx));
151 40
        FREE_OBJ(dyn);
152 40
}
153
154
VCL_BACKEND v_matchproto_()
155 640
xyzzy_dyn_backend(VRT_CTX, struct xyzzy_debug_dyn *dyn)
156
{
157 640
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
158 640
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
159 640
        AN(dyn->dir);
160 640
        return (dyn->dir);
161
}
162
163
VCL_VOID
164 360
xyzzy_dyn_refresh(VRT_CTX, struct xyzzy_debug_dyn *dyn,
165
    VCL_STRING addr, VCL_STRING port, VCL_PROBE probe, VCL_BACKEND via)
166
{
167 360
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
168 360
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
169 360
        dyn_dir_init(ctx, dyn, addr, port, probe, via);
170 360
}
171
172
static int
173 680
dyn_uds_init(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path)
174
{
175
        VCL_BACKEND dir, dir2;
176
        struct vrt_endpoint vep;
177
        struct vrt_backend vrt;
178
        struct stat st;
179
180 680
        if (path == NULL) {
181 0
                VRT_fail(ctx, "path is NULL");
182 0
                return (-1);
183
        }
184 680
        if (! VUS_is(path)) {
185 80
                VRT_fail(ctx, "path must be an absolute path: %s", path);
186 80
                return (-1);
187
        }
188
189
        /* XXX: now that we accept that sockets may come after vcl.load
190
         * and change during the life of a VCL, do we still need to check
191
         * this? It looks like both if blocks can be retired.
192
         */
193 600
        errno = 0;
194 600
        if (stat(path, &st) != 0) {
195 40
                VRT_fail(ctx, "Cannot stat path %s: %s", path, strerror(errno));
196 40
                return (-1);
197
        }
198 560
        if (!S_ISSOCK(st.st_mode)) {
199 40
                VRT_fail(ctx, "%s is not a socket", path);
200 40
                return (-1);
201
        }
202
203 520
        INIT_OBJ(&vep, VRT_ENDPOINT_MAGIC);
204 520
        INIT_OBJ(&vrt, VRT_BACKEND_MAGIC);
205 520
        vrt.endpoint = &vep;
206 520
        vep.uds_path = path;
207 520
        vrt.vcl_name = uds->vcl_name;
208 520
        vrt.hosthdr = "localhost";
209 520
        vep.ipv4 = NULL;
210 520
        vep.ipv6 = NULL;
211
212
        // we support via: uds -> ip, but not via: ip -> uds
213 520
        if ((dir = VRT_new_backend(ctx, &vrt, NULL)) == NULL)
214 0
                return (-1);
215
216 520
        PTOK(pthread_mutex_lock(&uds->mtx));
217 520
        dir2 = uds->dir;
218 520
        uds->dir = dir;
219 520
        PTOK(pthread_mutex_unlock(&uds->mtx));
220
221 520
        if (dir2 != NULL)
222 200
                VRT_delete_backend(ctx, &dir2);
223 520
        return (0);
224 680
}
225
226
VCL_VOID v_matchproto_(td_xyzzy_debug_dyn_uds__init)
227 480
xyzzy_dyn_uds__init(VRT_CTX, struct xyzzy_debug_dyn_uds **udsp,
228
    const char *vcl_name, VCL_STRING path)
229
{
230
        struct xyzzy_debug_dyn_uds *uds;
231
232 480
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
233 480
        AN(udsp);
234 480
        AZ(*udsp);
235 480
        AN(vcl_name);
236
237 480
        ALLOC_OBJ(uds, VMOD_DEBUG_UDS_MAGIC);
238 480
        AN(uds);
239 480
        REPLACE(uds->vcl_name, vcl_name);
240 480
        PTOK(pthread_mutex_init(&uds->mtx, NULL));
241
242 480
        if (dyn_uds_init(ctx, uds, path) != 0) {
243 160
                free(uds->vcl_name);
244 160
                PTOK(pthread_mutex_destroy(&uds->mtx));
245 160
                FREE_OBJ(uds);
246 160
                return;
247
        }
248
249 320
        *udsp = uds;
250 480
}
251
252
VCL_VOID v_matchproto_(td_debug_dyn_uds__fini)
253 40
xyzzy_dyn_uds__fini(struct xyzzy_debug_dyn_uds **udsp)
254
{
255
        struct xyzzy_debug_dyn_uds *uds;
256
257 40
        TAKE_OBJ_NOTNULL(uds, udsp, VMOD_DEBUG_UDS_MAGIC);
258 40
        free(uds->vcl_name);
259 40
        PTOK(pthread_mutex_destroy(&uds->mtx));
260 40
        FREE_OBJ(uds);
261 40
}
262
263
VCL_BACKEND v_matchproto_(td_debug_dyn_uds_backend)
264 520
xyzzy_dyn_uds_backend(VRT_CTX, struct xyzzy_debug_dyn_uds *uds)
265
{
266 520
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
267 520
        CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC);
268 520
        AN(uds->dir);
269 520
        return (uds->dir);
270
}
271
272
VCL_VOID v_matchproto_(td_debug_dyn_uds_refresh)
273 200
xyzzy_dyn_uds_refresh(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path)
274
{
275 200
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
276 200
        CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC);
277 200
        (void) dyn_uds_init(ctx, uds, path);
278 200
}