varnish-cache/bin/varnishd/mgt/mgt_param.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2011 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
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 <ctype.h>
33
#include <stdarg.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <unistd.h>
38
39
#include "mgt/mgt.h"
40
#include "common/heritage.h"
41
42
#include "mgt/mgt_param.h"
43
#include "vav.h"
44
#include "vcli_serve.h"
45
46
#if !defined(__has_feature)
47
#define __has_feature(x)        0
48
#endif
49
50
struct plist {
51
        unsigned                        magic;
52
#define PLIST_MAGIC                     0xbfc3ea16
53
        VTAILQ_ENTRY(plist)             list;
54
        struct parspec                  *spec;
55
};
56
57
static VTAILQ_HEAD(, plist)             phead = VTAILQ_HEAD_INITIALIZER(phead);
58
59
struct params mgt_param;
60
static const int margin1 = 8;
61
static int margin2 = 0;
62
static const int wrap_at = 72;
63
static const int tab0 = 3;
64
65
/*--------------------------------------------------------------------*/
66
67
static const char OBJ_STICKY_TEXT[] =
68
        "\n\n"
69
        "NB: This parameter is evaluated only when objects are created. "
70
        "To change it for all objects, restart or ban everything.";
71
72
static const char DELAYED_EFFECT_TEXT[] =
73
        "\n\n"
74
        "NB: This parameter may take quite some time to take (full) effect.";
75
76
static const char MUST_RESTART_TEXT[] =
77
        "\n\n"
78
        "NB: This parameter will not take any effect until the "
79
        "child process has been restarted.";
80
81
static const char MUST_RELOAD_TEXT[] =
82
        "\n\n"
83
        "NB: This parameter will not take any effect until the "
84
        "VCL programs have been reloaded.";
85
86
static const char EXPERIMENTAL_TEXT[] =
87
        "\n\n"
88
        "NB: We do not know yet if it is a good idea to change "
89
        "this parameter, or if the default value is even sensible. "
90
        "Caution is advised, and feedback is most welcome.";
91
92
static const char WIZARD_TEXT[] =
93
        "\n\n"
94
        "NB: Do not change this parameter, unless a developer tell "
95
        "you to do so.";
96
97
static const char PROTECTED_TEXT[] =
98
        "\n\n"
99
        "NB: This parameter is protected and can not be changed.";
100
101
static const char ONLY_ROOT_TEXT[] =
102
        "\n\n"
103
        "NB: This parameter only works if varnishd is run as root.";
104
105
static const char NOT_IMPLEMENTED_TEXT[] =
106
        "This parameter depends on a feature which is not available"
107
        " on this platform.";
108
109
static const char NOT_IMPLEMENTED_DOC[] =
110
        "NB: This parameter depends on a feature which is not available"
111
        " on all platforms.";
112
113
/*--------------------------------------------------------------------*/
114
115
static struct parspec *
116 29801
mcf_findpar(const char *name)
117
{
118
        struct plist *pl;
119
120 29801
        AN(name);
121 2425431
        VTAILQ_FOREACH(pl, &phead, list)
122 2425427
                if (!strcmp(pl->spec->name, name))
123 29797
                        return (pl->spec);
124 4
        return (NULL);
125 29801
}
126
127
static void
128 87360
mcf_addpar(struct parspec *ps)
129
{
130
        struct plist *pl, *pl2;
131
        int i;
132
133 87360
        ALLOC_OBJ(pl, PLIST_MAGIC);
134 87360
        AN(pl);
135 87360
        pl->spec = ps;
136 3758144
        VTAILQ_FOREACH(pl2, &phead, list) {
137 3719040
                i = strcmp(pl2->spec->name, pl->spec->name);
138 3719040
                if (i == 0) {
139 0
                        fprintf(stderr, "Duplicate param: %s\n", ps->name);
140 0
                        exit(4);
141 3719040
                } else if (i > 0) {
142 48256
                        VTAILQ_INSERT_BEFORE(pl2, pl, list);
143 48256
                        return;
144
                }
145 3670784
        }
146 39104
        VTAILQ_INSERT_TAIL(&phead, pl, list);
147 87360
}
148
149
/*--------------------------------------------------------------------
150
 * Wrap the text nicely.
151
 * Lines are allowed to contain two TABS and we render that as a table
152
 * taking the width of the first column into account.
153
 */
154
155
static void
156 1293
mcf_wrap_line(struct cli *cli, const char *b, const char *e, int tabs, int m0)
157
{
158 1293
        int n, hadtabs = 0;
159
        const char *w;
160
161 1293
        n = m0;
162 1293
        VCLI_Out(cli, "%*s", n, "");
163
164 19194
        while (b < e) {
165 18502
                if (!isspace(*b)) {
166 8914
                        VCLI_Out(cli, "%c", *b);
167 8914
                        b++;
168 8914
                        n++;
169 18502
                } else if (*b == '\t') {
170 304
                        assert(tabs);
171 304
                        assert(hadtabs < 2);
172 304
                        do {
173 2348
                                VCLI_Out(cli, " ");
174 2348
                                n++;
175 2348
                        } while ((n % tabs) != (m0 + tab0) % tabs);
176 304
                        b++;
177 304
                        hadtabs++;
178 304
                } else {
179 9284
                        assert (*b == ' ');
180 54814
                        for (w = b + 1; w < e; w++)
181 54122
                                if (isspace(*w))
182 8592
                                        break;
183 9284
                        if (n + (w - b) < wrap_at) {
184 8683
                                VCLI_Out(cli, "%.*s", (int)(w - b), b);
185 8683
                                n += (w - b);
186 8683
                                b = w;
187 8683
                        } else {
188 601
                                assert(hadtabs == 0 || hadtabs == 2);
189 601
                                VCLI_Out(cli, "\n");
190 1202
                                mcf_wrap_line(cli, b + 1, e, 0,
191 601
                                    hadtabs ? m0 + tab0 + tabs : m0);
192 601
                                return;
193
                        }
194
                }
195
        }
196 692
        assert(b == e);
197 1293
}
198
199
static void
200 330
mcf_wrap(struct cli *cli, const char *text)
201
{
202
        const char *p, *q, *r;
203 330
        int tw = 0;
204
205 330
        if (strchr(text, '\t') != NULL) {
206 172
                for (p = text; *p != '\0'; ) {
207 172
                        q = strstr(p, "\n\t");
208 172
                        if (q == NULL)
209 20
                                break;
210 152
                        q += 2;
211 152
                        r = strchr(q, '\t');
212 152
                        if (r == NULL) {
213 0
                                fprintf(stderr,
214 0
                                    "LINE with just one TAB: <%s>\n", text);
215 0
                                exit(4);
216
                        }
217 152
                        if (r - q > tw)
218 48
                                tw = r - q;
219 152
                        p = q;
220
                }
221 20
                tw += 2;
222 20
                if (tw < 20)
223 16
                        tw = 20;
224 20
        }
225
226 1698
        for (p = text; *p != '\0'; ) {
227 1368
                if (*p == '\n') {
228 676
                        VCLI_Out(cli, "\n");
229 676
                        p++;
230 676
                        continue;
231
                }
232 692
                q = strchr(p, '\n');
233 692
                if (q == NULL)
234 330
                        q = strchr(p, '\0');
235 692
                mcf_wrap_line(cli, p, q, tw, margin1);
236 692
                p = q;
237
        }
238 330
}
239
240
/*--------------------------------------------------------------------*/
241
242
static void v_matchproto_(cli_func_t)
243 25
mcf_param_show(struct cli *cli, const char * const *av, void *priv)
244
{
245
        int n;
246
        struct plist *pl;
247
        const struct parspec *pp;
248 25
        int lfmt = 0, chg = 0;
249
        struct vsb *vsb;
250
251 25
        vsb = VSB_new_auto();
252 25
        (void)priv;
253
254 25
        if (av[2] != NULL && !strcmp(av[2], "changed"))
255 1
                chg = 1;
256 24
        else if (av[2] != NULL)
257 19
                lfmt = 1;
258
259 25
        n = 0;
260 2650
        VTAILQ_FOREACH(pl, &phead, list) {
261 2625
                pp = pl->spec;
262 2625
                if (lfmt && strcmp(pp->name, av[2]) && strcmp("-l", av[2]))
263 1769
                        continue;
264 856
                n++;
265
266 856
                VSB_clear(vsb);
267 856
                if (pp->func(vsb, pp, NULL))
268 0
                        VCLI_SetResult(cli, CLIS_PARAM);
269 856
                AZ(VSB_finish(vsb));
270 856
                if (chg && pp->def != NULL && !strcmp(pp->def, VSB_data(vsb)))
271 96
                        continue;
272
273 760
                if (pp->flags & NOT_IMPLEMENTED) {
274 7
                        if (lfmt) {
275 2
                                VCLI_Out(cli, "%s\n", pp->name);
276 2
                                VCLI_Out(cli, "%-*sNot available", margin1, " ");
277 2
                        } else {
278 5
                                VCLI_Out(cli, "%-*s-", margin2, pp->name);
279
                        }
280 7
                } else {
281 753
                        if (lfmt) {
282 224
                                VCLI_Out(cli, "%s\n", pp->name);
283 224
                                VCLI_Out(cli, "%-*sValue is: ", margin1, " ");
284 224
                        } else {
285 529
                                VCLI_Out(cli, "%-*s", margin2, pp->name);
286
                        }
287
288 753
                        VCLI_Out(cli, "%s", VSB_data(vsb));
289 753
                        if (pp->units != NULL && *pp->units != '\0')
290 616
                                VCLI_Out(cli, " [%s]", pp->units);
291 753
                        if (pp->def != NULL && !strcmp(pp->def, VSB_data(vsb)))
292 679
                                VCLI_Out(cli, " (default)");
293
                }
294 760
                VCLI_Out(cli, "\n");
295
296 760
                if (lfmt && pp->flags & NOT_IMPLEMENTED) {
297 2
                        VCLI_Out(cli, "\n");
298 2
                        mcf_wrap(cli, NOT_IMPLEMENTED_TEXT);
299 2
                        VCLI_Out(cli, "\n\n");
300 760
                } else if (lfmt) {
301 224
                        if (pp->def != NULL && strcmp(pp->def, VSB_data(vsb)))
302 52
                                VCLI_Out(cli, "%-*sDefault is: %s\n",
303 26
                                    margin1, "", pp->def);
304 224
                        if (pp->min != NULL)
305 344
                                VCLI_Out(cli, "%-*sMinimum is: %s\n",
306 172
                                    margin1, "", pp->min);
307 224
                        if (pp->max != NULL)
308 100
                                VCLI_Out(cli, "%-*sMaximum is: %s\n",
309 50
                                    margin1, "", pp->max);
310 224
                        VCLI_Out(cli, "\n");
311 224
                        mcf_wrap(cli, pp->descr);
312 224
                        if (pp->flags & OBJ_STICKY)
313 7
                                mcf_wrap(cli, OBJ_STICKY_TEXT);
314 224
                        if (pp->flags & DELAYED_EFFECT)
315 26
                                mcf_wrap(cli, DELAYED_EFFECT_TEXT);
316 224
                        if (pp->flags & EXPERIMENTAL)
317 50
                                mcf_wrap(cli, EXPERIMENTAL_TEXT);
318 224
                        if (pp->flags & MUST_RELOAD)
319 2
                                mcf_wrap(cli, MUST_RELOAD_TEXT);
320 224
                        if (pp->flags & MUST_RESTART)
321 10
                                mcf_wrap(cli, MUST_RESTART_TEXT);
322 224
                        if (pp->flags & WIZARD)
323 8
                                mcf_wrap(cli, WIZARD_TEXT);
324 224
                        if (pp->flags & PROTECTED)
325 1
                                mcf_wrap(cli, PROTECTED_TEXT);
326 224
                        if (pp->flags & ONLY_ROOT)
327 0
                                mcf_wrap(cli, ONLY_ROOT_TEXT);
328 224
                        VCLI_Out(cli, "\n\n");
329 224
                }
330 760
        }
331 25
        if (av[2] != NULL && lfmt && strcmp(av[2], "-l") && n == 0) {
332 1
                VCLI_SetResult(cli, CLIS_PARAM);
333 1
                VCLI_Out(cli, "Unknown parameter \"%s\".", av[2]);
334 1
        }
335 25
        VSB_destroy(&vsb);
336 25
}
337
338
static inline void
339 549
mcf_json_key_valstr(struct cli *cli, const char *key, const char *val)
340
{
341 549
        VCLI_Out(cli, "\"%s\": ", key);
342 549
        VCLI_JSON_str(cli, val);
343 549
        VCLI_Out(cli, ",\n");
344 549
}
345
346
static void v_matchproto_(cli_func_t)
347 6
mcf_param_show_json(struct cli *cli, const char * const *av, void *priv)
348
{
349 6
        int n, comma = 0;
350
        struct plist *pl;
351
        const struct parspec *pp;
352 6
        int chg = 0, flags;
353
        struct vsb *vsb, *def;
354 6
        const char *show = NULL;
355
356 6
        (void)priv;
357
358 16
        for (int i = 2; av[i] != NULL; i++) {
359 11
                if (strcmp(av[i], "-l") == 0) {
360 1
                        VCLI_SetResult(cli, CLIS_PARAM);
361 1
                        VCLI_Out(cli, "-l not permitted with param.show -j");
362 1
                        return;
363
                }
364 10
                if (strcmp(av[i], "changed") == 0) {
365 1
                        chg = 1;
366 1
                        continue;
367
                }
368 9
                if (strcmp(av[i], "-j") == 0)
369 6
                        continue;
370 3
                show = av[i];
371 3
        }
372
373 5
        vsb = VSB_new_auto();
374 5
        def = VSB_new_auto();
375
376 5
        n = 0;
377 5
        VCLI_JSON_begin(cli, 2, av);
378 5
        VCLI_Out(cli, ",\n");
379 530
        VTAILQ_FOREACH(pl, &phead, list) {
380 525
                pp = pl->spec;
381 525
                if (show != NULL && strcmp(pp->name, show) != 0)
382 313
                        continue;
383 212
                n++;
384
385 212
                VSB_clear(vsb);
386 212
                if (pp->func(vsb, pp, JSON_FMT))
387 0
                        VCLI_SetResult(cli, CLIS_PARAM);
388 212
                AZ(VSB_finish(vsb));
389 212
                VSB_clear(def);
390 212
                if (pp->func(def, pp, NULL))
391 0
                        VCLI_SetResult(cli, CLIS_PARAM);
392 212
                AZ(VSB_finish(def));
393 212
                if (chg && pp->def != NULL && !strcmp(pp->def, VSB_data(def)))
394 96
                        continue;
395
396 116
                VCLI_Out(cli, "%s", comma ? ",\n" : "");
397 116
                comma++;
398 116
                VCLI_Out(cli, "{\n");
399 116
                VSB_indent(cli->sb, 2);
400 116
                mcf_json_key_valstr(cli, "name", pp->name);
401 116
                if (pp->flags & NOT_IMPLEMENTED) {
402 1
                        VCLI_Out(cli, "\"implemented\": false\n");
403 1
                        VSB_indent(cli->sb, -2);
404 1
                        VCLI_Out(cli, "}");
405 1
                        continue;
406
                }
407 115
                VCLI_Out(cli, "\"implemented\": true,\n");
408 115
                VCLI_Out(cli, "\"value\": %s,\n", VSB_data(vsb));
409 115
                if (pp->units != NULL && *pp->units != '\0')
410 92
                        mcf_json_key_valstr(cli, "units", pp->units);
411
412 115
                if (pp->def != NULL)
413 115
                        mcf_json_key_valstr(cli, "default", pp->def);
414 115
                if (pp->min != NULL)
415 84
                        mcf_json_key_valstr(cli, "minimum", pp->min);
416 115
                if (pp->max != NULL)
417 27
                        mcf_json_key_valstr(cli, "maximum", pp->max);
418 115
                mcf_json_key_valstr(cli, "description", pp->descr);
419
420 115
                flags = 0;
421 115
                VCLI_Out(cli, "\"flags\": [\n");
422 115
                VSB_indent(cli->sb, 2);
423
424
#define flag_out(flag, string) do {                                     \
425
                        if (pp->flags & flag) {                         \
426
                                if (flags)                              \
427
                                        VCLI_Out(cli, ",\n");           \
428
                                VCLI_Out(cli, "\"%s\"", #string);       \
429
                                flags++;                                \
430
                        }                                               \
431
                } while(0)
432
433 115
                flag_out(OBJ_STICKY, obj_sticky);
434 115
                flag_out(DELAYED_EFFECT, delayed_effect);
435 115
                flag_out(EXPERIMENTAL, experimental);
436 115
                flag_out(MUST_RELOAD, must_reload);
437 115
                flag_out(MUST_RESTART, must_restart);
438 115
                flag_out(WIZARD, wizard);
439 115
                flag_out(PROTECTED, protected);
440 115
                flag_out(ONLY_ROOT, only_root);
441
442
#undef flag_out
443
444 115
                VSB_indent(cli->sb, -2);
445 115
                VCLI_Out(cli, "\n]");
446 115
                VSB_indent(cli->sb, -2);
447 115
                VCLI_Out(cli, "\n}");
448 115
        }
449 5
        VCLI_JSON_end(cli);
450 5
        if (show != NULL && n == 0) {
451 1
                VSB_clear(cli->sb);
452 1
                VCLI_SetResult(cli, CLIS_PARAM);
453 1
                VCLI_Out(cli, "Unknown parameter \"%s\".", show);
454 1
        }
455 5
        VSB_destroy(&vsb);
456 5
        VSB_destroy(&def);
457 6
}
458
459
/*--------------------------------------------------------------------
460
 * Mark parameters as protected
461
 */
462
463
void
464 1
MCF_ParamProtect(struct cli *cli, const char *args)
465
{
466
        char **av;
467
        struct parspec *pp;
468
        int i;
469
470 1
        av = VAV_Parse(args, NULL, ARGV_COMMA);
471 1
        if (av[0] != NULL) {
472 0
                VCLI_Out(cli, "Parse error: %s", av[0]);
473 0
                VCLI_SetResult(cli, CLIS_PARAM);
474 0
                VAV_Free(av);
475 0
                return;
476
        }
477 2
        for (i = 1; av[i] != NULL; i++) {
478 1
                pp = mcf_findpar(av[i]);
479 1
                if (pp == NULL) {
480 0
                        VCLI_Out(cli, "Unknown parameter %s", av[i]);
481 0
                        VCLI_SetResult(cli, CLIS_PARAM);
482 0
                        VAV_Free(av);
483 0
                        return;
484
                }
485 1
                pp->flags |= PROTECTED;
486 1
        }
487 1
        VAV_Free(av);
488 1
}
489
490
/*--------------------------------------------------------------------*/
491
492
void
493 6552
MCF_ParamSet(struct cli *cli, const char *param, const char *val)
494
{
495
        const struct parspec *pp;
496
497 6552
        pp = mcf_findpar(param);
498 6552
        if (pp == NULL) {
499 4
                VCLI_SetResult(cli, CLIS_PARAM);
500 4
                VCLI_Out(cli, "Unknown parameter \"%s\".", param);
501 4
                return;
502
        }
503 6548
        if (pp->flags & PROTECTED) {
504 1
                VCLI_SetResult(cli, CLIS_AUTH);
505 1
                VCLI_Out(cli, "parameter \"%s\" is protected.", param);
506 1
                return;
507
        }
508 6547
        if (!val)
509 10
                val = pp->def;
510 6547
        if (pp->func(cli->sb, pp, val))
511 25
                VCLI_SetResult(cli, CLIS_PARAM);
512
513 6547
        if (cli->result == CLIS_OK && heritage.param != NULL)
514 251
                *heritage.param = mgt_param;
515
516 6547
        if (cli->result != CLIS_OK) {
517 50
                VCLI_Out(cli, "\n(attempting to set param '%s' to '%s')",
518 25
                    pp->name, val);
519 6547
        } else if (MCH_Running() && pp->flags & MUST_RESTART) {
520 0
                VCLI_Out(cli,
521
                    "\nChange will take effect when child is restarted");
522 6522
        } else if (pp->flags & MUST_RELOAD) {
523 0
                VCLI_Out(cli,
524
                    "\nChange will take effect when VCL script is reloaded");
525 0
        }
526 6552
}
527
528
529
/*--------------------------------------------------------------------*/
530
531
static void v_matchproto_(cli_func_t)
532 355
mcf_param_set(struct cli *cli, const char * const *av, void *priv)
533
{
534
535 355
        (void)priv;
536 355
        MCF_ParamSet(cli, av[2], av[3]);
537 355
}
538
539
/*--------------------------------------------------------------------*/
540
541
static void v_matchproto_(cli_func_t)
542 10
mcf_param_reset(struct cli *cli, const char * const *av, void *priv)
543
{
544
545 10
        (void)priv;
546 10
        MCF_ParamSet(cli, av[2], NULL);
547 10
}
548
549
/*--------------------------------------------------------------------
550
 * Add a group of parameters to the global set and sort by name.
551
 */
552
553
void
554 2496
MCF_AddParams(struct parspec *ps)
555
{
556
        struct parspec *pp;
557
        const char *s;
558
559 89856
        for (pp = ps; pp->name != NULL; pp++) {
560 87360
                AN(pp->func);
561 87360
                s = strchr(pp->descr, '\0');
562 87360
                if (isspace(s[-1])) {
563 0
                        fprintf(stderr,
564 0
                            "Param->descr has trailing space: %s\n", pp->name);
565 0
                        exit(4);
566
                }
567 87360
                mcf_addpar(pp);
568 87360
                if (strlen(pp->name) + 1L > margin2)
569 4160
                        margin2 = strlen(pp->name) + 1;
570 87360
        }
571 2496
}
572
573
/*--------------------------------------------------------------------
574
 * Wash a min/max/default value
575
 */
576
577
static void
578 174720
mcf_wash_param(struct cli *cli, const struct parspec *pp, const char **val,
579
    const char *name, struct vsb *vsb)
580
{
581
        int err;
582
583 174720
        AN(*val);
584 174720
        VSB_clear(vsb);
585 349440
        VSB_printf(vsb, "FAILED to set %s for param %s: %s\n",
586 174720
            name, pp->name, *val);
587 174720
        err = pp->func(vsb, pp, *val);
588 174720
        AZ(VSB_finish(vsb));
589 174720
        if (err) {
590 0
                VCLI_Out(cli, "%s\n", VSB_data(vsb));
591 0
                VCLI_SetResult(cli, CLIS_CANT);
592 0
                return;
593
        }
594 174720
        VSB_clear(vsb);
595 174720
        err = pp->func(vsb, pp, NULL);
596 174720
        AZ(err);
597 174720
        AZ(VSB_finish(vsb));
598 174720
        if (strcmp(*val, VSB_data(vsb))) {
599 32448
                *val = strdup(VSB_data(vsb));
600 32448
                AN(*val);
601 32448
        }
602 174720
}
603
604
/*--------------------------------------------------------------------*/
605
606
static struct cli_proto cli_params[] = {
607
        { CLICMD_PARAM_SHOW,            "", mcf_param_show,
608
          mcf_param_show_json },
609
        { CLICMD_PARAM_SET,             "", mcf_param_set },
610
        { CLICMD_PARAM_RESET,           "", mcf_param_reset },
611
        { NULL }
612
};
613
614
/*--------------------------------------------------------------------
615
 * Configure the parameters
616
 */
617
618
void
619 832
MCF_InitParams(struct cli *cli)
620
{
621
        struct plist *pl;
622
        struct parspec *pp;
623
        struct vsb *vsb;
624
        ssize_t def, low;
625
626 832
        MCF_AddParams(mgt_parspec);
627 832
        MCF_AddParams(WRK_parspec);
628 832
        MCF_AddParams(VSL_parspec);
629
630 832
        MCF_TcpParams();
631
632 832
        def = 56 * 1024;
633
634
        if (sizeof(void *) < 8) {               /*lint !e506 !e774  */
635
                /*
636
                 * Adjust default parameters for 32 bit systems to conserve
637
                 * VM space.
638
                 *
639
                 * Reflect changes in doc/sphinx/reference/varnishd.rst !
640
                 */
641
                MCF_ParamConf(MCF_DEFAULT, "workspace_client", "24k");
642
                MCF_ParamConf(MCF_DEFAULT, "workspace_backend", "20k");
643
                MCF_ParamConf(MCF_DEFAULT, "http_resp_size", "8k");
644
                MCF_ParamConf(MCF_DEFAULT, "http_req_size", "12k");
645
                MCF_ParamConf(MCF_DEFAULT, "gzip_buffer", "4k");
646
                MCF_ParamConf(MCF_MAXIMUM, "vsl_space", "1G");
647
                def = 52 * 1024;
648
        }
649
650 832
        low = sysconf(_SC_THREAD_STACK_MIN);
651 832
        MCF_ParamConf(MCF_MINIMUM, "thread_pool_stack", "%jdb", (intmax_t)low);
652
653
#if defined(__SANITIZER) || __has_feature(address_sanitizer)
654
        def = 92 * 1024;
655
#endif
656
657 832
        if (def < low)
658 0
                def = low;
659 832
        MCF_ParamConf(MCF_DEFAULT, "thread_pool_stack", "%jdb", (intmax_t)def);
660
661
#if !defined(MAX_THREAD_POOLS)
662
#  define MAX_THREAD_POOLS 32
663
#endif
664
665 832
        MCF_ParamConf(MCF_MAXIMUM, "thread_pools", "%d", MAX_THREAD_POOLS);
666
667
#if !defined(HAVE_ACCEPT_FILTERS) || defined(__linux)
668
        MCF_ParamConf(MCF_DEFAULT, "accept_filter", "off");
669
#endif
670
671 832
        VCLS_AddFunc(mgt_cls, MCF_AUTH, cli_params);
672
673 832
        vsb = VSB_new_auto();
674 832
        AN(vsb);
675
676 88192
        VTAILQ_FOREACH(pl, &phead, list) {
677 87360
                pp = pl->spec;
678
679 87360
                if (pp->flags & NOT_IMPLEMENTED)
680 832
                        continue;
681 86528
                if (pp->min != NULL)
682 67392
                        mcf_wash_param(cli, pp, &pp->min, "minimum", vsb);
683 86528
                if (pp->max != NULL)
684 20800
                        mcf_wash_param(cli, pp, &pp->max, "maximum", vsb);
685 86528
                AN(pp->def);
686 86528
                mcf_wash_param(cli, pp, &pp->def, "default", vsb);
687 86528
        }
688 832
        VSB_destroy(&vsb);
689 832
}
690
691
/*--------------------------------------------------------------------*/
692
693
void
694 23248
MCF_ParamConf(enum mcf_which_e which, const char * const param,
695
    const char *fmt, ...)
696
{
697
        struct parspec *pp;
698
        struct vsb *vsb;
699
        va_list ap;
700
701 23248
        pp = mcf_findpar(param);
702 23248
        AN(pp);
703 23248
        vsb = VSB_new_auto();
704 23248
        AN(vsb);
705 23248
        va_start(ap, fmt);
706 23248
        VSB_vprintf(vsb, fmt, ap);
707 23248
        va_end(ap);
708 23248
        AZ(VSB_finish(vsb));
709 23248
        switch (which) {
710
        case MCF_DEFAULT:
711 3328
                REPLACE(pp->dyn_def, VSB_data(vsb));
712 3328
                pp->def = pp->dyn_def;
713 3328
                break;
714
        case MCF_MINIMUM:
715 9948
                REPLACE(pp->dyn_min, VSB_data(vsb));
716 9948
                pp->min = pp->dyn_min;
717 9948
                break;
718
        case MCF_MAXIMUM:
719 9972
                REPLACE(pp->dyn_max, VSB_data(vsb));
720 9972
                pp->max = pp->dyn_max;
721 9972
                break;
722
        default:
723 0
                WRONG("bad 'which'");
724 0
        }
725 23248
        VSB_delete(vsb);
726 23248
}
727
728
/*--------------------------------------------------------------------*/
729
730
void
731 2
MCF_DumpRstParam(void)
732
{
733
        struct plist *pl;
734
        const struct parspec *pp;
735
        const char *p, *q, *t1, *t2;
736
        size_t z;
737
738 2
        printf("\n.. The following is the autogenerated "
739
            "output from varnishd -x parameter\n\n");
740 212
        VTAILQ_FOREACH(pl, &phead, list) {
741 210
                pp = pl->spec;
742 210
                printf(".. _ref_param_%s:\n\n", pp->name);
743 210
                printf("%s\n", pp->name);
744 3322
                for (z = 0; z < strlen(pp->name); z++)
745 3112
                        printf("~");
746 210
                printf("\n");
747
748 210
                if (pp->flags && pp->flags & NOT_IMPLEMENTED)
749 2
                        printf("\n%s\n\n", NOT_IMPLEMENTED_DOC);
750
751 210
                if (pp->units != NULL && *pp->units != '\0')
752 174
                        printf("\t* Units: %s\n", pp->units);
753 210
                printf("\t* Default: %s\n", pp->def);
754 210
                if (pp->dyn_min_reason != NULL)
755 2
                        printf("\t* Minimum: %s\n", pp->dyn_min_reason);
756 208
                else if (pp->min != NULL)
757 162
                        printf("\t* Minimum: %s\n", pp->min);
758 210
                if (pp->dyn_max_reason != NULL)
759 4
                        printf("\t* Maximum: %s\n", pp->dyn_max_reason);
760 206
                else if (pp->max != NULL)
761 46
                        printf("\t* Maximum: %s\n", pp->max);
762
                /*
763
                 * XXX: we should mark the params with one/two flags
764
                 * XXX: that say if ->min/->max are valid, so we
765
                 * XXX: can emit those also in help texts.
766
                 */
767 210
                if (pp->flags) {
768 98
                        printf("\t* Flags: ");
769 98
                        q = "";
770
771 98
                        if (pp->flags & DELAYED_EFFECT) {
772 26
                                printf("%sdelayed", q);
773 26
                                q = ", ";
774 26
                        }
775 98
                        if (pp->flags & MUST_RESTART) {
776 10
                                printf("%smust_restart", q);
777 10
                                q = ", ";
778 10
                        }
779 98
                        if (pp->flags & MUST_RELOAD) {
780 2
                                printf("%smust_reload", q);
781 2
                                q = ", ";
782 2
                        }
783 98
                        if (pp->flags & EXPERIMENTAL) {
784 50
                                printf("%sexperimental", q);
785 50
                                q = ", ";
786 50
                        }
787 98
                        if (pp->flags & WIZARD) {
788 8
                                printf("%swizard", q);
789 8
                                q = ", ";
790 8
                        }
791 98
                        if (pp->flags & ONLY_ROOT) {
792 0
                                printf("%sonly_root", q);
793 0
                                q = ", ";
794 0
                        }
795 98
                        if (pp->flags & OBJ_STICKY) {
796 6
                                printf("%sobj_sticky", q);
797 6
                                q = ", ";
798 6
                        }
799 98
                        printf("\n");
800 98
                }
801 210
                printf("\n");
802 210
                p = pp->descr;
803 792
                while (*p != '\0') {
804 582
                        q = strchr(p, '\n');
805 582
                        if (q == NULL)
806 210
                                q = strchr(p, '\0');
807 582
                        t1 = strchr(p, '\t');
808 582
                        if (t1 != NULL && t1 < q) {
809 84
                                t2 = strchr(t1 + 1, '\t');
810 84
                                AN(t2);
811 84
                                printf("\n\t*");
812 84
                                (void)fwrite(t1 + 1, (t2 - 1) - t1, 1, stdout);
813 84
                                printf("*\n\t\t");
814 84
                                p = t2 + 1;
815 84
                        }
816 582
                        (void)fwrite(p, q - p, 1, stdout);
817 582
                        p = q;
818 582
                        if (*p == '\n') {
819 372
                                printf("\n");
820 372
                                p++;
821 372
                        }
822 582
                        continue;
823
                }
824 210
                printf("\n\n");
825 210
        }
826 2
}