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