From: John Ernberg <[email protected]>

Prevents glib from causing SIGFPE during certain circumstances of modem
removal.
---
 plugins/dun_gw_bluez4.c | 24 ++++++++++++++++++++++--
 plugins/hfp_ag_bluez4.c | 25 ++++++++++++++++++++++---
 2 files changed, 44 insertions(+), 5 deletions(-)

diff --git a/plugins/dun_gw_bluez4.c b/plugins/dun_gw_bluez4.c
index a1de7a4..570da7f 100644
--- a/plugins/dun_gw_bluez4.c
+++ b/plugins/dun_gw_bluez4.c
@@ -40,6 +40,7 @@
 static struct server *server;
 static guint modemwatch_id;
 static GList *modems;
+static GHashTable *gprs_watches = NULL;
 
 static const gchar *dun_record =
 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
@@ -129,15 +130,29 @@ static void gprs_watch(struct ofono_atom *atom,
        }
 }
 
+static gboolean atom_watch_remove(gpointer key, gpointer value,
+                                       gpointer user_data)
+{
+       struct ofono_modem *modem = key;
+
+       __ofono_modem_remove_atom_watch(modem, GPOINTER_TO_UINT(value));
+
+       return TRUE;
+}
+
 static void modem_watch(struct ofono_modem *modem, gboolean added, void *user)
 {
+       int gprs;
        DBG("modem: %p, added: %d", modem, added);
 
-       if (added == FALSE)
+       if (added == FALSE) {
+               g_hash_table_remove(gprs_watches, modem);
                return;
+       }
 
-       __ofono_modem_add_atom_watch(modem, OFONO_ATOM_TYPE_GPRS,
+       gprs = __ofono_modem_add_atom_watch(modem, OFONO_ATOM_TYPE_GPRS,
                                                gprs_watch, modem, NULL);
+       g_hash_table_insert(gprs_watches, modem, GUINT_TO_POINTER(gprs));
 }
 
 static void call_modemwatch(struct ofono_modem *modem, void *user)
@@ -149,6 +164,8 @@ static int dun_gw_init(void)
 {
        DBG("");
 
+       gprs_watches = g_hash_table_new(g_direct_hash, g_direct_equal);
+
        modemwatch_id = __ofono_modemwatch_add(modem_watch, NULL, NULL);
 
        __ofono_modem_foreach(call_modemwatch, NULL);
@@ -161,6 +178,9 @@ static void dun_gw_exit(void)
        __ofono_modemwatch_remove(modemwatch_id);
        g_list_free(modems);
 
+       g_hash_table_foreach_remove(gprs_watches, atom_watch_remove, NULL);
+       g_hash_table_destroy(gprs_watches);
+
        if (server) {
                bluetooth_unregister_server(server);
                server = NULL;
diff --git a/plugins/hfp_ag_bluez4.c b/plugins/hfp_ag_bluez4.c
index 039b665..b931ae1 100644
--- a/plugins/hfp_ag_bluez4.c
+++ b/plugins/hfp_ag_bluez4.c
@@ -41,6 +41,7 @@ static struct server *server;
 static guint modemwatch_id;
 static GList *modems;
 static GHashTable *sim_hash = NULL;
+static GHashTable *sim_watches = NULL;
 
 static const gchar *hfp_ag_record =
 "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
@@ -142,6 +143,16 @@ static void sim_state_watch(enum ofono_sim_state 
new_state, void *data)
                                                hfp_ag_connect_cb, NULL);
 }
 
+static gboolean atom_watch_remove(gpointer key, gpointer value,
+                                       gpointer user_data)
+{
+       struct ofono_modem *modem = key;
+
+       __ofono_modem_remove_atom_watch(modem, GPOINTER_TO_UINT(value));
+
+       return TRUE;
+}
+
 static gboolean sim_watch_remove(gpointer key, gpointer value,
                                gpointer user_data)
 {
@@ -176,13 +187,17 @@ static void sim_watch(struct ofono_atom *atom,
 
 static void modem_watch(struct ofono_modem *modem, gboolean added, void *user)
 {
+       int sim;
        DBG("modem: %p, added: %d", modem, added);
 
-       if (added == FALSE)
+       if (added == FALSE) {
+               g_hash_table_remove(sim_watches, modem);
                return;
+       }
 
-       __ofono_modem_add_atom_watch(modem, OFONO_ATOM_TYPE_SIM,
-                                       sim_watch, modem, NULL);
+       sim = __ofono_modem_add_atom_watch(modem, OFONO_ATOM_TYPE_SIM,
+                                               sim_watch, modem, NULL);
+       g_hash_table_insert(sim_watches, modem, GUINT_TO_POINTER(sim));
 }
 
 static void call_modemwatch(struct ofono_modem *modem, void *user)
@@ -193,6 +208,7 @@ static void call_modemwatch(struct ofono_modem *modem, void 
*user)
 static int hfp_ag_init(void)
 {
        sim_hash = g_hash_table_new(g_direct_hash, g_direct_equal);
+       sim_watches = g_hash_table_new(g_direct_hash, g_direct_equal);
 
        modemwatch_id = __ofono_modemwatch_add(modem_watch, NULL, NULL);
        __ofono_modem_foreach(call_modemwatch, NULL);
@@ -207,6 +223,9 @@ static void hfp_ag_exit(void)
        g_hash_table_foreach_remove(sim_hash, sim_watch_remove, NULL);
        g_hash_table_destroy(sim_hash);
 
+       g_hash_table_foreach_remove(sim_watches, atom_watch_remove, NULL);
+       g_hash_table_destroy(sim_watches);
+
        if (server) {
                bluetooth_unregister_server(server);
                server = NULL;
-- 
1.9.1
_______________________________________________
ofono mailing list
[email protected]
https://lists.ofono.org/mailman/listinfo/ofono

Reply via email to