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

Function configfile_replace_var

lightningd/configs.c:411–463  ·  view source on GitHub ↗

This comments out the config file entry or maybe replace one */

Source from the content-addressed store, hash-verified

409
410/* This comments out the config file entry or maybe replace one */
411static void configfile_replace_var(struct lightningd *ld,
412 const struct configvar *cv,
413 const char *replace)
414{
415 char **lines, *template, *contents;
416 const char *err;
417 int outfd;
418
419 switch (cv->src) {
420 case CONFIGVAR_CMDLINE:
421 case CONFIGVAR_CMDLINE_SHORT:
422 case CONFIGVAR_PLUGIN_START:
423 case CONFIGVAR_SETCONFIG_TRANSIENT:
424 /* These can't be commented out */
425 abort();
426 case CONFIGVAR_EXPLICIT_CONF:
427 case CONFIGVAR_BASE_CONF:
428 case CONFIGVAR_NETWORK_CONF:
429 break;
430 }
431
432 /* If it changed *now*, that's fatal: we already set it locally! */
433 err = grab_and_check(tmpctx, cv->file, cv->linenum, cv->configline,
434 &lines);
435 if (err)
436 fatal("%s", err);
437
438 if (replace)
439 lines[cv->linenum - 1] = cast_const(char *, replace);
440 else
441 lines[cv->linenum - 1] = tal_fmt(lines, "# setconfig commented out (see config.setconfig): %s",
442 lines[cv->linenum - 1]);
443
444 log_info(ld->log, "setconfig: %s line %u of %s (%s)",
445 replace ? "replaced" : "commented out",
446 cv->linenum, cv->file, cv->configline);
447
448 template = tal_fmt(tmpctx, "%s.setconfig.XXXXXX", cv->file);
449 outfd = mkstemp(template);
450 if (outfd < 0)
451 fatal("Creating %s: %s", template, strerror(errno));
452
453 contents = tal_strjoin(tmpctx, take(lines), "\n", STR_TRAIL);
454 if (!write_all(outfd, contents, strlen(contents)))
455 fatal("Writing %s: %s", template, strerror(errno));
456 if (fsync(outfd) != 0)
457 fatal("Syncing %s: %s", template, strerror(errno));
458
459 if (rename(template, cv->file) != 0)
460 fatal("Renaming %s over %s: %s",
461 template, cv->file, strerror(errno));
462 close(outfd);
463}
464
465static const char *base_conf_file(const tal_t *ctx,
466 struct lightningd *ld,

Callers 2

configvar_saveFunction · 0.85
configvar_save_multiFunction · 0.85

Calls 5

abortFunction · 0.85
grab_and_checkFunction · 0.85
log_infoClass · 0.85
fatalFunction · 0.70
write_allFunction · 0.50

Tested by

no test coverage detected