Code Coverage: vinyl-cache/bin/vinyladm/vinyladm.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2026 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Cecilie Fritzvold <cecilihf@linpro.no>
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
31
#include "config.h"
32
33
#include <sys/types.h>
34
#include <sys/socket.h>
35
36
#include <stdio.h>
37
38
#if defined(HAVE_EDIT_READLINE_READLINE_H)
39
#  include <edit/readline/readline.h>
40
#elif defined(HAVE_LIBEDIT)
41
#  include <editline/readline.h>
42
#elif defined (HAVE_READLINE_READLINE_H)
43
#  include <readline/readline.h>
44
#  ifdef HAVE_READLINE_HISTORY_H
45
#    include <readline/history.h>
46
#  else
47
#    error missing history.h - this should have got caught in configure
48
#  endif
49
#else
50
#  error missing readline.h - this should have got caught in configure
51
#endif
52
53
#include <math.h>
54
#include <fcntl.h>
55
#include <poll.h>
56
#include <stdint.h>
57
#include <stdlib.h>
58
#include <string.h>
59
#include <unistd.h>
60
61
#include "vdef.h"
62
63
#include "vqueue.h"
64
65
#include "vapi/vsig.h"
66
#include "vapi/vsm.h"
67
#include "vas.h"
68
#include "vcli.h"
69
#include "vin.h"
70
#include "vjsn.h"
71
#include "vtcp.h"
72
73
#define RL_EXIT(status) \
74 3
        do { \
75 3
                rl_callback_handler_remove(); \
76 3
                exit(status); \
77 3
        } while (0)
78
79
enum pass_mode_e {
80
        pass_script,
81
        pass_interactive,
82
};
83
84
static double timeout = 5;
85
static int p_arg = 0;
86
static int line_sock;
87
88
static void
89
cli_write(int sock, const char *s)
90 313
{
91 313
        int i, l;
92
93 313
        i = vstrlen(s);
94 313
        l = write (sock, s, i);
95 313
        if (i == l)
96 313
                return;
97 0
        perror("Write error CLI socket");
98 0
        RL_EXIT(1);
99 0
}
100
101
/*
102
 * This function establishes a connection to the specified ip and port and
103
 * sends a command to vinyld. If vinyld returns an OK status, the result
104
 * is printed and 0 returned. Else, an error message is printed and 1 is
105
 * returned
106
 */
107
static int
108
cli_sock(const char *T_arg, const char *S_arg)
109 49
{
110 49
        int fd;
111 49
        int sock;
112 49
        unsigned status;
113 49
        char *answer = NULL;
114 49
        char buf[CLI_AUTH_RESPONSE_LEN + 1];
115 49
        const char *err;
116
117 49
        sock = VTCP_open(T_arg, NULL, timeout, &err);
118 49
        if (sock < 0) {
119 1
                fprintf(stderr, "Connection failed (%s): %s\n", T_arg, err);
120 1
                return (-1);
121 1
        }
122
123 48
        (void)VCLI_ReadResult(sock, &status, &answer, timeout);
124 48
        if (status == CLIS_AUTH) {
125 47
                if (S_arg == NULL) {
126 1
                        fprintf(stderr, "Authentication required\n");
127 1
                        free(answer);
128 1
                        closefd(&sock);
129 1
                        return (-1);
130 1
                }
131 46
                fd = open(S_arg, O_RDONLY);
132 46
                if (fd < 0) {
133 1
                        fprintf(stderr, "Cannot open \"%s\": %s\n",
134 1
                            S_arg, strerror(errno));
135 1
                        closefd(&sock);
136 1
                        free(answer);
137 1
                        return (-1);
138 1
                }
139 45
                VCLI_AuthResponse(fd, answer, buf);
140 45
                closefd(&fd);
141 45
                free(answer);
142
143 45
                cli_write(sock, "auth ");
144 45
                cli_write(sock, buf);
145 45
                cli_write(sock, "\n");
146 45
                (void)VCLI_ReadResult(sock, &status, &answer, timeout);
147 45
        }
148 46
        if (status != CLIS_OK && status != CLIS_TRUNCATED) {
149 2
                fprintf(stderr, "Rejected %u\n%s\n", status, answer);
150 2
                closefd(&sock);
151 2
                free(answer);
152 2
                return (-1);
153 2
        }
154 44
        free(answer);
155
156 44
        cli_write(sock, "ping\n");
157 44
        (void)VCLI_ReadResult(sock, &status, &answer, timeout);
158 44
        if (status != CLIS_OK || strstr(answer, "PONG") == NULL) {
159 1
                fprintf(stderr, "No pong received from server\n");
160 1
                closefd(&sock);
161 1
                free(answer);
162 1
                return (-1);
163 1
        }
164 43
        free(answer);
165
166 43
        return (sock);
167 44
}
168
169
static unsigned
170
pass_answer(int fd, enum pass_mode_e mode)
171 51
{
172 51
        unsigned status;
173 51
        char *answer = NULL;
174
175 51
        if (VCLI_ReadResult(fd, &status, &answer, timeout)) {
176 0
                fprintf(stderr, "%s\n", answer);
177 0
                RL_EXIT(2);
178 0
        }
179
180 51
        if (p_arg && answer != NULL) {
181 5
                printf("%-3u %-8zu\n%s", status, vstrlen(answer), answer);
182 46
        } else if (p_arg) {
183 0
                printf("%-3u %-8u\n", status, 0U);
184 46
        } else {
185 46
                if (mode == pass_interactive)
186 8
                        printf("%u\n", status);
187 46
                if (answer != NULL)
188 46
                        printf("%s\n", answer);
189 46
                if (status == CLIS_TRUNCATED)
190 1
                        printf("[response was truncated]\n");
191 46
        }
192 51
        free(answer);
193 51
        (void)fflush(stdout);
194 51
        return (status);
195 51
}
196
197
static void v_noreturn_
198
do_args(int sock, int argc, char * const *argv)
199 38
{
200 38
        int i;
201 38
        unsigned status;
202
203 93
        for (i = 0; i < argc; i++) {
204
                /* XXX: We should really CLI-quote these */
205 55
                if (i > 0)
206 17
                        cli_write(sock, " ");
207 55
                cli_write(sock, argv[i]);
208 55
        }
209 38
        cli_write(sock, "\n");
210 38
        status = pass_answer(sock, pass_script);
211 38
        closefd(&sock);
212 38
        if (status == CLIS_OK || status == CLIS_TRUNCATED)
213 30
                exit(0);
214 8
        if (!p_arg)
215 8
                fprintf(stderr, "Command failed with error code %u\n", status);
216 8
        exit(1);
217 38
}
218
219
/* Callback for readline, doesn't take a private pointer, so we need
220
 * to have a global variable.
221
 */
222
static void v_matchproto_()
223
send_line(char *l)
224 6
{
225 6
        if (l) {
226 6
                cli_write(line_sock, l);
227 6
                cli_write(line_sock, "\n");
228 6
                if (*l)
229 6
                        add_history(l);
230 6
                rl_callback_handler_install("vinyl> ", send_line);
231 6
        } else {
232 0
                RL_EXIT(0);
233 0
        }
234 6
}
235
236
static char *
237
command_generator (const char *text, int state)
238 14
{
239 14
        static struct vjsn *jsn_cmds;
240 14
        static const struct vjsn_val *jv;
241 14
        struct vjsn_val *jv2;
242 14
        unsigned u;
243 14
        char *answer = NULL;
244 14
        const char *err;
245
246 14
        if (!state) {
247 5
                cli_write(line_sock, "help -j\n");
248 5
                u = VCLI_ReadResult(line_sock, NULL, &answer, timeout);
249 5
                if (u) {
250 0
                        free(answer);
251 0
                        return (NULL);
252 0
                }
253 5
                jsn_cmds = vjsn_parse(answer, &err);
254 5
                free(answer);
255 5
                if (err != NULL)
256 0
                        return (NULL);
257 5
                AN(jsn_cmds);
258 5
                AN(jsn_cmds->value);
259 5
                assert (vjsn_is_array(jsn_cmds->value));
260 5
                jv = VTAILQ_FIRST(&jsn_cmds->value->children);
261 5
                assert (vjsn_is_number(jv));
262 5
                jv = VTAILQ_NEXT(jv, list);
263 5
                assert (vjsn_is_array(jv));
264 5
                jv = VTAILQ_NEXT(jv, list);
265 5
                assert (vjsn_is_number(jv));
266 5
                jv = VTAILQ_NEXT(jv, list);
267 5
        }
268 190
        while (jv != NULL) {
269 185
                assert (vjsn_is_object(jv));
270 185
                jv2 = VTAILQ_FIRST(&jv->children);
271 185
                AN(jv2);
272 185
                jv = VTAILQ_NEXT(jv, list);
273 185
                assert (vjsn_is_string(jv2));
274 185
                assert (!vstrcmp(jv2->name, "request"));
275 185
                if (!vstrncmp(text, jv2->value, vstrlen(text)))
276 9
                        return (strdup(jv2->value));
277 185
        }
278 5
        vjsn_delete(&jsn_cmds);
279 5
        return (NULL);
280 14
}
281
282
static char **
283
vinyladm_completion (const char *text, int start, int end)
284 5
{
285 5
        char **matches;
286 5
        (void)end;
287 5
        matches = (char **)NULL;
288 5
        if (start == 0)
289 5
                matches = rl_completion_matches(text, command_generator);
290 5
        return (matches);
291 5
}
292
293
294
/*
295
 * No arguments given, simply pass bytes on stdin/stdout and CLI socket
296
 * Send a "banner" to vinyld, to provoke a welcome message.
297
 */
298
static void v_noreturn_
299
interactive(int sock)
300 2
{
301 2
        struct pollfd fds[2];
302 2
        int i;
303 2
        unsigned status;
304 2
        line_sock = sock;
305 2
        rl_already_prompted = 1;
306 2
        rl_callback_handler_install("vinyl> ", send_line);
307 2
        rl_attempted_completion_function = vinyladm_completion;
308
309 2
        fds[0].fd = sock;
310 2
        fds[0].events = POLLIN;
311 2
        fds[1].fd = 0;
312 2
        fds[1].events = POLLIN;
313
314 2
        cli_write(sock, "banner\n");
315 53
        while (1) {
316 53
                i = poll(fds, 2, -1);
317 53
                if (i == -1 && errno == EINTR) {
318 0
                        continue;
319 0
                }
320 53
                assert(i > 0);
321 53
                if (fds[0].revents & POLLIN) {
322
                        /* Get rid of the prompt, kinda hackish */
323 8
                        printf("\r           \r");
324 8
                        status = pass_answer(fds[0].fd, pass_interactive);
325 8
                        rl_forced_update_display();
326 8
                        if (status == CLIS_CLOSE)
327 2
                                RL_EXIT(0);
328 8
                }
329 51
                if (fds[1].revents & POLLIN) {
330 45
                        rl_callback_read_char();
331 45
                }
332 51
        }
333 2
}
334
335
/*
336
 * No arguments given, simply pass bytes on stdin/stdout and CLI socket
337
 */
338
static void v_noreturn_
339
pass(int sock)
340 3
{
341 3
        struct pollfd fds[2];
342 3
        char buf[1024];
343 3
        int i;
344 3
        ssize_t n;
345 3
        int busy = 0;
346
347 3
        fds[0].fd = sock;
348 3
        fds[0].events = POLLIN;
349 3
        fds[1].fd = 0;
350 3
        fds[1].events = POLLIN;
351 15
        while (1) {
352 13
                i = poll(fds, 2, -1);
353 13
                if (i == -1 && errno == EINTR) {
354 0
                        continue;
355 0
                }
356 13
                assert(i > 0);
357 13
                if (fds[0].revents & POLLIN) {
358 5
                        (void)pass_answer(fds[0].fd, pass_script);
359 5
                        busy = 0;
360 5
                        if (fds[1].fd < 0)
361 0
                                RL_EXIT(0);
362 5
                }
363 13
                if (fds[1].revents & POLLIN || fds[1].revents & POLLHUP) {
364 6
                        n = read(fds[1].fd, buf, sizeof buf - 1);
365 6
                        if (n == 0) {
366 1
                                if (!busy)
367 1
                                        RL_EXIT(0);
368 0
                                fds[1].fd = -1;
369 5
                        } else if (n < 0) {
370 0
                                RL_EXIT(0);
371 5
                        } else {
372 5
                                busy = 1;
373 5
                                buf[n] = '\0';
374 5
                                cli_write(sock, buf);
375 5
                        }
376 6
                }
377 13
        }
378 3
}
379
380
381
static void v_noreturn_
382
usage(int status)
383 8
{
384 8
        fprintf(stderr,
385 8
            "Usage: vinyladm [-h] [-n workdir] [-p] [-S secretfile] "
386 8
            "[-T [address]:port] [-t timeout] [command [...]]\n");
387 8
        fprintf(stderr,
388 8
            "       vinyladm [-n name] -x workdir\n");
389 8
        fprintf(stderr, "\t-n is mutually exclusive with -S and -T\n");
390 8
        exit(status);
391 8
}
392
393
static int
394
n_arg_sock(const char *n_arg, const char *t_arg)
395 45
{
396 45
        char *T_arg, *T_start;
397 45
        char *S_arg;
398 45
        struct vsm *vsm;
399 45
        char *p;
400 45
        int sock;
401
402 45
        vsm = VSM_New();
403 45
        AN(vsm);
404 45
        if (VSM_Arg(vsm, 'n', n_arg) < 0 ||
405 45
            VSM_Arg(vsm, 't', t_arg) < 0 ||
406 45
            VSM_Attach(vsm, STDERR_FILENO) < 0) {
407 3
                fprintf(stderr, "%s\n", VSM_Error(vsm));
408 3
                VSM_Destroy(&vsm);
409 3
                return (-1);
410 3
        }
411
412 42
        T_start = T_arg = VSM_Dup(vsm, "Arg", "-T");
413 42
        S_arg = VSM_Dup(vsm, "Arg", "-S");
414 42
        VSM_Destroy(&vsm);
415
416 42
        if (T_arg == NULL) {
417 0
                fprintf(stderr, "No -T in shared memory\n");
418 0
                return (-1);
419 0
        }
420
421 42
        sock = -1;
422 42
        while (*T_arg) {
423 42
                p = strchr(T_arg, '\n');
424 42
                AN(p);
425 42
                *p = '\0';
426 42
                sock = cli_sock(T_arg, S_arg);
427 42
                if (sock >= 0)
428 42
                        break;
429 0
                T_arg = p + 1;
430 0
        }
431 42
        free(T_start);
432 42
        free(S_arg);
433 42
        return (sock);
434 42
}
435
436
static int
437
t_arg_timeout(const char *t_arg)
438 3
{
439 3
        char *p = NULL;
440
441 3
        AN(t_arg);
442 3
        timeout = strtod(t_arg, &p);
443 3
        if ((p != NULL && *p != '\0') ||
444 3
            !isfinite(timeout) || timeout < 0) {
445 0
                fprintf(stderr, "-t: Invalid argument: %s", t_arg);
446 0
                return (-1);
447 0
        }
448 3
        return (1);
449 3
}
450
451 133
#define OPTARG "hn:pS:T:t:x:"
452
453
int
454
main(int argc, char * const *argv)
455 64
{
456 64
        char *wd;
457 64
        const char *T_arg = NULL;
458 64
        const char *S_arg = NULL;
459 64
        const char *n_arg = NULL;
460 64
        const char *t_arg = NULL;
461 64
        int opt, sock;
462
463 64
        if (argc == 2 && !vstrcmp(argv[1], "--optstring")) {
464 1
                printf(OPTARG "\n");
465 1
                exit(0);
466 1
        }
467
        /*
468
         * By default linux::getopt(3) mangles the argv order, such that
469
         *      vinyladm -n bla param.set foo -bar
470
         * gets interpreted as
471
         *      vinyladm -n bla -bar param.set foo
472
         * The '+' stops that from happening
473
         * See #1496
474
         */
475 132
        while ((opt = getopt(argc, argv, "+" OPTARG)) != -1) {
476 78
                switch (opt) {
477 1
                case 'h':
478
                        /* Usage help */
479 1
                        usage(0);
480 45
                case 'n':
481 45
                        n_arg = optarg;
482 45
                        break;
483 3
                case 'p':
484 3
                        p_arg = 1;
485 3
                        break;
486 7
                case 'S':
487 7
                        S_arg = optarg;
488 7
                        break;
489 8
                case 'T':
490 8
                        T_arg = optarg;
491 8
                        break;
492 6
                case 't':
493 6
                        t_arg = optarg;
494 6
                        break;
495 5
                case 'x':
496 5
                        AN(optarg);
497 5
                        if (vstrcmp(optarg, "workdir")) {
498 1
                                fprintf(stderr, "Invalid -x argument\n");
499 1
                                usage(1);
500 1
                        }
501 4
                        if (argc != optind) {
502 1
                                fprintf(stderr, "-x workdir must be the last argument\n");
503 1
                                usage(1);
504 1
                        }
505 3
                        wd = VIN_n_Arg(n_arg);
506 3
                        AN(wd);
507 3
                        printf("%s\n", wd);
508 3
                        free(wd);
509 3
                        exit(0);
510 3
                default:
511 3
                        usage(1);
512 78
                }
513 78
        }
514
515 54
        argc -= optind;
516 54
        argv += optind;
517
518 54
        if (T_arg != NULL) {
519 8
                if (n_arg != NULL)
520 1
                        usage(1);
521 7
                sock = cli_sock(T_arg, S_arg);
522 46
        } else {
523 46
                if (S_arg != NULL)
524 1
                        usage(1);
525 45
                sock = n_arg_sock(n_arg, t_arg);
526 45
        }
527 52
        if (sock < 0)
528 9
                exit(2);
529
530 43
        if (t_arg != NULL && t_arg_timeout(t_arg) < 0)
531 0
                exit(2);
532
533 43
        if (argc > 0) {
534 38
                VSIG_Arm_int();
535 38
                VSIG_Arm_term();
536 38
                do_args(sock, argc, argv);
537 38
                NEEDLESS(exit(0));
538 38
        }
539
540 5
        if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) && !p_arg)
541 2
                interactive(sock);
542 3
        else
543 3
                pass(sock);
544 0
        NEEDLESS(exit(0));
545 5
}