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 222583
CLI_AddFuncs(struct cli_proto *p)
64
{
65
66 222583
        AZ(add_check);
67 222583
        Lck_Lock(&cli_mtx);
68 222583
        VCLS_AddFunc(cache_cls, 0, p);
69 222583
        Lck_Unlock(&cli_mtx);
70 222583
}
71
72
static void
73 180676
cli_cb_before(const struct cli *cli)
74
{
75
76 180676
        ASSERT_CLI();
77 180676
        VSL(SLT_CLI, NO_VXID, "Rd %s", VSB_data(cli->cmd));
78 180676
        Lck_Lock(&cli_mtx);
79 180676
        VCL_Poll();
80 180676
}
81
82
static void
83 180626
cli_cb_after(const struct cli *cli)
84
{
85
86 180626
        ASSERT_CLI();
87 180626
        Lck_Unlock(&cli_mtx);
88 361252
        VSL(SLT_CLI, NO_VXID, "Wr %03u %zd %s",
89 180626
            cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
90 180626
}
91
92
void
93 22144
CLI_Run(void)
94
{
95
        int i;
96
        struct cli *cli;
97
98 22144
        add_check = 1;
99
100
        /* Tell waiting MGT that we are ready to speak CLI */
101 22144
        AZ(VCLI_WriteResult(heritage.cli_fd, CLIS_OK, "Ready"));
102
103 44200
        cli = VCLS_AddFd(cache_cls,
104 22100
            heritage.cli_fd, heritage.cli_fd, NULL, NULL);
105 22100
        AN(cli);
106 22100
        cli->auth = 255;        // Non-zero to disable paranoia in vcli_serve
107
108 22100
        do {
109 202726
                i = VCLS_Poll(cache_cls, cli, -1);
110 202726
        } while (i == 0);
111 22100
        VSL(SLT_CLI, NO_VXID, "EOF on CLI connection, worker stops");
112 22100
}
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 22197
CLI_Init(void)
128
{
129
130 22197
        Lck_New(&cli_mtx, lck_cli);
131 22197
        cli_thread = pthread_self();
132
133 22197
        cache_cls = VCLS_New(heritage.cls);
134 22197
        AN(cache_cls);
135 22197
        VCLS_SetLimit(cache_cls, &cache_param->cli_limit);
136 22197
        VCLS_SetHooks(cache_cls, cli_cb_before, cli_cb_after);
137
138 22197
        CLI_AddFuncs(cli_cmds);
139 22197
}