int main (int argc, char *argv[]) { DBusServer *server; DBusError error; DBusLoop *loop; DBusConnection *session; TestServiceData *testdata; dbus_error_init (&error); loop = _dbus_loop_new (); testdata = dbus_new (TestServiceData, 1); testdata->loop = loop; session = dbus_bus_get (DBUS_BUS_SESSION, &error); if (!session) die ("couldn't access session bus"); test_connection_setup (loop, session); dbus_bus_request_name (session, "org.freedesktop.DBus.TestSuite.PrivServer", 0, &error); if (dbus_error_is_set (&error)) die ("couldn't request name: %s", error.message); if (!dbus_connection_add_filter (session, filter_session_message, testdata, NULL)) die ("couldn't add filter"); #ifdef DBUS_CMAKE server = dbus_server_listen (TEST_LISTEN, &error); #else server = dbus_server_listen ("unix:tmpdir=/tmp", &error); #endif if (!server) die ("%s", error.message); testdata->private_addr = dbus_server_get_address (server); fprintf (stderr, "test server listening on %s\n", testdata->private_addr); dbus_server_set_new_connection_function (server, new_connection_callback, testdata, NULL); test_server_setup (loop, server); fprintf (stderr, "server running mainloop\n"); _dbus_loop_run (loop); fprintf (stderr, "server mainloop quit\n"); test_server_shutdown (loop, server); test_connection_shutdown (loop, session); dbus_connection_unref (session); _dbus_loop_unref (loop); dbus_free (testdata); return 0; }
DBusRMutex * _dbus_platform_rmutex_new (void) { DBusRMutex *pmutex; pthread_mutexattr_t mutexattr; int result; pmutex = dbus_new (DBusRMutex, 1); if (pmutex == NULL) return NULL; pthread_mutexattr_init (&mutexattr); pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE); result = pthread_mutex_init (&pmutex->lock, &mutexattr); pthread_mutexattr_destroy (&mutexattr); if (result == ENOMEM || result == EAGAIN) { dbus_free (pmutex); return NULL; } else { PTHREAD_CHECK ("pthread_mutex_init", result); } return pmutex; }
static dbus_bool_t add_cancel_ownership_to_transaction (BusTransaction *transaction, BusService *service, BusOwner *owner) { OwnershipCancelData *d; d = dbus_new (OwnershipCancelData, 1); if (d == NULL) return FALSE; d->service = service; d->owner = owner; if (!bus_transaction_add_cancel_hook (transaction, cancel_ownership, d, free_ownership_cancel_data)) { dbus_free (d); return FALSE; } bus_service_ref (d->service); bus_owner_ref (owner); dbus_connection_ref (d->owner->conn); return TRUE; }
static DBusMutex* _dbus_pthread_mutex_new (void) { DBusMutexPThread *pmutex; int result; pmutex = dbus_new (DBusMutexPThread, 1); if (pmutex == NULL) return NULL; result = pthread_mutex_init (&pmutex->lock, NULL); if (result == ENOMEM || result == EAGAIN) { dbus_free (pmutex); return NULL; } else { PTHREAD_CHECK ("pthread_mutex_init", result); } /* Only written */ pmutex->count = 0; /* There's no portable way to have a "null" pthread afaik so we * can't set pmutex->holder to anything sensible. We only access it * once the lock is held (which means we've set it). */ return DBUS_MUTEX (pmutex); }
static DBusCondVar * _dbus_pthread_condvar_new (void) { DBusCondVarPThread *pcond; pthread_condattr_t attr; int result; pcond = dbus_new (DBusCondVarPThread, 1); if (pcond == NULL) return NULL; pthread_condattr_init (&attr); #ifdef HAVE_MONOTONIC_CLOCK if (have_monotonic_clock) pthread_condattr_setclock (&attr, CLOCK_MONOTONIC); #endif result = pthread_cond_init (&pcond->cond, &attr); pthread_condattr_destroy (&attr); if (result == EAGAIN || result == ENOMEM) { dbus_free (pcond); return NULL; } else { PTHREAD_CHECK ("pthread_cond_init", result); } return DBUS_COND_VAR (pcond); }
static DBusMessage* message_with_nesting_levels (int levels) { DBusMessage *message; dbus_int32_t v_INT32; DBusMessageIter *parents; DBusMessageIter *children; int i; /* If levels is higher it breaks sig_refcount in DBusMessageRealIter * in dbus-message.c, this assert is just to help you know you need * to fix that if you hit it */ _dbus_assert (levels < 256); parents = dbus_new(DBusMessageIter, levels + 1); children = dbus_new(DBusMessageIter, levels + 1); v_INT32 = 42; message = simple_method_call (); i = 0; dbus_message_iter_init_append (message, &parents[i]); while (i < levels) { dbus_message_iter_open_container (&parents[i], DBUS_TYPE_VARIANT, i == (levels - 1) ? DBUS_TYPE_INT32_AS_STRING : DBUS_TYPE_VARIANT_AS_STRING, &children[i]); ++i; parents[i] = children[i-1]; } --i; dbus_message_iter_append_basic (&children[i], DBUS_TYPE_INT32, &v_INT32); while (i >= 0) { dbus_message_iter_close_container (&parents[i], &children[i]); --i; } dbus_free(parents); dbus_free(children); return message; }
static dbus_bool_t add_restore_ownership_to_transaction (BusTransaction *transaction, BusService *service, BusOwner *owner) { OwnershipRestoreData *d; DBusList *link; d = dbus_new (OwnershipRestoreData, 1); if (d == NULL) return FALSE; d->service = service; d->owner = owner; d->service_link = _dbus_list_alloc_link (service); d->owner_link = _dbus_list_alloc_link (owner); d->hash_entry = _dbus_hash_table_preallocate_entry (service->registry->service_hash); bus_service_ref (d->service); bus_owner_ref (d->owner); dbus_connection_ref (d->owner->conn); d->before_owner = NULL; link = _dbus_list_get_first_link (&service->owners); while (link != NULL) { if (link->data == owner) { link = _dbus_list_get_next_link (&service->owners, link); if (link) d->before_owner = link->data; break; } link = _dbus_list_get_next_link (&service->owners, link); } if (d->service_link == NULL || d->owner_link == NULL || d->hash_entry == NULL || !bus_transaction_add_cancel_hook (transaction, restore_ownership, d, free_ownership_restore_data)) { free_ownership_restore_data (d); return FALSE; } return TRUE; }
static DBusCondVar * _dbus_windows_condvar_new (void) { DBusCondVar *cond; cond = dbus_new (DBusCondVar, 1); if (cond == NULL) return NULL; cond->list = NULL; InitializeCriticalSection (&cond->lock); return (DBusCondVar *) cond; }
static TimeoutCallback* timeout_callback_new (DBusTimeout *timeout) { TimeoutCallback *cb; cb = dbus_new (TimeoutCallback, 1); if (cb == NULL) return NULL; cb->timeout = timeout; _dbus_get_monotonic_time (&cb->last_tv_sec, &cb->last_tv_usec); return cb; }
DBusCondVar * _dbus_platform_condvar_new (void) { DBusCondVar *cond; cond = dbus_new (DBusCondVar, 1); if (cond == NULL) return NULL; cond->list = NULL; InitializeCriticalSection (&cond->lock); return cond; }
/** * Gets all groups corresponding to the given UID. Returns #FALSE * if no memory, or user isn't known, but always initializes * group_ids to a NULL array. * * @param uid the UID * @param group_ids return location for array of group IDs * @param n_group_ids return location for length of returned array * @returns #TRUE if the UID existed and we got some credentials */ dbus_bool_t _dbus_groups_from_uid (dbus_uid_t uid, dbus_gid_t **group_ids, int *n_group_ids) { DBusUserDatabase *db; const DBusUserInfo *info; *group_ids = NULL; *n_group_ids = 0; /* FIXME: this can't distinguish ENOMEM from other errors */ if (!_dbus_user_database_lock_system ()) return FALSE; db = _dbus_user_database_get_system (); if (db == NULL) { _dbus_user_database_unlock_system (); return FALSE; } if (!_dbus_user_database_get_uid (db, uid, &info, NULL)) { _dbus_user_database_unlock_system (); return FALSE; } _dbus_assert (info->uid == uid); if (info->n_group_ids > 0) { *group_ids = dbus_new (dbus_gid_t, info->n_group_ids); if (*group_ids == NULL) { _dbus_user_database_unlock_system (); return FALSE; } *n_group_ids = info->n_group_ids; memcpy (*group_ids, info->group_ids, info->n_group_ids * sizeof (dbus_gid_t)); } _dbus_user_database_unlock_system (); return TRUE; }
/** * Creates a new credentials object. * * @returns the new object or #NULL if no memory */ DBusCredentials* _dbus_credentials_new (void) { DBusCredentials *creds; creds = dbus_new (DBusCredentials, 1); if (creds == NULL) return NULL; creds->refcount = 1; creds->unix_uid = DBUS_UID_UNSET; creds->pid = DBUS_PID_UNSET; creds->windows_sid = NULL; creds->adt_audit_data = NULL; creds->adt_audit_data_size = 0; return creds; }
dbus_bool_t _dbus_register_shutdown_func_unlocked (DBusShutdownFunction func, void *data) { ShutdownClosure *c; c = dbus_new (ShutdownClosure, 1); if (c == NULL) return FALSE; c->func = func; c->data = data; c->next = registered_globals; registered_globals = c; return TRUE; }
/** * Creates a new DBusCounter. DBusCounter is used * to count usage of some resource such as memory. * * @returns new counter or #NULL on failure */ DBusCounter* _dbus_counter_new (void) { DBusCounter *counter; counter = dbus_new (DBusCounter, 1); if (counter == NULL) return NULL; counter->refcount = 1; counter->size_value = 0; counter->unix_fd_value = 0; counter->notify_size_guard_value = 0; counter->notify_unix_fd_guard_value = 0; counter->notify_function = NULL; counter->notify_data = NULL; counter->notify_pending = FALSE; return counter; }
static WatchCallback* watch_callback_new (DBusWatch *watch, DBusWatchFunction function, void *data, DBusFreeFunction free_data_func) { WatchCallback *cb; cb = dbus_new (WatchCallback, 1); if (cb == NULL) return NULL; cb->watch = watch; cb->function = function; cb->last_iteration_oom = FALSE; cb->callback.refcount = 1; cb->callback.type = CALLBACK_WATCH; cb->callback.data = data; cb->callback.free_data_func = free_data_func; return cb; }
static TimeoutCallback* timeout_callback_new (DBusTimeout *timeout, DBusTimeoutFunction function, void *data, DBusFreeFunction free_data_func) { TimeoutCallback *cb; cb = dbus_new (TimeoutCallback, 1); if (cb == NULL) return NULL; cb->timeout = timeout; cb->function = function; _dbus_get_current_time (&cb->last_tv_sec, &cb->last_tv_usec); cb->callback.refcount = 1; cb->callback.type = CALLBACK_TIMEOUT; cb->callback.data = data; cb->callback.free_data_func = free_data_func; return cb; }
DBusCMutex * _dbus_platform_cmutex_new (void) { DBusCMutex *pmutex; int result; pmutex = dbus_new (DBusCMutex, 1); if (pmutex == NULL) return NULL; result = pthread_mutex_init (&pmutex->lock, NULL); if (result == ENOMEM || result == EAGAIN) { dbus_free (pmutex); return NULL; } else { PTHREAD_CHECK ("pthread_mutex_init", result); } return pmutex; }