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 50848
CLI_AddFuncs(struct cli_proto *p)
64
{
65
66 50848
        AZ(add_check);
67 50848
        Lck_Lock(&cli_mtx);
68 50848
        VCLS_AddFunc(cache_cls, 0, p);
69 50848
        Lck_Unlock(&cli_mtx);
70 50848
}
71
72
static void
73 37604
cli_cb_before(const struct cli *cli)
74
{
75
76 37604
        ASSERT_CLI();
77 37604
        VSL(SLT_CLI, NO_VXID, "Rd %s", VSB_data(cli->cmd));
78 37604
        Lck_Lock(&cli_mtx);
79 37604
        VCL_Poll();
80 37604
        VCP_RelPoll();
81 37604
}
82
83
static void
84 37594
cli_cb_after(const struct cli *cli)
85
{
86
87 37594
        ASSERT_CLI();
88 37594
        Lck_Unlock(&cli_mtx);
89 75188
        VSL(SLT_CLI, NO_VXID, "Wr %03u %zd %s",
90 37594
            cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
91 37594
}
92
93
void
94 4601
CLI_Run(void)
95
{
96
        int i;
97
        struct cli *cli;
98
99 4601
        add_check = 1;
100
101
        /* Tell waiting MGT that we are ready to speak CLI */
102 4601
        AZ(VCLI_WriteResult(heritage.cli_fd, CLIS_OK, "Ready"));
103
104 9190
        cli = VCLS_AddFd(cache_cls,
105 4595
            heritage.cli_fd, heritage.cli_fd, NULL, NULL);
106 4595
        AN(cli);
107 4595
        cli->auth = 255;        // Non-zero to disable paranoia in vcli_serve
108
109 4595
        do {
110 42189
                i = VCLS_Poll(cache_cls, cli, -1);
111 42189
        } while (i == 0);
112 4595
        VSL(SLT_CLI, NO_VXID, "EOF on CLI connection, worker stops");
113 4595
}
114
115
/*--------------------------------------------------------------------*/
116
117
static struct cli_proto cli_cmds[] = {
118
        { CLICMD_PING,  "i", VCLS_func_ping, VCLS_func_ping_json },
119
        { CLICMD_HELP,  "i", VCLS_func_help, VCLS_func_help_json },
120
        { NULL }
121
};
122
123
/*--------------------------------------------------------------------
124
 * Initialize the CLI subsystem
125
 */
126
127
void
128 4613
CLI_Init(void)
129
{
130
131 4613
        Lck_New(&cli_mtx, lck_cli);
132 4613
        cli_thread = pthread_self();
133
134 4613
        cache_cls = VCLS_New(heritage.cls);
135 4613
        AN(cache_cls);
136 4613
        VCLS_SetLimit(cache_cls, &cache_param->cli_limit);
137 4613
        VCLS_SetHooks(cache_cls, cli_cb_before, cli_cb_after);
138
139 4613
        CLI_AddFuncs(cli_cmds);
140 4613
}