varnish-cache/lib/libvarnish/vcli_serve.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
 * Stuff for handling the CLI protocol
31
 */
32
33
#include "config.h"
34
35
#include <time.h>
36
#include <ctype.h>
37
#include <poll.h>
38
#include <stdarg.h>
39
#include <stdint.h>
40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <string.h>
43
#include <unistd.h>
44
45
#include "vdef.h"
46
#include "vas.h"
47
#include "vqueue.h"
48
#include "miniobj.h"
49
50
#include "vav.h"
51
#include "vcli_serve.h"
52
#include "vsb.h"
53
#include "vtim.h"
54
55
struct VCLS_fd {
56
        unsigned                        magic;
57
#define VCLS_FD_MAGIC                   0x010dbd1e
58
        VTAILQ_ENTRY(VCLS_fd)           list;
59
        int                             fdi, fdo;
60
        struct VCLS                     *cls;
61
        struct cli                      *cli, clis;
62
        cls_cb_f                        *closefunc;
63
        void                            *priv;
64
        struct vsb                      *last_arg;
65
        char                            **argv;
66
        int                             argc;
67
        char                            *match;
68
};
69
70
struct VCLS {
71
        unsigned                        magic;
72
#define VCLS_MAGIC                      0x60f044a3
73
        VTAILQ_HEAD(,VCLS_fd)           fds;
74
        unsigned                        nfd;
75
        VTAILQ_HEAD(,cli_proto)         funcs;
76
        cls_cbc_f                       *before, *after;
77
        volatile unsigned               *limit;
78
        struct cli_proto                *wildcard;
79
};
80
81
/*--------------------------------------------------------------------*/
82
83
void v_matchproto_(cli_func_t)
84 3
VCLS_func_close(struct cli *cli, const char *const *av, void *priv)
85
{
86
87 3
        (void)av;
88 3
        (void)priv;
89 3
        VCLI_Out(cli, "Closing CLI connection");
90 3
        VCLI_SetResult(cli, CLIS_CLOSE);
91 3
}
92
93
/*--------------------------------------------------------------------*/
94
95
void v_matchproto_(cli_func_t)
96 238
VCLS_func_ping(struct cli *cli, const char * const *av, void *priv)
97
{
98
        time_t t;
99
100 238
        (void)av;
101 238
        (void)priv;
102 238
        t = time(NULL);
103 238
        VCLI_Out(cli, "PONG %jd 1.0", (intmax_t)t);
104 238
}
105
106
void v_matchproto_(cli_func_t)
107 2
VCLS_func_ping_json(struct cli *cli, const char * const *av, void *priv)
108
{
109 2
        (void)av;
110 2
        (void)priv;
111 2
        VCLI_JSON_begin(cli, 2, av);
112 2
        VCLI_Out(cli, ", \"PONG\"\n");
113 2
        VCLI_JSON_end(cli);
114 2
}
115
116
/*--------------------------------------------------------------------*/
117
118
static void
119 233
help_helper(struct cli *cli, struct cli_proto *clp, const char * const *av)
120
{
121 233
        AN(clp->desc->syntax);
122 233
        if (av[0] != NULL)
123 3
                VCLI_Out(cli, "%s\n%s\n", clp->desc->syntax, clp->desc->help);
124
        else
125 230
                VCLI_Out(cli, "%s\n", clp->desc->syntax);
126 233
}
127
128
void v_matchproto_(cli_func_t)
129 18
VCLS_func_help(struct cli *cli, const char * const *av, void *priv)
130
{
131
        struct cli_proto *clp;
132 18
        unsigned filter = 1, d;
133
        struct VCLS *cs;
134
135 18
        (void)priv;
136 18
        cs = cli->cls;
137 18
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
138
139 22
        for (av += 2; av[0] != NULL && av[0][0] == '-'; av++) {
140 6
                if (!strcmp(av[0], "-a")) {
141 2
                        filter = 3;
142 6
                } else if (!strcmp(av[0], "-d")) {
143 2
                        filter = 2;
144 2
                } else {
145 2
                        VCLI_Out(cli, "Unknown flag\n");
146 2
                        VCLI_SetResult(cli, CLIS_UNKNOWN);
147 2
                        return;
148
                }
149 4
        }
150 437
        VTAILQ_FOREACH(clp, &cs->funcs, list) {
151 424
                if (clp->auth > cli->auth)
152 0
                        continue;
153 424
                if (av[0] != NULL && !strcmp(clp->desc->request, av[0])) {
154 3
                        help_helper(cli, clp, av);
155 3
                        return;
156 421
                } else if (av[0] == NULL) {
157 314
                        d = strchr(clp->flags, 'd') != NULL ? 2 : 1;
158 314
                        if (filter & d)
159 230
                                help_helper(cli, clp, av);
160 314
                }
161 421
        }
162 13
        if (av[0] != NULL) {
163 2
                VCLI_Out(cli, "Unknown request.\nType 'help' for more info.\n");
164 2
                VCLI_SetResult(cli, CLIS_UNKNOWN);
165 2
        }
166 18
}
167
168
void v_matchproto_(cli_func_t)
169 6
VCLS_func_help_json(struct cli *cli, const char * const *av, void *priv)
170
{
171
        struct cli_proto *clp;
172
        struct VCLS *cs;
173
174 6
        (void)priv;
175 6
        cs = cli->cls;
176 6
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
177
178 6
        VCLI_JSON_begin(cli, 2, av);
179 234
        VTAILQ_FOREACH(clp, &cs->funcs, list) {
180 228
                if (clp->auth > cli->auth)
181 0
                        continue;
182 228
                VCLI_Out(cli, ",\n  {\n");
183 228
                VSB_indent(cli->sb, 2);
184 228
                VCLI_Out(cli, "\"request\": ");
185 228
                VCLI_JSON_str(cli, clp->desc->request);
186 228
                VCLI_Out(cli, ",\n");
187 228
                VCLI_Out(cli, "\"syntax\": ");
188 228
                VCLI_JSON_str(cli, clp->desc->syntax);
189 228
                VCLI_Out(cli, ",\n");
190 228
                VCLI_Out(cli, "\"help\": ");
191 228
                VCLI_JSON_str(cli, clp->desc->help);
192 228
                VCLI_Out(cli, ",\n");
193 228
                VCLI_Out(cli, "\"minarg\": %d", clp->desc->minarg);
194 228
                VCLI_Out(cli, ",\n");
195 228
                VCLI_Out(cli, "\"maxarg\": %d", clp->desc->maxarg);
196 228
                VCLI_Out(cli, ",\n");
197 228
                VCLI_Out(cli, "\"flags\": ");
198 228
                VCLI_JSON_str(cli, clp->flags);
199 228
                VCLI_Out(cli, ",\n");
200 456
                VCLI_Out(cli, "\"json\": %s",
201 228
                    clp->jsonfunc == NULL ? "false" : "true");
202 228
                VCLI_Out(cli, "\n");
203 228
                VSB_indent(cli->sb, -2);
204 228
                VCLI_Out(cli, "}");
205 228
        }
206 6
        VCLI_JSON_end(cli);
207 6
}
208
209
/*--------------------------------------------------------------------
210
 * Look for a CLI command to execute
211
 */
212
213
static void
214 21020
cls_dispatch(struct cli *cli, struct VCLS *cs, char * const * av, int ac)
215
{
216 21020
        int json = 0;
217
        struct cli_proto *cp;
218
219 21020
        AN(av);
220 21020
        assert(ac >= 0);
221 21020
        AZ(av[0]);
222 21020
        AN(av[1]);
223
224 383767
        VTAILQ_FOREACH(cp, &cs->funcs, list) {
225 379716
                if (cp->auth > cli->auth)
226 0
                        continue;
227 379716
                if (!strcmp(cp->desc->request, av[1]))
228 16969
                        break;
229 362747
        }
230
231 21020
        if (cp == NULL && cs->wildcard && cs->wildcard->auth <= cli->auth)
232 4050
                cp = cs->wildcard;
233
234 21020
        if (cp == NULL) {
235 1
                VCLI_Out(cli, "Unknown request.\nType 'help' for more info.\n");
236 1
                return;
237
        }
238
239 21019
        VSB_clear(cli->sb);
240
241 21019
        if (ac > 1 && !strcmp(av[2], "-j"))
242 80
                json = 1;
243
244 21019
        if (cp->func == NULL && !json) {
245 0
                VCLI_Out(cli, "Unimplemented\n");
246 0
                VCLI_SetResult(cli, CLIS_UNIMPL);
247 0
                return;
248
        }
249 21019
        if (cp->jsonfunc == NULL && json) {
250 1
                VCLI_Out(cli, "JSON unimplemented\n");
251 1
                VCLI_SetResult(cli, CLIS_UNIMPL);
252 1
                return;
253
        }
254
255 21018
        if (ac - 1 < cp->desc->minarg + json) {
256 4
                VCLI_Out(cli, "Too few parameters\n");
257 4
                VCLI_SetResult(cli, CLIS_TOOFEW);
258 4
                return;
259
        }
260
261 21014
        if (cp->desc->maxarg >= 0 && ac - 1 > cp->desc->maxarg + json) {
262 2
                VCLI_Out(cli, "Too many parameters\n");
263 2
                VCLI_SetResult(cli, CLIS_TOOMANY);
264 2
                return;
265
        }
266
267 21012
        cli->result = CLIS_OK;
268 21012
        cli->cls = cs;
269 21012
        if (json)
270 79
                cp->jsonfunc(cli, (const char * const *)av, cp->priv);
271
        else
272 20933
                cp->func(cli, (const char * const *)av, cp->priv);
273 21012
        cli->cls = NULL;
274 21020
}
275
276
/*--------------------------------------------------------------------
277
 * We have collected a full cli line, parse it and execute, if possible.
278
 */
279
280
static int
281 21026
cls_exec(struct VCLS_fd *cfd, char * const *av, int ac)
282
{
283
        struct VCLS *cs;
284
        struct cli *cli;
285
        ssize_t len;
286
        char *s;
287
        unsigned lim;
288 21026
        int retval = 0;
289
290 21026
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
291 21026
        cs = cfd->cls;
292 21026
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
293
294 21026
        cli = cfd->cli;
295 21026
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
296 21026
        AN(cli->cmd);
297
298 21026
        cli->result = CLIS_UNKNOWN;
299 21026
        VSB_clear(cli->sb);
300
301 21026
        if (cs->before != NULL)
302 21026
                cs->before(cli);
303
304 21026
        do {
305 21026
                if (av[0] != NULL) {
306 2
                        VCLI_Out(cli, "Syntax Error: %s\n", av[0]);
307 2
                        VCLI_SetResult(cli, CLIS_SYNTAX);
308 2
                        break;
309
                }
310
311 21024
                if (av[1] == NULL) {
312 1
                        VCLI_Out(cli, "Empty CLI command.\n");
313 1
                        VCLI_SetResult(cli, CLIS_SYNTAX);
314 1
                        break;
315
                }
316
317 21023
                if (!islower(av[1][0])) {
318 3
                        VCLI_Out(cli, "All commands are in lower-case.\n");
319 3
                        VCLI_SetResult(cli, CLIS_UNKNOWN);
320 3
                        break;
321
                }
322
323 21020
                cls_dispatch(cli, cs, av, ac);
324
325 21020
        } while (0);
326
327 21026
        AZ(VSB_finish(cli->sb));
328
329 21026
        if (cs->after != NULL)
330 21024
                cs->after(cli);
331
332 21026
        s = VSB_data(cli->sb);
333 21026
        len = VSB_len(cli->sb);
334 21026
        lim = *cs->limit;
335 21026
        if (len > lim) {
336 5
                if (cli->result == CLIS_OK)
337 0
                        cli->result = CLIS_TRUNCATED;
338 5
                s[lim - 1] = '\0';
339 5
                assert(strlen(s) <= lim);
340 5
        }
341 21026
        if (VCLI_WriteResult(cfd->fdo, cli->result, s) ||
342 21023
            cli->result == CLIS_CLOSE)
343 3
                retval = 1;
344
345
        /*
346
         * In unauthenticated mode we are very intolerant, and close the
347
         * connection at the least provocation.
348
         */
349 21026
        if (cli->auth == 0 && cli->result != CLIS_OK)
350 0
                retval = 1;
351
352 21026
        return (retval);
353
}
354
355
static int
356 33390
cls_feed(struct VCLS_fd *cfd, const char *p, const char *e)
357
{
358
        struct cli *cli;
359 33390
        int i, retval = 0, ac;
360
        char **av, *q;
361
362 33390
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
363 33390
        AN(p);
364 33390
        assert(e > p);
365
366 33390
        cli = cfd->cli;
367 33390
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
368
369 929199
        for (;p < e; p++) {
370 895812
                if (cli->cmd == NULL && isspace(*p)) {
371
                        /* Ignore all leading space before cmd */
372 2412
                        continue;
373
                }
374 893400
                if (cfd->argv == NULL) {
375
376
                        /* Collect first line up to \n or \r */
377 431931
                        if (cli->cmd == NULL) {
378 21028
                                cli->cmd = VSB_new_auto();
379 21028
                                AN(cli->cmd);
380 21028
                        }
381
382
                        /* Until authenticated, limit length hard */
383 499551
                        if (*p != '\n' && *p != '\r' &&
384 410907
                            (cli->auth > 0 || VSB_len(cli->cmd) < 80)) {
385 410907
                                VSB_putc(cli->cmd, *p);
386 410907
                                continue;
387
                        }
388
389 21024
                        AZ(VSB_finish(cli->cmd));
390
391
                        /* Ignore leading '-' */
392 21024
                        q = VSB_data(cli->cmd);
393 21024
                        if (*q == '-')
394 4
                                q++;
395 21024
                        av = VAV_Parse(q, &ac, 0);
396 21024
                        AN(av);
397
398 22492
                        if (cli->auth > 0 &&
399 20047
                            av[0] == NULL &&
400 20045
                            ac >= 3 &&
401 8114
                            !strcmp(av[ac-2], "<<") &&
402 1468
                            *av[ac - 1] != '\0') {
403
                                /* Go to "<< nonce" mode */
404 1468
                                cfd->argv = av;
405 1468
                                cfd->argc = ac;
406 1468
                                cfd->match = av[ac - 1];
407 1468
                                cfd->last_arg = VSB_new_auto();
408 1468
                                AN(cfd->last_arg);
409 1468
                        } else {
410
                                /* Plain command */
411 19556
                                i = cls_exec(cfd, av, ac - 1);
412 19556
                                VAV_Free(av);
413 19556
                                VSB_destroy(&cli->cmd);
414 19556
                                if (i)
415 3
                                        return (i);
416
                        }
417 21021
                } else {
418
                        /* "<< nonce" mode */
419 461469
                        AN(cfd->argv);
420 461469
                        AN(cfd->argc);
421 461469
                        AN(cfd->match);
422 461469
                        AN(cfd->last_arg);
423 461469
                        if (*cfd->match == '\0' && (*p == '\r' || *p == '\n')) {
424 1467
                                AZ(VSB_finish(cfd->last_arg));
425
                                // NB: VAV lib internals trusted
426 1467
                                cfd->match = NULL;
427 1467
                                REPLACE(cfd->argv[cfd->argc - 1], NULL);
428 1467
                                REPLACE(cfd->argv[cfd->argc - 2], NULL);
429 1467
                                cfd->argv[cfd->argc - 2] =
430 1467
                                    VSB_data(cfd->last_arg);
431 1467
                                i = cls_exec(cfd, cfd->argv, cfd->argc - 2);
432 1467
                                cfd->argv[cfd->argc - 2] = NULL;
433 1467
                                VAV_Free(cfd->argv);
434 1467
                                cfd->argv = NULL;
435 1467
                                VSB_destroy(&cfd->last_arg);
436 1467
                                VSB_destroy(&cli->cmd);
437 1467
                                if (i)
438 0
                                        return (i);
439 461469
                        } else if (*p == *cfd->match) {
440 26289
                                cfd->match++;
441 460002
                        } else if (cfd->match != cfd->argv[cfd->argc - 1]) {
442 2843
                                q = cfd->argv[cfd->argc - 1];
443 2843
                                VSB_bcat(cfd->last_arg, q, cfd->match - q);
444 2843
                                cfd->match = q;
445 2843
                                VSB_putc(cfd->last_arg, *p);
446 2843
                        } else {
447 430870
                                VSB_putc(cfd->last_arg, *p);
448
                        }
449
                }
450 482490
        }
451 33387
        return (retval);
452 33390
}
453
454
struct VCLS *
455 1957
VCLS_New(struct VCLS *model)
456
{
457
        struct VCLS *cs;
458
459 1957
        CHECK_OBJ_ORNULL(model, VCLS_MAGIC);
460
461 1957
        ALLOC_OBJ(cs, VCLS_MAGIC);
462 1957
        AN(cs);
463 1957
        VTAILQ_INIT(&cs->fds);
464 1957
        VTAILQ_INIT(&cs->funcs);
465 1957
        if (model != NULL)
466 928
                VTAILQ_CONCAT(&cs->funcs, &model->funcs, list);
467 1957
        return (cs);
468
}
469
470
void
471 1890
VCLS_SetLimit(struct VCLS *cs, volatile unsigned *limit)
472
{
473 1890
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
474 1890
        cs->limit = limit;
475 1890
}
476
477
void
478 1957
VCLS_SetHooks(struct VCLS *cs, cls_cbc_f *before, cls_cbc_f *after)
479
{
480
481 1957
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
482 1957
        cs->before = before;
483 1957
        cs->after = after;
484 1957
}
485
486
struct cli *
487 2851
VCLS_AddFd(struct VCLS *cs, int fdi, int fdo, cls_cb_f *closefunc, void *priv)
488
{
489
        struct VCLS_fd *cfd;
490
491 2851
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
492 2851
        assert(fdi >= 0);
493 2851
        assert(fdo >= 0);
494 2851
        ALLOC_OBJ(cfd, VCLS_FD_MAGIC);
495 2851
        AN(cfd);
496 2851
        cfd->cls = cs;
497 2851
        cfd->fdi = fdi;
498 2851
        cfd->fdo = fdo;
499 2851
        cfd->cli = &cfd->clis;
500 2851
        cfd->cli->magic = CLI_MAGIC;
501 2851
        cfd->cli->sb = VSB_new_auto();
502 2851
        AN(cfd->cli->sb);
503 2851
        cfd->cli->limit = cs->limit;
504 2851
        cfd->cli->priv = priv;
505 2851
        cfd->closefunc = closefunc;
506 2851
        cfd->priv = priv;
507 2851
        VTAILQ_INSERT_TAIL(&cs->fds, cfd, list);
508 2851
        cs->nfd++;
509 2851
        return (cfd->cli);
510
}
511
512
static int
513 2841
cls_close_fd(struct VCLS *cs, struct VCLS_fd *cfd)
514
{
515 2841
        int retval = 0;
516
517 2841
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
518 2841
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
519
520 2841
        VTAILQ_REMOVE(&cs->fds, cfd, list);
521 2841
        if (cfd->match != NULL) {
522 2
                cfd->cli->result = CLIS_TRUNCATED;
523 2
                if (cs->after != NULL)
524 1
                        cs->after(cfd->cli);
525 2
                VSB_destroy(&cfd->last_arg);
526 2841
        } else if (cfd->cli->cmd != NULL) {
527 2
                (void)VSB_finish(cfd->cli->cmd);
528 2
                cfd->cli->result = CLIS_TRUNCATED;
529 2
                if (cs->after != NULL)
530 1
                        cs->after(cfd->cli);
531 2
                VSB_destroy(&cfd->cli->cmd);
532 2
        }
533 2837
        cs->nfd--;
534 2837
        VSB_destroy(&cfd->cli->sb);
535 2837
        if (cfd->closefunc != NULL)
536 1880
                retval = cfd->closefunc(cfd->priv);
537 2837
        (void)close(cfd->fdi);
538 2837
        if (cfd->fdo != cfd->fdi)
539 941
                (void)close(cfd->fdo);
540 2837
        if (cfd->cli->ident != NULL)
541 1921
                free(cfd->cli->ident);
542 2837
        FREE_OBJ(cfd);
543 2837
        return (retval);
544
}
545
546
void
547 18249
VCLS_AddFunc(struct VCLS *cs, unsigned auth, struct cli_proto *clp)
548
{
549
        struct cli_proto *clp2;
550
        int i;
551
552 18249
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
553 18249
        AN(clp);
554
555 64290
        for (;clp->desc != NULL; clp++) {
556 46041
                clp->auth = auth;
557 46041
                if (!strcmp(clp->desc->request, "*")) {
558 1029
                        cs->wildcard = clp;
559 1029
                } else {
560 45012
                        i = 0;
561 475247
                        VTAILQ_FOREACH(clp2, &cs->funcs, list) {
562 938554
                                i = strcmp(clp->desc->request,
563 469277
                                    clp2->desc->request);
564 469277
                                if (i <= 0)
565 39042
                                        break;
566 430235
                        }
567 45012
                        if (clp2 != NULL && i == 0) {
568 8350
                                VTAILQ_INSERT_BEFORE(clp2, clp, list);
569 8350
                                VTAILQ_REMOVE(&cs->funcs, clp2, list);
570 45012
                        } else if (clp2 != NULL)
571 30692
                                VTAILQ_INSERT_BEFORE(clp2, clp, list);
572
                        else
573 5970
                                VTAILQ_INSERT_TAIL(&cs->funcs, clp, list);
574
                }
575 46041
        }
576 18249
}
577
578
int
579 36130
VCLS_Poll(struct VCLS *cs, const struct cli *cli, int timeout)
580
{
581
        struct VCLS_fd *cfd;
582
        struct pollfd pfd[1];
583
        int i, j, k;
584
        char buf[BUFSIZ];
585
586 36130
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
587 36130
        if (cs->nfd == 0) {
588 0
                errno = 0;
589 0
                return (-1);
590
        }
591 36130
        assert(cs->nfd > 0);
592
593 36130
        i = 0;
594 62680
        VTAILQ_FOREACH(cfd, &cs->fds, list) {
595 62673
                if (cfd->cli != cli)
596 26550
                        continue;
597 36123
                pfd[i].fd = cfd->fdi;
598 36123
                pfd[i].events = POLLIN;
599 36123
                pfd[i].revents = 0;
600 36123
                i++;
601 36123
                break;
602
        }
603 36130
        assert(i == 1);
604 36116
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
605
606 36116
        j = poll(pfd, 1, timeout);
607 36116
        if (j <= 0)
608 0
                return (j);
609 36116
        if (pfd[0].revents & POLLHUP)
610 1854
                k = 1;
611
        else {
612 34262
                i = read(cfd->fdi, buf, sizeof buf);
613 34262
                if (i <= 0)
614 869
                        k = 1;
615
                else
616 33393
                        k = cls_feed(cfd, buf, buf + i);
617
        }
618 36116
        if (k) {
619 2724
                i = cls_close_fd(cs, cfd);
620 2724
                if (i < 0)
621 939
                        k = i;
622 2724
        }
623 36116
        return (k);
624 36116
}
625
626
void
627 943
VCLS_Destroy(struct VCLS **csp)
628
{
629
        struct VCLS *cs;
630
        struct VCLS_fd *cfd, *cfd2;
631
        struct cli_proto *clp;
632
633 943
        TAKE_OBJ_NOTNULL(cs, csp, VCLS_MAGIC);
634 1056
        VTAILQ_FOREACH_SAFE(cfd, &cs->fds, list, cfd2)
635 113
                (void)cls_close_fd(cs, cfd);
636
637 24518
        while (!VTAILQ_EMPTY(&cs->funcs)) {
638 23575
                clp = VTAILQ_FIRST(&cs->funcs);
639 23575
                VTAILQ_REMOVE(&cs->funcs, clp, list);
640
        }
641 943
        FREE_OBJ(cs);
642 943
}
643
644
/**********************************************************************
645
 * Utility functions for implementing CLI commands
646
 */
647
648
static void
649 106301
vcli_outv(struct cli *cli, const char *fmt, va_list ap)
650
{
651
652 106301
        if (VSB_len(cli->sb) < *cli->limit)
653 105143
                (void)VSB_vprintf(cli->sb, fmt, ap);
654 1158
        else if (cli->result == CLIS_OK)
655 5
                cli->result = CLIS_TRUNCATED;
656 106301
}
657
658
/*lint -e{818} cli could be const */
659
void
660 95698
VCLI_Out(struct cli *cli, const char *fmt, ...)
661
{
662
        va_list ap;
663
664 95698
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
665 95698
        AN(fmt);
666
667 95698
        va_start(ap, fmt);
668 95698
        vcli_outv(cli, fmt, ap);
669 95698
        va_end(ap);
670 95698
}
671
672
int v_matchproto_(VTE_format_f)
673 10603
VCLI_VTE_format(void *priv, const char *fmt, ...)
674
{
675
        struct cli *cli;
676
        va_list ap;
677
678 10603
        CAST_OBJ_NOTNULL(cli, priv, CLI_MAGIC);
679 10603
        AN(fmt);
680
681 10603
        va_start(ap, fmt);
682 10603
        vcli_outv(cli, fmt, ap);
683 10603
        va_end(ap);
684
685 10603
        return (0);
686
}
687
688
/*lint -e{818} cli could be const */
689
int
690 276
VCLI_Overflow(struct cli *cli)
691
{
692 276
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
693 276
        if (cli->result == CLIS_TRUNCATED ||
694 276
            VSB_len(cli->sb) >= *cli->limit)
695 0
                return (1);
696 276
        return (0);
697 276
}
698
699
/*lint -e{818} cli could be const */
700
void
701 2319
VCLI_JSON_str(struct cli *cli, const char *s)
702
{
703
704 2319
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
705 2319
        VSB_putc(cli->sb, '"');
706 2319
        VSB_quote(cli->sb, s, -1, VSB_QUOTE_JSON);
707 2319
        VSB_putc(cli->sb, '"');
708 2319
}
709
710
/*lint -e{818} cli could be const */
711
void
712 55
VCLI_JSON_begin(struct cli *cli, unsigned ver, const char * const * av)
713
{
714
        int i;
715
716 55
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
717 55
        VCLI_Out(cli, "[ %u, [", ver);
718 177
        for (i = 1; av[i] != NULL; i++) {
719 122
                VCLI_JSON_str(cli, av[i]);
720 122
                if (av[i + 1] != NULL)
721 67
                        VCLI_Out(cli, ", ");
722 122
        }
723 55
        VCLI_Out(cli, "], %.3f", VTIM_real());
724 55
        VSB_indent(cli->sb, 2);
725 55
}
726
727
void
728 55
VCLI_JSON_end(struct cli *cli)
729
{
730 55
        VSB_indent(cli->sb, -2);
731 55
        VCLI_Out(cli, "\n");
732 55
        VCLI_Out(cli, "]\n");
733 55
}
734
735
/*lint -e{818} cli could be const */
736
void
737 13
VCLI_Quote(struct cli *cli, const char *s)
738
{
739
740 13
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
741 13
        VSB_quote(cli->sb, s, -1, 0);
742 13
}
743
744
void
745 9549
VCLI_SetResult(struct cli *cli, unsigned res)
746
{
747
748 9549
        if (cli != NULL) {
749 9549
                CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
750 9549
                if (cli->result != CLIS_TRUNCATED || res != CLIS_OK)
751 9548
                        cli->result = res;      /*lint !e64 type mismatch */
752 9549
        } else {
753 0
                printf("CLI result = %u\n", res);
754
        }
755 9549
}