dispatch.c 147 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 3 4
/* dispatch.c  Message dispatcher
 *
 * Copyright (C) 2003  CodeFactory AB
5
 * Copyright (C) 2003, 2004, 2005  Red Hat, Inc.
6
 * Copyright (C) 2004  Imendio HB
7
 *
8
 * Licensed under the Academic Free License version 2.1
9
 *
10 11 12 13 14 15 16 17 18
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
20 21
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23 24 25
 *
 */

26
#include <config.h>
27 28 29
#include "dispatch.h"
#include "connection.h"
#include "driver.h"
30
#include "services.h"
31
#include "activation.h"
32
#include "utils.h"
33
#include "bus.h"
34
#include "signals.h"
35
#include "test.h"
36
#include <dbus/dbus-internals.h>
37
#include <dbus/dbus-misc.h>
38 39
#include <string.h>

40 41 42 43 44
#ifdef HAVE_UNIX_FD_PASSING
#include <dbus/dbus-sysdeps-unix.h>
#include <unistd.h>
#endif

45 46 47 48
/* This is hard-coded in the files in valid-config-files-*. We have to use
 * the debug-pipe transport because the tests in this file require that
 * dbus_connection_open_private() does not block. */
#define TEST_DEBUG_PIPE "debug-pipe:name=test-server"
49

50 51 52 53 54 55 56
static inline const char *
nonnull (const char *maybe_null,
         const char *if_null)
{
  return (maybe_null ? maybe_null : if_null);
}

57
static dbus_bool_t
58 59 60 61 62 63 64
send_one_message (DBusConnection *connection,
                  BusContext     *context,
                  DBusConnection *sender,
                  DBusConnection *addressed_recipient,
                  DBusMessage    *message,
                  BusTransaction *transaction,
                  DBusError      *error)
65
{
66 67
  DBusError stack_error = DBUS_ERROR_INIT;

68
  if (!bus_context_check_security_policy (context, transaction,
69 70
                                          sender,
                                          addressed_recipient,
71
                                          connection,
72
                                          message,
73 74
                                          &stack_error))
    {
75 76
      if (!bus_transaction_capture_error_reply (transaction, sender,
                                                &stack_error, message))
77 78 79 80 81 82 83 84 85
        {
          bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
                           "broadcast rejected, but not enough "
                           "memory to tell monitors");
        }

      dbus_error_free (&stack_error);
      return TRUE; /* don't send it but don't return an error either */
    }
86 87 88

  if (dbus_message_contains_unix_fds(message) &&
      !dbus_connection_can_send_type(connection, DBUS_TYPE_UNIX_FD))
89 90 91 92 93 94 95
    {
      dbus_set_error (&stack_error, DBUS_ERROR_NOT_SUPPORTED,
                      "broadcast cannot be delivered to %s (%s) because "
                      "it does not support receiving Unix fds",
                      bus_connection_get_name (connection),
                      bus_connection_get_loginfo (connection));

96 97
      if (!bus_transaction_capture_error_reply (transaction, sender,
                                                &stack_error, message))
98 99 100 101 102 103 104 105 106
        {
          bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
                           "broadcast with Unix fd not delivered, but not "
                           "enough memory to tell monitors");
        }

      dbus_error_free (&stack_error);
      return TRUE; /* don't send it but don't return an error either */
    }
107

108
  if (!bus_transaction_send (transaction,
109
                             connection,
110
                             message))
111
    {
112
      BUS_SET_OOM (error);
113 114 115 116
      return FALSE;
    }

  return TRUE;
117 118
}

119
dbus_bool_t
120 121 122 123 124
bus_dispatch_matches (BusTransaction *transaction,
                      DBusConnection *sender,
                      DBusConnection *addressed_recipient,
                      DBusMessage    *message,
                      DBusError      *error)
125
{
126
  DBusError tmp_error;
127
  BusConnections *connections;
128 129 130 131
  DBusList *recipients;
  BusMatchmaker *matchmaker;
  DBusList *link;
  BusContext *context;
132 133

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
134 135 136 137 138 139

  /* sender and recipient can both be NULL for the bus driver,
   * or for signals with no particular recipient
   */

  _dbus_assert (sender == NULL || bus_connection_is_active (sender));
140 141
  _dbus_assert (dbus_message_get_sender (message) != NULL);

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
  context = bus_transaction_get_context (transaction);

  /* First, send the message to the addressed_recipient, if there is one. */
  if (addressed_recipient != NULL)
    {
      if (!bus_context_check_security_policy (context, transaction,
                                              sender, addressed_recipient,
                                              addressed_recipient,
                                              message, error))
        return FALSE;

      if (dbus_message_contains_unix_fds (message) &&
          !dbus_connection_can_send_type (addressed_recipient,
                                          DBUS_TYPE_UNIX_FD))
        {
          dbus_set_error (error,
                          DBUS_ERROR_NOT_SUPPORTED,
                          "Tried to send message with Unix file descriptors"
                          "to a client that doesn't support that.");
          return FALSE;
      }

      /* Dispatch the message */
      if (!bus_transaction_send (transaction, addressed_recipient, message))
        {
          BUS_SET_OOM (error);
          return FALSE;
        }
    }

  /* Now dispatch to others who look interested in this message */
173
  connections = bus_context_get_connections (context);
174
  dbus_error_init (&tmp_error);
175 176 177
  matchmaker = bus_context_get_matchmaker (context);

  recipients = NULL;
178
  if (!bus_matchmaker_get_recipients (matchmaker, connections,
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
                                      sender, addressed_recipient, message,
                                      &recipients))
    {
      BUS_SET_OOM (error);
      return FALSE;
    }

  link = _dbus_list_get_first_link (&recipients);
  while (link != NULL)
    {
      DBusConnection *dest;

      dest = link->data;

      if (!send_one_message (dest, context, sender, addressed_recipient,
                             message, transaction, &tmp_error))
        break;

      link = _dbus_list_get_next_link (&recipients, link);
    }
199

200
  _dbus_list_clear (&recipients);
201

202 203 204 205 206 207 208 209 210
  if (dbus_error_is_set (&tmp_error))
    {
      dbus_move_error (&tmp_error, error);
      return FALSE;
    }
  else
    return TRUE;
}

211
static DBusHandlerResult
212 213
bus_dispatch (DBusConnection *connection,
              DBusMessage    *message)
214
{
215
  const char *sender, *service_name;
216 217
  DBusError error;
  BusTransaction *transaction;
218
  BusContext *context;
219
  DBusHandlerResult result;
220
  DBusConnection *addressed_recipient;
221

222
  result = DBUS_HANDLER_RESULT_HANDLED;
223

224
  transaction = NULL;
225
  addressed_recipient = NULL;
226
  dbus_error_init (&error);
227

228 229
  context = bus_connection_get_context (connection);
  _dbus_assert (context != NULL);
230

231 232 233 234
  /* If we can't even allocate an OOM error, we just go to sleep
   * until we can.
   */
  while (!bus_connection_preallocate_oom_error (connection))
235
    _dbus_wait_for_memory ();
236

237 238
  /* Ref connection in case we disconnect it at some point in here */
  dbus_connection_ref (connection);
239

240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
  /* Monitors aren't meant to send messages to us. */
  if (bus_connection_is_monitor (connection))
    {
      sender = bus_connection_get_name (connection);

      /* should never happen */
      if (sender == NULL)
        sender = "(unknown)";

      if (dbus_message_is_signal (message,
                                  DBUS_INTERFACE_LOCAL,
                                  "Disconnected"))
        {
          bus_context_log (context, DBUS_SYSTEM_LOG_INFO,
                           "Monitoring connection %s closed.", sender);
          bus_connection_disconnected (connection);
          goto out;
        }
      else
        {
          /* Monitors are not allowed to send messages, because that
           * probably indicates that the monitor is incorrectly replying
           * to its eavesdropped messages, and we want the authors of
           * such monitors to fix them.
           */
          bus_context_log (context, DBUS_SYSTEM_LOG_WARNING,
                           "Monitoring connection %s (%s) is not allowed "
                           "to send messages; closing it. Please fix the "
                           "monitor to not do that. "
                           "(message type=\"%s\" interface=\"%s\" "
                           "member=\"%s\" error name=\"%s\" "
                           "destination=\"%s\")",
                           sender, bus_connection_get_loginfo (connection),
                           dbus_message_type_to_string (
                             dbus_message_get_type (message)),
                           nonnull (dbus_message_get_interface (message),
                                    "(unset)"),
                           nonnull (dbus_message_get_member (message),
                                    "(unset)"),
                           nonnull (dbus_message_get_error_name (message),
                                    "(unset)"),
                           nonnull (dbus_message_get_destination (message),
                                    DBUS_SERVICE_DBUS));
          dbus_connection_close (connection);
          goto out;
        }
    }

288
  service_name = dbus_message_get_destination (message);
289

290 291 292 293 294 295 296
#ifdef DBUS_ENABLE_VERBOSE_MODE
  {
    const char *interface_name, *member_name, *error_name;

    interface_name = dbus_message_get_interface (message);
    member_name = dbus_message_get_member (message);
    error_name = dbus_message_get_error_name (message);
297

298 299 300 301 302 303 304
    _dbus_verbose ("DISPATCH: %s %s %s to %s\n",
                   interface_name ? interface_name : "(no interface)",
                   member_name ? member_name : "(no member)",
                   error_name ? error_name : "(no error name)",
                   service_name ? service_name : "peer");
  }
#endif /* DBUS_ENABLE_VERBOSE_MODE */
305

306
  /* If service_name is NULL, if it's a signal we send it to all
307 308
   * connections with a match rule. If it's not a signal, there
   * are some special cases here but mostly we just bail out.
309 310
   */
  if (service_name == NULL)
311
    {
312
      if (dbus_message_is_signal (message,
313
                                  DBUS_INTERFACE_LOCAL,
314
                                  "Disconnected"))
315 316 317 318
        {
          bus_connection_disconnected (connection);
          goto out;
        }
319

320 321 322 323
      if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL)
        {
          /* DBusConnection also handles some of these automatically, we leave
           * it to do so.
324 325 326 327
           *
           * FIXME: this means monitors won't get the opportunity to see
           * non-signals with NULL destination, or their replies (which in
           * practice are UnknownMethod errors)
328 329 330 331
           */
          result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
          goto out;
        }
332
    }
333

334
  /* Create our transaction */
335
  transaction = bus_transaction_new (context);
336 337 338 339 340
  if (transaction == NULL)
    {
      BUS_SET_OOM (&error);
      goto out;
    }
341

342 343
  /* Assign a sender to the message */
  if (bus_connection_is_active (connection))
344
    {
345 346
      sender = bus_connection_get_name (connection);
      _dbus_assert (sender != NULL);
347

348 349 350 351 352
      if (!dbus_message_set_sender (message, sender))
        {
          BUS_SET_OOM (&error);
          goto out;
        }
353 354 355 356 357 358 359 360 361 362 363 364
    }
  else
    {
      /* For monitors' benefit: we don't want the sender to be able to
       * trick the monitor by supplying a forged sender, and we also
       * don't want the message to have no sender at all. */
      if (!dbus_message_set_sender (message, ":not.active.yet"))
        {
          BUS_SET_OOM (&error);
          goto out;
        }
    }
365

366 367 368 369 370 371 372
  /* We need to refetch the service name here, because
   * dbus_message_set_sender can cause the header to be
   * reallocated, and thus the service_name pointer will become
   * invalid.
   */
  service_name = dbus_message_get_destination (message);

373
  if (service_name &&
374
      strcmp (service_name, DBUS_SERVICE_DBUS) == 0) /* to bus driver */
375
    {
376 377 378 379 380 381
      if (!bus_transaction_capture (transaction, connection, NULL, message))
        {
          BUS_SET_OOM (&error);
          goto out;
        }

382
      if (!bus_context_check_security_policy (context, transaction,
383
                                              connection, NULL, NULL, message, &error))
384 385 386 387 388
        {
          _dbus_verbose ("Security policy rejected message\n");
          goto out;
        }

389
      _dbus_verbose ("Giving message to %s\n", DBUS_SERVICE_DBUS);
390
      if (!bus_driver_handle_message (connection, transaction, message, &error))
391
        goto out;
392
    }
393
  else if (!bus_connection_is_active (connection)) /* clients must talk to bus driver first */
394
    {
395 396 397 398 399 400
      if (!bus_transaction_capture (transaction, connection, NULL, message))
        {
          BUS_SET_OOM (&error);
          goto out;
        }

401
      _dbus_verbose ("Received message from non-registered client. Disconnecting.\n");
402
      dbus_connection_close (connection);
403
      goto out;
404
    }
405
  else if (service_name != NULL) /* route to named service */
406
    {
407 408
      DBusString service_string;
      BusService *service;
409
      BusRegistry *registry;
410

411
      _dbus_assert (service_name != NULL);
412

413
      registry = bus_connection_get_registry (connection);
414

415
      _dbus_string_init_const (&service_string, service_name);
416
      service = bus_registry_lookup (registry, &service_string);
417

418
      if (service == NULL && dbus_message_get_auto_start (message))
419
        {
420
          BusActivation *activation;
421 422 423 424 425 426 427 428

          if (!bus_transaction_capture (transaction, connection, NULL,
                                        message))
            {
              BUS_SET_OOM (&error);
              goto out;
            }

429 430 431 432 433 434 435 436 437 438
          /* We can't do the security policy check here, since the addressed
           * recipient service doesn't exist yet. We do it before sending the
           * message after the service has been created.
           */
          activation = bus_connection_get_activation (connection);

          if (!bus_activation_activate_service (activation, connection, transaction, TRUE,
                                                message, service_name, &error))
            {
              _DBUS_ASSERT_ERROR_IS_SET (&error);
439
              _dbus_verbose ("bus_activation_activate_service() failed: %s\n", error.name);
440 441
              goto out;
            }
442

443 444
          goto out;
        }
445
      else if (service == NULL)
446
        {
447 448 449 450 451 452 453
          if (!bus_transaction_capture (transaction, connection,
                                        NULL, message))
            {
              BUS_SET_OOM (&error);
              goto out;
            }

454
          dbus_set_error (&error,
455 456
                          DBUS_ERROR_NAME_HAS_NO_OWNER,
                          "Name \"%s\" does not exist",
457 458
                          service_name);
          goto out;
459 460
        }
      else
461
        {
462
          addressed_recipient = bus_service_get_primary_owners_connection (service);
463
          _dbus_assert (addressed_recipient != NULL);
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

          if (!bus_transaction_capture (transaction, connection,
                                        addressed_recipient, message))
            {
              BUS_SET_OOM (&error);
              goto out;
            }
        }
    }
  else /* service_name == NULL */
    {
      if (!bus_transaction_capture (transaction, connection, NULL, message))
        {
          BUS_SET_OOM (&error);
          goto out;
479 480
        }
    }
481

482 483 484
  /* Now send the message to its destination (or not, if
   * addressed_recipient == NULL), and match it against other connections'
   * match rules.
485 486 487
   */
  if (!bus_dispatch_matches (transaction, connection, addressed_recipient, message, &error))
    goto out;
488

489 490 491
 out:
  if (dbus_error_is_set (&error))
    {
492 493 494 495
      /* Even if we disconnected it, pretend to send it any pending error
       * messages so that monitors can observe them.
       */
      if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
496 497
        {
          bus_connection_send_oom_error (connection, message);
498

499 500 501 502 503 504 505
          /* cancel transaction due to OOM */
          if (transaction != NULL)
            {
              bus_transaction_cancel_and_free (transaction);
              transaction = NULL;
            }
        }
506
      else
507 508 509 510 511 512 513 514 515
        {
          /* Try to send the real error, if no mem to do that, send
           * the OOM error
           */
          _dbus_assert (transaction != NULL);
          if (!bus_transaction_send_error_reply (transaction, connection,
                                                 &error, message))
            {
              bus_connection_send_oom_error (connection, message);
516

517 518 519 520 521 522 523 524
              /* cancel transaction due to OOM */
              if (transaction != NULL)
                {
                  bus_transaction_cancel_and_free (transaction);
                  transaction = NULL;
                }
            }
        }
525 526


527
      dbus_error_free (&error);
528 529
    }

530 531 532 533 534 535
  if (transaction != NULL)
    {
      bus_transaction_execute_and_free (transaction);
    }

  dbus_connection_unref (connection);
536 537

  return result;
538 539 540
}

static DBusHandlerResult
541 542 543
bus_dispatch_message_filter (DBusConnection     *connection,
                             DBusMessage        *message,
                             void               *user_data)
544
{
545
  return bus_dispatch (connection, message);
546 547
}

548 549
dbus_bool_t
bus_dispatch_add_connection (DBusConnection *connection)
550
{
551 552 553
  if (!dbus_connection_add_filter (connection,
                                   bus_dispatch_message_filter,
                                   NULL, NULL))
554
    return FALSE;
555

556 557 558 559 560 561 562 563
  return TRUE;
}

void
bus_dispatch_remove_connection (DBusConnection *connection)
{
  /* Here we tell the bus driver that we want to get off. */
  bus_driver_remove_connection (connection);
564

565 566 567
  dbus_connection_remove_filter (connection,
                                 bus_dispatch_message_filter,
                                 NULL);
568
}
569

570
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
571

572 573
#include <stdio.h>

574 575
#include "stats.h"

576 577 578 579 580 581
/* This is used to know whether we need to block in order to finish
 * sending a message, or whether the initial dbus_connection_send()
 * already flushed the queue.
 */
#define SEND_PENDING(connection) (dbus_connection_has_messages_to_send (connection))

582 583 584 585
typedef dbus_bool_t (* Check1Func) (BusContext     *context);
typedef dbus_bool_t (* Check2Func) (BusContext     *context,
                                    DBusConnection *connection);

586 587
static dbus_bool_t check_no_leftovers (BusContext *context);

588 589
static void
block_connection_until_message_from_bus (BusContext     *context,
590 591
                                         DBusConnection *connection,
                                         const char     *what_is_expected)
592
{
593
  _dbus_verbose ("expecting: %s\n", what_is_expected);
594

595 596 597 598 599 600 601 602 603
  while (dbus_connection_get_dispatch_status (connection) ==
         DBUS_DISPATCH_COMPLETE &&
         dbus_connection_get_is_connected (connection))
    {
      bus_test_run_bus_loop (context, TRUE);
      bus_test_run_clients_loop (FALSE);
    }
}

604 605 606 607 608 609 610 611 612 613 614 615 616 617
static void
spin_connection_until_authenticated (BusContext     *context,
                                     DBusConnection *connection)
{
  _dbus_verbose ("Spinning to auth connection %p\n", connection);
  while (!dbus_connection_get_is_authenticated (connection) &&
         dbus_connection_get_is_connected (connection))
    {
      bus_test_run_bus_loop (context, FALSE);
      bus_test_run_clients_loop (FALSE);
    }
  _dbus_verbose (" ... done spinning to auth connection %p\n", connection);
}

618 619 620 621 622 623 624 625 626 627 628
/* compensate for fact that pop_message() can return #NULL due to OOM */
static DBusMessage*
pop_message_waiting_for_memory (DBusConnection *connection)
{
  while (dbus_connection_get_dispatch_status (connection) ==
         DBUS_DISPATCH_NEED_MEMORY)
    _dbus_wait_for_memory ();

  return dbus_connection_pop_message (connection);
}

629 630 631 632 633 634 635 636 637 638
static DBusMessage*
borrow_message_waiting_for_memory (DBusConnection *connection)
{
  while (dbus_connection_get_dispatch_status (connection) ==
         DBUS_DISPATCH_NEED_MEMORY)
    _dbus_wait_for_memory ();

  return dbus_connection_borrow_message (connection);
}

639
static void
640 641 642 643 644
warn_unexpected_real (DBusConnection *connection,
                      DBusMessage    *message,
                      const char     *expected,
                      const char     *function,
                      int             line)
645
{
646 647 648 649 650 651 652 653 654 655 656 657 658 659
  if (message)
    _dbus_warn ("%s:%d received message interface \"%s\" member \"%s\" error name \"%s\" on %p, expecting %s\n",
                function, line,
                dbus_message_get_interface (message) ?
                dbus_message_get_interface (message) : "(unset)",
                dbus_message_get_member (message) ?
                dbus_message_get_member (message) : "(unset)",
                dbus_message_get_error_name (message) ?
                dbus_message_get_error_name (message) : "(unset)",
                connection,
                expected);
  else
    _dbus_warn ("%s:%d received no message on %p, expecting %s\n",
                function, line, connection, expected);
660 661
}

662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
#define warn_unexpected(connection, message, expected) \
  warn_unexpected_real (connection, message, expected, _DBUS_FUNCTION_NAME, __LINE__)

static void
verbose_message_received (DBusConnection *connection,
                          DBusMessage    *message)
{
  _dbus_verbose ("Received message interface \"%s\" member \"%s\" error name \"%s\" on %p\n",
                 dbus_message_get_interface (message) ?
                 dbus_message_get_interface (message) : "(unset)",
                 dbus_message_get_member (message) ?
                 dbus_message_get_member (message) : "(unset)",
                 dbus_message_get_error_name (message) ?
                 dbus_message_get_error_name (message) : "(unset)",
                 connection);
}

679 680 681 682 683 684 685
typedef enum
{
  SERVICE_CREATED,
  OWNER_CHANGED,
  SERVICE_DELETED
} ServiceInfoKind;

686 687
typedef struct
{
688
  ServiceInfoKind expected_kind;
689 690
  const char *expected_service_name;
  dbus_bool_t failed;
691
  DBusConnection *skip_connection;
692
  BusContext *context;
693
} CheckServiceOwnerChangedData;
694 695

static dbus_bool_t
696
check_service_owner_changed_foreach (DBusConnection *connection,
697
                                     void           *data)
698
{
699
  CheckServiceOwnerChangedData *d = data;
700 701
  DBusMessage *message;
  DBusError error;
702
  const char *service_name, *old_owner, *new_owner;
703

704
  if (d->expected_kind == SERVICE_CREATED
705 706
      && connection == d->skip_connection)
    return TRUE;
707 708 709

  dbus_error_init (&error);
  d->failed = TRUE;
710

711
  message = pop_message_waiting_for_memory (connection);
712 713
  if (message == NULL)
    {
714 715 716 717 718 719 720 721
      block_connection_until_message_from_bus (d->context, connection, "NameOwnerChanged");
      message = pop_message_waiting_for_memory (connection);
      if (message == NULL)
        {
          _dbus_warn ("Did not receive a message on %p, expecting %s\n",
                      connection, "NameOwnerChanged");
          goto out;
        }
722
    }
723
  else if (!dbus_message_is_signal (message,
724
                                    DBUS_INTERFACE_DBUS,
725
                                    "NameOwnerChanged"))
726
    {
727
      warn_unexpected (connection, message, "NameOwnerChanged");
728

729 730 731 732
      goto out;
    }
  else
    {
733 734 735 736 737 738 739 740 741 742 743 744
    reget_service_info_data:
      service_name = NULL;
      old_owner = NULL;
      new_owner = NULL;

      dbus_message_get_args (message, &error,
                             DBUS_TYPE_STRING, &service_name,
                             DBUS_TYPE_STRING, &old_owner,
                             DBUS_TYPE_STRING, &new_owner,
                             DBUS_TYPE_INVALID);

      if (dbus_error_is_set (&error))
745 746 747
        {
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
            {
748
              dbus_error_free (&error);
749
              _dbus_wait_for_memory ();
750
              goto reget_service_info_data;
751 752 753 754 755 756 757
            }
          else
            {
              _dbus_warn ("Did not get the expected arguments\n");
              goto out;
            }
        }
758 759 760 761

      if ((d->expected_kind == SERVICE_CREATED    && ( old_owner[0] || !new_owner[0]))
          || (d->expected_kind == OWNER_CHANGED   && (!old_owner[0] || !new_owner[0]))
          || (d->expected_kind == SERVICE_DELETED && (!old_owner[0] ||  new_owner[0])))
762
        {
763
          _dbus_warn ("inconsistent NameOwnerChanged arguments\n");
764
          goto out;
765
        }
766 767

      if (strcmp (service_name, d->expected_service_name) != 0)
768
        {
769
          _dbus_warn ("expected info on service %s, got info on %s\n",
770 771 772 773
                      d->expected_service_name,
                      service_name);
          goto out;
        }
774

775
      if (*service_name == ':' && new_owner[0]
776 777 778 779 780 781
          && strcmp (service_name, new_owner) != 0)
        {
          _dbus_warn ("inconsistent ServiceOwnedChanged message (\"%s\" [ %s -> %s ])\n",
                      service_name, old_owner, new_owner);
          goto out;
        }
782 783 784
    }

  d->failed = FALSE;
785

786 787
 out:
  dbus_error_free (&error);
788

789 790 791 792 793 794
  if (message)
    dbus_message_unref (message);

  return !d->failed;
}

795

796
static void
797 798
kill_client_connection (BusContext     *context,
                        DBusConnection *connection)
799
{
800 801
  char *base_service;
  const char *s;
802
  CheckServiceOwnerChangedData socd;
803 804

  _dbus_verbose ("killing connection %p\n", connection);
805

806
  s = dbus_bus_get_unique_name (connection);
807 808 809
  _dbus_assert (s != NULL);

  while ((base_service = _dbus_strdup (s)) == NULL)
810
    _dbus_wait_for_memory ();
811 812

  dbus_connection_ref (connection);
813

814
  /* kick in the disconnect handler that unrefs the connection */
815
  dbus_connection_close (connection);
816

817
  bus_test_run_everything (context);
818

819
  _dbus_assert (bus_test_client_listed (connection));
820

821
  /* Run disconnect handler in test.c */
822
  if (bus_connection_dispatch_one_message (connection))
823
    _dbus_assert_not_reached ("something received on connection being killed other than the disconnect");
824

825 826 827 828
  _dbus_assert (!dbus_connection_get_is_connected (connection));
  dbus_connection_unref (connection);
  connection = NULL;
  _dbus_assert (!bus_test_client_listed (connection));
829

830 831 832 833
  socd.expected_kind = SERVICE_DELETED;
  socd.expected_service_name = base_service;
  socd.failed = FALSE;
  socd.skip_connection = NULL;
834
  socd.context = context;
835

836 837
  bus_test_clients_foreach (check_service_owner_changed_foreach,
                            &socd);
838 839

  dbus_free (base_service);
840

841
  if (socd.failed)
842
    _dbus_assert_not_reached ("didn't get the expected NameOwnerChanged (deletion) messages");
843

844 845 846 847
  if (!check_no_leftovers (context))
    _dbus_assert_not_reached ("stuff left in message queues after disconnecting a client");
}

848 849 850 851 852
static void
kill_client_connection_unchecked (DBusConnection *connection)
{
  /* This kills the connection without expecting it to affect
   * the rest of the bus.
853
   */
854 855 856
  _dbus_verbose ("Unchecked kill of connection %p\n", connection);

  dbus_connection_ref (connection);
857
  dbus_connection_close (connection);
858 859 860
  /* dispatching disconnect handler will unref once */
  if (bus_connection_dispatch_one_message (connection))
    _dbus_assert_not_reached ("message other than disconnect dispatched after failure to register");
861

862
  _dbus_assert (!bus_test_client_listed (connection));
863
  dbus_connection_unref (connection);
864 865
}

866 867 868 869 870 871 872 873 874 875 876 877
typedef struct
{
  dbus_bool_t failed;
} CheckNoMessagesData;

static dbus_bool_t
check_no_messages_foreach (DBusConnection *connection,
                           void           *data)
{
  CheckNoMessagesData *d = data;
  DBusMessage *message;

878
  message = pop_message_waiting_for_memory (connection);
879 880
  if (message != NULL)
    {
881 882
      warn_unexpected (connection, message, "no messages");

883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
      d->failed = TRUE;
    }

  if (message)
    dbus_message_unref (message);
  return !d->failed;
}

static dbus_bool_t
check_no_leftovers (BusContext *context)
{
  CheckNoMessagesData nmd;

  nmd.failed = FALSE;
  bus_test_clients_foreach (check_no_messages_foreach,
                            &nmd);
899

900
  if (nmd.failed)
901
    {
902
      _dbus_verbose ("leftover message found\n");
903 904
      return FALSE;
    }
905 906
  else
    return TRUE;
907 908
}

909 910 911
/* returns TRUE if the correct thing happens,
 * but the correct thing may include OOM errors.
 */
912 913 914 915 916
static dbus_bool_t
check_hello_message (BusContext     *context,
                     DBusConnection *connection)
{
  DBusMessage *message;
917
  DBusMessage *name_message;
918
  dbus_uint32_t serial;
919 920
  dbus_bool_t retval;
  DBusError error;
921 922
  const char *name;
  const char *acquired;
923 924

  retval = FALSE;
925
  dbus_error_init (&error);
926 927
  name = NULL;
  acquired = NULL;
928
  message = NULL;
929
  name_message = NULL;
930 931

  _dbus_verbose ("check_hello_message for %p\n", connection);
932

933 934 935
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
                                          DBUS_PATH_DBUS,
                                          DBUS_INTERFACE_DBUS,
936
                                          "Hello");
937 938

  if (message == NULL)
939
    return TRUE;
940

941
  dbus_connection_ref (connection); /* because we may get disconnected */
942

943
  if (!dbus_connection_send (connection, message, &serial))
944 945
    {
      dbus_message_unref (message);
946
      dbus_connection_unref (connection);
947 948
      return TRUE;
    }
949

950
  _dbus_assert (dbus_message_has_signature (message, ""));
951

952
  dbus_message_unref (message);
953
  message = NULL;
954

955 956 957
  if (!dbus_connection_get_is_connected (connection))
    {
      _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
958

959
      dbus_connection_unref (connection);
960

961 962
      return TRUE;
    }
963

964
  /* send our message */
965
  bus_test_run_clients_loop (SEND_PENDING (connection));
966

967 968 969
  if (!dbus_connection_get_is_connected (connection))
    {
      _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
970

971
      dbus_connection_unref (connection);
972

973 974
      return TRUE;
    }
975

976
  block_connection_until_message_from_bus (context, connection, "reply to Hello");
977

978 979
  if (!dbus_connection_get_is_connected (connection))
    {
980
      _dbus_verbose ("connection was disconnected (presumably auth failed)\n");
981

982
      dbus_connection_unref (connection);
983

984 985
      return TRUE;
    }
986

987
  message = pop_message_waiting_for_memory (connection);
988 989 990
  if (message == NULL)
    {
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
991
                  "Hello", serial, connection);
992
      goto out;
993 994
    }

995
  verbose_message_received (connection, message);
996

997
  if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
998 999 1000 1001 1002 1003
    {
      _dbus_warn ("Message has wrong sender %s\n",
                  dbus_message_get_sender (message) ?
                  dbus_message_get_sender (message) : "(none)");
      goto out;
    }
1004

1005
  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
1006
    {
1007
      if (dbus_message_is_error (message,
1008
                                 DBUS_ERROR_NO_MEMORY))
1009 1010 1011 1012 1013
        {
          ; /* good, this is a valid response */
        }
      else
        {
1014 1015
          warn_unexpected (connection, message, "not this error");

1016 1017 1018 1019 1020
          goto out;
        }
    }
  else
    {
1021