vinyl-cache/lib/libvinyl/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 84
VCLS_func_close(struct cli *cli, const char *const *av, void *priv)
85
{
86
87 84
        (void)av;
88 84
        (void)priv;
89 84
        VCLI_Out(cli, "Closing CLI connection");
90 84
        VCLI_SetResult(cli, CLIS_CLOSE);
91 84
}
92
93
/*--------------------------------------------------------------------*/
94
95
void v_matchproto_(cli_func_t)
96 5464
VCLS_func_ping(struct cli *cli, const char * const *av, void *priv)
97
{
98
        time_t t;
99
100 5464
        (void)av;
101 5464
        (void)priv;
102 5464
        t = time(NULL);
103 5464
        VCLI_Out(cli, "PONG %jd 1.0", (intmax_t)t);
104 5464
}
105
106
void v_matchproto_(cli_func_t)
107 42
VCLS_func_ping_json(struct cli *cli, const char * const *av, void *priv)
108
{
109 42
        (void)av;
110 42
        (void)priv;
111 42
        VCLI_JSON_begin(cli, 2, av);
112 42
        VCLI_Out(cli, ", \"PONG\"\n");
113 42
        VCLI_JSON_end(cli);
114 42
}
115
116
/*--------------------------------------------------------------------*/
117
118
static void
119 4893
help_helper(struct cli *cli, struct cli_proto *clp, const char * const *av)
120
{
121 4893
        AN(clp->desc->syntax);
122 4893
        if (av[0] != NULL)
123 63
                VCLI_Out(cli, "%s\n%s\n", clp->desc->syntax, clp->desc->help);
124
        else
125 4830
                VCLI_Out(cli, "%s\n", clp->desc->syntax);
126 4893
}
127
128
void v_matchproto_(cli_func_t)
129 378
VCLS_func_help(struct cli *cli, const char * const *av, void *priv)
130
{
131
        struct cli_proto *clp;
132 378
        unsigned filter = 1, d;
133
        struct VCLS *cs;
134
135 378
        (void)priv;
136 378
        cs = cli->cls;
137 378
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
138
139 462
        for (av += 2; av[0] != NULL && av[0][0] == '-'; av++) {
140 126
                if (!vstrcmp(av[0], "-a")) {
141 42
                        filter = 3;
142 126
                } else if (!vstrcmp(av[0], "-d")) {
143 42
                        filter = 2;
144 42
                } else {
145 42
                        VCLI_Out(cli, "Unknown flag\n");
146 42
                        VCLI_SetResult(cli, CLIS_UNKNOWN);
147 42
                        return;
148
                }
149 84
        }
150 9093
        VTAILQ_FOREACH(clp, &cs->funcs, list) {
151 8820
                if (clp->auth > cli->auth)
152 0
                        continue;
153 8820
                if (av[0] != NULL && !vstrcmp(clp->desc->request, av[0])) {
154 63
                        help_helper(cli, clp, av);
155 63
                        return;
156 8757
                } else if (av[0] == NULL) {
157 6531
                        d = strchr(clp->flags, 'd') != NULL ? 2 : 1;
158 6531
                        if (filter & d)
159 4830
                                help_helper(cli, clp, av);
160 6531
                }
161 8757
        }
162 273
        if (av[0] != NULL) {
163 42
                VCLI_Out(cli, "Unknown request.\nType 'help' for more info.\n");
164 42
                VCLI_SetResult(cli, CLIS_UNKNOWN);
165 42
        }
166 378
}
167
168
void v_matchproto_(cli_func_t)
169 126
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 126
        (void)priv;
175 126
        cs = cli->cls;
176 126
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
177
178 126
        VCLI_JSON_begin(cli, 2, av);
179 4788
        VTAILQ_FOREACH(clp, &cs->funcs, list) {
180 4662
                if (clp->auth > cli->auth)
181 0
                        continue;
182 4662
                VCLI_Out(cli, ",\n  {\n");
183 4662
                VSB_indent(cli->sb, 2);
184 4662
                VCLI_Out(cli, "\"request\": ");
185 4662
                VCLI_JSON_str(cli, clp->desc->request);
186 4662
                VCLI_Out(cli, ",\n");
187 4662
                VCLI_Out(cli, "\"syntax\": ");
188 4662
                VCLI_JSON_str(cli, clp->desc->syntax);
189 4662
                VCLI_Out(cli, ",\n");
190 4662
                VCLI_Out(cli, "\"help\": ");
191 4662
                VCLI_JSON_str(cli, clp->desc->help);
192 4662
                VCLI_Out(cli, ",\n");
193 4662
                VCLI_Out(cli, "\"minarg\": %d", clp->desc->minarg);
194 4662
                VCLI_Out(cli, ",\n");
195 4662
                VCLI_Out(cli, "\"maxarg\": %d", clp->desc->maxarg);
196 4662
                VCLI_Out(cli, ",\n");
197 4662
                VCLI_Out(cli, "\"flags\": ");
198 4662
                VCLI_JSON_str(cli, clp->flags);
199 4662
                VCLI_Out(cli, ",\n");
200 9324
                VCLI_Out(cli, "\"json\": %s",
201 4662
                    clp->jsonfunc == NULL ? "false" : "true");
202 4662
                VCLI_Out(cli, "\n");
203 4662
                VSB_indent(cli->sb, -2);
204 4662
                VCLI_Out(cli, "}");
205 4662
        }
206 126
        VCLI_JSON_end(cli);
207 126
}
208
209
/*--------------------------------------------------------------------
210
 * Look for a CLI command to execute
211
 */
212
213
static void
214 474094
cls_dispatch(struct cli *cli, struct VCLS *cs, char * const * av, int ac)
215
{
216 474094
        int json = 0;
217
        struct cli_proto *cp;
218
219 474094
        AN(av);
220 474094
        assert(ac >= 0);
221 474094
        AZ(av[0]);
222 474094
        AN(av[1]);
223
224 8504923
        VTAILQ_FOREACH(cp, &cs->funcs, list) {
225 8413469
                if (cp->auth > cli->auth)
226 0
                        continue;
227 8413469
                if (!vstrcmp(cp->desc->request, av[1]))
228 382640
                        break;
229 8030829
        }
230
231 474094
        if (cp == NULL && cs->wildcard && cs->wildcard->auth <= cli->auth)
232 91433
                cp = cs->wildcard;
233
234 474094
        if (cp == NULL) {
235 21
                VCLI_Out(cli, "Unknown request.\nType 'help' for more info.\n");
236 21
                return;
237
        }
238
239 474073
        VSB_clear(cli->sb);
240
241 474073
        if (ac > 1 && !vstrcmp(av[2], "-j"))
242 1680
                json = 1;
243
244 474073
        if (cp->func == NULL && !json) {
245 0
                VCLI_Out(cli, "Unimplemented\n");
246 0
                VCLI_SetResult(cli, CLIS_UNIMPL);
247 0
                return;
248
        }
249 474073
        if (cp->jsonfunc == NULL && json) {
250 21
                VCLI_Out(cli, "JSON unimplemented\n");
251 21
                VCLI_SetResult(cli, CLIS_UNIMPL);
252 21
                return;
253
        }
254
255 474052
        if (ac - 1 < cp->desc->minarg + json) {
256 84
                VCLI_Out(cli, "Too few parameters\n");
257 84
                VCLI_SetResult(cli, CLIS_TOOFEW);
258 84
                return;
259
        }
260
261 473968
        if (cp->desc->maxarg >= 0 && ac - 1 > cp->desc->maxarg + json) {
262 42
                VCLI_Out(cli, "Too many parameters\n");
263 42
                VCLI_SetResult(cli, CLIS_TOOMANY);
264 42
                return;
265
        }
266
267 473926
        cli->result = CLIS_OK;
268 473926
        cli->cls = cs;
269 473926
        if (json)
270 1659
                cp->jsonfunc(cli, (const char * const *)av, cp->priv);
271
        else
272 472267
                cp->func(cli, (const char * const *)av, cp->priv);
273 473926
        cli->cls = NULL;
274 474094
}
275
276
/*--------------------------------------------------------------------
277
 * We have collected a full cli line, parse it and execute, if possible.
278
 */
279
280
static int
281 474220
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 474220
        int retval = 0;
289
290 474220
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
291 474220
        cs = cfd->cls;
292 474220
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
293
294 474220
        cli = cfd->cli;
295 474220
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
296 474220
        AN(cli->cmd);
297
298 474220
        cli->result = CLIS_UNKNOWN;
299 474220
        VSB_clear(cli->sb);
300
301 474220
        if (cs->before != NULL)
302 474220
                cs->before(cli);
303
304 474220
        do {
305 474220
                if (av[0] != NULL) {
306 42
                        VCLI_Out(cli, "Syntax Error: %s\n", av[0]);
307 42
                        VCLI_SetResult(cli, CLIS_SYNTAX);
308 42
                        break;
309
                }
310
311 474178
                if (av[1] == NULL) {
312 21
                        VCLI_Out(cli, "Empty CLI command.\n");
313 21
                        VCLI_SetResult(cli, CLIS_SYNTAX);
314 21
                        break;
315
                }
316
317 474157
                if (!islower(av[1][0])) {
318 63
                        VCLI_Out(cli, "All commands are in lower-case.\n");
319 63
                        VCLI_SetResult(cli, CLIS_UNKNOWN);
320 63
                        break;
321
                }
322
323 474094
                cls_dispatch(cli, cs, av, ac);
324
325 474094
        } while (0);
326
327 474220
        AZ(VSB_finish(cli->sb));
328
329 474220
        if (cs->after != NULL)
330 474178
                cs->after(cli);
331
332 474220
        s = VSB_data(cli->sb);
333 474220
        len = VSB_len(cli->sb);
334 474220
        lim = *cs->limit;
335 474220
        if (len > lim) {
336 105
                if (cli->result == CLIS_OK)
337 0
                        cli->result = CLIS_TRUNCATED;
338 105
                s[lim - 1] = '\0';
339 105
                assert(vstrlen(s) <= lim);
340 105
        }
341 474220
        if (VCLI_WriteResult(cfd->fdo, cli->result, s) ||
342 474157
            cli->result == CLIS_CLOSE)
343 84
                retval = 1;
344
345
        /*
346
         * In unauthenticated mode we are very intolerant, and close the
347
         * connection at the least provocation.
348
         */
349 474220
        if (cli->auth == 0 && cli->result != CLIS_OK)
350 0
                retval = 1;
351
352 474220
        return (retval);
353
}
354
355
static int
356 754110
cls_feed(struct VCLS_fd *cfd, const char *p, const char *e)
357
{
358
        struct cli *cli;
359 754110
        int i, retval = 0, ac;
360
        char **av, *q;
361
362 754110
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
363 754110
        AN(p);
364 754110
        assert(e > p);
365
366 754110
        cli = cfd->cli;
367 754110
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
368
369 21533504
        for (;p < e; p++) {
370 20779478
                if (cli->cmd == NULL && isspace(*p)) {
371
                        /* Ignore all leading space before cmd */
372 54577
                        continue;
373
                }
374 20724901
                if (cfd->argv == NULL) {
375
376
                        /* Collect first line up to \n or \r */
377 9750168
                        if (cli->cmd == NULL) {
378 474262
                                cli->cmd = VSB_new_auto();
379 474262
                                AN(cli->cmd);
380 474262
                        }
381
382
                        /* Until authenticated, limit length hard */
383 11278794
                        if (*p != '\n' && *p != '\r' &&
384 9275990
                            (cli->auth > 0 || VSB_len(cli->cmd) < 80)) {
385 9275990
                                VSB_putc(cli->cmd, *p);
386 9275990
                                continue;
387
                        }
388
389 474178
                        AZ(VSB_finish(cli->cmd));
390
391
                        /* Ignore leading '-' */
392 474178
                        q = VSB_data(cli->cmd);
393 474178
                        if (*q == '-')
394 84
                                q++;
395 474178
                        av = VAV_Parse(q, &ac, 0);
396 474178
                        AN(av);
397
398 507378
                        if (cli->auth > 0 &&
399 452087
                            av[0] == NULL &&
400 452045
                            ac >= 3 &&
401 183287
                            !vstrcmp(av[ac - 2], "<<") &&
402 33200
                            *av[ac - 1] != '\0') {
403
                                /* Go to "<< nonce" mode */
404 33200
                                cfd->argv = av;
405 33200
                                cfd->argc = ac;
406 33200
                                cfd->match = av[ac - 1];
407 33200
                                cfd->last_arg = VSB_new_auto();
408 33200
                                AN(cfd->last_arg);
409 33200
                        } else {
410
                                /* Plain command */
411 440978
                                i = cls_exec(cfd, av, ac - 1);
412 440978
                                VAV_Free(av);
413 440978
                                VSB_destroy(&cli->cmd);
414 440978
                                if (i)
415 84
                                        return (i);
416
                        }
417 474094
                } else {
418
                        /* "<< nonce" mode */
419 10974733
                        AN(cfd->argv);
420 10974733
                        AN(cfd->argc);
421 10974733
                        AN(cfd->match);
422 10974733
                        AN(cfd->last_arg);
423 10974733
                        if (*cfd->match == '\0' && (*p == '\r' || *p == '\n')) {
424 33179
                                AZ(VSB_finish(cfd->last_arg));
425
                                // NB: VAV lib internals trusted
426 33179
                                cfd->match = NULL;
427 33179
                                REPLACE(cfd->argv[cfd->argc - 1], NULL);
428 33179
                                REPLACE(cfd->argv[cfd->argc - 2], NULL);
429 33179
                                cfd->argv[cfd->argc - 2] =
430 33179
                                    VSB_data(cfd->last_arg);
431 33179
                                i = cls_exec(cfd, cfd->argv, cfd->argc - 2);
432 33179
                                cfd->argv[cfd->argc - 2] = NULL;
433 33179
                                VAV_Free(cfd->argv);
434 33179
                                cfd->argv = NULL;
435 33179
                                VSB_destroy(&cfd->last_arg);
436 33179
                                VSB_destroy(&cli->cmd);
437 33179
                                if (i)
438 0
                                        return (i);
439 10974733
                        } else if (*p == *cfd->match) {
440 589958
                                cfd->match++;
441 10941554
                        } else if (cfd->match != cfd->argv[cfd->argc - 1]) {
442 59913
                                q = cfd->argv[cfd->argc - 1];
443 59913
                                VSB_bcat(cfd->last_arg, q, cfd->match - q);
444 59913
                                cfd->match = q;
445 59913
                                VSB_putc(cfd->last_arg, *p);
446 59913
                        } else {
447 10291683
                                VSB_putc(cfd->last_arg, *p);
448
                        }
449
                }
450 11448827
        }
451 754026
        return (retval);
452 754110
}
453
454
struct VCLS *
455 44239
VCLS_New(struct VCLS *model)
456
{
457
        struct VCLS *cs;
458
459 44239
        CHECK_OBJ_ORNULL(model, VCLS_MAGIC);
460
461 44239
        ALLOC_OBJ(cs, VCLS_MAGIC);
462 44239
        AN(cs);
463 44239
        VTAILQ_INIT(&cs->fds);
464 44239
        VTAILQ_INIT(&cs->funcs);
465 44239
        if (model != NULL)
466 20888
                VTAILQ_CONCAT(&cs->funcs, &model->funcs, list);
467 44239
        return (cs);
468
}
469
470
void
471 42685
VCLS_SetLimit(struct VCLS *cs, volatile unsigned *limit)
472
{
473 42685
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
474 42685
        cs->limit = limit;
475 42685
}
476
477
void
478 44239
VCLS_SetHooks(struct VCLS *cs, cls_cbc_f *before, cls_cbc_f *after)
479
{
480
481 44239
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
482 44239
        cs->before = before;
483 44239
        cs->after = after;
484 44239
}
485
486
struct cli *
487 64384
VCLS_AddFd(struct VCLS *cs, int fdi, int fdo, cls_cb_f *closefunc, void *priv)
488
{
489
        struct VCLS_fd *cfd;
490
491 64384
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
492 64384
        assert(fdi >= 0);
493 64384
        assert(fdo >= 0);
494 64384
        ALLOC_OBJ(cfd, VCLS_FD_MAGIC);
495 64384
        AN(cfd);
496 64384
        cfd->cls = cs;
497 64384
        cfd->fdi = fdi;
498 64384
        cfd->fdo = fdo;
499 64384
        cfd->cli = &cfd->clis;
500 64384
        cfd->cli->magic = CLI_MAGIC;
501 64384
        cfd->cli->sb = VSB_new_auto();
502 64384
        AN(cfd->cli->sb);
503 64384
        cfd->cli->limit = cs->limit;
504 64384
        cfd->cli->priv = priv;
505 64384
        cfd->closefunc = closefunc;
506 64384
        cfd->priv = priv;
507 64384
        VTAILQ_INSERT_TAIL(&cs->fds, cfd, list);
508 64384
        cs->nfd++;
509 64384
        return (cfd->cli);
510
}
511
512
static int
513 64174
cls_close_fd(struct VCLS *cs, struct VCLS_fd *cfd)
514
{
515 64174
        int retval = 0;
516
517 64174
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
518 64174
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
519
520 64174
        VTAILQ_REMOVE(&cs->fds, cfd, list);
521 64174
        if (cfd->match != NULL) {
522 42
                cfd->cli->result = CLIS_TRUNCATED;
523 42
                if (cs->after != NULL)
524 21
                        cs->after(cfd->cli);
525 42
                VSB_destroy(&cfd->last_arg);
526 64174
        } else if (cfd->cli->cmd != NULL) {
527 42
                (void)VSB_finish(cfd->cli->cmd);
528 42
                cfd->cli->result = CLIS_TRUNCATED;
529 42
                if (cs->after != NULL)
530 21
                        cs->after(cfd->cli);
531 42
                VSB_destroy(&cfd->cli->cmd);
532 42
        }
533 64090
        cs->nfd--;
534 64090
        VSB_destroy(&cfd->cli->sb);
535 64090
        if (cfd->closefunc != NULL)
536 42586
                retval = cfd->closefunc(cfd->priv);
537 64090
        (void)close(cfd->fdi);
538 64090
        if (cfd->fdo != cfd->fdi)
539 21314
                (void)close(cfd->fdo);
540 64090
        if (cfd->cli->ident != NULL)
541 43468
                free(cfd->cli->ident);
542 64090
        FREE_OBJ(cfd);
543 64090
        return (retval);
544
}
545
546
void
547 390960
VCLS_AddFunc(struct VCLS *cs, unsigned auth, struct cli_proto *clp)
548
{
549
        struct cli_proto *clp2;
550
        int i;
551
552 390960
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
553 390960
        AN(clp);
554
555 1409500
        for (;clp->desc != NULL; clp++) {
556 1018540
                clp->auth = auth;
557 1018540
                if (!vstrcmp(clp->desc->request, "*")) {
558 23351
                        cs->wildcard = clp;
559 23351
                } else {
560 995189
                        i = 0;
561 10359250
                        VTAILQ_FOREACH(clp2, &cs->funcs, list) {
562 10224121
                                i = vstrcmp(clp->desc->request,
563
                                    clp2->desc->request);
564 10224121
                                if (i <= 0)
565 860060
                                        break;
566 9364061
                        }
567 995189
                        if (clp2 != NULL && i == 0) {
568 187945
                                VTAILQ_INSERT_BEFORE(clp2, clp, list);
569 187945
                                VTAILQ_REMOVE(&cs->funcs, clp2, list);
570 995189
                        } else if (clp2 != NULL)
571 672115
                                VTAILQ_INSERT_BEFORE(clp2, clp, list);
572
                        else
573 135129
                                VTAILQ_INSERT_TAIL(&cs->funcs, clp, list);
574
                }
575 1018540
        }
576 390960
}
577
578
int
579 817645
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 817645
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
587 817645
        if (cs->nfd == 0) {
588 0
                errno = 0;
589 0
                return (-1);
590
        }
591 817645
        assert(cs->nfd > 0);
592
593 817645
        i = 0;
594 1422205
        VTAILQ_FOREACH(cfd, &cs->fds, list) {
595 1422058
                if (cfd->cli != cli)
596 604560
                        continue;
597 817498
                pfd[i].fd = cfd->fdi;
598 817498
                pfd[i].events = POLLIN;
599 817498
                pfd[i].revents = 0;
600 817498
                i++;
601 817498
                break;
602
        }
603 817645
        assert(i == 1);
604 817351
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
605
606 817351
        j = poll(pfd, 1, timeout);
607 817351
        if (j <= 0)
608 0
                return (j);
609 817351
        if (pfd[0].revents & POLLHUP)
610 41873
                k = 1;
611
        else {
612 775478
                i = read(cfd->fdi, buf, sizeof buf);
613 775478
                if (i <= 0)
614 21305
                        k = 1;
615
                else
616 754173
                        k = cls_feed(cfd, buf, buf + i);
617
        }
618 817351
        if (k) {
619 63220
                i = cls_close_fd(cs, cfd);
620 63220
                if (i < 0)
621 21272
                        k = i;
622 63220
        }
623 817351
        return (k);
624 817351
}
625
626
void
627 21356
VCLS_Destroy(struct VCLS **csp)
628
{
629
        struct VCLS *cs;
630
        struct VCLS_fd *cfd, *cfd2;
631
        struct cli_proto *clp;
632
633 21356
        TAKE_OBJ_NOTNULL(cs, csp, VCLS_MAGIC);
634 22226
        VTAILQ_FOREACH_SAFE(cfd, &cs->fds, list, cfd2)
635 870
                (void)cls_close_fd(cs, cfd);
636
637 555256
        while (!VTAILQ_EMPTY(&cs->funcs)) {
638 533900
                clp = VTAILQ_FIRST(&cs->funcs);
639 533900
                VTAILQ_REMOVE(&cs->funcs, clp, list);
640
        }
641 21356
        FREE_OBJ(cs);
642 21356
}
643
644
/**********************************************************************
645
 * Utility functions for implementing CLI commands
646
 */
647
648
static void
649 2340610
vcli_outv(struct cli *cli, const char *fmt, va_list ap)
650
{
651
652 2340610
        if (VSB_len(cli->sb) < *cli->limit)
653 2316082
                (void)VSB_vprintf(cli->sb, fmt, ap);
654 24528
        else if (cli->result == CLIS_OK)
655 105
                cli->result = CLIS_TRUNCATED;
656 2340610
}
657
658
/*lint -e{818} cli could be const */
659
void
660 2107557
VCLI_Out(struct cli *cli, const char *fmt, ...)
661
{
662
        va_list ap;
663
664 2107557
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
665 2107557
        AN(fmt);
666
667 2107557
        va_start(ap, fmt);
668 2107557
        vcli_outv(cli, fmt, ap);
669 2107557
        va_end(ap);
670 2107557
}
671
672
int v_matchproto_(VTE_format_f)
673 233053
VCLI_VTE_format(void *priv, const char *fmt, ...)
674
{
675
        struct cli *cli;
676
        va_list ap;
677
678 233053
        CAST_OBJ_NOTNULL(cli, priv, CLI_MAGIC);
679 233053
        AN(fmt);
680
681 233053
        va_start(ap, fmt);
682 233053
        vcli_outv(cli, fmt, ap);
683 233053
        va_end(ap);
684
685 233053
        return (0);
686
}
687
688
/*lint -e{818} cli could be const */
689
int
690 6405
VCLI_Overflow(struct cli *cli)
691
{
692 6405
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
693 6405
        if (cli->result == CLIS_TRUNCATED ||
694 6405
            VSB_len(cli->sb) >= *cli->limit)
695 0
                return (1);
696 6405
        return (0);
697 6405
}
698
699
/*lint -e{818} cli could be const */
700
void
701 48615
VCLI_JSON_str(struct cli *cli, const char *s)
702
{
703
704 48615
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
705 48615
        VSB_putc(cli->sb, '"');
706 48615
        VSB_quote(cli->sb, s, -1, VSB_QUOTE_JSON);
707 48615
        VSB_putc(cli->sb, '"');
708 48615
}
709
710
/*lint -e{818} cli could be const */
711
void
712 1155
VCLI_JSON_begin(struct cli *cli, unsigned ver, const char * const * av)
713
{
714
        int i;
715
716 1155
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
717 1155
        VCLI_Out(cli, "[ %u, [", ver);
718 3717
        for (i = 1; av[i] != NULL; i++) {
719 2562
                VCLI_JSON_str(cli, av[i]);
720 2562
                if (av[i + 1] != NULL)
721 1407
                        VCLI_Out(cli, ", ");
722 2562
        }
723 1155
        VCLI_Out(cli, "], %.3f", VTIM_real());
724 1155
        VSB_indent(cli->sb, 2);
725 1155
}
726
727
void
728 1155
VCLI_JSON_end(struct cli *cli)
729
{
730 1155
        VSB_indent(cli->sb, -2);
731 1155
        VCLI_Out(cli, "\n");
732 1155
        VCLI_Out(cli, "]\n");
733 1155
}
734
735
/*lint -e{818} cli could be const */
736
void
737 273
VCLI_Quote(struct cli *cli, const char *s)
738
{
739
740 273
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
741 273
        VSB_quote(cli->sb, s, -1, 0);
742 273
}
743
744
void
745 216082
VCLI_SetResult(struct cli *cli, unsigned res)
746
{
747
748 216082
        if (cli != NULL) {
749 216082
                CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
750 216082
                if (cli->result != CLIS_TRUNCATED || res != CLIS_OK)
751 216061
                        cli->result = res;      /*lint !e64 type mismatch */
752 216082
        } else {
753 0
                printf("CLI result = %u\n", res);
754
        }
755 216082
}