MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_subdaemons

Function test_subdaemons

lightningd/lightningd.c:444–507  ·  view source on GitHub ↗

~ Check we can run them, and check their versions */

Source from the content-addressed store, hash-verified

442
443/*~ Check we can run them, and check their versions */
444void test_subdaemons(const struct lightningd *ld)
445{
446 size_t i;
447
448 /*~ CCAN's ARRAY_SIZE() should always be used on defined arrays like
449 * the subdaemons array above. You can calculate the number of
450 * elements it has using `sizeof(subdaemons)/sizeof(subdaemons[0])`
451 * but if `subdaemons` were refactored into a pointer (eg. to make
452 * it a dynamic array) that would erroneously evaluate to `1`.
453 *
454 * ARRAY_SIZE will cause a compiler error if the argument is actually
455 * a pointer, not an array. */
456 for (i = 0; i < ARRAY_SIZE(subdaemons); i++) {
457 /*~ CCAN's path module uses tal, so wants a context to
458 * allocate from. We have a magic convenience context
459 * `tmpctx` for temporary allocations like this.
460 *
461 * Because all our daemons at their core are of form `while
462 * (!stopped) handle_events();` (an event loop pattern), we
463 * can free `tmpctx` in that top-level loop after each event
464 * is handled.
465 */
466 int outfd;
467 const char *dpath = subdaemon_path(tmpctx, ld, subdaemons[i]);
468 const char *verstring;
469 /*~ CCAN's pipecmd module is like popen for grownups: it
470 * takes pointers to fill in stdin, stdout and stderr file
471 * descriptors if desired, and the remainder of arguments
472 * are the command and its argument. */
473 pid_t pid = pipecmd(NULL, &outfd, &outfd,
474 dpath, "--version", NULL);
475
476 /*~ Our logging system: spam goes in at log_debug level, but
477 * logging is mainly added by developer necessity and removed
478 * by developer/user complaints. The only strong convention
479 * is that log_broken() is used for "should never happen".
480 *
481 * Note, however, that logging takes care to preserve the
482 * global `errno` which is set above. */
483 log_debug(ld->log, "testing %s", dpath);
484
485 /*~ ccan/err is a wrapper around BSD's err.h, which defines
486 * the convenience functions err() (error with message
487 * followed by a string based on errno) and errx() (same,x
488 * but no errno string). */
489 if (pid == -1)
490 err(EXITCODE_SUBDAEMON_FAIL, "Could not run %s", dpath);
491
492 /*~ CCAN's grab_file module contains a routine to read into a
493 * tallocated buffer until EOF (with nul appended!) */
494 verstring = grab_fd_str(tmpctx, outfd);
495 /*~ Like many CCAN modules, it set errno on failure, which
496 * err (ccan/err, but usually just the BSD <err.h>) prints */
497 if (!verstring)
498 err(1, "Could not get output from %s", dpath);
499 /*~ strstarts is from CCAN/str. */
500 if (!strstarts(verstring, version())
501 || verstring[strlen(version())] != '\n')

Callers 2

test_subdaemons_and_exitFunction · 0.85
mainFunction · 0.85

Calls 6

pipecmdFunction · 0.85
errFunction · 0.85
grab_fd_strFunction · 0.85
versionFunction · 0.85
errxFunction · 0.85
subdaemon_pathFunction · 0.70

Tested by

no test coverage detected