Code Coverage: vinyl-cache/bin/vinyltest/vtest2/src/vtc_haproxy.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2008-2018 Varnish Software AS
2
 * All rights reserved.
3
 *
4
 * Author: Frédéric Lécaille <flecaille@haproxy.com>
5
 *
6
 * SPDX-License-Identifier: BSD-2-Clause
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include <inttypes.h>
33
#include <poll.h>
34
#include <stdio.h>
35
#include <stdlib.h>
36
#include <string.h>
37
#include <sys/stat.h> /* for MUSL (mode_t) */
38
#include <sys/socket.h>
39
#include <sys/un.h>
40
#include <unistd.h>
41
42
#include "vtc.h"
43
44
#include "vfil.h"
45
#include "vpf.h"
46
#include "vre.h"
47
#include "vtcp.h"
48
#include "vsa.h"
49
#include "vtim.h"
50
#include "vudp.h"
51
52 11
#define HAPROXY_PROGRAM_ENV_VAR "HAPROXY_PROGRAM"
53 11
#define HAPROXY_ARGS_ENV_VAR    "HAPROXY_ARGS"
54
#define HAPROXY_OPT_WORKER      "-W"
55
#define HAPROXY_OPT_SD_WORKER   "-Ws"
56
#define HAPROXY_OPT_MCLI        "-S"
57
#define HAPROXY_OPT_DAEMON      "-D"
58 1
#define HAPROXY_SIGNAL          SIGINT
59 1
#define HAPROXY_EXPECT_EXIT     (128 + HAPROXY_SIGNAL)
60
61
struct envar {
62
        VTAILQ_ENTRY(envar) list;
63
        char *name;
64
        char *value;
65
};
66
67
struct haproxy {
68
        unsigned                magic;
69
#define HAPROXY_MAGIC           0x8a45cf75
70
        char                    *name;
71
        struct vtclog           *vl;
72
        VTAILQ_ENTRY(haproxy)   list;
73
74
        const char              *filename;
75
        struct vsb              *args;
76
        int                     opt_worker;
77
        int                     opt_mcli;
78
        int                     opt_daemon;
79
        int                     opt_check_mode;
80
        char                    *pid_fn;
81
        pid_t                   pid;
82
        pid_t                   ppid;
83
        int                     fds[4];
84
        char                    *cfg_fn;
85
        struct vsb              *cfg_vsb;
86
87
        pthread_t               tp;
88
        int                     expect_exit;
89
        int                     expect_signal;
90
        int                     its_dead_jim;
91
92
        /* sd_notify unix socket */
93
        struct sockaddr_un      sd_uds;
94
        int                     sd_sock;
95
96
        /* UNIX socket CLI. */
97
        char                    *cli_fn;
98
        /* TCP socket CLI. */
99
        struct haproxy_cli *cli;
100
101
        /* master CLI */
102
        struct haproxy_cli *mcli;
103
104
        char                    *workdir;
105
        struct vsb              *msgs;
106
        char                    closed_sock[256]; /* Closed TCP socket */
107
        VTAILQ_HEAD(,envar) envars;
108
};
109
110
static VTAILQ_HEAD(, haproxy)   haproxies =
111
    VTAILQ_HEAD_INITIALIZER(haproxies);
112
113
struct haproxy_cli {
114
        unsigned                magic;
115
#define HAPROXY_CLI_MAGIC       0xb09a4ed8
116
        struct vtclog           *vl;
117
        char                    running;
118
119
        char                    *spec;
120
121
        int                     sock;
122
        char                    connect[256];
123
124
        pthread_t               tp;
125
        size_t                  txbuf_sz;
126
        char                    *txbuf;
127
        size_t                  rxbuf_sz;
128
        char                    *rxbuf;
129
130
        vtim_dur                timeout;
131
};
132
133
static void haproxy_write_conf(struct haproxy *h);
134
135
static void
136
haproxy_add_envar(struct haproxy *h,
137
                  const char *name, const char *value)
138 19
{
139 19
        struct envar *e;
140
141 19
        e = malloc(sizeof *e);
142 19
        AN(e);
143 19
        e->name = strdup(name);
144 19
        e->value = strdup(value);
145 19
        AN(e->name);
146 19
        AN(e->value);
147 19
        VTAILQ_INSERT_TAIL(&h->envars, e, list);
148 19
}
149
150
static void
151
haproxy_delete_envars(struct haproxy *h)
152 11
{
153 11
        struct envar *e, *e2;
154 19
        VTAILQ_FOREACH_SAFE(e, &h->envars, list, e2) {
155 19
                VTAILQ_REMOVE(&h->envars, e, list);
156 19
                free(e->name);
157 19
                free(e->value);
158 19
                free(e);
159 19
        }
160 11
}
161
162
static void
163
haproxy_build_env(const struct haproxy *h)
164 11
{
165 11
        struct envar *e;
166
167 19
        VTAILQ_FOREACH(e, &h->envars, list) {
168 19
                if (setenv(e->name, e->value, 0) == -1)
169 0
                        vtc_fatal(h->vl, "setenv() failed: %s (%d)",
170 0
                                  strerror(errno), errno);
171 19
        }
172 11
}
173
174
/**********************************************************************
175
 * Socket connect (same as client_tcp_connect()).
176
 */
177
178
static int
179
haproxy_cli_tcp_connect(struct vtclog *vl, const char *addr, vtim_dur tmo,
180
    const char **errp)
181 2
{
182 2
        int fd;
183 2
        char mabuf[VTCP_ADDRBUFSIZE], mpbuf[VTCP_PORTBUFSIZE];
184
185 2
        AN(addr);
186 2
        AN(errp);
187 2
        fd = VTCP_open(addr, NULL, tmo, errp);
188 2
        if (fd < 0)
189 0
                return (fd);
190 2
        VTCP_myname(fd, mabuf, sizeof mabuf, mpbuf, sizeof mpbuf);
191 2
        vtc_log(vl, 3,
192 2
            "CLI connected fd %d from %s %s to %s", fd, mabuf, mpbuf, addr);
193 2
        return (fd);
194 2
}
195
196
/*
197
 * SECTION: haproxy.cli haproxy CLI Specification
198
 * SECTION: haproxy.cli.send
199
 * send STRING
200
 *         Push STRING on the CLI connection. STRING will be terminated by an
201
 *         end of line character (\n).
202
 */
203
static void v_matchproto_(cmd_f)
204
cmd_haproxy_cli_send(CMD_ARGS)
205 2
{
206 2
        struct vsb *vsb;
207 2
        struct haproxy_cli *hc;
208 2
        int j;
209
210 2
        (void)vl;
211 2
        CAST_OBJ_NOTNULL(hc, priv, HAPROXY_CLI_MAGIC);
212 2
        AZ(vstrcmp(av[0], "send"));
213 2
        ARGN(vl, av, 1);
214 2
        ARGZ(vl, av, 2);
215
216 2
        vsb = VSB_new_auto();
217 2
        AN(vsb);
218 2
        AZ(VSB_cat(vsb, av[1]));
219 2
        AZ(VSB_cat(vsb, "\n"));
220 2
        AZ(VSB_finish(vsb));
221 2
        if (hc->sock == -1) {
222 0
                int fd;
223 0
                const char *err;
224 0
                struct vsb *vsb_connect;
225
226 0
                vsb_connect = macro_expand(hc->vl, hc->connect);
227 0
                AN(vsb_connect);
228 0
                fd = haproxy_cli_tcp_connect(hc->vl,
229 0
                    VSB_data(vsb_connect), 10., &err);
230 0
                if (fd < 0)
231 0
                        vtc_fatal(hc->vl,
232 0
                            "CLI failed to open %s: %s", VSB_data(vsb), err);
233 0
                VSB_destroy(&vsb_connect);
234 0
                hc->sock = fd;
235 0
        }
236 2
        vtc_dump(hc->vl, 4, "CLI send", VSB_data(vsb), -1);
237
238 2
        if (VSB_tofile(vsb, hc->sock))
239 0
                vtc_fatal(hc->vl,
240 0
                    "CLI fd %d send error %s", hc->sock, strerror(errno));
241
242
        /* a CLI command must be followed by a SHUT_WR if we want HAProxy to
243
         * close after the response */
244 2
        j = shutdown(hc->sock, SHUT_WR);
245 2
        vtc_log(hc->vl, 3, "CLI shutting fd %d", hc->sock);
246 2
        if (!VTCP_Check(j))
247 0
                vtc_fatal(hc->vl, "Shutdown failed: %s", strerror(errno));
248
249 2
        VSB_destroy(&vsb);
250 2
}
251
252 4
#define HAPROXY_CLI_RECV_LEN (1 << 14)
253
static void
254
haproxy_cli_recv(struct haproxy_cli *hc)
255 2
{
256 2
        ssize_t ret;
257 2
        size_t rdz, left, off;
258
259 2
        rdz = ret = off = 0;
260
        /* We want to null terminate this buffer. */
261 2
        left = hc->rxbuf_sz - 1;
262 4
        while (!vtc_error && left > 0) {
263 4
                VTCP_set_read_timeout(hc->sock, hc->timeout);
264
265 4
                ret = recv(hc->sock, hc->rxbuf + off, HAPROXY_CLI_RECV_LEN, 0);
266 4
                if (ret < 0) {
267 0
                        if (errno == EINTR || errno == EAGAIN)
268 0
                                continue;
269
270 0
                        vtc_fatal(hc->vl,
271 0
                            "CLI fd %d recv() failed (%s)",
272 0
                            hc->sock, strerror(errno));
273 0
                }
274
                /* Connection closed. */
275 4
                if (ret == 0) {
276 2
                        if (rdz > 0 && hc->rxbuf[rdz - 1] != '\n')
277 0
                                vtc_fatal(hc->vl,
278 0
                                    "CLI rx timeout (fd: %d %.3fs ret: %zd)",
279 0
                                    hc->sock, hc->timeout, ret);
280
281 2
                        vtc_log(hc->vl, 4, "CLI connection normally closed");
282 2
                        vtc_log(hc->vl, 3, "CLI closing fd %d", hc->sock);
283 2
                        VTCP_close(&hc->sock);
284 2
                        break;
285 2
                }
286
287 2
                rdz += ret;
288 2
                left -= ret;
289 2
                off  += ret;
290 2
        }
291 2
        hc->rxbuf[rdz] = '\0';
292 2
        vtc_dump(hc->vl, 4, "CLI recv", hc->rxbuf, rdz);
293 2
}
294
295
/*
296
 * SECTION: haproxy.cli.expect
297
 * expect OP STRING
298
 *         Regex match the CLI reception buffer with STRING
299
 *         if OP is ~ or, on the contrary, if OP is !~ check that there is
300
 *         no regex match.
301
 */
302
static void v_matchproto_(cmd_f)
303
cmd_haproxy_cli_expect(CMD_ARGS)
304 2
{
305 2
        struct haproxy_cli *hc;
306 2
        struct vsb vsb[1];
307 2
        vre_t *vre;
308 2
        int error, erroroffset, i, ret;
309 2
        char *cmp, *spec, errbuf[VRE_ERROR_LEN];
310
311 2
        (void)vl;
312 2
        CAST_OBJ_NOTNULL(hc, priv, HAPROXY_CLI_MAGIC);
313 2
        AZ(vstrcmp(av[0], "expect"));
314 2
        av++;
315
316 2
        cmp = av[0];
317 2
        spec = av[1];
318 2
        AN(cmp);
319 2
        AN(spec);
320 2
        ARGZ(vl, av, 2);
321
322 2
        assert(!vstrcmp(cmp, "~") || !vstrcmp(cmp, "!~"));
323
324 2
        haproxy_cli_recv(hc);
325
326 2
        vre = VRE_compile(spec, 0, &error, &erroroffset, 1);
327 2
        if (vre == NULL) {
328 0
                AN(VSB_init(vsb, errbuf, sizeof errbuf));
329 0
                AZ(VRE_error(vsb, error));
330 0
                AZ(VSB_finish(vsb));
331 0
                VSB_fini(vsb);
332 0
                vtc_fatal(hc->vl, "CLI regexp error: '%s' (@%d) (%s)",
333 0
                    errbuf, erroroffset, spec);
334 0
        }
335
336 2
        i = VRE_match(vre, hc->rxbuf, 0, 0, NULL);
337
338 2
        VRE_free(&vre);
339
340 2
        ret = (i >= 0 && *cmp == '~') || (i < 0 && *cmp == '!');
341 2
        if (!ret)
342 0
                vtc_fatal(hc->vl, "CLI expect failed %s \"%s\"", cmp, spec);
343 2
        else
344 2
                vtc_log(hc->vl, 4, "CLI expect match %s \"%s\"", cmp, spec);
345 2
}
346
347
static const struct cmds haproxy_cli_cmds[] = {
348
#define CMD_HAPROXY_CLI(n) { CMDS_MAGIC, #n, cmd_haproxy_cli_##n },
349
        CMD_HAPROXY_CLI(send)
350
        CMD_HAPROXY_CLI(expect)
351
#undef CMD_HAPROXY_CLI
352
        { CMDS_MAGIC, NULL, NULL }
353
};
354
355
/**********************************************************************
356
 * HAProxy CLI client thread
357
 */
358
359
static void *
360
haproxy_cli_thread(void *priv)
361 2
{
362 2
        struct haproxy_cli *hc;
363 2
        struct vsb *vsb;
364 2
        int fd;
365 2
        const char *err;
366
367 2
        CAST_OBJ_NOTNULL(hc, priv, HAPROXY_CLI_MAGIC);
368 2
        AN(*hc->connect);
369
370 2
        vsb = macro_expand(hc->vl, hc->connect);
371 2
        AN(vsb);
372
373 2
        fd = haproxy_cli_tcp_connect(hc->vl, VSB_data(vsb), 10., &err);
374 2
        if (fd < 0)
375 0
                vtc_fatal(hc->vl,
376 0
                    "CLI failed to open %s: %s", VSB_data(vsb), err);
377 2
        VTCP_blocking(fd);
378 2
        hc->sock = fd;
379 2
        parse_string(hc->vl, hc, hc->spec);
380 2
        vtc_log(hc->vl, 2, "CLI ending");
381 2
        VSB_destroy(&vsb);
382 2
        return (NULL);
383 2
}
384
385
/**********************************************************************
386
 * Wait for the CLI client thread to stop
387
 */
388
389
static void
390
haproxy_cli_wait(struct haproxy_cli *hc)
391 2
{
392 2
        void *res;
393
394 2
        CHECK_OBJ_NOTNULL(hc, HAPROXY_CLI_MAGIC);
395 2
        vtc_log(hc->vl, 2, "CLI waiting");
396 2
        PTOK(pthread_join(hc->tp, &res));
397 2
        if (res != NULL)
398 0
                vtc_fatal(hc->vl, "CLI returned \"%s\"", (char *)res);
399 2
        REPLACE(hc->spec, NULL);
400 2
        hc->tp = 0;
401 2
        hc->running = 0;
402 2
}
403
404
/**********************************************************************
405
 * Start the CLI client thread
406
 */
407
408
static void
409
haproxy_cli_start(struct haproxy_cli *hc)
410 2
{
411 2
        CHECK_OBJ_NOTNULL(hc, HAPROXY_CLI_MAGIC);
412 2
        vtc_log(hc->vl, 2, "CLI starting");
413 2
        PTOK(pthread_create(&hc->tp, NULL, haproxy_cli_thread, hc));
414 2
        hc->running = 1;
415
416 2
}
417
418
/**********************************************************************
419
 * Run the CLI client thread
420
 */
421
422
static void
423
haproxy_cli_run(struct haproxy_cli *hc)
424 2
{
425 2
        haproxy_cli_start(hc);
426 2
        haproxy_cli_wait(hc);
427 2
}
428
429
/**********************************************************************
430
 * Wait for the pidfile
431
 */
432
433
static void
434
haproxy_wait_pidfile(struct haproxy *h)
435 4
{
436 4
        char buf_err[1024] = {0};
437 4
        int usleep_time = 1000;
438 4
        double t0;
439 4
        pid_t pid;
440
441 4
        vtc_log(h->vl, 3, "wait-pid-file");
442 237
        for (t0 = VTIM_mono(); VTIM_mono() - t0 < 3;) {
443 237
                if (vtc_error)
444 0
                        return;
445
446 237
                if (VPF_Read(h->pid_fn, &pid) != 0) {
447 233
                        bprintf(buf_err,
448 233
                            "Could not read PID file '%s'", h->pid_fn);
449 233
                        usleep(usleep_time);
450 233
                        continue;
451 233
                }
452
453 4
                if (!h->opt_daemon && pid != h->pid) {
454 0
                        bprintf(buf_err,
455 0
                            "PID file has different PID (%ld != %lld)",
456 0
                            (long)pid, (long long)h->pid);
457 0
                        usleep(usleep_time);
458 0
                        continue;
459 0
                }
460
461 4
                if (kill(pid, 0) < 0) {
462 0
                        bprintf(buf_err,
463 0
                            "Could not find PID %ld process", (long)pid);
464 0
                        usleep(usleep_time);
465 0
                        continue;
466 0
                }
467
468 4
                h->pid = pid;
469
470 4
                vtc_log(h->vl, 2, "haproxy PID %ld successfully started",
471 4
                    (long)pid);
472 4
                return;
473 4
        }
474 0
        vtc_fatal(h->vl, "haproxy %s PID file check failed:\n\t%s\n",
475 0
                  h->name, buf_err);
476 4
}
477
478
/**********************************************************************
479
 * Bind the sd_notify socket
480
 */
481
static void
482
haproxy_bind_sdnotify(struct haproxy *h)
483 1
{
484 1
        char sd_path[PATH_MAX];
485 1
        int sd;
486 1
        int ret;
487 1
        const char *err = NULL;
488 1
        struct sockaddr_un *uds = &h->sd_uds;
489 1
        socklen_t sl = sizeof(*uds);
490
491 1
        bprintf(sd_path, "%s/sd_notify.sock", h->workdir);
492 1
        assert(sd_path[0] == '/');
493
494 1
        if (vstrlen(sd_path) + 1 > sizeof(uds->sun_path)) {
495 0
                vtc_fatal(h->vl, "Path %s too long for a Unix domain socket", sd_path);
496 0
        }
497 1
        memset(uds->sun_path, 0, sizeof(uds->sun_path));
498 1
        bprintf(uds->sun_path, "%s", sd_path);
499 1
        uds->sun_family = PF_UNIX;
500
501 1
        sd = socket(AF_UNIX, SOCK_DGRAM, 0);
502 1
        if (sd < 0) {
503 0
                err = "socket(2)";
504 0
                goto error;
505 0
        }
506
507 1
        if (unlink(uds->sun_path) != 0 && errno != ENOENT) {
508 0
                err = "unlink(2)";
509 0
                closefd(&sd);
510 0
                goto error;
511 0
        }
512
513 1
        if (bind(sd, (const void*)uds, sl) != 0) {
514 0
                err = "bind(2)";
515 0
                closefd(&sd);
516 0
                goto error;
517 0
        }
518
519 1
        h->sd_sock = sd;
520
521 1
        assert(h->sd_sock > 0);
522 1
        vtc_log(h->vl, 4, "sd_notify %s", sd_path);
523 1
        ret = setenv("NOTIFY_SOCKET", sd_path, 1);
524 1
        assert(ret == 0);
525
526 1
error:
527 1
        if (err != NULL)
528 0
                vtc_fatal(h->vl, "Create sd_notify socket failed: %s", err);
529 1
}
530
531
/**********************************************************************
532
 * Wait for the "READY" from sd_notify
533
 */
534
static void
535
haproxy_wait_sdnotify_ready(struct haproxy *h)
536 1
{
537 1
        struct pollfd fd[1];
538 1
        char buf[BUFSIZ];
539 1
        int i, r;
540 1
        char *ready = NULL;
541
542 1
        vtc_log(h->vl, 3, "wait-sdnotify-ready");
543
544
        /* First try to do an accept on h->sd_sock */
545 1
        memset(fd, 0, sizeof(fd));
546 1
        fd[0].fd = h->sd_sock;
547 1
        fd[0].events = POLLIN;
548
549 1
        i = poll(fd, 1, (int)(vtc_maxdur * 1000 / 3));
550 1
        vtc_log(h->vl, 4, "sd_notify recv poll %d 0x%x ", i, fd[0].revents);
551 1
        if (i == 0)
552 0
                vtc_fatal(h->vl, "FAIL timeout waiting for sd_notify recv");
553 1
        if (!(fd[0].revents & POLLIN))
554 0
                vtc_fatal(h->vl, "FAIL sd_notify recv wait failure");
555
556 1
        r = recv(h->sd_sock, buf, sizeof(buf) - 1, 0);
557 1
        if (r > 0) {
558 1
                buf[r] = '\0';
559 1
                ready = strstr(buf, "READY=1");
560 1
        }
561
562 1
        if (!ready)
563 0
                vtc_fatal(h->vl, "FAIL sd_notify recv READY failure");
564 1
        else
565 1
                vtc_log(h->vl, 3, "sd_notify READY=1");
566 1
}
567
/**********************************************************************
568
 * Allocate and initialize a CLI client
569
 */
570
571
static struct haproxy_cli *
572
haproxy_cli_new(struct haproxy *h)
573 11
{
574 11
        struct haproxy_cli *hc;
575
576 11
        ALLOC_OBJ(hc, HAPROXY_CLI_MAGIC);
577 11
        AN(hc);
578
579 11
        hc->vl = h->vl;
580 11
        vtc_log_set_cmd(hc->vl, haproxy_cli_cmds);
581 11
        hc->sock = -1;
582 11
        bprintf(hc->connect, "${%s_cli_sock}", h->name);
583
584 11
        hc->txbuf_sz = hc->rxbuf_sz = 2048 * 1024;
585 11
        hc->txbuf = malloc(hc->txbuf_sz);
586 11
        AN(hc->txbuf);
587 11
        hc->rxbuf = malloc(hc->rxbuf_sz);
588 11
        AN(hc->rxbuf);
589
590 11
        return (hc);
591 11
}
592
593
/* creates a master CLI client (-mcli) */
594
static struct haproxy_cli *
595
haproxy_mcli_new(struct haproxy *h)
596 11
{
597 11
        struct haproxy_cli *hc;
598
599 11
        ALLOC_OBJ(hc, HAPROXY_CLI_MAGIC);
600 11
        AN(hc);
601
602 11
        hc->vl = h->vl;
603 11
        vtc_log_set_cmd(hc->vl, haproxy_cli_cmds);
604 11
        hc->sock = -1;
605 11
        bprintf(hc->connect, "${%s_mcli_sock}", h->name);
606
607 11
        hc->txbuf_sz = hc->rxbuf_sz = 2048 * 1024;
608 11
        hc->txbuf = malloc(hc->txbuf_sz);
609 11
        AN(hc->txbuf);
610 11
        hc->rxbuf = malloc(hc->rxbuf_sz);
611 11
        AN(hc->rxbuf);
612
613 11
        return (hc);
614 11
}
615
616
/* Bind an address/port for the master CLI (-mcli) */
617
static int
618
haproxy_create_mcli(struct haproxy *h)
619 1
{
620 1
        int sock;
621 1
        const char *err;
622 1
        char buf[128], addr[128], port[128];
623 1
        char vsabuf[vsa_suckaddr_len];
624 1
        const struct suckaddr *sua;
625
626 1
        sock = VTCP_listen_on(default_listen_addr, NULL, 100, &err);
627 1
        if (err != NULL)
628 0
                vtc_fatal(h->vl,
629 0
                          "Create listen socket failed: %s", err);
630 1
        assert(sock > 0);
631 1
        sua = VSA_getsockname(sock, vsabuf, sizeof vsabuf);
632 1
        AN(sua);
633
634 1
        VTCP_name(sua, addr, sizeof addr, port, sizeof port);
635 1
        bprintf(buf, "%s_mcli", h->name);
636 1
        if (VSA_Get_Proto(sua) == AF_INET)
637 1
                macro_def(h->vl, buf, "sock", "%s:%s", addr, port);
638 0
        else
639 0
                macro_def(h->vl, buf, "sock", "[%s]:%s", addr, port);
640 1
        macro_def(h->vl, buf, "addr", "%s", addr);
641 1
        macro_def(h->vl, buf, "port", "%s", port);
642
643 1
        return (sock);
644 1
}
645
646
static void
647
haproxy_cli_delete(struct haproxy_cli *hc)
648 22
{
649 22
        CHECK_OBJ_NOTNULL(hc, HAPROXY_CLI_MAGIC);
650 22
        REPLACE(hc->spec, NULL);
651 22
        REPLACE(hc->txbuf, NULL);
652 22
        REPLACE(hc->rxbuf, NULL);
653 22
        FREE_OBJ(hc);
654 22
}
655
656
/**********************************************************************
657
 * Allocate and initialize a haproxy
658
 */
659
660
static struct haproxy *
661
haproxy_new(const char *name)
662 11
{
663 11
        struct haproxy *h;
664 11
        struct vsb *vsb;
665 11
        char buf[PATH_MAX];
666 11
        int closed_sock;
667 11
        char addr[128], port[128];
668 11
        const char *err;
669 11
        const char *env_args;
670 11
        char vsabuf[vsa_suckaddr_len];
671 11
        const struct suckaddr *sua;
672
673 11
        ALLOC_OBJ(h, HAPROXY_MAGIC);
674 11
        AN(h);
675 11
        REPLACE(h->name, name);
676
677 11
        h->args = VSB_new_auto();
678 11
        env_args = getenv(HAPROXY_ARGS_ENV_VAR);
679 11
        if (env_args) {
680 0
                VSB_cat(h->args, env_args);
681 0
                VSB_cat(h->args, " ");
682 0
        }
683
684 11
        h->vl = vtc_logopen("%s", name);
685 11
        vtc_log_set_cmd(h->vl, haproxy_cli_cmds);
686 11
        AN(h->vl);
687
688 11
        h->filename = getenv(HAPROXY_PROGRAM_ENV_VAR);
689 11
        if (h->filename == NULL)
690 11
                h->filename = "haproxy";
691
692 11
        bprintf(buf, "${tmpdir}/%s", name);
693 11
        vsb = macro_expand(h->vl, buf);
694 11
        AN(vsb);
695 11
        h->workdir = strdup(VSB_data(vsb));
696 11
        AN(h->workdir);
697 11
        VSB_destroy(&vsb);
698
699 11
        bprintf(buf, "%s/stats.sock", h->workdir);
700 11
        h->cli_fn = strdup(buf);
701 11
        AN(h->cli_fn);
702
703 11
        bprintf(buf, "%s/cfg", h->workdir);
704 11
        h->cfg_fn = strdup(buf);
705 11
        AN(h->cfg_fn);
706
707
        /* Create a new TCP socket to reserve an IP:port and close it asap.
708
         * May be useful to simulate an unreachable server.
709
         */
710 11
        bprintf(h->closed_sock, "%s_closed", h->name);
711 11
        closed_sock = VTCP_listen_on("127.0.0.1:0", NULL, 100, &err);
712 11
        if (err != NULL)
713 0
                vtc_fatal(h->vl,
714 0
                        "Create listen socket failed: %s", err);
715 11
        assert(closed_sock > 0);
716 11
        sua = VSA_getsockname(closed_sock, vsabuf, sizeof vsabuf);
717 11
        AN(sua);
718 11
        VTCP_name(sua, addr, sizeof addr, port, sizeof port);
719 11
        if (VSA_Get_Proto(sua) == AF_INET)
720 11
                macro_def(h->vl, h->closed_sock, "sock", "%s:%s", addr, port);
721 0
        else
722 0
                macro_def(h->vl, h->closed_sock, "sock", "[%s]:%s", addr, port);
723 11
        macro_def(h->vl, h->closed_sock, "addr", "%s", addr);
724 11
        macro_def(h->vl, h->closed_sock, "port", "%s", port);
725 11
        VTCP_close(&closed_sock);
726
727 11
        h->cli = haproxy_cli_new(h);
728 11
        AN(h->cli);
729
730 11
        h->mcli = haproxy_mcli_new(h);
731 11
        AN(h->mcli);
732
733 11
        bprintf(buf, "rm -rf \"%s\" ; mkdir -p \"%s\"", h->workdir, h->workdir);
734 11
        AZ(system(buf));
735
736 11
        h->sd_sock = -1;
737
738 11
        VTAILQ_INIT(&h->envars);
739 11
        VTAILQ_INSERT_TAIL(&haproxies, h, list);
740
741 11
        return (h);
742 11
}
743
744
/**********************************************************************
745
 * Delete a haproxy instance
746
 */
747
748
static void
749
haproxy_delete(struct haproxy *h)
750 11
{
751 11
        char buf[PATH_MAX];
752
753 11
        CHECK_OBJ_NOTNULL(h, HAPROXY_MAGIC);
754 11
        vtc_logclose(h->vl);
755
756 11
        if (!leave_temp) {
757 11
                bprintf(buf, "rm -rf \"%s\"", h->workdir);
758 11
                AZ(system(buf));
759 11
        }
760
761 11
        if (h->sd_sock >= 0)
762 1
                closefd(&h->sd_sock);
763
764 11
        free(h->name);
765 11
        free(h->workdir);
766 11
        free(h->cli_fn);
767 11
        free(h->cfg_fn);
768 11
        free(h->pid_fn);
769 11
        VSB_destroy(&h->args);
770 11
        haproxy_cli_delete(h->cli);
771 11
        haproxy_cli_delete(h->mcli);
772
773
        /* XXX: MEMLEAK (?) */
774 11
        FREE_OBJ(h);
775 11
}
776
777
/**********************************************************************
778
 * HAProxy listener
779
 */
780
781
static void *
782
haproxy_thread(void *priv)
783 11
{
784 11
        struct haproxy *h;
785
786 11
        CAST_OBJ_NOTNULL(h, priv, HAPROXY_MAGIC);
787 11
        (void)vtc_record(h->vl, h->fds[0], h->msgs);
788 11
        h->its_dead_jim = 1;
789 11
        return (NULL);
790 11
}
791
792
793
/**********************************************************************
794
 * Start a HAProxy instance.
795
 */
796
797
static void
798
haproxy_start(struct haproxy *h)
799 11
{
800 11
        char buf[PATH_MAX];
801 11
        struct vsb *vsb;
802
803 11
        vtc_log(h->vl, 2, "%s", __func__);
804
805 11
        AZ(VSB_finish(h->args));
806 11
        vtc_log(h->vl, 4, "opt_worker %d opt_daemon %d opt_check_mode %d opt_mcli %d",
807 11
            h->opt_worker, h->opt_daemon, h->opt_check_mode, h->opt_mcli);
808
809 11
        vsb = VSB_new_auto();
810 11
        AN(vsb);
811
812 11
        VSB_printf(vsb, "exec \"%s\"", h->filename);
813 11
        if (h->opt_check_mode)
814 2
                VSB_cat(vsb, " -c");
815 9
        else if (h->opt_daemon)
816 3
                VSB_cat(vsb, " -D");
817 6
        else
818 6
                VSB_cat(vsb, " -d");
819
820 11
        if (h->opt_worker) {
821 1
                if (h->opt_worker == 2) { /* sd_notify mode */
822 1
                        VSB_cat(vsb, " -Ws");
823 1
                        haproxy_bind_sdnotify(h);
824 1
                } else {
825 0
                        VSB_cat(vsb, " -W");
826 0
                }
827 1
                if (h->opt_mcli) {
828 1
                        int sock;
829 1
                        sock = haproxy_create_mcli(h);
830 1
                        VSB_printf(vsb, " -S \"fd@%d\"", sock);
831 1
                }
832 1
        }
833
834 11
        VSB_printf(vsb, " %s", VSB_data(h->args));
835
836 11
        VSB_printf(vsb, " -f \"%s\" ", h->cfg_fn);
837
838 11
        if (h->opt_worker || h->opt_daemon) {
839 4
                bprintf(buf, "%s/pid", h->workdir);
840 4
                h->pid_fn = strdup(buf);
841 4
                AN(h->pid_fn);
842 4
                VSB_printf(vsb, " -p \"%s\"", h->pid_fn);
843 4
        }
844
845 11
        AZ(VSB_finish(vsb));
846 11
        vtc_dump(h->vl, 4, "argv", VSB_data(vsb), -1);
847
848 11
        if (h->opt_worker && !h->opt_daemon) {
849
                /*
850
                 * HAProxy master process must exit with status 128 + <signum>
851
                 * if signaled by <signum> signal.
852
                 */
853 1
                h->expect_exit = HAPROXY_EXPECT_EXIT;
854 1
        }
855
856 11
        haproxy_write_conf(h);
857
858 11
        AZ(pipe(&h->fds[0]));
859 11
        vtc_log(h->vl, 4, "XXX %d @%d", h->fds[1], __LINE__);
860 11
        AZ(pipe(&h->fds[2]));
861 11
        h->pid = h->ppid = fork();
862 11
        assert(h->pid >= 0);
863 11
        if (h->pid == 0) {
864 11
                haproxy_build_env(h);
865 11
                haproxy_delete_envars(h);
866 11
                AZ(chdir(h->name));
867 11
                AZ(dup2(h->fds[0], 0));
868 11
                assert(dup2(h->fds[3], 1) == 1);
869 11
                assert(dup2(1, 2) == 2);
870 11
                closefd(&h->fds[0]);
871 11
                closefd(&h->fds[1]);
872 11
                closefd(&h->fds[2]);
873 11
                closefd(&h->fds[3]);
874 11
                AZ(execl("/bin/sh", "/bin/sh", "-c", VSB_data(vsb), (char*)0));
875 11
                exit(1);
876 11
        }
877 0
        VSB_destroy(&vsb);
878
879 0
        vtc_log(h->vl, 3, "PID: %ld", (long)h->pid);
880 0
        macro_def(h->vl, h->name, "pid", "%ld", (long)h->pid);
881 0
        macro_def(h->vl, h->name, "name", "%s", h->workdir);
882
883 0
        closefd(&h->fds[0]);
884 0
        closefd(&h->fds[3]);
885 0
        h->fds[0] = h->fds[2];
886 0
        h->fds[2] = h->fds[3] = -1;
887
888 0
        PTOK(pthread_create(&h->tp, NULL, haproxy_thread, h));
889
890 11
        if (h->pid_fn != NULL)
891 4
                haproxy_wait_pidfile(h);
892
893 11
        if (h->opt_worker == 2) /* sd_notify mode */
894 1
                haproxy_wait_sdnotify_ready(h);
895 11
}
896
897
898
/**********************************************************************
899
 * Wait for a HAProxy instance.
900
 */
901
902
static void
903
haproxy_wait(struct haproxy *h)
904 11
{
905 11
        void *p;
906 11
        int i, n, sig;
907
908 11
        vtc_log(h->vl, 2, "Wait");
909
910 11
        if (h->pid < 0)
911 0
                haproxy_start(h);
912
913 11
        if (h->cli->spec)
914 0
                haproxy_cli_run(h->cli);
915
916 11
        if (h->mcli->spec)
917 0
                haproxy_cli_run(h->mcli);
918
919 11
        closefd(&h->fds[1]);
920
921 11
        sig = SIGINT;
922 11
        n = 0;
923 11
        vtc_log(h->vl, 2, "Stop HAproxy pid=%ld", (long)h->pid);
924 77
        while (h->opt_daemon || (!h->opt_check_mode && !h->its_dead_jim)) {
925 69
                assert(h->pid > 0);
926 69
                if (n == 0) {
927 12
                        i = kill(h->pid, sig);
928 12
                        if (i == 0)
929 9
                                h->expect_signal = -sig;
930 12
                        if (i && errno == ESRCH)
931 3
                                break;
932 9
                        vtc_log(h->vl, 4,
933 9
                            "Kill(%d)=%d: %s", sig, i, strerror(errno));
934 9
                }
935 66
                VTIM_sleep(0.1);
936 66
                if (++n == 20) {
937 3
                        switch (sig) {
938 3
                        case SIGINT:    sig = SIGTERM ; break;
939 0
                        case SIGTERM:   sig = SIGKILL ; break;
940 0
                        default:        break;
941 3
                        }
942 3
                        n = 0;
943 3
                }
944 66
        }
945
946 11
        PTOK(pthread_join(h->tp, &p));
947 11
        AZ(p);
948 11
        closefd(&h->fds[0]);
949 11
        if (!h->opt_daemon) {
950 8
                vtc_wait4(h->vl, h->ppid, h->expect_exit, h->expect_signal, 0);
951 8
                h->ppid = -1;
952 8
        }
953 11
        h->pid = -1;
954 11
}
955
956 30
#define HAPROXY_FD_ADDR_FAM_PREFIX        "fd@${"
957 19
#define HAPROXY_FD_ADDR_FAM_PREFIX_LEN    vstrlen(HAPROXY_FD_ADDR_FAM_PREFIX)
958
959
#define HAPROXY_QUIC_SOCK_TYPE            "quic"
960
#define HAPROXY_QUIC_SOCK_TYPE_PREFIX     HAPROXY_QUIC_SOCK_TYPE "+"
961 19
#define HAPROXY_QUIC_SOCK_TYPE_PREFIX_LEN vstrlen(HAPROXY_QUIC_SOCK_TYPE_PREFIX)
962
963 11
#define HAPROXY_VTC_SOCK_TYPE_ENV_VAR     "VTC_SOCK_TYPE"
964
#define HAPROXY_VTC_SOCK_TYPE_PREFIX      "${" HAPROXY_VTC_SOCK_TYPE_ENV_VAR "}+"
965 19
#define HAPROXY_VTC_SOCK_TYPE_PREFIX_LEN  vstrlen(HAPROXY_VTC_SOCK_TYPE_PREFIX)
966
967
static int
968
haproxy_build_backends(struct haproxy *h, const char *vsb_data)
969 11
{
970 11
        char *s, *p, *q;
971 11
        char *sock_type = getenv(HAPROXY_VTC_SOCK_TYPE_ENV_VAR);
972
973 11
        s = strdup(vsb_data);
974 11
        if (!s)
975 0
                return (-1);
976
977 11
        if (sock_type)
978 0
                vtc_log(h->vl, 4, "%s value: '%s'",
979 0
                        HAPROXY_VTC_SOCK_TYPE_ENV_VAR, sock_type);
980 11
        p = s;
981 30
        while (1) {
982 30
                int sock;
983 30
                char buf[128], addr[128], port[128];
984 30
                const char *err;
985 30
                char vsabuf[vsa_suckaddr_len];
986 30
                const struct suckaddr *sua;
987 30
                int quic_sock;
988
989 30
                p = strstr(p, HAPROXY_FD_ADDR_FAM_PREFIX);
990 30
                if (!p)
991 11
                        break;
992
993
                /* The socket to be used is a QUIC one if the fd addresses
994
                 * are prefixed by the haproxy sock type "quic" as follows:
995
                 *    bind "quic+fd@${...}"
996
                 *    bind "${VTC_SOCK_TYPE}+fd@${...}"
997
                 * with "quic" as VTC_SOCK_TYPE environment variable value.
998
                 */
999 19
                quic_sock =
1000 19
                        (p - s >= (ssize_t)HAPROXY_QUIC_SOCK_TYPE_PREFIX_LEN &&
1001 19
                         !vmemcmp(p - HAPROXY_QUIC_SOCK_TYPE_PREFIX_LEN, HAPROXY_QUIC_SOCK_TYPE_PREFIX, HAPROXY_QUIC_SOCK_TYPE_PREFIX_LEN)) ||
1002 19
                        (p - s >= (ssize_t)HAPROXY_VTC_SOCK_TYPE_PREFIX_LEN &&
1003 19
                         !vmemcmp(p - HAPROXY_VTC_SOCK_TYPE_PREFIX_LEN, HAPROXY_VTC_SOCK_TYPE_PREFIX, HAPROXY_VTC_SOCK_TYPE_PREFIX_LEN) &&
1004 19
                         sock_type != NULL &&
1005 19
                         !vstrcmp(sock_type, HAPROXY_QUIC_SOCK_TYPE));
1006
1007 19
                q = p += HAPROXY_FD_ADDR_FAM_PREFIX_LEN;
1008 80
                while (*q && *q != '}')
1009 61
                        q++;
1010 19
                if (*q != '}')
1011 0
                        break;
1012
1013 19
                *q++ = '\0';
1014 19
                if (quic_sock) {
1015 0
                        sock = VUDP_bind_on("127.0.0.1:0", "0", &err);
1016 0
                }
1017 19
                else {
1018 19
                        sock = VTCP_listen_on("127.0.0.1:0", NULL, 100, &err);
1019 19
                }
1020 19
                if (err != NULL)
1021 0
                        vtc_fatal(h->vl,
1022 0
                            "Create listen socket failed: %s", err);
1023 19
                assert(sock > 0);
1024 19
                sua = VSA_getsockname(sock, vsabuf, sizeof vsabuf);
1025 19
                AN(sua);
1026
1027 19
                VTCP_name(sua, addr, sizeof addr, port, sizeof port);
1028 19
                bprintf(buf, "%s_%s", h->name, p);
1029 19
                if (VSA_Get_Proto(sua) == AF_INET)
1030 19
                        macro_def(h->vl, buf, "sock", "%s:%s", addr, port);
1031 0
                else
1032 0
                        macro_def(h->vl, buf, "sock", "[%s]:%s", addr, port);
1033 19
                macro_def(h->vl, buf, "addr", "%s", addr);
1034 19
                macro_def(h->vl, buf, "port", "%s", port);
1035
1036 19
                bprintf(buf, "%d", sock);
1037 19
                vtc_log(h->vl, 4, "setenv(%s, %s) for %s socket", p, buf,
1038 19
                        quic_sock ? "QUIC" : "TCP");
1039 19
                haproxy_add_envar(h, p, buf);
1040 19
                p = q;
1041 19
        }
1042 11
        free(s);
1043 11
        return (0);
1044 11
}
1045
1046
static void
1047
haproxy_check_conf(struct haproxy *h, const char *expect)
1048 2
{
1049
1050 2
        h->msgs = VSB_new_auto();
1051 2
        AN(h->msgs);
1052 2
        h->opt_check_mode = 1;
1053 2
        haproxy_start(h);
1054 2
        haproxy_wait(h);
1055 2
        AZ(VSB_finish(h->msgs));
1056 2
        if (strstr(VSB_data(h->msgs), expect) == NULL)
1057 0
                vtc_fatal(h->vl, "Did not find expected string '%s'", expect);
1058 2
        vtc_log(h->vl, 2, "Found expected '%s'", expect);
1059 2
        VSB_destroy(&h->msgs);
1060 2
}
1061
1062
/**********************************************************************
1063
 * Write a configuration for <h> HAProxy instance.
1064
 */
1065
1066
static void
1067
haproxy_store_conf(struct haproxy *h, const char *cfg, int auto_be)
1068 11
{
1069 11
        struct vsb *vsb, *vsb2;
1070
1071 11
        vsb = VSB_new_auto();
1072 11
        AN(vsb);
1073
1074 11
        vsb2 = VSB_new_auto();
1075 11
        AN(vsb2);
1076
1077 11
        VSB_printf(vsb, "    global\n\tstats socket \"%s\" "
1078 11
                   "level admin mode 600\n", h->cli_fn);
1079 11
        VSB_cat(vsb, "    stats socket \"fd@${cli}\" level admin\n");
1080 11
        AZ(VSB_cat(vsb, cfg));
1081
1082 11
        if (auto_be)
1083 1
                cmd_server_gen_haproxy_conf(vsb);
1084
1085 11
        AZ(VSB_finish(vsb));
1086
1087 11
        AZ(haproxy_build_backends(h, VSB_data(vsb)));
1088
1089 11
        h->cfg_vsb = macro_expand(h->vl, VSB_data(vsb));
1090 11
        AN(h->cfg_vsb);
1091
1092 11
        VSB_destroy(&vsb2);
1093 11
        VSB_destroy(&vsb);
1094 11
}
1095
1096
static void
1097
haproxy_write_conf(struct haproxy *h)
1098 11
{
1099 11
        struct vsb *vsb;
1100
1101 11
        vsb = macro_expand(h->vl, VSB_data(h->cfg_vsb));
1102 11
        AN(vsb);
1103 11
        assert(VSB_len(vsb) >= 0);
1104
1105 11
        vtc_dump(h->vl, 4, "conf", VSB_data(vsb), VSB_len(vsb));
1106 11
        if (VFIL_writefile(h->workdir, h->cfg_fn,
1107 11
            VSB_data(vsb), VSB_len(vsb)) != 0)
1108 0
                vtc_fatal(h->vl,
1109 0
                    "failed to write haproxy configuration file: %s (%d)",
1110 0
                    strerror(errno), errno);
1111
1112 11
        VSB_destroy(&vsb);
1113 11
}
1114
1115
/* SECTION: haproxy haproxy
1116
 *
1117
 * Define and interact with haproxy instances.
1118
 *
1119
 * To define a haproxy server, you'll use this syntax::
1120
 *
1121
 *      haproxy hNAME -conf-OK CONFIG
1122
 *      haproxy hNAME -conf-BAD ERROR CONFIG
1123
 *      haproxy hNAME [-D] [-W] [-arg STRING] [-conf[+vcl] STRING]
1124
 *
1125
 * The first ``haproxy hNAME`` invocation will start the haproxy master
1126
 * process in the background, waiting for the ``-start`` switch to actually
1127
 * start the child.
1128
 *
1129
 * Arguments:
1130
 *
1131
 * hNAME
1132
 *         Identify the HAProxy server with a string, it must starts with 'h'.
1133
 *
1134
 * \-conf-OK CONFIG
1135
 *         Run haproxy in '-c' mode to check config is OK
1136
 *         stdout/stderr should contain 'Configuration file is valid'
1137
 *         The exit code should be 0.
1138
 *
1139
 * \-conf-BAD ERROR CONFIG
1140
 *         Run haproxy in '-c' mode to check config is BAD.
1141
 *         "ERROR" should be part of the diagnostics on stdout/stderr.
1142
 *         The exit code should be 1.
1143
 *
1144
 * \-D
1145
 *         Run HAproxy in daemon mode.  If not given '-d' mode used.
1146
 *
1147
 * \-W
1148
 *         Enable HAproxy in Worker mode.
1149
 *
1150
 * \-S
1151
 *         Enable HAproxy Master CLI in Worker mode
1152
 *
1153
 * \-arg STRING
1154
 *         Pass an argument to haproxy, for example "-h simple_list".
1155
 *
1156
 * \-cli STRING
1157
 *         Specify the spec to be run by the command line interface (CLI).
1158
 *
1159
 * \-mcli STRING
1160
 *         Specify the spec to be run by the command line interface (CLI)
1161
 *         of the Master process.
1162
 *
1163
 * \-conf STRING
1164
 *         Specify the configuration to be loaded by this HAProxy instance.
1165
 *
1166
 * \-conf+backend STRING
1167
 *         Specify the configuration to be loaded by this HAProxy instance,
1168
 *         all server instances will be automatically appended
1169
 *
1170
 * \-start
1171
 *         Start this HAProxy instance.
1172
 *
1173
 * \-wait
1174
 *         Stop this HAProxy instance.
1175
 *
1176
 * \-expectexit NUMBER
1177
 *         Expect haproxy to exit(3) with this value
1178
 *
1179
 */
1180
1181
void
1182
cmd_haproxy(CMD_ARGS)
1183 1136
{
1184 1136
        struct haproxy *h, *h2;
1185
1186 1136
        (void)priv;
1187
1188 1136
        if (av == NULL) {
1189
                /* Reset and free */
1190 1122
                VTAILQ_FOREACH_SAFE(h, &haproxies, list, h2) {
1191 11
                        vtc_log(h->vl, 2,
1192 11
                            "Reset and free %s haproxy %ld",
1193 11
                            h->name, (long)h->pid);
1194 11
                        if (h->pid >= 0)
1195 6
                                haproxy_wait(h);
1196 11
                        VTAILQ_REMOVE(&haproxies, h, list);
1197 11
                        haproxy_delete(h);
1198 11
                }
1199 1122
                return;
1200 1122
        }
1201
1202 14
        AZ(vstrcmp(av[0], "haproxy"));
1203 14
        av++;
1204
1205 14
        VTC_CHECK_NAME(vl, av[0], "haproxy", 'h');
1206 14
        VTAILQ_FOREACH(h, &haproxies, list)
1207 5
                if (!vstrcmp(h->name, av[0]))
1208 3
                        break;
1209 14
        if (h == NULL)
1210 11
                h = haproxy_new(av[0]);
1211 14
        av++;
1212
1213 44
        for (; *av != NULL; av++) {
1214 30
                if (vtc_error)
1215 0
                        break;
1216
1217 30
                if (!vstrcmp(*av, "-conf-OK")) {
1218 1
                        ARGN(vl, av, 1);
1219 1
                        haproxy_store_conf(h, av[1], 0);
1220 1
                        h->expect_exit = 0;
1221 1
                        haproxy_check_conf(h, "");
1222 1
                        av++;
1223 1
                        continue;
1224 1
                }
1225 29
                if (!vstrcmp(*av, "-conf-BAD")) {
1226 1
                        ARGN(vl, av, 1);
1227 1
                        ARGN(vl, av, 2);
1228 1
                        haproxy_store_conf(h, av[2], 0);
1229 1
                        h->expect_exit = 1;
1230 1
                        haproxy_check_conf(h, av[1]);
1231 1
                        av += 2;
1232 1
                        continue;
1233 1
                }
1234
1235 28
                if (!vstrcmp(*av, HAPROXY_OPT_DAEMON)) {
1236 3
                        h->opt_daemon = 1;
1237 3
                        continue;
1238 3
                }
1239 25
                if (!vstrcmp(*av, HAPROXY_OPT_WORKER)) {
1240 0
                        h->opt_worker = 1;
1241 0
                        continue;
1242 0
                }
1243 25
                if (!vstrcmp(*av, HAPROXY_OPT_SD_WORKER)) {
1244 1
                        h->opt_worker = 2;
1245 1
                        continue;
1246 1
                }
1247 24
                if (!vstrcmp(*av, HAPROXY_OPT_MCLI)) {
1248 1
                        h->opt_mcli = 1;
1249 1
                        continue;
1250 1
                }
1251 23
                if (!vstrcmp(*av, "-arg")) {
1252 0
                        ARGN(vl, av, 1);
1253 0
                        AZ(h->pid);
1254 0
                        VSB_cat(h->args, " ");
1255 0
                        VSB_cat(h->args, av[1]);
1256 0
                        av++;
1257 0
                        continue;
1258 0
                }
1259
1260 23
                if (!vstrcmp(*av, "-cli")) {
1261 1
                        REPLACE(h->cli->spec, av[1]);
1262 1
                        if (h->tp)
1263 1
                                haproxy_cli_run(h->cli);
1264 1
                        av++;
1265 1
                        continue;
1266 1
                }
1267
1268 22
                if (!vstrcmp(*av, "-mcli")) {
1269 1
                        REPLACE(h->mcli->spec, av[1]);
1270 1
                        if (h->tp)
1271 1
                                haproxy_cli_run(h->mcli);
1272 1
                        av++;
1273 1
                        continue;
1274 1
                }
1275
1276 21
                if (!vstrcmp(*av, "-conf")) {
1277 8
                        ARGN(vl, av, 1);
1278 8
                        haproxy_store_conf(h, av[1], 0);
1279 8
                        av++;
1280 8
                        continue;
1281 8
                }
1282 13
                if (!vstrcmp(*av, "-conf+backend")) {
1283 1
                        ARGN(vl, av, 1);
1284 1
                        haproxy_store_conf(h, av[1], 1);
1285 1
                        av++;
1286 1
                        continue;
1287 1
                }
1288
1289 12
                if (!vstrcmp(*av, "-expectexit")) {
1290 0
                        h->expect_exit = strtoul(av[1], NULL, 0);
1291 0
                        av++;
1292 0
                        continue;
1293 0
                }
1294 12
                if (!vstrcmp(*av, "-start")) {
1295 9
                        haproxy_start(h);
1296 9
                        continue;
1297 9
                }
1298 3
                if (!vstrcmp(*av, "-wait")) {
1299 3
                        haproxy_wait(h);
1300 3
                        continue;
1301 3
                }
1302 0
                vtc_fatal(h->vl, "Unknown haproxy argument: %s", *av);
1303 3
        }
1304 14
}