Code Coverage: vinyl-cache/bin/vinyld/mgt/mgt_main.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
mgt_DumpOptions(void)
86 0
{
87 0
        printf(".. _opt_n:\n\n");
88 0
        printf("-n workdir\n\n");
89 0
        printf("  Runtime directory for the shared memory, "
90 0
               "compiled VCLs etc.\n\n");
91 0
        printf("  In performance critical applications, this directory "
92 0
               "should be on a RAM backed filesystem.\n\n");
93 0
        printf("  When running multiple vinyld instances, separate "
94 0
               "directories need to be used.\n\n");
95 0
        VIN_DumpDefaults();
96 0
}
97
98
static void
99
usage(void)
100 2
{
101 2
        char *p;
102
103 106
#define FMT_NONL "  %-35s # %s"
104 104
#define FMT FMT_NONL "\n"
105
106 2
        printf( "Usage: vinyld [options]\n");
107
108 2
        printf("\nBasic options:\n");
109
110 2
        printf(FMT, "[-a [name=][listen_address",
111 2
            "HTTP listen address, protocol, options.");
112 2
        printf(FMT, "     [,PROTO|,option=value,...]]",
113 2
            "Can be specified multiple times.");
114 2
        printf(FMT, "",
115 2
            "  default: \":80,HTTP\"");
116 2
        printf(FMT, "  options:",
117 2
            "Proto can be \"PROXY\" or \"HTTP\" (default)");
118 2
        printf(FMT, "    [,user=<u>][,group=<g>]",
119 2
            "user, group and mode set permissions for");
120 2
        printf(FMT, "    [,mode=<m>]",
121 2
            "  a Unix domain socket.");
122 2
        printf(FMT, "-b none", "No backend");
123 2
        printf(FMT, "-b [addr[:port]|path]", "Backend address and port");
124 2
        printf(FMT, "", "  or socket file path");
125 2
        printf(FMT, "", "  default: \":80\"");
126 2
        printf(FMT, "-f vclfile", "VCL program");
127 2
        printf(FMT, "", "Can be specified multiple times.");
128 2
        printf(FMT, "-n dir", "Working directory");
129
130 2
        p = VIN_n_Arg(NULL);
131 2
        AN(p);
132 2
        printf(FMT_NONL "  default: %s\n", "", "", p);
133 2
        free(p);
134
135 2
        printf("\n-b can be used only once, and not together with -f\n");
136
137 2
        printf("\nDocumentation options:\n");
138 2
        printf(FMT, "-?", "Prints this usage message");
139 2
        printf(FMT, "-x parameter", "Parameter documentation in RST format");
140 2
        printf(FMT, "-x parameter-json", "Parameter documentation in JSON format");
141 2
        printf(FMT, "-x vsl", "VSL record documentation");
142 2
        printf(FMT, "-x cli", "CLI command documentation");
143 2
        printf(FMT, "-x builtin", "Builtin VCL program");
144 2
        printf(FMT, "-x optstring", "List of getopt options");
145 2
        printf(FMT, "-x options", "Dynamic options documentation");
146
147 2
        printf("\nOperations options:\n");
148
149 2
        printf(FMT, "-F", "Run in foreground");
150 2
        printf(FMT, "-T address[:port]", "CLI address");
151 2
        printf(FMT, "", "Can be specified multiple times.");
152 2
        printf(FMT, "-M address:port", "Reverse CLI destination");
153 2
        printf(FMT, "", "Can be specified multiple times.");
154 2
        printf(FMT, "-P file", "PID file");
155 2
        printf(FMT, "-i identity", "Identity of Vinyl Cache instance");
156 2
        printf(FMT, "-I clifile", "Initialization CLI commands");
157 2
        printf(FMT, "-E extension", "Load extension");
158
159 2
        printf("\nTuning options:\n");
160
161 2
        printf(FMT, "-t TTL", "Default TTL");
162 2
        printf(FMT, "-p param=value", "set parameter");
163 2
        printf(FMT, "", "Can be specified multiple times.");
164
165 2
        printf(FMT, "-s [name=]kind[,options]", "Storage specification");
166 2
        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 2
        printf(FMT, "", "  -s default (=malloc)");
172 2
#endif
173 2
        printf(FMT, "", "  -s malloc");
174 2
        printf(FMT, "", "  -s file");
175
176 2
        printf(FMT, "-l vsl", "Size of shared memory log");
177 2
        printf(FMT, "", "  vsl: space for VSL records [80m]");
178
179 2
        printf("\nSecurity options:\n");
180
181 2
        printf(FMT, "-r param[,param...]", "Set parameters read-only from CLI");
182 2
        printf(FMT, "", "Can be specified multiple times.");
183 2
        printf(FMT, "-S secret-file", "Secret file for CLI authentication");
184 2
        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 2
        printf(FMT, "", "  -j unix");
192 2
        printf(FMT, "", "  -j none");
193
194 2
        printf("\nAdvanced/Dev/Debug options:\n");
195
196 2
        printf(FMT, "-d", "debug mode");
197 2
        printf(FMT, "", "Stay in foreground, CLI on stdin.");
198 2
        printf(FMT, "-C", "Output VCL code compiled to C language");
199 2
        printf(FMT, "-V", "version");
200 2
        printf(FMT, "-h kind[,options]", "Hash specification");
201 2
        printf(FMT, "-W waiter", "Waiter implementation");
202 2
}
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
arg_list_add(const char arg, const char *val)
219 14891
{
220 14891
        struct arg_list *alp;
221
222 14891
        ALLOC_OBJ(alp, ARG_LIST_MAGIC);
223 14891
        AN(alp);
224 14891
        alp->arg[0] = arg;
225 14891
        alp->arg[1] = '\0';
226 14891
        REPLACE(alp->val, val);
227 14891
        VTAILQ_INSERT_TAIL(&arglist, alp, list);
228 14891
        return (alp);
229 14891
}
230
231
static unsigned
232
arg_list_count(const char *arg)
233 4411
{
234 4411
        unsigned retval = 0;
235 4411
        struct arg_list *alp;
236
237 55570
        VTAILQ_FOREACH(alp, &arglist, list) {
238 55570
                if (!vstrcmp(alp->arg, arg))
239 164
                        retval++;
240 55570
        }
241 4411
        return (retval);
242 4411
}
243
244
/*--------------------------------------------------------------------*/
245
246
static void
247
cli_check(const struct cli *cli)
248 14882
{
249 14882
        if (cli->result == CLIS_OK || cli->result == CLIS_TRUNCATED) {
250 14880
                AZ(VSB_finish(cli->sb));
251 14880
                if (VSB_len(cli->sb) > 0)
252 1
                        fprintf(stderr, "Warnings:\n%s\n", VSB_data(cli->sb));
253 14880
                VSB_clear(cli->sb);
254 14880
                return;
255 14880
        }
256 2
        AZ(VSB_finish(cli->sb));
257 2
        fprintf(stderr, "Error:\n%s\n", VSB_data(cli->sb));
258 2
        exit(2);
259 2
}
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
mgt_stdin_close(void *priv)
267 1026
{
268
269 1026
        (void)priv;
270 1026
        return (-42);
271 1026
}
272
273
/*--------------------------------------------------------------------
274
 * Autogenerate a -S file using strong random bits from the kernel.
275
 */
276
277
static void
278
mgt_secret_atexit(void)
279 4974
{
280
281
        /* Only master process */
282 4974
        if (getpid() != heritage.mgt_pid)
283 3937
                return;
284 1037
        VJ_master(JAIL_MASTER_FILE);
285 1037
        (void)unlink("_.secret");
286 1037
        VJ_master(JAIL_MASTER_LOW);
287 1037
}
288
289
static const char *
290
make_secret(const char *dirname)
291 1038
{
292 1038
        char *fn;
293 1038
        int fdo;
294 1038
        int i;
295 1038
        unsigned char b;
296
297 1038
        assert(asprintf(&fn, "%s/_.secret", dirname) > 0);
298
299 1038
        VJ_master(JAIL_MASTER_FILE);
300 1038
        if (unlink(fn) < 0 && errno != ENOENT) {
301 1
                ARGV_ERR("Cannot remove preexisting secret-file in %s (%s)\n",
302 1
                    dirname, VAS_errtxt(errno));
303 1
        }
304
305 1037
        fdo = open(fn, O_RDWR|O_CREAT|O_EXCL, 0640);
306 1037
        if (fdo < 0) {
307 0
                ARGV_ERR("Cannot create secret-file in %s (%s)\n",
308 0
                    dirname, VAS_errtxt(errno));
309 0
        }
310
311 266509
        for (i = 0; i < 256; i++) {
312 265472
                AZ(VRND_RandomCrypto(&b, 1));
313 265472
                assert(1 == write(fdo, &b, 1));
314 265472
        }
315 1037
        closefd(&fdo);
316 1037
        VJ_master(JAIL_MASTER_LOW);
317 1037
        AZ(atexit(mgt_secret_atexit));
318 1037
        return (fn);
319 1037
}
320
321
static void
322
mgt_Cflag_atexit(void)
323 16
{
324
325
        /* Only master process */
326 16
        if (getpid() != heritage.mgt_pid)
327 10
                return;
328 6
        if (arg_list_count("E")) {
329 0
                vext_cleanup(1);
330 0
                VJ_rmdir("vext_cache");
331 0
        }
332 6
        VJ_rmdir("vmod_cache");
333 6
        VJ_rmdir("worker_tmpdir");
334 6
        (void)chdir("/");
335 6
        VJ_rmdir(workdir);
336 6
}
337
338
/*--------------------------------------------------------------------*/
339
340
static void
341
mgt_tests(void)
342 1121
{
343 1121
        assert(VTIM_parse("Sun, 06 Nov 1994 08:49:37 GMT") == 784111777);
344 1121
        assert(VTIM_parse("Sunday, 06-Nov-94 08:49:37 GMT") == 784111777);
345 1121
        assert(VTIM_parse("Sun Nov  6 08:49:37 1994") == 784111777);
346
347
        /* Check that our VSHA256 works */
348 1121
        VSHA256_Test();
349 1121
}
350
351
static void
352
mgt_initialize(struct cli *cli)
353 1121
{
354 1121
        static unsigned clilim = 32768;
355
356
        /* for ASSERT_MGT() */
357 1121
        heritage.mgt_pid = getpid();
358
359
        /* Create a cli for convenience in otherwise CLI functions */
360 1121
        INIT_OBJ(cli, CLI_MAGIC);
361 1121
        cli[0].sb = VSB_new_auto();
362 1121
        AN(cli[0].sb);
363 1121
        cli[0].result = CLIS_OK;
364 1121
        cli[0].limit = &clilim;
365
366 1121
        mgt_cli_init_cls();             // CLI commands can be registered
367
368 1121
        MCF_InitParams(cli);
369
370 1121
        VCC_VCL_Range(&heritage.min_vcl_version, &heritage.max_vcl_version);
371
372 1121
        cli_check(cli);
373 1121
}
374
375
static void
376
mgt_x_arg(const char *x_arg)
377 7
{
378 7
        if (!vstrcmp(x_arg, "parameter"))
379 1
                MCF_DumpRstParam();
380 6
        else if (!vstrcmp(x_arg, "parameter-json"))
381 1
                MCF_DumpJsonParam();
382 5
        else if (!vstrcmp(x_arg, "vsl"))
383 1
                mgt_DumpRstVsl();
384 4
        else if (!vstrcmp(x_arg, "cli"))
385 1
                mgt_DumpRstCli();
386 3
        else if (!vstrcmp(x_arg, "builtin"))
387 1
                mgt_DumpBuiltin();
388 2
        else if (!vstrcmp(x_arg, "optstring"))
389 1
                (void)printf("%s\n", opt_spec);
390 1
        else if (!vstrcmp(x_arg, "options"))
391 0
                mgt_DumpOptions();
392 1
        else
393 1
                ARGV_ERR("Invalid -x argument\n");
394 7
}
395
396
/*--------------------------------------------------------------------*/
397
398 5
#define ERIC_MAGIC 0x2246988a           /* Eric is not random */
399
400
static int
401
mgt_eric(void)
402 21
{
403 21
        int eric_pipes[2];
404 21
        unsigned u;
405 21
        ssize_t sz;
406
407 21
        AZ(pipe(eric_pipes));
408
409 21
        switch (fork()) {
410 0
        case -1:
411 0
                fprintf(stderr, "Fork() failed: %s\n", VAS_errtxt(errno));
412 0
                exit(-1);
413 21
        case 0:
414 21
                closefd(&eric_pipes[0]);
415 21
                assert(setsid() > 1);
416
417 21
                VFIL_null_fd(STDIN_FILENO);
418 21
                return (eric_pipes[1]);
419 21
        default:
420 21
                break;
421 21
        }
422 21
        closefd(&eric_pipes[1]);
423 21
        sz = read(eric_pipes[0], &u, sizeof u);
424 21
        if (sz == sizeof u && u == ERIC_MAGIC)
425 0
                exit(0);
426 21
        else if (sz == sizeof u && u != 0)
427 5
                exit(u);
428 16
        else
429 16
                exit(-1);
430 21
}
431
432
static void
433
mgt_eric_im_done(int eric_fd, unsigned u)
434 5
{
435
436 5
        if (u == 0)
437 0
                u = ERIC_MAGIC;
438
439 5
        VFIL_null_fd(STDIN_FILENO);
440 5
        VFIL_null_fd(STDOUT_FILENO);
441 5
        VFIL_null_fd(STDERR_FILENO);
442
443 5
        assert(write(eric_fd, &u, sizeof u) == sizeof u);
444 5
        closefd(&eric_fd);
445 5
}
446
447
/*--------------------------------------------------------------------*/
448
449
static int v_matchproto_(vev_cb_f)
450
mgt_sigint(const struct vev *e, int what)
451 4
{
452
453 4
        (void)what;
454 4
        MGT_Complain(C_INFO, "Manager got %s from PID %jd",
455 4
            e->name, (intmax_t)e->siginfo->si_pid);
456 4
        (void)fflush(stdout);
457 4
        if (MCH_Running())
458 3
                MCH_Stop_Child();
459 4
        return (-42);
460 4
}
461
462
/*--------------------------------------------------------------------*/
463
464
static int v_matchproto_(vev_cb_f)
465
mgt_uptime(const struct vev *e, int what)
466 2082
{
467 2082
        static double mgt_uptime_t0 = 0;
468
469 2082
        (void)e;
470 2082
        (void)what;
471 2082
        AN(VSC_C_mgt);
472 2082
        if (mgt_uptime_t0 == 0)
473 994
                mgt_uptime_t0 = VTIM_real();
474 2082
        VSC_C_mgt->uptime = (uint64_t)(VTIM_real() - mgt_uptime_t0);
475 2082
        return (0);
476 2082
}
477
478
/*--------------------------------------------------------------------*/
479
480
static int v_matchproto_(mgt_cli_close_f)
481
mgt_I_close(void *priv)
482 1
{
483 1
        (void)priv;
484 1
        fprintf(stderr, "END of -I file processing\n");
485 1
        I_fd = -1;
486 1
        return (0);
487 1
}
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
mgt_f_read(const char *fn)
500 15
{
501 15
        struct f_arg *fa;
502 15
        char *f, *fnp;
503
504 15
        ALLOC_OBJ(fa, F_ARG_MAGIC);
505 15
        AN(fa);
506 15
        REPLACE(fa->farg, fn);
507 15
        VFIL_setpath(&vcl_path, mgt_vcl_path);
508 15
        if (VFIL_searchpath(vcl_path, NULL, &f, fn, &fnp) || f == NULL) {
509 3
                ARGV_ERR("Cannot read -f file '%s' (%s)\n",
510 3
                    fnp != NULL ? fnp : fn, VAS_errtxt(errno));
511 3
        }
512 12
        free(fa->farg);
513 12
        fa->farg = fnp;
514 12
        fa->src = f;
515 12
        return (fa);
516 15
}
517
518
static void
519
mgt_b_conv(const char *b_arg)
520 27
{
521 27
        struct f_arg *fa;
522 27
        struct vsb *vsb;
523
524 27
        ALLOC_OBJ(fa, F_ARG_MAGIC);
525 27
        AN(fa);
526 27
        REPLACE(fa->farg, "<-b argument>");
527 27
        vsb = VSB_new_auto();
528 27
        AN(vsb);
529 27
        VSB_cat(vsb, "vcl 4.1;\n");
530 27
        VSB_cat(vsb, "backend default ");
531 27
        if (!strcasecmp(b_arg, "none"))
532 14
                VSB_cat(vsb, "none;\n");
533 13
        else if (VUS_is(b_arg))
534 1
                VSB_printf(vsb, "{\n    .path = \"%s\";\n}\n", b_arg);
535 12
        else
536 12
                VSB_printf(vsb, "{\n    .host = \"%s\";\n}\n", b_arg);
537 27
        AZ(VSB_finish(vsb));
538 27
        fa->src = strdup(VSB_data(vsb));
539 27
        AN(fa->src);
540 27
        VSB_destroy(&vsb);
541 27
        AZ(arg_list_count("f"));
542 27
        arg_list_add('f', "")->priv = fa;
543 27
}
544
545
static int
546
mgt_process_f_arg(struct cli *cli, unsigned C_flag, void **fap)
547 27
{
548 27
        int retval = 0;
549 27
        struct f_arg *fa;
550 27
        const char *name = NULL;
551
552 27
        TAKE_OBJ_NOTNULL(fa, fap, F_ARG_MAGIC);
553 27
        if (arg_list_count("f") == 1)
554 25
                name = "boot";
555 27
        mgt_vcl_startup(cli, fa->src, name, fa->farg, C_flag);
556 27
        if (C_flag) {
557 6
                if (cli->result != CLIS_OK &&
558 6
                    cli->result != CLIS_TRUNCATED)
559 2
                        retval = 2;
560 6
                AZ(VSB_finish(cli->sb));
561 6
                fprintf(stderr, "%s\n", VSB_data(cli->sb));
562 6
                VSB_clear(cli->sb);
563 21
        } else {
564 21
                cli_check(cli);
565 21
        }
566 27
        free(fa->farg);
567 27
        free(fa->src);
568 27
        FREE_OBJ(fa);
569 27
        return (retval);
570 27
}
571
572
static char *
573
create_bogo_n_arg(void)
574 4
{
575 4
        struct vsb *vsb;
576 4
        char *p;
577
578 4
        vsb = VSB_new_auto();
579 4
        AN(vsb);
580 4
        if (getenv("TMPDIR") != NULL)
581 0
                VSB_printf(vsb, "%s", getenv("TMPDIR"));
582 4
        else
583 4
                VSB_cat(vsb, "/tmp");
584 4
        VSB_cat(vsb, "/vinyld_C_XXXXXXX");
585 4
        AZ(VSB_finish(vsb));
586 4
        p = strdup(VSB_data(vsb));
587 4
        AN(p);
588 4
        VSB_destroy(&vsb);
589 4
        AN(mkdtemp(p));
590 4
        AZ(chmod(p, 0750));
591 4
        return (p);
592 4
}
593
594
static struct vpf_fh *
595
create_pid_file(pid_t *ppid, const char *fmt, ...)
596 2068
{
597 2068
        struct vsb *vsb;
598 2068
        va_list ap;
599 2068
        struct vpf_fh *pfh;
600
601 2068
        va_start(ap, fmt);
602 2068
        vsb = VSB_new_auto();
603 2068
        AN(vsb);
604 2068
        VSB_vprintf(vsb, fmt, ap);
605 2068
        AZ(VSB_finish(vsb));
606 2068
        VJ_master(JAIL_MASTER_FILE);
607 2068
        pfh = VPF_Open(VSB_data(vsb), 0644, ppid);
608 2068
        if (pfh == NULL && errno == EEXIST)
609 3
                ARGV_ERR(
610 2068
                    "vinyld is already running (pid=%jd) (pidfile=%s)\n",
611 2068
                    (intmax_t)*ppid, VSB_data(vsb));
612 2065
        if (pfh == NULL)
613 1
                ARGV_ERR("Could not open pid-file (%s): %s\n",
614 2065
                    VSB_data(vsb), VAS_errtxt(errno));
615 2064
        VJ_master(JAIL_MASTER_LOW);
616 2064
        VSB_destroy(&vsb);
617 2064
        va_end(ap);
618 2064
        return (pfh);
619 2065
}
620
621
int
622
main(int argc, char * const *argv)
623 1122
{
624 1122
        int o, eric_fd = -1;
625 1122
        unsigned C_flag = 0;
626 1122
        unsigned F_flag = 0;
627 1122
        const char *b_arg = NULL;
628 1122
        const char *i_arg = NULL;
629 1122
        const char *j_arg = NULL;
630 1122
        const char *h_arg = "critbit";
631 1122
        const char *n_arg = getenv("VINYL_DEFAULT_N");
632 1122
        const char *S_arg = NULL;
633 1122
        const char *s_arg = "default,100m";
634 1122
        const char *W_arg = NULL;
635 1122
        const char *c;
636 1122
        char *p;
637 1122
        struct cli cli[1];
638 1122
        const char *err;
639 1122
        unsigned u;
640 1122
        struct sigaction sac;
641 1122
        struct vev *e;
642 1122
        pid_t pid;
643 1122
        struct arg_list *alp;
644 1122
        int first_arg = 1;
645 1122
        struct vsb *vsb;
646
647 1122
        if (argc == 2 && !vstrcmp(argv[1], "--optstring")) {
648 1
                printf("%s\n", opt_spec);
649 1
                exit(0);
650 1
        }
651
652 1121
        heritage.argc = argc;
653 1121
        heritage.argv = argv;
654
655 1121
        setbuf(stdout, NULL);
656 1121
        setbuf(stderr, NULL);
657
658 1121
        mgt_tests();
659
660 1121
        mgt_initialize(cli);
661
662 17064
        for (; (o = getopt(argc, argv, opt_spec)) != -1; first_arg = 0) {
663 15957
                switch (o) {
664 3
                case 'V':
665 3
                        if (!first_arg)
666 1
                                ARGV_ERR("-V must be the first argument\n");
667 2
                        if (argc != 2)
668 1
                                ARGV_ERR("Too many arguments for -V\n");
669 1
                        VCS_Message("vinyld");
670 1
                        exit(0);
671 9
                case 'x':
672 9
                        if (!first_arg)
673 1
                                ARGV_ERR("-x must be the first argument\n");
674 8
                        if (argc != 3)
675 1
                                ARGV_ERR("Too many arguments for -x\n");
676 7
                        mgt_x_arg(optarg);
677 7
                        exit(0);
678 30
                case 'b':
679 30
                        b_arg = optarg;
680 30
                        break;
681 9
                case 'C':
682 9
                        C_flag = 1;
683 9
                        break;
684 1064
                case 'd':
685 1064
                        d_flag++;
686 1064
                        break;
687 14
                case 'F':
688 14
                        F_flag = 1;
689 14
                        break;
690 11
                case 'j':
691 11
                        j_arg = optarg;
692 11
                        break;
693 4
                case 'h':
694 4
                        h_arg = optarg;
695 4
                        break;
696 1028
                case 'i':
697 1028
                        i_arg = optarg;
698 1028
                        break;
699 1030
                case 'l':
700 1030
                        arg_list_add('p', "vsl_space")->priv = optarg;
701 1030
                        break;
702 1052
                case 'n':
703 1052
                        n_arg = optarg;
704 1052
                        break;
705 1
                case 'S':
706 1
                        S_arg = optarg;
707 1
                        break;
708 1
                case 't':
709 1
                        arg_list_add('p', "default_ttl")->priv = optarg;
710 1
                        break;
711 6
                case 'W':
712 6
                        W_arg = optarg;
713 6
                        break;
714 1095
                case 'a':
715 1098
                case 'E':
716 1132
                case 'f':
717 1139
                case 'I':
718 9539
                case 'p':
719 10566
                case 'P':
720 11591
                case 'M':
721 11594
                case 'r':
722 11693
                case 's':
723 11693
                case 'T':
724 11693
                        (void)arg_list_add(o, optarg);
725 11693
                        break;
726 2
                default:
727 2
                        usage();
728 2
                        exit(2);
729 15957
                }
730 15957
        }
731
732 1107
        if (argc != optind)
733 1
                ARGV_ERR("Too many arguments (%s...)\n", argv[optind]);
734
735 1106
        if (b_arg != NULL && arg_list_count("f"))
736 1
                ARGV_ERR("Only one of -b or -f can be specified\n");
737
738 1105
        if (d_flag && F_flag)
739 1
                ARGV_ERR("Only one of -d or -F can be specified\n");
740
741 1104
        if (C_flag && b_arg == NULL && !arg_list_count("f"))
742 1
                ARGV_ERR("-C needs either -b <backend> or -f <vcl_file>\n");
743
744 1103
        if (d_flag && C_flag)
745 1
                ARGV_ERR("-d makes no sense with -C\n");
746
747 1102
        if (F_flag && C_flag)
748 1
                ARGV_ERR("-F makes no sense with -C\n");
749
750 1101
        if (!d_flag && b_arg == NULL && !arg_list_count("f"))
751 1
                ARGV_ERR("Neither -b nor -f given. (use -f '' to override)\n");
752
753 1100
        if (arg_list_count("I") > 1)
754 1
                ARGV_ERR("\tOnly one -I allowed\n");
755
756 1099
        if (d_flag || F_flag)
757 1072
                complain_to_stderr = 1;
758
759 1099
        if (!arg_list_count("T"))
760 1099
                (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 1099
        VSUB_closefrom(STDERR_FILENO + 1);
767 1099
        MCH_TrackHighFd(STDERR_FILENO);
768
769
        /*
770
         * Have Eric Daemonize us if need be
771
         */
772 1099
        if (!C_flag && !d_flag && !F_flag) {
773 21
                eric_fd = mgt_eric();
774 21
                MCH_TrackHighFd(eric_fd);
775 21
                heritage.mgt_pid = getpid();
776 21
        }
777
778 1099
        VRND_SeedAll();
779
780 1099
        vident = mgt_BuildVident();
781
782
        /* Various initializations */
783 1099
        VTAILQ_INIT(&heritage.socks);
784 1099
        VCA_Config();
785 1099
        mgt_evb = VEV_New();
786 1099
        AN(mgt_evb);
787
788
        /* Initialize transport protocols */
789 1099
        XPORT_Init();
790
791 1099
        VJ_Init(j_arg);
792
793
        /* Initialize the bogo-IP VSA */
794 1099
        VSA_Init();
795
796 1099
        if (b_arg != NULL)
797 27
                mgt_b_conv(b_arg);
798
799
        /* Process delayed arguments */
800 13782
        VTAILQ_FOREACH(alp, &arglist, list) {
801 13782
                switch(alp->arg[0]) {
802 1092
                case 'a':
803 1092
                        VCA_Arg(alp->val);
804 1092
                        break;
805 47
                case 'f':
806 47
                        if (*alp->val != '\0')
807 15
                                alp->priv = mgt_f_read(alp->val);
808 47
                        break;
809 3
                case 'E':
810 3
                        vext_argument(alp->val);
811 3
                        break;
812 5
                case 'I':
813 5
                        VJ_master(JAIL_MASTER_FILE);
814 5
                        I_fd = open(alp->val, O_RDONLY);
815 5
                        if (I_fd < 0)
816 1
                                ARGV_ERR("\tCan't open %s: %s\n",
817 5
                                    alp->val, VAS_errtxt(errno));
818 4
                        VJ_master(JAIL_MASTER_LOW);
819 4
                        break;
820 9431
                case 'p':
821 9431
                        if (alp->priv) {
822 1031
                                MCF_ParamSet(cli, alp->val, alp->priv);
823 8400
                        } else {
824 8400
                                p = strchr(alp->val, '=');
825 8400
                                if (p == NULL)
826 0
                                        ARGV_ERR("\t-p lacks '='\n");
827 8400
                                AN(p);
828 8400
                                *p++ = '\0';
829 8400
                                MCF_ParamSet(cli, alp->val, p);
830 8400
                        }
831 9431
                        break;
832 9431
                case 'r':
833 3
                        MCF_ParamProtect(cli, alp->val);
834 3
                        break;
835 99
                case 's':
836 99
                        STV_Config(alp->val);
837 99
                        break;
838 3102
                default:
839 3102
                        break;
840 13782
                }
841 13740
                cli_check(cli);
842 13740
        }
843
844 1057
        VCLS_SetLimit(mgt_cls, &mgt_param.cli_limit);
845
846 1057
        assert(d_flag == 0 || F_flag == 0);
847
848 1057
        p = NULL;
849 1057
        if (C_flag && n_arg == NULL) {
850 4
                p = create_bogo_n_arg();
851 4
                n_arg = p;
852 4
        }
853
854 1057
        if (S_arg != NULL && !vstrcmp(S_arg, "none")) {
855 0
                fprintf(stderr,
856 0
                    "Warning: CLI authentication disabled.\n");
857 1057
        } else if (S_arg != NULL) {
858 1
                VJ_master(JAIL_MASTER_FILE);
859 1
                o = open(S_arg, O_RDONLY, 0);
860 1
                if (o < 0)
861 1
                        ARGV_ERR("Cannot open -S file (%s): %s\n",
862 1
                            S_arg, VAS_errtxt(errno));
863 0
                closefd(&o);
864 0
                VJ_master(JAIL_MASTER_LOW);
865 0
        }
866
867 1056
        workdir = VIN_n_Arg(n_arg);
868 1056
        AN(workdir);
869 1056
        if (p != NULL)
870 4
                free(p);
871
872 1056
        if (i_arg == NULL || *i_arg == '\0')
873 24
                i_arg = mgt_HostName();
874 3100
        else for (c = i_arg; *c != '\0'; c++) {
875 2068
                if (!vct_istchar(*c))
876 0
                        ARGV_ERR("Invalid character '%c' for -i\n", *c);
877 2068
        }
878 1056
        heritage.identity = i_arg;
879
880 1056
        mgt_ProcTitle("Mgt");
881
882 1056
        openlog("vinyld", LOG_PID, LOG_LOCAL0);
883
884 1056
        vsb = VSB_new_auto();
885 1056
        AN(vsb);
886 1056
        o = VJ_make_workdir(workdir, vsb);
887 1056
        MGT_ComplainVSB(o ? C_ERR : C_INFO, vsb);
888 1056
        if (o)
889 0
                ARGV_EXIT;
890
891 1056
        if (! C_flag) {
892 13692
                VTAILQ_FOREACH(alp, &arglist, list) {
893 13692
                        if (!vstrcmp(alp->arg, "P"))
894 1027
                                alp->priv = create_pid_file(&pid, "%s", alp->val);
895 13692
                }
896
897
                /* Implicit -P argument */
898 1043
                alp = arg_list_add('P', NULL);
899 1043
                alp->priv = create_pid_file(&pid, "%s/_.pid", workdir);
900 1043
        }
901
902 1056
        VJ_master(JAIL_MASTER_SYSTEM);
903 1056
#ifdef RLIMIT_MEMLOCK
904
        /* try to raise to max (ignore error), then set _cur to whatever we got */
905 1056
        struct rlimit rlim;
906 1056
        rlim.rlim_cur = 0;
907 1056
        rlim.rlim_max = RLIM_INFINITY;
908 1056
        (void)setrlimit(RLIMIT_MEMLOCK, &rlim);
909 1056
        AZ(getrlimit(RLIMIT_MEMLOCK, &rlim));
910 1056
        rlim.rlim_cur = rlim.rlim_max;
911 1056
        AZ(setrlimit(RLIMIT_MEMLOCK, &rlim));
912 1056
#endif
913
914 1056
        AZ(system("rm -rf vmod_cache vext_cache worker_tmpdir"));
915 1056
        VJ_master(JAIL_MASTER_LOW);
916
917 1056
        VSB_clear(vsb);
918 1056
        o = VJ_make_subdir("vmod_cache", "VMOD cache", vsb) ||
919 1056
            VJ_make_subdir("worker_tmpdir",
920 1045
                "TMPDIR for the worker process", vsb) ||
921 1056
            (arg_list_count("E") &&
922 1045
                VJ_make_subdir("vext_cache", "VEXT cache", vsb));
923 1056
        MGT_ComplainVSB(o ? C_ERR : C_INFO, vsb);
924 1056
        VSB_destroy(&vsb);
925 1056
        if (o)
926 0
                ARGV_EXIT;
927
928 1056
        o = open("worker_tmpdir", O_RDONLY);
929 1056
        VJ_master(JAIL_MASTER_SYSTEM);
930 1056
        VJ_fix_fd(o, JAIL_FIXFD_WRKTMP);
931 1056
        VJ_master(JAIL_MASTER_LOW);
932 1056
        closefd(&o);
933
934 1056
        if (C_flag)
935 6
                AZ(atexit(mgt_Cflag_atexit));
936
937
        /* If no -s argument specified, process default -s argument */
938 1056
        if (!arg_list_count("s"))
939 978
                STV_Config(s_arg);
940
941
        /* Configure CLI and special storages, if user did not */
942 1056
        STV_Config_Final();
943
944 1056
        mgt_vcl_init();
945
946 1056
        vext_copyin(vident);
947
948 1056
        u = 0;
949 14735
        VTAILQ_FOREACH(alp, &arglist, list) {
950 14735
                if (!vstrcmp(alp->arg, "f") && alp->priv != NULL)
951 27
                        u |= mgt_process_f_arg(cli, C_flag, &alp->priv);
952 14735
        }
953 1056
        if (C_flag)
954 6
                exit(u);
955
956 1050
        if (VTAILQ_EMPTY(&heritage.socks))
957 0
                VCA_Arg(":80\0");       // XXX: extra NUL for FlexeLint
958
959 1050
        assert(!VTAILQ_EMPTY(&heritage.socks));
960
961 1050
        HSH_config(h_arg);
962
963 1050
        Wait_config(W_arg);
964
965 1050
        mgt_SHM_Init();
966
967 1050
        mgt_SHM_static_alloc(i_arg, vstrlen(i_arg) + 1L, "Arg", "-i");
968 1050
        VSC_C_mgt = VSC_mgt_New(NULL, NULL, "");
969
970 14720
        VTAILQ_FOREACH(alp, &arglist, list) {
971 14720
                if (!vstrcmp(alp->arg, "M"))
972 1025
                        mgt_cli_master(alp->val);
973 13695
                else if (!vstrcmp(alp->arg, "T") && vstrcmp(alp->val, "none"))
974 1038
                        mgt_cli_telnet(alp->val);
975 12657
                else if (!vstrcmp(alp->arg, "P"))
976 2063
                        VPF_Write(alp->priv);
977 14720
        }
978
979 1050
        AZ(VSB_finish(vident));
980
981 1050
        if (S_arg == NULL)
982 1038
                S_arg = make_secret(workdir);
983 1050
        AN(S_arg);
984
985 1050
        MGT_Complain(C_DEBUG, "Version: %s", VCS_String("V"));
986 1050
        MGT_Complain(C_DEBUG, "Platform: %s", VSB_data(vident) + 1);
987
988 1050
        if (d_flag)
989 1028
                mgt_cli_setup(0, 1, 1, "debug", mgt_stdin_close, NULL);
990
991 1050
        if (vstrcmp(S_arg, "none"))
992 1037
                mgt_cli_secret(S_arg);
993
994 1050
        memset(&sac, 0, sizeof sac);
995 1050
        sac.sa_handler = SIG_IGN;
996 1050
        sac.sa_flags = SA_RESTART;
997
998 1050
        AZ(sigaction(SIGPIPE, &sac, NULL));
999 1050
        AZ(sigaction(SIGHUP, &sac, NULL));
1000
1001 1050
        MCH_Init();
1002
1003 1050
        if (I_fd >= 0) {
1004 4
                fprintf(stderr, "BEGIN of -I file processing\n");
1005
                /* We must dup stderr, because VCLS closes the output fd */
1006 4
                mgt_cli_setup(I_fd, dup(2), 1, "-I file", mgt_I_close, stderr);
1007 11
                while (I_fd >= 0) {
1008 7
                        o = VEV_Once(mgt_evb);
1009 7
                        if (o != 1)
1010 0
                                MGT_Complain(C_ERR,
1011 0
                                    "VEV_Once() = %d", o);
1012 7
                }
1013 4
        }
1014 1050
        assert(I_fd == -1);
1015
1016 1050
        err = mgt_has_vcl();
1017 1050
        if (!d_flag && err != NULL && !arg_list_count("f"))
1018 0
                MGT_Complain(C_ERR, "%s", err);
1019
1020 1050
        if (err == NULL && !d_flag)
1021 6
                u = MCH_Start_Child();
1022 1044
        else
1023 1044
                u = 0;
1024
1025 1050
        if (eric_fd >= 0)
1026 5
                mgt_eric_im_done(eric_fd, u);
1027
1028 1050
        if (u)
1029 5
                exit(u);
1030
1031
        /* Failure is no longer an option */
1032
1033 1045
        if (F_flag)
1034 3
                VFIL_null_fd(STDIN_FILENO);
1035
1036 1045
        e = VEV_Alloc();
1037 1045
        AN(e);
1038 1045
        e->callback = mgt_uptime;
1039 1045
        e->timeout = 1.0;
1040 1045
        e->name = "mgt_uptime";
1041 1045
        AZ(VEV_Start(mgt_evb, e));
1042
1043 1045
        e = VEV_Alloc();
1044 1045
        AN(e);
1045 1045
        e->sig = SIGTERM;
1046 1045
        e->callback = mgt_sigint;
1047 1045
        e->name = "SIGTERM";
1048 1045
        AZ(VEV_Start(mgt_evb, e));
1049
1050 1045
        e = VEV_Alloc();
1051 1045
        AN(e);
1052 1045
        e->sig = SIGINT;
1053 1045
        e->callback = mgt_sigint;
1054 1045
        e->name = "SIGINT";
1055 1045
        AZ(VEV_Start(mgt_evb, e));
1056
1057 1045
        o = VEV_Loop(mgt_evb);
1058 1045
        if (o != 0 && o != -42)
1059 0
                MGT_Complain(C_ERR, "VEV_Loop() = %d", o);
1060
1061 1045
        MGT_Complain(C_INFO, "manager stopping child");
1062 1045
        MCH_Stop_Child();
1063 1045
        MGT_Complain(C_INFO, "manager dies");
1064 1045
        mgt_cli_close_all();
1065 1045
        VEV_Destroy(&mgt_evb);
1066 1045
        VJ_master(JAIL_MASTER_SYSTEM);
1067
        /*lint -e(730)*/
1068 1045
        vext_cleanup(! MGT_DO_DEBUG(DBG_VMOD_SO_KEEP));
1069 1045
        (void)rmdir("vext_cache");
1070 1045
        VJ_master(JAIL_MASTER_LOW);
1071 14674
        VTAILQ_FOREACH(alp, &arglist, list) {
1072 14674
                if (!vstrcmp(alp->arg, "P"))
1073 2054
                        VPF_Remove(alp->priv);
1074 14674
        }
1075 1045
        exit(exit_status);
1076 1045
}