[gnome-settings-daemon] rfkill: Refactor error handling for cc_rfkill_glib_open()



commit d0bc039706410ca57379a62cfa65381575d70091
Author: Philip Withnall <withnall endlessm com>
Date:   Mon Mar 27 10:46:12 2017 +0100

    rfkill: Refactor error handling for cc_rfkill_glib_open()
    
    Returning the FD is a bad idea, since ownership of it is already
    transferred to the GUnixInputStream inside rfkill-glib.c. The only
    caller (in gsd-rfkill-manager.c) doesn’t use the return value anyway.
    
    Tidy this up a bit by using a GError instead. This changes how warning
    messages are printed, but otherwise introduces no functional changes.
    
    Coverity ID: 1418249
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780587

 plugins/rfkill/gsd-rfkill-manager.c |    8 +++++++-
 plugins/rfkill/rfkill-glib.c        |   22 ++++++++++++----------
 plugins/rfkill/rfkill-glib.h        |    3 ++-
 3 files changed, 21 insertions(+), 12 deletions(-)
---
diff --git a/plugins/rfkill/gsd-rfkill-manager.c b/plugins/rfkill/gsd-rfkill-manager.c
index 6e6e130..17933c5 100644
--- a/plugins/rfkill/gsd-rfkill-manager.c
+++ b/plugins/rfkill/gsd-rfkill-manager.c
@@ -652,6 +652,8 @@ gboolean
 gsd_rfkill_manager_start (GsdRfkillManager *manager,
                          GError         **error)
 {
+        g_autoptr(GError) local_error = NULL;
+
         gnome_settings_profile_start (NULL);
 
         manager->priv->introspection_data = g_dbus_node_info_new_for_xml (introspection_xml, NULL);
@@ -662,7 +664,11 @@ gsd_rfkill_manager_start (GsdRfkillManager *manager,
         manager->priv->rfkill = cc_rfkill_glib_new ();
         g_signal_connect (G_OBJECT (manager->priv->rfkill), "changed",
                           G_CALLBACK (rfkill_changed), manager);
-        cc_rfkill_glib_open (manager->priv->rfkill);
+
+        if (!cc_rfkill_glib_open (manager->priv->rfkill, &local_error)) {
+                g_warning ("Error setting up rfkill: %s", local_error->message);
+                g_clear_error (&local_error);
+        }
 
         manager->priv->cancellable = g_cancellable_new ();
 
diff --git a/plugins/rfkill/rfkill-glib.c b/plugins/rfkill/rfkill-glib.c
index 0ef265f..3ba4f89 100644
--- a/plugins/rfkill/rfkill-glib.c
+++ b/plugins/rfkill/rfkill-glib.c
@@ -358,28 +358,30 @@ cc_rfkill_glib_init (CcRfkillGlib *rfkill)
 {
 }
 
-int
-cc_rfkill_glib_open (CcRfkillGlib *rfkill)
+gboolean
+cc_rfkill_glib_open (CcRfkillGlib  *rfkill,
+                     GError       **error)
 {
        int fd;
        int ret;
        GList *events;
 
-       g_return_val_if_fail (CC_RFKILL_IS_GLIB (rfkill), -1);
-       g_return_val_if_fail (rfkill->stream == NULL, -1);
+       g_return_val_if_fail (CC_RFKILL_IS_GLIB (rfkill), FALSE);
+       g_return_val_if_fail (rfkill->stream == NULL, FALSE);
 
        fd = open("/dev/rfkill", O_RDWR);
        if (fd < 0) {
-               if (errno == EACCES)
-                       g_warning ("Could not open RFKILL control device, please verify your installation");
-               return fd;
+               g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (errno),
+                                    "Could not open RFKILL control device, please verify your installation");
+               return FALSE;
        }
 
        ret = fcntl(fd, F_SETFL, O_NONBLOCK);
        if (ret < 0) {
-               g_debug ("Can't set RFKILL control device to non-blocking");
+               g_set_error_literal (error, G_IO_ERROR, g_io_error_from_errno (errno),
+                                    "Can't set RFKILL control device to non-blocking");
                close(fd);
-               return ret;
+               return FALSE;
        }
 
        events = NULL;
@@ -430,7 +432,7 @@ cc_rfkill_glib_open (CcRfkillGlib *rfkill)
        /* Setup write stream */
        rfkill->stream = g_unix_output_stream_new (fd, TRUE);
 
-       return fd;
+       return TRUE;
 }
 
 static void
diff --git a/plugins/rfkill/rfkill-glib.h b/plugins/rfkill/rfkill-glib.h
index a7d09b6..9468407 100644
--- a/plugins/rfkill/rfkill-glib.h
+++ b/plugins/rfkill/rfkill-glib.h
@@ -34,7 +34,8 @@ G_BEGIN_DECLS
 G_DECLARE_FINAL_TYPE (CcRfkillGlib, cc_rfkill_glib, CC_RFKILL, GLIB, GObject)
 
 CcRfkillGlib *cc_rfkill_glib_new               (void);
-int           cc_rfkill_glib_open              (CcRfkillGlib *rfkill);
+gboolean      cc_rfkill_glib_open              (CcRfkillGlib  *rfkill,
+                                                GError       **error);
 
 void          cc_rfkill_glib_send_change_all_event        (CcRfkillGlib        *rfkill,
                                                           guint                rfkill_type,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]