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

Function test_subdaemons

lightningd/lightningd.c:442–505  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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