Code Coverage: vinyl-cache/bin/vinyld/storage/mgt_stevedore.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2007-2011 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Dag-Erling Smørgav <des@des.no>
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
 * STEVEDORE: one who works at or is responsible for loading and
30
 * unloading ships in port.  Example: "on the wharves, stevedores were
31
 * unloading cargo from the far corners of the world." Origin: Spanish
32
 * estibador, from estibar to pack.  First Known Use: 1788
33
 */
34
35
#include "config.h"
36
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <unistd.h>
41
42
#include "mgt/mgt.h"
43
#include "common/heritage.h"
44
#include "vcli_serve.h"
45
46
#include "storage/storage.h"
47
48
VTAILQ_HEAD(stevedore_head, stevedore);
49
50
static struct stevedore_head proto_stevedores =
51
    VTAILQ_HEAD_INITIALIZER(proto_stevedores);
52
53
static struct stevedore_head pre_stevedores =
54
    VTAILQ_HEAD_INITIALIZER(pre_stevedores);
55
56
static struct stevedore_head stevedores =
57
    VTAILQ_HEAD_INITIALIZER(stevedores);
58
59
/* Name of special storages */
60 1026
#define TRANSIENT_STORAGE       "Transient"     // passes
61 1044
#define SYNTH_STORAGE           "Synth"         // vcl_synth resp.body
62
63
struct stevedore *stv_transient;
64
struct stevedore *stv_synth;
65
66
const char *mgt_stv_h2_rxbuf;
67
68
/*--------------------------------------------------------------------*/
69
70
int
71
STV__iter(struct stevedore ** const pp)
72 36663
{
73
74 36663
        AN(pp);
75 36663
        CHECK_OBJ_ORNULL(*pp, STEVEDORE_MAGIC);
76 36663
        if (*pp != NULL)
77 26001
                *pp = VTAILQ_NEXT(*pp, list);
78 10662
        else if (!VTAILQ_EMPTY(&stevedores))
79 7969
                *pp = VTAILQ_FIRST(&stevedores);
80 2693
        else
81 2693
                *pp = VTAILQ_FIRST(&pre_stevedores);
82 36663
        return (*pp != NULL);
83 36663
}
84
85
/*--------------------------------------------------------------------*/
86
87
static void v_matchproto_(cli_func_t)
88
stv_cli_list(struct cli *cli, const char * const *av, void *priv)
89 4
{
90 4
        struct stevedore *stv;
91
92 4
        ASSERT_MGT();
93 4
        (void)av;
94 4
        (void)priv;
95 4
        VCLI_Out(cli, "Storage devices:\n");
96 4
        STV_Foreach(stv)
97 13
                VCLI_Out(cli, "\tstorage.%s = %s\n", stv->ident, stv->name);
98 4
}
99
100
static void v_matchproto_(cli_func_t)
101
stv_cli_list_json(struct cli *cli, const char * const *av, void *priv)
102 1
{
103 1
        struct stevedore *stv;
104 1
        int n = 0;
105
106 1
        (void)priv;
107 1
        ASSERT_MGT();
108 1
        VCLI_JSON_begin(cli, 2, av);
109 1
        VCLI_Out(cli, ",\n");
110 3
        STV_Foreach(stv) {
111 3
                VCLI_Out(cli, "%s", n ? ",\n" : "");
112 3
                n++;
113 3
                VCLI_Out(cli, "{\n");
114 3
                VSB_indent(cli->sb, 2);
115 3
                VCLI_Out(cli, "\"name\": ");
116 3
                VCLI_JSON_str(cli, stv->ident);
117 3
                VCLI_Out(cli, ",\n");
118 3
                VCLI_Out(cli, "\"storage\": ");
119 3
                VCLI_JSON_str(cli, stv->name);
120 3
                VSB_indent(cli->sb, -2);
121 3
                VCLI_Out(cli, "\n}");
122 3
        }
123 1
        VCLI_JSON_end(cli);
124 1
}
125
126
/*--------------------------------------------------------------------*/
127
128
static struct cli_proto cli_stv[] = {
129
        { CLICMD_STORAGE_LIST,          "", stv_cli_list, stv_cli_list_json },
130
        { NULL}
131
};
132
133
/*--------------------------------------------------------------------
134
 */
135
136
#ifdef WITH_PERSISTENT_STORAGE
137
static void v_noreturn_ v_matchproto_(storage_init_f)
138
smp_fake_init(struct stevedore *parent, int ac, char * const *av)
139 1
{
140
141 1
        (void)parent;
142 1
        (void)ac;
143 1
        (void)av;
144 1
        ARGV_ERR(
145 1
            "-spersistent has been deprecated, please see:\n"
146 1
            "  https://vinyl-cache.org/docs/trunk/phk/persistent.html\n"
147 1
            "for details.\n"
148 1
        );
149 1
}
150
151
static const struct stevedore smp_fake_stevedore = {
152
        .magic = STEVEDORE_MAGIC,
153
        .name = "persistent",
154
        .init = smp_fake_init,
155
};
156
#endif
157
158
/*--------------------------------------------------------------------
159
 * Register a stevedore implementation by name.
160
 * VEXTs get to do this first, and since the list is searched front to
161
 * back a VEXT stevedore which inadvisably wants to steal "default" or
162
 * the name of another stevedore implementation can do so.
163
 */
164
165
void
166
STV_Register(const struct stevedore *cstv, const char *altname)
167 7224
{
168 7224
        struct stevedore *stv;
169
170 7224
        CHECK_OBJ_NOTNULL(cstv, STEVEDORE_MAGIC);
171 7224
        ALLOC_OBJ(stv, STEVEDORE_MAGIC);
172 7224
        AN(stv);
173 7224
        *stv = *cstv;
174 7224
        if (altname != NULL)
175 1032
                stv->ident = altname;
176 6192
        else
177 6192
                stv->ident = stv->name;
178 7224
        VTAILQ_INSERT_TAIL(&proto_stevedores, stv, list);
179 7224
}
180
181
static void
182
STV_Register_The_Usual_Suspects(void)
183 1032
{
184 1032
        STV_Register(&smf_stevedore, NULL);
185 1032
        STV_Register(&sma_stevedore, NULL);
186 1032
        STV_Register(&smd_stevedore, NULL);
187 1032
        STV_Register(&ssy_stevedore, NULL);
188 1032
#ifdef WITH_PERSISTENT_STORAGE
189 1032
        STV_Register(&smp_stevedore, NULL);
190 1032
        STV_Register(&smp_fake_stevedore, NULL);
191 1032
#endif
192
#if defined(HAVE_UMEM_H)
193
        STV_Register(&smu_stevedore, NULL);
194
        STV_Register(&smu_stevedore, "default");
195
#else
196 1032
        STV_Register(&sma_stevedore, "default");
197 1032
#endif
198 1032
}
199
200
/*--------------------------------------------------------------------
201
 * Parse a stevedore argument on the form:
202
 *     [ name '=' ] strategy [ ',' arg ] *
203
 */
204
205
void
206
STV_Config(const char *spec)
207 3147
{
208 3147
        char **av, buf[8];
209 3147
        const char *ident;
210 3147
        struct stevedore *stv;
211 3147
        static unsigned seq = 0;
212
213 3147
        av = MGT_NamedArg(spec, &ident, "-s");
214 3147
        AN(av);
215
216 3147
        if (av[1] == NULL)
217 0
                ARGV_ERR("-s argument lacks strategy {malloc, file, ...}\n");
218
219
        /* Append strategy to ident string */
220 3147
        VSB_printf(vident, ",-s%s", av[1]);
221
222 3147
        if (ident == NULL) {
223 1023
                bprintf(buf, "s%u", seq++);
224 1023
                ident = strdup(buf);
225 1023
        }
226
227 3147
        VTAILQ_FOREACH(stv, &pre_stevedores, list)
228 3168
                if (!vstrcmp(stv->ident, ident))
229 3
                        ARGV_ERR("(-s %s) '%s' is already defined\n",
230 3147
                            spec, ident);
231
232 3144
        ALLOC_OBJ(stv, STEVEDORE_MAGIC);
233 3144
        AN(stv);
234 3144
        stv->av = av;
235 3144
        stv->ident = ident;
236 3144
        stv->name = av[1];
237 3144
        VTAILQ_INSERT_TAIL(&pre_stevedores, stv, list);
238 3144
}
239
240
/*--------------------------------------------------------------------*/
241
242
void
243
STV_Config_Final(void)
244 1045
{
245 1045
        unsigned have_transient = 0, have_synth = 0;
246 1045
        struct stevedore *stv;
247 1045
        ASSERT_MGT();
248
249 1045
        VCLS_AddFunc(mgt_cls, MCF_AUTH, cli_stv);
250 1069
        STV_Foreach(stv) {
251 1069
                if (!vstrcmp(stv->ident, TRANSIENT_STORAGE))
252 19
                        have_transient = 1;
253 1050
                else if (!vstrcmp(stv->ident, SYNTH_STORAGE))
254 1
                        have_synth = 1;
255 1069
        }
256
257 1045
        if (! have_transient)
258 1026
                STV_Config(TRANSIENT_STORAGE "=default");
259 1045
        if (! have_synth)
260 1044
                STV_Config(SYNTH_STORAGE "=synth");
261 1045
}
262
263
/*--------------------------------------------------------------------
264
 * Initialize configured stevedores in the worker process
265
 */
266
267
void
268
STV_Init(void)
269 1032
{
270 1032
        char **av;
271 1032
        const char *ident;
272 1032
        struct stevedore *stv;
273 1032
        const struct stevedore *stv2;
274 1032
        int ac;
275
276 1032
        STV_Register_The_Usual_Suspects();
277 4126
        while (!VTAILQ_EMPTY(&pre_stevedores)) {
278 3094
                stv = VTAILQ_FIRST(&pre_stevedores);
279 3094
                VTAILQ_REMOVE(&pre_stevedores, stv, list);
280 3094
                CHECK_OBJ_NOTNULL(stv, STEVEDORE_MAGIC);
281 3094
                AN(stv->av);
282 3094
                av = stv->av;
283 3094
                AN(stv->ident);
284 3094
                ident = stv->ident;
285
286 4185
                for (ac = 0; av[ac + 2] != NULL; ac++)
287 1091
                        continue;
288
289 3094
                VTAILQ_FOREACH(stv2, &proto_stevedores, list)
290 18395
                        if (!vstrcmp(stv2->ident, av[1]))
291 3094
                                break;
292 3094
                if (stv2 == NULL)
293 0
                        ARGV_ERR("Unknown stevedore method \"%s\"\n", av[1]);
294
295 3094
                CHECK_OBJ_NOTNULL(stv2, STEVEDORE_MAGIC);
296 3094
                *stv = *stv2;
297 3094
                AN(stv->name);
298
299 3094
                av += 2;
300
301 3094
                stv->ident = ident;
302 3094
                stv->av = av;
303
304 3094
                if (stv->init != NULL)
305 2069
                        stv->init(stv, ac, av);
306 1025
                else if (ac != 0)
307 0
                        ARGV_ERR("(-s %s) too many arguments\n", stv->name);
308
309 3094
                AN(stv->allocobj);
310 3094
                AN(stv->methods);
311
312 3094
                if (!vstrcmp(stv->ident, TRANSIENT_STORAGE)) {
313 1026
                        AZ(stv_transient);
314 1026
                        stv_transient = stv;
315 2068
                } else if (!vstrcmp(stv->ident, SYNTH_STORAGE)) {
316 1026
                        AZ(stv_synth);
317 1026
                        stv_synth = stv;
318 1026
                } else
319 1042
                        VTAILQ_INSERT_TAIL(&stevedores, stv, list);
320
                /* NB: Do not free av, stevedore gets to keep it */
321 3094
        }
322 1032
        AN(stv_transient);
323 1032
        VTAILQ_INSERT_TAIL(&stevedores, stv_transient, list);
324 1032
        AN(stv_synth);
325 1032
        VTAILQ_INSERT_TAIL(&stevedores, stv_synth, list);
326 1032
}