varnish-cache/bin/varnishd/storage/mgt_stevedore.c
1
/*-
2
 * Copyright (c) 2007-2011 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Dag-Erling Smørgav <des@des.no>
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
 * STEVEDORE: one who works at or is responsible for loading and
29
 * unloading ships in port.  Example: "on the wharves, stevedores were
30
 * unloading cargo from the far corners of the world." Origin: Spanish
31
 * estibador, from estibar to pack.  First Known Use: 1788
32
 */
33
34
#include "config.h"
35
36
#include <stdio.h>
37
#include <stdlib.h>
38
#include <string.h>
39
#include <unistd.h>
40
41
#include "mgt/mgt.h"
42
#include "common/heritage.h"
43
#include "vcli_serve.h"
44
45
#include "storage/storage.h"
46
47
static VTAILQ_HEAD(, stevedore) stevedores =
48
    VTAILQ_HEAD_INITIALIZER(stevedores);
49
50
/* Name of transient storage */
51
#define TRANSIENT_STORAGE       "Transient"
52
53
struct stevedore *stv_transient;
54
55
/*--------------------------------------------------------------------*/
56
57
int
58 17048
STV__iter(struct stevedore ** const pp)
59
{
60
61 17048
        AN(pp);
62 17048
        CHECK_OBJ_ORNULL(*pp, STEVEDORE_MAGIC);
63 17048
        if (*pp != NULL)
64 10325
                *pp = VTAILQ_NEXT(*pp, list);
65
        else
66 6723
                *pp = VTAILQ_FIRST(&stevedores);
67 17048
        return (*pp != NULL);
68
}
69
70
/*--------------------------------------------------------------------*/
71
72
static void v_matchproto_(cli_func_t)
73 4
stv_cli_list(struct cli *cli, const char * const *av, void *priv)
74
{
75
        struct stevedore *stv;
76
77 4
        ASSERT_MGT();
78
        (void)av;
79
        (void)priv;
80 4
        VCLI_Out(cli, "Storage devices:\n");
81 17
        STV_Foreach(stv)
82 9
                VCLI_Out(cli, "\tstorage.%s = %s\n", stv->ident, stv->name);
83 4
}
84
85
/*--------------------------------------------------------------------*/
86
87
static struct cli_proto cli_stv[] = {
88
        { CLICMD_STORAGE_LIST,          "", stv_cli_list },
89
        { NULL}
90
};
91
92
/*--------------------------------------------------------------------
93
 */
94
95
static void v_matchproto_(storage_init_f)
96 2
smp_fake_init(struct stevedore *parent, int ac, char * const *av)
97
{
98
99
        (void)parent;
100
        (void)ac;
101
        (void)av;
102 2
        ARGV_ERR(
103
            "-spersistent has been deprecated, please see:\n"
104
            "  https://www.varnish-cache.org/docs/trunk/phk/persistent.html\n"
105
            "for details.\n"
106
        );
107
}
108
109
static const struct stevedore smp_fake_stevedore = {
110
        .magic = STEVEDORE_MAGIC,
111
        .name = "deprecated_persistent",
112
        .init = smp_fake_init,
113
};
114
115
/*--------------------------------------------------------------------
116
 * Parse a stevedore argument on the form:
117
 *      [ name '=' ] strategy [ ',' arg ] *
118
 */
119
120
static const struct choice STV_choice[] = {
121
        { "file",                       &smf_stevedore },
122
        { "malloc",                     &sma_stevedore },
123
        { "deprecated_persistent",      &smp_stevedore },
124
        { "persistent",                 &smp_fake_stevedore },
125
#if defined(HAVE_LIBUMEM)
126
        { "umem",                       &smu_stevedore },
127
        { "default",                    &smu_stevedore },
128
#else
129
        { "default",                    &sma_stevedore },
130
#endif
131
        { NULL,         NULL }
132
};
133
134
static void
135 1272
stv_check_ident(const char *spec, const char *ident)
136
{
137
        struct stevedore *stv;
138 1272
        unsigned found = 0;
139
140 1272
        if (!strcmp(ident, TRANSIENT_STORAGE))
141 630
                found = (stv_transient != NULL);
142
        else {
143 1303
                STV_Foreach(stv)
144 21
                        if (!strcmp(stv->ident, ident)) {
145 2
                                found = 1;
146 2
                                break;
147
                        }
148
        }
149
150 1272
        if (found)
151 3
                ARGV_ERR("(-s %s) '%s' is already defined\n", spec, ident);
152 1269
}
153
154
void
155 1274
STV_Config(const char *spec)
156
{
157
        char **av, buf[8];
158
        const char *name;
159
        struct stevedore *stv;
160
        const struct stevedore *stv2;
161
        int ac;
162
        static unsigned seq = 0;
163
164 1274
        av = MGT_NamedArg(spec, &name, "-s");
165 1272
        AN(av);
166
167 1272
        if (av[1] == NULL)
168 0
                ARGV_ERR("-s argument lacks strategy {malloc, file, ...}\n");
169
170 1942
        for (ac = 0; av[ac + 2] != NULL; ac++)
171 670
                continue;
172
173 1272
        stv2 = MGT_Pick(STV_choice, av[1], "storage");
174 1272
        AN(stv2);
175
176
        /* Append strategy to ident string */
177 1272
        VSB_printf(vident, ",-s%s", av[1]);
178
179 1272
        av += 2;
180
181 1272
        CHECK_OBJ_NOTNULL(stv2, STEVEDORE_MAGIC);
182 1272
        ALLOC_OBJ(stv, STEVEDORE_MAGIC);
183 1272
        AN(stv);
184
185 1272
        *stv = *stv2;
186 1272
        AN(stv->name);
187
188 1272
        if (name == NULL) {
189 624
                bprintf(buf, "s%u", seq++);
190 624
                name = buf;
191
        }
192
193 1272
        stv->ident = strdup(name);
194 1272
        AN(stv->ident);
195 1272
        stv_check_ident(spec, stv->ident);
196
197 1269
        if (stv->init != NULL)
198 1269
                stv->init(stv, ac, av);
199 0
        else if (ac != 0)
200 0
                ARGV_ERR("(-s %s) too many arguments\n", stv->name);
201
202 1267
        AN(stv->allocobj);
203 1267
        AN(stv->methods);
204
205 1267
        if (!strcmp(stv->ident, TRANSIENT_STORAGE)) {
206 629
                AZ(stv_transient);
207 629
                stv_transient = stv;
208
        } else
209 638
                VTAILQ_INSERT_TAIL(&stevedores, stv, list);
210
        /* NB: Do not free av, stevedore gets to keep it */
211 1267
}
212
213
/*--------------------------------------------------------------------*/
214
215
void
216 628
STV_Config_Transient(void)
217
{
218
219 628
        ASSERT_MGT();
220
221 628
        VCLS_AddFunc(mgt_cls, MCF_AUTH, cli_stv);
222 628
        if (stv_transient == NULL)
223 617
                STV_Config(TRANSIENT_STORAGE "=default");
224 628
        AN(stv_transient);
225 628
        VTAILQ_INSERT_TAIL(&stevedores, stv_transient, list);
226 628
}