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 5555
CLI_AddFuncs(struct cli_proto *p)
63
{
64
65 5555
        AZ(add_check);
66 5555
        Lck_Lock(&cli_mtx);
67 5555
        VCLS_AddFunc(cache_cls, 0, p);
68 5555
        Lck_Unlock(&cli_mtx);
69 5555
}
70
71
static void
72 4986
cli_cb_before(const struct cli *cli)
73
{
74
75 4986
        ASSERT_CLI();
76 4986
        VSL(SLT_CLI, 0, "Rd %s", VSB_data(cli->cmd));
77 4986
        VCL_Poll();
78 4986
        VBE_Poll();
79 4986
        Lck_Lock(&cli_mtx);
80 4986
}
81
82
static void
83 4985
cli_cb_after(const struct cli *cli)
84
{
85
86 4985
        ASSERT_CLI();
87 4985
        Lck_Unlock(&cli_mtx);
88 14955
        VSL(SLT_CLI, 0, "Wr %03u %zd %s",
89 14955
            cli->result, VSB_len(cli->sb), VSB_data(cli->sb));
90 4985
}
91
92
void
93 614
CLI_Run(void)
94
{
95
        int i;
96
        struct cli *cli;
97
98 614
        add_check = 1;
99
100 614
        cli = VCLS_AddFd(cache_cls,
101
            heritage.cli_in, heritage.cli_out, NULL, NULL);
102 614
        AN(cli);
103 614
        cli->auth = 1;  // Non-zero to disable paranoia in vcli_serve
104
105
        do {
106 5599
                i = VCLS_Poll(cache_cls, cli, -1);
107 5594
        } while (i == 0);
108 609
        VSL(SLT_CLI, 0, "EOF on CLI connection, worker stops");
109 609
}
110
111
/*--------------------------------------------------------------------*/
112
113
static struct cli_proto cli_cmds[] = {
114
        { CLICMD_PING,  "i", VCLS_func_ping },
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 614
CLI_Init(void)
125
{
126
127 614
        Lck_New(&cli_mtx, lck_cli);
128 614
        cli_thread = pthread_self();
129
130 614
        cache_cls = VCLS_New(heritage.cls);
131 614
        AN(cache_cls);
132 614
        VCLS_SetLimit(cache_cls, &cache_param->cli_limit);
133 614
        VCLS_SetHooks(cache_cls, cli_cb_before, cli_cb_after);
134
135 614
        CLI_AddFuncs(cli_cmds);
136 614
}