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

Function configfile_replace_var

lightningd/configs.c:412–464  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

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