Skip to content

Commit

Permalink
opts: Split early from non-early args so plugins can register theirs
Browse files Browse the repository at this point in the history
The idea is that `plugin` is an early arg that is parsed (from command
line or the config file). We can then start the plugins and have them
tell us about the options they'd like to add to the mix, before we
actually parse them.

Signed-off-by: Christian Decker <@cdecker>
  • Loading branch information
cdecker committed Nov 12, 2018
1 parent 084224f commit 8e83d43
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 33 deletions.
3 changes: 0 additions & 3 deletions gossipd/test/run-bench-find_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,6 @@ bool fromwire_node_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDE
/* Generated stub for fromwire_peektype */
int fromwire_peektype(const u8 *cursor UNNEEDED)
{ fprintf(stderr, "fromwire_peektype called!\n"); abort(); }
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
Expand Down
3 changes: 0 additions & 3 deletions gossipd/test/run-find_route-specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ bool fromwire_node_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDE
/* Generated stub for fromwire_peektype */
int fromwire_peektype(const u8 *cursor UNNEEDED)
{ fprintf(stderr, "fromwire_peektype called!\n"); abort(); }
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
Expand Down
3 changes: 0 additions & 3 deletions gossipd/test/run-find_route.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ bool fromwire_node_announcement(const tal_t *ctx UNNEEDED, const void *p UNNEEDE
/* Generated stub for fromwire_peektype */
int fromwire_peektype(const u8 *cursor UNNEEDED)
{ fprintf(stderr, "fromwire_peektype called!\n"); abort(); }
/* Generated stub for fromwire_u8 */
u8 fromwire_u8(const u8 **cursor UNNEEDED, size_t *max UNNEEDED)
{ fprintf(stderr, "fromwire_u8 called!\n"); abort(); }
/* Generated stub for fromwire_wireaddr */
bool fromwire_wireaddr(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, struct wireaddr *addr UNNEEDED)
{ fprintf(stderr, "fromwire_wireaddr called!\n"); abort(); }
Expand Down
15 changes: 11 additions & 4 deletions lightningd/lightningd.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,20 @@ int main(int argc, char *argv[])
* mimic this API here, even though they're on separate lines.*/
register_opts(ld);

/*~ Handle early options, but don't move to --lightning-dir
* just yet. Plugins may add new options, which is why we are
* splitting between early args (including --plugin
* registration) and non-early opts. */
handle_early_opts(ld, argc, argv);

/*~ Initialize all the plugins we just registered, so they can
* do their thing and tell us about themselves (including
* options registration). */
plugins_init(ld->plugins);

/*~ Handle options and config; move to .lightningd (--lightning-dir) */
handle_opts(ld, argc, argv);

/*~ Initialize all the plugins we just registered, so they can do their
* thing and tell us about themselves */
plugins_init(ld->plugins);

/*~ Make sure we can reach the subdaemons, and versions match. */
test_subdaemons(ld);

Expand Down
57 changes: 37 additions & 20 deletions lightningd/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,8 +619,11 @@ static void config_log_stderr_exit(const char *fmt, ...)
fatal("%s", msg);
}

/* We turn the config file into cmdline arguments. */
static void opt_parse_from_config(struct lightningd *ld)
/**
* We turn the config file into cmdline arguments. @early tells us
* whether to parse early options only, or the non-early options.
*/
static void opt_parse_from_config(struct lightningd *ld, bool early)
{
char *contents, **lines;
char **all_args; /*For each line: either argument string or NULL*/
Expand Down Expand Up @@ -669,23 +672,28 @@ static void opt_parse_from_config(struct lightningd *ld)
argv[0] = "lightning config file";
argv[argc] = NULL;

for (i = 0; i < tal_count(all_args); i++) {
if(all_args[i] != NULL) {
config_parse_line_number = i + 1;
argv[1] = all_args[i];
opt_early_parse(argc, argv, config_log_stderr_exit);
if (early) {
for (i = 0; i < tal_count(all_args); i++) {
if (all_args[i] != NULL) {
config_parse_line_number = i + 1;
argv[1] = all_args[i];
opt_early_parse(argc, argv,
config_log_stderr_exit);
}
}
}

/* Now we can set up defaults, depending on whether testnet or not */
setup_default_config(ld);
/* Now we can set up defaults, depending on whether testnet or
* not */
setup_default_config(ld);
} else {

for (i = 0; i < tal_count(all_args); i++) {
if(all_args[i] != NULL) {
config_parse_line_number = i + 1;
argv[1] = all_args[i];
opt_parse(&argc, argv, config_log_stderr_exit);
argc = 2; /* opt_parse might have changed it */
for (i = 0; i < tal_count(all_args); i++) {
if (all_args[i] != NULL) {
config_parse_line_number = i + 1;
argv[1] = all_args[i];
opt_parse(&argc, argv, config_log_stderr_exit);
argc = 2; /* opt_parse might have changed it */
}
}
}

Expand Down Expand Up @@ -813,18 +821,22 @@ void setup_color_and_alias(struct lightningd *ld)
}
}

void handle_opts(struct lightningd *ld, int argc, char *argv[])
void handle_early_opts(struct lightningd *ld, int argc, char *argv[])
{
/* Load defaults. The actual values loaded here will be overwritten
* later by opt_parse_from_config. */
setup_default_config(ld);

/* Get any configdir/testnet options first. */
opt_early_parse(argc, argv, opt_log_stderr_exit);
opt_early_parse_incomplete(argc, argv, opt_log_stderr_exit);

/* Now look for config file */
opt_parse_from_config(ld);
/* Now look for config file, but only handle the early
* options, others may be added on-demand */
opt_parse_from_config(ld, true);
}

void handle_opts(struct lightningd *ld, int argc, char *argv[])
{
/* Move to config dir, to save ourselves the hassle of path manip. */
if (chdir(ld->config_dir) != 0) {
log_unusual(ld->log, "Creating configuration directory %s",
Expand All @@ -837,6 +849,11 @@ void handle_opts(struct lightningd *ld, int argc, char *argv[])
ld->config_dir, strerror(errno));
}

/* Now look for config file, but only handle non-early
* options, early ones have been parsed in
* handle_early_opts */
opt_parse_from_config(ld, false);

opt_parse(&argc, argv, opt_log_stderr_exit);
if (argc != 1)
errx(1, "no arguments accepted");
Expand Down
3 changes: 3 additions & 0 deletions lightningd/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ struct lightningd;
void register_opts(struct lightningd *ld);

/* After this, we're in the .lightning dir, config files parsed. */
void handle_early_opts(struct lightningd *ld, int argc, char *argv[]);

/* After this we've parsed all options */
void handle_opts(struct lightningd *ld, int argc, char *argv[]);

/* Derive default color and alias from the pubkey. */
Expand Down
3 changes: 3 additions & 0 deletions lightningd/test/run-find_my_abspath.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ struct log_book *get_log_book(const struct log *log UNNEEDED)
/* Generated stub for gossip_init */
void gossip_init(struct lightningd *ld UNNEEDED, int connectd_fd UNNEEDED)
{ fprintf(stderr, "gossip_init called!\n"); abort(); }
/* Generated stub for handle_early_opts */
void handle_early_opts(struct lightningd *ld UNNEEDED, int argc UNNEEDED, char *argv[])
{ fprintf(stderr, "handle_early_opts called!\n"); abort(); }
/* Generated stub for handle_opts */
void handle_opts(struct lightningd *ld UNNEEDED, int argc UNNEEDED, char *argv[])
{ fprintf(stderr, "handle_opts called!\n"); abort(); }
Expand Down

0 comments on commit 8e83d43

Please sign in to comment.