This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvtc_vsm.c
More file actions
294 lines (254 loc) · 6.76 KB
/
vtc_vsm.c
File metadata and controls
294 lines (254 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*-
* Copyright (c) 2023 Varnish Software AS
* All rights reserved.
*
* Author: Dridi Boukelmoune <dridi.boukelmoune@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#ifdef VTEST_WITH_VTC_VSM
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "vapi/vsm.h"
#include "vtc.h"
#include "vav.h"
struct vtc_vsm {
unsigned magic;
#define VTC_VSM_MAGIC 0x5ca77a36
VTAILQ_ENTRY(vtc_vsm) list;
char *name;
char *n_arg;
struct vtclog *vl;
struct vsm *vsm;
};
static VTAILQ_HEAD(, vtc_vsm) vsms = VTAILQ_HEAD_INITIALIZER(vsms);
static struct vtc_vsm *
vsm_new(const char *name)
{
struct vtc_vsm *m;
ALLOC_OBJ(m, VTC_VSM_MAGIC);
AN(m);
REPLACE(m->name, name);
REPLACE(m->n_arg, "${v1_name}");
m->vl = vtc_logopen("%s", name);
VTAILQ_INSERT_TAIL(&vsms, m, list);
return (m);
}
static void
vsm_detach(struct vtc_vsm *m)
{
CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
if (m->vsm == NULL)
vtc_fatal(m->vl, "Cannot detach unattached VSM");
vtc_log(m->vl, 3, "Detaching from VSM");
VSM_Destroy(&m->vsm);
AZ(m->vsm);
}
static void
vsm_attach(struct vtc_vsm *m)
{
struct vsb *n_arg;
CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
if (m->vsm != NULL)
vsm_detach(m);
n_arg = macro_expandf(m->vl, "%s", m->n_arg);
if (n_arg == NULL)
vtc_fatal(m->vl, "Could not expand -n argument");
vtc_log(m->vl, 3, "Attaching to VSM: %s", VSB_data(n_arg));
m->vsm = VSM_New();
AN(m->vsm);
if (VSM_Arg(m->vsm, 'n', VSB_data(n_arg)) <= 0)
vtc_fatal(m->vl, "-n argument error: %s", VSM_Error(m->vsm));
if (VSM_Attach(m->vsm, -1))
vtc_fatal(m->vl, "VSM_Attach: %s", VSM_Error(m->vsm));
VSB_destroy(&n_arg);
}
static void
vsm_delete(struct vtc_vsm *m)
{
CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
if (m->vsm != NULL)
vsm_detach(m);
REPLACE(m->name, NULL);
REPLACE(m->n_arg, NULL);
vtc_logclose(m->vl);
FREE_OBJ(m);
}
#define STATUS_BITS() \
STATUS_BIT(VSM_MGT_RUNNING, mgt-running); \
STATUS_BIT(VSM_MGT_CHANGED, mgt-changed); \
STATUS_BIT(VSM_MGT_RESTARTED, mgt-restarted); \
STATUS_BIT(VSM_WRK_RUNNING, wrk-running); \
STATUS_BIT(VSM_WRK_CHANGED, wrk-changed); \
STATUS_BIT(VSM_WRK_RESTARTED, wrk-restarted);
static void
vsm_expect_status(struct vtc_vsm *m, const char *exp)
{
struct vsb *stat;
const char *sep;
char **av;
unsigned bstat, bexp, bfound;
int argc, i;
CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
if (exp == NULL)
vtc_fatal(m->vl, "Missing expected status");
if (m->vsm == NULL)
vsm_attach(m);
av = VAV_Parse(exp, &argc, ARGV_COMMA|ARGV_NOESC);
AN(av);
bexp = 0;
for (i = 1; i < argc; i++) {
#define STATUS_BIT(b, s) \
if (!strcasecmp(#s, av[i])) { \
bexp |= b; \
continue; \
}
STATUS_BITS()
#undef STATUS_BIT
vtc_fatal(m->vl, "Unknown status bit: %s", av[i]);
}
VAV_Free(av);
bfound = 0;
bstat = VSM_Status(m->vsm);
stat = VSB_new_auto();
AN(stat);
sep = "";
#define STATUS_BIT(b, s) \
if (bstat & b) { \
VSB_cat(stat, sep); \
VSB_cat(stat, #s); \
bfound |= b; \
sep = ","; \
}
STATUS_BITS();
#undef STATUS_BIT
if (bstat != bfound) {
vtc_fatal(m->vl, "VSM status bits not handled: %x",
bstat & ~bfound);
}
if (bstat != bexp) {
AZ(VSB_finish(stat));
vtc_fatal(m->vl, "Expected VSM status '%s' got '%s'",
exp, VSB_data(stat));
}
VSB_destroy(&stat);
vtc_log(m->vl, 4, "Found expected VSM status");
}
/* SECTION: vsm vsm
*
* Interact with the shared memory of a varnish instance.
*
* To define a VSM consumer, use this syntax::
*
* vsm mNAME [-n STRING]
*
* Arguments:
*
* mNAME
* Identify the VSM consumer, it must starts with 'm'.
*
* \-n STRING
* Choose the working directory of the varnish instance. By default
* a VSM consumer connects to ``${v1_name}``.
*
* \-attach
* Attach to a new varnish instance. Implicitly detach from the
* current varnish instance if applicable.
*
* \-detach
* Detach from the current varnish instance.
*
* \-expect-status STRING
* Check that the status of VSM matches the list of status flags from
* STRING. The expected status is represented as a comma-separated
* list of flags. The list of flags in STRING is not sensitive to the
* order of flags.
*
* The available flags are:
*
* - ``mgt-running``
* - ``mgt-changed``
* - ``mgt-restarted``
* - ``wrk-running``
* - ``wrk-changed``
* - ``wrk-restarted``
*
* Expecting a status automatically attaches to the varnish instance
* if that was not already the case.
*/
void
cmd_vsm(CMD_ARGS)
{
struct vtc_vsm *m, *m2;
(void)priv;
if (av == NULL) {
/* Reset and free */
VTAILQ_FOREACH_SAFE(m, &vsms, list, m2) {
CHECK_OBJ_NOTNULL(m, VTC_VSM_MAGIC);
VTAILQ_REMOVE(&vsms, m, list);
vsm_delete(m);
}
return;
}
AZ(strcmp(av[0], "vsm"));
av++;
VTC_CHECK_NAME(vl, av[0], "VSM", 'm');
VTAILQ_FOREACH(m, &vsms, list) {
if (!strcmp(m->name, av[0]))
break;
}
if (m == NULL) {
m = vsm_new(*av);
AN(m);
}
av++;
for (; *av != NULL; av++) {
if (vtc_error)
break;
if (!strcmp(*av, "-attach")) {
vsm_attach(m);
continue;
}
if (!strcmp(*av, "-detach")) {
vsm_detach(m);
continue;
}
if (!strcmp(*av, "-expect-status")) {
vsm_expect_status(m, av[1]);
av++;
continue;
}
if (!strcmp(*av, "-n")) {
if (av[1] == NULL)
vtc_fatal(m->vl, "Missing -n argument");
REPLACE(m->n_arg, av[1]);
av++;
continue;
}
vtc_fatal(vl, "Unknown VSM argument: %s", *av);
}
}
#endif /* VTEST_WITH_VTC_VSM */