2022-06-06 15:18:36 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright 2006 Red Hat, Inc.
|
|
|
|
|
* Copyright 2013-2018 Collabora Ltd.
|
|
|
|
|
* Copyright 2017 Endless OS Foundation, LLC
|
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
|
*/
|
2006-08-16 22:30:15 +00:00
|
|
|
/**
|
2006-10-01 15:36:19 +00:00
|
|
|
* Test to make sure late thread initialization works
|
|
|
|
|
*/
|
2006-08-16 22:30:15 +00:00
|
|
|
|
2010-03-19 12:36:49 +01:00
|
|
|
#include <config.h>
|
2006-08-16 22:30:15 +00:00
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
#include <dbus/dbus-sysdeps.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
#include <dbus/dbus-internals.h>
|
2006-10-01 15:36:19 +00:00
|
|
|
#include <dbus/dbus-connection-internal.h>
|
2018-07-12 13:06:32 +01:00
|
|
|
#include <dbus/dbus-valgrind-internal.h>
|
2006-08-16 22:30:15 +00:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
_run_iteration (DBusConnection *conn)
|
|
|
|
|
{
|
|
|
|
|
DBusPendingCall *echo_pending;
|
|
|
|
|
DBusPendingCall *dbus_pending;
|
|
|
|
|
DBusMessage *method;
|
|
|
|
|
DBusMessage *reply;
|
Be more const-correct
As a general design principle, strings that we aren't going to modify
should usually be const. When compiling with -Wwrite-strings, quoted
string constants are of type "const char *", causing compiler warnings
when they are assigned to char * variables.
Unfortunately, we need to add casts in a few places:
* _dbus_list_append(), _dbus_test_oom_handling() and similar generic
"user-data" APIs take a void *, not a const void *, so we have
to cast
* For historical reasons the execve() family of functions take a
(char * const *), i.e. a constant pointer to an array of mutable
strings, so again we have to cast
* _dbus_spawn_async_with_babysitter similarly takes a char **,
although we can make it a little more const-correct by making it
take (char * const *) like execve() does
This also incorporates a subsequent patch by Thomas Zimmermann to
put various string constants in static storage, which is a little
more efficient.
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thomas Zimmermann <tdz@users.sourceforge.net>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357
2016-10-07 17:41:01 +01:00
|
|
|
const char *echo = "echo";
|
2006-08-16 22:30:15 +00:00
|
|
|
|
|
|
|
|
/* send the first message */
|
|
|
|
|
method = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
|
|
|
|
|
"/org/freedesktop/TestSuite",
|
|
|
|
|
"org.freedesktop.TestSuite",
|
|
|
|
|
"Echo");
|
|
|
|
|
|
2017-02-06 13:07:21 +00:00
|
|
|
if (!dbus_message_append_args (method, DBUS_TYPE_STRING, &echo, NULL))
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Bail out! Failed to append arguments: OOM\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-16 22:30:15 +00:00
|
|
|
dbus_connection_send_with_reply (conn, method, &echo_pending, -1);
|
|
|
|
|
dbus_message_unref (method);
|
|
|
|
|
|
|
|
|
|
/* send the second message */
|
|
|
|
|
method = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
|
|
|
|
|
DBUS_PATH_DBUS,
|
|
|
|
|
"org.freedesktop.Introspectable",
|
|
|
|
|
"Introspect");
|
|
|
|
|
|
|
|
|
|
dbus_connection_send_with_reply (conn, method, &dbus_pending, -1);
|
|
|
|
|
dbus_message_unref (method);
|
|
|
|
|
|
|
|
|
|
/* block on the second message (should return immediately) */
|
|
|
|
|
dbus_pending_call_block (dbus_pending);
|
|
|
|
|
|
|
|
|
|
/* block on the first message */
|
|
|
|
|
/* if it does not return immediately chances
|
|
|
|
|
are we hit the block in poll bug */
|
|
|
|
|
dbus_pending_call_block (echo_pending);
|
|
|
|
|
|
|
|
|
|
/* check the reply only to make sure we
|
|
|
|
|
are not getting errors unrelated
|
|
|
|
|
to the block in poll bug */
|
|
|
|
|
reply = dbus_pending_call_steal_reply (echo_pending);
|
|
|
|
|
|
|
|
|
|
if (reply == NULL)
|
|
|
|
|
{
|
2017-02-06 13:07:21 +00:00
|
|
|
printf ("Bail out! Reply is NULL ***\n");
|
2006-08-16 22:30:15 +00:00
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (dbus_message_get_type (reply) == DBUS_MESSAGE_TYPE_ERROR)
|
|
|
|
|
{
|
2017-02-06 13:07:21 +00:00
|
|
|
printf ("Bail out! Reply is error: %s ***\n", dbus_message_get_error_name (reply));
|
2006-08-16 22:30:15 +00:00
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (reply);
|
|
|
|
|
dbus_pending_call_unref (dbus_pending);
|
|
|
|
|
dbus_pending_call_unref (echo_pending);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
static void
|
|
|
|
|
check_mutex_lock (DBusMutex *mutex1,
|
|
|
|
|
DBusMutex *mutex2,
|
|
|
|
|
dbus_bool_t is_same)
|
|
|
|
|
{
|
|
|
|
|
_dbus_assert (mutex1 != NULL);
|
|
|
|
|
_dbus_assert (mutex2 != NULL);
|
|
|
|
|
|
|
|
|
|
if (is_same)
|
|
|
|
|
{
|
|
|
|
|
_dbus_assert (mutex1 == mutex2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_dbus_assert (mutex1 != mutex2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
check_condvar_lock (DBusCondVar *condvar1,
|
|
|
|
|
DBusCondVar *condvar2,
|
|
|
|
|
dbus_bool_t is_same)
|
|
|
|
|
{
|
|
|
|
|
_dbus_assert (condvar1 != NULL);
|
|
|
|
|
_dbus_assert (condvar2 != NULL);
|
|
|
|
|
|
|
|
|
|
if (is_same)
|
|
|
|
|
{
|
|
|
|
|
_dbus_assert (condvar1 == condvar2);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_dbus_assert (condvar1 != condvar2);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-08 20:28:21 +00:00
|
|
|
/* This test outputs TAP syntax: http://testanything.org/ */
|
2006-08-16 22:30:15 +00:00
|
|
|
int
|
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
DBusMessage *method;
|
|
|
|
|
DBusConnection *conn;
|
|
|
|
|
DBusError error;
|
|
|
|
|
DBusMutex *mutex1, *dispatch_mutex1, *io_path_mutex1;
|
|
|
|
|
DBusCondVar *dispatch_cond1, *io_path_cond1;
|
|
|
|
|
DBusMutex *mutex2, *dispatch_mutex2, *io_path_mutex2;
|
|
|
|
|
DBusCondVar *dispatch_cond2, *io_path_cond2;
|
2016-02-08 20:28:21 +00:00
|
|
|
int test_num = 0;
|
2006-08-16 22:30:15 +00:00
|
|
|
|
2018-07-12 13:06:32 +01:00
|
|
|
if (RUNNING_ON_VALGRIND)
|
|
|
|
|
{
|
|
|
|
|
printf ("1..0 # SKIP Not ready to run under valgrind yet\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2017-02-06 13:07:21 +00:00
|
|
|
printf ("# Testing late thread init\n");
|
2006-08-16 22:30:15 +00:00
|
|
|
|
|
|
|
|
dbus_error_init (&error);
|
|
|
|
|
|
|
|
|
|
conn = dbus_bus_get (DBUS_BUS_SESSION, &error);
|
|
|
|
|
|
2017-02-06 13:07:21 +00:00
|
|
|
if (conn == NULL)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Bail out! Failed to open connection to session bus: %s\n",
|
|
|
|
|
error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_dbus_connection_test_get_locks (conn, &mutex1,
|
2006-08-16 22:30:15 +00:00
|
|
|
&dispatch_mutex1,
|
|
|
|
|
&io_path_mutex1,
|
|
|
|
|
&dispatch_cond1,
|
|
|
|
|
&io_path_cond1);
|
|
|
|
|
_run_iteration (conn);
|
|
|
|
|
_dbus_connection_test_get_locks (conn, &mutex2,
|
|
|
|
|
&dispatch_mutex2,
|
|
|
|
|
&io_path_mutex2,
|
|
|
|
|
&dispatch_cond2,
|
|
|
|
|
&io_path_cond2);
|
|
|
|
|
|
|
|
|
|
check_mutex_lock (mutex1, mutex2, TRUE);
|
|
|
|
|
check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
|
|
|
|
|
check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
|
|
|
|
|
check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
|
|
|
|
|
check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
|
2016-02-08 20:28:21 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
2006-08-16 22:30:15 +00:00
|
|
|
|
2017-02-06 13:07:21 +00:00
|
|
|
if (!dbus_threads_init_default ())
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Bail out! Failed to initialise threads: OOM\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2006-08-16 22:30:15 +00:00
|
|
|
|
|
|
|
|
_dbus_connection_test_get_locks (conn, &mutex1,
|
|
|
|
|
&dispatch_mutex1,
|
|
|
|
|
&io_path_mutex1,
|
|
|
|
|
&dispatch_cond1,
|
|
|
|
|
&io_path_cond1);
|
|
|
|
|
|
|
|
|
|
_run_iteration (conn);
|
|
|
|
|
_dbus_connection_test_get_locks (conn, &mutex2,
|
|
|
|
|
&dispatch_mutex2,
|
|
|
|
|
&io_path_mutex2,
|
|
|
|
|
&dispatch_cond2,
|
|
|
|
|
&io_path_cond2);
|
|
|
|
|
|
|
|
|
|
check_mutex_lock (mutex1, mutex2, TRUE);
|
|
|
|
|
check_mutex_lock (dispatch_mutex1, dispatch_mutex2, TRUE);
|
|
|
|
|
check_mutex_lock (io_path_mutex1, io_path_mutex2, TRUE);
|
|
|
|
|
check_condvar_lock (dispatch_cond1, dispatch_cond2, TRUE);
|
|
|
|
|
check_condvar_lock (io_path_cond1, io_path_cond2, TRUE);
|
2016-02-08 20:28:21 +00:00
|
|
|
printf ("ok %d\n", ++test_num);
|
2006-08-16 22:30:15 +00:00
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2017-02-06 13:07:21 +00:00
|
|
|
printf ("# Testing completed\n1..%d\n", test_num);
|
2006-08-16 22:30:15 +00:00
|
|
|
exit (0);
|
|
|
|
|
}
|