varnish-cache/lib/libvmod_debug/vmod_debug_dyn.c
1
/*-
2
 * Copyright (c) 2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Dridi Boukelmoune <dridi@varnish-software.com>
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. Redistributions in binary form must reproduce the above copyright
13
 *    notice, this list of conditions and the following disclaimer in the
14
 *    documentation and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
20
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26
 * SUCH DAMAGE.
27
 */
28
29
#include "config.h"
30
31
#include <netdb.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <sys/socket.h>
35
#include <sys/types.h>
36
#include <sys/stat.h>
37
#include <unistd.h>
38
#include <errno.h>
39
40
#include "cache/cache.h"
41
42
#include "vsa.h"
43
#include "vcc_if.h"
44
45
struct xyzzy_debug_dyn {
46
        unsigned                magic;
47
#define VMOD_DEBUG_DYN_MAGIC    0x9b77ccbd
48
        pthread_mutex_t         mtx;
49
        char                    *vcl_name;
50
        VCL_BACKEND             dir;
51
};
52
53
struct xyzzy_debug_dyn_uds {
54
        unsigned                magic;
55
#define VMOD_DEBUG_UDS_MAGIC    0x6c7370e6
56
        pthread_mutex_t         mtx;
57
        char                    *vcl_name;
58
        VCL_BACKEND             dir;
59
};
60
61
static void
62 156
dyn_dir_init(VRT_CTX, struct xyzzy_debug_dyn *dyn,
63
    VCL_STRING addr, VCL_STRING port)
64
{
65 156
        struct addrinfo hints, *res = NULL;
66
        struct suckaddr *sa;
67
        VCL_BACKEND dir, dir2;
68
        struct vrt_backend vrt;
69
70 156
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
71 156
        XXXAN(addr);
72 156
        XXXAN(port);
73
74 156
        INIT_OBJ(&vrt, VRT_BACKEND_MAGIC);
75 156
        vrt.port = port;
76 156
        vrt.vcl_name = dyn->vcl_name;
77 156
        vrt.hosthdr = addr;
78
79 156
        memset(&hints, 0, sizeof(hints));
80 156
        hints.ai_family = AF_UNSPEC;
81 156
        hints.ai_socktype = SOCK_STREAM;
82 156
        AZ(getaddrinfo(addr, port, &hints, &res));
83 156
        XXXAZ(res->ai_next);
84
85 156
        sa = VSA_Malloc(res->ai_addr, res->ai_addrlen);
86 156
        AN(sa);
87 156
        if (VSA_Get_Proto(sa) == AF_INET) {
88 156
                vrt.ipv4_addr = addr;
89 156
                vrt.ipv4_suckaddr = sa;
90 0
        } else if (VSA_Get_Proto(sa) == AF_INET6) {
91 0
                vrt.ipv6_addr = addr;
92 0
                vrt.ipv6_suckaddr = sa;
93
        } else
94 0
                WRONG("Wrong proto family");
95
96 156
        freeaddrinfo(res);
97
98 156
        dir = VRT_new_backend(ctx, &vrt);
99 156
        AN(dir);
100
101
        /*
102
         * NB: A real dynamic backend should not replace the previous
103
         * instance if the new one is identical.  We do it here because
104
         * the d* tests requires a replacement.
105
         */
106 156
        AZ(pthread_mutex_lock(&dyn->mtx));
107 156
        dir2 = dyn->dir;
108 156
        dyn->dir = dir;
109 156
        AZ(pthread_mutex_unlock(&dyn->mtx));
110
111 156
        if (dir2 != NULL)
112 60
                VRT_delete_backend(ctx, &dir2);
113
114 156
        free(sa);
115 156
}
116
117
VCL_VOID
118 108
xyzzy_dyn__init(VRT_CTX, struct xyzzy_debug_dyn **dynp,
119
    const char *vcl_name, VCL_STRING addr, VCL_STRING port)
120
{
121
        struct xyzzy_debug_dyn *dyn;
122
123 108
        ASSERT_CLI();
124 108
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
125 108
        AN(dynp);
126 108
        AZ(*dynp);
127 108
        AN(vcl_name);
128
129 108
        if (*addr == '\0' || *port == '\0') {
130 12
                AN(ctx->handling);
131 12
                AZ(*ctx->handling);
132 12
                VRT_fail(ctx, "Missing dynamic backend address or port");
133 12
                return;
134
        }
135
136 96
        ALLOC_OBJ(dyn, VMOD_DEBUG_DYN_MAGIC);
137 96
        AN(dyn);
138 96
        REPLACE(dyn->vcl_name, vcl_name);
139
140 96
        AZ(pthread_mutex_init(&dyn->mtx, NULL));
141
142 96
        dyn_dir_init(ctx, dyn, addr, port);
143 96
        XXXAN(dyn->dir);
144 96
        *dynp = dyn;
145
}
146
147
VCL_VOID
148 24
xyzzy_dyn__fini(struct xyzzy_debug_dyn **dynp)
149
{
150
        struct xyzzy_debug_dyn *dyn;
151
152 24
        AN(dynp);
153 24
        if (*dynp == NULL)
154 12
                return; /* failed initialization */
155
156 12
        CAST_OBJ_NOTNULL(dyn, *dynp, VMOD_DEBUG_DYN_MAGIC);
157
        /* at this point all backends will be deleted by the vcl */
158 12
        free(dyn->vcl_name);
159 12
        AZ(pthread_mutex_destroy(&dyn->mtx));
160 12
        FREE_OBJ(dyn);
161 12
        *dynp = NULL;
162
}
163
164
VCL_BACKEND v_matchproto_()
165 156
xyzzy_dyn_backend(VRT_CTX, struct xyzzy_debug_dyn *dyn)
166
{
167 156
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
168 156
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
169 156
        AN(dyn->dir);
170 156
        return (dyn->dir);
171
}
172
173
VCL_VOID
174 60
xyzzy_dyn_refresh(VRT_CTX, struct xyzzy_debug_dyn *dyn,
175
    VCL_STRING addr, VCL_STRING port)
176
{
177 60
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
178 60
        CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
179 60
        dyn_dir_init(ctx, dyn, addr, port);
180 60
}
181
182
static int
183 204
dyn_uds_init(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path)
184
{
185
        VCL_BACKEND dir, dir2;
186
        struct vrt_backend vrt;
187
        struct stat st;
188
189 204
        if (path == NULL) {
190 0
                VRT_fail(ctx, "path is NULL");
191 0
                return (-1);
192
        }
193 204
        if (*path != '/') {
194 24
                VRT_fail(ctx, "path must be an absolute path: %s", path);
195 24
                return (-1);
196
        }
197 180
        errno = 0;
198 180
        if (stat(path, &st) != 0) {
199 12
                VRT_fail(ctx, "Cannot stat path %s: %s", path, strerror(errno));
200 12
                return (-1);
201
        }
202 168
        if (!S_ISSOCK(st.st_mode)) {
203 12
                VRT_fail(ctx, "%s is not a socket", path);
204 12
                return (-1);
205
        }
206
207 156
        INIT_OBJ(&vrt, VRT_BACKEND_MAGIC);
208 156
        vrt.path = path;
209 156
        vrt.vcl_name = uds->vcl_name;
210 156
        vrt.hosthdr = "localhost";
211 156
        vrt.ipv4_suckaddr = NULL;
212 156
        vrt.ipv6_suckaddr = NULL;
213
214 156
        if ((dir = VRT_new_backend(ctx, &vrt)) == NULL)
215 0
                return (-1);
216
217 156
        AZ(pthread_mutex_lock(&uds->mtx));
218 156
        dir2 = uds->dir;
219 156
        uds->dir = dir;
220 156
        AZ(pthread_mutex_unlock(&uds->mtx));
221
222 156
        if (dir2 != NULL)
223 60
                VRT_delete_backend(ctx, &dir2);
224 156
        return (0);
225
}
226
227
VCL_VOID v_matchproto_(td_debug_dyn_uds__init)
228 144
xyzzy_dyn_uds__init(VRT_CTX, struct xyzzy_debug_dyn_uds **udsp,
229
                    const char *vcl_name, VCL_STRING path)
230
{
231
        struct xyzzy_debug_dyn_uds *uds;
232
233 144
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
234 144
        AN(udsp);
235 144
        AZ(*udsp);
236 144
        AN(vcl_name);
237
238 144
        ALLOC_OBJ(uds, VMOD_DEBUG_UDS_MAGIC);
239 144
        AN(uds);
240 144
        REPLACE(uds->vcl_name, vcl_name);
241 144
        AZ(pthread_mutex_init(&uds->mtx, NULL));
242
243 144
        if (dyn_uds_init(ctx, uds, path) != 0) {
244 48
                free(uds->vcl_name);
245 48
                AZ(pthread_mutex_destroy(&uds->mtx));
246 48
                FREE_OBJ(uds);
247 48
                return;
248
        }
249 96
        *udsp = uds;
250
}
251
252
VCL_VOID v_matchproto_(td_debug_dyn_uds__fini)
253 60
xyzzy_dyn_uds__fini(struct xyzzy_debug_dyn_uds **udsp)
254
{
255
        struct xyzzy_debug_dyn_uds *uds;
256
257 60
        if (udsp == NULL || *udsp == NULL)
258 48
                return;
259 12
        CHECK_OBJ(*udsp, VMOD_DEBUG_UDS_MAGIC);
260 12
        uds = *udsp;
261 12
        free(uds->vcl_name);
262 12
        AZ(pthread_mutex_destroy(&uds->mtx));
263 12
        FREE_OBJ(uds);
264 12
        *udsp = NULL;
265
}
266
267
VCL_BACKEND v_matchproto_(td_debug_dyn_uds_backend)
268 156
xyzzy_dyn_uds_backend(VRT_CTX, struct xyzzy_debug_dyn_uds *uds)
269
{
270 156
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
271 156
        CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC);
272 156
        AN(uds->dir);
273 156
        return (uds->dir);
274
}
275
276
VCL_VOID v_matchproto_(td_debug_dyn_uds_refresh)
277 60
xyzzy_dyn_uds_refresh(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path)
278
{
279 60
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
280 60
        CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC);
281 60
        (void) dyn_uds_init(ctx, uds, path);
282 60
}