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 67560
mcf_findpar(const char *name)
117
{
118
        struct plist *pl;
119
120 67560
        AN(name);
121 5537708
        VTAILQ_FOREACH(pl, &phead, list)
122 5537700
                if (!strcmp(pl->spec->name, name))
123 67552
                        return (pl->spec);
124 8
        return (NULL);
125
}
126
127
static void
128 168846
mcf_addpar(struct parspec *ps)
129
{
130
        struct plist *pl, *pl2;
131
        int i;
132
133 168846
        ALLOC_OBJ(pl, PLIST_MAGIC);
134 168846
        AN(pl);
135 168846
        pl->spec = ps;
136 7344012
        VTAILQ_FOREACH(pl2, &phead, list) {
137 7271424
                i = strcmp(pl2->spec->name, pl->spec->name);
138 7271424
                if (i == 0) {
139 0
                        fprintf(stderr, "Duplicate param: %s\n", ps->name);
140 0
                        exit(4);
141 7271424
                } else if (i > 0) {
142 96258
                        VTAILQ_INSERT_BEFORE(pl2, pl, list);
143 96258
                        return;
144
                }
145
        }
146 72588
        VTAILQ_INSERT_TAIL(&phead, pl, list);
147
}
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 2542
mcf_wrap_line(struct cli *cli, const char *b, const char *e, int tabs, int m0)
157
{
158 2542
        int n, hadtabs = 0;
159
        const char *w;
160
161 2542
        n = m0;
162 2542
        VCLI_Out(cli, "%*s", n, "");
163
164 2542
        while (b < e) {
165 36338
                if (!isspace(*b)) {
166 17472
                        VCLI_Out(cli, "%c", *b);
167 17472
                        b++;
168 17472
                        n++;
169 18866
                } else if (*b == '\t') {
170 592
                        assert(tabs);
171 592
                        assert(hadtabs < 2);
172
                        do {
173 4552
                                VCLI_Out(cli, " ");
174 4552
                                n++;
175 4552
                        } while ((n % tabs) != (m0 + tab0) % tabs);
176 592
                        b++;
177 592
                        hadtabs++;
178
                } else {
179 18274
                        assert (*b == ' ');
180 107910
                        for (w = b + 1; w < e; w++)
181 106562
                                if (isspace(*w))
182 16926
                                        break;
183 18274
                        if (n + (w - b) < wrap_at) {
184 17080
                                VCLI_Out(cli, "%.*s", (int)(w - b), b);
185 17080
                                n += (w - b);
186 17080
                                b = w;
187
                        } else {
188 1194
                                assert(hadtabs == 0 || hadtabs == 2);
189 1194
                                VCLI_Out(cli, "\n");
190 1218
                                mcf_wrap_line(cli, b + 1, e, 0,
191 24
                                    hadtabs ? m0 + tab0 + tabs : m0);
192 1194
                                return;
193
                        }
194
                }
195
        }
196 1348
        assert(b == e);
197
}
198
199
static void
200 658
mcf_wrap(struct cli *cli, const char *text)
201
{
202
        const char *p, *q, *r;
203 658
        int tw = 0;
204
205 658
        if (strchr(text, '\t') != NULL) {
206 376
                for (p = text; *p != '\0'; ) {
207 336
                        q = strstr(p, "\n\t");
208 336
                        if (q == NULL)
209 40
                                break;
210 296
                        q += 2;
211 296
                        r = strchr(q, '\t');
212 296
                        if (r == NULL) {
213 0
                                fprintf(stderr,
214
                                    "LINE with just one TAB: <%s>\n", text);
215 0
                                exit(4);
216
                        }
217 296
                        if (r - q > tw)
218 96
                                tw = r - q;
219 296
                        p = q;
220
                }
221 40
                tw += 2;
222 40
                if (tw < 20)
223 32
                        tw = 20;
224
        }
225
226 3984
        for (p = text; *p != '\0'; ) {
227 2668
                if (*p == '\n') {
228 1320
                        VCLI_Out(cli, "\n");
229 1320
                        p++;
230 1320
                        continue;
231
                }
232 1348
                q = strchr(p, '\n');
233 1348
                if (q == NULL)
234 658
                        q = strchr(p, '\0');
235 1348
                mcf_wrap_line(cli, p, q, tw, margin1);
236 1348
                p = q;
237
        }
238 658
}
239
240
/*--------------------------------------------------------------------*/
241
242
static void v_matchproto_(cli_func_t)
243 40
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 40
        int lfmt = 0, chg = 0;
249
        struct vsb *vsb;
250
251 40
        vsb = VSB_new_auto();
252
        (void)priv;
253
254 40
        if (av[2] != NULL && !strcmp(av[2], "changed"))
255 2
                chg = 1;
256 38
        else if (av[2] != NULL)
257 28
                lfmt = 1;
258
259 40
        n = 0;
260 4320
        VTAILQ_FOREACH(pl, &phead, list) {
261 4280
                pp = pl->spec;
262 4280
                if (lfmt && strcmp(pp->name, av[2]) && strcmp("-l", av[2]))
263 2546
                        continue;
264 1734
                n++;
265
266 1734
                VSB_clear(vsb);
267 1734
                if (pp->func(vsb, pp, NULL))
268 0
                        VCLI_SetResult(cli, CLIS_PARAM);
269 1734
                AZ(VSB_finish(vsb));
270 1734
                if (chg && pp->def != NULL && !strcmp(pp->def, VSB_data(vsb)))
271 196
                        continue;
272
273 1538
                if (pp->flags & NOT_IMPLEMENTED) {
274 14
                        if (lfmt) {
275 4
                                VCLI_Out(cli, "%s\n", pp->name);
276 4
                                VCLI_Out(cli, "%-*sNot available", margin1, " ");
277
                        } else {
278 10
                                VCLI_Out(cli, "%-*s-", margin2, pp->name);
279
                        }
280
                } else {
281 1524
                        if (lfmt) {
282 446
                                VCLI_Out(cli, "%s\n", pp->name);
283 446
                                VCLI_Out(cli, "%-*sValue is: ", margin1, " ");
284
                        } else {
285 1078
                                VCLI_Out(cli, "%-*s", margin2, pp->name);
286
                        }
287
288 1524
                        VCLI_Out(cli, "%s", VSB_data(vsb));
289 1524
                        if (pp->units != NULL && *pp->units != '\0')
290 1222
                                VCLI_Out(cli, " [%s]", pp->units);
291 1524
                        if (pp->def != NULL && !strcmp(pp->def, VSB_data(vsb)))
292 1380
                                VCLI_Out(cli, " (default)");
293
                }
294 1538
                VCLI_Out(cli, "\n");
295
296 1538
                if (lfmt && pp->flags & NOT_IMPLEMENTED) {
297 4
                        VCLI_Out(cli, "\n");
298 4
                        mcf_wrap(cli, NOT_IMPLEMENTED_TEXT);
299 4
                        VCLI_Out(cli, "\n\n");
300 1534
                } else if (lfmt) {
301 446
                        if (pp->def != NULL && strcmp(pp->def, VSB_data(vsb)))
302 48
                                VCLI_Out(cli, "%-*sDefault is: %s\n",
303
                                    margin1, "", pp->def);
304 446
                        if (pp->min != NULL)
305 334
                                VCLI_Out(cli, "%-*sMinimum is: %s\n",
306
                                    margin1, "", pp->min);
307 446
                        if (pp->max != NULL)
308 106
                                VCLI_Out(cli, "%-*sMaximum is: %s\n",
309
                                    margin1, "", pp->max);
310 446
                        VCLI_Out(cli, "\n");
311 446
                        mcf_wrap(cli, pp->descr);
312 446
                        if (pp->flags & OBJ_STICKY)
313 14
                                mcf_wrap(cli, OBJ_STICKY_TEXT);
314 446
                        if (pp->flags & DELAYED_EFFECT)
315 52
                                mcf_wrap(cli, DELAYED_EFFECT_TEXT);
316 446
                        if (pp->flags & EXPERIMENTAL)
317 100
                                mcf_wrap(cli, EXPERIMENTAL_TEXT);
318 446
                        if (pp->flags & MUST_RELOAD)
319 4
                                mcf_wrap(cli, MUST_RELOAD_TEXT);
320 446
                        if (pp->flags & MUST_RESTART)
321 20
                                mcf_wrap(cli, MUST_RESTART_TEXT);
322 446
                        if (pp->flags & WIZARD)
323 16
                                mcf_wrap(cli, WIZARD_TEXT);
324 446
                        if (pp->flags & PROTECTED)
325 2
                                mcf_wrap(cli, PROTECTED_TEXT);
326 446
                        if (pp->flags & ONLY_ROOT)
327 0
                                mcf_wrap(cli, ONLY_ROOT_TEXT);
328 446
                        VCLI_Out(cli, "\n\n");
329
                }
330
        }
331 40
        if (av[2] != NULL && lfmt && strcmp(av[2], "-l") && n == 0) {
332 2
                VCLI_SetResult(cli, CLIS_PARAM);
333 2
                VCLI_Out(cli, "Unknown parameter \"%s\".", av[2]);
334
        }
335 40
        VSB_destroy(&vsb);
336 40
}
337
338
static inline void
339 1112
mcf_json_key_valstr(struct cli *cli, const char *key, const char *val)
340
{
341 1112
        VCLI_Out(cli, "\"%s\": ", key);
342 1112
        VCLI_JSON_str(cli, val);
343 1112
        VCLI_Out(cli, ",\n");
344 1112
}
345
346
static void v_matchproto_(cli_func_t)
347 12
mcf_param_show_json(struct cli *cli, const char * const *av, void *priv)
348
{
349 12
        int n, comma = 0;
350
        struct plist *pl;
351
        const struct parspec *pp;
352 12
        int chg = 0, flags;
353
        struct vsb *vsb, *def;
354 12
        const char *show = NULL;
355
356
        (void)priv;
357
358 32
        for (int i = 2; av[i] != NULL; i++) {
359 22
                if (strcmp(av[i], "-l") == 0) {
360 2
                        VCLI_SetResult(cli, CLIS_PARAM);
361 2
                        VCLI_Out(cli, "-l not permitted with param.show -j");
362 2
                        return;
363
                }
364 20
                if (strcmp(av[i], "changed") == 0) {
365 2
                        chg = 1;
366 2
                        continue;
367
                }
368 18
                if (strcmp(av[i], "-j") == 0)
369 12
                        continue;
370 6
                show = av[i];
371
        }
372
373 10
        vsb = VSB_new_auto();
374 10
        def = VSB_new_auto();
375
376 10
        n = 0;
377 10
        VCLI_JSON_begin(cli, 2, av);
378 10
        VCLI_Out(cli, ",\n");
379 1080
        VTAILQ_FOREACH(pl, &phead, list) {
380 1070
                pp = pl->spec;
381 1070
                if (show != NULL && strcmp(pp->name, show) != 0)
382 638
                        continue;
383 432
                n++;
384
385 432
                VSB_clear(vsb);
386 432
                if (pp->func(vsb, pp, JSON_FMT))
387 0
                        VCLI_SetResult(cli, CLIS_PARAM);
388 432
                AZ(VSB_finish(vsb));
389 432
                VSB_clear(def);
390 432
                if (pp->func(def, pp, NULL))
391 0
                        VCLI_SetResult(cli, CLIS_PARAM);
392 432
                AZ(VSB_finish(def));
393 432
                if (chg && pp->def != NULL && !strcmp(pp->def, VSB_data(def)))
394 196
                        continue;
395
396 236
                VCLI_Out(cli, "%s", comma ? ",\n" : "");
397 236
                comma++;
398 236
                VCLI_Out(cli, "{\n");
399 236
                VSB_indent(cli->sb, 2);
400 236
                mcf_json_key_valstr(cli, "name", pp->name);
401 236
                if (pp->flags & NOT_IMPLEMENTED) {
402 2
                        VCLI_Out(cli, "\"implemented\": false\n");
403 2
                        VSB_indent(cli->sb, -2);
404 2
                        VCLI_Out(cli, "}");
405 2
                        continue;
406
                }
407 234
                VCLI_Out(cli, "\"implemented\": true,\n");
408 234
                VCLI_Out(cli, "\"value\": %s,\n", VSB_data(vsb));
409 234
                if (pp->units != NULL && *pp->units != '\0')
410 184
                        mcf_json_key_valstr(cli, "units", pp->units);
411
412 234
                if (pp->def != NULL)
413 234
                        mcf_json_key_valstr(cli, "default", pp->def);
414 234
                if (pp->min != NULL)
415 168
                        mcf_json_key_valstr(cli, "minimum", pp->min);
416 234
                if (pp->max != NULL)
417 56
                        mcf_json_key_valstr(cli, "maximum", pp->max);
418 234
                mcf_json_key_valstr(cli, "description", pp->descr);
419
420 234
                flags = 0;
421 234
                VCLI_Out(cli, "\"flags\": [\n");
422 234
                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 234
                flag_out(OBJ_STICKY, obj_sticky);
434 234
                flag_out(DELAYED_EFFECT, delayed_effect);
435 234
                flag_out(EXPERIMENTAL, experimental);
436 234
                flag_out(MUST_RELOAD, must_reload);
437 234
                flag_out(MUST_RESTART, must_restart);
438 234
                flag_out(WIZARD, wizard);
439 234
                flag_out(PROTECTED, protected);
440 234
                flag_out(ONLY_ROOT, only_root);
441
442
#undef flag_out
443
444 234
                VSB_indent(cli->sb, -2);
445 234
                VCLI_Out(cli, "\n]");
446 234
                VSB_indent(cli->sb, -2);
447 234
                VCLI_Out(cli, "\n}");
448
        }
449 10
        VCLI_JSON_end(cli);
450 10
        if (show != NULL && n == 0) {
451 2
                VSB_clear(cli->sb);
452 2
                VCLI_SetResult(cli, CLIS_PARAM);
453 2
                VCLI_Out(cli, "Unknown parameter \"%s\".", show);
454
        }
455 10
        VSB_destroy(&vsb);
456 10
        VSB_destroy(&def);
457
}
458
459
/*--------------------------------------------------------------------
460
 * Mark parameters as protected
461
 */
462
463
void
464 2
MCF_ParamProtect(struct cli *cli, const char *args)
465
{
466
        char **av;
467
        struct parspec *pp;
468
        int i;
469
470 2
        av = VAV_Parse(args, NULL, ARGV_COMMA);
471 2
        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 4
        for (i = 1; av[i] != NULL; i++) {
478 2
                pp = mcf_findpar(av[i]);
479 2
                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 2
                pp->flags |= PROTECTED;
486
        }
487 2
        VAV_Free(av);
488
}
489
490
/*--------------------------------------------------------------------*/
491
492
void
493 10828
MCF_ParamSet(struct cli *cli, const char *param, const char *val)
494
{
495
        const struct parspec *pp;
496
497 10828
        pp = mcf_findpar(param);
498 10828
        if (pp == NULL) {
499 8
                VCLI_SetResult(cli, CLIS_PARAM);
500 8
                VCLI_Out(cli, "Unknown parameter \"%s\".", param);
501 8
                return;
502
        }
503 10820
        if (pp->flags & PROTECTED) {
504 2
                VCLI_SetResult(cli, CLIS_AUTH);
505 2
                VCLI_Out(cli, "parameter \"%s\" is protected.", param);
506 2
                return;
507
        }
508 10818
        if (pp->func(cli->sb, pp, val))
509 44
                VCLI_SetResult(cli, CLIS_PARAM);
510
511 10818
        if (cli->result == CLIS_OK && heritage.param != NULL)
512 440
                *heritage.param = mgt_param;
513
514 10818
        if (cli->result != CLIS_OK) {
515 44
                VCLI_Out(cli, "\n(attempting to set param '%s' to '%s')",
516
                    pp->name, val);
517 10774
        } else if (MCH_Running() && pp->flags & MUST_RESTART) {
518 0
                VCLI_Out(cli,
519
                    "\nChange will take effect when child is restarted");
520 10774
        } else if (pp->flags & MUST_RELOAD) {
521 0
                VCLI_Out(cli,
522
                    "\nChange will take effect when VCL script is reloaded");
523
        }
524
}
525
526
527
/*--------------------------------------------------------------------*/
528
529
static void v_matchproto_(cli_func_t)
530 630
mcf_param_set(struct cli *cli, const char * const *av, void *priv)
531
{
532
533
        (void)priv;
534 630
        MCF_ParamSet(cli, av[2], av[3]);
535 630
}
536
537
/*--------------------------------------------------------------------
538
 * Add a group of parameters to the global set and sort by name.
539
 */
540
541
void
542 4734
MCF_AddParams(struct parspec *ps)
543
{
544
        struct parspec *pp;
545
        const char *s;
546
547 173580
        for (pp = ps; pp->name != NULL; pp++) {
548 168846
                AN(pp->func);
549 168846
                s = strchr(pp->descr, '\0');
550 168846
                if (isspace(s[-1])) {
551 0
                        fprintf(stderr,
552
                            "Param->descr has trailing space: %s\n", pp->name);
553 0
                        exit(4);
554
                }
555 168846
                mcf_addpar(pp);
556 168846
                if (strlen(pp->name) + 1 > margin2)
557 7890
                        margin2 = strlen(pp->name) + 1;
558
        }
559 4734
}
560
561
/*--------------------------------------------------------------------
562
 * Wash a min/max/default value
563
 */
564
565
static void
566 334536
mcf_wash_param(struct cli *cli, const struct parspec *pp, const char **val,
567
    const char *name, struct vsb *vsb)
568
{
569
        int err;
570
571 334536
        AN(*val);
572 334536
        VSB_clear(vsb);
573 334536
        VSB_printf(vsb, "FAILED to set %s for param %s: %s\n",
574
            name, pp->name, *val);
575 334536
        err = pp->func(vsb, pp, *val);
576 334536
        AZ(VSB_finish(vsb));
577 334536
        if (err) {
578 0
                VCLI_Out(cli, "%s\n", VSB_data(vsb));
579 0
                VCLI_SetResult(cli, CLIS_CANT);
580 0
                return;
581
        }
582 334536
        VSB_clear(vsb);
583 334536
        err = pp->func(vsb, pp, NULL);
584 334536
        AZ(err);
585 334536
        AZ(VSB_finish(vsb));
586 334536
        if (strcmp(*val, VSB_data(vsb))) {
587 63120
                *val = strdup(VSB_data(vsb));
588 63120
                AN(*val);
589
        }
590
}
591
592
/*--------------------------------------------------------------------*/
593
594
static struct cli_proto cli_params[] = {
595
        { CLICMD_PARAM_SHOW,            "", mcf_param_show,
596
          mcf_param_show_json },
597
        { CLICMD_PARAM_SET,             "", mcf_param_set },
598
        { NULL }
599
};
600
601
/*--------------------------------------------------------------------
602
 * Configure the parameters
603
 */
604
605
void
606 1578
MCF_InitParams(struct cli *cli)
607
{
608
        struct plist *pl;
609
        struct parspec *pp;
610
        struct vsb *vsb;
611
        ssize_t def, low;
612
613 1578
        MCF_AddParams(mgt_parspec);
614 1578
        MCF_AddParams(WRK_parspec);
615 1578
        MCF_AddParams(VSL_parspec);
616
617 1578
        MCF_TcpParams();
618
619
        if (sizeof(void *) < 8) {               /*lint !e506 !e774  */
620
                /*
621
                 * Adjust default parameters for 32 bit systems to conserve
622
                 * VM space.
623
                 */
624
                MCF_ParamConf(MCF_DEFAULT, "workspace_client", "24k");
625
                MCF_ParamConf(MCF_DEFAULT, "workspace_backend", "16k");
626
                MCF_ParamConf(MCF_DEFAULT, "http_resp_size", "8k");
627
                MCF_ParamConf(MCF_DEFAULT, "http_req_size", "12k");
628
                MCF_ParamConf(MCF_DEFAULT, "gzip_buffer", "4k");
629
                MCF_ParamConf(MCF_MAXIMUM, "vsl_space", "1G");
630
        }
631
632
#if !defined(HAVE_ACCEPT_FILTERS) || defined(__linux)
633
        MCF_ParamConf(MCF_DEFAULT, "accept_filter", "off");
634
#endif
635
636 1578
        low = sysconf(_SC_THREAD_STACK_MIN);
637 1578
        MCF_ParamConf(MCF_MINIMUM, "thread_pool_stack", "%jdb", (intmax_t)low);
638
639
#if defined(__SANITIZER) || __has_feature(address_sanitizer)
640
        def = 92 * 1024;
641
#else
642 1578
        def = 48 * 1024;
643
#endif
644 1578
        if (def < low)
645 0
                def = low;
646 1578
        MCF_ParamConf(MCF_DEFAULT, "thread_pool_stack", "%jdb", (intmax_t)def);
647
648
#if !defined(MAX_THREAD_POOLS)
649
#  define MAX_THREAD_POOLS 32
650
#endif
651
652 1578
        MCF_ParamConf(MCF_MAXIMUM, "thread_pools", "%d", MAX_THREAD_POOLS);
653
654 1578
        VCLS_AddFunc(mgt_cls, MCF_AUTH, cli_params);
655
656 1578
        vsb = VSB_new_auto();
657 1578
        AN(vsb);
658
659 170424
        VTAILQ_FOREACH(pl, &phead, list) {
660 168846
                pp = pl->spec;
661
662 168846
                if (pp->flags & NOT_IMPLEMENTED)
663 1578
                        continue;
664 167268
                if (pp->min != NULL)
665 127818
                        mcf_wash_param(cli, pp, &pp->min, "minimum", vsb);
666 167268
                if (pp->max != NULL)
667 39450
                        mcf_wash_param(cli, pp, &pp->max, "maximum", vsb);
668 167268
                AN(pp->def);
669 167268
                mcf_wash_param(cli, pp, &pp->def, "default", vsb);
670
        }
671 1578
        VSB_destroy(&vsb);
672 1578
}
673
674
/*--------------------------------------------------------------------*/
675
676
void
677 56730
MCF_ParamConf(enum mcf_which_e which, const char *param, const char *fmt, ...)
678
{
679
        struct parspec *pp;
680
        struct vsb *vsb;
681
        va_list ap;
682
683 56730
        pp = mcf_findpar(param);
684 56730
        AN(pp);
685 56730
        vsb = VSB_new_auto();
686 56730
        AN(vsb);
687 56730
        va_start(ap, fmt);
688 56730
        VSB_vprintf(vsb, fmt, ap);
689 56730
        va_end(ap);
690 56730
        AZ(VSB_finish(vsb));
691 56730
        switch (which) {
692
        case MCF_DEFAULT:
693 6312
                pp->def = strdup(VSB_data(vsb));
694 6312
                AN(pp->def);
695 6312
                break;
696
        case MCF_MINIMUM:
697 25190
                pp->min = strdup(VSB_data(vsb));
698 25190
                AN(pp->min);
699 25190
                break;
700
        case MCF_MAXIMUM:
701 25228
                pp->max = strdup(VSB_data(vsb));
702 25228
                AN(pp->max);
703 25228
                break;
704
        }
705 56730
        VSB_delete(vsb);
706 56730
}
707
708
/*--------------------------------------------------------------------*/
709
710
void
711 4
MCF_DumpRstParam(void)
712
{
713
        struct plist *pl;
714
        const struct parspec *pp;
715
        const char *p, *q, *t1, *t2;
716
        int j;
717
718 4
        printf("\n.. The following is the autogenerated "
719
            "output from varnishd -x parameter\n\n");
720 432
        VTAILQ_FOREACH(pl, &phead, list) {
721 428
                pp = pl->spec;
722 428
                printf(".. _ref_param_%s:\n\n", pp->name);
723 428
                printf("%s\n", pp->name);
724 6700
                for (j = 0; j < strlen(pp->name); j++)
725 6272
                        printf("~");
726 428
                printf("\n");
727
728 428
                if (pp->flags && pp->flags & NOT_IMPLEMENTED)
729 4
                        printf("\n%s\n\n", NOT_IMPLEMENTED_DOC);
730
731 428
                if (pp->units != NULL && *pp->units != '\0')
732 348
                        printf("\t* Units: %s\n", pp->units);
733 428
                printf("\t* Default: %s\n", pp->def);
734 428
                if (pp->min != NULL)
735 328
                        printf("\t* Minimum: %s\n", pp->min);
736 428
                if (pp->max != NULL)
737 104
                        printf("\t* Maximum: %s\n", pp->max);
738
                /*
739
                 * XXX: we should mark the params with one/two flags
740
                 * XXX: that say if ->min/->max are valid, so we
741
                 * XXX: can emit those also in help texts.
742
                 */
743 428
                if (pp->flags) {
744 196
                        printf("\t* Flags: ");
745 196
                        q = "";
746
747 196
                        if (pp->flags & DELAYED_EFFECT) {
748 52
                                printf("%sdelayed", q);
749 52
                                q = ", ";
750
                        }
751 196
                        if (pp->flags & MUST_RESTART) {
752 20
                                printf("%smust_restart", q);
753 20
                                q = ", ";
754
                        }
755 196
                        if (pp->flags & MUST_RELOAD) {
756 4
                                printf("%smust_reload", q);
757 4
                                q = ", ";
758
                        }
759 196
                        if (pp->flags & EXPERIMENTAL) {
760 100
                                printf("%sexperimental", q);
761 100
                                q = ", ";
762
                        }
763 196
                        if (pp->flags & WIZARD) {
764 16
                                printf("%swizard", q);
765 16
                                q = ", ";
766
                        }
767 196
                        if (pp->flags & ONLY_ROOT) {
768 0
                                printf("%sonly_root", q);
769 0
                                q = ", ";
770
                        }
771 196
                        if (pp->flags & OBJ_STICKY) {
772 12
                                printf("%sobj_sticky", q);
773 12
                                q = ", ";
774
                        }
775 196
                        printf("\n");
776
                }
777 428
                printf("\n");
778 428
                p = pp->descr;
779 2020
                while (*p != '\0') {
780 1164
                        q = strchr(p, '\n');
781 1164
                        if (q == NULL)
782 428
                                q = strchr(p, '\0');
783 1164
                        t1 = strchr(p, '\t');
784 1164
                        if (t1 != NULL && t1 < q) {
785 164
                                t2 = strchr(t1 + 1, '\t');
786 164
                                AN(t2);
787 164
                                printf("\n\t*");
788 164
                                (void)fwrite(t1 + 1, (t2 - 1) - t1, 1, stdout);
789 164
                                printf("*\n\t\t");
790 164
                                p = t2 + 1;
791
                        }
792 1164
                        (void)fwrite(p, q - p, 1, stdout);
793 1164
                        p = q;
794 1164
                        if (*p == '\n') {
795 736
                                printf("\n");
796 736
                                p++;
797
                        }
798 1164
                        continue;
799
                }
800 428
                printf("\n\n");
801
        }
802 4
}