From ee4543bd6e4958e93d19a878f8403c4afa398c6e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 20:47:03 +0100 Subject: 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. --- 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 164dc84a99..1c479d8a72 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -172,8 +172,8 @@ done: gboolean nm_main_utils_early_setup (const char *progname, - char **argv[], int *argc, + char **argv[], GOptionEntry *options, void (*option_context_hook) (gpointer user_data, GOptionContext *opt_ctx), gpointer option_context_hook_data, diff --git a/src/main-utils.h b/src/main-utils.h index 9b29866be0..88d217a559 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, void (*option_context_hook) (gpointer user_data, GOptionContext *opt_ctx), gpointer option_context_hook_data, diff --git a/src/main.c b/src/main.c index 3df3e11eb4..d76bbffa3b 100644 --- a/src/main.c +++ b/src/main.c @@ -242,8 +242,8 @@ main (int argc, char *argv[]) config_cli = nm_config_cmd_line_options_new (); if (!nm_main_utils_early_setup ("NetworkManager", - &argv, &argc, + &argv, options, (void (*)(gpointer, GOptionContext *)) nm_config_cmd_line_options_add_to_entries, config_cli, diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 49eda87718..09054c0237 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -311,8 +311,8 @@ main (int argc, char *argv[]) setpgid (getpid (), getpid ()); if (!nm_main_utils_early_setup ("nm-iface-helper", - &argv, &argc, + &argv, options, NULL, NULL, -- cgit v1.2.1 From 5e962bef878682c3821c1527758adbae4ee3e721 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 21:31:46 +0100 Subject: main: don't unref config instance at end of main() config is a singleton implemented using NM_DEFINE_SINGLETON_DESTRUCTOR(). No need to unref it manually. --- src/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index d76bbffa3b..0e9e923dcd 100644 --- a/src/main.c +++ b/src/main.c @@ -215,7 +215,7 @@ main (int argc, char *argv[]) gboolean success, show_version = FALSE; NMManager *manager = NULL; gs_unref_object NMSettings *settings = NULL; - gs_unref_object NMConfig *config = NULL; + NMConfig *config; GError *error = NULL; gboolean wrote_pidfile = FALSE; char *bad_domains = NULL; -- cgit v1.2.1 From b5ca5bd7b7e8894450786c0874a47620e777d3bb Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Thu, 19 Mar 2015 16:28:59 +0100 Subject: main-utils: don't leak description for command line arguments in nm_main_utils_early_setup() --- 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 1c479d8a72..5a470fa0de 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -183,6 +183,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 @@ -207,10 +209,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 */ @@ -231,6 +238,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; } -- cgit v1.2.1 From 1fc5aba42a175c0eca0b6d8804d6ee9088c1dfe5 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 19:59:32 +0100 Subject: 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. --- 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 0e9e923dcd..a1747f2e59 100644 --- a/src/main.c +++ b/src/main.c @@ -67,6 +67,20 @@ static GMainLoop *main_loop = NULL; static gboolean configure_and_quit = 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, @@ -204,15 +218,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 NMSettings *settings = NULL; NMConfig *config; @@ -222,17 +229,17 @@ main (int argc, char *argv[]) NMConfigCmdLineOptions *config_cli; 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} }; @@ -250,13 +257,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, @@ -272,7 +279,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; @@ -300,11 +307,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 */ @@ -321,7 +328,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, @@ -338,16 +345,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; @@ -357,7 +364,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)); @@ -365,7 +372,7 @@ main (int argc, char *argv[]) /* Set up unix signal handling - before creating threads, but after daemonizing! */ nm_main_utils_setup_signals (main_loop); - if (g_fatal_warnings) { + if (global_opt.g_fatal_warnings) { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); @@ -373,7 +380,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 (); @@ -413,7 +420,7 @@ main (int argc, char *argv[]) } manager = nm_manager_new (settings, - state_file, + global_opt.state_file, net_enabled, wifi_enabled, wwan_enabled, @@ -469,8 +476,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 09054c0237..eb376b5e6d 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 @@ -263,12 +284,7 @@ setup_signals (void) 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; GError *error = NULL; gboolean wrote_pidfile = FALSE; gs_free char *pidfile = NULL; @@ -278,33 +294,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} }; @@ -319,18 +333,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, @@ -351,7 +365,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; @@ -369,7 +383,7 @@ main (int argc, char *argv[]) main_loop = g_main_loop_new (NULL, FALSE); setup_signals (); - if (g_fatal_warnings) { + if (global_opt.g_fatal_warnings) { GLogLevelFlags fatal_mask; fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); @@ -377,7 +391,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 (); @@ -388,9 +402,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); } @@ -400,39 +414,39 @@ 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"); dhcp4_client = nm_dhcp_manager_start_ip4 (nm_dhcp_manager_get (), - 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, @@ -440,19 +454,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, -- cgit v1.2.1 From 5775df9a6dcfaa4a9d208c8e78d6a7419c0e6a07 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 20:09:46 +0100 Subject: 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,23 +211,9 @@ manager_configure_quit (NMManager *manager, gpointer user_data) configure_and_quit = TRUE; } -/* - * main - * - */ -int -main (int argc, char *argv[]) +static void +do_early_setup (int *argc, char **argv[], NMConfigCmdLineOptions *config_cli) { - gboolean wifi_enabled = TRUE, net_enabled = TRUE, wwan_enabled = TRUE, wimax_enabled = TRUE; - gboolean success; - NMManager *manager = NULL; - gs_unref_object NMSettings *settings = NULL; - NMConfig *config; - GError *error = NULL; - gboolean wrote_pidfile = FALSE; - 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 }, @@ -243,20 +229,43 @@ main (int argc, char *argv[]) {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, + 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 + * + */ +int +main (int argc, char *argv[]) +{ + gboolean wifi_enabled = TRUE, net_enabled = TRUE, wwan_enabled = TRUE, wimax_enabled = TRUE; + gboolean success; + NMManager *manager = NULL; + gs_unref_object NMSettings *settings = NULL; + NMConfig *config; + GError *error = NULL; + gboolean wrote_pidfile = FALSE; + char *bad_domains = NULL; + NMConfigCmdLineOptions *config_cli; + + _nm_utils_is_manager_process = TRUE; + + main_loop = g_main_loop_new (NULL, FALSE); + + config_cli = nm_config_cmd_line_options_new (); + do_early_setup (&argc, &argv, config_cli); + if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); exit (0); @@ -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, -- cgit v1.2.1 From 29718fcaa43f52606e4f0b3addbca3f7afb2a224 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 22:55:39 +0100 Subject: main: split out nm_main_utils_ensure_rundir () Also, don't use nm_log_err() as nm-logging is not yet setup. --- 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 5a470fa0de..4daa0eef52 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -108,6 +108,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 @@ -129,12 +139,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 88d217a559..be3d08626f 100644 --- a/src/main-utils.h +++ b/src/main-utils.h @@ -25,6 +25,8 @@ void nm_main_utils_setup_signals (GMainLoop *main_loop); +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 c06c746ddf..35a47b9502 100644 --- a/src/main.c +++ b/src/main.c @@ -316,6 +316,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 4125ba5012..3a3a71157e 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -375,6 +375,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); -- cgit v1.2.1 From 12ad2c7fe7ebd94c40edd9a28b81ce27563bb2e3 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:06:29 +0100 Subject: 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. --- 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 4daa0eef52..564254dc91 100644 --- a/src/main-utils.c +++ b/src/main-utils.c @@ -34,6 +34,7 @@ #include #include +#include "gsystem-local-alloc.h" #include "main-utils.h" #include "nm-logging.h" @@ -119,59 +120,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 (); - if (!g_file_get_contents (pidfile, &contents, &len, NULL)) - return FALSE; + g_return_if_fail (prgname); + if (!pidfile || !*pidfile) + return; + + if (!g_file_get_contents (pidfile, &contents, &len, NULL)) + 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 be3d08626f..e928e20b69 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 35a47b9502..77866a473d 100644 --- a/src/main.c +++ b/src/main.c @@ -318,9 +318,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_setup (config_cli, &error); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 3a3a71157e..8fc420df3d 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -378,11 +378,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) { -- cgit v1.2.1 From 0587dbe96c977787188f03bb2b7a60169fdc05bc Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 19:42:21 +0100 Subject: 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. --- 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 77866a473d..9018dc6e80 100644 --- a/src/main.c +++ b/src/main.c @@ -259,6 +259,10 @@ main (int argc, char *argv[]) char *bad_domains = NULL; NMConfigCmdLineOptions *config_cli; +#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); @@ -388,10 +392,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 8fc420df3d..87e0830ba9 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -311,6 +311,10 @@ do_early_setup (int *argc, char **argv[]) {NULL} }; +#if !GLIB_CHECK_VERSION (2, 35, 0) + g_type_init (); +#endif + setpgid (getpid (), getpid ()); if (!nm_main_utils_early_setup ("nm-iface-helper", @@ -408,10 +412,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 */ -- cgit v1.2.1 From c50622c84566363c6a0309150800c660c7fd589b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:24:05 +0100 Subject: main: (order) set g_log_set_always_fatal() early on After parsing the cmd line options, we already can setup fatal glog level. --- 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 9018dc6e80..f02421df61 100644 --- a/src/main.c +++ b/src/main.c @@ -270,6 +270,14 @@ main (int argc, char *argv[]) config_cli = nm_config_cmd_line_options_new (); do_early_setup (&argc, &argv, config_cli); + 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); @@ -382,14 +390,6 @@ main (int argc, char *argv[]) /* Set up unix signal handling - before creating threads, but after daemonizing! */ nm_main_utils_setup_signals (main_loop); - 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 87e0830ba9..803ee54291 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -354,6 +354,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); @@ -402,14 +410,6 @@ main (int argc, char *argv[]) main_loop = g_main_loop_new (NULL, FALSE); setup_signals (); - 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..."); -- cgit v1.2.1 From 3696c675fe9fd17efe04d89ef0944de3dff8fd4f Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:19:58 +0100 Subject: main: (order) early call _init_nm_debug() _init_nm_debug() only depends on DEBUG config setting. Let's call it first after parsing configuration. --- src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index f02421df61..f34302d23b 100644 --- a/src/main.c +++ b/src/main.c @@ -343,6 +343,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. */ @@ -385,8 +387,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! */ nm_main_utils_setup_signals (main_loop); -- cgit v1.2.1 From 95786a4e4e307dfd2c961280e65d16d5138050d6 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 20:57:18 +0100 Subject: main: (order) move root user check after help/version option With this change, `NetworkManager --help` and `NetworkManager --version` work for non-root user. --- 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 564254dc91..1d899587ec 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 "NetworkManagerUtils.h" #include "nm-logging.h" static gboolean @@ -172,6 +173,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, @@ -205,11 +215,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 e928e20b69..7adfa94940 100644 --- a/src/main-utils.h +++ b/src/main-utils.h @@ -23,6 +23,8 @@ #include +void nm_main_utils_ensure_root (void); + void nm_main_utils_setup_signals (GMainLoop *main_loop); void nm_main_utils_ensure_rundir (void); diff --git a/src/main.c b/src/main.c index f34302d23b..8deb62cccb 100644 --- a/src/main.c +++ b/src/main.c @@ -283,6 +283,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 803ee54291..d26e05665c 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -367,6 +367,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); -- cgit v1.2.1 From e36bfaadf74ee2f4080d6416f979897b000ba94e Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 15 Mar 2015 17:18:59 +0100 Subject: 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. --- 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 d26e05665c..33ba269236 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" @@ -391,6 +395,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); @@ -419,12 +428,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); -- cgit v1.2.1 From af2b44cb43ecdd7d2ad0dcaf930f2674701a5d15 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 14 Mar 2015 17:58:23 +0100 Subject: main: (order) check pidfile earlier for running NM We should check for conflicting process (pidfile) early on and error out. --- 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 8deb62cccb..067a0cee3c 100644 --- a/src/main.c +++ b/src/main.c @@ -285,6 +285,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, @@ -332,8 +334,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_setup (config_cli, &error); nm_config_cmd_line_options_free (config_cli); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 33ba269236..226c6a8f14 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -378,6 +378,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, @@ -395,14 +403,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; -- cgit v1.2.1 From 7be9024c826f524a1383bb6fd1264a1f9247b1d1 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:28:13 +0100 Subject: 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. --- src/main.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main.c b/src/main.c index 067a0cee3c..8bfc05c681 100644 --- a/src/main.c +++ b/src/main.c @@ -366,16 +366,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; @@ -394,6 +384,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 -- cgit v1.2.1 From f64c79afda1006bbe6f569883b37721c3d0f14d5 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:29:46 +0100 Subject: main: (order) log "is starting" message immediately after setting up logging --- src/main.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index 8bfc05c681..35c7b4e3b0 100644 --- a/src/main.c +++ b/src/main.c @@ -250,7 +250,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 NMSettings *settings = NULL; NMConfig *config; @@ -384,6 +384,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", @@ -401,9 +403,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_data_get_config_description (nm_config_get_data (config))); nm_log_info (LOGD_CORE, "WEXT support is %s", #if HAVE_WEXT -- cgit v1.2.1 From 49cfe6487492dd69a16f8120b22841d39cb92f2b Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 15 Mar 2015 15:59:02 +0100 Subject: 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. --- 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 35c7b4e3b0..420e56952b 100644 --- a/src/main.c +++ b/src/main.c @@ -287,6 +287,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, @@ -326,14 +334,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_setup (config_cli, &error); nm_config_cmd_line_options_free (config_cli); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 226c6a8f14..95fffa7a02 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -386,6 +386,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, @@ -401,8 +403,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; -- cgit v1.2.1 From 7fe0f349ce49500871befc7f00cac59139809fae Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sun, 15 Mar 2015 16:00:36 +0100 Subject: 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. --- src/main.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index 420e56952b..ef4438c54a 100644 --- a/src/main.c +++ b/src/main.c @@ -295,21 +295,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) { @@ -334,6 +319,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_setup (config_cli, &error); nm_config_cmd_line_options_free (config_cli); -- cgit v1.2.1 From 21562052ec6d5a044003d222bf5b12da4475f9d2 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Fri, 13 Mar 2015 23:34:47 +0100 Subject: 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 --- src/main.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main.c b/src/main.c index ef4438c54a..2648a08f41 100644 --- a/src/main.c +++ b/src/main.c @@ -412,6 +412,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 (); @@ -439,21 +454,6 @@ main (int argc, char *argv[]) goto done; } - 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); -- cgit v1.2.1