From 126723f94de794284294b53af92a683358907ecc Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 20:47:03 +0100 Subject: [PATCH 01/17] main/trivial: change order of arguments for nm_main_utils_early_setup() Change the order of the argv and argc argument to match the main() function. (cherry picked from commit ee4543bd6e4958e93d19a878f8403c4afa398c6e) --- src/main-utils.c | 2 +- src/main-utils.h | 2 +- src/main.c | 2 +- src/nm-iface-helper.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main-utils.c b/src/main-utils.c index 0fa3644f3e..ea11ed767c 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -216,8 +216,8 @@ done: gboolean nm_main_utils_early_setup (const char *progname, - char **argv[], int *argc, + char **argv[], GOptionEntry *options, GOptionEntry *more_options, const char *summary) diff --git a/src/main-utils.h b/src/main-utils.h index 472fa5e70e..e33935562a 100644 --- a/src/main-utils.h +++ b/src/main-utils.h @@ -30,8 +30,8 @@ gboolean nm_main_utils_write_pidfile (const char *pidfile); gboolean nm_main_utils_check_pidfile (const char *pidfile, const char *name); gboolean nm_main_utils_early_setup (const char *progname, - char **argv[], int *argc, + char **argv[], GOptionEntry *options, GOptionEntry *more_options, const char *summary); diff --git a/src/main.c b/src/main.c index 7518e7011e..691f0b869c 100644 --- a/src/main.c +++ b/src/main.c @@ -237,8 +237,8 @@ main (int argc, char *argv[]) main_loop = g_main_loop_new (NULL, FALSE); if (!nm_main_utils_early_setup ("NetworkManager", - &argv, &argc, + &argv, options, nm_config_get_options (), _("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."))) diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 9d5cdc05cf..e83bc49790 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -321,8 +321,8 @@ main (int argc, char *argv[]) setpgid (getpid (), getpid ()); if (!nm_main_utils_early_setup ("nm-iface-helper", - &argv, &argc, + &argv, options, NULL, _("nm-iface-helper is a small, standalone process that manages a single network interface."))) From 9cc54f43bc54e9225e2e5f27674620d82a92b41c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 19 Mar 2015 16:28:59 +0100 Subject: [PATCH 02/17] main-utils: don't leak description for command line arguments in nm_main_utils_early_setup() (cherry picked from commit b5ca5bd7b7e8894450786c0874a47620e777d3bb) --- src/main-utils.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main-utils.c b/src/main-utils.c index ea11ed767c..90bc7e0cdb 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -226,6 +226,8 @@ nm_main_utils_early_setup (const char *progname, GError *error = NULL; gboolean success = FALSE; int i; + const char *opt_fmt_log_level = NULL, *opt_fmt_log_domains = NULL; + const char **opt_loc_log_level = NULL, **opt_loc_log_domains = NULL; /* Make GIO ignore the remote VFS service; otherwise it tries to use the * session bus to contact the remote service, and NM shouldn't ever be @@ -250,10 +252,15 @@ nm_main_utils_early_setup (const char *progname, } for (i = 0; options[i].long_name; i++) { - if (!strcmp (options[i].long_name, "log-level")) + if (!strcmp (options[i].long_name, "log-level")) { + opt_fmt_log_level = options[i].description; + opt_loc_log_level = &options[i].description; options[i].description = g_strdup_printf (options[i].description, nm_logging_all_levels_to_string ()); - else if (!strcmp (options[i].long_name, "log-domains")) + } else if (!strcmp (options[i].long_name, "log-domains")) { + opt_fmt_log_domains = options[i].description; + opt_loc_log_domains = &options[i].description; options[i].description = g_strdup_printf (options[i].description, nm_logging_all_domains_to_string ()); + } } /* Parse options */ @@ -274,6 +281,15 @@ nm_main_utils_early_setup (const char *progname, } g_option_context_free (opt_ctx); + if (opt_loc_log_level) { + g_free ((char *) *opt_loc_log_level); + *opt_loc_log_level = opt_fmt_log_level; + } + if (opt_loc_log_domains) { + g_free ((char *) *opt_loc_log_domains); + *opt_loc_log_domains = opt_fmt_log_domains; + } + return success; } From 8295b75742498d97da8850c17f3918b0848025ed Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 19:59:32 +0100 Subject: [PATCH 03/17] main: move option variables to static variable Move the variables to a static struct so that we can factor out some of the initialization code. Also it's nice to have all options placed together in one struct so that is is obvious which static variables are part of the command line options, and which have other use. (cherry picked from commit 1fc5aba42a175c0eca0b6d8804d6ee9088c1dfe5) Conflicts: src/nm-iface-helper.c --- src/main.c | 75 ++++++++++--------- src/nm-iface-helper.c | 162 +++++++++++++++++++++++------------------- 2 files changed, 129 insertions(+), 108 deletions(-) diff --git a/src/main.c b/src/main.c index 691f0b869c..27095f2c3f 100644 --- a/src/main.c +++ b/src/main.c @@ -71,6 +71,20 @@ static GMainLoop *main_loop = NULL; static gboolean quit_early = FALSE; +static struct { + gboolean show_version; + gboolean become_daemon; + gboolean debug; + gboolean g_fatal_warnings; + gboolean run_from_build_dir; + char *opt_log_level; + char *opt_log_domains; + char *pidfile; + char *state_file; +} global_opt = { + .become_daemon = TRUE, +}; + static gboolean parse_state_file (const char *filename, gboolean *net_enabled, @@ -195,15 +209,8 @@ manager_configure_quit (NMManager *manager, gpointer user_data) int main (int argc, char *argv[]) { - char *opt_log_level = NULL; - char *opt_log_domains = NULL; - gboolean become_daemon = TRUE, run_from_build_dir = FALSE; - gboolean debug = FALSE; - gboolean g_fatal_warnings = FALSE; - gs_free char *pidfile = NULL; - gs_free char *state_file = NULL; gboolean wifi_enabled = TRUE, net_enabled = TRUE, wwan_enabled = TRUE, wimax_enabled = TRUE; - gboolean success, show_version = FALSE; + gboolean success; NMManager *manager = NULL; gs_unref_object NMVpnManager *vpn_manager = NULL; gs_unref_object NMDnsManager *dns_mgr = NULL; @@ -218,17 +225,17 @@ main (int argc, char *argv[]) char *bad_domains = NULL; GOptionEntry options[] = { - { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Print NetworkManager version and exit"), NULL }, - { "no-daemon", 'n', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &become_daemon, N_("Don't become a daemon"), NULL }, - { "debug", 'd', 0, G_OPTION_ARG_NONE, &debug, N_("Don't become a daemon, and log to stderr"), NULL }, - { "log-level", 0, 0, G_OPTION_ARG_STRING, &opt_log_level, N_("Log level: one of [%s]"), "INFO" }, - { "log-domains", 0, 0, G_OPTION_ARG_STRING, &opt_log_domains, + { "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, &g_fatal_warnings, N_("Make all warnings fatal"), NULL }, - { "pid-file", 'p', 0, G_OPTION_ARG_FILENAME, &pidfile, N_("Specify the location of a PID file"), N_("filename") }, - { "state-file", 0, 0, G_OPTION_ARG_FILENAME, &state_file, N_("State file location"), N_("/path/to/state.file") }, - { "run-from-build-dir", 0, 0, G_OPTION_ARG_NONE, &run_from_build_dir, "Run from build directory", NULL }, + { "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} }; @@ -244,13 +251,13 @@ main (int argc, char *argv[]) _("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); - if (show_version) { + if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); exit (0); } - if (!nm_logging_setup (opt_log_level, - opt_log_domains, + if (!nm_logging_setup (global_opt.opt_log_level, + global_opt.opt_log_domains, &bad_domains, &error)) { fprintf (stderr, @@ -266,7 +273,7 @@ main (int argc, char *argv[]) /* When running from the build directory, determine our build directory * base and set helper paths in the build tree */ - if (run_from_build_dir) { + if (global_opt.run_from_build_dir) { char *path, *slash; int g; @@ -294,11 +301,11 @@ main (int argc, char *argv[]) exit (1); } - pidfile = pidfile ? pidfile : g_strdup (NM_DEFAULT_PID_FILE); - state_file = state_file ? state_file : g_strdup (NM_DEFAULT_SYSTEM_STATE_FILE); + 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 (pidfile, "NetworkManager")) + if (nm_main_utils_check_pidfile (global_opt.pidfile, "NetworkManager")) exit (1); /* Read the config file and CLI overrides */ @@ -313,7 +320,7 @@ main (int argc, char *argv[]) /* Initialize logging from config file *only* if not explicitly * specified by commandline. */ - if (opt_log_level == NULL && opt_log_domains == NULL) { + if (global_opt.opt_log_level == NULL && global_opt.opt_log_domains == NULL) { if (!nm_logging_setup (nm_config_get_log_level (config), nm_config_get_log_domains (config), &bad_domains, @@ -330,16 +337,16 @@ main (int argc, char *argv[]) } /* Parse the state file */ - if (!parse_state_file (state_file, &net_enabled, &wifi_enabled, &wwan_enabled, &wimax_enabled, &error)) { + if (!parse_state_file (global_opt.state_file, &net_enabled, &wifi_enabled, &wwan_enabled, &wimax_enabled, &error)) { fprintf (stderr, _("State file %s parsing failed: (%d) %s\n"), - state_file, + global_opt.state_file, error ? error->code : -1, (error && error->message) ? error->message : _("unknown")); /* Not a hard failure */ } g_clear_error (&error); - if (become_daemon && !debug) { + if (global_opt.become_daemon && !global_opt.debug) { if (daemon (0, 0) < 0) { int saved_errno; @@ -349,7 +356,7 @@ main (int argc, char *argv[]) saved_errno); exit (1); } - wrote_pidfile = nm_main_utils_write_pidfile (pidfile); + wrote_pidfile = nm_main_utils_write_pidfile (global_opt.pidfile); } _init_nm_debug (nm_config_get_debug (config)); @@ -358,7 +365,7 @@ main (int argc, char *argv[]) if (!nm_main_utils_setup_signals (main_loop, &quit_early)) exit (1); - if (g_fatal_warnings) { + if (global_opt.g_fatal_warnings) { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); @@ -366,7 +373,7 @@ main (int argc, char *argv[]) g_log_set_always_fatal (fatal_mask); } - nm_logging_syslog_openlog (debug); + nm_logging_syslog_openlog (global_opt.debug); #if !GLIB_CHECK_VERSION (2, 35, 0) g_type_init (); @@ -416,7 +423,7 @@ main (int argc, char *argv[]) } manager = nm_manager_new (settings, - state_file, + global_opt.state_file, net_enabled, wifi_enabled, wwan_enabled, @@ -485,8 +492,8 @@ done: nm_logging_syslog_closelog (); - if (pidfile && wrote_pidfile) - unlink (pidfile); + if (global_opt.pidfile && wrote_pidfile) + unlink (global_opt.pidfile); nm_log_info (LOGD_CORE, "exiting (%s)", success ? "success" : "error"); exit (success ? 0 : 1); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index e83bc49790..d4d38314cd 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -51,14 +51,35 @@ #define NMIH_PID_FILE_FMT NMRUNDIR "/nm-iface-helper-%d.pid" static GMainLoop *main_loop = NULL; -static char *ifname = NULL; static int ifindex = -1; -static gboolean slaac_required = FALSE; -static gboolean dhcp4_required = FALSE; -static int tempaddr = NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN; static guint32 priority_v4 = NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP4; static guint32 priority_v6 = NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6; +static struct { + gboolean slaac; + gboolean show_version; + gboolean become_daemon; + gboolean debug; + gboolean g_fatal_warnings; + gboolean slaac_required; + gboolean dhcp4_required; + int tempaddr; + char *ifname; + char *uuid; + char *dhcp4_address; + char *dhcp4_clientid; + char *dhcp4_hostname; + char *iid_str; + char *opt_log_level; + char *opt_log_domains; + gint64 priority64_v4; + gint64 priority64_v6; +} global_opt = { + .tempaddr = NM_SETTING_IP6_CONFIG_PRIVACY_UNKNOWN, + .priority64_v4 = -1, + .priority64_v6 = -1, +}; + static void dhcp4_state_changed (NMDhcpClient *client, NMDhcpState state, @@ -71,7 +92,7 @@ dhcp4_state_changed (NMDhcpClient *client, g_return_if_fail (!ip4_config || NM_IS_IP4_CONFIG (ip4_config)); - nm_log_dbg (LOGD_DHCP4, "(%s): new DHCPv4 client state %d", ifname, state); + nm_log_dbg (LOGD_DHCP4, "(%s): new DHCPv4 client state %d", global_opt.ifname, state); switch (state) { case NM_DHCP_STATE_BOUND: @@ -82,7 +103,7 @@ dhcp4_state_changed (NMDhcpClient *client, nm_ip4_config_merge (existing, ip4_config); if (!nm_ip4_config_commit (existing, ifindex, priority_v4)) - nm_log_warn (LOGD_DHCP4, "(%s): failed to apply DHCPv4 config", ifname); + nm_log_warn (LOGD_DHCP4, "(%s): failed to apply DHCPv4 config", global_opt.ifname); if (last_config) { g_object_unref (last_config); @@ -93,11 +114,11 @@ dhcp4_state_changed (NMDhcpClient *client, case NM_DHCP_STATE_TIMEOUT: case NM_DHCP_STATE_DONE: case NM_DHCP_STATE_FAIL: - if (dhcp4_required) { - nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 timed out or failed, quitting...", ifname); + if (global_opt.dhcp4_required) { + nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 timed out or failed, quitting...", global_opt.ifname); g_main_loop_quit (main_loop); } else - nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 timed out or failed", ifname); + nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 timed out or failed", global_opt.ifname); break; default: break; @@ -128,8 +149,8 @@ rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, gpointer user_da if (system_support) ifa_flags = IFA_F_NOPREFIXROUTE; - if (tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR - || tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR) + if (global_opt.tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_TEMP_ADDR + || global_opt.tempaddr == NM_SETTING_IP6_CONFIG_PRIVACY_PREFER_PUBLIC_ADDR) { /* without system_support, this flag will be ignored. Still set it, doesn't seem to do any harm. */ ifa_flags |= IFA_F_MANAGETEMPADDR; @@ -210,23 +231,23 @@ rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, gpointer user_da char val[16]; g_snprintf (val, sizeof (val), "%d", rdisc->hop_limit); - nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "hop_limit"), val); + nm_platform_sysctl_set (nm_utils_ip6_property_path (global_opt.ifname, "hop_limit"), val); } if (changed & NM_RDISC_CONFIG_MTU) { char val[16]; g_snprintf (val, sizeof (val), "%d", rdisc->mtu); - nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "mtu"), val); + nm_platform_sysctl_set (nm_utils_ip6_property_path (global_opt.ifname, "mtu"), val); } - existing = nm_ip6_config_capture (ifindex, FALSE, tempaddr); + existing = nm_ip6_config_capture (ifindex, FALSE, global_opt.tempaddr); if (last_config) nm_ip6_config_subtract (existing, last_config); nm_ip6_config_merge (existing, ip6_config); if (!nm_ip6_config_commit (existing, ifindex)) - nm_log_warn (LOGD_IP6, "(%s): failed to apply IPv6 config", ifname); + nm_log_warn (LOGD_IP6, "(%s): failed to apply IPv6 config", global_opt.ifname); if (last_config) { g_object_unref (last_config); @@ -238,11 +259,11 @@ rdisc_config_changed (NMRDisc *rdisc, NMRDiscConfigMap changed, gpointer user_da static void rdisc_ra_timeout (NMRDisc *rdisc, gpointer user_data) { - if (slaac_required) { - nm_log_warn (LOGD_IP6, "(%s): IPv6 timed out or failed, quitting...", ifname); + if (global_opt.slaac_required) { + nm_log_warn (LOGD_IP6, "(%s): IPv6 timed out or failed, quitting...", global_opt.ifname); g_main_loop_quit (main_loop); } else - nm_log_warn (LOGD_IP6, "(%s): IPv6 timed out or failed", ifname); + nm_log_warn (LOGD_IP6, "(%s): IPv6 timed out or failed", global_opt.ifname); } static gboolean @@ -271,12 +292,7 @@ setup_signals (gboolean *quit_early_ptr) int main (int argc, char *argv[]) { - char *opt_log_level = NULL; - char *opt_log_domains = NULL; - gboolean debug = FALSE, g_fatal_warnings = FALSE, become_daemon = FALSE; - gboolean show_version = FALSE, slaac = FALSE; - char *bad_domains = NULL, *dhcp4_hostname = NULL, *uuid = NULL; - char *iid_str = NULL, *dhcp4_clientid = NULL, *dhcp4_address = NULL; + char *bad_domains = NULL; gs_unref_object NMDhcpManager *dhcp_mgr = NULL; GError *error = NULL; gboolean wrote_pidfile = FALSE; @@ -288,33 +304,31 @@ main (int argc, char *argv[]) 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, &ifname, N_("The interface to manage"), N_("eth0") }, - { "uuid", 'u', 0, G_OPTION_ARG_STRING, &uuid, N_("Connection UUID"), N_("661e8cd0-b618-46b8-9dc9-31a52baaa16b") }, - { "slaac", 's', 0, G_OPTION_ARG_NONE, &slaac, N_("Whether to manage IPv6 SLAAC"), NULL }, - { "slaac-required", '6', 0, G_OPTION_ARG_NONE, &slaac_required, N_("Whether SLAAC must be successful"), NULL }, - { "slaac-tempaddr", 't', 0, G_OPTION_ARG_INT, &tempaddr, N_("Use an IPv6 temporary privacy address"), NULL }, - { "dhcp4", 'd', 0, G_OPTION_ARG_STRING, &dhcp4_address, N_("Current DHCPv4 address"), NULL }, - { "dhcp4-required", '4', 0, G_OPTION_ARG_NONE, &dhcp4_required, N_("Whether DHCPv4 must be successful"), NULL }, - { "dhcp4-clientid", 'c', 0, G_OPTION_ARG_STRING, &dhcp4_clientid, N_("Hex-encoded DHCPv4 client ID"), NULL }, - { "dhcp4-hostname", 'h', 0, G_OPTION_ARG_STRING, &dhcp4_hostname, N_("Hostname to send to DHCP server"), N_("barbar") }, - { "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, &iid_str, N_("Hex-encoded Interface Identifier"), N_("") }, + { "ifname", 'i', 0, G_OPTION_ARG_STRING, &global_opt.ifname, N_("The interface to manage"), N_("eth0") }, + { "uuid", 'u', 0, G_OPTION_ARG_STRING, &global_opt.uuid, N_("Connection UUID"), N_("661e8cd0-b618-46b8-9dc9-31a52baaa16b") }, + { "slaac", 's', 0, G_OPTION_ARG_NONE, &global_opt.slaac, N_("Whether to manage IPv6 SLAAC"), NULL }, + { "slaac-required", '6', 0, G_OPTION_ARG_NONE, &global_opt.slaac_required, N_("Whether SLAAC must be successful"), NULL }, + { "slaac-tempaddr", 't', 0, G_OPTION_ARG_INT, &global_opt.tempaddr, N_("Use an IPv6 temporary privacy address"), NULL }, + { "dhcp4", 'd', 0, G_OPTION_ARG_STRING, &global_opt.dhcp4_address, N_("Current DHCPv4 address"), NULL }, + { "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") }, + { "iid", 'e', 0, G_OPTION_ARG_STRING, &global_opt.iid_str, N_("Hex-encoded Interface Identifier"), N_("") }, /* Logging/debugging */ - { "version", 'V', 0, G_OPTION_ARG_NONE, &show_version, N_("Print NetworkManager version and exit"), NULL }, - { "no-daemon", 'n', G_OPTION_FLAG_REVERSE, G_OPTION_ARG_NONE, &become_daemon, N_("Don't become a daemon"), NULL }, - { "debug", 'b', 0, G_OPTION_ARG_NONE, &debug, N_("Don't become a daemon, and log to stderr"), NULL }, - { "log-level", 0, 0, G_OPTION_ARG_STRING, &opt_log_level, N_("Log level: one of [%s]"), "INFO" }, - { "log-domains", 0, 0, G_OPTION_ARG_STRING, &opt_log_domains, + { "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", 'b', 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, &g_fatal_warnings, N_("Make all warnings fatal"), NULL }, + { "g-fatal-warnings", 0, 0, G_OPTION_ARG_NONE, &global_opt.g_fatal_warnings, N_("Make all warnings fatal"), NULL }, {NULL} }; @@ -328,18 +342,18 @@ main (int argc, char *argv[]) _("nm-iface-helper is a small, standalone process that manages a single network interface."))) exit (1); - if (show_version) { + if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); exit (0); } - if (!ifname || !uuid) { + if (!global_opt.ifname || !global_opt.uuid) { fprintf (stderr, _("An interface name and UUID are required\n")); exit (1); } - if (!nm_logging_setup (opt_log_level, - opt_log_domains, + if (!nm_logging_setup (global_opt.opt_log_level, + global_opt.opt_log_domains, &bad_domains, &error)) { fprintf (stderr, @@ -360,7 +374,7 @@ main (int argc, char *argv[]) if (nm_main_utils_check_pidfile (pidfile, "nm-iface-helper")) exit (1); - if (become_daemon && !debug) { + if (global_opt.become_daemon && !global_opt.debug) { if (daemon (0, 0) < 0) { int saved_errno; @@ -378,7 +392,7 @@ main (int argc, char *argv[]) main_loop = g_main_loop_new (NULL, FALSE); setup_signals (&quit_early); - if (g_fatal_warnings) { + if (global_opt.g_fatal_warnings) { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); @@ -386,7 +400,7 @@ main (int argc, char *argv[]) g_log_set_always_fatal (fatal_mask); } - nm_logging_syslog_openlog (debug); + nm_logging_syslog_openlog (global_opt.debug); #if !GLIB_CHECK_VERSION (2, 35, 0) g_type_init (); @@ -397,9 +411,9 @@ main (int argc, char *argv[]) /* Set up platform interaction layer */ nm_linux_platform_setup (); - ifindex = nm_platform_link_get_ifindex (ifname); + ifindex = nm_platform_link_get_ifindex (global_opt.ifname); if (ifindex <= 0) { - fprintf (stderr, _("Failed to find interface index for %s\n"), ifname); + fprintf (stderr, _("Failed to find interface index for %s\n"), global_opt.ifname); exit (1); } @@ -409,43 +423,43 @@ main (int argc, char *argv[]) g_byte_array_append (hwaddr, tmp, hwaddr_len); } - if (iid_str) { + if (global_opt.iid_str) { GBytes *bytes; gsize ignored = 0; - bytes = nm_utils_hexstr2bin (iid_str); + bytes = nm_utils_hexstr2bin (global_opt.iid_str); if (!bytes || g_bytes_get_size (bytes) != sizeof (*iid)) { - fprintf (stderr, _("(%s): Invalid IID %s\n"), ifname, iid_str); + fprintf (stderr, _("(%s): Invalid IID %s\n"), global_opt.ifname, global_opt.iid_str); exit (1); } iid = g_bytes_unref_to_data (bytes, &ignored); } - if (priority64_v4 >= 0 && priority64_v4 <= G_MAXUINT32) - priority_v4 = (guint32) priority64_v4; + if (global_opt.priority64_v4 >= 0 && global_opt.priority64_v4 <= G_MAXUINT32) + priority_v4 = (guint32) global_opt.priority64_v4; - if (priority64_v6 >= 0 && priority64_v6 <= G_MAXUINT32) - priority_v6 = (guint32) priority64_v6; + if (global_opt.priority64_v6 >= 0 && global_opt.priority64_v6 <= G_MAXUINT32) + priority_v6 = (guint32) global_opt.priority64_v6; - if (dhcp4_address) { - nm_platform_sysctl_set (nm_utils_ip4_property_path (ifname, "promote_secondaries"), "1"); + if (global_opt.dhcp4_address) { + nm_platform_sysctl_set (nm_utils_ip4_property_path (global_opt.ifname, "promote_secondaries"), "1"); /* Initialize DHCP manager */ dhcp_mgr = nm_dhcp_manager_get (); g_assert (dhcp_mgr != NULL); dhcp4_client = nm_dhcp_manager_start_ip4 (dhcp_mgr, - ifname, + global_opt.ifname, ifindex, hwaddr, - uuid, + global_opt.uuid, priority_v4, - !!dhcp4_hostname, - dhcp4_hostname, - dhcp4_clientid, + !!global_opt.dhcp4_hostname, + global_opt.dhcp4_hostname, + global_opt.dhcp4_clientid, 45, NULL, - dhcp4_address); + global_opt.dhcp4_address); g_assert (dhcp4_client); g_signal_connect (dhcp4_client, NM_DHCP_CLIENT_SIGNAL_STATE_CHANGED, @@ -453,19 +467,19 @@ main (int argc, char *argv[]) NULL); } - if (slaac) { + if (global_opt.slaac) { nm_platform_link_set_user_ipv6ll_enabled (ifindex, TRUE); - rdisc = nm_lndp_rdisc_new (ifindex, ifname); + rdisc = nm_lndp_rdisc_new (ifindex, global_opt.ifname); g_assert (rdisc); if (iid) nm_rdisc_set_iid (rdisc, *iid); - nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra"), "1"); - nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra_defrtr"), "0"); - nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra_pinfo"), "0"); - nm_platform_sysctl_set (nm_utils_ip6_property_path (ifname, "accept_ra_rtr_pref"), "0"); + nm_platform_sysctl_set (nm_utils_ip6_property_path (global_opt.ifname, "accept_ra"), "1"); + nm_platform_sysctl_set (nm_utils_ip6_property_path (global_opt.ifname, "accept_ra_defrtr"), "0"); + nm_platform_sysctl_set (nm_utils_ip6_property_path (global_opt.ifname, "accept_ra_pinfo"), "0"); + nm_platform_sysctl_set (nm_utils_ip6_property_path (global_opt.ifname, "accept_ra_rtr_pref"), "0"); g_signal_connect (rdisc, NM_RDISC_CONFIG_CHANGED, From c7fe834bdf5d954bec44f44db89520f013bddbe1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 20:09:46 +0100 Subject: [PATCH 04/17] 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. (cherry picked from commit 5775df9a6dcfaa4a9d208c8e78d6a7419c0e6a07) (cherry picked from commit 8e58fc742580836df67da84a4bfbf53453e989e5) Conflicts: src/main.c src/nm-iface-helper.c --- src/main.c | 56 +++++++++++++---------- src/nm-iface-helper.c | 103 ++++++++++++++++++++++-------------------- 2 files changed, 85 insertions(+), 74 deletions(-) diff --git a/src/main.c b/src/main.c index 27095f2c3f..8a776ab0e2 100644 --- a/src/main.c +++ b/src/main.c @@ -202,6 +202,36 @@ manager_configure_quit (NMManager *manager, gpointer user_data) quit_early = TRUE; } +static void +do_early_setup (int *argc, char **argv[]) +{ + 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, + nm_config_get_options (), + _("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 * @@ -224,32 +254,11 @@ main (int argc, char *argv[]) gboolean wrote_pidfile = FALSE; char *bad_domains = NULL; - 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); - if (!nm_main_utils_early_setup ("NetworkManager", - &argc, - &argv, - options, - nm_config_get_options (), - _("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); if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); @@ -301,9 +310,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 d4d38314cd..599a5c5bc8 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); } @@ -289,6 +287,52 @@ setup_signals (gboolean *quit_early_ptr) g_unix_signal_add (SIGTERM, quit_handler, quit_early_ptr); } +static void +do_early_setup (int *argc, char **argv[]) +{ + 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") }, + { "uuid", 'u', 0, G_OPTION_ARG_STRING, &global_opt.uuid, N_("Connection UUID"), N_("661e8cd0-b618-46b8-9dc9-31a52baaa16b") }, + { "slaac", 's', 0, G_OPTION_ARG_NONE, &global_opt.slaac, N_("Whether to manage IPv6 SLAAC"), NULL }, + { "slaac-required", '6', 0, G_OPTION_ARG_NONE, &global_opt.slaac_required, N_("Whether SLAAC must be successful"), NULL }, + { "slaac-tempaddr", 't', 0, G_OPTION_ARG_INT, &global_opt.tempaddr, N_("Use an IPv6 temporary privacy address"), NULL }, + { "dhcp4", 'd', 0, G_OPTION_ARG_STRING, &global_opt.dhcp4_address, N_("Current DHCPv4 address"), NULL }, + { "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, &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 */ + { "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", 'b', 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 }, + {NULL} + }; + + if (!nm_main_utils_early_setup ("nm-iface-helper", + argc, + argv, + options, + 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[]) { @@ -305,42 +349,9 @@ main (int argc, char *argv[]) gconstpointer tmp; gs_free NMUtilsIPv6IfaceId *iid = NULL; - GOptionEntry options[] = { - /* Interface/IP config */ - { "ifname", 'i', 0, G_OPTION_ARG_STRING, &global_opt.ifname, N_("The interface to manage"), N_("eth0") }, - { "uuid", 'u', 0, G_OPTION_ARG_STRING, &global_opt.uuid, N_("Connection UUID"), N_("661e8cd0-b618-46b8-9dc9-31a52baaa16b") }, - { "slaac", 's', 0, G_OPTION_ARG_NONE, &global_opt.slaac, N_("Whether to manage IPv6 SLAAC"), NULL }, - { "slaac-required", '6', 0, G_OPTION_ARG_NONE, &global_opt.slaac_required, N_("Whether SLAAC must be successful"), NULL }, - { "slaac-tempaddr", 't', 0, G_OPTION_ARG_INT, &global_opt.tempaddr, N_("Use an IPv6 temporary privacy address"), NULL }, - { "dhcp4", 'd', 0, G_OPTION_ARG_STRING, &global_opt.dhcp4_address, N_("Current DHCPv4 address"), NULL }, - { "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") }, - { "iid", 'e', 0, G_OPTION_ARG_STRING, &global_opt.iid_str, N_("Hex-encoded Interface Identifier"), N_("") }, - - /* Logging/debugging */ - { "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", 'b', 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 }, - {NULL} - }; - setpgid (getpid (), getpid ()); - if (!nm_main_utils_early_setup ("nm-iface-helper", - &argc, - &argv, - options, - NULL, - _("nm-iface-helper is a small, standalone process that manages a single network interface."))) - exit (1); + do_early_setup (&argc, &argv); if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); @@ -435,12 +446,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"); @@ -453,7 +458,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, From 878299805635a0cd3f01d0915cf6561630514a4d Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 22:55:39 +0100 Subject: [PATCH 05/17] main: split out nm_main_utils_ensure_rundir () Also, don't use nm_log_err() as nm-logging is not yet setup. (cherry picked from commit 29718fcaa43f52606e4f0b3addbca3f7afb2a224) --- src/main-utils.c | 16 ++++++++++------ src/main-utils.h | 2 ++ src/main.c | 2 ++ src/nm-iface-helper.c | 2 ++ 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main-utils.c b/src/main-utils.c index 90bc7e0cdb..bf8eb4b329 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -152,6 +152,16 @@ nm_main_utils_write_pidfile (const char *pidfile) return success; } +void +nm_main_utils_ensure_rundir () +{ + /* Setup runtime directory */ + if (g_mkdir_with_parents (NMRUNDIR, 0755) != 0) { + fprintf (stderr, _("Cannot create '%s': %s"), NMRUNDIR, strerror (errno)); + exit (1); + } +} + /** * nm_main_utils_check_pidfile: * @pidfile: the pid file @@ -173,12 +183,6 @@ nm_main_utils_check_pidfile (const char *pidfile, const char *name) gboolean nm_running = FALSE; const char *process_name; - /* Setup runtime directory */ - if (g_mkdir_with_parents (NMRUNDIR, 0755) != 0) { - nm_log_err (LOGD_CORE, "Cannot create '%s': %s", NMRUNDIR, strerror (errno)); - exit (1); - } - if (!g_file_get_contents (pidfile, &contents, &len, NULL)) return FALSE; diff --git a/src/main-utils.h b/src/main-utils.h index e33935562a..18e7e534bd 100644 --- a/src/main-utils.h +++ b/src/main-utils.h @@ -25,6 +25,8 @@ gboolean nm_main_utils_setup_signals (GMainLoop *main_loop, gboolean *quit_early_ptr); +void nm_main_utils_ensure_rundir (void); + gboolean nm_main_utils_write_pidfile (const char *pidfile); gboolean nm_main_utils_check_pidfile (const char *pidfile, const char *name); diff --git a/src/main.c b/src/main.c index 8a776ab0e2..28d8defa64 100644 --- a/src/main.c +++ b/src/main.c @@ -310,6 +310,8 @@ main (int argc, char *argv[]) exit (1); } + nm_main_utils_ensure_rundir (); + /* 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 599a5c5bc8..0c39d8fa5b 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -378,6 +378,8 @@ main (int argc, char *argv[]) g_clear_pointer (&bad_domains, g_free); } + nm_main_utils_ensure_rundir (); + pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); g_assert (pidfile); From 4c396b6e2c12dbf3901e23551d16ef8f73dceba6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:06:29 +0100 Subject: [PATCH 06/17] main: refactor nm_main_utils_check_pidfile() to exit directly on failure And rename the function to nm_main_utils_ensure_not_running_pidfile() to match the other _ensure_ functions that exit(1). Also no longer pass @name to nm_main_utils_ensure_not_running_pidfile() and use g_get_prgname() instead. nm_main_utils_ensure_not_running_pidfile() checks that the running process has the same program name, so this changes behavior if the user renamed the binary. Before, we would check whether the running process is named 'NetworkManager' ('nm-iface-helper'). Now we check whether the process has the same name as the current process. This means, that if you rename the binary to 'NetworkManager2' we would now only detect a conflicting 'NetworkManager2'. Before we would only detect conflicting 'NetworkManager' binaries. (cherry picked from commit 12ad2c7fe7ebd94c40edd9a28b81ce27563bb2e3) --- src/main-utils.c | 44 +++++++++++++++++++++---------------------- src/main-utils.h | 2 +- src/main.c | 4 +--- src/nm-iface-helper.c | 6 +----- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/src/main-utils.c b/src/main-utils.c index bf8eb4b329..f6a2a60e9e 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -33,6 +33,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "main-utils.h" #include "nm-posix-signals.h" #include "nm-logging.h" @@ -163,59 +164,56 @@ nm_main_utils_ensure_rundir () } /** - * nm_main_utils_check_pidfile: + * nm_main_utils_ensure_not_running_pidfile: * @pidfile: the pid file - * @name: the process name * * Checks whether the pidfile already exists and contains PID of a running * process. * - * Returns: %TRUE if the specified pidfile already exists and contains the PID - * of a running process named @name, or %FALSE if not + * Exits with code 1 if a conflicting process is running. */ -gboolean -nm_main_utils_check_pidfile (const char *pidfile, const char *name) +void +nm_main_utils_ensure_not_running_pidfile (const char *pidfile) { - char *contents = NULL; + gs_free char *contents = NULL; + gs_free char *proc_cmdline = NULL; gsize len = 0; glong pid; - char *proc_cmdline = NULL; - gboolean nm_running = FALSE; const char *process_name; + const char *prgname = g_get_prgname (); + + g_return_if_fail (prgname); + + if (!pidfile || !*pidfile) + return; if (!g_file_get_contents (pidfile, &contents, &len, NULL)) - return FALSE; - + return; if (len <= 0) - goto done; + return; errno = 0; pid = strtol (contents, NULL, 10); if (pid <= 0 || pid > 65536 || errno) - goto done; + return; - g_free (contents); + g_clear_pointer (&contents, g_free); proc_cmdline = g_strdup_printf ("/proc/%ld/cmdline", pid); if (!g_file_get_contents (proc_cmdline, &contents, &len, NULL)) - goto done; + return; process_name = strrchr (contents, '/'); if (process_name) process_name++; else process_name = contents; - if (strcmp (process_name, name) == 0) { + if (strcmp (process_name, prgname) == 0) { /* Check that the process exists */ if (kill (pid, 0) == 0) { - fprintf (stderr, _("%s is already running (pid %ld)\n"), name, pid); - nm_running = TRUE; + fprintf (stderr, _("%s is already running (pid %ld)\n"), prgname, pid); + exit (1); } } - -done: - g_free (proc_cmdline); - g_free (contents); - return nm_running; } gboolean diff --git a/src/main-utils.h b/src/main-utils.h index 18e7e534bd..3d64535768 100644 --- a/src/main-utils.h +++ b/src/main-utils.h @@ -29,7 +29,7 @@ void nm_main_utils_ensure_rundir (void); gboolean nm_main_utils_write_pidfile (const char *pidfile); -gboolean nm_main_utils_check_pidfile (const char *pidfile, const char *name); +void nm_main_utils_ensure_not_running_pidfile (const char *pidfile); gboolean nm_main_utils_early_setup (const char *progname, int *argc, diff --git a/src/main.c b/src/main.c index 28d8defa64..6c1fd9960d 100644 --- a/src/main.c +++ b/src/main.c @@ -312,9 +312,7 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); - /* check pid file */ - if (nm_main_utils_check_pidfile (global_opt.pidfile, "NetworkManager")) - exit (1); + nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile); /* Read the config file and CLI overrides */ config = nm_config_new (&error); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 0c39d8fa5b..74db5b0ec0 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -381,11 +381,7 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); - g_assert (pidfile); - - /* check pid file */ - if (nm_main_utils_check_pidfile (pidfile, "nm-iface-helper")) - exit (1); + nm_main_utils_ensure_not_running_pidfile (pidfile); if (global_opt.become_daemon && !global_opt.debug) { if (daemon (0, 0) < 0) { From 4753027ddd2543e3093d6c4f547b7100ae4c4b1c Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 19:42:21 +0100 Subject: [PATCH 07/17] main: (order) call g_type_init() very early in main() g_type_init() is independent of all NetworkManager functionality. Just get it done early on. (cherry picked from commit 0587dbe96c977787188f03bb2b7a60169fdc05bc) Conflicts: src/nm-iface-helper.c --- src/main.c | 8 ++++---- src/nm-iface-helper.c | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index 6c1fd9960d..87503831b1 100644 --- a/src/main.c +++ b/src/main.c @@ -254,6 +254,10 @@ main (int argc, char *argv[]) gboolean wrote_pidfile = FALSE; char *bad_domains = NULL; +#if !GLIB_CHECK_VERSION (2, 35, 0) + g_type_init (); +#endif + _nm_utils_is_manager_process = TRUE; main_loop = g_main_loop_new (NULL, FALSE); @@ -381,10 +385,6 @@ main (int argc, char *argv[]) nm_logging_syslog_openlog (global_opt.debug); -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - dbus_threads_init_default (); /* Ensure that non-exported properties don't leak out, and that the diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 74db5b0ec0..078ee315c2 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -349,6 +349,10 @@ main (int argc, char *argv[]) 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); @@ -411,10 +415,6 @@ main (int argc, char *argv[]) nm_logging_syslog_openlog (global_opt.debug); -#if !GLIB_CHECK_VERSION (2, 35, 0) - g_type_init (); -#endif - nm_log_info (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting..."); /* Set up platform interaction layer */ From ed26762e13a50a9802db933b12ddfc28c98cf4ad Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:24:05 +0100 Subject: [PATCH 08/17] main: (order) set g_log_set_always_fatal() early on After parsing the cmd line options, we already can setup fatal glog level. (cherry picked from commit c50622c84566363c6a0309150800c660c7fd589b) --- src/main.c | 16 ++++++++-------- src/nm-iface-helper.c | 16 ++++++++-------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index 87503831b1..d95cdce4d0 100644 --- a/src/main.c +++ b/src/main.c @@ -264,6 +264,14 @@ main (int argc, char *argv[]) do_early_setup (&argc, &argv); + if (global_opt.g_fatal_warnings) { + GLogLevelFlags fatal_mask; + + fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); + fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; + g_log_set_always_fatal (fatal_mask); + } + if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); exit (0); @@ -375,14 +383,6 @@ main (int argc, char *argv[]) if (!nm_main_utils_setup_signals (main_loop, &quit_early)) exit (1); - if (global_opt.g_fatal_warnings) { - GLogLevelFlags fatal_mask; - - fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); - fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; - g_log_set_always_fatal (fatal_mask); - } - nm_logging_syslog_openlog (global_opt.debug); dbus_threads_init_default (); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 078ee315c2..802b50a241 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -357,6 +357,14 @@ main (int argc, char *argv[]) do_early_setup (&argc, &argv); + if (global_opt.g_fatal_warnings) { + GLogLevelFlags fatal_mask; + + fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); + fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; + g_log_set_always_fatal (fatal_mask); + } + if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); exit (0); @@ -405,14 +413,6 @@ main (int argc, char *argv[]) main_loop = g_main_loop_new (NULL, FALSE); setup_signals (&quit_early); - if (global_opt.g_fatal_warnings) { - GLogLevelFlags fatal_mask; - - fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); - fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; - g_log_set_always_fatal (fatal_mask); - } - nm_logging_syslog_openlog (global_opt.debug); nm_log_info (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting..."); From 3aad126ec113252c6d0ab2480807debf0e4e08f9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:19:58 +0100 Subject: [PATCH 09/17] main: (order) early call _init_nm_debug() _init_nm_debug() only depends on DEBUG config setting. Let's call it first after parsing configuration. (cherry picked from commit 3696c675fe9fd17efe04d89ef0944de3dff8fd4f) --- src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index d95cdce4d0..189ee75f28 100644 --- a/src/main.c +++ b/src/main.c @@ -335,6 +335,8 @@ main (int argc, char *argv[]) exit (1); } + _init_nm_debug (nm_config_get_debug (config)); + /* Initialize logging from config file *only* if not explicitly * specified by commandline. */ @@ -377,8 +379,6 @@ main (int argc, char *argv[]) wrote_pidfile = nm_main_utils_write_pidfile (global_opt.pidfile); } - _init_nm_debug (nm_config_get_debug (config)); - /* Set up unix signal handling - before creating threads, but after daemonizing! */ if (!nm_main_utils_setup_signals (main_loop, &quit_early)) exit (1); From 4a427923227e6e9a5449756fb2c96c1bb7cdec56 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 20:57:18 +0100 Subject: [PATCH 10/17] main: (order) move root user check after help/version option With this change, `NetworkManager --help` and `NetworkManager --version` work for non-root user. (cherry picked from commit 95786a4e4e307dfd2c961280e65d16d5138050d6) Conflicts: src/main-utils.c src/main-utils.h --- src/main-utils.c | 15 ++++++++++----- src/main-utils.h | 2 ++ src/main.c | 2 ++ src/nm-iface-helper.c | 2 ++ 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main-utils.c b/src/main-utils.c index f6a2a60e9e..c4d0eeb902 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -36,6 +36,7 @@ #include "gsystem-local-alloc.h" #include "main-utils.h" #include "nm-posix-signals.h" +#include "NetworkManagerUtils.h" #include "nm-logging.h" static sigset_t signal_set; @@ -216,6 +217,15 @@ nm_main_utils_ensure_not_running_pidfile (const char *pidfile) } } +void +nm_main_utils_ensure_root () +{ + if (getuid () != 0) { + fprintf (stderr, _("You must be root to run %s!\n"), str_if_set (g_get_prgname (), "")); + exit (1); + } +} + gboolean nm_main_utils_early_setup (const char *progname, int *argc, @@ -248,11 +258,6 @@ nm_main_utils_early_setup (const char *progname, setlocale (LC_ALL, ""); textdomain (GETTEXT_PACKAGE); - if (getuid () != 0) { - fprintf (stderr, _("You must be root to run %s!\n"), progname); - exit (1); - } - for (i = 0; options[i].long_name; i++) { if (!strcmp (options[i].long_name, "log-level")) { opt_fmt_log_level = options[i].description; diff --git a/src/main-utils.h b/src/main-utils.h index 3d64535768..1fceb35006 100644 --- a/src/main-utils.h +++ b/src/main-utils.h @@ -23,6 +23,8 @@ #include +void nm_main_utils_ensure_root (void); + gboolean nm_main_utils_setup_signals (GMainLoop *main_loop, gboolean *quit_early_ptr); void nm_main_utils_ensure_rundir (void); diff --git a/src/main.c b/src/main.c index 189ee75f28..1c18306b9e 100644 --- a/src/main.c +++ b/src/main.c @@ -277,6 +277,8 @@ main (int argc, char *argv[]) exit (0); } + nm_main_utils_ensure_root (); + if (!nm_logging_setup (global_opt.opt_log_level, global_opt.opt_log_domains, &bad_domains, diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 802b50a241..fdcb8c9676 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -370,6 +370,8 @@ main (int argc, char *argv[]) exit (0); } + nm_main_utils_ensure_root (); + if (!global_opt.ifname || !global_opt.uuid) { fprintf (stderr, _("An interface name and UUID are required\n")); exit (1); From f8430aaa9fb93d13cf3265d72058be145d03d9df Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 15 Mar 2015 17:18:59 +0100 Subject: [PATCH 11/17] nm-iface-helper: fix pidfile name and obtain the ifindex earlier The @ifindex is needed for the @pidfile. Obtain the @ifindex earlier without resorting to platform. (cherry picked from commit e36bfaadf74ee2f4080d6416f979897b000ba94e) --- src/nm-iface-helper.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index fdcb8c9676..1d842358c4 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -34,6 +34,10 @@ #include #include +/* Cannot include due to conflict with . + * Forward declare if_nametoindex. */ +extern unsigned int if_nametoindex (const char *__ifname); + #include "gsystem-local-alloc.h" #include "NetworkManagerUtils.h" #include "nm-linux-platform.h" @@ -394,6 +398,11 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); + ifindex = if_nametoindex (global_opt.ifname); + if (ifindex <= 0) { + fprintf (stderr, _("Failed to find interface index for %s (%s)\n"), global_opt.ifname, strerror (errno)); + exit (1); + } pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); nm_main_utils_ensure_not_running_pidfile (pidfile); @@ -422,12 +431,6 @@ main (int argc, char *argv[]) /* Set up platform interaction layer */ nm_linux_platform_setup (); - ifindex = nm_platform_link_get_ifindex (global_opt.ifname); - if (ifindex <= 0) { - fprintf (stderr, _("Failed to find interface index for %s\n"), global_opt.ifname); - exit (1); - } - tmp = nm_platform_link_get_address (ifindex, &hwaddr_len); if (tmp) { hwaddr = g_byte_array_sized_new (hwaddr_len); From bc2ea72a79d0149f4c138c833c49866b52f27652 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 14 Mar 2015 17:58:23 +0100 Subject: [PATCH 12/17] main: (order) check pidfile earlier for running NM We should check for conflicting process (pidfile) early on and error out. (cherry picked from commit af2b44cb43ecdd7d2ad0dcaf930f2674701a5d15) --- src/main.c | 4 ++-- src/nm-iface-helper.c | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 1c18306b9e..e6b48c13ca 100644 --- a/src/main.c +++ b/src/main.c @@ -279,6 +279,8 @@ main (int argc, char *argv[]) nm_main_utils_ensure_root (); + nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile); + if (!nm_logging_setup (global_opt.opt_log_level, global_opt.opt_log_domains, &bad_domains, @@ -326,8 +328,6 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); - nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile); - /* Read the config file and CLI overrides */ config = nm_config_new (&error); if (config == NULL) { diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 1d842358c4..7d90bff812 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -381,6 +381,14 @@ main (int argc, char *argv[]) exit (1); } + ifindex = if_nametoindex (global_opt.ifname); + if (ifindex <= 0) { + fprintf (stderr, _("Failed to find interface index for %s (%s)\n"), global_opt.ifname, strerror (errno)); + exit (1); + } + pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); + nm_main_utils_ensure_not_running_pidfile (pidfile); + if (!nm_logging_setup (global_opt.opt_log_level, global_opt.opt_log_domains, &bad_domains, @@ -398,14 +406,6 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); - ifindex = if_nametoindex (global_opt.ifname); - if (ifindex <= 0) { - fprintf (stderr, _("Failed to find interface index for %s (%s)\n"), global_opt.ifname, strerror (errno)); - exit (1); - } - pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); - nm_main_utils_ensure_not_running_pidfile (pidfile); - if (global_opt.become_daemon && !global_opt.debug) { if (daemon (0, 0) < 0) { int saved_errno; From 22db307456850db879e71f8492b26e4081d5c6df Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:28:13 +0100 Subject: [PATCH 13/17] main: (order) parse state file later and use nm-logging Parse the state file a bit later after daemonizing and setting up logging. That way, we can use nm-logging. (cherry picked from commit 7be9024c826f524a1383bb6fd1264a1f9247b1d1) --- src/main.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index e6b48c13ca..0d0d3361d4 100644 --- a/src/main.c +++ b/src/main.c @@ -358,16 +358,6 @@ main (int argc, char *argv[]) } } - /* Parse the state file */ - if (!parse_state_file (global_opt.state_file, &net_enabled, &wifi_enabled, &wwan_enabled, &wimax_enabled, &error)) { - fprintf (stderr, _("State file %s parsing failed: (%d) %s\n"), - global_opt.state_file, - error ? error->code : -1, - (error && error->message) ? error->message : _("unknown")); - /* Not a hard failure */ - } - g_clear_error (&error); - if (global_opt.become_daemon && !global_opt.debug) { if (daemon (0, 0) < 0) { int saved_errno; @@ -387,6 +377,16 @@ main (int argc, char *argv[]) nm_logging_syslog_openlog (global_opt.debug); + /* Parse the state file */ + if (!parse_state_file (global_opt.state_file, &net_enabled, &wifi_enabled, &wwan_enabled, &wimax_enabled, &error)) { + nm_log_err (LOGD_CORE, "State file %s parsing failed: (%d) %s", + global_opt.state_file, + error ? error->code : -1, + (error && error->message) ? error->message : _("unknown")); + /* Not a hard failure */ + } + g_clear_error (&error); + dbus_threads_init_default (); /* Ensure that non-exported properties don't leak out, and that the From ba054666f04dc44cc607c9a4e82b1cac34388610 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:29:46 +0100 Subject: [PATCH 14/17] main: (order) log "is starting" message immediately after setting up logging (cherry picked from commit f64c79afda1006bbe6f569883b37721c3d0f14d5) Conflicts: src/main.c --- src/main.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 0d0d3361d4..fb0cc06259 100644 --- a/src/main.c +++ b/src/main.c @@ -240,7 +240,7 @@ int main (int argc, char *argv[]) { gboolean wifi_enabled = TRUE, net_enabled = TRUE, wwan_enabled = TRUE, wimax_enabled = TRUE; - gboolean success; + gboolean success = FALSE; NMManager *manager = NULL; gs_unref_object NMVpnManager *vpn_manager = NULL; gs_unref_object NMDnsManager *dns_mgr = NULL; @@ -377,6 +377,8 @@ main (int argc, char *argv[]) nm_logging_syslog_openlog (global_opt.debug); + nm_log_info (LOGD_CORE, "NetworkManager (version " NM_DIST_VERSION ") is starting..."); + /* Parse the state file */ if (!parse_state_file (global_opt.state_file, &net_enabled, &wifi_enabled, &wwan_enabled, &wimax_enabled, &error)) { nm_log_err (LOGD_CORE, "State file %s parsing failed: (%d) %s", @@ -394,9 +396,6 @@ main (int argc, char *argv[]) */ dbus_glib_global_set_disable_legacy_property_access (); - nm_log_info (LOGD_CORE, "NetworkManager (version " NM_DIST_VERSION ") is starting..."); - success = FALSE; - nm_log_info (LOGD_CORE, "Read config: %s", nm_config_get_description (config)); nm_log_info (LOGD_CORE, "WEXT support is %s", #if HAVE_WEXT From 06d038cd5801ed17d86b825cc87a4ee7b33468b2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 15 Mar 2015 15:59:02 +0100 Subject: [PATCH 15/17] main: (order) earlier create rundir Create the rundir earlier and before setting up nm-logging. nm_main_utils_ensure_rundir() errors out with fprintf(stderr) and does not need nm-logging. (cherry picked from commit 49cfe6487492dd69a16f8120b22841d39cb92f2b) --- src/main.c | 16 ++++++++-------- src/nm-iface-helper.c | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index fb0cc06259..391219e241 100644 --- a/src/main.c +++ b/src/main.c @@ -281,6 +281,14 @@ main (int argc, char *argv[]) nm_main_utils_ensure_not_running_pidfile (global_opt.pidfile); + /* Ensure state directory exists */ + if (g_mkdir_with_parents (NMSTATEDIR, 0755) != 0) { + fprintf (stderr, "Cannot create '%s': %s", NMSTATEDIR, strerror (errno)); + exit (1); + } + + nm_main_utils_ensure_rundir (); + if (!nm_logging_setup (global_opt.opt_log_level, global_opt.opt_log_domains, &bad_domains, @@ -320,14 +328,6 @@ main (int argc, char *argv[]) g_free (path); } - /* Ensure state directory exists */ - if (g_mkdir_with_parents (NMSTATEDIR, 0755) != 0) { - nm_log_err (LOGD_CORE, "Cannot create '%s': %s", NMSTATEDIR, strerror (errno)); - exit (1); - } - - nm_main_utils_ensure_rundir (); - /* Read the config file and CLI overrides */ config = nm_config_new (&error); if (config == NULL) { diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 7d90bff812..d2ab0a1be0 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -389,6 +389,8 @@ main (int argc, char *argv[]) pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); nm_main_utils_ensure_not_running_pidfile (pidfile); + nm_main_utils_ensure_rundir (); + if (!nm_logging_setup (global_opt.opt_log_level, global_opt.opt_log_domains, &bad_domains, @@ -404,8 +406,6 @@ main (int argc, char *argv[]) g_clear_pointer (&bad_domains, g_free); } - nm_main_utils_ensure_rundir (); - if (global_opt.become_daemon && !global_opt.debug) { if (daemon (0, 0) < 0) { int saved_errno; From b24d44cb2db12d26476504f5e98c0fc5c64b9d21 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 15 Mar 2015 16:00:36 +0100 Subject: [PATCH 16/17] main: (order) move run_from_build_dir check before setting up logging Or: move setup of nm-logging immediately after it is really needed: before setup of config. (cherry picked from commit 7fe0f349ce49500871befc7f00cac59139809fae) --- src/main.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 391219e241..95d773155d 100644 --- a/src/main.c +++ b/src/main.c @@ -289,21 +289,6 @@ main (int argc, char *argv[]) nm_main_utils_ensure_rundir (); - if (!nm_logging_setup (global_opt.opt_log_level, - global_opt.opt_log_domains, - &bad_domains, - &error)) { - fprintf (stderr, - _("%s. Please use --help to see a list of valid options.\n"), - error->message); - exit (1); - } else if (bad_domains) { - fprintf (stderr, - _("Ignoring unrecognized log domain(s) '%s' passed on command line.\n"), - bad_domains); - g_clear_pointer (&bad_domains, g_free); - } - /* When running from the build directory, determine our build directory * base and set helper paths in the build tree */ if (global_opt.run_from_build_dir) { @@ -328,6 +313,21 @@ main (int argc, char *argv[]) g_free (path); } + if (!nm_logging_setup (global_opt.opt_log_level, + global_opt.opt_log_domains, + &bad_domains, + &error)) { + fprintf (stderr, + _("%s. Please use --help to see a list of valid options.\n"), + error->message); + exit (1); + } else if (bad_domains) { + fprintf (stderr, + _("Ignoring unrecognized log domain(s) '%s' passed on command line.\n"), + bad_domains); + g_clear_pointer (&bad_domains, g_free); + } + /* Read the config file and CLI overrides */ config = nm_config_new (&error); if (config == NULL) { From e860c0e4ea1e06e2fa1d818dcd6813cc62202946 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:34:47 +0100 Subject: [PATCH 17/17] main: (order) early start D-Bus service systemd considers the startup time of NetworkManager until the D-Bus service is claimed. By doing that earlier, this time is significantly reduced. This has the advantage, that services that are ordered to start after NetworkManager can start earlier. Most notably, 'network.target' orders itself After=NetworkManager.service and many services are ordered After=network.target. $ systemd-analyze blame | grep NetworkManager.service (cherry picked from commit 21562052ec6d5a044003d222bf5b12da4475f9d2) Conflicts: src/main.c --- src/main.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 95d773155d..7a6623d693 100644 --- a/src/main.c +++ b/src/main.c @@ -405,6 +405,21 @@ main (int argc, char *argv[]) #endif ); + if (!nm_dbus_manager_get_connection (nm_dbus_manager_get ())) { +#if HAVE_DBUS_GLIB_100 + nm_log_warn (LOGD_CORE, "Failed to connect to D-Bus; only private bus is available"); +#else + nm_log_err (LOGD_CORE, "Failed to connect to D-Bus, exiting..."); + goto done; +#endif + } else { + /* Start our DBus service */ + if (!nm_dbus_manager_start_service (nm_dbus_manager_get ())) { + nm_log_err (LOGD_CORE, "failed to start the dbus service."); + goto done; + } + } + /* Set up platform interaction layer */ nm_linux_platform_setup (); @@ -454,21 +469,6 @@ main (int argc, char *argv[]) session_monitor = nm_session_monitor_get (); g_assert (session_monitor != NULL); - if (!nm_dbus_manager_get_connection (nm_dbus_manager_get ())) { -#if HAVE_DBUS_GLIB_100 - nm_log_warn (LOGD_CORE, "Failed to connect to D-Bus; only private bus is available"); -#else - nm_log_err (LOGD_CORE, "Failed to connect to D-Bus, exiting..."); - goto done; -#endif - } else { - /* Start our DBus service */ - if (!nm_dbus_manager_start_service (nm_dbus_manager_get ())) { - nm_log_err (LOGD_CORE, "failed to start the dbus service."); - goto done; - } - } - g_signal_connect (manager, NM_MANAGER_CONFIGURE_QUIT, G_CALLBACK (manager_configure_quit), config); nm_manager_start (manager);