static void new_connection_cb (DBusServer * server, DBusConnection* connection, gpointer user_data) { dbus_int32_t slot = -1; GObject * object; if (!dbus_connection_allocate_data_slot (&slot)) { g_warning ("error allocating data slot for DBusConnection"); dbus_connection_close (connection); return; } dbus_connection_ref (connection); dbus_connection_set_allow_anonymous (connection, TRUE); dbus_connection_setup_with_g_main (connection, NULL); object = g_object_new (p2p_object_get_type (), NULL); dbus_g_connection_register_g_object (dbus_connection_get_g_connection (connection), "/", object); dbus_connection_set_data (connection, slot, object, g_object_unref); }
void ibus_connection_set_connection (IBusConnection *connection, DBusConnection *dbus_connection, gboolean shared) { gboolean result; IBusConnectionPrivate *priv; g_assert (IBUS_IS_CONNECTION (connection)); g_assert (dbus_connection != NULL); g_assert (dbus_connection_get_is_connected (dbus_connection)); priv = IBUS_CONNECTION_GET_PRIVATE (connection); g_assert (priv->connection == NULL); priv->connection = dbus_connection_ref (dbus_connection); priv->shared = shared; dbus_connection_set_data (priv->connection, _get_slot(), connection, NULL); dbus_connection_set_unix_user_function (priv->connection, (DBusAllowUnixUserFunction) _connection_allow_unix_user_cb, connection, NULL); result = dbus_connection_add_filter (priv->connection, (DBusHandleMessageFunction) _connection_handle_message_cb, connection, NULL); ibus_dbus_connection_setup (priv->connection); g_warn_if_fail (result); }
int main() { DBusConnection* connection; DBusError error; int cnt; int data_slot = -1; threadData1 thrData; pthread_t thread[MAX_THREAD]; int thrVal[MAX_THREAD]={0}; void* thrValPtr[MAX_THREAD]; for(cnt=0; cnt<MAX_THREAD; cnt++) thrValPtr[cnt] = (void*)&thrVal[cnt]; dbus_error_init(&error); connection = dbus_bus_get(DBUS_BUS_SESSION, &error); if(!connection || dbus_error_is_set(&error)) return handle_error(&error); pthread_mutex_init(&thrData.mutex, NULL); pthread_cond_init(&thrData.cond, NULL); thrData.ret = 0; dbus_connection_allocate_data_slot(&data_slot); dbus_connection_set_data(connection, data_slot, &thrData, NULL); dbus_threads_init_default(); for(cnt=0; cnt<MAX_THREAD; cnt++) pthread_create(&thread[cnt], NULL, &send_msg1, &data_slot); sleep(1); pthread_cond_broadcast(&thrData.cond); for(cnt=0; cnt<MAX_THREAD; cnt++) pthread_join(thread[cnt], &thrValPtr[cnt]); if(thrData.ret != MAX_THREAD) { std_log(LOG_FILENAME_LINE, "No. of threads crashed %d", (MAX_THREAD - thrData.ret)); create_xml(1); return 1; } dbus_connection_unref(connection); std_log(LOG_FILENAME_LINE, "Test Successful"); create_xml(0); return 0; }
/** * Sets the watch and timeout functions of a #DBusConnection * to integrate the connection with the GUI HID's main loop. * * @param connection the connection */ void pcb_dbus_connection_setup_with_mainloop (DBusConnection * connection) { //ConnectionSetup *cs; hidval temp; /* FIXME we never free the slot, so its refcount just keeps growing, * which is kind of broken. */ //dbus_connection_allocate_data_slot (&connection_slot); //if (connection_slot < 0) // goto nomem; #if 0 cs = connection_setup_new (connection); if (!dbus_connection_set_data (connection, connection_slot, cs, (DBusFreeFunction) connection_setup_free)) { goto nomem; } #endif if (!dbus_connection_set_watch_functions (connection, watch_add, watch_remove, watch_toggled, NULL, NULL)) // cs, NULL)) { goto nomem; } if (!dbus_connection_set_timeout_functions (connection, timeout_add, timeout_remove, timeout_toggled, NULL, NULL)) // cs, NULL)) { goto nomem; } dbus_connection_set_dispatch_status_function (connection, dispatch_status_changed, NULL, NULL); // cs, NULL); /* Register a new mainloop hook to mop up any unfinished IO. */ temp.ptr = (void *)connection; gui->add_block_hook (block_hook_cb, temp); return; nomem: fprintf (stderr, "Not enough memory to set up DBusConnection for use with PCB\n"); }
static void new_connection_callback (DBusServer *server, DBusConnection *new_connection, void *user_data) { const char *test_messages[] = { "org.librdf.redland.Test" }; const char *disconnect_messages[] = { "org.freedesktop.Local.Disconnect" }; DBusMessageHandler *test_message_handler; TestData *data; g_print ("new_connection_callback\n"); dbus_connection_ref (new_connection); dbus_connection_setup_with_g_main (new_connection, NULL); data = test_message_data_new(); test_message_handler = dbus_message_handler_new (handle_test_message, data, (DBusFreeFunction)test_message_data_free); if (!dbus_connection_register_handler (new_connection, test_message_handler, test_messages, 1)) goto nomem; if (!dbus_connection_set_data (new_connection, handler_slot, test_message_handler, (DBusFreeFunction)dbus_message_handler_unref)) goto nomem; if (!dbus_connection_register_handler (new_connection, disconnect_handler, disconnect_messages, 1)) goto nomem; return; nomem: g_error ("Out of memory in setting up new connection"); }
void dbus_a_cleanup_connection(DBusConnection* connection) { struct ConnectionData* c; kprintf("CleanupAmigaConnection\n"); kprintf("Slot X: %ld\n", Slot); c = dbus_connection_get_data(connection, Slot); if (c != NULL) { dbus_connection_set_data(connection, Slot, NULL, NULL); dbus_connection_free_data_slot(&Slot); kprintf("Slot Y: %ld\n", Slot); } dbus_connection_set_dispatch_status_function(connection, NULL, NULL, NULL); dbus_connection_set_wakeup_main_function(connection, NULL, NULL, NULL); dbus_connection_set_watch_functions(connection, NULL, NULL, NULL, NULL, NULL); dbus_connection_set_timeout_functions(connection, NULL, NULL, NULL, NULL, NULL); kprintf("Slot X: %ld\n", Slot); }
dbus_bool_t dbus_a_setup_connection(DBusConnection* connection) { struct ConnectionData* c; c = CreateConnectionData(connection); if (c != NULL) { dbus_connection_set_watch_functions(connection, AddWatchFunction, RemoveWatchFunction, WatchToggledFunction, c, NULL); dbus_connection_set_timeout_functions(connection, AddTimeoutFunction, RemoveTimeoutFunction, TimeoutToggledFunction, c, NULL); dbus_connection_set_dispatch_status_function(connection, DispatchStatusFunction, c, NULL); dbus_connection_set_wakeup_main_function(connection, WakeupMainFunction, c, NULL); kprintf("Slot A: %ld\n", Slot); if (dbus_connection_allocate_data_slot(&Slot)) { kprintf("Slot B: %ld\n", Slot); if (dbus_connection_set_data(connection, Slot, c, DeleteConnectionData)) { kprintf("Slot C: %ld\n", Slot); return TRUE; } } } DeleteConnectionData(c); return FALSE; }
static BusData* ensure_bus_data (DBusConnection *connection) { BusData *bd; if (!dbus_connection_allocate_data_slot (&bus_data_slot)) return NULL; bd = dbus_connection_get_data (connection, bus_data_slot); if (bd == NULL) { bd = dbus_new0 (BusData, 1); if (bd == NULL) { dbus_connection_free_data_slot (&bus_data_slot); return NULL; } bd->connection = connection; if (!dbus_connection_set_data (connection, bus_data_slot, bd, bus_data_free)) { dbus_free (bd); dbus_connection_free_data_slot (&bus_data_slot); return NULL; } /* Data slot refcount now held by the BusData */ } else { dbus_connection_free_data_slot (&bus_data_slot); } return bd; }
/* This test outputs TAP syntax: http://testanything.org/ */ int main (int argc, char *argv[]) { dbus_int32_t slot_connection = -1; dbus_int32_t slot_message = -1; dbus_int32_t slot_pending = -1; DBusError error; DBusConnection *conn; DBusMessage *method; DBusPendingCall *pending; DBusMessage *reply; printf ("# Testing pending call error\n"); dbus_connection_allocate_data_slot (&slot_connection); dbus_message_allocate_data_slot (&slot_message); dbus_pending_call_allocate_data_slot (&slot_pending); dbus_error_init (&error); conn = dbus_bus_get_private (DBUS_BUS_SESSION, &error); dbus_connection_set_data (conn, slot_connection, (void*)"connection", free_data); ++count; dbus_connection_set_exit_on_disconnect (conn, FALSE); method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService", "/org/freedesktop/TestSuite", "org.freedesktop.TestSuite", "Exit"); dbus_message_set_data (method, slot_message, (void*)"method", free_data); ++count; dbus_connection_send_with_reply (conn, method, &pending, -1); dbus_message_unref (method); dbus_pending_call_set_data (pending, slot_pending, (void*)"pending", free_data); ++count; dbus_connection_close (conn); dbus_pending_call_block (pending); reply = dbus_pending_call_steal_reply (pending); dbus_pending_call_unref (pending); if (reply == NULL) { printf ("Bail out! Reply is NULL ***\n"); exit (1); } dbus_message_set_data (reply, slot_message, (void*)"reply", free_data); ++count; if (dbus_message_get_type (reply) != DBUS_MESSAGE_TYPE_ERROR) { printf ("Bail out! Reply is not error ***\n"); exit (1); } dbus_message_unref (reply); dbus_connection_unref (conn); dbus_connection_free_data_slot (&slot_connection); dbus_message_free_data_slot (&slot_message); dbus_pending_call_free_data_slot (&slot_pending); if (count != 0) { printf ("not ok # Not all refs were unrefed ***\n"); exit (1); } else { printf ("ok\n# Testing completed\n1..1\n"); exit (0); } }
static void test_connection (Fixture *f, gconstpointer data) { Thread public_api = { f, f->connection, (RefFunc) dbus_connection_ref, NULL, (VoidFunc) dbus_connection_unref, NULL, NULL, NULL }; Thread internal_api = { f, f->connection, (RefFunc) _dbus_connection_ref_unlocked, NULL, (VoidFunc) _dbus_connection_unref_unlocked, f->connection, (VoidFunc) _dbus_connection_lock, (VoidFunc) _dbus_connection_unlock }; unsigned i; /* Use a slot as a pseudo-weakref */ if (!dbus_connection_set_data (f->connection, connection_slot, f, last_unref)) g_error ("OOM"); for (i = 0; i < f->n_threads; i++) { if ((i % 2) == 0) f->threads[i] = g_thread_new (NULL, ref_thread, &public_api); else f->threads[i] = g_thread_new (NULL, ref_thread, &internal_api); g_assert (f->threads[i] != NULL); } wait_for_all_threads (f); for (i = 0; i < f->n_threads; i++) { if ((i % 2) == 0) f->threads[i] = g_thread_new (NULL, cycle_thread, &public_api); else f->threads[i] = g_thread_new (NULL, cycle_thread, &internal_api); g_assert (f->threads[i] != NULL); } wait_for_all_threads (f); for (i = 0; i < f->n_threads; i++) { if ((i % 2) == 0) f->threads[i] = g_thread_new (NULL, unref_thread, &public_api); else f->threads[i] = g_thread_new (NULL, unref_thread, &internal_api); g_assert (f->threads[i] != NULL); } wait_for_all_threads (f); /* Destroy the connection. This should be the last-unref. */ g_assert (!f->last_unref); dbus_connection_close (f->connection); dbus_connection_unref (f->connection); f->connection = NULL; g_assert (f->last_unref); }
/** * dbus_connection_setup_with_g_main: * @connection: the connection * @context: the #GMainContext or #NULL for default context * * Sets the watch and timeout functions of a #DBusConnection * to integrate the connection with the GLib main loop. * Pass in #NULL for the #GMainContext unless you're * doing something specialized. * * If called twice for the same context, does nothing the second * time. If called once with context A and once with context B, * context B replaces context A as the context monitoring the * connection. */ void dbus_connection_setup (DBusConnection *connection, GMainContext *context) { ConnectionSetup *old_setup; ConnectionSetup *cs; do { /* FIXME we never free the slot, so its refcount just keeps growing, * which is kind of broken. */ dbus_connection_allocate_data_slot (&_dbus_gmain_connection_slot); if (_dbus_gmain_connection_slot < 0) break; if (context == NULL) context = g_main_context_default (); cs = NULL; old_setup = dbus_connection_get_data (connection, _dbus_gmain_connection_slot); if (old_setup != NULL) { if (old_setup->context == context) return; /* nothing to do */ cs = connection_setup_new_from_old (context, old_setup); /* Nuke the old setup */ dbus_connection_set_data (connection, _dbus_gmain_connection_slot, NULL, NULL); old_setup = NULL; } if (cs == NULL) cs = connection_setup_new (context, connection); if (!dbus_connection_set_data (connection, _dbus_gmain_connection_slot, cs, (DBusFreeFunction)connection_setup_free)) break; if (!dbus_connection_set_watch_functions (connection, add_watch, remove_watch, watch_toggled, cs, NULL)) break; if (!dbus_connection_set_timeout_functions (connection, add_timeout, remove_timeout, timeout_toggled, cs, NULL)) break; dbus_connection_set_wakeup_main_function (connection, wakeup_main, cs, NULL); return; } while (0); g_error ("Not enough memory to set up DBusConnection for use with GLib"); }
/* Return a new reference to a Python Connection or subclass (given by cls) * corresponding to the DBusConnection conn, which must have been newly * created. For use by the Connection and Bus constructors. * * Raises AssertionError if the DBusConnection already has a Connection. */ static PyObject * DBusPyConnection_NewConsumingDBusConnection(PyTypeObject *cls, DBusConnection *conn, PyObject *mainloop) { Connection *self = NULL; PyObject *ref; dbus_bool_t ok; DBG("%s(cls=%p, conn=%p, mainloop=%p)", __func__, cls, conn, mainloop); DBUS_PY_RAISE_VIA_NULL_IF_FAIL(conn); Py_BEGIN_ALLOW_THREADS ref = (PyObject *)dbus_connection_get_data(conn, _connection_python_slot); Py_END_ALLOW_THREADS if (ref) { self = (Connection *)PyWeakref_GetObject(ref); ref = NULL; if (self && (PyObject *)self != Py_None) { self = NULL; PyErr_SetString(PyExc_AssertionError, "Newly created D-Bus connection already has a " "Connection instance associated with it"); DBG("%s() fail - assertion failed, DBusPyConn has a DBusConn already", __func__); DBG_WHEREAMI; return NULL; } } ref = NULL; /* Change mainloop from a borrowed reference to an owned reference */ if (!mainloop || mainloop == Py_None) { mainloop = dbus_py_get_default_main_loop(); if (!mainloop) goto err; } else { Py_INCREF(mainloop); } DBG("Constructing Connection from DBusConnection at %p", conn); self = (Connection *)(cls->tp_alloc(cls, 0)); if (!self) goto err; TRACE(self); DBG_WHEREAMI; self->has_mainloop = (mainloop != Py_None); self->conn = NULL; self->filters = PyList_New(0); if (!self->filters) goto err; self->object_paths = PyDict_New(); if (!self->object_paths) goto err; ref = PyWeakref_NewRef((PyObject *)self, NULL); if (!ref) goto err; DBG("Created weak ref %p to (Connection *)%p for (DBusConnection *)%p", ref, self, conn); Py_BEGIN_ALLOW_THREADS ok = dbus_connection_set_data(conn, _connection_python_slot, (void *)ref, (DBusFreeFunction)dbus_py_take_gil_and_xdecref); Py_END_ALLOW_THREADS if (ok) { DBG("Attached weak ref %p ((Connection *)%p) to (DBusConnection *)%p", ref, self, conn); ref = NULL; /* don't DECREF it - the DBusConnection owns it now */ } else { DBG("Failed to attached weak ref %p ((Connection *)%p) to " "(DBusConnection *)%p - will dispose of it", ref, self, conn); PyErr_NoMemory(); goto err; } DBUS_PY_RAISE_VIA_GOTO_IF_FAIL(conn, err); self->conn = conn; /* the DBusPyConnection will close it now */ conn = NULL; if (self->has_mainloop && !dbus_py_set_up_connection((PyObject *)self, mainloop)) { goto err; } Py_CLEAR(mainloop); DBG("%s() -> %p", __func__, self); TRACE(self); return (PyObject *)self; err: DBG("Failed to construct Connection from DBusConnection at %p", conn); Py_CLEAR(mainloop); Py_CLEAR(self); Py_CLEAR(ref); if (conn) { Py_BEGIN_ALLOW_THREADS dbus_connection_close(conn); dbus_connection_unref(conn); Py_END_ALLOW_THREADS } DBG("%s() fail", __func__); DBG_WHEREAMI; return NULL; }