From 885126174d32a2d565619c9907fdd8fc08b4eb3e Mon Sep 17 00:00:00 2001 From: Ariadne Conill Date: Wed, 9 Sep 2020 14:18:16 -0600 Subject: [PATCH] ifupdown: consistently update the state file when exiting --- cmd/ifupdown.c | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/cmd/ifupdown.c b/cmd/ifupdown.c index 6405cbf..bc8b657 100644 --- a/cmd/ifupdown.c +++ b/cmd/ifupdown.c @@ -186,6 +186,27 @@ change_auto_interfaces(struct lif_dict *collection, struct lif_dict *state, stru return true; } +int +update_state_file_and_exit(int rc, struct lif_dict *state) +{ + if (exec_opts.mock) + { + exit(rc); + return rc; + } + + if (!lif_state_write_path(state, exec_opts.state_file)) + { + fprintf(stderr, "%s: could not update %s\n", argv0, exec_opts.state_file); + + exit(EXIT_FAILURE); + return EXIT_FAILURE; + } + + exit(rc); + return rc; +} + int ifupdown_main(int argc, char *argv[]) { @@ -217,9 +238,9 @@ ifupdown_main(int argc, char *argv[]) if (match_opts.is_auto) { if (!change_auto_interfaces(&collection, &state, &match_opts)) - return EXIT_FAILURE; + return update_state_file_and_exit(EXIT_FAILURE, &state); - return EXIT_SUCCESS; + return update_state_file_and_exit(EXIT_SUCCESS, &state); } else if (optind >= argc) generic_usage(self_applet, EXIT_FAILURE); @@ -248,23 +269,17 @@ ifupdown_main(int argc, char *argv[]) if (entry == NULL) { fprintf(stderr, "%s: unknown interface %s\n", argv0, argv[idx]); - return EXIT_FAILURE; + return update_state_file_and_exit(EXIT_FAILURE, &state); } iface = entry->data; } if (!change_interface(iface, &collection, &state, ifname)) - return EXIT_FAILURE; + return update_state_file_and_exit(EXIT_FAILURE, &state); } - if (!exec_opts.mock && !lif_state_write_path(&state, exec_opts.state_file)) - { - fprintf(stderr, "%s: could not update %s\n", argv0, exec_opts.state_file); - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; + return update_state_file_and_exit(EXIT_SUCCESS, &state); } struct if_applet ifup_applet = {