varnish-cache/bin/varnishd/cache/cache_cli.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2011 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 * Caching process CLI handling.
30
 *
31
 * We only have one CLI source, the stdin/stdout pipes from the manager
32
 * process, but we complicate things by having undocumented commands that
33
 * we do not want to show in a plain help, and by having commands that the
34
 * manager has already shown in help before asking us.
35
 */
36
37
#include "config.h"
38
39
#include "cache_varnishd.h"
40
#include "common/heritage.h"
41
42
#include "vcli_serve.h"
43
44
pthread_t               cli_thread;
45
static struct lock      cli_mtx;
46
static int              add_check;
47
static struct VCLS      *cache_cls;
48
49
/*
50
 * The CLI commandlist is split in three:
51
 *  - Commands we get from/share with the manager, we don't show these
52
 *      in help, as the manager already did that.
53
 *  - Cache process commands, show in help
54
 *  - Undocumented debug commands, show in undocumented "help -d"
55
 */
56
57
/*--------------------------------------------------------------------
58
 * Add CLI functions to the appropriate command set
59
 */
60
61
void
62 18582
CLI_AddFuncs(struct cli_proto *p)
63
{
64
65 18582
        AZ(add_check);
66 18582
        Lck_Lock(&cli_mtx);
67 18582
        VCLS_AddFunc(cache_cls, 0, p);
68 18582
        Lck_Unlock(&cli_mtx);
69 18582
}
70
71
static void
72 16852
cli_cb_before(const struct cli *cli)
73
{
74
75 16852
        ASSERT_CLI();
76 16852
        VSL(SLT_CLI, 0, "Rd %s", VSB_data(cli->cmd));
77 16852
        VCL_Poll();
78 16852
        VBE_Poll();
79 16852
        Lck_Lock(&cli_mtx);
80 16852
}
81
82
static void
83 16849
cli_cb_after(const struct cli *cli)
84
{
85
86 16849
        ASSERT_CLI();
87 16849
        Lck_Unlock(&cli_mtx);
88 50547
        VSL(SLT_CLI, 0, "Wr %03u %zd %s",
89 50547
            cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
90 16849
}
91
92
void
93 2055
CLI_Run(void)
94
{
95
        int i;
96
        struct cli *cli;
97
98 2055
        add_check = 1;
99
100 2055
        cli = VCLS_AddFd(cache_cls,
101
            heritage.cli_in, heritage.cli_out, NULL, NULL);
102 2055
        AN(cli);
103 2055
        cli->auth = 1;  // Non-zero to disable paranoia in vcli_serve
104
105
        do {
106 18904
                i = VCLS_Poll(cache_cls, cli, -1);
107 18886
        } while (i == 0);
108 2037
        VSL(SLT_CLI, 0, "EOF on CLI connection, worker stops");
109 2037
}
110
111
/*--------------------------------------------------------------------*/
112
113
static struct cli_proto cli_cmds[] = {
114
        { CLICMD_PING,  "i", VCLS_func_ping, VCLS_func_ping_json },
115
        { CLICMD_HELP,  "i", VCLS_func_help, VCLS_func_help_json },
116
        { NULL }
117
};
118
119
/*--------------------------------------------------------------------
120
 * Initialize the CLI subsystem
121
 */
122
123
void
124 2055
CLI_Init(void)
125
{
126
127 2055
        Lck_New(&cli_mtx, lck_cli);
128 2055
        cli_thread = pthread_self();
129
130 2055
        cache_cls = VCLS_New(heritage.cls);
131 2055
        AN(cache_cls);
132 2055
        VCLS_SetLimit(cache_cls, &cache_param->cli_limit);
133 2055
        VCLS_SetHooks(cache_cls, cli_cb_before, cli_cb_after);
134
135 2055
        CLI_AddFuncs(cli_cmds);
136 2055
}