summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-05-14 12:39:27 +0200
committerThomas Haller <thaller@redhat.com>2019-05-17 21:12:38 +0200
commitf8cbf06a77cbe5ef6d24a6f6b35f7b5607a2f530 (patch)
tree6eda8c32763fe5fb3042ba7c2f5c74abd66f6a0a
parent9cddb9f8bd77325935296e75cbc03499cfa0a1b7 (diff)
downloadNetworkManager-f8cbf06a77cbe5ef6d24a6f6b35f7b5607a2f530.tar.gz
logging: use stack allocated string buffer to constuct NM_LOG_DOMAINS field
NM_LOG_DOMAINS is a comma-separated list of the selected logging domains. As the number of all logging domains is fixed at compile-time, the maximum length of the buffer is known. $ git grep $'^\t{ LOGD_' | sed 's/.*"\(.*\)" .*/\1/' | xargs | sed 's/ */,/g' | sed 's/^/NM_LOG_DOMAINS=/' NM_LOG_DOMAINS=PLATFORM,RFKILL,ETHER,WIFI,BT,MB,DHCP4,DHCP6,PPP,WIFI_SCAN,IP4,IP6,AUTOIP4,DNS,VPN,SHARING,SUPPLICANT,AGENTS,SETTINGS,SUSPEND,CORE,DEVICE,OLPC,INFINIBAND,FIREWALL,ADSL,BOND,VLAN,BRIDGE,DBUS_PROPS,TEAM,CONCHECK,DCB,DISPATCH,AUDIT,SYSTEMD,VPN_PLUGIN,PROXY Note that the static buffer "_all_logging_domains_to_str" is known to be large enough to contain these domain names (it's even longer, as it also contains "ALL", "IP", and "DHCP" alises). We can use that to define the array of suitable size.
-rw-r--r--src/nm-logging.c29
1 files changed, 9 insertions, 20 deletions
diff --git a/src/nm-logging.c b/src/nm-logging.c
index eeffeb657f..dfc6a19dbc 100644
--- a/src/nm-logging.c
+++ b/src/nm-logging.c
@@ -775,7 +775,6 @@ _nm_log_impl (const char *file,
struct iovec *iov = iov_data;
char *iov_free_data[5];
char **iov_free = iov_free_data;
- nm_auto_free_gstring GString *s_domain_all = NULL;
now = nm_utils_get_monotonic_timestamp_ns ();
boottime = nm_utils_monotonic_timestamp_as_boottime (now, 1);
@@ -786,32 +785,22 @@ _nm_log_impl (const char *file,
_iovec_set_format_a (iov++, 30, "SYSLOG_PID=%ld", (long) getpid ());
{
const LogDesc *diter;
- const char *s_domain_1 = NULL;
NMLogDomain dom_all = domain;
+ char s_log_domains_buf[NM_STRLEN ("NM_LOG_DOMAINS=") + sizeof (_all_logging_domains_to_str)];
+ char *s_log_domains = s_log_domains_buf;
+ gsize l_log_domains = sizeof (s_log_domains_buf);
+ nm_utils_strbuf_append_str (&s_log_domains, &l_log_domains, "NM_LOG_DOMAINS=");
for (diter = &domain_desc[0]; dom_all != 0 && diter->name; diter++) {
if (!NM_FLAGS_ANY (dom_all, diter->num))
continue;
-
- /* construct a list of all domains (not only the enabled ones).
- * Note that in by far most cases, there is only one domain present.
- * Hence, save the construction of the GString. */
+ if (dom_all != domain)
+ nm_utils_strbuf_append_c (&s_log_domains, &l_log_domains, ',');
+ nm_utils_strbuf_append_str (&s_log_domains, &l_log_domains, diter->name);
dom_all &= ~diter->num;
- if (!s_domain_1)
- s_domain_1 = diter->name;
- else {
- if (!s_domain_all) {
- s_domain_all = g_string_new ("NM_LOG_DOMAINS=");
- g_string_append (s_domain_all, s_domain_1);
- }
- g_string_append_c (s_domain_all, ',');
- g_string_append (s_domain_all, diter->name);
- }
}
- if (s_domain_all)
- _iovec_set (iov++, s_domain_all->str, s_domain_all->len);
- else
- _iovec_set_format_str_a (iov++, 30, "NM_LOG_DOMAINS=%s", s_domain_1);
+ nm_assert (l_log_domains > 0);
+ _iovec_set (iov++, s_log_domains_buf, s_log_domains - s_log_domains_buf);
}
G_STATIC_ASSERT_EXPR (LOG_FAC (LOG_DAEMON) == 3);
_iovec_set_string_literal (iov++, "SYSLOG_FACILITY=3");