varnish-cache/bin/varnishd/cache/cache_cli.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2011 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
 * Caching process CLI handling.
31
 *
32
 * We only have one CLI source, the stdin/stdout pipes from the manager
33
 * process, but we complicate things by having undocumented commands that
34
 * we do not want to show in a plain help, and by having commands that the
35
 * manager has already shown in help before asking us.
36
 */
37
38
#include "config.h"
39
40
#include "cache_varnishd.h"
41
#include "common/heritage.h"
42
43
#include "vcli_serve.h"
44
45
pthread_t               cli_thread;
46
static struct lock      cli_mtx;
47
static int              add_check;
48
static struct VCLS      *cache_cls;
49
50
/*
51
 * The CLI commandlist is split in three:
52
 *  - Commands we get from/share with the manager, we don't show these
53
 *      in help, as the manager already did that.
54
 *  - Cache process commands, show in help
55
 *  - Undocumented debug commands, show in undocumented "help -d"
56
 */
57
58
/*--------------------------------------------------------------------
59
 * Add CLI functions to the appropriate command set
60
 */
61
62
void
63 404331
CLI_AddFuncs(struct cli_proto *p)
64
{
65
66 404331
        AZ(add_check);
67 404331
        Lck_Lock(&cli_mtx);
68 404331
        VCLS_AddFunc(cache_cls, 0, p);
69 404331
        Lck_Unlock(&cli_mtx);
70 404331
}
71
72
static void
73 298952
cli_cb_before(const struct cli *cli)
74
{
75
76 298952
        ASSERT_CLI();
77 298952
        VSL(SLT_CLI, NO_VXID, "Rd %s", VSB_data(cli->cmd));
78 298952
        Lck_Lock(&cli_mtx);
79 298952
        VCL_Poll();
80 298952
}
81
82
static void
83 298872
cli_cb_after(const struct cli *cli)
84
{
85
86 298872
        ASSERT_CLI();
87 298872
        Lck_Unlock(&cli_mtx);
88 597744
        VSL(SLT_CLI, NO_VXID, "Wr %03u %zd %s",
89 298872
            cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
90 298872
}
91
92
void
93 36591
CLI_Run(void)
94
{
95
        int i;
96
        struct cli *cli;
97
98 36591
        add_check = 1;
99
100
        /* Tell waiting MGT that we are ready to speak CLI */
101 36591
        AZ(VCLI_WriteResult(heritage.cli_fd, CLIS_OK, "Ready"));
102
103 73040
        cli = VCLS_AddFd(cache_cls,
104 36520
            heritage.cli_fd, heritage.cli_fd, NULL, NULL);
105 36520
        AN(cli);
106 36520
        cli->auth = 255;        // Non-zero to disable paranoia in vcli_serve
107
108 36520
        do {
109 335392
                i = VCLS_Poll(cache_cls, cli, -1);
110 335392
        } while (i == 0);
111 36520
        VSL(SLT_CLI, NO_VXID, "EOF on CLI connection, worker stops");
112 36520
}
113
114
/*--------------------------------------------------------------------*/
115
116
static struct cli_proto cli_cmds[] = {
117
        { CLICMD_PING,  "i", VCLS_func_ping, VCLS_func_ping_json },
118
        { CLICMD_HELP,  "i", VCLS_func_help, VCLS_func_help_json },
119
        { NULL }
120
};
121
122
/*--------------------------------------------------------------------
123
 * Initialize the CLI subsystem
124
 */
125
126
void
127 36676
CLI_Init(void)
128
{
129
130 36676
        Lck_New(&cli_mtx, lck_cli);
131 36676
        cli_thread = pthread_self();
132
133 36676
        cache_cls = VCLS_New(heritage.cls);
134 36676
        AN(cache_cls);
135 36676
        VCLS_SetLimit(cache_cls, &cache_param->cli_limit);
136 36676
        VCLS_SetHooks(cache_cls, cli_cb_before, cli_cb_after);
137
138 36676
        CLI_AddFuncs(cli_cmds);
139 36676
}