summaryrefslogtreecommitdiff
path: root/shared
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2019-01-16 16:40:45 +0100
committerThomas Haller <thaller@redhat.com>2019-02-05 08:18:08 +0100
commitfcfd4f4ff29b1399da5e88da32787725a8d193a8 (patch)
tree99719a035a5a9d66db622f81ee576041ee5ea21a /shared
parent834c092b51d6bb1f4e0ef4c1eedb018acd9a0b3b (diff)
downloadNetworkManager-fcfd4f4ff29b1399da5e88da32787725a8d193a8.tar.gz
logging: make nm-logging thread-safe
NetworkManager is single-threaded and uses a mainloop. However, sometimes we may need multiple threads. For example, we will need to write sysctl values asynchronously, using the glib thread-pool. For that to work, we also need to switch the network-namespace of the thread-pool thread. We want to use NMPNetns for that. Hence it's better to have NMPNetns thread-safe, instead of coming up with a duplicate implementation. But NMPNetns may want to log, so we also need nm-logging thread-safe. In general, code under "shared/nm-utils" and nm-logging should be usable from multiple threads. It's simpler to make this code thread-safe than re-implementing it. Also, it's a bad limitation to be unable to log from other threads. If there is an error, the best we can often do is to log about it. Make nm-logging thread-safe. Actually, we only need to be able to log from multiple threads. We don't need to setup or configure logging from multiple threads. This restriction allows us to access logging from the main-thread without any thread-synchronization (because all changes in the logging setup are also done from the main-thread). So, while logging from other threads requires a mutex, logging from the main-thread is lock-free.
Diffstat (limited to 'shared')
-rw-r--r--shared/nm-utils/nm-logging-fwd.h8
-rw-r--r--shared/systemd/nm-logging-stub.c6
-rw-r--r--shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h4
3 files changed, 11 insertions, 7 deletions
diff --git a/shared/nm-utils/nm-logging-fwd.h b/shared/nm-utils/nm-logging-fwd.h
index 303d59514c..900dfff812 100644
--- a/shared/nm-utils/nm-logging-fwd.h
+++ b/shared/nm-utils/nm-logging-fwd.h
@@ -94,18 +94,20 @@ typedef enum { /*< skip >*/
_LOGL_N, /* the number of logging levels including "OFF" */
} NMLogLevel;
-gboolean _nm_log_enabled (NMLogLevel level,
- NMLogDomain domain);
+gboolean _nm_log_enabled_impl (gboolean mt_require_locking,
+ NMLogLevel level,
+ NMLogDomain domain);
void _nm_log_impl (const char *file,
guint line,
const char *func,
+ gboolean mt_require_locking,
NMLogLevel level,
NMLogDomain domain,
int error,
const char *ifname,
const char *con_uuid,
const char *fmt,
- ...) _nm_printf (9, 10);
+ ...) _nm_printf (10, 11);
#endif /* __NM_LOGGING_DEFINES_H__ */
diff --git a/shared/systemd/nm-logging-stub.c b/shared/systemd/nm-logging-stub.c
index 80d4360783..5be69b4bf3 100644
--- a/shared/systemd/nm-logging-stub.c
+++ b/shared/systemd/nm-logging-stub.c
@@ -24,8 +24,9 @@
/*****************************************************************************/
gboolean
-_nm_log_enabled (NMLogLevel level,
- NMLogDomain domain)
+_nm_log_enabled_impl (gboolean mt_require_locking,
+ NMLogLevel level,
+ NMLogDomain domain)
{
return FALSE;
}
@@ -34,6 +35,7 @@ void
_nm_log_impl (const char *file,
guint line,
const char *func,
+ gboolean mt_require_locking,
NMLogLevel level,
NMLogDomain domain,
int error,
diff --git a/shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h b/shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h
index 905e45c0e1..f68144d896 100644
--- a/shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h
+++ b/shared/systemd/sd-adapt-shared/nm-sd-adapt-shared.h
@@ -54,10 +54,10 @@ _nm_log_get_max_level_realm (void)
const int _nm_e = (error); \
const NMLogLevel _nm_l = _slog_level_to_nm ((level)); \
\
- if (_nm_log_enabled (_nm_l, LOGD_SYSTEMD)) { \
+ if (_nm_log_enabled_impl (!(NM_THREAD_SAFE_ON_MAIN_THREAD), _nm_l, LOGD_SYSTEMD)) { \
const char *_nm_location = strrchr ((""file), '/'); \
\
- _nm_log_impl (_nm_location ? _nm_location + 1 : (""file), (line), (func), _nm_l, LOGD_SYSTEMD, _nm_e, NULL, NULL, ("%s"format), "libsystemd: ", ## __VA_ARGS__); \
+ _nm_log_impl (_nm_location ? _nm_location + 1 : (""file), (line), (func), !(NM_THREAD_SAFE_ON_MAIN_THREAD), _nm_l, LOGD_SYSTEMD, _nm_e, NULL, NULL, ("%s"format), "libsystemd: ", ## __VA_ARGS__); \
} \
(_nm_e > 0 ? -_nm_e : _nm_e); \
})