From 5775df9a6dcfaa4a9d208c8e78d6a7419c0e6a07 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 20:09:46 +0100 Subject: [PATCH] main: refactor early setup in main() Move call to nm_main_utils_early_setup() to a separate function. Also move the @options array away from the main function, saving a few bytes on the stack. Now only do_early_setup() modifies the @global_opt structure. --- src/main.c | 58 ++++++++++++++++++--------------- src/nm-iface-helper.c | 75 +++++++++++++++++++++++++------------------ 2 files changed, 75 insertions(+), 58 deletions(-) diff --git a/src/main.c b/src/main.c index a1747f2e59..c06c746ddf 100644 --- a/src/main.c +++ b/src/main.c @@ -211,6 +211,37 @@ manager_configure_quit (NMManager *manager, gpointer user_data) configure_and_quit = TRUE; } +static void +do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) +{ + GOptionEntry options[] = { + { "version", 'V', 0, G_OPTION_ARG_NONE, &global_opt.show_version, N_("Print NetworkManager version and exit"), NULL }, + { "no-daemon", 'n', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &global_opt.become_daemon, N_("Don't become a daemon"), NULL }, + { "debug", 'd', 0, G_OPTION_ARG_NONE, &global_opt.debug, N_("Don't become a daemon, and log to stderr"), NULL }, + { "log-level", 0, 0, G_OPTION_ARG_STRING, &global_opt.opt_log_level, N_("Log level: one of [%s]"), "INFO" }, + { "log-domains", 0, 0, G_OPTION_ARG_STRING, &global_opt.opt_log_domains, + N_("Log domains separated by ',': any combination of [%s]"), + "PLATFORM,RFKILL,WIFI" }, + { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &global_opt.g_fatal_warnings, N_("Make all warnings fatal"), NULL }, + { "pid-file", 'p', 0, G_OPTION_ARG_FILENAME, &global_opt.pidfile, N_("Specify the location of a PID file"), N_("filename") }, + { "state-file", 0, 0, G_OPTION_ARG_FILENAME, &global_opt.state_file, N_("State file location"), N_("/path/to/state.file") }, + { "run-from-build-dir", 0, 0, G_OPTION_ARG_NONE, &global_opt.run_from_build_dir, "Run from build directory", NULL }, + {NULL} + }; + + if (!nm_main_utils_early_setup ("NetworkManager", + argc, + argv, + options, + (void (*)(gpointer, GOptionContext *)) nm_config_cmd_line_options_add_to_entries, + config_cli, + _("NetworkManager monitors all network connections and automatically\nchooses the best connection to use. It also allows the user to\nspecify wireless access points which wireless cards in the computer\nshould associate with."))) + exit (1); + + global_opt.pidfile = global_opt.pidfile ? global_opt.pidfile : g_strdup (NM_DEFAULT_PID_FILE); + global_opt.state_file = global_opt.state_file ? global_opt.state_file : g_strdup (NM_DEFAULT_SYSTEM_STATE_FILE); +} + /* * main * @@ -228,34 +259,12 @@ main (int argc, char *argv[]) char *bad_domains = NULL; NMConfigCmdLineOptions *config_cli; - GOptionEntry options[] = { - { "version", 'V', 0, G_OPTION_ARG_NONE, &global_opt.show_version, N_("Print NetworkManager version and exit"), NULL }, - { "no-daemon", 'n', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &global_opt.become_daemon, N_("Don't become a daemon"), NULL }, - { "debug", 'd', 0, G_OPTION_ARG_NONE, &global_opt.debug, N_("Don't become a daemon, and log to stderr"), NULL }, - { "log-level", 0, 0, G_OPTION_ARG_STRING, &global_opt.opt_log_level, N_("Log level: one of [%s]"), "INFO" }, - { "log-domains", 0, 0, G_OPTION_ARG_STRING, &global_opt.opt_log_domains, - N_("Log domains separated by ',': any combination of [%s]"), - "PLATFORM,RFKILL,WIFI" }, - { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &global_opt.g_fatal_warnings, N_("Make all warnings fatal"), NULL }, - { "pid-file", 'p', 0, G_OPTION_ARG_FILENAME, &global_opt.pidfile, N_("Specify the location of a PID file"), N_("filename") }, - { "state-file", 0, 0, G_OPTION_ARG_FILENAME, &global_opt.state_file, N_("State file location"), N_("/path/to/state.file") }, - { "run-from-build-dir", 0, 0, G_OPTION_ARG_NONE, &global_opt.run_from_build_dir, "Run from build directory", NULL }, - {NULL} - }; - _nm_utils_is_manager_process = TRUE; main_loop = g_main_loop_new (NULL, FALSE); config_cli = nm_config_cmd_line_options_new (); - if (!nm_main_utils_early_setup ("NetworkManager", - &argc, - &argv, - options, - (void (*)(gpointer, GOptionContext *)) nm_config_cmd_line_options_add_to_entries, - config_cli, - _("NetworkManager monitors all network connections and automatically\nchooses the best connection to use. It also allows the user to\nspecify wireless access points which wireless cards in the computer\nshould associate with."))) - exit (1); + do_early_setup (&argc, &argv, config_cli); if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); @@ -307,9 +316,6 @@ main (int argc, char *argv[]) exit (1); } - global_opt.pidfile = global_opt.pidfile ? global_opt.pidfile : g_strdup (NM_DEFAULT_PID_FILE); - global_opt.state_file = global_opt.state_file ? global_opt.state_file : g_strdup (NM_DEFAULT_SYSTEM_STATE_FILE); - /* check pid file */ if (nm_main_utils_check_pidfile (global_opt.pidfile, "NetworkManager")) exit (1); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index eb376b5e6d..4125ba5012 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -52,8 +52,6 @@ static GMainLoop *main_loop = NULL; static int ifindex = -1; -static guint32 priority_v4 = NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP4; -static guint32 priority_v6 = NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6; static struct { gboolean slaac; @@ -72,12 +70,12 @@ static struct { char *iid_str; char *opt_log_level; char *opt_log_domains; - gint64 priority64_v4; - gint64 priority64_v6; + guint32 priority_v4; + guint32 priority_v6; } global_opt = { .tempaddr = NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN, - .priority64_v4 = -1, - .priority64_v6 = -1, + .priority_v4 = NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP4, + .priority_v6 = NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6, }; static void @@ -102,7 +100,7 @@ dhcp4_state_changed (NMDhcpClient *client, nm_ip4_config_subtract (existing, last_config); nm_ip4_config_merge (existing, ip4_config); - if (!nm_ip4_config_commit (existing, ifindex, priority_v4)) + if (!nm_ip4_config_commit (existing, ifindex, global_opt.priority_v4)) nm_log_warn (LOGD_DHCP4, "(%s): failed to apply DHCPv4 config", global_opt.ifname); if (last_config) { @@ -214,7 +212,7 @@ rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, gpointer user_da route.plen = discovered_route->plen; route.gateway = discovered_route->gateway; route.source = NM_IP_CONFIG_SOURCE_RDISC; - route.metric = priority_v6; + route.metric = global_opt.priority_v6; nm_ip6_config_add_route (ip6_config, &route); } @@ -281,20 +279,11 @@ setup_signals (void) g_unix_signal_add (SIGTERM, quit_handler, NULL); } -int -main (int argc, char *argv[]) +static void +do_early_setup (int *argc, char **argv[]) { - char *bad_domains = NULL; - GError *error = NULL; - gboolean wrote_pidfile = FALSE; - gs_free char *pidfile = NULL; - gs_unref_object NMDhcpClient *dhcp4_client = NULL; - gs_unref_object NMRDisc *rdisc = NULL; - GByteArray *hwaddr = NULL; - size_t hwaddr_len = 0; - gconstpointer tmp; - gs_free NMUtilsIPv6IfaceId *iid = NULL; - + gint64 priority64_v4 = -1; + gint64 priority64_v6 = -1; GOptionEntry options[] = { /* Interface/IP config */ { "ifname", 'i', 0, G_OPTION_ARG_STRING, &global_opt.ifname, N_("The interface to manage"), N_("eth0") }, @@ -306,8 +295,8 @@ main (int argc, char *argv[]) { "dhcp4-required", '4', 0, G_OPTION_ARG_NONE, &global_opt.dhcp4_required, N_("Whether DHCPv4 must be successful"), NULL }, { "dhcp4-clientid", 'c', 0, G_OPTION_ARG_STRING, &global_opt.dhcp4_clientid, N_("Hex-encoded DHCPv4 client ID"), NULL }, { "dhcp4-hostname", 'h', 0, G_OPTION_ARG_STRING, &global_opt.dhcp4_hostname, N_("Hostname to send to DHCP server"), N_("barbar") }, - { "priority4", '\0', 0, G_OPTION_ARG_INT64, &global_opt.priority64_v4, N_("Route priority for IPv4"), N_("0") }, - { "priority6", '\0', 0, G_OPTION_ARG_INT64, &global_opt.priority64_v6, N_("Route priority for IPv6"), N_("1024") }, + { "priority4", '\0', 0, G_OPTION_ARG_INT64, &priority64_v4, N_("Route priority for IPv4"), N_("0") }, + { "priority6", '\0', 0, G_OPTION_ARG_INT64, &priority64_v6, N_("Route priority for IPv6"), N_("1024") }, { "iid", 'e', 0, G_OPTION_ARG_STRING, &global_opt.iid_str, N_("Hex-encoded Interface Identifier"), N_("") }, /* Logging/debugging */ @@ -325,14 +314,42 @@ main (int argc, char *argv[]) setpgid (getpid (), getpid ()); if (!nm_main_utils_early_setup ("nm-iface-helper", - &argc, - &argv, + argc, + argv, options, NULL, NULL, _("nm-iface-helper is a small, standalone process that manages a single network interface."))) exit (1); + if (priority64_v4 >= 0 && priority64_v4 <= G_MAXUINT32) + global_opt.priority_v4 = (guint32) priority64_v4; + if (priority64_v6 >= 0 && priority64_v6 <= G_MAXUINT32) + global_opt.priority_v6 = (guint32) priority64_v6; +} + +int +main (int argc, char *argv[]) +{ + char *bad_domains = NULL; + GError *error = NULL; + gboolean wrote_pidfile = FALSE; + gs_free char *pidfile = NULL; + gs_unref_object NMDhcpClient *dhcp4_client = NULL; + gs_unref_object NMRDisc *rdisc = NULL; + GByteArray *hwaddr = NULL; + size_t hwaddr_len = 0; + gconstpointer tmp; + gs_free NMUtilsIPv6IfaceId *iid = NULL; + +#if !GLIB_CHECK_VERSION (2, 35, 0) + g_type_init (); +#endif + + setpgid (getpid (), getpid ()); + + do_early_setup (&argc, &argv); + if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); exit (0); @@ -426,12 +443,6 @@ main (int argc, char *argv[]) iid = g_bytes_unref_to_data (bytes, &ignored); } - if (global_opt.priority64_v4 >= 0 && global_opt.priority64_v4 <= G_MAXUINT32) - priority_v4 = (guint32) global_opt.priority64_v4; - - if (global_opt.priority64_v6 >= 0 && global_opt.priority64_v6 <= G_MAXUINT32) - priority_v6 = (guint32) global_opt.priority64_v6; - if (global_opt.dhcp4_address) { nm_platform_sysctl_set (nm_utils_ip4_property_path (global_opt.ifname, "promote_secondaries"), "1"); @@ -440,7 +451,7 @@ main (int argc, char *argv[]) ifindex, hwaddr, global_opt.uuid, - priority_v4, + global_opt.priority_v4, !!global_opt.dhcp4_hostname, global_opt.dhcp4_hostname, global_opt.dhcp4_clientid,