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