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