core: move creating singleton instance out of "nm-platform.c"

In core, NMPlatform is (also) a singleton instance. As we will move platform code
to libnm-platform, this singleton part makes no sense there. Move the code
to NetworkManagerUtils.c.
This commit is contained in:
Thomas Haller 2021-03-04 07:54:57 +01:00
parent 9113a672cf
commit d3585243c3
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728
7 changed files with 72 additions and 66 deletions

View file

@ -23,6 +23,7 @@
#include "platform/nmp-object.h"
#include "platform/nm-platform.h"
#include "platform/nm-linux-platform.h"
#include "nm-auth-utils.h"
#include "libnm-systemd-shared/nm-sd-utils-shared.h"
@ -1829,3 +1830,63 @@ nm_utils_share_rules_add_all_rules(NMUtilsShareRules *self,
"INPUT --in-interface %s --protocol tcp --destination-port 53 --jump ACCEPT",
ip_iface);
}
/*****************************************************************************/
/* Singleton NMPlatform subclass instance and cached class object */
NM_DEFINE_SINGLETON_INSTANCE(NMPlatform);
NM_DEFINE_SINGLETON_REGISTER(NMPlatform);
/**
* nm_platform_setup:
* @instance: the #NMPlatform instance
*
* Failing to set up #NMPlatform singleton results in a fatal error,
* as well as trying to initialize it multiple times without freeing
* it.
*
* NetworkManager will typically use only one platform object during
* its run. Test programs might want to switch platform implementations,
* though.
*/
void
nm_platform_setup(NMPlatform *instance)
{
g_return_if_fail(NM_IS_PLATFORM(instance));
g_return_if_fail(!singleton_instance);
singleton_instance = instance;
nm_singleton_instance_register();
nm_log_dbg(LOGD_CORE,
"setup %s singleton (" NM_HASH_OBFUSCATE_PTR_FMT ")",
"NMPlatform",
NM_HASH_OBFUSCATE_PTR(instance));
}
/**
* nm_platform_get:
* @self: platform instance
*
* Retrieve #NMPlatform singleton. Use this whenever you want to connect to
* #NMPlatform signals. It is an error to call it before nm_platform_setup().
*
* Returns: (transfer none): The #NMPlatform singleton reference.
*/
NMPlatform *
nm_platform_get()
{
g_assert(singleton_instance);
return singleton_instance;
}
/*****************************************************************************/
void
nm_linux_platform_setup(void)
{
nm_platform_setup(nm_linux_platform_new(FALSE, FALSE));
}

View file

@ -251,4 +251,13 @@ void nm_utils_share_rules_apply(NMUtilsShareRules *self, gboolean shared);
/*****************************************************************************/
void nm_platform_setup(NMPlatform *instance);
NMPlatform *nm_platform_get(void);
#define NM_PLATFORM_GET (nm_platform_get())
void nm_linux_platform_setup(void);
/*****************************************************************************/
#endif /* __NETWORKMANAGER_UTILS_H__ */

View file

@ -9,6 +9,7 @@
#include <net/ethernet.h>
#include "NetworkManagerUtils.h"
#include "libnm-std-aux/unaligned.h"
#include "platform/nm-platform.h"
#include "libnm-glib-aux/nm-c-list.h"

View file

@ -7732,7 +7732,7 @@ link_set_sriov_params_async(NMPlatform * platform,
if (NM_IN_SET(autoprobe, NM_OPTION_BOOL_TRUE, NM_OPTION_BOOL_FALSE)
&& current_autoprobe != autoprobe
&& !nm_platform_sysctl_set(
NM_PLATFORM_GET,
platform,
NMP_SYSCTL_PATHID_NETDIR(dirfd, ifname, "device/sriov_drivers_autoprobe"),
nm_sprintf_buf(buf, "%d", (int) autoprobe))) {
g_set_error(&error,
@ -9361,14 +9361,6 @@ handle_udev_event(NMUdevClient *udev_client, struct udev_device *udevice, gpoint
/*****************************************************************************/
void
nm_linux_platform_setup(void)
{
nm_platform_setup(nm_linux_platform_new(FALSE, FALSE));
}
/*****************************************************************************/
static void
nm_linux_platform_init(NMLinuxPlatform *self)
{

View file

@ -25,6 +25,4 @@ GType nm_linux_platform_get_type(void);
NMPlatform *nm_linux_platform_new(gboolean log_with_ptr, gboolean netns_support);
void nm_linux_platform_setup(void);
#endif /* __NETWORKMANAGER_LINUX_PLATFORM_H__ */

View file

@ -225,11 +225,6 @@ _nm_platform_signal_id_get(NMPlatformSignalIdType signal_type)
/*****************************************************************************/
/* Singleton NMPlatform subclass instance and cached class object */
NM_DEFINE_SINGLETON_INSTANCE(NMPlatform);
NM_DEFINE_SINGLETON_REGISTER(NMPlatform);
/* Just always initialize a @klass instance. NM_PLATFORM_GET_CLASS()
* is only a plain read on the self instance, which the compiler
* like can optimize out.
@ -261,51 +256,6 @@ NM_DEFINE_SINGLETON_REGISTER(NMPlatform);
return (err_val); \
} while (0)
/**
* nm_platform_setup:
* @instance: the #NMPlatform instance
*
* Failing to set up #NMPlatform singleton results in a fatal error,
* as well as trying to initialize it multiple times without freeing
* it.
*
* NetworkManager will typically use only one platform object during
* its run. Test programs might want to switch platform implementations,
* though.
*/
void
nm_platform_setup(NMPlatform *instance)
{
g_return_if_fail(NM_IS_PLATFORM(instance));
g_return_if_fail(!singleton_instance);
singleton_instance = instance;
nm_singleton_instance_register();
nm_log_dbg(LOGD_CORE,
"setup %s singleton (" NM_HASH_OBFUSCATE_PTR_FMT ")",
"NMPlatform",
NM_HASH_OBFUSCATE_PTR(instance));
}
/**
* nm_platform_get:
* @self: platform instance
*
* Retrieve #NMPlatform singleton. Use this whenever you want to connect to
* #NMPlatform signals. It is an error to call it before nm_platform_setup().
*
* Returns: (transfer none): The #NMPlatform singleton reference.
*/
NMPlatform *
nm_platform_get()
{
g_assert(singleton_instance);
return singleton_instance;
}
/*****************************************************************************/
NMDedupMultiIndex *

View file

@ -1282,11 +1282,6 @@ const char *nm_platform_signal_change_type_to_string(NMPlatformSignalChangeType
GType nm_platform_get_type(void);
void nm_platform_setup(NMPlatform *instance);
NMPlatform *nm_platform_get(void);
#define NM_PLATFORM_GET (nm_platform_get())
/*****************************************************************************/
static inline in_addr_t