int main (int argc, char *argv[]) { long start_tv_sec, start_tv_usec; long end_tv_sec, end_tv_usec; int i; DBusMessage *method; DBusConnection *conn; DBusError error; /* Time each iteration and make sure it doesn't take more than 5 seconds to complete. Outside influences may cause connections to take longer but if it does and we are stuck in a poll call then we know the stuck in poll bug has come back to haunt us */ printf ("*** Testing stuck in poll\n"); dbus_error_init (&error); conn = dbus_bus_get (DBUS_BUS_SESSION, &error); /* run 100 times to make sure */ for (i = 0; i < 100; i++) { long delta; _dbus_get_current_time (&start_tv_sec, &start_tv_usec); _run_iteration (conn); _dbus_get_current_time (&end_tv_sec, &end_tv_usec); /* we just care about seconds */ delta = end_tv_sec - start_tv_sec; printf ("Iter %i: %lis\n", i, delta); if (delta >= 5) { printf ("Failed: looks like we might have been be stuck in poll ***\n"); exit (1); } } method = dbus_message_new_method_call ("org.freedesktop.TestSuiteEchoService", "/org/freedesktop/TestSuite", "org.freedesktop.TestSuite", "Exit"); dbus_connection_send (conn, method, NULL); dbus_message_unref (method); printf ("Success ***\n"); exit (0); }
void _dbus_generate_pseudorandom_bytes_buffer (char *buffer, int n_bytes) { long tv_usec; int i; /* fall back to pseudorandom */ _dbus_verbose ("Falling back to pseudorandom for %d bytes\n", n_bytes); _dbus_get_current_time (NULL, &tv_usec); srand (tv_usec); i = 0; while (i < n_bytes) { double r; unsigned int b; r = rand (); b = (r / (double) RAND_MAX) * 255.0; buffer[i] = b; ++i; } }
/** * Generates a new UUID. If you change how this is done, * there's some text about it in the spec that should also change. * * @param uuid the uuid to initialize */ void _dbus_generate_uuid (DBusGUID *uuid) { long now; _dbus_get_current_time (&now, NULL); uuid->as_uint32s[DBUS_UUID_LENGTH_WORDS - 1] = DBUS_UINT32_TO_BE (now); _dbus_generate_random_bytes_buffer (uuid->as_bytes, DBUS_UUID_LENGTH_BYTES - 4); }
static void bus_expirelist_expire (BusExpireList *list) { int next_interval; next_interval = -1; if (list->items != NULL) { long tv_sec, tv_usec; _dbus_get_current_time (&tv_sec, &tv_usec); next_interval = do_expiration_with_current_time (list, tv_sec, tv_usec); } bus_expire_timeout_set_interval (list->timeout, next_interval); }
static unsigned int get_random_seed (void) { DBusString bytes; unsigned int seed; int fd; const char *s; seed = 0; if (!_dbus_string_init (&bytes)) exit (1); fd = open ("/dev/urandom", O_RDONLY); if (fd < 0) goto use_fallback; if (_dbus_read (fd, &bytes, 4) != 4) goto use_fallback; close (fd); s = _dbus_string_get_const_data (&bytes); seed = * (unsigned int*) s; goto out; use_fallback: { long tv_usec; fprintf (stderr, "could not open/read /dev/urandom, using current time for seed\n"); _dbus_get_current_time (NULL, &tv_usec); seed = tv_usec; } out: _dbus_string_free (&bytes); return seed; }
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; }
dbus_bool_t bus_expire_list_test (const DBusString *test_data_dir) { DBusLoop *loop; BusExpireList *list; long tv_sec, tv_usec; long tv_sec_not_expired, tv_usec_not_expired; long tv_sec_expired, tv_usec_expired; long tv_sec_past, tv_usec_past; TestExpireItem *item; int next_interval; dbus_bool_t result = FALSE; loop = _dbus_loop_new (); _dbus_assert (loop != NULL); #define EXPIRE_AFTER 100 list = bus_expire_list_new (loop, EXPIRE_AFTER, test_expire_func, NULL); _dbus_assert (list != NULL); _dbus_get_current_time (&tv_sec, &tv_usec); tv_sec_not_expired = tv_sec; tv_usec_not_expired = tv_usec; time_add_milliseconds (&tv_sec_not_expired, &tv_usec_not_expired, EXPIRE_AFTER - 1); tv_sec_expired = tv_sec; tv_usec_expired = tv_usec; time_add_milliseconds (&tv_sec_expired, &tv_usec_expired, EXPIRE_AFTER); tv_sec_past = tv_sec - 1; tv_usec_past = tv_usec; item = dbus_new0 (TestExpireItem, 1); if (item == NULL) goto oom; item->item.added_tv_sec = tv_sec; item->item.added_tv_usec = tv_usec; if (!bus_expire_list_add (list, &item->item)) _dbus_assert_not_reached ("out of memory"); next_interval = do_expiration_with_current_time (list, tv_sec_not_expired, tv_usec_not_expired); _dbus_assert (item->expire_count == 0); _dbus_verbose ("next_interval = %d\n", next_interval); _dbus_assert (next_interval == 1); next_interval = do_expiration_with_current_time (list, tv_sec_expired, tv_usec_expired); _dbus_assert (item->expire_count == 1); _dbus_verbose ("next_interval = %d\n", next_interval); _dbus_assert (next_interval == -1); next_interval = do_expiration_with_current_time (list, tv_sec_past, tv_usec_past); _dbus_assert (item->expire_count == 1); _dbus_verbose ("next_interval = %d\n", next_interval); _dbus_assert (next_interval == 1000 + EXPIRE_AFTER); bus_expire_list_remove (list, &item->item); dbus_free (item); bus_expire_list_free (list); _dbus_loop_unref (loop); result = TRUE; oom: return result; }