| | 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 |
VRT_BACKEND_INIT(&vrt); |
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 |
|
VCL_BACKEND retval; |
158 |
640 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
159 |
640 |
CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC); |
160 |
640 |
PTOK(pthread_mutex_lock(&dyn->mtx)); |
161 |
640 |
retval = dyn->dir; |
162 |
640 |
PTOK(pthread_mutex_unlock(&dyn->mtx)); |
163 |
640 |
AN(retval); |
164 |
640 |
return (retval); |
165 |
|
} |
166 |
|
|
167 |
|
VCL_VOID |
168 |
360 |
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 |
360 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
172 |
360 |
CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC); |
173 |
360 |
dyn_dir_init(ctx, dyn, addr, port, probe, via); |
174 |
360 |
} |
175 |
|
|
176 |
|
static int |
177 |
680 |
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 |
680 |
if (path == NULL) { |
185 |
0 |
VRT_fail(ctx, "path is NULL"); |
186 |
0 |
return (-1); |
187 |
|
} |
188 |
680 |
if (! VUS_is(path)) { |
189 |
80 |
VRT_fail(ctx, "path must be an absolute path: %s", path); |
190 |
80 |
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 |
600 |
errno = 0; |
198 |
600 |
if (stat(path, &st) != 0) { |
199 |
40 |
VRT_fail(ctx, "Cannot stat path %s: %s", path, strerror(errno)); |
200 |
40 |
return (-1); |
201 |
|
} |
202 |
560 |
if (!S_ISSOCK(st.st_mode)) { |
203 |
40 |
VRT_fail(ctx, "%s is not a socket", path); |
204 |
40 |
return (-1); |
205 |
|
} |
206 |
|
|
207 |
520 |
INIT_OBJ(&vep, VRT_ENDPOINT_MAGIC); |
208 |
520 |
VRT_BACKEND_INIT(&vrt); |
209 |
520 |
vrt.endpoint = &vep; |
210 |
520 |
vep.uds_path = path; |
211 |
520 |
vrt.vcl_name = uds->vcl_name; |
212 |
520 |
vrt.hosthdr = "localhost"; |
213 |
520 |
vep.ipv4 = NULL; |
214 |
520 |
vep.ipv6 = NULL; |
215 |
|
|
216 |
|
// we support via: uds -> ip, but not via: ip -> uds |
217 |
520 |
if ((dir = VRT_new_backend(ctx, &vrt, NULL)) == NULL) |
218 |
0 |
return (-1); |
219 |
|
|
220 |
520 |
PTOK(pthread_mutex_lock(&uds->mtx)); |
221 |
520 |
dir2 = uds->dir; |
222 |
520 |
uds->dir = dir; |
223 |
520 |
PTOK(pthread_mutex_unlock(&uds->mtx)); |
224 |
|
|
225 |
520 |
if (dir2 != NULL) |
226 |
200 |
VRT_delete_backend(ctx, &dir2); |
227 |
520 |
return (0); |
228 |
680 |
} |
229 |
|
|
230 |
|
VCL_VOID v_matchproto_(td_xyzzy_debug_dyn_uds__init) |
231 |
480 |
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 |
480 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
237 |
480 |
AN(udsp); |
238 |
480 |
AZ(*udsp); |
239 |
480 |
AN(vcl_name); |
240 |
|
|
241 |
480 |
ALLOC_OBJ(uds, VMOD_DEBUG_UDS_MAGIC); |
242 |
480 |
AN(uds); |
243 |
480 |
REPLACE(uds->vcl_name, vcl_name); |
244 |
480 |
PTOK(pthread_mutex_init(&uds->mtx, NULL)); |
245 |
|
|
246 |
480 |
if (dyn_uds_init(ctx, uds, path) != 0) { |
247 |
160 |
free(uds->vcl_name); |
248 |
160 |
PTOK(pthread_mutex_destroy(&uds->mtx)); |
249 |
160 |
FREE_OBJ(uds); |
250 |
160 |
return; |
251 |
|
} |
252 |
|
|
253 |
320 |
*udsp = uds; |
254 |
480 |
} |
255 |
|
|
256 |
|
VCL_VOID v_matchproto_(td_debug_dyn_uds__fini) |
257 |
40 |
xyzzy_dyn_uds__fini(struct xyzzy_debug_dyn_uds **udsp) |
258 |
|
{ |
259 |
|
struct xyzzy_debug_dyn_uds *uds; |
260 |
|
|
261 |
40 |
TAKE_OBJ_NOTNULL(uds, udsp, VMOD_DEBUG_UDS_MAGIC); |
262 |
40 |
free(uds->vcl_name); |
263 |
40 |
PTOK(pthread_mutex_destroy(&uds->mtx)); |
264 |
40 |
FREE_OBJ(uds); |
265 |
40 |
} |
266 |
|
|
267 |
|
VCL_BACKEND v_matchproto_(td_debug_dyn_uds_backend) |
268 |
520 |
xyzzy_dyn_uds_backend(VRT_CTX, struct xyzzy_debug_dyn_uds *uds) |
269 |
|
{ |
270 |
520 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
271 |
520 |
CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC); |
272 |
520 |
AN(uds->dir); |
273 |
520 |
return (uds->dir); |
274 |
|
} |
275 |
|
|
276 |
|
VCL_VOID v_matchproto_(td_debug_dyn_uds_refresh) |
277 |
200 |
xyzzy_dyn_uds_refresh(VRT_CTX, struct xyzzy_debug_dyn_uds *uds, VCL_STRING path) |
278 |
|
{ |
279 |
200 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
280 |
200 |
CHECK_OBJ_NOTNULL(uds, VMOD_DEBUG_UDS_MAGIC); |
281 |
200 |
(void) dyn_uds_init(ctx, uds, path); |
282 |
200 |
} |