vinyl-cache/bin/vinyld/mgt/mgt_main.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2017 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 *
30
 * The management process and CLI handling
31
 */
32
33
#include "config.h"
34
35
#include <ctype.h>
36
#include <fcntl.h>
37
#include <signal.h>
38
#include <stdarg.h>
39
#include <stdio.h>
40
#include <stdlib.h>
41
#include <string.h>
42
#include <syslog.h>
43
#include <unistd.h>
44
#include <sys/resource.h>
45
#include <sys/stat.h>
46
#include <sys/socket.h>
47
48
#include "mgt/mgt.h"
49
#include "acceptor/mgt_acceptor.h"
50
#include "common/heritage.h"
51
52
#include "hash/hash_slinger.h"
53
#include "libvcc.h"
54
#include "vcli_serve.h"
55
#include "vct.h"
56
#include "vev.h"
57
#include "vfil.h"
58
#include "vin.h"
59
#include "vpf.h"
60
#include "vrnd.h"
61
#include "vsha256.h"
62
#include "vsub.h"
63
#include "vtim.h"
64
#include "waiter/mgt_waiter.h"
65
#include "vsa.h"
66
#include "vus.h"
67
68
struct heritage         heritage;
69
unsigned                d_flag = 0;
70
struct vev_root         *mgt_evb;
71
int                     exit_status = 0;
72
struct vsb              *vident;
73
struct VSC_mgt          *VSC_C_mgt;
74
static int              I_fd = -1;
75
static char             *workdir;
76
77
static struct vfil_path *vcl_path = NULL;
78
79
static const char opt_spec[] = "?a:b:CdE:f:Fh:i:I:j:l:M:n:P:p:r:S:s:T:t:VW:x:";
80
81
/*--------------------------------------------------------------------*/
82
83
// Basic options documentation with compile time dependencies
84
static void
85 4
mgt_DumpOptions(void)
86
{
87 4
        printf(".. _opt_n:\n\n");
88 4
        printf("-n workdir\n\n");
89 4
        printf("  Runtime directory for the shared memory, "
90
               "compiled VCLs etc.\n\n");
91 4
        printf("  In performance critical applications, this directory "
92
               "should be on a RAM backed filesystem.\n\n");
93 4
        printf("  When running multiple vinyld instances, separate "
94
               "directories need to be used.\n\n");
95 4
        VIN_DumpDefaults();
96 4
}
97
98
static void
99 8
usage(void)
100
{
101
        char *p;
102
103
#define FMT_NONL "  %-35s # %s"
104
#define FMT FMT_NONL "\n"
105
106 8
        printf( "Usage: vinyld [options]\n");
107
108 8
        printf("\nBasic options:\n");
109
110 8
        printf(FMT, "[-a [name=][listen_address",
111
            "HTTP listen address, protocol, options.");
112 8
        printf(FMT, "     [,PROTO|,option=value,...]]",
113
            "Can be specified multiple times.");
114 8
        printf(FMT, "",
115
            "  default: \":80,HTTP\"");
116 8
        printf(FMT, "  options:",
117
            "Proto can be \"PROXY\" or \"HTTP\" (default)");
118 8
        printf(FMT, "    [,user=<u>][,group=<g>]",
119
            "user, group and mode set permissions for");
120 8
        printf(FMT, "    [,mode=<m>]",
121
            "  a Unix domain socket.");
122 8
        printf(FMT, "-b none", "No backend");
123 8
        printf(FMT, "-b [addr[:port]|path]", "Backend address and port");
124 8
        printf(FMT, "", "  or socket file path");
125 8
        printf(FMT, "", "  default: \":80\"");
126 8
        printf(FMT, "-f vclfile", "VCL program");
127 8
        printf(FMT, "", "Can be specified multiple times.");
128 8
        printf(FMT, "-n dir", "Working directory");
129
130 8
        p = VIN_n_Arg(NULL);
131 8
        AN(p);
132 8
        printf(FMT_NONL "  default: %s\n", "", "", p);
133 8
        free(p);
134
135 8
        printf("\n-b can be used only once, and not together with -f\n");
136
137 8
        printf("\nDocumentation options:\n");
138 8
        printf(FMT, "-?", "Prints this usage message");
139 8
        printf(FMT, "-x parameter", "Parameter documentation in RST format");
140 8
        printf(FMT, "-x parameter-json", "Parameter documentation in JSON format");
141 8
        printf(FMT, "-x vsl", "VSL record documentation");
142 8
        printf(FMT, "-x cli", "CLI command documentation");
143 8
        printf(FMT, "-x builtin", "Builtin VCL program");
144 8
        printf(FMT, "-x optstring", "List of getopt options");
145 8
        printf(FMT, "-x options", "Dynamic options documentation");
146
147 8
        printf("\nOperations options:\n");
148
149 8
        printf(FMT, "-F", "Run in foreground");
150 8
        printf(FMT, "-T address[:port]", "CLI address");
151 8
        printf(FMT, "", "Can be specified multiple times.");
152 8
        printf(FMT, "-M address:port", "Reverse CLI destination");
153 8
        printf(FMT, "", "Can be specified multiple times.");
154 8
        printf(FMT, "-P file", "PID file");
155 8
        printf(FMT, "-i identity", "Identity of Vinyl Cache instance");
156 8
        printf(FMT, "-I clifile", "Initialization CLI commands");
157 8
        printf(FMT, "-E extension", "Load extension");
158
159 8
        printf("\nTuning options:\n");
160
161 8
        printf(FMT, "-t TTL", "Default TTL");
162 8
        printf(FMT, "-p param=value", "set parameter");
163 8
        printf(FMT, "", "Can be specified multiple times.");
164
165 8
        printf(FMT, "-s [name=]kind[,options]", "Storage specification");
166 8
        printf(FMT, "", "Can be specified multiple times.");
167
#ifdef HAVE_UMEM_H
168
        printf(FMT, "", "  -s default (=umem)");
169
        printf(FMT, "", "  -s umem");
170
#else
171 8
        printf(FMT, "", "  -s default (=malloc)");
172
#endif
173 8
        printf(FMT, "", "  -s malloc");
174 8
        printf(FMT, "", "  -s file");
175
176 8
        printf(FMT, "-l vsl", "Size of shared memory log");
177 8
        printf(FMT, "", "  vsl: space for VSL records [80m]");
178
179 8
        printf("\nSecurity options:\n");
180
181 8
        printf(FMT, "-r param[,param...]", "Set parameters read-only from CLI");
182 8
        printf(FMT, "", "Can be specified multiple times.");
183 8
        printf(FMT, "-S secret-file", "Secret file for CLI authentication");
184 8
        printf(FMT, "-j jail[,options]", "Jail specification");
185
#ifdef HAVE_SETPPRIV
186
        printf(FMT, "", "  -j solaris");
187
#endif
188
#ifdef __linux__
189
        printf(FMT, "", "  -j linux");
190
#endif
191 8
        printf(FMT, "", "  -j unix");
192 8
        printf(FMT, "", "  -j none");
193
194 8
        printf("\nAdvanced/Dev/Debug options:\n");
195
196 8
        printf(FMT, "-d", "debug mode");
197 8
        printf(FMT, "", "Stay in foreground, CLI on stdin.");
198 8
        printf(FMT, "-C", "Output VCL code compiled to C language");
199 8
        printf(FMT, "-V", "version");
200 8
        printf(FMT, "-h kind[,options]", "Hash specification");
201 8
        printf(FMT, "-W waiter", "Waiter implementation");
202 8
}
203
204
/*--------------------------------------------------------------------*/
205
206
struct arg_list {
207
        unsigned                magic;
208
#define ARG_LIST_MAGIC          0x7b0d1bc4
209
        char                    arg[2];
210
        char                    *val;
211
        VTAILQ_ENTRY(arg_list)  list;
212
        void                    *priv;
213
};
214
215
static VTAILQ_HEAD(, arg_list) arglist = VTAILQ_HEAD_INITIALIZER(arglist);
216
217
static struct arg_list *
218 57832
arg_list_add(const char arg, const char *val)
219
{
220
        struct arg_list *alp;
221
222 57832
        ALLOC_OBJ(alp, ARG_LIST_MAGIC);
223 57832
        AN(alp);
224 57832
        alp->arg[0] = arg;
225 57832
        alp->arg[1] = '\0';
226 57832
        REPLACE(alp->val, val);
227 57832
        VTAILQ_INSERT_TAIL(&arglist, alp, list);
228 57832
        return (alp);
229
}
230
231
static unsigned
232 17156
arg_list_count(const char *arg)
233
{
234 17156
        unsigned retval = 0;
235
        struct arg_list *alp;
236
237 232880
        VTAILQ_FOREACH(alp, &arglist, list) {
238 215724
                if (!vstrcmp(alp->arg, arg))
239 672
                        retval++;
240 215724
        }
241 17156
        return (retval);
242
}
243
244
/*--------------------------------------------------------------------*/
245
246
static void
247 57808
cli_check(const struct cli *cli)
248
{
249 57808
        if (cli->result == CLIS_OK || cli->result == CLIS_TRUNCATED) {
250 57800
                AZ(VSB_finish(cli->sb));
251 57800
                if (VSB_len(cli->sb) > 0)
252 8
                        fprintf(stderr, "Warnings:\n%s\n", VSB_data(cli->sb));
253 57800
                VSB_clear(cli->sb);
254 57800
                return;
255
        }
256 8
        AZ(VSB_finish(cli->sb));
257 8
        fprintf(stderr, "Error:\n%s\n", VSB_data(cli->sb));
258 8
        exit(2);
259
}
260
261
/*--------------------------------------------------------------------
262
 * This function is called when the CLI on stdin is closed.
263
 */
264
265
static int v_matchproto_(mgt_cli_close_f)
266 3980
mgt_stdin_close(void *priv)
267
{
268
269 3980
        (void)priv;
270 3980
        return (-42);
271
}
272
273
/*--------------------------------------------------------------------
274
 * Autogenerate a -S file using strong random bits from the kernel.
275
 */
276
277
static void
278 19148
mgt_secret_atexit(void)
279
{
280
281
        /* Only master process */
282 19148
        if (getpid() != heritage.mgt_pid)
283 15112
                return;
284 4036
        VJ_master(JAIL_MASTER_FILE);
285 4036
        (void)unlink("_.secret");
286 4036
        VJ_master(JAIL_MASTER_LOW);
287 19148
}
288
289
static const char *
290 4040
make_secret(const char *dirname)
291
{
292
        char *fn;
293
        int fdo;
294
        int i;
295
        unsigned char b;
296
297 4040
        assert(asprintf(&fn, "%s/_.secret", dirname) > 0);
298
299 4040
        VJ_master(JAIL_MASTER_FILE);
300 4040
        if (unlink(fn) < 0 && errno != ENOENT) {
301 4
                ARGV_ERR("Cannot remove preexisting secret-file in %s (%s)\n",
302
                    dirname, VAS_errtxt(errno));
303 0
        }
304
305 0
        fdo = open(fn, O_RDWR|O_CREAT|O_EXCL, 0640);
306 0
        if (fdo < 0) {
307 0
                ARGV_ERR("Cannot create secret-file in %s (%s)\n",
308
                    dirname, VAS_errtxt(errno));
309 0
        }
310
311 1037252
        for (i = 0; i < 256; i++) {
312 1033216
                AZ(VRND_RandomCrypto(&b, 1));
313 1033216
                assert(1 == write(fdo, &b, 1));
314 1033216
        }
315 4036
        closefd(&fdo);
316 4036
        VJ_master(JAIL_MASTER_LOW);
317 4036
        AZ(atexit(mgt_secret_atexit));
318 4036
        return (fn);
319
}
320
321
static void
322 48
mgt_Cflag_atexit(void)
323
{
324
325
        /* Only master process */
326 48
        if (getpid() != heritage.mgt_pid)
327 32
                return;
328 16
        if (arg_list_count("E")) {
329 0
                vext_cleanup(1);
330 0
                VJ_rmdir("vext_cache");
331 0
        }
332 16
        VJ_rmdir("vmod_cache");
333 16
        VJ_rmdir("worker_tmpdir");
334 16
        (void)chdir("/");
335 16
        VJ_rmdir(workdir);
336 48
}
337
338
/*--------------------------------------------------------------------*/
339
340
static void
341 4376
mgt_tests(void)
342
{
343 4376
        assert(VTIM_parse("Sun, 06 Nov 1994 08:49:37 GMT") == 784111777);
344 4376
        assert(VTIM_parse("Sunday, 06-Nov-94 08:49:37 GMT") == 784111777);
345 4376
        assert(VTIM_parse("Sun Nov  6 08:49:37 1994") == 784111777);
346
347
        /* Check that our VSHA256 works */
348 4376
        VSHA256_Test();
349 4376
}
350
351
static void
352 4376
mgt_initialize(struct cli *cli)
353
{
354
        static unsigned clilim = 32768;
355
356
        /* for ASSERT_MGT() */
357 4376
        heritage.mgt_pid = getpid();
358
359
        /* Create a cli for convenience in otherwise CLI functions */
360 4376
        INIT_OBJ(cli, CLI_MAGIC);
361 4376
        cli[0].sb = VSB_new_auto();
362 4376
        AN(cli[0].sb);
363 4376
        cli[0].result = CLIS_OK;
364 4376
        cli[0].limit = &clilim;
365
366 4376
        mgt_cli_init_cls();             // CLI commands can be registered
367
368 4376
        MCF_InitParams(cli);
369
370 4376
        VCC_VCL_Range(&heritage.min_vcl_version, &heritage.max_vcl_version);
371
372 4376
        cli_check(cli);
373 4376
}
374
375
static void
376 40
mgt_x_arg(const char *x_arg)
377
{
378 40
        if (!vstrcmp(x_arg, "parameter"))
379 8
                MCF_DumpRstParam();
380 32
        else if (!vstrcmp(x_arg, "parameter-json"))
381 4
                MCF_DumpJsonParam();
382 28
        else if (!vstrcmp(x_arg, "vsl"))
383 4
                mgt_DumpRstVsl();
384 24
        else if (!vstrcmp(x_arg, "cli"))
385 8
                mgt_DumpRstCli();
386 16
        else if (!vstrcmp(x_arg, "builtin"))
387 4
                mgt_DumpBuiltin();
388 12
        else if (!vstrcmp(x_arg, "optstring"))
389 4
                (void)printf("%s\n", opt_spec);
390 8
        else if (!vstrcmp(x_arg, "options"))
391 4
                mgt_DumpOptions();
392
        else
393 4
                ARGV_ERR("Invalid -x argument\n");
394 36
}
395
396
/*--------------------------------------------------------------------*/
397
398
#define ERIC_MAGIC 0x2246988a           /* Eric is not random */
399
400
static int
401 96
mgt_eric(void)
402
{
403
        int eric_pipes[2];
404
        unsigned u;
405
        ssize_t sz;
406
407 96
        AZ(pipe(eric_pipes));
408
409 96
        switch (fork()) {
410
        case -1:
411 0
                fprintf(stderr, "Fork() failed: %s\n", VAS_errtxt(errno));
412 0
                exit(-1);
413
        case 0:
414 96
                closefd(&eric_pipes[0]);
415 96
                assert(setsid() > 1);
416
417 96
                VFIL_null_fd(STDIN_FILENO);
418 96
                return (eric_pipes[1]);
419
        default:
420 96
                break;
421
        }
422 96
        closefd(&eric_pipes[1]);
423 96
        sz = read(eric_pipes[0], &u, sizeof u);
424 96
        if (sz == sizeof u && u == ERIC_MAGIC)
425 0
                exit(0);
426 32
        else if (sz == sizeof u && u != 0)
427 32
                exit(u);
428
        else
429 64
                exit(-1);
430
}
431
432
static void
433 32
mgt_eric_im_done(int eric_fd, unsigned u)
434
{
435
436 32
        if (u == 0)
437 0
                u = ERIC_MAGIC;
438
439 32
        VFIL_null_fd(STDIN_FILENO);
440 32
        VFIL_null_fd(STDOUT_FILENO);
441 32
        VFIL_null_fd(STDERR_FILENO);
442
443 32
        assert(write(eric_fd, &u, sizeof u) == sizeof u);
444 32
        closefd(&eric_fd);
445 32
}
446
447
/*--------------------------------------------------------------------*/
448
449
static int v_matchproto_(vev_cb_f)
450 16
mgt_sigint(const struct vev *e, int what)
451
{
452
453 16
        (void)what;
454 16
        MGT_Complain(C_INFO, "Manager got %s from PID %jd",
455 16
            e->name, (intmax_t)e->siginfo->si_pid);
456 16
        (void)fflush(stdout);
457 16
        if (MCH_Running())
458 12
                MCH_Stop_Child();
459 16
        return (-42);
460
}
461
462
/*--------------------------------------------------------------------*/
463
464
static int v_matchproto_(vev_cb_f)
465 10435
mgt_uptime(const struct vev *e, int what)
466
{
467
        static double mgt_uptime_t0 = 0;
468
469 10435
        (void)e;
470 10435
        (void)what;
471 10435
        AN(VSC_C_mgt);
472 10435
        if (mgt_uptime_t0 == 0)
473 3864
                mgt_uptime_t0 = VTIM_real();
474 10435
        VSC_C_mgt->uptime = (uint64_t)(VTIM_real() - mgt_uptime_t0);
475 10435
        return (0);
476
}
477
478
/*--------------------------------------------------------------------*/
479
480
static int v_matchproto_(mgt_cli_close_f)
481 4
mgt_I_close(void *priv)
482
{
483 4
        (void)priv;
484 4
        fprintf(stderr, "END of -I file processing\n");
485 4
        I_fd = -1;
486 4
        return (0);
487
}
488
489
/*--------------------------------------------------------------------*/
490
491
struct f_arg {
492
        unsigned                magic;
493
#define F_ARG_MAGIC             0x840649a8
494
        char                    *farg;
495
        char                    *src;
496
};
497
498
static struct f_arg *
499 52
mgt_f_read(const char *fn)
500
{
501
        struct f_arg *fa;
502
        char *f, *fnp;
503
504 52
        ALLOC_OBJ(fa, F_ARG_MAGIC);
505 52
        AN(fa);
506 52
        REPLACE(fa->farg, fn);
507 52
        VFIL_setpath(&vcl_path, mgt_vcl_path);
508 52
        if (VFIL_searchpath(vcl_path, NULL, &f, fn, &fnp) || f == NULL) {
509 12
                ARGV_ERR("Cannot read -f file '%s' (%s)\n",
510
                    fnp != NULL ? fnp : fn, VAS_errtxt(errno));
511 0
        }
512 40
        free(fa->farg);
513 40
        fa->farg = fnp;
514 40
        fa->src = f;
515 40
        return (fa);
516
}
517
518
static void
519 116
mgt_b_conv(const char *b_arg)
520
{
521
        struct f_arg *fa;
522
        struct vsb *vsb;
523
524 116
        ALLOC_OBJ(fa, F_ARG_MAGIC);
525 116
        AN(fa);
526 116
        REPLACE(fa->farg, "<-b argument>");
527 116
        vsb = VSB_new_auto();
528 116
        AN(vsb);
529 116
        VSB_cat(vsb, "vcl 4.1;\n");
530 116
        VSB_cat(vsb, "backend default ");
531 116
        if (!strcasecmp(b_arg, "none"))
532 68
                VSB_cat(vsb, "none;\n");
533 48
        else if (VUS_is(b_arg))
534 4
                VSB_printf(vsb, "{\n    .path = \"%s\";\n}\n", b_arg);
535
        else
536 44
                VSB_printf(vsb, "{\n    .host = \"%s\";\n}\n", b_arg);
537 116
        AZ(VSB_finish(vsb));
538 116
        fa->src = strdup(VSB_data(vsb));
539 116
        AN(fa->src);
540 116
        VSB_destroy(&vsb);
541 116
        AZ(arg_list_count("f"));
542 116
        arg_list_add('f', "")->priv = fa;
543 116
}
544
545
static int
546 108
mgt_process_f_arg(struct cli *cli, unsigned C_flag, void **fap)
547
{
548 108
        int retval = 0;
549
        struct f_arg *fa;
550 108
        const char *name = NULL;
551
552 108
        TAKE_OBJ_NOTNULL(fa, fap, F_ARG_MAGIC);
553 108
        if (arg_list_count("f") == 1)
554 100
                name = "boot";
555 108
        mgt_vcl_startup(cli, fa->src, name, fa->farg, C_flag);
556 108
        if (C_flag) {
557 16
                if (cli->result != CLIS_OK &&
558 0
                    cli->result != CLIS_TRUNCATED)
559 0
                        retval = 2;
560 16
                AZ(VSB_finish(cli->sb));
561 16
                fprintf(stderr, "%s\n", VSB_data(cli->sb));
562 16
                VSB_clear(cli->sb);
563 16
        } else {
564 92
                cli_check(cli);
565
        }
566 108
        free(fa->farg);
567 108
        free(fa->src);
568 108
        FREE_OBJ(fa);
569 104
        return (retval);
570
}
571
572
static char *
573 8
create_bogo_n_arg(void)
574
{
575
        struct vsb *vsb;
576
        char *p;
577
578 8
        vsb = VSB_new_auto();
579 8
        AN(vsb);
580 8
        if (getenv("TMPDIR") != NULL)
581 8
                VSB_printf(vsb, "%s", getenv("TMPDIR"));
582
        else
583 0
                VSB_cat(vsb, "/tmp");
584 8
        VSB_cat(vsb, "/vinyld_C_XXXXXXX");
585 8
        AZ(VSB_finish(vsb));
586 8
        p = strdup(VSB_data(vsb));
587 8
        AN(p);
588 8
        VSB_destroy(&vsb);
589 8
        AN(mkdtemp(p));
590 8
        AZ(chmod(p, 0750));
591 8
        return (p);
592
}
593
594
static struct vpf_fh *
595 8036
create_pid_file(pid_t *ppid, const char *fmt, ...)
596
{
597
        struct vsb *vsb;
598
        va_list ap;
599
        struct vpf_fh *pfh;
600
601 8036
        va_start(ap, fmt);
602 8036
        vsb = VSB_new_auto();
603 8036
        AN(vsb);
604 8036
        VSB_vprintf(vsb, fmt, ap);
605 8036
        AZ(VSB_finish(vsb));
606 8036
        VJ_master(JAIL_MASTER_FILE);
607 8036
        pfh = VPF_Open(VSB_data(vsb), 0644, ppid);
608 8036
        if (pfh == NULL && errno == EEXIST)
609 12
                ARGV_ERR(
610
                    "vinyld is already running (pid=%jd) (pidfile=%s)\n",
611
                    (intmax_t)*ppid, VSB_data(vsb));
612 4
        if (pfh == NULL)
613 4
                ARGV_ERR("Could not open pid-file (%s): %s\n",
614
                    VSB_data(vsb), VAS_errtxt(errno));
615 8020
        VJ_master(JAIL_MASTER_LOW);
616 8020
        VSB_destroy(&vsb);
617 8020
        va_end(ap);
618 8020
        return (pfh);
619
}
620
621
int
622 4380
main(int argc, char * const *argv)
623
{
624 4380
        int o, eric_fd = -1;
625 4380
        unsigned C_flag = 0;
626 4380
        unsigned F_flag = 0;
627 4380
        const char *b_arg = NULL;
628 4380
        const char *i_arg = NULL;
629 4380
        const char *j_arg = NULL;
630 4380
        const char *h_arg = "critbit";
631 4380
        const char *n_arg = getenv("VINYL_DEFAULT_N");
632 4380
        const char *S_arg = NULL;
633 4380
        const char *s_arg = "default,100m";
634 4380
        const char *W_arg = NULL;
635
        const char *c;
636
        char *p;
637
        struct cli cli[1];
638
        const char *err;
639
        unsigned u;
640
        struct sigaction sac;
641
        struct vev *e;
642
        pid_t pid;
643
        struct arg_list *alp;
644 4380
        int first_arg = 1;
645
        struct vsb *vsb;
646
647 4380
        if (argc == 2 && !vstrcmp(argv[1], "--optstring")) {
648 4
                printf("%s\n", opt_spec);
649 4
                exit(0);
650
        }
651
652 4376
        heritage.argc = argc;
653 4376
        heritage.argv = argv;
654
655 4376
        setbuf(stdout, NULL);
656 4376
        setbuf(stderr, NULL);
657
658 4376
        mgt_tests();
659
660 4376
        mgt_initialize(cli);
661
662 66280
        for (; (o = getopt(argc, argv, opt_spec)) != -1; first_arg = 0) {
663 61972
                switch (o) {
664
                case 'V':
665 12
                        if (!first_arg)
666 4
                                ARGV_ERR("-V must be the first argument\n");
667 4
                        if (argc != 2)
668 4
                                ARGV_ERR("Too many arguments for -V\n");
669 4
                        VCS_Message("vinyld");
670 4
                        exit(0);
671
                case 'x':
672 48
                        if (!first_arg)
673 4
                                ARGV_ERR("-x must be the first argument\n");
674 4
                        if (argc != 3)
675 4
                                ARGV_ERR("Too many arguments for -x\n");
676 40
                        mgt_x_arg(optarg);
677 40
                        exit(0);
678
                case 'b':
679 128
                        b_arg = optarg;
680 128
                        break;
681
                case 'C':
682 28
                        C_flag = 1;
683 28
                        break;
684
                case 'd':
685 4132
                        d_flag++;
686 4132
                        break;
687
                case 'F':
688 56
                        F_flag = 1;
689 56
                        break;
690
                case 'j':
691 44
                        j_arg = optarg;
692 44
                        break;
693
                case 'h':
694 16
                        h_arg = optarg;
695 16
                        break;
696
                case 'i':
697 3988
                        i_arg = optarg;
698 3988
                        break;
699
                case 'l':
700 3996
                        arg_list_add('p', "vsl_space")->priv = optarg;
701 3996
                        break;
702
                case 'n':
703 4096
                        n_arg = optarg;
704 4096
                        break;
705
                case 'S':
706 4
                        S_arg = optarg;
707 4
                        break;
708
                case 't':
709 4
                        arg_list_add('p', "default_ttl")->priv = optarg;
710 4
                        break;
711
                case 'W':
712 24
                        W_arg = optarg;
713 24
                        break;
714
                case 'a':
715
                case 'E':
716
                case 'f':
717
                case 'I':
718
                case 'p':
719
                case 'P':
720
                case 'M':
721
                case 'r':
722
                case 's':
723
                case 'T':
724 45388
                        (void)arg_list_add(o, optarg);
725 45388
                        break;
726
                default:
727 8
                        usage();
728 8
                        exit(2);
729
                }
730 61904
        }
731
732 4308
        if (argc != optind)
733 4
                ARGV_ERR("Too many arguments (%s...)\n", argv[optind]);
734
735 128
        if (b_arg != NULL && arg_list_count("f"))
736 4
                ARGV_ERR("Only one of -b or -f can be specified\n");
737
738 4124
        if (d_flag && F_flag)
739 4
                ARGV_ERR("Only one of -d or -F can be specified\n");
740
741 28
        if (C_flag && b_arg == NULL && !arg_list_count("f"))
742 4
                ARGV_ERR("-C needs either -b <backend> or -f <vcl_file>\n");
743
744 4120
        if (d_flag && C_flag)
745 4
                ARGV_ERR("-d makes no sense with -C\n");
746
747 52
        if (F_flag && C_flag)
748 4
                ARGV_ERR("-F makes no sense with -C\n");
749
750 168
        if (!d_flag && b_arg == NULL && !arg_list_count("f"))
751 4
                ARGV_ERR("Neither -b nor -f given. (use -f '' to override)\n");
752
753 4
        if (arg_list_count("I") > 1)
754 4
                ARGV_ERR("\tOnly one -I allowed\n");
755
756 160
        if (d_flag || F_flag)
757 4164
                complain_to_stderr = 1;
758
759 4276
        if (!arg_list_count("T"))
760 4276
                (void)arg_list_add('T', "localhost:0");
761
762
        /*
763
         * Start out by closing all unwanted file descriptors we might
764
         * have inherited from sloppy process control daemons.
765
         */
766 4292
        VSUB_closefrom(STDERR_FILENO + 1);
767 4292
        MCH_TrackHighFd(STDERR_FILENO);
768
769
        /*
770
         * Have Eric Daemonize us if need be
771
         */
772 4292
        if (!C_flag && !d_flag && !F_flag) {
773 96
                eric_fd = mgt_eric();
774 96
                MCH_TrackHighFd(eric_fd);
775 96
                heritage.mgt_pid = getpid();
776 96
        }
777
778 4276
        VRND_SeedAll();
779
780 4276
        vident = mgt_BuildVident();
781
782
        /* Various initializations */
783 4276
        VTAILQ_INIT(&heritage.socks);
784 4276
        VCA_Config();
785 4276
        mgt_evb = VEV_New();
786 4276
        AN(mgt_evb);
787
788
        /* Initialize transport protocols */
789 4276
        XPORT_Init();
790
791 4276
        VJ_Init(j_arg);
792
793
        /* Initialize the bogo-IP VSA */
794 4276
        VSA_Init();
795
796 4276
        if (b_arg != NULL)
797 116
                mgt_b_conv(b_arg);
798
799
        /* Process delayed arguments */
800 57592
        VTAILQ_FOREACH(alp, &arglist, list) {
801 53508
                switch(alp->arg[0]) {
802
                case 'a':
803 4244
                        VCA_Arg(alp->val);
804 4244
                        break;
805
                case 'f':
806 188
                        if (*alp->val != '\0')
807 52
                                alp->priv = mgt_f_read(alp->val);
808 176
                        break;
809
                case 'E':
810 12
                        vext_argument(alp->val);
811 12
                        break;
812
                case 'I':
813 20
                        VJ_master(JAIL_MASTER_FILE);
814 20
                        I_fd = open(alp->val, O_RDONLY);
815 20
                        if (I_fd < 0)
816 4
                                ARGV_ERR("\tCan't open %s: %s\n",
817
                                    alp->val, VAS_errtxt(errno));
818 16
                        VJ_master(JAIL_MASTER_LOW);
819 16
                        break;
820
                case 'p':
821 36564
                        if (alp->priv) {
822 4000
                                MCF_ParamSet(cli, alp->val, alp->priv);
823 4000
                        } else {
824 32564
                                p = strchr(alp->val, '=');
825 32564
                                if (p == NULL)
826 0
                                        ARGV_ERR("\t-p lacks '='\n");
827 32564
                                AN(p);
828 32564
                                *p++ = '\0';
829 32564
                                MCF_ParamSet(cli, alp->val, p);
830
                        }
831 36564
                        break;
832
                case 'r':
833 12
                        MCF_ParamProtect(cli, alp->val);
834 12
                        break;
835
                case 's':
836 428
                        STV_Config(alp->val);
837 428
                        break;
838
                default:
839 12040
                        break;
840
                }
841 53492
                cli_check(cli);
842 53492
        }
843
844 4080
        VCLS_SetLimit(mgt_cls, &mgt_param.cli_limit);
845
846 4080
        assert(d_flag == 0 || F_flag == 0);
847
848 4080
        p = NULL;
849 4080
        if (C_flag && n_arg == NULL) {
850 8
                p = create_bogo_n_arg();
851 8
                n_arg = p;
852 8
        }
853
854 12
        if (S_arg != NULL && !vstrcmp(S_arg, "none")) {
855 0
                fprintf(stderr,
856
                    "Warning: CLI authentication disabled.\n");
857 4
        } else if (S_arg != NULL) {
858 4
                VJ_master(JAIL_MASTER_FILE);
859 4
                o = open(S_arg, O_RDONLY, 0);
860 4
                if (o < 0)
861 4
                        ARGV_ERR("Cannot open -S file (%s): %s\n",
862
                            S_arg, VAS_errtxt(errno));
863 0
                closefd(&o);
864 0
                VJ_master(JAIL_MASTER_LOW);
865 0
        }
866
867 4076
        workdir = VIN_n_Arg(n_arg);
868 4076
        AN(workdir);
869 4076
        if (p != NULL)
870 8
                free(p);
871
872 3976
        if (i_arg == NULL || *i_arg == '\0')
873 100
                i_arg = mgt_HostName();
874 12000
        else for (c = i_arg; *c != '\0'; c++) {
875 8024
                if (!vct_istchar(*c))
876 0
                        ARGV_ERR("Invalid character '%c' for -i\n", *c);
877 8024
        }
878 4076
        heritage.identity = i_arg;
879
880 4076
        mgt_ProcTitle("Mgt");
881
882 4076
        openlog("vinyld", LOG_PID, LOG_LOCAL0);
883
884 4076
        vsb = VSB_new_auto();
885 4076
        AN(vsb);
886 4076
        o = VJ_make_workdir(workdir, vsb);
887 4076
        MGT_ComplainVSB(o ? C_ERR : C_INFO, vsb);
888 4076
        if (o)
889 0
                ARGV_EXIT;
890
891 4060
        if (! C_flag) {
892 57216
                VTAILQ_FOREACH(alp, &arglist, list) {
893 53164
                        if (!vstrcmp(alp->arg, "P"))
894 3984
                                alp->priv = create_pid_file(&pid, "%s", alp->val);
895 53156
                }
896
897
                /* Implicit -P argument */
898 4052
                alp = arg_list_add('P', NULL);
899 4052
                alp->priv = create_pid_file(&pid, "%s/_.pid", workdir);
900 4052
        }
901
902 4060
        VJ_master(JAIL_MASTER_SYSTEM);
903
#ifdef RLIMIT_MEMLOCK
904
        /* try to raise to max (ignore error), then set _cur to whatever we got */
905
        struct rlimit rlim;
906 4060
        rlim.rlim_cur = 0;
907 4060
        rlim.rlim_max = RLIM_INFINITY;
908 4060
        (void)setrlimit(RLIMIT_MEMLOCK, &rlim);
909 4060
        AZ(getrlimit(RLIMIT_MEMLOCK, &rlim));
910 4060
        rlim.rlim_cur = rlim.rlim_max;
911 4060
        AZ(setrlimit(RLIMIT_MEMLOCK, &rlim));
912
#endif
913
914 4060
        AZ(system("rm -rf vmod_cache vext_cache worker_tmpdir"));
915 4060
        VJ_master(JAIL_MASTER_LOW);
916
917 4060
        VSB_clear(vsb);
918 8120
        o = VJ_make_subdir("vmod_cache", "VMOD cache", vsb) ||
919 4060
            VJ_make_subdir("worker_tmpdir",
920 16240
                "TMPDIR for the worker process", vsb) ||
921 4060
            (arg_list_count("E") &&
922 8
                VJ_make_subdir("vext_cache", "VEXT cache", vsb));
923 8120
        MGT_ComplainVSB(o ? C_ERR : C_INFO, vsb);
924 8120
        VSB_destroy(&vsb);
925 8120
        if (o)
926 0
                ARGV_EXIT;
927
928 4060
        o = open("worker_tmpdir", O_RDONLY);
929 4060
        VJ_master(JAIL_MASTER_SYSTEM);
930 4060
        VJ_fix_fd(o, JAIL_FIXFD_WRKTMP);
931 4060
        VJ_master(JAIL_MASTER_LOW);
932 4060
        closefd(&o);
933
934 4060
        if (C_flag)
935 16
                AZ(atexit(mgt_Cflag_atexit));
936
937
        /* If no -s argument specified, process default -s argument */
938 3764
        if (!arg_list_count("s"))
939 3764
                STV_Config(s_arg);
940
941
        /* Configure CLI and special storages, if user did not */
942 4060
        STV_Config_Final();
943
944 4060
        mgt_vcl_init();
945
946 4060
        vext_copyin(vident);
947
948 4060
        u = 0;
949 61264
        VTAILQ_FOREACH(alp, &arglist, list) {
950 57208
                if (!vstrcmp(alp->arg, "f") && alp->priv != NULL)
951 108
                        u |= mgt_process_f_arg(cli, C_flag, &alp->priv);
952 57204
        }
953 4056
        if (C_flag)
954 16
                exit(u);
955
956 4040
        if (VTAILQ_EMPTY(&heritage.socks))
957 0
                VCA_Arg(":80\0");       // XXX: extra NUL for FlexeLint
958
959 4040
        assert(!VTAILQ_EMPTY(&heritage.socks));
960
961 4040
        HSH_config(h_arg);
962
963 4040
        Wait_config(W_arg);
964
965 4040
        mgt_SHM_Init();
966
967 4040
        mgt_SHM_static_alloc(i_arg, vstrlen(i_arg) + 1L, "Arg", "-i");
968 4040
        VSC_C_mgt = VSC_mgt_New(NULL, NULL, "");
969
970 61204
        VTAILQ_FOREACH(alp, &arglist, list) {
971 57164
                if (!vstrcmp(alp->arg, "M"))
972 3976
                        mgt_cli_master(alp->val);
973 53188
                else if (!vstrcmp(alp->arg, "T") && vstrcmp(alp->val, "none"))
974 4040
                        mgt_cli_telnet(alp->val);
975 8016
                else if (!vstrcmp(alp->arg, "P"))
976 8016
                        VPF_Write(alp->priv);
977 57164
        }
978
979 4040
        AZ(VSB_finish(vident));
980
981 4040
        if (S_arg == NULL)
982 4040
                S_arg = make_secret(workdir);
983 4044
        AN(S_arg);
984
985 4036
        MGT_Complain(C_DEBUG, "Version: %s", VCS_String("V"));
986 4036
        MGT_Complain(C_DEBUG, "Platform: %s", VSB_data(vident) + 1);
987
988 4036
        if (d_flag)
989 3988
                mgt_cli_setup(0, 1, 1, "debug", mgt_stdin_close, NULL);
990
991 4036
        if (vstrcmp(S_arg, "none"))
992 4036
                mgt_cli_secret(S_arg);
993
994 4036
        memset(&sac, 0, sizeof sac);
995 4036
        sac.sa_handler = SIG_IGN;
996 4036
        sac.sa_flags = SA_RESTART;
997
998 4036
        AZ(sigaction(SIGPIPE, &sac, NULL));
999 4036
        AZ(sigaction(SIGHUP, &sac, NULL));
1000
1001 4036
        MCH_Init();
1002
1003 4036
        if (I_fd >= 0) {
1004 16
                fprintf(stderr, "BEGIN of -I file processing\n");
1005
                /* We must dup stderr, because VCLS closes the output fd */
1006 16
                mgt_cli_setup(I_fd, dup(2), 1, "-I file", mgt_I_close, stderr);
1007 32
                while (I_fd >= 0) {
1008 28
                        o = VEV_Once(mgt_evb);
1009 28
                        if (o != 1)
1010 0
                                MGT_Complain(C_ERR,
1011 0
                                    "VEV_Once() = %d", o);
1012
                }
1013 4
        }
1014 4024
        assert(I_fd == -1);
1015
1016 4024
        err = mgt_has_vcl();
1017 4024
        if (!d_flag && err != NULL && !arg_list_count("f"))
1018 0
                MGT_Complain(C_ERR, "%s", err);
1019
1020 84
        if (err == NULL && !d_flag)
1021 36
                u = MCH_Start_Child();
1022
        else
1023 3988
                u = 0;
1024
1025 4024
        if (eric_fd >= 0)
1026 32
                mgt_eric_im_done(eric_fd, u);
1027
1028 4024
        if (u)
1029 32
                exit(u);
1030
1031
        /* Failure is no longer an option */
1032
1033 3992
        if (F_flag)
1034 12
                VFIL_null_fd(STDIN_FILENO);
1035
1036 3992
        e = VEV_Alloc();
1037 3992
        AN(e);
1038 3992
        e->callback = mgt_uptime;
1039 3992
        e->timeout = 1.0;
1040 3992
        e->name = "mgt_uptime";
1041 3992
        AZ(VEV_Start(mgt_evb, e));
1042
1043 3992
        e = VEV_Alloc();
1044 3992
        AN(e);
1045 3992
        e->sig = SIGTERM;
1046 3992
        e->callback = mgt_sigint;
1047 3992
        e->name = "SIGTERM";
1048 3992
        AZ(VEV_Start(mgt_evb, e));
1049
1050 3992
        e = VEV_Alloc();
1051 3992
        AN(e);
1052 3992
        e->sig = SIGINT;
1053 3992
        e->callback = mgt_sigint;
1054 3992
        e->name = "SIGINT";
1055 3992
        AZ(VEV_Start(mgt_evb, e));
1056
1057 3992
        o = VEV_Loop(mgt_evb);
1058 3992
        if (o != 0 && o != -42)
1059 0
                MGT_Complain(C_ERR, "VEV_Loop() = %d", o);
1060
1061 3992
        MGT_Complain(C_INFO, "manager stopping child");
1062 3992
        MCH_Stop_Child();
1063 3992
        MGT_Complain(C_INFO, "manager dies");
1064 3992
        mgt_cli_close_all();
1065 3992
        VEV_Destroy(&mgt_evb);
1066 3992
        VJ_master(JAIL_MASTER_SYSTEM);
1067
        /*lint -e(730)*/
1068 3992
        vext_cleanup(! MGT_DO_DEBUG(DBG_VMOD_SO_KEEP));
1069 3992
        (void)rmdir("vext_cache");
1070 3992
        VJ_master(JAIL_MASTER_LOW);
1071 60908
        VTAILQ_FOREACH(alp, &arglist, list) {
1072 56916
                if (!vstrcmp(alp->arg, "P"))
1073 7968
                        VPF_Remove(alp->priv);
1074 56916
        }
1075 3992
        exit(exit_status);
1076
}