5 * (c) 2023 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the mLib utilities library.
12 * mLib is free software: you can redistribute it and/or modify it under
13 * the terms of the GNU Library General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * mLib is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
20 * License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with mLib. If not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
28 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
52 /*----- Preliminaries -----------------------------------------------------*/
54 /* The control macros I'm using below provoke `dangling-else' warnings from
55 * compilers. Suppress them. I generally don't care.
58 #if GCC_VERSION_P(7, 1)
59 # pragma GCC diagnostic ignored "-Wdangling-else"
60 #elif GCC_VERSION_P(4, 2)
61 # pragma GCC diagnostic ignored "-Wparentheses"
64 #if CLANG_VERSION_P(3, 1)
65 # pragma clang diagnostic ignored "-Wdangling-else"
68 /*----- Basic I/O ---------------------------------------------------------*/
70 /* --- @init_comms@ --- *
72 * Arguments: @struct tvec_remotecomms *rc@ = communication state
76 * Use: Initialize a communication state. This doesn't allocate any
77 * resurces: it just ensures that everything is set up so that
78 * subsequent operations -- in particular @release_comms@ --
82 static void init_comms(struct tvec_remotecomms *rc)
84 rc->bin = 0; rc->binsz = 0; dbuf_create(&rc->bout);
85 rc->infd = rc->outfd = -1; rc->f = 0;
88 /* --- @close_comms@ --- *
90 * Arguments: @struct tvec_remotecomms *rc@ = communication state
94 * Use: Close the input and output descriptors.
96 * If the descriptors are already closed -- or were never opened
97 * -- then nothing happens.
100 static void close_comms(struct tvec_remotecomms *rc)
103 if (rc->infd != rc->outfd) close(rc->infd);
107 { close(rc->outfd); rc->outfd = -1; }
108 rc->f |= TVRF_BROKEN;
111 /* --- @release_comms@ --- *
113 * Arguments: @struct tvec_remotecomms *rc@ = communication state
117 * Use: Releases the resources -- most notably the input and output
118 * buffers -- held by the communication state. Also calls
122 static void release_comms(struct tvec_remotecomms *rc)
123 { close_comms(rc); xfree(rc->bin); dbuf_destroy(&rc->bout); }
125 /* --- @setup_comms@ --- *
127 * Arguments: @struct tvec_remotecomms *rc@ = communication state
128 * @int infd, outfd@ = input and output file descriptors
132 * Use: Use the given descriptors for communication.
134 * Clears the private flags.
137 static void setup_comms(struct tvec_remotecomms *rc, int infd, int outfd)
139 rc->infd = infd; rc->outfd = outfd;
140 rc->binoff = rc->binlen = 0;
146 * Arguments: @struct tvec_state *tv@ = test-vector state
147 * @struct tvec_remotecomms *rc@ = communication state
148 * @const char *msg, ...@ = format string and arguments
152 * Use: Reports the message as an error, closes communications and
153 * marks them as broken.
156 static PRINTF_LIKE(3, 4)
157 int ioerr(struct tvec_state *tv, struct tvec_remotecomms *rc,
158 const char *msg, ...)
163 close_comms(rc); rc->f |= TVRF_BROKEN;
164 tvec_report_v(tv, TVLEV_ERR, msg, &ap);
169 /* --- @send_all@ --- *
171 * Arguments: @struct tvec_state *tv@ = test-vector state
172 * @struct tvec_remotecomms *rc@ = communication state
173 * @const unsigned char *p@, @size_t sz@ = output buffer
175 * Returns: Zero on success, %$-1$% on error.
177 * Use: Send the output buffer over the communication state's output
178 * descriptor, even if it has to be written in multiple pieces.
181 static int send_all(struct tvec_state *tv, struct tvec_remotecomms *rc,
182 const unsigned char *p, size_t sz)
188 n = write(rc->outfd, p, sz);
192 ret = ioerr(tv, rc, "failed to send: %s",
193 n ? strerror(errno) : "empty write");
202 /* --- @recv_all@ --- *
204 * Arguments: @struct tvec_state *tv@ = test-vector state
205 * @struct tvec_remotecomms *rc@ = communication state
206 * @unsigned f@ = flags (@RCVF_...@)
207 * @unsigned char *p@, @size_t sz@ = input buffer
208 * @size_t min@ = minimum acceptable size to read
209 * @size_t *n_out@ = size read
211 * Returns: A @RECV_...@ code.
213 * Use: Receive data on the communication state's input descriptor to
214 * read at least @min@ bytes into the input buffer, even if it
215 * has to be done in multiple pieces. If more data is readily
216 * available, then up to @sz@ bytes will be read in total.
218 * If the descriptor immediately reports end-of-file, and
219 * @RCVF_ALLOWEOF@ is set in @f@, then return @RECV_EOF@.
220 * Otherwise, EOF is treated as an I/O error, resulting in a
221 * call to @ioerr@ and a return code of @RECV_FAIL@. If the
222 * read succeeded, then set @*n_out@ to the number of bytes read
223 * and return @RECV_OK@.
226 #define RCVF_ALLOWEOF 1u
234 static int recv_all(struct tvec_state *tv, struct tvec_remotecomms *rc,
235 unsigned f, unsigned char *p, size_t sz,
236 size_t min, size_t *n_out)
242 n = read(rc->infd, p, sz);
244 p += n; sz -= n; tot += n;
245 if (tot >= min) break;
246 } else if (!n && !tot && (f&RCVF_ALLOWEOF))
247 { rc->f |= TVRF_BROKEN; return (RECV_EOF); }
249 return (ioerr(tv, rc, "failed to receive: %s",
250 n ? strerror(errno) : "unexpected end-of-file"));
252 *n_out = tot; return (RECV_OK);
257 /* --- @buferr@ --- *
259 * Arguments: @struct tvec_state *tv@ = test-vector state
260 * @struct tvec_remotecomms *rc@ = communication state
264 * Use: Report a problem preparing the output buffer.
267 static int buferr(struct tvec_state *tv, struct tvec_remotecomms *rc)
268 { return (ioerr(tv, rc, "failed to build output packet")); }
270 /* --- @malformed@ --- *
272 * Arguments: @struct tvec_state *tv@ = test-vector state
273 * @struct tvec_remotecomms *rc@ = communication state
277 * Use: Report an I/O error that the incoming packet is malformed.
280 static int malformed(struct tvec_state *tv, struct tvec_remotecomms *rc)
281 { return (ioerr(tv, rc, "received malformed packet")); }
283 /* --- @remote_send@ --- *
285 * Arguments: @struct tvec_state *tv@ = test-vector state
286 * @struct tvec_remotecomms *rc@ = communication state
288 * Returns: Zero on success, %$-1$% on error.
290 * Use: Send the accuulated contents of the output buffer @rc->bout@.
292 * The function arranges to convert @SIGPIPE@ into an error.
294 * If the output buffer is broken, report this as an I/O error.
297 #define SENDBUFSZ 4096
299 static int remote_send(struct tvec_state *tv, struct tvec_remotecomms *rc)
301 void (*opipe)(int) = SIG_ERR;
304 /* Various preflight checks. */
305 if (rc->f&TVRF_BROKEN) { ret = -1; goto end; }
306 if (DBBAD(&rc->bout)) { ret = buferr(tv, rc); goto end; }
308 /* Arrange to trap broken-pipe errors. */
309 opipe = signal(SIGPIPE, SIG_IGN);
310 if (opipe == SIG_ERR) {
311 ret = ioerr(tv, rc, "failed to ignore `SIGPIPE': %s", strerror(errno));
315 /* Transmit the packet. */
316 if (send_all(tv, rc, DBBASE(&rc->bout), DBLEN(&rc->bout)))
317 { ret = -1; goto end; }
319 /* Done. Put things back the way we found them. */
323 if (opipe != SIG_ERR) signal(SIGPIPE, opipe);
327 /* --- @receive_buffered@ --- *
329 * Arguments: @struct tvec_state *tv@ = test-vector state
330 * @struct tvec_remotecomms *rc@ = communication state
331 * @unsigned f@ = flags (@RCVF_...@)
332 * @size_t want@ = data block size required
334 * Returns: A @RECV_...@ code.
336 * Use: Reads a block of data from the input descriptor into the
339 * This is the main machinery for manipulating the input buffer.
340 * The buffer has three regions:
342 * * from the buffer start to @rc->binoff@ is `consumed';
343 * * from @rc->binoff@ to @rc->binlen@ is `available'; and
344 * * from @rc->binlen@ to @rc->binsz@ is `free'.
346 * Data is read into the start of the `free' region, and the
347 * `available' region is extended to include it. Data in the
348 * `consumed' region is periodically discarded by moving the
349 * data from the `available' region to the start of the buffer
350 * and decreasing @rc->binoff@ and @rc->binlen@.
352 * This function ensures that the `available' region contains at
353 * least @want@ bytes, by (a) extending the buffer, if
354 * necessary, so that @rc->binsz >= rc->binoff + want@, and (b)
355 * reading fresh data from the input descriptor to extend the
356 * `available' region.
358 * If absolutely no data is available, and @RCVF_ALLOWEOF@ is
359 * set in @f@, then return @RECV_EOF@. On I/O errors, including
360 * a short read or end-of-file if @RCVF_ALLOWEOF@ is clear,
361 * return @RECV_FAIL@. On success, return @RECV_OK@. The
362 * amount of data read is indicated by updating the input buffer
363 * variables as described above.
366 #define RECVBUFSZ 4096u
368 static int receive_buffered(struct tvec_state *tv,
369 struct tvec_remotecomms *rc,
370 unsigned f, size_t want)
375 /* If we can supply the caller's requirement from the buffer then do
378 if (rc->binlen - rc->binoff >= want) return (RECV_OK);
380 /* If the buffer is too small then we must grow it. */
381 if (want > rc->binsz) {
382 sz = rc->binsz; if (!sz) sz = RECVBUFSZ;
383 while (sz < want) { assert(sz < (size_t)-1/2); sz *= 2; }
384 if (!rc->bin) rc->bin = xmalloc(sz);
385 else rc->bin = xrealloc(rc->bin, sz, rc->binsz);
389 /* Shunt the unused existing material to the start of the buffer. */
390 memmove(rc->bin, rc->bin + rc->binoff, rc->binlen - rc->binoff);
391 rc->binlen -= rc->binoff; rc->binoff = 0;
393 /* Satisfy the caller from the input stream, and try to fill up as much of
394 * the rest of the buffer as we can.
396 ret = recv_all(tv, rc, rc->binlen ? 0 : f,
397 rc->bin + rc->binlen, rc->binsz - rc->binlen,
398 want - rc->binlen, &sz);
399 if (ret) return (ret);
401 /* Note how much material we have and return. */
402 rc->binlen += sz; return (RECV_OK);
405 /* --- @remote_recv@ --- *
407 * Arguments: @struct tvec_state *tv@ = test-vector state
408 * @unsigned f@ = flags (@RCVF_...@)
409 * @buf *b_out@ = buffer to establish around the packet contents
411 * Returns: A @RECV_...@ code.
413 * Use: Receive a packet into the input buffer @rc->bin@ and
414 * establish @*b_out@ to read from it.
417 static int remote_recv(struct tvec_state *tv, struct tvec_remotecomms *rc,
418 unsigned f, buf *b_out)
424 ASSIGN64(szmax, (size_t)-1);
426 /* Preflight checks. */
427 if (rc->f&TVRF_BROKEN) return (RECV_FAIL);
429 /* See if we can read the next packet length from what we already have. */
430 ret = receive_buffered(tv, rc, f, 8); if (ret) return (ret);
431 LOAD64_L_(k, rc->bin + rc->binoff); rc->binoff += 8;
432 if (CMP64(k, >, szmax))
433 return (ioerr(tv, rc, "packet size 0x%08lx%08lx out of range",
434 (unsigned long)HI64(k), (unsigned long)LO64(k)));
435 want = GET64(size_t, k);
437 /* Read the next packet payload. */
438 ret = receive_buffered(tv, rc, 0, want); if (ret) return (ret);
439 buf_init(b_out, rc->bin + rc->binoff, want); rc->binoff += want;
443 /* --- @QUEUEPK_TAG@, @QUEUEPK@ --- *
445 * Arguments: @tag@ = control structure tag
446 * @struct tvec_state *tv@ = test-vector state
447 * @struct tvec_remotecomms *rc@ = communication state
448 * @unsigned fl@ = flags (@QF_...@)
449 * @unsigned pk@ = packet type
451 * Use: This is syntactically a statement head: the syntax is
452 * @QUEUEPK(tv, rc, f) body [else alt]@. The @body@ should
453 * write material to the output buffer @rc->bout@. The macro
454 * applies appropriate framing. If enough material has been
455 * collected, or if @QF_FORCE@ is set in @fl@, then
456 * @remote_send@ is invoked to transmit the buffered packets.
457 * If there is an error of any kind, then the @alt@ statement,
458 * if any, is executed.
462 #define QUEUEPK_TAG(tag, tv, rc, fl, pk) \
463 if ((rc)->f&TVRF_BROKEN) MC_GOELSE(tag##__else); else \
464 MC_ALLOWELSE(tag##__else) \
465 MC_AFTER(tag##__send, { \
466 if ((DBBAD(&(rc)->bout) && (buferr((tv), (rc)), 1)) || \
467 ((((fl)&QF_FORCE) || DBLEN(&(rc)->bout) >= SENDBUFSZ) && \
468 remote_send(tv, rc))) \
469 MC_GOELSE(tag##__else); \
471 DBUF_ENCLOSEITAG(tag##__frame, &(rc)->bout, (rc)->t, 64_L) \
472 MC_BEFORE(tag##__pkty, { \
473 dbuf_putu16l(&(rc)->bout, (pk)); \
476 #define QUEUEPK(tv, rc, fl, pk) QUEUEPK_TAG(queue, (tv), (rc), (fl), (pk))
478 /*----- Packet types ------------------------------------------------------*/
480 #define TVPF_ACK 0x0001u
482 #define TVPK_VER 0x0000u /* --> min, max: u16 *
484 #define TVPK_BGROUP 0x0002u /* --> name: str16
486 #define TVPK_SETVAR 0x0004u /* --> name: str16, rv: value
488 #define TVPK_TEST 0x0006u /* --> in: regs
490 #define TVPK_EGROUP 0x0008u /* --> --- *
493 #define TVPK_REPORT 0x0100u /* <-- level: u16; msg: string */
494 #define TVPK_PROGRESS 0x0102u /* <-- st: str16 */
496 #define TVPK_SKIPGRP 0x0104u /* <-- excuse: str16 */
497 #define TVPK_SKIP 0x0106u /* <-- excuse: str16 */
498 #define TVPK_FAIL 0x0108u /* <-- flag: u8, detail: str16 */
499 #define TVPK_DUMPREG 0x010au /* <-- ri: u16; disp: u16;
500 * flag: u8, rv: value */
501 #define TVPK_BBENCH 0x010cu /* <-- ident: str32; unit: u16 */
502 #define TVPK_EBENCH 0x010eu /* <-- ident: str32; unit: u16;
503 * flags: u16; n, t, cy: f64 */
505 /*----- Server ------------------------------------------------------------*/
507 /* Forward declaration of output operations. */
508 static const struct tvec_outops remote_ops;
510 static struct tvec_state srvtv; /* server's test-vector state */
511 static struct tvec_remotecomms srvrc = TVEC_REMOTECOMMS_INIT; /* comms */
512 static struct tvec_output srvout = { &remote_ops }; /* output state */
514 /* --- @tvec_setprogress@, @tvec_setprogress_v@ --- *
516 * Arguments: @const char *status@ = progress status token format
517 * @va_list ap@ = argument tail
521 * Use: Reports the progress of a test execution to the client.
523 * The framework makes use of tokens beginning with %|%|%:
525 * * %|%IDLE|%: during the top-level server code;
527 * * %|%SETUP|%: during the enclosing environment's @before@
530 * * %|%RUN|%: during the environment's @run@ function, or the
533 * * %|%DONE|%: during the enclosing environment's @after@
536 * The intent is that a test can use the progress token to check
537 * that a function which is expected to crash does so at the
538 * correct point, so it's expected that more complex test
539 * functions and/or environments will set their own progress
540 * tokens to reflect what's going on.
543 int tvec_setprogress(const char *status, ...)
548 va_start(ap, status); rc = tvec_setprogress_v(status, &ap); va_end(ap);
552 int tvec_setprogress_v(const char *status, va_list *ap)
554 /* Force immediate output in case we crash before the buffer is output
557 QUEUEPK(&srvtv, &srvrc, QF_FORCE, TVPK_PROGRESS)
558 dbuf_vputstrf16l(&srvrc.bout, status, ap);
563 /* --- @tvec_remoteserver@ --- *
565 * Arguments: @int infd@, @int outfd@ = input and output file descriptors
566 * @const struct tvec_config *config@ = test configuration
568 * Returns: Suggested exit code.
570 * Use: Run a test server, reading packets from @infd@ and writing
571 * responses and notifications to @outfd@, and invoking tests as
572 * described by @config@.
574 * This function is not particularly general purpose. It
575 * expects to `take over' the process, and makes use of private
579 int tvec_remoteserver(int infd, int outfd, const struct tvec_config *config)
585 const struct tvec_test *t;
587 const struct tvec_env *env = 0;
588 const struct tvec_vardef *vd = 0; void *varctx;
589 struct tvec_reg *r = 0, rbuf, *r_alloc = 0; size_t rsz = 0;
593 /* Initialize the communication machinery. */
594 setup_comms(&srvrc, infd, outfd);
596 /* Begin a test session using our custom output driver. */
597 tvec_begin(&srvtv, config, &srvout);
599 /* Version negotiation. Expect a @TVPK_VER@ packet. At the moment,
600 * there's only version zero, so we return that.
602 if (remote_recv(&srvtv, &srvrc, 0, &b)) { rc = -1; goto end; }
603 if (buf_getu16l(&b, &pk)) goto bad;
604 if (pk != TVPK_VER) {
605 rc = ioerr(&srvtv, &srvrc,
606 "unexpected packet type 0x%04x instead of client version",
610 if (buf_getu16l(&b, &u) || buf_getu16l(&b, &v)) goto bad;
611 QUEUEPK(&srvtv, &srvrc, QF_FORCE, TVPK_VER | TVPF_ACK)
612 dbuf_putu16l(&srvrc.bout, 0);
613 else { rc = -1; goto end; }
615 /* Handle packets until the server closes the connection.
617 * The protocol looks much simpler from our point of view than from the
620 * * Receive @TVPK_VER@; respond with @TVPK_VER | TVPF_ACK@.
622 * * Receive zero or more @TVPK_BGROUP@. Open a test group, producing
623 * output packets, and eventually answer with @TVPK_BGROUP | TVPF_ACK@.
625 * -- Receive zero or more @TVPK_TEST@. Run a test, producing output
626 * packets, and eventually answer with @TVPK_TEST | TVPF_ACK@.
628 * -- Receive @TVPK_EGROUP@. Maybe produce output packets, and
629 * answer with @TVPK_EGROUP | TVPF_ACK@.
635 /* Read a packet. End-of-file is expected here (and pretty much nowhere
636 * else). Otherwise, we expect to see @TVPK_BGROUP@.
638 rc = remote_recv(&srvtv, &srvrc, RCVF_ALLOWEOF, &b);
639 if (rc == RECV_EOF) break;
640 else if (rc == RECV_FAIL) goto end;
641 if (buf_getu16l(&b, &pk)) goto bad;
648 /* Parse the packet payload. */
649 p = buf_getmem16l(&b, &sz); if (!p) goto bad;
650 if (BLEFT(&b)) goto bad;
652 /* Find the group given its name. */
653 for (t = srvtv.tests; t->name; t++)
654 if (strlen(t->name) == sz && MEMCMP(t->name, ==, p, sz))
656 rc = ioerr(&srvtv, &srvrc, "unknown test group `%.*s'",
661 /* Set up the test environment. */
662 srvtv.test = t; env = t->env;
663 if (env && env->setup == tvec_remotesetup)
664 env = ((struct tvec_remoteenv *)env)->r.env;
665 if (!env || !env->ctxsz) ctx = 0;
666 else ctx = xmalloc(env->ctxsz);
667 if (env && env->setup) env->setup(&srvtv, env, 0, ctx);
669 /* Initialize the registers. */
670 tvec_initregs(&srvtv);
672 /* Report that the group has been opened and that we're ready to run
675 QUEUEPK(&srvtv, &srvrc, QF_FORCE, TVPK_BGROUP | TVPF_ACK);
676 else { rc = -1; goto end; }
678 /* Handle packets until we're told to end the group. */
681 /* Read a packet. We expect @TVPK_EGROUP@ or @TVPK_TEST@. */
682 if (remote_recv(&srvtv, &srvrc, 0, &b)) { rc = -1; goto end; }
683 if (buf_getu16l(&b, &pk)) goto bad;
690 /* Check the payload. */
691 if (BLEFT(&b)) goto bad;
693 /* Leave the group loop. */
697 /* Set a subenvironment variable. */
699 /* Get the variable name. */
700 p = buf_getmem16l(&b, &sz); if (!p) goto bad;
701 DRESET(&d); DPUTM(&d, p, sz); DPUTZ(&d);
703 /* Look up the variable definition. */
704 if (env && env->findvar) {
705 vd = env->findvar(&srvtv, d.buf, &varctx, ctx);
706 if (vd) goto found_var;
708 rc = tvec_unkreg(&srvtv, d.buf); goto setvar_end;
711 /* Set up the register. */
712 if (vd->regsz <= sizeof(rbuf))
715 if (rsz < vd->regsz) {
717 if (!rsz) rsz = 8*sizeof(void *);
718 while (rsz < vd->regsz) rsz *= 2;
719 r_alloc = xmalloc(rsz);
724 /* Collect and set the value. */
725 vd->def.ty->init(&r->v, &vd->def);
726 if (vd->def.ty->frombuf(&b, &r->v, &vd->def)) goto bad;
727 if (BLEFT(&b)) goto bad;
728 rc = vd->setvar(&srvtv, d.buf, &r->v, varctx);
730 /* Send the reply. */
732 QUEUEPK(&srvtv, &srvrc, QF_FORCE, TVPK_SETVAR | TVPF_ACK)
733 dbuf_putbyte(&srvrc.bout, rc ? 0xff : 0);
734 else { rc = -1; goto end; }
735 if (vd) { vd->def.ty->release(&r->v, &vd->def); vd = 0; }
741 /* Parse the packet payload. */
742 if (tvec_deserialize(srvtv.in, &b, srvtv.test->regs,
743 srvtv.nreg, srvtv.regsz))
745 if (BLEFT(&b)) goto bad;
747 /* If we're not skipping the test group, then actually try to
750 if (!(srvtv.f&TVSF_SKIP)) {
752 /* Prepare the output registers and reset the test outcome.
753 * (The environment may force a skip.)
755 for (i = 0; i < srvtv.nrout; i++)
756 if (TVEC_REG(&srvtv, in, i)->f&TVRF_LIVE)
757 TVEC_REG(&srvtv, out, i)->f |= TVRF_LIVE;
758 srvtv.f |= TVSF_ACTIVE; srvtv.f &= ~TVSF_OUTMASK;
760 /* Invoke the environment @before@ function. */
761 tvec_setprogress("%%SETUP");
762 if (env && env->before) env->before(&srvtv, ctx);
764 /* Run the actual test. */
765 if (!(srvtv.f&TVSF_ACTIVE))
766 /* setup forced a skip */;
768 tvec_setprogress("%%RUN");
770 env->run(&srvtv, t->fn, ctx);
772 t->fn(srvtv.in, srvtv.out, ctx);
773 tvec_check(&srvtv, 0);
777 /* Conclude the test. */
778 tvec_setprogress("%%DONE");
779 if (env && env->after) env->after(&srvtv, ctx);
780 tvec_endtest(&srvtv);
783 /* Reset the input registers and report completion. */
784 tvec_releaseregs(&srvtv); tvec_initregs(&srvtv);
785 QUEUEPK(&srvtv, &srvrc, QF_FORCE, TVPK_TEST | TVPF_ACK);
786 else { rc = -1; goto end; }
790 /* Some other kind of packet. Complain. */
792 rc = ioerr(&srvtv, &srvrc,
793 "unexpected packet type 0x%04x during test group",
801 /* The test group completed. */
803 /* Tear down the environment and release other resources. */
804 if (env && env->teardown) env->teardown(&srvtv, ctx);
805 tvec_releaseregs(&srvtv);
806 xfree(ctx); srvtv.test = 0; env = 0; ctx = 0;
808 /* Report completion. */
809 QUEUEPK(&srvtv, &srvrc, QF_FORCE, TVPK_EGROUP | TVPF_ACK);
810 else { rc = -1; goto end; }
814 rc = ioerr(&srvtv, &srvrc,
815 "unexpected packet type 0x%04x at top level", pk);
821 /* Clean up and return. */
822 if (env && env->teardown) env->teardown(&srvtv, ctx);
823 if (vd) vd->def.ty->release(&r->v, &vd->def);
824 xfree(ctx); xfree(r_alloc);
825 if (srvtv.test) tvec_releaseregs(&srvtv);
826 release_comms(&srvrc); tvec_end(&srvtv);
830 /* Miscellaneous malformed packet. */
831 rc = malformed(&srvtv, &srvrc); goto end;
834 /*----- Server output driver ----------------------------------------------*/
836 /* --- @remote_bsession@ --- *
838 * Arguments: @struct tvec_output *o@ = output sink (ignored)
839 * @struct tvec_state *tv@ = the test state producing output
843 * Use: Begin a test session.
845 * The remote driver does nothing at all.
848 static void remote_bsession(struct tvec_output *o, struct tvec_state *tv)
851 /* --- @remote_esession@ --- *
853 * Arguments: @struct tvec_output *o@ = output sink (ignored)
855 * Returns: Suggested exit code.
857 * Use: End a test session.
859 * The remote driver returns a suitable exit code without
863 static int remote_esession(struct tvec_output *o)
864 { return (srvtv.f&TVSF_ERROR ? 2 : 0); }
866 /* --- @remote_bgroup@ --- *
868 * Arguments: @struct tvec_output *o@ = output sink (ignored)
872 * Use: Begin a test group.
874 * This is a stub which should never be called.
877 static void remote_bgroup(struct tvec_output *o)
878 { assert(!"remote_bgroup"); }
880 /* --- @remote_skipgroup@ --- *
882 * Arguments: @struct tvec_output *o@ = output sink (ignored)
883 * @const char *excuse@, @va_list *ap@ = reason for skipping the
888 * Use: Report that a test group is being skipped.
890 * The remote driver sends a @TVPK_SKIP@ packet to its client.
893 static void remote_skipgroup(struct tvec_output *o,
894 const char *excuse, va_list *ap)
896 QUEUEPK(&srvtv, &srvrc, 0, TVPK_SKIPGRP)
897 dbuf_vputstrf16l(&srvrc.bout, excuse, ap);
900 /* --- @remote_egroup@ --- *
902 * Arguments: @struct tvec_output *o@ = output sink (ignored)
906 * Use: Report that a test group has finished.
908 * This is a stub which should never be called.
911 static void remote_egroup(struct tvec_output *o)
912 { assert(!"remote_egroup"); }
914 /* --- @remote_btest@ --- *
916 * Arguments: @struct tvec_output *o@ = output sink (ignored)
920 * Use: Report that a test is starting.
922 * This is a stub which should never be called.
925 static void remote_btest(struct tvec_output *o)
926 { assert(!"remote_btest"); }
928 /* --- @remote_skip@, @remote_fail@ --- *
930 * Arguments: @struct tvec_output *o@ = output sink (ignored)
931 * @unsigned attr@ = attribute to apply to the outcome
932 * @const char *outcome@ = outcome string to report
933 * @const char *detail@, @va_list *ap@ = a detail message
934 * @const char *excuse@, @va_list *ap@ = reason for skipping the
939 * Use: Report that a test has been skipped or failed.
941 * The remote driver sends a @TVPK_SKIP@ or @TVPK_FAIL@ packet
942 * to its client as appropriate.
945 static void remote_skip(struct tvec_output *o,
946 const char *excuse, va_list *ap)
948 QUEUEPK(&srvtv, &srvrc, 0, TVPK_SKIP)
949 dbuf_vputstrf16l(&srvrc.bout, excuse, ap);
952 static void remote_fail(struct tvec_output *o,
953 const char *detail, va_list *ap)
955 QUEUEPK(&srvtv, &srvrc, 0, TVPK_FAIL)
957 dbuf_putbyte(&srvrc.bout, 0);
959 dbuf_putbyte(&srvrc.bout, 1);
960 dbuf_vputstrf16l(&srvrc.bout, detail, ap);
964 /* --- @remote_dumpreg@ --- *
966 * Arguments: @struct tvec_output *o@ = output sink (ignored)
967 * @unsigned disp@ = register disposition
968 * @const union tvec_regval *rv@ = register value
969 * @const struct tvec_regdef *rd@ = register definition
973 * Use: Dump a register.
975 * The remote driver sends a @TVPK_DUMPREG@ packet to its
976 * client. This will only work if the register definition is
977 * one of those listed in the current test definition.
980 static void remote_dumpreg(struct tvec_output *o,
981 unsigned disp, const union tvec_regval *rv,
982 const struct tvec_regdef *rd)
984 const struct tvec_regdef *reg;
987 /* Find the register definition. */
988 for (reg = srvtv.test->regs, r = 0; reg->name; reg++, r++)
989 if (reg == rd) goto found;
990 assert(!"unexpected register definition");
993 QUEUEPK(&srvtv, &srvrc, 0, TVPK_DUMPREG) {
994 dbuf_putu16l(&srvrc.bout, r);
995 dbuf_putu16l(&srvrc.bout, disp);
997 dbuf_putbyte(&srvrc.bout, 0);
999 dbuf_putbyte(&srvrc.bout, 1);
1000 rd->ty->tobuf(DBUF_BUF(&srvrc.bout), rv, rd);
1005 /* --- @remote_etest@ --- *
1007 * Arguments: @struct tvec_output *o@ = output sink (ignored)
1008 * @unsigned outcome@ = the test outcome
1012 * Use: Report that a test has finished.
1014 * The remote driver does nothing at all.
1017 static void remote_etest(struct tvec_output *o, unsigned outcome)
1020 /* --- @remote_bbench@ --- *
1022 * Arguments: @struct tvec_output *o@ = output sink (ignored)
1023 * @const char *ident@ = identifying register values
1024 * @unsigned unit@ = measurement unit (@TVBU_...@)
1028 * Use: Report that a benchmark has started.
1030 * The remote driver sends a @TVPK_BBENCH@ packet to its client.
1033 static void remote_bbench(struct tvec_output *o,
1034 const char *ident, unsigned unit)
1036 QUEUEPK(&srvtv, &srvrc, 0, TVPK_BBENCH) {
1037 dbuf_putstr32l(&srvrc.bout, ident);
1038 dbuf_putu16l(&srvrc.bout, unit);
1042 /* --- @remote_ebench@ --- *
1044 * Arguments: @struct tvec_output *o@ = output sink (ignored)
1045 * @const char *ident@ = identifying register values
1046 * @unsigned unit@ = measurement unit (@TVBU_...@)
1047 * @const struct bench_timing *tm@ = measurement
1051 * Use: Report a benchmark's results
1053 * The remote driver sends a @TVPK_EBENCH@ packet to its client.
1056 static void remote_ebench(struct tvec_output *o,
1057 const char *ident, unsigned unit,
1058 const struct bench_timing *t)
1060 QUEUEPK(&srvtv, &srvrc, 0, TVPK_EBENCH) {
1061 dbuf_putstr32l(&srvrc.bout, ident);
1062 dbuf_putu16l(&srvrc.bout, unit);
1063 if (!t || !(t->f&BTF_ANY))
1064 dbuf_putu16l(&srvrc.bout, 0);
1066 dbuf_putu16l(&srvrc.bout, t->f);
1067 dbuf_putf64l(&srvrc.bout, t->n);
1068 if (t->f&BTF_TIMEOK) dbuf_putf64l(&srvrc.bout, t->t);
1069 if (t->f&BTF_CYOK) dbuf_putf64l(&srvrc.bout, t->cy);
1074 /* --- @remote_report@ --- *
1076 * Arguments: @struct tvec_output *o@ = output sink (ignored)
1077 * @unsigned level@ = message level (@TVLEV_...@)
1078 * @const char *msg@, @va_list *ap@ = format string and
1083 * Use: Report a message to the user.
1085 * The remote driver sends a @TVPK_REPORT@ packet to its
1086 * client. If its attempt to transmit the packet fails, then
1087 * the message is written to the standard error stream instead,
1088 * in the hope that this will help it be noticed.
1091 static void remote_report(struct tvec_output *o, unsigned level,
1092 const char *msg, va_list *ap)
1094 QUEUEPK(&srvtv, &srvrc, 0, TVPK_REPORT) {
1095 dbuf_putu16l(&srvrc.bout, level);
1096 dbuf_vputstrf16l(&srvrc.bout, msg, ap);
1098 fprintf(stderr, "%s %s: ", QUIS, tvec_strlevel(level));
1099 vfprintf(stderr, msg, *ap);
1100 fputc('\n', stderr);
1104 /* --- @remote_destroy@ --- *
1106 * Arguments: @struct tvec_output *o@ = output sink (ignored)
1110 * Use: Release the resources held by the output driver.
1112 * The remote driver does nothing at all.
1115 static void remote_destroy(struct tvec_output *o)
1118 static const struct tvec_outops remote_ops = {
1119 remote_bsession, remote_esession,
1120 remote_bgroup, remote_skipgroup, remote_egroup,
1121 remote_btest, remote_skip, remote_fail, remote_dumpreg, remote_etest,
1122 remote_bbench, remote_ebench,
1127 /*----- Pseudoregister definitions ----------------------------------------*/
1129 static tvec_setvarfn setvar_local, setvar_remote;
1131 static const struct tvec_flag exit_flags[] = {
1134 { "running", TVXF_CAUSEMASK, TVXST_RUN },
1135 { "exited", TVXF_CAUSEMASK, TVXST_EXIT },
1136 { "killed", TVXF_CAUSEMASK, TVXST_KILL },
1137 { "stopped", TVXF_CAUSEMASK, TVXST_STOP },
1138 { "continued", TVXF_CAUSEMASK, TVXST_CONT },
1139 { "disconnected", TVXF_CAUSEMASK, TVXST_DISCONN },
1140 { "unknown", TVXF_CAUSEMASK, TVXST_UNK },
1141 { "error", TVXF_CAUSEMASK, TVXST_ERR },
1144 ;;; The signal name table is very boring to type. To make life less
1145 ;;; awful, put the signal names in this list and evaluate the code to
1146 ;;; get Emacs to regenerate it.
1148 (let ((signals '(HUP INT QUIT ILL TRAP ABRT IOT EMT FPE KILL BUS SEGV SYS
1149 PIPE ALRM TERM URG STOP TSTP CONT CHLD CLD TTIN TTOU
1150 POLL IO TIN XCPU XFSZ VTALRM PROF WINCH USR1 USR2
1151 STKFLT INFO PWR THR LWP LIBRT LOST)))
1153 (goto-char (point-min))
1154 (search-forward (concat "***" "BEGIN siglist" "***"))
1155 (beginning-of-line 2)
1156 (delete-region (point)
1158 (search-forward "***END***")
1161 (dolist (sig signals)
1162 (insert (format "#ifdef SIG%s\n { \"SIG%s\", TVXF_VALMASK | TVXF_SIG, SIG%s | TVXF_SIG },\n#endif\n"
1166 /***BEGIN siglist***/
1168 { "SIGHUP", TVXF_VALMASK | TVXF_SIG, SIGHUP | TVXF_SIG },
1171 { "SIGINT", TVXF_VALMASK | TVXF_SIG, SIGINT | TVXF_SIG },
1174 { "SIGQUIT", TVXF_VALMASK | TVXF_SIG, SIGQUIT | TVXF_SIG },
1177 { "SIGILL", TVXF_VALMASK | TVXF_SIG, SIGILL | TVXF_SIG },
1180 { "SIGTRAP", TVXF_VALMASK | TVXF_SIG, SIGTRAP | TVXF_SIG },
1183 { "SIGABRT", TVXF_VALMASK | TVXF_SIG, SIGABRT | TVXF_SIG },
1186 { "SIGIOT", TVXF_VALMASK | TVXF_SIG, SIGIOT | TVXF_SIG },
1189 { "SIGEMT", TVXF_VALMASK | TVXF_SIG, SIGEMT | TVXF_SIG },
1192 { "SIGFPE", TVXF_VALMASK | TVXF_SIG, SIGFPE | TVXF_SIG },
1195 { "SIGKILL", TVXF_VALMASK | TVXF_SIG, SIGKILL | TVXF_SIG },
1198 { "SIGBUS", TVXF_VALMASK | TVXF_SIG, SIGBUS | TVXF_SIG },
1201 { "SIGSEGV", TVXF_VALMASK | TVXF_SIG, SIGSEGV | TVXF_SIG },
1204 { "SIGSYS", TVXF_VALMASK | TVXF_SIG, SIGSYS | TVXF_SIG },
1207 { "SIGPIPE", TVXF_VALMASK | TVXF_SIG, SIGPIPE | TVXF_SIG },
1210 { "SIGALRM", TVXF_VALMASK | TVXF_SIG, SIGALRM | TVXF_SIG },
1213 { "SIGTERM", TVXF_VALMASK | TVXF_SIG, SIGTERM | TVXF_SIG },
1216 { "SIGURG", TVXF_VALMASK | TVXF_SIG, SIGURG | TVXF_SIG },
1219 { "SIGSTOP", TVXF_VALMASK | TVXF_SIG, SIGSTOP | TVXF_SIG },
1222 { "SIGTSTP", TVXF_VALMASK | TVXF_SIG, SIGTSTP | TVXF_SIG },
1225 { "SIGCONT", TVXF_VALMASK | TVXF_SIG, SIGCONT | TVXF_SIG },
1228 { "SIGCHLD", TVXF_VALMASK | TVXF_SIG, SIGCHLD | TVXF_SIG },
1231 { "SIGCLD", TVXF_VALMASK | TVXF_SIG, SIGCLD | TVXF_SIG },
1234 { "SIGTTIN", TVXF_VALMASK | TVXF_SIG, SIGTTIN | TVXF_SIG },
1237 { "SIGTTOU", TVXF_VALMASK | TVXF_SIG, SIGTTOU | TVXF_SIG },
1240 { "SIGPOLL", TVXF_VALMASK | TVXF_SIG, SIGPOLL | TVXF_SIG },
1243 { "SIGIO", TVXF_VALMASK | TVXF_SIG, SIGIO | TVXF_SIG },
1246 { "SIGTIN", TVXF_VALMASK | TVXF_SIG, SIGTIN | TVXF_SIG },
1249 { "SIGXCPU", TVXF_VALMASK | TVXF_SIG, SIGXCPU | TVXF_SIG },
1252 { "SIGXFSZ", TVXF_VALMASK | TVXF_SIG, SIGXFSZ | TVXF_SIG },
1255 { "SIGVTALRM", TVXF_VALMASK | TVXF_SIG, SIGVTALRM | TVXF_SIG },
1258 { "SIGPROF", TVXF_VALMASK | TVXF_SIG, SIGPROF | TVXF_SIG },
1261 { "SIGWINCH", TVXF_VALMASK | TVXF_SIG, SIGWINCH | TVXF_SIG },
1264 { "SIGUSR1", TVXF_VALMASK | TVXF_SIG, SIGUSR1 | TVXF_SIG },
1267 { "SIGUSR2", TVXF_VALMASK | TVXF_SIG, SIGUSR2 | TVXF_SIG },
1270 { "SIGSTKFLT", TVXF_VALMASK | TVXF_SIG, SIGSTKFLT | TVXF_SIG },
1273 { "SIGINFO", TVXF_VALMASK | TVXF_SIG, SIGINFO | TVXF_SIG },
1276 { "SIGPWR", TVXF_VALMASK | TVXF_SIG, SIGPWR | TVXF_SIG },
1279 { "SIGTHR", TVXF_VALMASK | TVXF_SIG, SIGTHR | TVXF_SIG },
1282 { "SIGLWP", TVXF_VALMASK | TVXF_SIG, SIGLWP | TVXF_SIG },
1285 { "SIGLIBRT", TVXF_VALMASK | TVXF_SIG, SIGLIBRT | TVXF_SIG },
1288 { "SIGLOST", TVXF_VALMASK | TVXF_SIG, SIGLOST | TVXF_SIG },
1292 /* This should be folded into the signal entries above. */
1293 { "signal", TVXF_SIG, TVXF_SIG },
1297 static const struct tvec_flaginfo exit_flaginfo =
1298 { "exit-status", exit_flags, &tvrange_uint };
1299 static const struct tvec_vardef exit_var =
1300 { sizeof(struct tvec_reg), setvar_local,
1301 { "@exit", -1, &tvty_flags, 0, { &exit_flaginfo } } };
1305 static const struct tvec_vardef progress_var =
1306 { sizeof(struct tvec_reg), setvar_local,
1307 { "@progress", -1, &tvty_text, 0 } };
1311 static const struct tvec_uassoc reconn_assocs[] = {
1312 { "on-demand", TVRCN_DEMAND },
1313 { "force", TVRCN_FORCE },
1314 { "skip", TVRCN_SKIP },
1317 static const struct tvec_uenuminfo reconn_enuminfo =
1318 { "remote-reconnection", reconn_assocs, &tvrange_uint };
1319 static const struct tvec_vardef reconn_var =
1320 { sizeof(struct tvec_reg), setvar_local,
1321 { "@reconnect", -1, &tvty_uenum, 0, { &reconn_enuminfo } } };
1323 /*----- Client ------------------------------------------------------------*/
1325 /* Connection state. */
1327 CONN_BROKEN = -2, /* previously broken */
1328 CONN_FAILED = -1, /* attempt freshly failed */
1329 CONN_ESTABLISHED = 0, /* previously established */
1330 CONN_FRESH = 1 /* freshly connected */
1333 /* --- @handle_packets@ --- *
1335 * Arguments: @struct tvec_state *tv@ = test-vector state
1336 * @struct tvec_remotectx *r@ = remote client context
1337 * @unsigned f@ = receive flags (@RCVF_...@)
1338 * @uint16 end@ = expected end packet type
1339 * @buf *b_out@ = buffer in which to return end packet payload
1341 * Returns: A @RECV_...@ code.
1343 * Use: Handles notification packets from the server until a final
1344 * termination packet is received.
1346 * The client/server protocol consists of a number of flows,
1347 * beginning with a request from the client, followed by a
1348 * number of notifications from the server, and terminated by an
1349 * acknowledgement to the original request indicating that the
1350 * server has completed acting on the original request.
1352 * This function handles the notifications issued by the server,
1353 * returning when one of the following occurs: (a) a packet of
1354 * type @end@ is received, in which case the function returns
1355 * @RECV_OK@ and the remainder of the packet payload is left in
1356 * @b_out@; (b) the flag @RCVF_ALLOWEOF@ was set in @f@ on entry
1357 * and end-of-file is received at a packet boundary, in which
1358 * case the function returns @RECV_EOF@; or (c) an I/O error
1359 * occurs, in which case @ioerr@ is called and the function
1360 * returns @RECV_FAIL@.
1363 static int handle_packets(struct tvec_state *tv, struct tvec_remotectx *r,
1364 unsigned f, uint16 end, buf *b_out)
1366 struct tvec_output *o = tv->output;
1368 const char *p; size_t n;
1371 const struct tvec_regdef *rd;
1372 struct bench_timing bt;
1373 struct tvec_reg *reg = 0;
1379 /* Read the next packet. If we didn't receive one then end the loop.
1380 * Otherwise, retrieve the packet type and check it against @end@: quit
1381 * the loop if we get a match.
1383 rc = remote_recv(tv, &r->rc, f, b); if (rc) break;
1384 if (buf_getu16l(b, &pk)) goto bad;
1385 if (pk == end) { rc = 0; break; }
1387 /* Dispatch based on the packet type. */
1391 /* A progress report. Update the saved progress. */
1393 p = buf_getmem16l(b, &n); if (!p) goto bad;
1394 if (BLEFT(b)) goto bad;
1396 DRESET(&r->progress); DPUTM(&r->progress, p, n); DPUTZ(&r->progress);
1400 /* A report. Recover the message and pass it along. */
1402 if (buf_getu16l(b, &u)) goto bad;
1403 p = buf_getmem16l(b, &n); if (!p) goto bad;
1404 if (BLEFT(b)) goto bad;
1406 DRESET(&d); DPUTM(&d, p, n); DPUTZ(&d);
1407 tvec_report(tv, u, "%s", d.buf);
1411 /* A request to skip the group. Recover the excuse message and pass
1415 p = buf_getmem16l(b, &n); if (!p) goto bad;
1416 if (BLEFT(b)) goto bad;
1418 DRESET(&d); DPUTM(&d, p, n); DPUTZ(&d);
1419 tvec_skipgroup(tv, "%s", d.buf);
1423 /* A request to skip the test. Recover the excuse message and pass
1424 * it along, if it's not unreasonable.
1427 if (!(tv->f&TVSF_ACTIVE)) {
1428 rc = ioerr(tv, &r->rc, "test `%s' not active", tv->test->name);
1432 p = buf_getmem16l(b, &n); if (!p) goto bad;
1433 if (BLEFT(b)) goto bad;
1435 DRESET(&d); DPUTM(&d, p, n); DPUTZ(&d);
1436 tvec_skip(tv, "%s", d.buf);
1440 /* A report that the test failed. Recover the detail message, if
1441 * any, and pass it along, if it's not unreasonable.
1444 if (!(tv->f&TVSF_ACTIVE) &&
1445 ((tv->f&TVSF_OUTMASK) != (TVOUT_LOSE << TVSF_OUTSHIFT))) {
1446 rc = ioerr(tv, &r->rc, "test `%s' not active or failing",
1451 rc = buf_getbyte(b); if (rc < 0) goto bad;
1452 if (rc) { p = buf_getmem16l(b, &n); if (!p) goto bad; }
1454 if (BLEFT(b)) goto bad;
1459 DRESET(&d); DPUTM(&d, p, n); DPUTZ(&d);
1460 tvec_fail(tv, "%s", d.buf);
1465 /* A request to dump a register. */
1467 /* Find the register definition. */
1468 if (buf_getu16l(b, &u) || buf_getu16l(b, &v)) goto bad;
1469 for (rd = tv->test->regs, i = 0; rd->name; rd++, i++)
1470 if (i == u) goto found_reg;
1471 rc = ioerr(tv, &r->rc,
1472 "register definition %u out of range for test `%s'",
1476 if (v >= TVRD_LIMIT) {
1477 rc = ioerr(tv, &r->rc, "register disposition %u out of range", v);
1481 /* Read the flag. If there's no register value, then `dump' its
1482 * absence. Otherwise retrieve the register value and dump it.
1484 rc = buf_getbyte(b); if (rc < 0) goto bad;
1486 tvec_dumpreg(tv, v, 0, rd);
1488 if (!reg) reg = xmalloc(tv->regsz);
1489 rd->ty->init(®->v, rd);
1490 rc = rd->ty->frombuf(b, ®->v, rd);
1491 if (!rc) tvec_dumpreg(tv, v, ®->v, rd);
1492 rd->ty->release(®->v, rd);
1495 if (BLEFT(b)) goto bad;
1499 /* A report that we're starting a benchmark. Pass this along. */
1501 p = buf_getmem32l(b, &n); if (!p) goto bad;
1502 if (buf_getu16l(b, &u)) goto bad;
1503 if (BLEFT(b)) goto bad;
1504 if (u >= TVBU_LIMIT) {
1505 rc = ioerr(tv, &r->rc, "unit code %u out of range", u);
1509 DRESET(&d); DPUTM(&d, p, n); DPUTZ(&d);
1510 o->ops->bbench(o, d.buf, u);
1514 /* A report that a benchmark completed. Pass this along. */
1516 p = buf_getmem32l(b, &n); if (!p) goto bad;
1517 if (buf_getu16l(b, &u) || buf_getu16l(b, &v)) goto bad;
1518 if (u >= TVBU_LIMIT) {
1519 rc = ioerr(tv, &r->rc, "unit code %u out of range", u);
1522 if ((v&BTF_ANY) && buf_getf64l(b, &bt.n)) goto bad;
1523 if ((v&BTF_TIMEOK) && buf_getf64l(b, &bt.t)) goto bad;
1524 if ((v&BTF_CYOK) && buf_getf64l(b, &bt.cy)) goto bad;
1525 if (BLEFT(b)) goto bad;
1527 DRESET(&d); DPUTM(&d, p, n); DPUTZ(&d);
1528 o->ops->ebench(o, d.buf, u, v&BTF_ANY ? &bt : 0);
1532 /* Something else. This is unexpected. */
1534 rc = ioerr(tv, &r->rc, "unexpected packet type 0x%04x", pk);
1544 rc = malformed(tv, &r->rc); goto end;
1547 /* --- @reap_kid@ --- *
1549 * Arguments: @struct tvec_state *tv@ = test-vector state
1550 * @struct tvec_remotectx *r@ = remote client context
1554 * Use: Determine the exit status of a broken connection, setting
1555 * @r->exit@ appropriately.
1557 * If @r->kid@ is negative, the exit status has already been
1558 * set, and nothing further happens; this is not an error.
1560 * If @r->kid@ is zero, then there is no real child process
1561 * (e.g., because the remote connection is a network connection
1562 * or similar), so @r->exit@ is set equal to @RVXST_DISCONN@.
1564 * If @r->kid@ is positive, then it holds a child process id;
1565 * the function waits for it to end and collects its exit status
1567 * It is an error to call this function if the connection is not
1571 static void reap_kid(struct tvec_state *tv, struct tvec_remotectx *r)
1576 assert(r->rc.f&TVRF_BROKEN);
1578 { r->exit = TVXST_DISCONN; r->kid = -1; }
1579 else if (r->kid > 0) {
1580 kid = waitpid(r->kid, &st, 0);
1582 tvec_notice(tv, "failed to wait for remote child: %s",
1584 r->exit = TVXST_ERR;
1586 tvec_notice(tv, "remote child vanished without a trace");
1587 r->exit = TVXST_ERR;
1588 } else if (WIFCONTINUED(st))
1589 r->exit = TVXST_CONT;
1590 else if (WIFSIGNALED(st))
1591 r->exit = TVXST_KILL | TVXF_SIG | WTERMSIG(st);
1592 else if (WIFSTOPPED(st))
1593 r->exit = TVXST_STOP | TVXF_SIG | WSTOPSIG(st);
1594 else if (WIFEXITED(st))
1595 r->exit = TVXST_EXIT | WEXITSTATUS(st);
1597 tvec_notice(tv, "remote child died with unknown status 0x%04x",
1599 r->exit = TVXST_UNK;
1605 /* --- @report_errline@ --- *
1607 * Arguments: @char *p@ = pointer to the line
1608 * @size_t n@ = length in characters
1609 * @void *ctx@ = context, secretly a @struct tvec_remotectx@
1613 * Use: Print a line of stderr output from the child. If
1614 * @TVRF_MUFFLE@ is set, then discard the line silently.
1616 * This is an @lbuf_func@, invoked via @drain_errfd@.
1619 static void report_errline(char *p, size_t n, void *ctx)
1621 struct tvec_remotectx *r = ctx;
1622 struct tvec_state *tv = r->tv;
1624 if (p && !(r->rc.f&TVRF_MUFFLE))
1625 tvec_notice(tv, "child process stderr: %s", p);
1628 /* --- @drain_errfd@ --- *
1630 * Arguments: @struct tvec_state *tv@ = test-vector state
1631 * @struct tvec_remotectx *r@ = remote client context
1632 * @unsigned f@ = receive flags (@ERF_...@)
1634 * Returns: Zero on success, %$-1$% on error.
1636 * Use: Collect material written by the child to its stderr stream
1639 * If @f@ has @ERF_SILENT@ set, then discard the stderr material
1640 * without reporting it. Otherwise it is reported as
1643 * if @f@ has @ERF_CLOSE@ set, then continue reading until
1644 * end-of-file is received; also, report any final partial line,
1645 * and close @r->errfd@.
1647 * If @r->errfd@ is already closed, or never established, then
1648 * do nothing and return successfully.
1651 #define ERF_SILENT 0x0001u
1652 #define ERF_CLOSE 0x0002u
1653 static int drain_errfd(struct tvec_state *tv, struct tvec_remotectx *r,
1660 /* Preliminaries. Bail if there is no error stream to fetch. Arrange
1661 * (rather clumsily) to muffle the output if we're supposed to be client.
1662 * And set the nonblocking state on @errfd@ appropriately.
1664 if (r->errfd < 0) { rc = 0; goto end; }
1665 if (f&ERF_SILENT) r->rc.f |= TVRF_MUFFLE;
1666 else r->rc.f &= ~TVRF_MUFFLE;
1667 if (fdflags(r->errfd, O_NONBLOCK, f&ERF_CLOSE ? 0 : O_NONBLOCK, 0, 0)) {
1668 rc = ioerr(tv, &r->rc, "failed to %s error non-blocking flag",
1669 f&ERF_CLOSE ? "clear" : "set");
1673 /* Read pieces of error output and feed them into the line buffer. */
1675 sz = lbuf_free(&r->errbuf, &p);
1676 n = read(r->errfd, p, sz);
1679 if (errno == EINTR) continue;
1680 if (!(f&ERF_CLOSE) && (errno == EWOULDBLOCK || errno == EAGAIN))
1682 rc = ioerr(tv, &r->rc, "failed to read child stderr: %s",
1686 lbuf_flush(&r->errbuf, p, n);
1693 lbuf_close(&r->errbuf);
1694 close(r->errfd); r->errfd = -1;
1699 /* --- @disconnect_remote@ --- *
1701 * Arguments: @struct tvec_state *tv@ = test-vector state
1702 * @struct tvec_remotectx *r@ = remote client context
1703 * @unsigned f@ = receive flags (@DCF_...@)
1707 * Use: Disconnect and shut down all of the remote client state.
1709 * If @f@ has @DCF_KILL@ set then send the child process (if
1710 * any) @SIGTERM@ to make sure it shuts down in a timely manner.
1712 * In detail: this function closes the @infd@ and @outfd@
1713 * descriptors, drains and closes @errfd@, and collects the exit
1717 #define DCF_KILL 0x0100u
1718 static void disconnect_remote(struct tvec_state *tv,
1719 struct tvec_remotectx *r, unsigned f)
1721 if (r->kid > 0 && (f&DCF_KILL)) kill(r->kid, SIGTERM);
1722 close_comms(&r->rc);
1723 drain_errfd(tv, r, f | ERF_CLOSE); reap_kid(tv, r);
1726 /* --- @connect_remote@ --- *
1728 * Arguments: @struct tvec_state *tv@ = test-vector state
1729 * @struct tvec_remotectx *r@ = remote client context
1731 * Returns: Zero on success, %$-1$% on error.
1733 * Use: Connect to the test server.
1736 static int connect_remote(struct tvec_state *tv, struct tvec_remotectx *r)
1738 const struct tvec_remoteenv *re = r->re;
1742 int infd = -1, outfd = -1, errfd = -1, rc;
1744 /* If we're already connected, then there's nothing to do. */
1745 if (r->kid >= 0) { rc = 0; goto end; }
1747 /* Set the preliminary progress indication. */
1748 DRESET(&r->progress); DPUTS(&r->progress, "%INIT");
1750 /* Call the connection function to establish descriptors. */
1751 if (re->r.connect(&kid, &infd, &outfd, &errfd, tv, re))
1752 { rc = -1; goto end; }
1754 /* Establish communications state. */
1755 setup_comms(&r->rc, infd, outfd); r->kid = kid; r->errfd = errfd;
1756 lbuf_init(&r->errbuf, report_errline, r);
1757 r->exit = TVXST_RUN; r->rc.f &= ~TVRF_BROKEN;
1759 /* Do version negotiation. */
1760 QUEUEPK(tv, &r->rc, QF_FORCE, TVPK_VER) {
1761 dbuf_putu16l(&r->rc.bout, 0);
1762 dbuf_putu16l(&r->rc.bout, 0);
1763 } else { rc = -1; goto end; }
1764 if (handle_packets(tv, r, 0, TVPK_VER | TVPF_ACK, &b))
1765 { rc = -1; goto end; }
1766 if (buf_getu16l(&b, &v)) goto bad;
1767 if (BLEFT(&b)) { rc = malformed(tv, &r->rc); goto end; }
1769 rc = ioerr(tv, &r->rc, "protocol version %u not supported", v);
1774 /* Begin the test group at the server. */
1775 QUEUEPK(tv, &r->rc, QF_FORCE, TVPK_BGROUP)
1776 dbuf_putstr16l(&r->rc.bout, tv->test->name);
1777 else { rc = -1; goto end; }
1778 if (handle_packets(tv, r, 0, TVPK_BGROUP | TVPF_ACK, &b))
1779 { rc = -1; goto end; }
1780 if (BLEFT(&b)) { rc = malformed(tv, &r->rc); goto end; }
1785 if (rc) disconnect_remote(tv, r, DCF_KILL);
1788 rc = malformed(tv, &r->rc); goto end;
1791 /* --- @check_comms@ --- *
1793 * Arguments: @struct tvec_state *tv@ = test-vector state
1794 * @struct tvec_remotectx *r@ = remote client context
1796 * Returns: A @CONN_...@ code reflecting the current communication
1799 * Use: Determine the current connection state. If the connection
1800 * has recently broken (i.e., @TVRF_BROKEN@ is set in @r->rc.f@)
1801 * since the last time we checked then disconnect.
1804 static int check_comms(struct tvec_state *tv, struct tvec_remotectx *r)
1807 return (CONN_BROKEN);
1808 else if (r->rc.f&TVRF_BROKEN)
1809 { disconnect_remote(tv, r, DCF_KILL); return (CONN_FAILED); }
1811 return (CONN_ESTABLISHED);
1814 /* --- @try_reconnect@ --- *
1816 * Arguments: @struct tvec_state *tv@ = test-vector state
1817 * @struct tvec_remotectx *r@ = remote client context
1819 * Returns: A @CONN_...@ code reflecting the new communication state.
1821 * Use: Reconnects to the server according to the configured
1822 * @TVRCN_...@ policy.
1825 static int try_reconnect(struct tvec_state *tv, struct tvec_remotectx *r)
1829 switch (r->rc.f&TVRF_RCNMASK) {
1831 rc = check_comms(tv, r);
1832 if (rc < CONN_ESTABLISHED) {
1833 close_comms(&r->rc);
1834 if (connect_remote(tv, r)) rc = CONN_FAILED;
1835 else rc = CONN_FRESH;
1839 disconnect_remote(tv, r, DCF_KILL);
1840 if (connect_remote(tv, r)) rc = CONN_FAILED;
1841 else rc = CONN_FRESH;
1844 rc = check_comms(tv, r);
1852 /*----- Remote environment ------------------------------------------------*/
1854 /* --- @reset_vars@ --- *
1856 * Arguments: @struct tvec_remotectx *r@ = remote client context
1860 * Use: Reset the pseudoregisters set through @tvec_remoteset@.
1863 static void reset_vars(struct tvec_remotectx *r)
1865 const struct tvec_remoteenv *re = r->re;
1867 r->exwant = TVXST_RUN;
1868 r->rc.f = (r->rc.f&~(TVRF_RCNMASK | TVRF_SETMASK)) |
1869 (re->r.dflt_reconn&TVRF_RCNMASK);
1870 DRESET(&r->prgwant); DPUTS(&r->prgwant, "%DONE");
1873 /* --- @tvec_remotesetup@ --- *
1875 * Arguments: @struct tvec_state *tv@ = test vector state
1876 * @const struct tvec_env *env@ = environment description
1877 * @void *pctx@ = parent context (ignored)
1878 * @void *ctx@ = context pointer to initialize
1882 * Use: Initialize a timeout environment context.
1884 * The environment description should be a @struct
1885 * tvec_remoteenv@ subclass suitable for use by the @connect@
1889 void tvec_remotesetup(struct tvec_state *tv, const struct tvec_env *env,
1890 void *pctx, void *ctx)
1892 struct tvec_remotectx *r = ctx;
1893 const struct tvec_remoteenv *re = (const struct tvec_remoteenv *)env;
1894 const struct tvec_env *subenv = re->r.env;
1898 r->re = re; r->kid = -1;
1899 DCREATE(&r->prgwant); DCREATE(&r->progress);
1900 if (connect_remote(tv, r))
1901 tvec_skipgroup(tv, "failed to connect to test backend");
1903 if (subenv && subenv->ctxsz) r->subctx = xmalloc(subenv->ctxsz);
1905 if (subenv && subenv->setup) subenv->setup(tv, subenv, r, r->subctx);
1908 /* --- @tvec_remotefindvar@, @setvar_local@, @setvar_remote@ --- *
1910 * Arguments: @struct tvec_state *tv@ = test vector state
1911 * @const char *var@ = variable name to set
1912 * @const union tvec_regval *rv@ = register value
1913 * @void **ctx_out@ = where to put the @setvar@ context
1914 * @void *ctx@ = context pointer
1916 * Returns: @tvec_remotefindvar@ returns a pointer to the variable
1917 * definition, or null; @remote_setvar@ returns zero on success
1918 * or %$-1$% on error.
1920 * Use: Set a special variable. The following special variables are
1923 * * %|@exit|% is the expected exit status; see @TVXF_...@ and
1926 * * %|progress|% is the expected progress token when the test
1927 * completes. On successful completion, this will be
1928 * %|%DONE|%; it's %|%RUN|% on entry to the test function,
1929 * but that can call @tvec_setprogress@ to change it.
1931 * * %|reconnect|% is a reconnection policy; see @TVRCN_...@.
1934 static int setvar_local(struct tvec_state *tv, const char *var,
1935 const union tvec_regval *rv, void *ctx)
1937 struct tvec_remotectx *r = ctx;
1939 if (STRCMP(var, ==, "@exit")) {
1940 if (r->rc.f&TVRF_SETEXIT) return (tvec_dupreg(tv, var));
1941 r->exwant = rv->u; r->rc.f |= TVRF_SETEXIT; return (0);
1942 } else if (STRCMP(var, ==, "@progress")) {
1943 if (r->rc.f&TVRF_SETPRG) return (tvec_dupreg(tv, var));
1944 DRESET(&r->prgwant); DPUTM(&r->prgwant, rv->text.p, rv->text.sz);
1946 r->rc.f |= TVRF_SETPRG; return (0);
1947 } else if (STRCMP(var, ==, "@reconnect")) {
1948 if (r->rc.f&TVRF_SETRCN) return (tvec_dupreg(tv, var));
1949 r->rc.f = (r->rc.f&~TVRF_RCNMASK) | (rv->u&TVRF_RCNMASK) | TVRF_SETRCN;
1951 } else assert(!"unknown var");
1954 static int setvar_remote(struct tvec_state *tv, const char *var,
1955 const union tvec_regval *rv, void *ctx)
1957 struct tvec_remotectx *r = ctx;
1961 if (try_reconnect(tv, r) < 0) { rc = 0; goto end; }
1963 QUEUEPK(tv, &r->rc, QF_FORCE, TVPK_SETVAR) {
1964 dbuf_putstr16l(&r->rc.bout, var);
1965 r->vd.def.ty->tobuf(DBUF_BUF(&r->rc.bout), rv, &r->vd.def);
1966 } else { rc = -1; goto end; }
1968 rc = handle_packets(tv, r, 0, TVPK_SETVAR | TVPF_ACK, &b);
1970 ch = buf_getbyte(&b);
1971 if (ch < 0) { rc = malformed(tv, &r->rc); goto end; }
1972 if (BLEFT(&b)) { rc = malformed(tv, &r->rc); goto end; }
1979 const struct tvec_vardef *tvec_remotefindvar
1980 (struct tvec_state *tv, const char *var, void **ctx_out, void *ctx)
1982 struct tvec_remotectx *r = ctx;
1983 const struct tvec_remoteenv *re = r->re;
1984 const struct tvec_env *subenv = re->r.env;
1985 const struct tvec_vardef *vd; void *varctx;
1987 if (STRCMP(var, ==, "@exit"))
1988 { *ctx_out = r; return (&exit_var); }
1989 else if (STRCMP(var, ==, "@progress"))
1990 { *ctx_out = r; return (&progress_var); }
1991 else if (STRCMP(var, ==, "@reconnect"))
1992 { *ctx_out = r; return (&reconn_var); }
1993 else if (subenv && subenv->findvar) {
1994 vd = subenv->findvar(tv, var, &varctx, r->subctx);
1995 if (!vd) return (0);
1996 r->vd.regsz = vd->regsz; r->vd.setvar = setvar_remote;
1997 r->vd.def = vd->def;
1998 *ctx_out = r; return (&r->vd);
2003 /* --- @tvec_remotebefore@ --- *
2005 * Arguments: @struct tvec_state *tv@ = test vector state
2006 * @void *ctx@ = context pointer
2010 * Use: Invoke the subordinate environment's @before@ function.
2013 void tvec_remotebefore(struct tvec_state *tv, void *ctx)
2015 struct tvec_remotectx *r = ctx;
2016 const struct tvec_remoteenv *re = r->re;
2017 const struct tvec_env *subenv = re->r.env;
2019 if (subenv && subenv->before) subenv->before(tv, r->subctx);
2022 /* --- @tvec_remoterun@ --- *
2024 * Arguments: @struct tvec_state *tv@ = test vector state
2025 * @tvec_testfn *fn@ = test function to run
2026 * @void *ctx@ = context pointer for the test function
2030 * Use: Run a test on a remote server.
2033 void tvec_remoterun(struct tvec_state *tv, tvec_testfn *fn, void *ctx)
2035 struct tvec_remotectx *r = ctx;
2036 union tvec_regval rv;
2039 #define f_progress 2u
2044 /* Reconnect to the server according to policy. */
2045 switch (try_reconnect(tv, r)) {
2047 tvec_skip(tv, "failed to connect to test backend"); return;
2049 tvec_skip(tv, "no connection"); return;
2052 /* Set initial progress state. */
2053 DRESET(&r->progress); DPUTS(&r->progress, "%IDLE");
2055 /* Send the command to the server and handle output. */
2056 QUEUEPK(tv, &r->rc, QF_FORCE, TVPK_TEST)
2057 tvec_serialize(tv->in, DBUF_BUF(&r->rc.bout),
2058 tv->test->regs, tv->nreg, tv->regsz);
2060 rc = handle_packets(tv, r, RCVF_ALLOWEOF, TVPK_TEST | TVPF_ACK, &b);
2062 /* Deal with the outcome. */
2066 /* Some kind of error. Abandon ship. */
2069 tvec_skip(tv, "remote test runner communications failed");
2070 disconnect_remote(tv, r, 0);
2074 /* End-of-file at a packet boundary. The server crashed trying to run
2075 * our test. Collect the exit status and continue.
2081 /* Successful completion (or EOF). */
2083 /* Notice if the exit status isn't right. */
2084 if (r->exit != r->exwant) f |= f_exit;
2086 /* Notice if the progress token isn't right. */
2087 if (r->progress.len != r->prgwant.len ||
2088 MEMCMP(r->progress.buf, !=, r->prgwant.buf, r->progress.len))
2091 /* If we found something wrong but the test is passing so far, then
2092 * report the failure and dump the input registers.
2094 if (f && (tv->f&TVSF_ACTIVE))
2095 { tvec_fail(tv, 0); tvec_mismatch(tv, TVMF_IN); }
2097 /* If the test failed, then report the exit and progress states
2098 * relative to their expectations.
2100 if (!(tv->f&TVSF_ACTIVE) &&
2101 (tv->f&TVSF_OUTMASK) == (TVOUT_LOSE << TVSF_OUTSHIFT)) {
2103 /* Note here that the test failed. */
2106 /* Report exit status. */
2108 tvec_dumpreg(tv, f&f_exit ? TVRD_FOUND : TVRD_MATCH,
2109 &rv, &exit_var.def);
2112 tvec_dumpreg(tv, TVRD_EXPECT, &rv, &exit_var.def);
2115 /* Report progress token. */
2116 rv.text.p = r->progress.buf; rv.text.sz = r->progress.len;
2117 tvec_dumpreg(tv, f&f_progress ? TVRD_FOUND : TVRD_MATCH,
2118 &rv, &progress_var.def);
2120 rv.text.p = r->prgwant.buf; rv.text.sz = r->prgwant.len;
2121 tvec_dumpreg(tv, TVRD_EXPECT, &rv, &progress_var.def);
2125 /* If we received end-of-file, then close the connection. Suppress
2126 * error output if the test passed: it was presumably expected.
2129 disconnect_remote(tv, r, f ? 0 : ERF_SILENT);
2138 /* --- @tvec_remoteafter@ --- *
2140 * Arguments: @struct tvec_state *tv@ = test vector state
2141 * @void *ctx@ = context pointer
2145 * Use: Reset variables to their default values.
2148 void tvec_remoteafter(struct tvec_state *tv, void *ctx)
2150 struct tvec_remotectx *r = ctx;
2151 const struct tvec_remoteenv *re = r->re;
2152 const struct tvec_env *subenv = re->r.env;
2155 if (subenv && subenv->after) subenv->after(tv, r->subctx);
2158 /* --- @tvec_remoteteardown@ --- *
2160 * Arguments: @struct tvec_state *tv@ = test vector state
2161 * @void *ctx@ = context pointer
2165 * Use: Tear down the remote environment.
2168 void tvec_remoteteardown(struct tvec_state *tv, void *ctx)
2170 struct tvec_remotectx *r = ctx;
2171 const struct tvec_remoteenv *re = r->re;
2172 const struct tvec_env *subenv = re->r.env;
2175 if (subenv && subenv->teardown) subenv->teardown(tv, r->subctx);
2177 if (r->rc.outfd >= 0) {
2178 QUEUEPK(tv, &r->rc, QF_FORCE, TVPK_EGROUP);
2179 if (!handle_packets(tv, r, RCVF_ALLOWEOF, TVPK_EGROUP | TVPF_ACK, &b))
2180 if (BLEFT(&b)) malformed(tv, &r->rc);
2182 disconnect_remote(tv, r, 0); release_comms(&r->rc);
2183 DDESTROY(&r->prgwant); DDESTROY(&r->progress);
2186 /*----- Connectors --------------------------------------------------------*/
2188 /* --- @fork_common@ --- *
2190 * Arguments: @pid_t *kid_out@ = where to put child process-id
2191 * @int *infd_out, *outfd_out, *errfd_out@ = where to put file
2193 * @struct tvec_state *tv@ = test vector state
2195 * Returns: Zero on success, %$-1$% on failure.
2197 * Use: Common @fork@ machinery for the connectors. Create a
2198 * subprocess. On successful return, in the subprocess,
2199 * @*kid_out@ is zero, and the error descriptor replaces the
2200 * standard-error descriptor; in the parent, @*kid_out@ is the
2201 * child process-id, and @*errfd_out@ is a descriptor on which
2202 * the child's standard-error output can be read; in both
2203 * @*infd_out@ and @*outfd_out@ are descriptors for input and
2204 * output respectively -- they're opposite ends of pipes, but
2205 * obviously they're crossed over so that the parent's output
2206 * matches the child's input and %%\emph{vice versa}%%.
2209 static int fork_common(pid_t *kid_out, int *infd_out, int *outfd_out,
2210 int *errfd_out, struct tvec_state *tv)
2212 int p0[2] = { -1, -1 }, p1[2] = { -1, -1 }, pe[2] = { -1, -1 };
2216 /* Try to create the pipes. */
2217 if (pipe(p0) || pipe(p1) || pipe(pe) ||
2218 fdflags(p0[1], 0, 0, FD_CLOEXEC, FD_CLOEXEC) ||
2219 fdflags(p1[0], 0, 0, FD_CLOEXEC, FD_CLOEXEC) ||
2220 fdflags(pe[0], 0, 0, FD_CLOEXEC, FD_CLOEXEC)) {
2221 tvec_error(tv, "pipe failed: %s", strerror(errno));
2225 /* Flush all of the stream buffers so that we don't get duplicated
2230 /* Try to set up the child process. */
2233 tvec_error(tv, "fork failed: %s", strerror(errno));
2238 /* Child process. */
2241 *infd_out = p0[0]; p0[0] = -1;
2242 *outfd_out = p1[1]; p1[1] = -1;
2243 if (pe[1] != STDERR_FILENO && dup2(pe[1], STDERR_FILENO) < 0) {
2244 fprintf(stderr, "failed to establish child stderr: %s",
2249 /* Parent process. */
2251 *kid_out = kid; kid = -1;
2252 *infd_out = p1[0]; p1[0] = -1;
2253 *outfd_out = p0[1]; p0[1] = -1;
2254 *errfd_out = pe[0]; pe[0] = -1;
2261 /* Clean up. So much of this... */
2262 if (p0[0] >= 0) close(p0[0]);
2263 if (p0[1] >= 0) close(p0[1]);
2264 if (p1[0] >= 0) close(p1[0]);
2265 if (p1[1] >= 0) close(p1[1]);
2266 if (pe[0] >= 0) close(pe[0]);
2267 if (pe[1] >= 0) close(pe[1]);
2271 /* --- @tvec_fork@ --- *
2273 * Arguments: @pid_t *kid_out@ = where to put child process-id
2274 * @int *infd_out, *outfd_out, *errfd_out@ = where to put file
2276 * @struct tvec_state *tv@ = test vector state
2277 * @const struct tvec_remoteenv@ = the remote environment
2279 * Returns: Zero on success, %$-1$% on failure.
2281 * Use: Starts a remote server running in a fork of the main
2282 * process. This is useful for testing functions which might --
2283 * or are even intended to -- crash.
2286 int tvec_fork(pid_t *kid_out, int *infd_out, int *outfd_out, int *errfd_out,
2287 struct tvec_state *tv, const struct tvec_remoteenv *env)
2289 struct tvec_config config;
2290 const struct tvec_remotefork *rf = (const struct tvec_remotefork *)env;
2292 int infd = -1, outfd = -1, errfd = -1;
2295 if (fork_common(&kid, &infd, &outfd, &errfd, tv)) { rc = -1; goto end; }
2297 if (tv->fp) fclose(tv->fp);
2298 config.tests = rf->f.tests ? rf->f.tests : tv->tests;
2299 config.nrout = tv->nrout; config.nreg = tv->nreg;
2300 config.regsz = tv->regsz;
2301 _exit(tvec_remoteserver(infd, outfd, &config));
2304 *kid_out = kid; *infd_out = infd; *outfd_out = outfd; *errfd_out = errfd;
2310 /* --- @tvec_exec@ --- *
2312 * Arguments: @pid_t *kid_out@ = where to put child process-id
2313 * @int *infd_out, *outfd_out, *errfd_out@ = where to put file
2315 * @struct tvec_state *tv@ = test vector state
2316 * @const struct tvec_remoteenv@ = the remote environment
2318 * Returns: Zero on success, %$-1$% on failure.
2320 * Use: Starts a remote server by running some program. The command
2321 * given in the environment description will probably some hairy
2322 * shell rune allowing for configuration via files or
2323 * environment variables.
2326 int tvec_exec(pid_t *kid_out, int *infd_out, int *outfd_out, int *errfd_out,
2327 struct tvec_state *tv, const struct tvec_remoteenv *env)
2329 const struct tvec_remoteexec *rx = (const struct tvec_remoteexec *)env;
2331 int infd = -1, outfd = -1, errfd = -1;
2335 if (fork_common(&kid, &infd, &outfd, &errfd, tv)) { rc = -1; goto end; }
2337 v[0].cur = infd; v[0].want = STDIN_FILENO;
2338 v[1].cur = outfd; v[1].want = STDOUT_FILENO;
2340 fprintf(stderr, "failed to establish standard file descriptors: %s",
2344 execvp(rx->x.args[0], (/*uncosnt*/ char *const *)rx->x.args);
2345 fprintf(stderr, "failed to invoke test runner: %s", strerror(errno));
2349 *kid_out = kid; *infd_out = infd; *outfd_out = outfd; *errfd_out = errfd;
2355 /*----- That's all, folks -------------------------------------------------*/