diff options
author | Thomas Haller <thaller@redhat.com> | 2016-10-06 13:34:23 +0200 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2016-10-06 13:34:23 +0200 |
commit | 1791978007c1d1af8916d0c7dc7b5826f60c8589 (patch) | |
tree | 629f543b2e8199c9472998e1b0d342d9d5a75a1f | |
parent | 67405bd5bcb513db96934a0da861b71159bb9ecf (diff) | |
parent | 2b728c7dda6f9abc96d1789aeb4b44aa1e2f5b64 (diff) | |
download | NetworkManager-1791978007c1d1af8916d0c7dc7b5826f60c8589.tar.gz |
iface-helper: merge branch 'th/iface-helper-logging-bgo772456'
https://bugzilla.gnome.org/show_bug.cgi?id=772456
-rw-r--r-- | src/devices/nm-device.c | 6 | ||||
-rw-r--r-- | src/nm-iface-helper.c | 103 | ||||
-rw-r--r-- | src/nm-logging.c | 128 | ||||
-rw-r--r-- | src/nm-logging.h | 4 |
4 files changed, 189 insertions, 52 deletions
diff --git a/src/devices/nm-device.c b/src/devices/nm-device.c index 5fd0a187f7..941f9ade84 100644 --- a/src/devices/nm-device.c +++ b/src/devices/nm-device.c @@ -10977,6 +10977,12 @@ nm_device_spawn_iface_helper (NMDevice *self) g_ptr_array_add (argv, logging_backend); } + g_ptr_array_add (argv, g_strdup ("--log-level")); + g_ptr_array_add (argv, g_strdup (nm_logging_level_to_string ())); + + g_ptr_array_add (argv, g_strdup ("--log-domains")); + g_ptr_array_add (argv, g_strdup (nm_logging_domains_to_string ())); + dhcp4_address = find_dhcp4_address (self); method = nm_utils_get_ip_config_method (connection, NM_TYPE_SETTING_IP4_CONFIG); diff --git a/src/nm-iface-helper.c b/src/nm-iface-helper.c index 3d270fa4df..9f66ff145f 100644 --- a/src/nm-iface-helper.c +++ b/src/nm-iface-helper.c @@ -52,8 +52,14 @@ extern unsigned int if_nametoindex (const char *__ifname); #define NMIH_PID_FILE_FMT NMRUNDIR "/nm-iface-helper-%d.pid" -static GMainLoop *main_loop = NULL; -static int ifindex = -1; +/*****************************************************************************/ + +static struct { + GMainLoop *main_loop; + int ifindex; +} gl/*obal*/ = { + .ifindex = -1, +}; static struct { gboolean slaac; @@ -84,6 +90,16 @@ static struct { .priority_v6 = NM_PLATFORM_ROUTE_METRIC_DEFAULT_IP6, }; +/*****************************************************************************/ + +#define _NMLOG_PREFIX_NAME "nm-iface-helper" +#define _NMLOG(level, domain, ...) \ + nm_log ((level), (domain), \ + "iface-helper: " _NM_UTILS_MACRO_FIRST (__VA_ARGS__) \ + _NM_UTILS_MACRO_REST (__VA_ARGS__)) + +/*****************************************************************************/ + static void dhcp4_state_changed (NMDhcpClient *client, NMDhcpState state, @@ -97,18 +113,18 @@ 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", global_opt.ifname, state); + _LOGD (LOGD_DHCP4, "new DHCPv4 client state %d", state); switch (state) { case NM_DHCP_STATE_BOUND: g_assert (ip4_config); - existing = nm_ip4_config_capture (ifindex, FALSE); + existing = nm_ip4_config_capture (gl.ifindex, FALSE); if (last_config) nm_ip4_config_subtract (existing, last_config); nm_ip4_config_merge (existing, ip4_config, NM_IP_CONFIG_MERGE_DEFAULT); - if (!nm_ip4_config_commit (existing, ifindex, TRUE, global_opt.priority_v4)) - nm_log_warn (LOGD_DHCP4, "(%s): failed to apply DHCPv4 config", global_opt.ifname); + if (!nm_ip4_config_commit (existing, gl.ifindex, TRUE, global_opt.priority_v4)) + _LOGW (LOGD_DHCP4, "failed to apply DHCPv4 config"); if (last_config) g_object_unref (last_config); @@ -119,10 +135,10 @@ dhcp4_state_changed (NMDhcpClient *client, case NM_DHCP_STATE_DONE: case NM_DHCP_STATE_FAIL: 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); + _LOGW (LOGD_DHCP4, "DHCPv4 timed out or failed, quitting..."); + g_main_loop_quit (gl.main_loop); } else - nm_log_warn (LOGD_DHCP4, "(%s): DHCPv4 timed out or failed", global_opt.ifname); + _LOGW (LOGD_DHCP4, "DHCPv4 timed out or failed"); break; default: break; @@ -159,11 +175,11 @@ rdisc_config_changed (NMRDisc *rdisc, const NMRDiscData *rdata, guint changed_in ifa_flags |= IFA_F_MANAGETEMPADDR; } - existing = nm_ip6_config_capture (ifindex, FALSE, global_opt.tempaddr); + existing = nm_ip6_config_capture (gl.ifindex, FALSE, global_opt.tempaddr); if (rdisc_config) nm_ip6_config_subtract (existing, rdisc_config); else - rdisc_config = nm_ip6_config_new (ifindex); + rdisc_config = nm_ip6_config_new (gl.ifindex); if (changed & NM_RDISC_CONFIG_GATEWAYS) { /* Use the first gateway as ordered in router discovery cache. */ @@ -234,24 +250,24 @@ rdisc_config_changed (NMRDisc *rdisc, const NMRDiscData *rdata, guint changed_in } nm_ip6_config_merge (existing, rdisc_config, NM_IP_CONFIG_MERGE_DEFAULT); - if (!nm_ip6_config_commit (existing, ifindex, TRUE)) - nm_log_warn (LOGD_IP6, "(%s): failed to apply IPv6 config", global_opt.ifname); + if (!nm_ip6_config_commit (existing, gl.ifindex, TRUE)) + _LOGW (LOGD_IP6, "failed to apply IPv6 config"); } static void rdisc_ra_timeout (NMRDisc *rdisc, gpointer user_data) { 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); + _LOGW (LOGD_IP6, "IPv6 timed out or failed, quitting..."); + g_main_loop_quit (gl.main_loop); } else - nm_log_warn (LOGD_IP6, "(%s): IPv6 timed out or failed", global_opt.ifname); + _LOGW (LOGD_IP6, "IPv6 timed out or failed"); } static gboolean quit_handler (gpointer user_data) { - g_main_loop_quit (main_loop); + g_main_loop_quit (gl.main_loop); return G_SOURCE_REMOVE; } @@ -263,7 +279,7 @@ setup_signals (void) g_unix_signal_add (SIGTERM, quit_handler, NULL); } -static void +static gboolean do_early_setup (int *argc, char **argv[]) { gint64 priority64_v4 = -1; @@ -306,12 +322,13 @@ do_early_setup (int *argc, char **argv[]) NULL, NULL, _("nm-iface-helper is a small, standalone process that manages a single network interface."))) - exit (1); + return FALSE; 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; + return TRUE; } static void @@ -346,7 +363,14 @@ main (int argc, char *argv[]) setpgid (getpid (), getpid ()); - do_early_setup (&argc, &argv); + if (!do_early_setup (&argc, &argv)) + return 1; + + nm_logging_set_syslog_identifier ("nm-iface-helper"); + nm_logging_set_prefix ("%s[%ld] (%s): ", + _NMLOG_PREFIX_NAME, + (long) getpid (), + global_opt.ifname ?: "???"); if (global_opt.g_fatal_warnings) { GLogLevelFlags fatal_mask; @@ -358,22 +382,22 @@ main (int argc, char *argv[]) if (global_opt.show_version) { fprintf (stdout, NM_DIST_VERSION "\n"); - exit (0); + return 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); + return 1; } - ifindex = if_nametoindex (global_opt.ifname); - if (ifindex <= 0) { + gl.ifindex = if_nametoindex (global_opt.ifname); + if (gl.ifindex <= 0) { fprintf (stderr, _("Failed to find interface index for %s (%s)\n"), global_opt.ifname, strerror (errno)); - exit (1); + return 1; } - pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, ifindex); + pidfile = g_strdup_printf (NMIH_PID_FILE_FMT, gl.ifindex); nm_main_utils_ensure_not_running_pidfile (pidfile); nm_main_utils_ensure_rundir (); @@ -385,7 +409,7 @@ main (int argc, char *argv[]) fprintf (stderr, _("%s. Please use --help to see a list of valid options.\n"), error->message); - exit (1); + return 1; } else if (bad_domains) { fprintf (stderr, _("Ignoring unrecognized log domain(s) '%s' passed on command line.\n"), @@ -401,26 +425,26 @@ main (int argc, char *argv[]) fprintf (stderr, _("Could not daemonize: %s [error %u]\n"), g_strerror (saved_errno), saved_errno); - exit (1); + return 1; } if (nm_main_utils_write_pidfile (pidfile)) wrote_pidfile = TRUE; } /* Set up unix signal handling - before creating threads, but after daemonizing! */ - main_loop = g_main_loop_new (NULL, FALSE); + gl.main_loop = g_main_loop_new (NULL, FALSE); setup_signals (); nm_logging_syslog_openlog (global_opt.logging_backend ? global_opt.logging_backend : (global_opt.debug ? "debug" : NULL)); - nm_log_info (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting..."); + _LOGI (LOGD_CORE, "nm-iface-helper (version " NM_DIST_VERSION ") is starting..."); /* Set up platform interaction layer */ nm_linux_platform_setup (); - tmp = nm_platform_link_get_address (NM_PLATFORM_GET, ifindex, &hwaddr_len); + tmp = nm_platform_link_get_address (NM_PLATFORM_GET, gl.ifindex, &hwaddr_len); if (tmp) { hwaddr = g_byte_array_sized_new (hwaddr_len); g_byte_array_append (hwaddr, tmp, hwaddr_len); @@ -433,7 +457,7 @@ main (int argc, char *argv[]) bytes = nm_utils_hexstr2bin (global_opt.iid_str); if (!bytes || g_bytes_get_size (bytes) != sizeof (*iid)) { fprintf (stderr, _("(%s): Invalid IID %s\n"), global_opt.ifname, global_opt.iid_str); - exit (1); + return 1; } iid = g_bytes_unref_to_data (bytes, &ignored); } @@ -443,7 +467,7 @@ main (int argc, char *argv[]) dhcp4_client = nm_dhcp_manager_start_ip4 (nm_dhcp_manager_get (), global_opt.ifname, - ifindex, + gl.ifindex, hwaddr, global_opt.uuid, global_opt.priority_v4, @@ -465,7 +489,7 @@ main (int argc, char *argv[]) NMUtilsStableType stable_type = NM_UTILS_STABLE_TYPE_UUID; const char *stable_id = global_opt.uuid; - nm_platform_link_set_user_ipv6ll_enabled (NM_PLATFORM_GET, ifindex, TRUE); + nm_platform_link_set_user_ipv6ll_enabled (NM_PLATFORM_GET, gl.ifindex, TRUE); if ( global_opt.stable_id && (global_opt.stable_id[0] >= '0' && global_opt.stable_id[0] <= '9') @@ -476,7 +500,7 @@ main (int argc, char *argv[]) stable_type = (global_opt.stable_id[0] - '0'); stable_id = &global_opt.stable_id[2]; } - rdisc = nm_lndp_rdisc_new (NM_PLATFORM_GET, ifindex, global_opt.ifname, + rdisc = nm_lndp_rdisc_new (NM_PLATFORM_GET, gl.ifindex, global_opt.ifname, stable_type, stable_id, global_opt.addr_gen_mode, NULL); g_assert (rdisc); @@ -506,17 +530,18 @@ main (int argc, char *argv[]) sd_id = nm_sd_event_attach_default (); - g_main_loop_run (main_loop); + g_main_loop_run (gl.main_loop); g_clear_pointer (&hwaddr, g_byte_array_unref); if (pidfile && wrote_pidfile) unlink (pidfile); - nm_log_info (LOGD_CORE, "exiting"); + _LOGI (LOGD_CORE, "exiting"); nm_clear_g_source (&sd_id); - exit (0); + g_clear_pointer (&gl.main_loop, g_main_loop_unref); + return 0; } /*****************************************************************************/ @@ -525,7 +550,7 @@ main (int argc, char *argv[]) void nm_main_config_reload (int signal) { - nm_log_info (LOGD_CORE, "reloading configuration not supported"); + _LOGI (LOGD_CORE, "reloading configuration not supported"); } gconstpointer nm_config_get (void); diff --git a/src/nm-logging.c b/src/nm-logging.c index 51dd2aad2c..baa8198f8f 100644 --- a/src/nm-logging.c +++ b/src/nm-logging.c @@ -79,9 +79,12 @@ NMLogDomain _nm_logging_enabled_state[_LOGL_N_REAL] = { [LOGL_ERR] = LOGD_DEFAULT, }; -static struct { +static struct Global { NMLogLevel log_level; bool uses_syslog:1; + bool syslog_identifier_initialized:1; + const char *prefix; + const char *syslog_identifier; enum { LOG_BACKEND_GLIB, LOG_BACKEND_SYSLOG, @@ -98,6 +101,8 @@ static struct { /* nm_logging_setup ("INFO", LOGD_DEFAULT_STRING, NULL, NULL); */ .log_level = LOGL_INFO, .log_backend = LOG_BACKEND_GLIB, + .syslog_identifier = "SYSLOG_IDENTIFIER="G_LOG_DOMAIN, + .prefix = "", .level_desc = { [LOGL_TRACE] = { "TRACE", "<trace>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, }, [LOGL_DEBUG] = { "DEBUG", "<debug>", LOG_DEBUG, G_LOG_LEVEL_DEBUG, }, @@ -167,6 +172,70 @@ static char *_domains_to_string (gboolean include_level_override); /*****************************************************************************/ static gboolean +_syslog_identifier_valid_domain (const char *domain) +{ + char c; + + if (!domain || !domain[0]) + return FALSE; + + /* we pass the syslog identifier as format string. No funny stuff. */ + + for (; (c = domain[0]); domain++) { + if ( (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || (c >= '0' && c <= '9') + || NM_IN_SET (c, '-', '_')) + continue; + return FALSE; + } + return TRUE; +} + +static gboolean +_syslog_identifier_assert (const struct Global *gl) +{ + g_assert (gl); + g_assert (gl->syslog_identifier); + g_assert (g_str_has_prefix (gl->syslog_identifier, "SYSLOG_IDENTIFIER=")); + g_assert (_syslog_identifier_valid_domain (&gl->syslog_identifier[NM_STRLEN ("SYSLOG_IDENTIFIER=")])); + return TRUE; +} + +static const char * +syslog_identifier_domain (const struct Global *gl) +{ + nm_assert (_syslog_identifier_assert (gl)); + return &gl->syslog_identifier[NM_STRLEN ("SYSLOG_IDENTIFIER=")]; +} + +static const char * +syslog_identifier_full (const struct Global *gl) +{ + nm_assert (_syslog_identifier_assert (gl)); + return &gl->syslog_identifier[0]; +} + +void +nm_logging_set_syslog_identifier (const char *domain) +{ + if (global.log_backend != LOG_BACKEND_GLIB) + g_return_if_reached (); + + if (!_syslog_identifier_valid_domain (domain)) + g_return_if_reached (); + + if (global.syslog_identifier_initialized) + g_return_if_reached (); + + global.syslog_identifier_initialized = TRUE; + global.syslog_identifier = g_strdup_printf ("SYSLOG_IDENTIFIER=%s", domain); + nm_assert (_syslog_identifier_assert (&global)); +} + +/*****************************************************************************/ + +static gboolean match_log_level (const char *level, NMLogLevel *out_level, GError **error) @@ -470,13 +539,20 @@ _iovec_set_format (struct iovec *iov, gboolean *iov_free, int i, const char *for } static void -_iovec_set_string (struct iovec *iov, gboolean *iov_free, int i, const char *str, gsize len) +_iovec_set_string_l (struct iovec *iov, gboolean *iov_free, int i, const char *str, gsize len) { iov[i].iov_base = (char *) str; iov[i].iov_len = len; iov_free[i] = FALSE; } -#define _iovec_set_literal_string(iov, iov_free, i, str) _iovec_set_string ((iov), (iov_free), (i), (""str""), NM_STRLEN (str)) + +static void +_iovec_set_string (struct iovec *iov, gboolean *iov_free, int i, const char *str) +{ + _iovec_set_string_l (iov, iov_free, i, str, strlen (str)); +} + +#define _iovec_set_literal_string(iov, iov_free, i, str) _iovec_set_string_l ((iov), (iov_free), (i), (""str""), NM_STRLEN (str)) #endif void @@ -531,11 +607,12 @@ _nm_log_impl (const char *file, _iovec_set_format (iov, iov_free, i_field++, "PRIORITY=%d", global.level_desc[level].syslog_level); _iovec_set_format (iov, iov_free, i_field++, "MESSAGE=" - "%-7s%s %s", + "%s%-7s%s %s", + global.prefix, global.level_desc[level].level_str, s_buf_timestamp, msg); - _iovec_set_literal_string (iov, iov_free, i_field++, "SYSLOG_IDENTIFIER=" G_LOG_DOMAIN); + _iovec_set_string (iov, iov_free, i_field++, syslog_identifier_full (&global)); _iovec_set_format (iov, iov_free, i_field++, "SYSLOG_PID=%ld", (long) getpid ()); { const LogDesc *diter; @@ -602,7 +679,8 @@ _nm_log_impl (const char *file, break; #endif default: - fullmsg = g_strdup_printf ("%-7s%s %s", + fullmsg = g_strdup_printf ("%s%-7s%s %s", + global.prefix, global.level_desc[level].level_str, s_buf_timestamp, msg); @@ -610,7 +688,7 @@ _nm_log_impl (const char *file, if (global.log_backend == LOG_BACKEND_SYSLOG) syslog (global.level_desc[level].syslog_level, "%s", fullmsg); else - g_log (G_LOG_DOMAIN, global.level_desc[level].g_log_level, "%s", fullmsg); + g_log (syslog_identifier_domain (&global), global.level_desc[level].g_log_level, "%s", fullmsg); g_free (fullmsg); break; } @@ -660,8 +738,8 @@ nm_log_handler (const gchar *log_domain, boottime = nm_utils_monotonic_timestamp_as_boottime (now, 1); sd_journal_send ("PRIORITY=%d", syslog_priority, - "MESSAGE=%s", message ?: "", - "SYSLOG_IDENTIFIER=%s", G_LOG_DOMAIN, + "MESSAGE=%s%s", global.prefix, message ?: "", + syslog_identifier_full (&global), "SYSLOG_PID=%ld", (long) getpid (), "SYSLOG_FACILITY=GLIB", "GLIB_DOMAIN=%s", log_domain ?: "", @@ -673,7 +751,7 @@ nm_log_handler (const gchar *log_domain, break; #endif default: - syslog (syslog_priority, "%s", message ?: ""); + syslog (syslog_priority, "%s%s", global.prefix, message ?: ""); break; } } @@ -685,6 +763,30 @@ nm_logging_syslog_enabled (void) } void +nm_logging_set_prefix (const char *format, ...) +{ + char *prefix; + va_list ap; + + /* prefix can only be set once, to a non-empty string. Also, after + * nm_logging_syslog_openlog() the prefix cannot be set either. */ + if (global.log_backend != LOG_BACKEND_GLIB) + g_return_if_reached (); + if (global.prefix[0]) + g_return_if_reached (); + + va_start (ap, format); + prefix = g_strdup_vprintf (format, ap); + va_end (ap); + + if (!prefix || !prefix[0]) + g_return_if_reached (); + + /* we pass the allocated string on and never free it. */ + global.prefix = prefix; +} + +void nm_logging_syslog_openlog (const char *logging_backend) { if (global.log_backend != LOG_BACKEND_GLIB) @@ -695,7 +797,7 @@ nm_logging_syslog_openlog (const char *logging_backend) if (strcmp (logging_backend, "debug") == 0) { global.log_backend = LOG_BACKEND_SYSLOG; - openlog (G_LOG_DOMAIN, LOG_CONS | LOG_PERROR | LOG_PID, LOG_USER); + openlog (syslog_identifier_domain (&global), LOG_CONS | LOG_PERROR | LOG_PID, LOG_USER); #if SYSTEMD_JOURNAL } else if (strcmp (logging_backend, "syslog") != 0) { global.log_backend = LOG_BACKEND_JOURNAL; @@ -708,10 +810,10 @@ nm_logging_syslog_openlog (const char *logging_backend) } else { global.log_backend = LOG_BACKEND_SYSLOG; global.uses_syslog = TRUE; - openlog (G_LOG_DOMAIN, LOG_PID, LOG_DAEMON); + openlog (syslog_identifier_domain (&global), LOG_PID, LOG_DAEMON); } - g_log_set_handler (G_LOG_DOMAIN, + g_log_set_handler (syslog_identifier_domain (&global), G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, nm_log_handler, NULL); diff --git a/src/nm-logging.h b/src/nm-logging.h index 9f85c16990..917a2ee0be 100644 --- a/src/nm-logging.h +++ b/src/nm-logging.h @@ -188,6 +188,10 @@ gboolean nm_logging_setup (const char *level, const char *domains, char **bad_domains, GError **error); + +void nm_logging_set_syslog_identifier (const char *domain); +void nm_logging_set_prefix (const char *format, ...) _nm_printf (1, 2); + void nm_logging_syslog_openlog (const char *logging_backend); gboolean nm_logging_syslog_enabled (void); |