Code Coverage: vinyl-cache/lib/libvinyl/vcli_serve.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
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
VCLS_func_close(struct cli *cli, const char *const *av, void *priv)
85 4
{
86
87 4
        (void)av;
88 4
        (void)priv;
89 4
        VCLI_Out(cli, "Closing CLI connection");
90 4
        VCLI_SetResult(cli, CLIS_CLOSE);
91 4
}
92
93
/*--------------------------------------------------------------------*/
94
95
void v_matchproto_(cli_func_t)
96
VCLS_func_ping(struct cli *cli, const char * const *av, void *priv)
97 244
{
98 244
        time_t t;
99
100 244
        (void)av;
101 244
        (void)priv;
102 244
        t = time(NULL);
103 244
        VCLI_Out(cli, "PONG %jd 1.0", (intmax_t)t);
104 244
}
105
106
void v_matchproto_(cli_func_t)
107
VCLS_func_ping_json(struct cli *cli, const char * const *av, void *priv)
108 2
{
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
help_helper(struct cli *cli, struct cli_proto *clp, const char * const *av)
120 290
{
121 290
        AN(clp->desc->syntax);
122 290
        if (av[0] != NULL)
123 4
                VCLI_Out(cli, "%s\n%s\n", clp->desc->syntax, clp->desc->help);
124 286
        else
125 286
                VCLI_Out(cli, "%s\n", clp->desc->syntax);
126 290
}
127
128
void v_matchproto_(cli_func_t)
129
VCLS_func_help(struct cli *cli, const char * const *av, void *priv)
130 21
{
131 21
        struct cli_proto *clp;
132 21
        unsigned filter = 1, d;
133 21
        struct VCLS *cs;
134
135 21
        (void)priv;
136 21
        cs = cli->cls;
137 21
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
138
139 25
        for (av += 2; av[0] != NULL && av[0][0] == '-'; av++) {
140 6
                if (!vstrcmp(av[0], "-a")) {
141 2
                        filter = 3;
142 4
                } else if (!vstrcmp(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 2
                }
149 6
        }
150 509
        VTAILQ_FOREACH(clp, &cs->funcs, list) {
151 509
                if (clp->auth > cli->auth)
152 0
                        continue;
153 509
                if (av[0] != NULL && !vstrcmp(clp->desc->request, av[0])) {
154 4
                        help_helper(cli, clp, av);
155 4
                        return;
156 505
                } else if (av[0] == NULL) {
157 385
                        d = strchr(clp->flags, 'd') != NULL ? 2 : 1;
158 385
                        if (filter & d)
159 286
                                help_helper(cli, clp, av);
160 385
                }
161 509
        }
162 15
        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 15
}
167
168
void v_matchproto_(cli_func_t)
169
VCLS_func_help_json(struct cli *cli, const char * const *av, void *priv)
170 7
{
171 7
        struct cli_proto *clp;
172 7
        struct VCLS *cs;
173
174 7
        (void)priv;
175 7
        cs = cli->cls;
176 7
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
177
178 7
        VCLI_JSON_begin(cli, 2, av);
179 259
        VTAILQ_FOREACH(clp, &cs->funcs, list) {
180 259
                if (clp->auth > cli->auth)
181 0
                        continue;
182 259
                VCLI_Out(cli, ",\n  {\n");
183 259
                VSB_indent(cli->sb, 2);
184 259
                VCLI_Out(cli, "\"request\": ");
185 259
                VCLI_JSON_str(cli, clp->desc->request);
186 259
                VCLI_Out(cli, ",\n");
187 259
                VCLI_Out(cli, "\"syntax\": ");
188 259
                VCLI_JSON_str(cli, clp->desc->syntax);
189 259
                VCLI_Out(cli, ",\n");
190 259
                VCLI_Out(cli, "\"help\": ");
191 259
                VCLI_JSON_str(cli, clp->desc->help);
192 259
                VCLI_Out(cli, ",\n");
193 259
                VCLI_Out(cli, "\"minarg\": %d", clp->desc->minarg);
194 259
                VCLI_Out(cli, ",\n");
195 259
                VCLI_Out(cli, "\"maxarg\": %d", clp->desc->maxarg);
196 259
                VCLI_Out(cli, ",\n");
197 259
                VCLI_Out(cli, "\"flags\": ");
198 259
                VCLI_JSON_str(cli, clp->flags);
199 259
                VCLI_Out(cli, ",\n");
200 259
                VCLI_Out(cli, "\"json\": %s",
201 259
                    clp->jsonfunc == NULL ? "false" : "true");
202 259
                VCLI_Out(cli, "\n");
203 259
                VSB_indent(cli->sb, -2);
204 259
                VCLI_Out(cli, "}");
205 259
        }
206 7
        VCLI_JSON_end(cli);
207 7
}
208
209
/*--------------------------------------------------------------------
210
 * Look for a CLI command to execute
211
 */
212
213
static void
214
cls_dispatch(struct cli *cli, struct VCLS *cs, char * const * av, int ac)
215 22972
{
216 22972
        int json = 0;
217 22972
        struct cli_proto *cp;
218
219 22972
        AN(av);
220 22972
        assert(ac >= 0);
221 22972
        AZ(av[0]);
222 22972
        AN(av[1]);
223
224 407534
        VTAILQ_FOREACH(cp, &cs->funcs, list) {
225 407534
                if (cp->auth > cli->auth)
226 0
                        continue;
227 407534
                if (!vstrcmp(cp->desc->request, av[1]))
228 18569
                        break;
229 407534
        }
230
231 22972
        if (cp == NULL && cs->wildcard && cs->wildcard->auth <= cli->auth)
232 4402
                cp = cs->wildcard;
233
234 22972
        if (cp == NULL) {
235 1
                VCLI_Out(cli, "Unknown request.\nType 'help' for more info.\n");
236 1
                return;
237 1
        }
238
239 22971
        VSB_clear(cli->sb);
240
241 22971
        if (ac > 1 && !vstrcmp(av[2], "-j"))
242 82
                json = 1;
243
244 22971
        if (cp->func == NULL && !json) {
245 0
                VCLI_Out(cli, "Unimplemented\n");
246 0
                VCLI_SetResult(cli, CLIS_UNIMPL);
247 0
                return;
248 0
        }
249 22971
        if (cp->jsonfunc == NULL && json) {
250 1
                VCLI_Out(cli, "JSON unimplemented\n");
251 1
                VCLI_SetResult(cli, CLIS_UNIMPL);
252 1
                return;
253 1
        }
254
255 22970
        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 4
        }
260
261 22966
        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 2
        }
266
267 22964
        cli->result = CLIS_OK;
268 22964
        cli->cls = cs;
269 22964
        if (json)
270 81
                cp->jsonfunc(cli, (const char * const *)av, cp->priv);
271 22883
        else
272 22883
                cp->func(cli, (const char * const *)av, cp->priv);
273 22964
        cli->cls = NULL;
274 22964
}
275
276
/*--------------------------------------------------------------------
277
 * We have collected a full cli line, parse it and execute, if possible.
278
 */
279
280
static int
281
cls_exec(struct VCLS_fd *cfd, char * const *av, int ac)
282 22978
{
283 22978
        struct VCLS *cs;
284 22978
        struct cli *cli;
285 22978
        ssize_t len;
286 22978
        char *s;
287 22978
        unsigned lim;
288 22978
        int retval = 0;
289
290 22978
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
291 22978
        cs = cfd->cls;
292 22978
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
293
294 22978
        cli = cfd->cli;
295 22978
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
296 22978
        AN(cli->cmd);
297
298 22978
        cli->result = CLIS_UNKNOWN;
299 22978
        VSB_clear(cli->sb);
300
301 22978
        if (cs->before != NULL)
302 22978
                cs->before(cli);
303
304 22978
        do {
305 22978
                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 2
                }
310
311 22976
                if (av[1] == NULL) {
312 1
                        VCLI_Out(cli, "Empty CLI command.\n");
313 1
                        VCLI_SetResult(cli, CLIS_SYNTAX);
314 1
                        break;
315 1
                }
316
317 22975
                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 3
                }
322
323 22972
                cls_dispatch(cli, cs, av, ac);
324
325 22972
        } while (0);
326
327 22978
        AZ(VSB_finish(cli->sb));
328
329 22978
        if (cs->after != NULL)
330 22976
                cs->after(cli);
331
332 22978
        s = VSB_data(cli->sb);
333 22978
        len = VSB_len(cli->sb);
334 22978
        lim = *cs->limit;
335 22978
        if (len > lim) {
336 6
                if (cli->result == CLIS_OK)
337 0
                        cli->result = CLIS_TRUNCATED;
338 6
                s[lim - 1] = '\0';
339 6
                assert(vstrlen(s) <= lim);
340 6
        }
341 22978
        if (VCLI_WriteResult(cfd->fdo, cli->result, s) ||
342 22978
            cli->result == CLIS_CLOSE)
343 4
                retval = 1;
344
345
        /*
346
         * In unauthenticated mode we are very intolerant, and close the
347
         * connection at the least provocation.
348
         */
349 22978
        if (cli->auth == 0 && cli->result != CLIS_OK)
350 0
                retval = 1;
351
352 22978
        return (retval);
353 22978
}
354
355
static int
356
cls_feed(struct VCLS_fd *cfd, const char *p, const char *e)
357 36481
{
358 36481
        struct cli *cli;
359 36481
        int i, retval = 0, ac;
360 36481
        char **av, *q;
361
362 36481
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
363 36481
        AN(p);
364 36481
        assert(e > p);
365
366 36481
        cli = cfd->cli;
367 36481
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
368
369 1043995
        for (;p < e; p++) {
370 1007518
                if (cli->cmd == NULL && isspace(*p)) {
371
                        /* Ignore all leading space before cmd */
372 2624
                        continue;
373 2624
                }
374 1004894
                if (cfd->argv == NULL) {
375
376
                        /* Collect first line up to \n or \r */
377 473258
                        if (cli->cmd == NULL) {
378 22980
                                cli->cmd = VSB_new_auto();
379 22980
                                AN(cli->cmd);
380 22980
                        }
381
382
                        /* Until authenticated, limit length hard */
383 473258
                        if (*p != '\n' && *p != '\r' &&
384 473258
                            (cli->auth > 0 || VSB_len(cli->cmd) < 80)) {
385 450279
                                VSB_putc(cli->cmd, *p);
386 450279
                                continue;
387 450279
                        }
388
389 22979
                        AZ(VSB_finish(cli->cmd));
390
391
                        /* Ignore leading '-' */
392 22979
                        q = VSB_data(cli->cmd);
393 22979
                        if (*q == '-')
394 4
                                q++;
395 22979
                        av = VAV_Parse(q, &ac, 0);
396 22979
                        AN(av);
397
398 22979
                        if (cli->auth > 0 &&
399 22979
                            av[0] == NULL &&
400 22979
                            ac >= 3 &&
401 22979
                            !vstrcmp(av[ac - 2], "<<") &&
402 22979
                            *av[ac - 1] != '\0') {
403
                                /* Go to "<< nonce" mode */
404 1594
                                cfd->argv = av;
405 1594
                                cfd->argc = ac;
406 1594
                                cfd->match = av[ac - 1];
407 1594
                                cfd->last_arg = VSB_new_auto();
408 1594
                                AN(cfd->last_arg);
409 21385
                        } else {
410
                                /* Plain command */
411 21385
                                i = cls_exec(cfd, av, ac - 1);
412 21385
                                VAV_Free(av);
413 21385
                                VSB_destroy(&cli->cmd);
414 21385
                                if (i)
415 4
                                        return (i);
416 21385
                        }
417 531636
                } else {
418
                        /* "<< nonce" mode */
419 531636
                        AN(cfd->argv);
420 531636
                        AN(cfd->argc);
421 531636
                        AN(cfd->match);
422 531636
                        AN(cfd->last_arg);
423 531636
                        if (*cfd->match == '\0' && (*p == '\r' || *p == '\n')) {
424 1593
                                AZ(VSB_finish(cfd->last_arg));
425
                                // NB: VAV lib internals trusted
426 1593
                                cfd->match = NULL;
427 1593
                                REPLACE(cfd->argv[cfd->argc - 1], NULL);
428 1593
                                REPLACE(cfd->argv[cfd->argc - 2], NULL);
429 1593
                                cfd->argv[cfd->argc - 2] =
430 1593
                                    VSB_data(cfd->last_arg);
431 1593
                                i = cls_exec(cfd, cfd->argv, cfd->argc - 2);
432 1593
                                cfd->argv[cfd->argc - 2] = NULL;
433 1593
                                VAV_Free(cfd->argv);
434 1593
                                cfd->argv = NULL;
435 1593
                                VSB_destroy(&cfd->last_arg);
436 1593
                                VSB_destroy(&cli->cmd);
437 1593
                                if (i)
438 0
                                        return (i);
439 530043
                        } else if (*p == *cfd->match) {
440 28302
                                cfd->match++;
441 501741
                        } else if (cfd->match != cfd->argv[cfd->argc - 1]) {
442 2853
                                q = cfd->argv[cfd->argc - 1];
443 2853
                                VSB_bcat(cfd->last_arg, q, cfd->match - q);
444 2853
                                cfd->match = q;
445 2853
                                VSB_putc(cfd->last_arg, *p);
446 498888
                        } else {
447 498888
                                VSB_putc(cfd->last_arg, *p);
448 498888
                        }
449 531636
                }
450 1004894
        }
451 36477
        return (retval);
452 36481
}
453
454
struct VCLS *
455
VCLS_New(struct VCLS *model)
456 2147
{
457 2147
        struct VCLS *cs;
458
459 2147
        CHECK_OBJ_ORNULL(model, VCLS_MAGIC);
460
461 2147
        ALLOC_OBJ(cs, VCLS_MAGIC);
462 2147
        AN(cs);
463 2147
        VTAILQ_INIT(&cs->fds);
464 2147
        VTAILQ_INIT(&cs->funcs);
465 2147
        if (model != NULL)
466 1026
                VTAILQ_CONCAT(&cs->funcs, &model->funcs, list);
467 2147
        return (cs);
468 2147
}
469
470
void
471
VCLS_SetLimit(struct VCLS *cs, volatile unsigned *limit)
472 2076
{
473 2076
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
474 2076
        cs->limit = limit;
475 2076
}
476
477
void
478
VCLS_SetHooks(struct VCLS *cs, cls_cbc_f *before, cls_cbc_f *after)
479 2147
{
480
481 2147
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
482 2147
        cs->before = before;
483 2147
        cs->after = after;
484 2147
}
485
486
struct cli *
487
VCLS_AddFd(struct VCLS *cs, int fdi, int fdo, cls_cb_f *closefunc, void *priv)
488 3123
{
489 3123
        struct VCLS_fd *cfd;
490
491 3123
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
492 3123
        assert(fdi >= 0);
493 3123
        assert(fdo >= 0);
494 3123
        ALLOC_OBJ(cfd, VCLS_FD_MAGIC);
495 3123
        AN(cfd);
496 3123
        cfd->cls = cs;
497 3123
        cfd->fdi = fdi;
498 3123
        cfd->fdo = fdo;
499 3123
        cfd->cli = &cfd->clis;
500 3123
        cfd->cli->magic = CLI_MAGIC;
501 3123
        cfd->cli->sb = VSB_new_auto();
502 3123
        AN(cfd->cli->sb);
503 3123
        cfd->cli->limit = cs->limit;
504 3123
        cfd->cli->priv = priv;
505 3123
        cfd->closefunc = closefunc;
506 3123
        cfd->priv = priv;
507 3123
        VTAILQ_INSERT_TAIL(&cs->fds, cfd, list);
508 3123
        cs->nfd++;
509 3123
        return (cfd->cli);
510 3123
}
511
512
static int
513
cls_close_fd(struct VCLS *cs, struct VCLS_fd *cfd)
514 3109
{
515 3109
        int retval = 0;
516
517 3109
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
518 3109
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
519
520 3109
        VTAILQ_REMOVE(&cs->fds, cfd, list);
521 3109
        if (cfd->match != NULL) {
522 1
                cfd->cli->result = CLIS_TRUNCATED;
523 1
                if (cs->after != NULL)
524 1
                        cs->after(cfd->cli);
525 1
                VSB_destroy(&cfd->last_arg);
526 3108
        } else if (cfd->cli->cmd != NULL) {
527 1
                (void)VSB_finish(cfd->cli->cmd);
528 1
                cfd->cli->result = CLIS_TRUNCATED;
529 1
                if (cs->after != NULL)
530 1
                        cs->after(cfd->cli);
531 1
                VSB_destroy(&cfd->cli->cmd);
532 1
        }
533 3109
        cs->nfd--;
534 3109
        VSB_destroy(&cfd->cli->sb);
535 3109
        if (cfd->closefunc != NULL)
536 2052
                retval = cfd->closefunc(cfd->priv);
537 3109
        (void)close(cfd->fdi);
538 3109
        if (cfd->fdo != cfd->fdi)
539 1027
                (void)close(cfd->fdo);
540 3109
        if (cfd->cli->ident != NULL)
541 2094
                free(cfd->cli->ident);
542 3109
        FREE_OBJ(cfd);
543 3109
        return (retval);
544 3109
}
545
546
void
547
VCLS_AddFunc(struct VCLS *cs, unsigned auth, struct cli_proto *clp)
548 19013
{
549 19013
        struct cli_proto *clp2;
550 19013
        int i;
551
552 19013
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
553 19013
        AN(clp);
554
555 68458
        for (;clp->desc != NULL; clp++) {
556 49445
                clp->auth = auth;
557 49445
                if (!vstrcmp(clp->desc->request, "*")) {
558 1121
                        cs->wildcard = clp;
559 48324
                } else {
560 48324
                        i = 0;
561 498010
                        VTAILQ_FOREACH(clp2, &cs->funcs, list) {
562 498010
                                i = vstrcmp(clp->desc->request,
563 498010
                                    clp2->desc->request);
564 498010
                                if (i <= 0)
565 41826
                                        break;
566 498010
                        }
567 48324
                        if (clp2 != NULL && i == 0) {
568 9232
                                VTAILQ_INSERT_BEFORE(clp2, clp, list);
569 9232
                                VTAILQ_REMOVE(&cs->funcs, clp2, list);
570 39092
                        } else if (clp2 != NULL)
571 32594
                                VTAILQ_INSERT_BEFORE(clp2, clp, list);
572 6498
                        else
573 6498
                                VTAILQ_INSERT_TAIL(&cs->funcs, clp, list);
574 48324
                }
575 49445
        }
576 19013
}
577
578
int
579
VCLS_Poll(struct VCLS *cs, const struct cli *cli, int timeout)
580 39532
{
581 39532
        struct VCLS_fd *cfd;
582 39532
        struct pollfd pfd[1];
583 39532
        int i, j, k;
584 39532
        char buf[BUFSIZ];
585
586 39532
        CHECK_OBJ_NOTNULL(cs, VCLS_MAGIC);
587 39532
        if (cs->nfd == 0) {
588 0
                errno = 0;
589 0
                return (-1);
590 0
        }
591 39532
        assert(cs->nfd > 0);
592
593 39532
        i = 0;
594 68105
        VTAILQ_FOREACH(cfd, &cs->fds, list) {
595 68105
                if (cfd->cli != cli)
596 28573
                        continue;
597 39532
                pfd[i].fd = cfd->fdi;
598 39532
                pfd[i].events = POLLIN;
599 39532
                pfd[i].revents = 0;
600 39532
                i++;
601 39532
                break;
602 68105
        }
603 39532
        assert(i == 1);
604 39532
        CHECK_OBJ_NOTNULL(cfd, VCLS_FD_MAGIC);
605
606 39532
        j = poll(pfd, 1, timeout);
607 39532
        if (j <= 0)
608 0
                return (j);
609 39532
        if (pfd[0].revents & POLLHUP)
610 2037
                k = 1;
611 37495
        else {
612 37495
                i = read(cfd->fdi, buf, sizeof buf);
613 37495
                if (i <= 0)
614 1005
                        k = 1;
615 36490
                else
616 36490
                        k = cls_feed(cfd, buf, buf + i);
617 37495
        }
618 39532
        if (k) {
619 3046
                i = cls_close_fd(cs, cfd);
620 3046
                if (i < 0)
621 1025
                        k = i;
622 3046
        }
623 39532
        return (k);
624 39532
}
625
626
void
627
VCLS_Destroy(struct VCLS **csp)
628 1029
{
629 1029
        struct VCLS *cs;
630 1029
        struct VCLS_fd *cfd, *cfd2;
631 1029
        struct cli_proto *clp;
632
633 1029
        TAKE_OBJ_NOTNULL(cs, csp, VCLS_MAGIC);
634 1029
        VTAILQ_FOREACH_SAFE(cfd, &cs->fds, list, cfd2)
635 63
                (void)cls_close_fd(cs, cfd);
636
637 26754
        while (!VTAILQ_EMPTY(&cs->funcs)) {
638 25725
                clp = VTAILQ_FIRST(&cs->funcs);
639 25725
                VTAILQ_REMOVE(&cs->funcs, clp, list);
640 25725
        }
641 1029
        FREE_OBJ(cs);
642 1029
}
643
644
/**********************************************************************
645
 * Utility functions for implementing CLI commands
646
 */
647
648
static void
649
vcli_outv(struct cli *cli, const char *fmt, va_list ap)
650 113112
{
651
652 113112
        if (VSB_len(cli->sb) < *cli->limit)
653 111910
                (void)VSB_vprintf(cli->sb, fmt, ap);
654 1202
        else if (cli->result == CLIS_OK)
655 6
                cli->result = CLIS_TRUNCATED;
656 113112
}
657
658
/*lint -e{818} cli could be const */
659
void
660
VCLI_Out(struct cli *cli, const char *fmt, ...)
661 101739
{
662 101739
        va_list ap;
663
664 101739
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
665 101739
        AN(fmt);
666
667 101739
        va_start(ap, fmt);
668 101739
        vcli_outv(cli, fmt, ap);
669 101739
        va_end(ap);
670 101739
}
671
672
int v_matchproto_(VTE_format_f)
673
VCLI_VTE_format(void *priv, const char *fmt, ...)
674 11373
{
675 11373
        struct cli *cli;
676 11373
        va_list ap;
677
678 11373
        CAST_OBJ_NOTNULL(cli, priv, CLI_MAGIC);
679 11373
        AN(fmt);
680
681 11373
        va_start(ap, fmt);
682 11373
        vcli_outv(cli, fmt, ap);
683 11373
        va_end(ap);
684
685 11373
        return (0);
686 11373
}
687
688
/*lint -e{818} cli could be const */
689
int
690
VCLI_Overflow(struct cli *cli)
691 305
{
692 305
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
693 305
        if (cli->result == CLIS_TRUNCATED ||
694 305
            VSB_len(cli->sb) >= *cli->limit)
695 0
                return (1);
696 305
        return (0);
697 305
}
698
699
/*lint -e{818} cli could be const */
700
void
701
VCLI_JSON_str(struct cli *cli, const char *s)
702 2478
{
703
704 2478
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
705 2478
        VSB_putc(cli->sb, '"');
706 2478
        VSB_quote(cli->sb, s, -1, VSB_QUOTE_JSON);
707 2478
        VSB_putc(cli->sb, '"');
708 2478
}
709
710
/*lint -e{818} cli could be const */
711
void
712
VCLI_JSON_begin(struct cli *cli, unsigned ver, const char * const * av)
713 57
{
714 57
        int i;
715
716 57
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
717 57
        VCLI_Out(cli, "[ %u, [", ver);
718 183
        for (i = 1; av[i] != NULL; i++) {
719 126
                VCLI_JSON_str(cli, av[i]);
720 126
                if (av[i + 1] != NULL)
721 69
                        VCLI_Out(cli, ", ");
722 126
        }
723 57
        VCLI_Out(cli, "], %.3f", VTIM_real());
724 57
        VSB_indent(cli->sb, 2);
725 57
}
726
727
void
728
VCLI_JSON_end(struct cli *cli)
729 57
{
730 57
        VSB_indent(cli->sb, -2);
731 57
        VCLI_Out(cli, "\n");
732 57
        VCLI_Out(cli, "]\n");
733 57
}
734
735
/*lint -e{818} cli could be const */
736
void
737
VCLI_Quote(struct cli *cli, const char *s)
738 13
{
739
740 13
        CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
741 13
        VSB_quote(cli->sb, s, -1, 0);
742 13
}
743
744
void
745
VCLI_SetResult(struct cli *cli, unsigned res)
746 10399
{
747
748 10399
        if (cli != NULL) {
749 10399
                CHECK_OBJ_NOTNULL(cli, CLI_MAGIC);
750 10399
                if (cli->result != CLIS_TRUNCATED || res != CLIS_OK)
751 10398
                        cli->result = res;      /*lint !e64 type mismatch */
752 10399
        } else {
753 0
                printf("CLI result = %u\n", res);
754 0
        }
755 10399
}