diff options
author | Thomas Haller <thaller@redhat.com> | 2015-01-05 19:39:56 +0100 |
---|---|---|
committer | Thomas Haller <thaller@redhat.com> | 2015-01-12 12:10:03 +0100 |
commit | f4bf50bf4a19797df6db4f91813ea06fce9fbc02 (patch) | |
tree | d1d4432839520741d345855e120d10ae6ad493d6 /src/settings/nm-inotify-helper.c | |
parent | e2739cfc1b635363bf442be84a45b333afde81b1 (diff) | |
download | NetworkManager-f4bf50bf4a19797df6db4f91813ea06fce9fbc02.tar.gz |
core: declare nm_inotify_helper_get() using NM_DEFINE_SINGLETON_GETTER()
Refactor NMInotifyHelper to implement the singleton getter using
NM_DEFINE_SINGLETON_GETTER().
For one this means that the getter no longer increments the reference
count. This was anyway wrong, because no caller of nm_inotify_helper_get()
unrefered the returned reference, hence leaking the singleton.
Also, the getter can no longer fail to create the singleton instance.
Note that none of the callers actually coped with a failure to get
the singleton.
Instead return an instance that does nothing.
One downside (upside?) of this is that we only try once to initialize
the inotify handle.
Diffstat (limited to 'src/settings/nm-inotify-helper.c')
-rw-r--r-- | src/settings/nm-inotify-helper.c | 43 |
1 files changed, 20 insertions, 23 deletions
diff --git a/src/settings/nm-inotify-helper.c b/src/settings/nm-inotify-helper.c index 41f0268d1c..3732e77a39 100644 --- a/src/settings/nm-inotify-helper.c +++ b/src/settings/nm-inotify-helper.c @@ -24,6 +24,7 @@ #include <string.h> #include <sys/inotify.h> #include <glib.h> +#include <errno.h> #include "nm-inotify-helper.h" #include "nm-logging.h" @@ -55,9 +56,10 @@ nm_inotify_helper_add_watch (NMInotifyHelper *self, const char *path) { NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self); int wd; - guint32 refcount; + guint refcount; - g_return_val_if_fail (priv->ifd >= 0, -1); + if (priv->ifd < 0) + return -1; /* We only care about modifications since we're just trying to get change * notifications on hardlinks. @@ -78,9 +80,10 @@ void nm_inotify_helper_remove_watch (NMInotifyHelper *self, int wd) { NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (self); - guint32 refcount; + guint refcount; - g_return_if_fail (priv->ifd >= 0); + if (priv->ifd < 0) + return; refcount = GPOINTER_TO_UINT (g_hash_table_lookup (priv->wd_refs, GINT_TO_POINTER (wd))); if (!refcount) @@ -128,7 +131,9 @@ init_inotify (NMInotifyHelper *self) priv->ifd = inotify_init (); if (priv->ifd == -1) { - nm_log_warn (LOGD_SETTINGS, "couldn't initialize inotify"); + int errsv = errno; + + nm_log_warn (LOGD_SETTINGS, "couldn't initialize inotify: %s (%d)", strerror (errsv), errsv); return FALSE; } @@ -145,24 +150,7 @@ init_inotify (NMInotifyHelper *self) return TRUE; } -NMInotifyHelper * -nm_inotify_helper_get (void) -{ - static NMInotifyHelper *singleton_instance = NULL; - - if (!singleton_instance) { - singleton_instance = (NMInotifyHelper *) g_object_new (NM_TYPE_INOTIFY_HELPER, NULL); - - if (!init_inotify (singleton_instance)) { - g_clear_object (&singleton_instance); - return NULL; - } - } else - g_object_ref (singleton_instance); - - g_assert (singleton_instance); - return singleton_instance; -} +NM_DEFINE_SINGLETON_GETTER (NMInotifyHelper, nm_inotify_helper_get, NM_TYPE_INOTIFY_HELPER); static void nm_inotify_helper_init (NMInotifyHelper *self) @@ -173,6 +161,14 @@ nm_inotify_helper_init (NMInotifyHelper *self) } static void +constructed (GObject *object) +{ + G_OBJECT_CLASS (nm_inotify_helper_parent_class)->constructed (object); + + init_inotify (NM_INOTIFY_HELPER (object)); +} + +static void finalize (GObject *object) { NMInotifyHelperPrivate *priv = NM_INOTIFY_HELPER_GET_PRIVATE (object); @@ -193,6 +189,7 @@ nm_inotify_helper_class_init (NMInotifyHelperClass *klass) g_type_class_add_private (klass, sizeof (NMInotifyHelperPrivate)); /* Virtual methods */ + object_class->constructed = constructed; object_class->finalize = finalize; /* Signals */ |