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 72
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 72
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
72 72
        XXXAN(addr);
73 72
        XXXAN(port);
74 72
        CHECK_OBJ_ORNULL(via, DIRECTOR_MAGIC);
75
76 72
        INIT_OBJ(&vep, VRT_ENDPOINT_MAGIC);
77 72
        VRT_BACKEND_INIT(&vrt);
78 72
        vrt.endpoint = &vep;
79 72
        vrt.vcl_name = dyn->vcl_name;
80 72
        vrt.hosthdr = addr;
81 72
        vrt.authority = addr;
82 72
        vrt.probe = probe;
83
84 72
        sa = VSS_ResolveOne(NULL, addr, port, AF_UNSPEC, SOCK_STREAM, 0);
85 72
        AN(sa);
86 72
        if (VSA_Get_Proto(sa) == AF_INET)
87 72
                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 72
        dir = VRT_new_backend(ctx, &vrt, via);
94 72
        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 72
        PTOK(pthread_mutex_lock(&dyn->mtx));
102 72
        dir2 = dyn->dir;
103 72
        dyn->dir = dir;
104 72
        PTOK(pthread_mutex_unlock(&dyn->mtx));
105
106 72
        if (dir2 != NULL)
107 36
                VRT_delete_backend(ctx, &dir2);
108
109 72
        VSA_free(&sa);
110 72
}
111
112
VCL_VOID
113 40
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 40
        ASSERT_CLI();
120 40
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
121 40
        AN(dynp);
122 40
        AZ(*dynp);
123 40
        AN(vcl_name);
124
125 40
        if (*addr == '\0' || *port == '\0') {
126 4
                AZ(VRT_handled(ctx));
127 4
                VRT_fail(ctx, "Missing dynamic backend address or port");
128 4
                return;
129
        }
130
131 36
        ALLOC_OBJ(dyn, VMOD_DEBUG_DYN_MAGIC);
132 36
        AN(dyn);
133 36
        REPLACE(dyn->vcl_name, vcl_name);
134
135 36
        PTOK(pthread_mutex_init(&dyn->mtx, NULL));
136
137 36
        dyn_dir_init(ctx, dyn, addr, port, probe, via);
138 36
        XXXAN(dyn->dir);
139 36
        *dynp = dyn;
140 40
}
141
142
VCL_VOID v_matchproto_(td_xyzzy_debug_dyn__fini)
143 4
xyzzy_dyn__fini(struct xyzzy_debug_dyn **dynp)
144
{
145
        struct xyzzy_debug_dyn *dyn;
146
147 4
        TAKE_OBJ_NOTNULL(dyn, dynp, VMOD_DEBUG_DYN_MAGIC);
148
        /* at this point all backends will be deleted by the vcl */
149 4
        free(dyn->vcl_name);
150 4
        PTOK(pthread_mutex_destroy(&dyn->mtx));
151 4
        FREE_OBJ(dyn);
152 4
}
153
154
VCL_BACKEND v_matchproto_()
155 64
xyzzy_dyn_backend(VRT_CTX, struct xyzzy_debug_dyn *dyn)
156
{
157
        VCL_BACKEND retval;
158 64
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
159 64
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
160 64
        PTOK(pthread_mutex_lock(&dyn->mtx));
161 64
        retval = dyn->dir;
162 64
        PTOK(pthread_mutex_unlock(&dyn->mtx));
163 64
        AN(retval);
164 64
        return (retval);
165
}
166
167
VCL_VOID
168 36
xyzzy_dyn_refresh(VRT_CTX, struct xyzzy_debug_dyn *dyn,
169
    VCL_STRING addr, VCL_STRING port, VCL_PROBE probe, VCL_BACKEND via)
170
{
171 36
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
172 36
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
173 36
        dyn_dir_init(ctx, dyn, addr, port, probe, via);
174 36
}
175
176
static int
177 68
dyn_uds_init(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path)
178
{
179
        VCL_BACKEND dir, dir2;
180
        struct vrt_endpoint vep;
181
        struct vrt_backend vrt;
182
        struct stat st;
183
184 68
        if (path == NULL) {
185 0
                VRT_fail(ctx, "path is NULL");
186 0
                return (-1);
187
        }
188 68
        if (! VUS_is(path)) {
189 8
                VRT_fail(ctx, "path must be an absolute path: %s", path);
190 8
                return (-1);
191
        }
192
193
        /* XXX: now that we accept that sockets may come after vcl.load
194
         * and change during the life of a VCL, do we still need to check
195
         * this? It looks like both if blocks can be retired.
196
         */
197 60
        errno = 0;
198 60
        if (stat(path, &st) != 0) {
199 4
                VRT_fail(ctx, "Cannot stat path %s: %s", path, strerror(errno));
200 4
                return (-1);
201
        }
202 56
        if (!S_ISSOCK(st.st_mode)) {
203 4
                VRT_fail(ctx, "%s is not a socket", path);
204 4
                return (-1);
205
        }
206
207 52
        INIT_OBJ(&vep, VRT_ENDPOINT_MAGIC);
208 52
        VRT_BACKEND_INIT(&vrt);
209 52
        vrt.endpoint = &vep;
210 52
        vep.uds_path = path;
211 52
        vrt.vcl_name = uds->vcl_name;
212 52
        vrt.hosthdr = "localhost";
213 52
        vep.ipv4 = NULL;
214 52
        vep.ipv6 = NULL;
215
216
        // we support via: uds -> ip, but not via: ip -> uds
217 52
        if ((dir = VRT_new_backend(ctx, &vrt, NULL)) == NULL)
218 0
                return (-1);
219
220 52
        PTOK(pthread_mutex_lock(&uds->mtx));
221 52
        dir2 = uds->dir;
222 52
        uds->dir = dir;
223 52
        PTOK(pthread_mutex_unlock(&uds->mtx));
224
225 52
        if (dir2 != NULL)
226 20
                VRT_delete_backend(ctx, &dir2);
227 52
        return (0);
228 68
}
229
230
VCL_VOID v_matchproto_(td_xyzzy_debug_dyn_uds__init)
231 48
xyzzy_dyn_uds__init(VRT_CTX, struct xyzzy_debug_dyn_uds **udsp,
232
    const char *vcl_name, VCL_STRING path)
233
{
234
        struct xyzzy_debug_dyn_uds *uds;
235
236 48
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
237 48
        AN(udsp);
238 48
        AZ(*udsp);
239 48
        AN(vcl_name);
240
241 48
        ALLOC_OBJ(uds, VMOD_DEBUG_UDS_MAGIC);
242 48
        AN(uds);
243 48
        REPLACE(uds->vcl_name, vcl_name);
244 48
        PTOK(pthread_mutex_init(&uds->mtx, NULL));
245
246 48
        if (dyn_uds_init(ctx, uds, path) != 0) {
247 16
                free(uds->vcl_name);
248 16
                PTOK(pthread_mutex_destroy(&uds->mtx));
249 16
                FREE_OBJ(uds);
250 16
                return;
251
        }
252
253 32
        *udsp = uds;
254 48
}
255
256
VCL_VOID v_matchproto_(td_debug_dyn_uds__fini)
257 4
xyzzy_dyn_uds__fini(struct xyzzy_debug_dyn_uds **udsp)
258
{
259
        struct xyzzy_debug_dyn_uds *uds;
260
261 4
        TAKE_OBJ_NOTNULL(uds, udsp, VMOD_DEBUG_UDS_MAGIC);
262 4
        free(uds->vcl_name);
263 4
        PTOK(pthread_mutex_destroy(&uds->mtx));
264 4
        FREE_OBJ(uds);
265 4
}
266
267
VCL_BACKEND v_matchproto_(td_debug_dyn_uds_backend)
268 52
xyzzy_dyn_uds_backend(VRT_CTX, struct xyzzy_debug_dyn_uds *uds)
269
{
270 52
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
271 52
        CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC);
272 52
        AN(uds->dir);
273 52
        return (uds->dir);
274
}
275
276
VCL_VOID v_matchproto_(td_debug_dyn_uds_refresh)
277 20
xyzzy_dyn_uds_refresh(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path)
278
{
279 20
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
280 20
        CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC);
281 20
        (void) dyn_uds_init(ctx, uds, path);
282 20
}