dbus: Don't crash if can't make a connection

This commit is contained in:
Dorota Czaplejewicz
2020-03-03 19:25:49 +00:00
parent b3cfc8a0f3
commit 8bae8fe5bb

View File

@ -222,9 +222,9 @@ main (int argc, char **argv)
error = NULL; error = NULL;
connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error); connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
if (connection == NULL) { if (connection == NULL) {
g_printerr ("Can't connect to the bus: %s\n", error->message); g_printerr ("Can't connect to the bus: %s. "
"Visibility switching unavailable.", error->message);
g_error_free (error); g_error_free (error);
exit (1);
} }
break; break;
case G_BUS_TYPE_NONE: case G_BUS_TYPE_NONE:
@ -246,25 +246,28 @@ main (int argc, char **argv)
g_assert_not_reached (); g_assert_not_reached ();
break; break;
} }
guint owner_id = 0;
DBusHandler *service = NULL;
if (connection) {
service = dbus_handler_new(connection, DBUS_SERVICE_PATH);
DBusHandler *service = dbus_handler_new(connection, DBUS_SERVICE_PATH); if (service == NULL) {
g_printerr ("Can't create dbus server\n");
exit (1);
}
instance.dbus_handler = service;
if (service == NULL) { owner_id = g_bus_own_name_on_connection (connection,
g_printerr ("Can't create dbus server\n"); DBUS_SERVICE_INTERFACE,
exit (1); G_BUS_NAME_OWNER_FLAGS_NONE,
} on_name_acquired,
instance.dbus_handler = service; on_name_lost,
NULL,
guint owner_id = g_bus_own_name_on_connection (connection, NULL);
DBUS_SERVICE_INTERFACE, if (owner_id == 0) {
G_BUS_NAME_OWNER_FLAGS_NONE, g_printerr ("Can't own the name\n");
on_name_acquired, exit (1);
on_name_lost, }
NULL,
NULL);
if (owner_id == 0) {
g_printerr ("Can't own the name\n");
exit (1);
} }
instance.submission = get_submission(instance.wayland.input_method_manager, instance.submission = get_submission(instance.wayland.input_method_manager,
@ -296,9 +299,15 @@ main (int argc, char **argv)
g_main_loop_run (loop); g_main_loop_run (loop);
g_bus_unown_name (owner_id); if (connection) {
g_object_unref (service); if (service) {
g_object_unref (connection); if (owner_id != 0) {
g_bus_unown_name (owner_id);
}
g_object_unref (service);
}
g_object_unref (connection);
}
g_main_loop_unref (loop); g_main_loop_unref (loop);
squeek_wayland_deinit (&instance.wayland); squeek_wayland_deinit (&instance.wayland);