varnish-cache/bin/varnishd/mgt/mgt_main.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2017 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
 * The management process and CLI handling
30
 */
31
32
#include "config.h"
33
34
#include <ctype.h>
35
#include <fcntl.h>
36
#include <signal.h>
37
#include <stdio.h>
38
#include <stdlib.h>
39
#include <string.h>
40
#include <syslog.h>
41
#include <unistd.h>
42
#include <sys/stat.h>
43
#include <sys/socket.h>
44
45
#include "mgt/mgt.h"
46
#include "common/heritage.h"
47
48
#include "hash/hash_slinger.h"
49
#include "libvcc.h"
50
#include "vav.h"
51
#include "vcli_serve.h"
52
#include "vend.h"
53
#include "vev.h"
54
#include "vfil.h"
55
#include "vin.h"
56
#include "vpf.h"
57
#include "vrnd.h"
58
#include "vsha256.h"
59
#include "vsub.h"
60
#include "vtim.h"
61
#include "waiter/mgt_waiter.h"
62
#include "vsa.h"
63
64
struct heritage         heritage;
65
unsigned                d_flag = 0;
66
struct vev_root         *mgt_evb;
67
int                     exit_status = 0;
68
struct vsb              *vident;
69
struct VSC_mgt          *VSC_C_mgt;
70
static int              I_fd = -1;
71
static char             *Cn_arg;
72
73
static struct vpf_fh *pfh1 = NULL;
74
static struct vpf_fh *pfh2 = NULL;
75
76
static struct vfil_path *vcl_path = NULL;
77
static VTAILQ_HEAD(,f_arg) f_args = VTAILQ_HEAD_INITIALIZER(f_args);
78
79
static const char opt_spec[] = "a:b:Cdf:Fh:i:I:j:l:M:n:P:p:r:S:s:T:t:VW:x:";
80
81
int optreset;   // Some has it, some doesn't.  Cheaper than auto*
82
83
/*--------------------------------------------------------------------*/
84
85
static void
86 2
usage(void)
87
{
88
#define FMT "  %-28s # %s\n"
89
90 2
        printf( "Usage: varnishd [options]\n");
91
92 2
        printf("\nBasic options:\n");
93
94 2
        printf(FMT, "-a [<name>=]address[:port][,proto]",
95
            "HTTP listen address and port");
96 2
        printf(FMT, "   [,user=<u>][,group=<g>]",
97
            "Can be specified multiple times.");
98 2
        printf(FMT, "   [,mode=<m>]", "  default: \":80,HTTP\"");
99 2
        printf(FMT, "", "Proto can be \"PROXY\" or \"HTTP\" (default)");
100 2
        printf(FMT, "", "user, group and mode set permissions for");
101 2
        printf(FMT, "", "  a Unix domain socket.");
102 2
        printf(FMT, "-b [addr[:port]|path]", "Backend address and port");
103 2
        printf(FMT, "", "  or socket file path");
104 2
        printf(FMT, "", "  default: \":80\"");
105 2
        printf(FMT, "-f vclfile", "VCL program");
106 2
        printf(FMT, "", "Can be specified multiple times.");
107 2
        printf(FMT, "-n dir", "Working directory");
108
109 2
        printf("\n-b can be used only once, and not together with -f\n");
110
111 2
        printf("\nDocumentation options:\n");
112 2
        printf(FMT, "-?", "Prints this usage message");
113 2
        printf(FMT, "-x parameter", "Parameter documentation");
114 2
        printf(FMT, "-x vsl", "VSL record documentation");
115 2
        printf(FMT, "-x cli", "CLI command documentation");
116 2
        printf(FMT, "-x builtin", "Builtin VCL program");
117 2
        printf(FMT, "-x optstring", "List of getopt options");
118
119 2
        printf("\nOperations options:\n");
120
121 2
        printf(FMT, "-F", "Run in foreground");
122 2
        printf(FMT, "-T address[:port]", "CLI address");
123 2
        printf(FMT, "", "Can be specified multiple times.");
124 2
        printf(FMT, "-M address:port", "Reverse CLI destination");
125 2
        printf(FMT, "", "Can be specified multiple times.");
126 2
        printf(FMT, "-P file", "PID file");
127 2
        printf(FMT, "-i identity", "Identity of varnish instance");
128 2
        printf(FMT, "-I clifile", "Initialization CLI commands");
129
130 2
        printf("\nTuning options:\n");
131
132 2
        printf(FMT, "-t TTL", "Default TTL");
133 2
        printf(FMT, "-p param=value", "set parameter");
134 2
        printf(FMT, "", "Can be specified multiple times.");
135
136 2
        printf(FMT, "-s [name=]kind[,options]", "Storage specification");
137 2
        printf(FMT, "", "Can be specified multiple times.");
138
#ifdef HAVE_UMEM_H
139
        printf(FMT, "", "  -s default (=umem)");
140
        printf(FMT, "", "  -s umem");
141
#else
142 2
        printf(FMT, "", "  -s default (=malloc)");
143
#endif
144 2
        printf(FMT, "", "  -s malloc");
145 2
        printf(FMT, "", "  -s file");
146
147 2
        printf(FMT, "-l vsl", "Size of shared memory log");
148 2
        printf(FMT, "", "  vsl: space for VSL records [80m]");
149
150 2
        printf("\nSecurity options:\n");
151
152 2
        printf(FMT, "-r param[,param...]", "Set parameters read-only from CLI");
153 2
        printf(FMT, "", "Can be specified multiple times.");
154 2
        printf(FMT, "-S secret-file", "Secret file for CLI authentication");
155 2
        printf(FMT, "-j jail[,options]", "Jail specification");
156
#ifdef HAVE_SETPPRIV
157
        printf(FMT, "", "  -j solaris");
158
#endif
159 2
        printf(FMT, "", "  -j unix");
160 2
        printf(FMT, "", "  -j none");
161
162 2
        printf("\nAdvanced/Dev/Debug options:\n");
163
164 2
        printf(FMT, "-d", "debug mode");
165 2
        printf(FMT, "", "Stay in foreground, CLI on stdin.");
166 2
        printf(FMT, "-C", "Output VCL code compiled to C language");
167 2
        printf(FMT, "-V", "version");
168 2
        printf(FMT, "-h kind[,options]", "Hash specification");
169 2
        printf(FMT, "-W waiter", "Waiter implementation");
170 2
}
171
172
/*--------------------------------------------------------------------*/
173
174
static void
175 7031
cli_check(const struct cli *cli)
176
{
177 7031
        if (cli->result == CLIS_OK) {
178 7030
                VSB_clear(cli->sb);
179 7030
                return;
180
        }
181 1
        AZ(VSB_finish(cli->sb));
182 1
        fprintf(stderr, "Error:\n%s\n", VSB_data(cli->sb));
183 1
        exit(2);
184
}
185
186
/*--------------------------------------------------------------------
187
 * This function is called when the CLI on stdin is closed.
188
 */
189
190
static int v_matchproto_(mgt_cli_close_f)
191 755
mgt_stdin_close(void *priv)
192
{
193
194 755
        (void)priv;
195 755
        return (-42);
196
}
197
198
/*--------------------------------------------------------------------
199
 * Autogenerate a -S file using strong random bits from the kernel.
200
 */
201
202
static void
203 3575
mgt_secret_atexit(void)
204
{
205
206
        /* Only master process */
207 3575
        if (getpid() != heritage.mgt_pid)
208 2817
                return;
209 758
        VJ_master(JAIL_MASTER_FILE);
210 758
        (void)unlink("_.secret");
211 758
        VJ_master(JAIL_MASTER_LOW);
212 3575
}
213
214
static const char *
215 758
make_secret(const char *dirname)
216
{
217
        char *fn;
218
        int fdo;
219
        int i;
220
        unsigned char b;
221
222 758
        assert(asprintf(&fn, "%s/_.secret", dirname) > 0);
223
224 758
        VJ_master(JAIL_MASTER_FILE);
225 758
        fdo = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0640);
226 758
        if (fdo < 0)
227 0
                ARGV_ERR("Cannot create secret-file in %s (%s)\n",
228
                    dirname, vstrerror(errno));
229
230 194806
        for (i = 0; i < 256; i++) {
231 194048
                AZ(VRND_RandomCrypto(&b, 1));
232 194048
                assert(1 == write(fdo, &b, 1));
233 194048
        }
234 758
        closefd(&fdo);
235 758
        VJ_master(JAIL_MASTER_LOW);
236 758
        AZ(atexit(mgt_secret_atexit));
237 758
        return (fn);
238
}
239
240
static void
241 3
mgt_Cflag_atexit(void)
242
{
243
244
        /* Only master process */
245 3
        if (getpid() != heritage.mgt_pid)
246 2
                return;
247 1
        (void)rmdir("vmod_cache");
248 1
        (void)unlink("_.pid");
249 1
        (void)rmdir(Cn_arg);
250 3
}
251
252
/*--------------------------------------------------------------------*/
253
254
static void
255 832
mgt_tests(void)
256
{
257 832
        assert(VTIM_parse("Sun, 06 Nov 1994 08:49:37 GMT") == 784111777);
258 832
        assert(VTIM_parse("Sunday, 06-Nov-94 08:49:37 GMT") == 784111777);
259 832
        assert(VTIM_parse("Sun Nov  6 08:49:37 1994") == 784111777);
260
261
        /* Check that our VSHA256 works */
262 832
        VSHA256_Test();
263 832
}
264
265
static void
266 832
mgt_initialize(struct cli *cli)
267
{
268
        static unsigned clilim = 32768;
269
270
        /* for ASSERT_MGT() */
271 832
        heritage.mgt_pid = getpid();
272
273
        /* Create a cli for convenience in otherwise CLI functions */
274 832
        INIT_OBJ(cli, CLI_MAGIC);
275 832
        cli[0].sb = VSB_new_auto();
276 832
        AN(cli[0].sb);
277 832
        cli[0].result = CLIS_OK;
278 832
        cli[0].limit = &clilim;
279
280 832
        mgt_cli_init_cls();             // CLI commands can be registered
281
282 832
        MCF_InitParams(cli);
283
284 832
        VCC_VCL_Range(&heritage.min_vcl, &heritage.max_vcl);
285
286 832
        cli_check(cli);
287 832
}
288
289
static void
290 7
mgt_x_arg(const char *x_arg)
291
{
292 7
        if (!strcmp(x_arg, "parameter"))
293 2
                MCF_DumpRstParam();
294 5
        else if (!strcmp(x_arg, "vsl"))
295 1
                mgt_DumpRstVsl();
296 4
        else if (!strcmp(x_arg, "cli"))
297 2
                mgt_DumpRstCli();
298 2
        else if (!strcmp(x_arg, "builtin"))
299 1
                mgt_DumpBuiltin();
300 1
        else if (!strcmp(x_arg, "optstring"))
301 0
                (void)printf("%s\n", opt_spec);
302
        else
303 1
                ARGV_ERR("Invalid -x argument\n");
304 6
}
305
306
/*--------------------------------------------------------------------*/
307
308
#define ERIC_MAGIC 0x2246988a           /* Eric is not random */
309
310
static int
311 21
mgt_eric(void)
312
{
313
        int eric_pipes[2];
314
        unsigned u;
315
        ssize_t sz;
316
317 21
        AZ(pipe(eric_pipes));
318
319 21
        switch (fork()) {
320
        case -1:
321 0
                fprintf(stderr, "Fork() failed: %s\n", vstrerror(errno));
322 0
                exit(-1);
323
        case 0:
324 21
                closefd(&eric_pipes[0]);
325 21
                assert(setsid() > 1);
326
327 21
                VFIL_null_fd(STDIN_FILENO);
328 21
                return (eric_pipes[1]);
329
        default:
330 21
                break;
331
        }
332 21
        closefd(&eric_pipes[1]);
333 21
        sz = read(eric_pipes[0], &u, sizeof u);
334 21
        if (sz == sizeof u && u == ERIC_MAGIC)
335 0
                exit(0);
336 21
        else if (sz == sizeof u && u != 0)
337 0
                exit(u);
338
        else
339 21
                exit(-1);
340
}
341
342
static void
343 0
mgt_eric_im_done(int eric_fd, unsigned u)
344
{
345
346 0
        if (u == 0)
347 0
                u = ERIC_MAGIC;
348
349 0
        VFIL_null_fd(STDIN_FILENO);
350 0
        VFIL_null_fd(STDOUT_FILENO);
351 0
        VFIL_null_fd(STDERR_FILENO);
352
353 0
        assert(write(eric_fd, &u, sizeof u) == sizeof u);
354 0
        closefd(&eric_fd);
355 0
}
356
357
/*--------------------------------------------------------------------*/
358
359
static int v_matchproto_(vev_cb_f)
360 2
mgt_sigint(const struct vev *e, int what)
361
{
362
363 2
        (void)e;
364 2
        (void)what;
365 2
        MGT_Complain(C_ERR, "Manager got %s", e->name);
366 2
        (void)fflush(stdout);
367 2
        if (MCH_Running())
368 2
                MCH_Stop_Child();
369 2
        return (-42);
370
}
371
372
/*--------------------------------------------------------------------*/
373
374
static int v_matchproto_(vev_cb_f)
375 2432
mgt_uptime(const struct vev *e, int what)
376
{
377
        static double mgt_uptime_t0 = 0;
378
379 2432
        (void)e;
380 2432
        (void)what;
381 2432
        AN(VSC_C_mgt);
382 2432
        if (mgt_uptime_t0 == 0)
383 745
                mgt_uptime_t0 = VTIM_real();
384 2432
        VSC_C_mgt->uptime = (uint64_t)(VTIM_real() - mgt_uptime_t0);
385 2432
        return (0);
386
}
387
388
/*--------------------------------------------------------------------*/
389
390
static int v_matchproto_(mgt_cli_close_f)
391 0
mgt_I_close(void *priv)
392
{
393 0
        (void)priv;
394 0
        fprintf(stderr, "END of -I file processing\n");
395 0
        I_fd = -1;
396 0
        return (0);
397
}
398
399
/*--------------------------------------------------------------------*/
400
401
struct f_arg {
402
        unsigned                magic;
403
#define F_ARG_MAGIC             0x840649a8
404
        char                    *farg;
405
        char                    *src;
406
        VTAILQ_ENTRY(f_arg)     list;
407
};
408
409
static void
410 11
mgt_f_read(const char *fn)
411
{
412
        struct f_arg *fa;
413
        char *f, *fnp;
414
415 11
        ALLOC_OBJ(fa, F_ARG_MAGIC);
416 11
        AN(fa);
417 11
        REPLACE(fa->farg, fn);
418 11
        VFIL_setpath(&vcl_path, mgt_vcl_path);
419 11
        if (VFIL_searchpath(vcl_path, NULL, &f, fn, &fnp) || f == NULL) {
420 3
                ARGV_ERR("Cannot read -f file '%s' (%s)\n",
421
                    fnp != NULL ? fnp : fn, vstrerror(errno));
422 0
        }
423 8
        free(fa->farg);
424 8
        fa->farg = fnp;
425 8
        fa->src = f;
426 8
        VTAILQ_INSERT_TAIL(&f_args, fa, list);
427 8
}
428
429
int
430 832
main(int argc, char * const *argv)
431
{
432 832
        int o, eric_fd = -1;
433 832
        unsigned C_flag = 0;
434 832
        unsigned f_flag = 0;
435 832
        unsigned F_flag = 0;
436 832
        const char *b_arg = NULL;
437 832
        const char *i_arg = NULL;
438 832
        const char *j_arg = NULL;
439 832
        const char *h_arg = "critbit";
440 832
        const char *M_arg = NULL;
441 832
        const char *n_arg = NULL;
442 832
        const char *P_arg = NULL;
443 832
        const char *S_arg = NULL;
444 832
        const char *s_arg = "default,100m";
445 832
        const char *W_arg = NULL;
446 832
        int s_arg_given = 0;
447 832
        int novcl = 0;
448 832
        const char *T_arg = "localhost:0";
449
        char *p;
450
        struct cli cli[1];
451
        char *dirname;
452
        char **av;
453
        unsigned u;
454
        struct sigaction sac;
455
        struct vev *e;
456
        struct f_arg *fa;
457
        struct vsb *vsb;
458
        pid_t pid;
459
460 832
        setbuf(stdout, NULL);
461 832
        setbuf(stderr, NULL);
462
463 832
        mgt_tests();
464
465 832
        mgt_initialize(cli);
466
467
        /* Check if first argument is a special flag */
468
469 832
        o = getopt(argc, argv, opt_spec);
470 832
        switch (o) {
471
        case 'x':
472 8
                if (argc != 3)
473 1
                        ARGV_ERR("Too many arguments for -x\n");
474 7
                mgt_x_arg(optarg);
475 7
                exit(0);
476
        case 'V':
477 2
                if (argc != 2)
478 1
                        ARGV_ERR("Too many arguments for -V\n");
479 1
                VCS_Message("varnishd");
480 1
                exit(0);
481
        default:
482 822
                break;
483
        }
484
485
        /* First pass over arguments to determine overall configuration */
486
487 822
        do {
488 10251
                switch (o) {
489
                case '?':
490 2
                        usage();
491 2
                        exit(2);
492
                case 'V':
493
                case 'x':
494 2
                        ARGV_ERR("-%c must be the first argument\n", o);
495 0
                        break;
496
                case 'b':
497 18
                        b_arg = optarg;
498 18
                        break;
499
                case 'C':
500 4
                        C_flag = 1;
501 4
                        break;
502
                case 'd':
503 784
                        d_flag++;
504 784
                        break;
505
                case 'f':
506 34
                        f_flag = 1;
507 34
                        break;
508
                case 'F':
509 11
                        F_flag = 1;
510 11
                        break;
511
                case 'j':
512 10
                        j_arg = optarg;
513 10
                        break;
514
                default:
515 9386
                        break;
516
                }
517 10247
                o = getopt(argc, argv, opt_spec);
518 10247
        } while (o != -1);
519
520 818
        if (argc != optind)
521 1
                ARGV_ERR("Too many arguments (%s...)\n", argv[optind]);
522
523 817
        if (b_arg != NULL && f_flag)
524 1
                ARGV_ERR("Only one of -b or -f can be specified\n");
525
526 816
        if (d_flag && F_flag)
527 1
                ARGV_ERR("Only one of -d or -F can be specified\n");
528
529 815
        if (C_flag && b_arg == NULL && !f_flag)
530 1
                ARGV_ERR("-C needs either -b <backend> or -f <vcl_file>\n");
531
532 814
        if (d_flag && C_flag)
533 1
                ARGV_ERR("-d makes no sense with -C\n");
534
535 813
        if (F_flag && C_flag)
536 1
                ARGV_ERR("-F makes no sense with -C\n");
537
538 812
        if (!d_flag && b_arg == NULL && !f_flag)
539 1
                ARGV_ERR("Neither -b nor -f given. (use -f '' to override)\n");
540
541
        /*
542
         * Start out by closing all unwanted file descriptors we might
543
         * have inherited from sloppy process control daemons.
544
         */
545 811
        VSUB_closefrom(STDERR_FILENO + 1);
546 811
        MCH_TrackHighFd(STDERR_FILENO);
547
548
        /*
549
         * Have Eric Daemonize us if need be
550
         */
551 811
        if (!C_flag && !d_flag && !F_flag) {
552 21
                eric_fd = mgt_eric();
553 21
                MCH_TrackHighFd(eric_fd);
554 21
                heritage.mgt_pid = getpid();
555 21
        }
556
557 811
        VRND_SeedAll();
558
559 811
        vident = mgt_BuildVident();
560
561
        /* Various initializations */
562 811
        VTAILQ_INIT(&heritage.socks);
563 811
        mgt_evb = VEV_New();
564 811
        AN(mgt_evb);
565
566
        /* Initialize transport protocols */
567 811
        XPORT_Init();
568
569 811
        VJ_Init(j_arg);
570
571
        /* Initialize the bogo-IP VSA */
572 811
        VSA_Init();
573
574 811
        optind = 1;
575 811
        optreset = 1;
576 10955
        while ((o = getopt(argc, argv, opt_spec)) != -1) {
577 10185
                switch (o) {
578
                case 'C':
579
                case 'd':
580
                case 'F':
581
                case 'j':
582
                        /* Handled in first pass */
583 771
                        break;
584
                case 'a':
585 803
                        MAC_Arg(optarg);
586 803
                        break;
587
                case 'b':
588 11
                        ALLOC_OBJ(fa, F_ARG_MAGIC);
589 11
                        AN(fa);
590 11
                        REPLACE(fa->farg, "<-b argument>");
591 11
                        vsb = VSB_new_auto();
592 11
                        AN(vsb);
593 11
                        VSB_cat(vsb, "vcl 4.1;\n");
594 11
                        VSB_cat(vsb, "backend default {\n");
595 11
                        if (*optarg != '/')
596 20
                                VSB_printf(vsb, "    .host = \"%s\";\n",
597 10
                                           optarg);
598
                        else
599 2
                                VSB_printf(vsb, "    .path = \"%s\";\n",
600 1
                                           optarg);
601 11
                        VSB_cat(vsb, "}\n");
602 11
                        AZ(VSB_finish(vsb));
603 11
                        fa->src = strdup(VSB_data(vsb));
604 11
                        AN(fa->src);
605 11
                        VSB_destroy(&vsb);
606 11
                        VTAILQ_INSERT_TAIL(&f_args, fa, list);
607 11
                        break;
608
                case 'f':
609 23
                        if (*optarg == '\0') {
610 12
                                novcl = 1;
611 12
                                break;
612
                        }
613 11
                        mgt_f_read(optarg);
614 11
                        break;
615
                case 'h':
616 4
                        h_arg = optarg;
617 4
                        break;
618
                case 'i':
619 3
                        i_arg = optarg;
620 3
                        break;
621
                case 'I':
622 6
                        if (I_fd >= 0)
623 1
                                ARGV_ERR("\tOnly one -I allowed\n");
624 5
                        VJ_master(JAIL_MASTER_FILE);
625 5
                        I_fd = open(optarg, O_RDONLY);
626 5
                        if (I_fd < 0)
627 0
                                ARGV_ERR("\tCant open %s: %s\n",
628
                                    optarg, vstrerror(errno));
629 5
                        VJ_master(JAIL_MASTER_LOW);
630 5
                        break;
631
                case 'l':
632 764
                        av = VAV_Parse(optarg, NULL, ARGV_COMMA);
633 764
                        AN(av);
634 764
                        if (av[0] != NULL)
635 3
                                ARGV_ERR("-l ...: %s\n", av[0]);
636 761
                        if (av[1] != NULL && av[2] != NULL && av[3] != NULL)
637 1
                                ARGV_ERR("Too many sub arguments to -l\n");
638 760
                        if (av[1] != NULL) {
639 760
                                MCF_ParamSet(cli, "vsl_space", av[1]);
640 760
                                cli_check(cli);
641 760
                        }
642 760
                        if (av[1] != NULL && av[2] != NULL) {
643 4
                                fprintf(stderr,
644
                                    "Warning: Ignoring deprecated second"
645
                                    " subargument to -l\n");
646 4
                        }
647 760
                        VAV_Free(av);
648 760
                        break;
649
                case 'M':
650 755
                        M_arg = optarg;
651 755
                        break;
652
                case 'n':
653 773
                        n_arg = optarg;
654 773
                        break;
655
                case 'P':
656 757
                        P_arg = optarg;
657 757
                        break;
658
                case 'p':
659 5426
                        p = strchr(optarg, '=');
660 5426
                        if (p == NULL)
661 0
                                ARGV_ERR("\t-p lacks '='\n");
662 5426
                        AN(p);
663 5426
                        *p++ = '\0';
664 5426
                        MCF_ParamSet(cli, optarg, p);
665 5426
                        *--p = '=';
666 5426
                        cli_check(cli);
667 5426
                        break;
668
                case 'r':
669 1
                        MCF_ParamProtect(cli, optarg);
670 1
                        cli_check(cli);
671 1
                        break;
672
                case 'S':
673 1
                        S_arg = optarg;
674 1
                        break;
675
                case 's':
676 80
                        s_arg_given = 1;
677 80
                        STV_Config(optarg);
678 80
                        break;
679
                case 'T':
680 0
                        if (!strcmp(optarg, "none"))
681 0
                                T_arg = NULL;
682
                        else
683 0
                                T_arg = optarg;
684 0
                        break;
685
                case 't':
686 1
                        MCF_ParamSet(cli, "default_ttl", optarg);
687 1
                        break;
688
                case 'W':
689 6
                        W_arg = optarg;
690 6
                        break;
691
                default:
692 0
                        WRONG("Error in argument parsing");
693 0
                }
694
        }
695 764
        assert(argc == optind);
696
697
        /* XXX: we can have multiple CLI actions above, is this enough ? */
698 764
        if (cli[0].result != CLIS_OK) {
699 0
                AZ(VSB_finish(cli[0].sb));
700 0
                ARGV_ERR("Failed parameter creation:\n%s\n",
701
                    VSB_data(cli[0].sb));
702 0
        }
703
704 764
        assert(d_flag == 0 || F_flag == 0);
705
706 764
        if (C_flag) {
707 1
                if (n_arg == NULL) {
708 1
                        vsb = VSB_new_auto();
709 1
                        AN(vsb);
710 1
                        if (getenv("TMPDIR") != NULL)
711 1
                                VSB_printf(vsb, "%s", getenv("TMPDIR"));
712
                        else
713 0
                                VSB_cat(vsb, "/tmp");
714 1
                        VSB_cat(vsb, "/varnishd_C_XXXXXXX");
715 1
                        AZ(VSB_finish(vsb));
716 1
                        Cn_arg = strdup(VSB_data(vsb));
717 1
                        AN(Cn_arg);
718 1
                        VSB_destroy(&vsb);
719 1
                        AN(mkdtemp(Cn_arg));
720 1
                        AZ(chmod(Cn_arg, 0755));
721 1
                        AZ(atexit(mgt_Cflag_atexit));
722 1
                        n_arg = Cn_arg;
723 1
                }
724 1
        }
725
726 764
        if (S_arg != NULL && !strcmp(S_arg, "none")) {
727 0
                fprintf(stderr,
728
                    "Warning: CLI authentication disabled.\n");
729 764
        } else if (S_arg != NULL) {
730 1
                VJ_master(JAIL_MASTER_FILE);
731 1
                o = open(S_arg, O_RDONLY, 0);
732 1
                if (o < 0)
733 1
                        ARGV_ERR("Cannot open -S file (%s): %s\n",
734
                            S_arg, vstrerror(errno));
735 0
                closefd(&o);
736 0
                VJ_master(JAIL_MASTER_LOW);
737 0
        }
738
739 763
        if (VIN_n_Arg(n_arg, &dirname) != 0)
740 0
                ARGV_ERR("Invalid instance (-n) name: %s\n", vstrerror(errno));
741
742 763
        if (i_arg == NULL || *i_arg == '\0')
743 760
                i_arg = mgt_HostName();
744 763
        heritage.identity = i_arg;
745
746 763
        mgt_ProcTitle("Mgt");
747
748 763
        openlog("varnishd", LOG_PID, LOG_LOCAL0);
749
750 763
        if (VJ_make_workdir(dirname))
751 0
                ARGV_ERR("Cannot create working directory (%s): %s\n",
752
                    dirname, vstrerror(errno));
753
754 763
        if (VJ_make_subdir("vmod_cache", "VMOD cache", NULL)) {
755 0
                ARGV_ERR(
756
                    "Cannot create vmod directory (%s/vmod_cache): %s\n",
757
                    dirname, vstrerror(errno));
758 0
        }
759
760 763
        vsb = VSB_new_auto();
761 763
        AN(vsb);
762 763
        VSB_printf(vsb, "%s/_.pid", dirname);
763 763
        AZ(VSB_finish(vsb));
764 763
        VJ_master(JAIL_MASTER_FILE);
765 763
        pfh1 = VPF_Open(VSB_data(vsb), 0644, &pid);
766 763
        if (pfh1 == NULL && errno == EEXIST)
767 1
                ARGV_ERR("Varnishd is already running (pid=%jd)\n",
768
                    (intmax_t)pid);
769 762
        if (pfh1 == NULL)
770 0
                ARGV_ERR("Could not open pid-file (%s): %s\n",
771
                    VSB_data(vsb), vstrerror(errno));
772 762
        VSB_destroy(&vsb);
773 762
        if (P_arg) {
774 757
                pfh2 = VPF_Open(P_arg, 0644, &pid);
775 757
                if (pfh2 == NULL && errno == EEXIST)
776 1
                        ARGV_ERR("Varnishd is already running (pid=%jd)\n",
777
                            (intmax_t)pid);
778 756
                if (pfh2 == NULL)
779 1
                        ARGV_ERR("Could not open pid-file (%s): %s\n",
780
                            P_arg, vstrerror(errno));
781 755
        }
782 760
        VJ_master(JAIL_MASTER_LOW);
783
784
        /* If no -s argument specified, process default -s argument */
785 760
        if (!s_arg_given)
786 708
                STV_Config(s_arg);
787
788
        /* Configure Transient storage, if user did not */
789 760
        STV_Config_Transient();
790
791 760
        mgt_vcl_init();
792
793 760
        if (C_flag) {
794 2
                VTAILQ_FOREACH(fa, &f_args, list) {
795 2
                        mgt_vcl_startup(cli, fa->src,
796 1
                            VTAILQ_NEXT(fa, list) == NULL ? "boot" : NULL,
797 1
                            fa->farg, 1);
798 1
                        AZ(VSB_finish(cli->sb));
799 1
                        fprintf(stderr, "%s\n", VSB_data(cli->sb));
800 1
                        VSB_clear(cli->sb);
801 1
                }
802 1
                exit(cli->result == CLIS_OK ? 0 : 2);
803
        } else {
804 770
                while (!VTAILQ_EMPTY(&f_args)) {
805 12
                        fa = VTAILQ_FIRST(&f_args);
806 12
                        VTAILQ_REMOVE(&f_args, fa, list);
807 24
                        mgt_vcl_startup(cli, fa->src,
808 12
                            VTAILQ_EMPTY(&f_args) ? "boot" : NULL,
809 12
                            fa->farg, 0);
810 12
                        cli_check(cli);
811 12
                        free(fa->farg);
812 12
                        free(fa->src);
813 12
                        FREE_OBJ(fa);
814
                }
815
        }
816
817 758
        if (VTAILQ_EMPTY(&heritage.socks))
818 0
                MAC_Arg(":80\0");       // XXX: extra NUL for FlexeLint
819
820 758
        assert(!VTAILQ_EMPTY(&heritage.socks));
821
822 758
        HSH_config(h_arg);
823
824 758
        Wait_config(W_arg);
825
826 758
        mgt_SHM_Init();
827
828 758
        mgt_SHM_static_alloc(i_arg, strlen(i_arg) + 1L, "Arg", "-i");
829 758
        VSC_C_mgt = VSC_mgt_New(NULL, NULL, "");
830
831 758
        if (M_arg != NULL)
832 755
                mgt_cli_master(M_arg);
833 758
        if (T_arg != NULL)
834 758
                mgt_cli_telnet(T_arg);
835
836 758
        AZ(VSB_finish(vident));
837
838 758
        if (S_arg == NULL)
839 758
                S_arg = make_secret(dirname);
840 758
        AN(S_arg);
841
842 758
        VPF_Write(pfh1);
843 758
        if (pfh2 != NULL)
844 755
                VPF_Write(pfh2);
845
846 758
        MGT_Complain(C_DEBUG, "Version: %s", VCS_String("V"));
847 758
        MGT_Complain(C_DEBUG, "Platform: %s", VSB_data(vident) + 1);
848
849 758
        if (d_flag)
850 755
                mgt_cli_setup(0, 1, 1, "debug", mgt_stdin_close, NULL);
851
852 758
        if (strcmp(S_arg, "none"))
853 758
                mgt_cli_secret(S_arg);
854
855 758
        memset(&sac, 0, sizeof sac);
856 758
        sac.sa_handler = SIG_IGN;
857 758
        sac.sa_flags = SA_RESTART;
858
859 758
        AZ(sigaction(SIGPIPE, &sac, NULL));
860 758
        AZ(sigaction(SIGHUP, &sac, NULL));
861
862 758
        MCH_Init();
863
864 758
        if (I_fd >= 0) {
865 2
                fprintf(stderr, "BEGIN of -I file processing\n");
866
                /* We must dup stderr, because VCLS closes the output fd */
867 2
                mgt_cli_setup(I_fd, dup(2), 1, "-I file", mgt_I_close, stderr);
868 2
                while (I_fd >= 0) {
869 2
                        o = VEV_Once(mgt_evb);
870 2
                        if (o != 1)
871 0
                                MGT_Complain(C_ERR,
872 0
                                    "VEV_Once() = %d", o);
873
                }
874 0
        }
875 756
        assert(I_fd == -1);
876
877 756
        if (!d_flag && !mgt_has_vcl() && !novcl)
878 0
                MGT_Complain(C_ERR, "No VCL loaded yet");
879
880 756
        if (mgt_has_vcl() && ! d_flag)
881 0
                u = MCH_Start_Child();
882
        else
883 756
                u = 0;
884
885 756
        if (eric_fd >= 0)
886 0
                mgt_eric_im_done(eric_fd, u);
887
888 756
        if (u)
889 0
                exit(u);
890
891
        /* Failure is no longer an option */
892
893 756
        if (F_flag)
894 1
                VFIL_null_fd(STDIN_FILENO);
895
896 756
        e = VEV_Alloc();
897 756
        AN(e);
898 756
        e->callback = mgt_uptime;
899 756
        e->timeout = 1.0;
900 756
        e->name = "mgt_uptime";
901 756
        AZ(VEV_Start(mgt_evb, e));
902
903 756
        e = VEV_Alloc();
904 756
        AN(e);
905 756
        e->sig = SIGTERM;
906 756
        e->callback = mgt_sigint;
907 756
        e->name = "SIGTERM";
908 756
        AZ(VEV_Start(mgt_evb, e));
909
910 756
        e = VEV_Alloc();
911 756
        AN(e);
912 756
        e->sig = SIGINT;
913 756
        e->callback = mgt_sigint;
914 756
        e->name = "SIGINT";
915 756
        AZ(VEV_Start(mgt_evb, e));
916
917 756
        o = VEV_Loop(mgt_evb);
918 756
        if (o != 0 && o != -42)
919 0
                MGT_Complain(C_ERR, "VEV_Loop() = %d", o);
920
921 756
        MGT_Complain(C_INFO, "manager stopping child");
922 756
        MCH_Stop_Child();
923 756
        MGT_Complain(C_INFO, "manager dies");
924 756
        mgt_cli_close_all();
925 756
        VEV_Destroy(&mgt_evb);
926 756
        VPF_Remove(pfh1);
927 756
        if (pfh2 != NULL)
928 755
                VPF_Remove(pfh2);
929 756
        exit(exit_status);
930 0
}