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
      CheckServiceOwnerChangedData socd;
1022

1023
      if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
1024 1025 1026 1027 1028
        {
          ; /* good, expected */
        }
      else
        {
1029 1030
          warn_unexpected (connection, message, "method return for Hello");

1031 1032 1033
          goto out;
        }

1034
    retry_get_hello_name:
1035
      if (!dbus_message_get_args (message, &error,
1036
                                  DBUS_TYPE_STRING, &name,
1037 1038
                                  DBUS_TYPE_INVALID))
        {
1039 1040 1041 1042
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
            {
              _dbus_verbose ("no memory to get service name arg from hello\n");
              dbus_error_free (&error);
1043
              _dbus_wait_for_memory ();
1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
              goto retry_get_hello_name;
            }
          else
            {
              _dbus_assert (dbus_error_is_set (&error));
              _dbus_warn ("Did not get the expected single string argument to hello\n");
              goto out;
            }
        }

      _dbus_verbose ("Got hello name: %s\n", name);

1056
      while (!dbus_bus_set_unique_name (connection, name))
1057
        _dbus_wait_for_memory ();
1058

1059 1060 1061 1062
      socd.expected_kind = SERVICE_CREATED;
      socd.expected_service_name = name;
      socd.failed = FALSE;
      socd.skip_connection = connection; /* we haven't done AddMatch so won't get it ourselves */
1063 1064
      socd.context = context;

1065 1066
      bus_test_clients_foreach (check_service_owner_changed_foreach,
                                &socd);
1067

1068
      if (socd.failed)
1069
        goto out;
1070 1071

      name_message = message;
1072
      /* Client should also have gotten ServiceAcquired */
1073

1074
      message = pop_message_waiting_for_memory (connection);
1075 1076
      if (message == NULL)
        {
1077 1078 1079 1080 1081
          block_connection_until_message_from_bus (context, connection, "signal NameAcquired");
          message = pop_message_waiting_for_memory (connection);
          if (message == NULL)
            {
              _dbus_warn ("Expecting %s, got nothing\n",
1082
                      "NameAcquired");
1083 1084
              goto out;
            }
1085
        }
1086
      if (! dbus_message_is_signal (message, DBUS_INTERFACE_DBUS,
1087
                                    "NameAcquired"))
1088 1089
        {
          _dbus_warn ("Expecting %s, got smthg else\n",
1090
                      "NameAcquired");
1091 1092
          goto out;
        }
1093

1094 1095 1096 1097 1098 1099 1100 1101 1102
    retry_get_acquired_name:
      if (!dbus_message_get_args (message, &error,
                                  DBUS_TYPE_STRING, &acquired,
                                  DBUS_TYPE_INVALID))
        {
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
            {
              _dbus_verbose ("no memory to get service name arg from acquired\n");
              dbus_error_free (&error);
1103
              _dbus_wait_for_memory ();
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
              goto retry_get_acquired_name;
            }
          else
            {
              _dbus_assert (dbus_error_is_set (&error));
              _dbus_warn ("Did not get the expected single string argument to ServiceAcquired\n");
              goto out;
            }
        }

      _dbus_verbose ("Got acquired name: %s\n", acquired);
1115

1116 1117 1118 1119 1120 1121
      if (strcmp (acquired, name) != 0)
        {
          _dbus_warn ("Acquired name is %s but expected %s\n",
                      acquired, name);
          goto out;
        }
1122
      acquired = NULL;
1123
    }
1124 1125 1126

  if (!check_no_leftovers (context))
    goto out;
1127

1128
  retval = TRUE;
1129

1130
 out:
1131
  _dbus_verbose ("ending - retval = %d\n", retval);
1132

1133
  dbus_error_free (&error);
1134

1135 1136
  if (message)
    dbus_message_unref (message);
1137 1138 1139

  if (name_message)
    dbus_message_unref (name_message);
1140

1141 1142
  dbus_connection_unref (connection);

1143 1144 1145
  return retval;
}

1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
/* returns TRUE if the correct thing happens,
 * but the correct thing may include OOM errors.
 */
static dbus_bool_t
check_double_hello_message (BusContext     *context,
                            DBusConnection *connection)
{
  DBusMessage *message;
  dbus_uint32_t serial;
  dbus_bool_t retval;
  DBusError error;

  retval = FALSE;
  dbus_error_init (&error);
  message = NULL;

  _dbus_verbose ("check_double_hello_message for %p\n", connection);
1163

1164 1165 1166
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
                                          DBUS_PATH_DBUS,
                                          DBUS_INTERFACE_DBUS,
1167 1168 1169 1170
                                          "Hello");

  if (message == NULL)
    return TRUE;
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
  if (!dbus_connection_send (connection, message, &serial))
    {
      dbus_message_unref (message);
      return TRUE;
    }

  dbus_message_unref (message);
  message = NULL;

  /* send our message */
1182
  bus_test_run_clients_loop (SEND_PENDING (connection));
1183 1184

  dbus_connection_ref (connection); /* because we may get disconnected */
1185
  block_connection_until_message_from_bus (context, connection, "reply to Hello");
1186 1187 1188

  if (!dbus_connection_get_is_connected (connection))
    {
1189
      _dbus_verbose ("connection was disconnected\n");
1190

1191
      dbus_connection_unref (connection);
1192

1193 1194 1195 1196
      return TRUE;
    }

  dbus_connection_unref (connection);
1197

1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
  message = pop_message_waiting_for_memory (connection);
  if (message == NULL)
    {
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
                  "Hello", serial, connection);
      goto out;
    }

  verbose_message_received (connection, message);

1208
  if (!dbus_message_has_sender (message, DBUS_SERVICE_DBUS))
1209 1210 1211 1212 1213 1214
    {
      _dbus_warn ("Message has wrong sender %s\n",
                  dbus_message_get_sender (message) ?
                  dbus_message_get_sender (message) : "(none)");
      goto out;
    }
1215

1216 1217 1218 1219 1220 1221 1222 1223
  if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
    {
      warn_unexpected (connection, message, "method return for Hello");
      goto out;
    }

  if (!check_no_leftovers (context))
    goto out;
1224

1225
  retval = TRUE;
1226

1227 1228
 out:
  dbus_error_free (&error);
1229

1230 1231
  if (message)
    dbus_message_unref (message);
1232

1233 1234 1235
  return retval;
}

1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
/* returns TRUE if the correct thing happens,
 * but the correct thing may include OOM errors.
 */
static dbus_bool_t
check_get_connection_unix_user (BusContext     *context,
                                DBusConnection *connection)
{
  DBusMessage *message;
  dbus_uint32_t serial;
  dbus_bool_t retval;
  DBusError error;
  const char *base_service_name;
  dbus_uint32_t uid;

  retval = FALSE;
  dbus_error_init (&error);
  message = NULL;

  _dbus_verbose ("check_get_connection_unix_user for %p\n", connection);
1255

1256 1257 1258
  message = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
                                          DBUS_PATH_DBUS,
                                          DBUS_INTERFACE_DBUS,
1259 1260 1261 1262 1263
                                          "GetConnectionUnixUser");

  if (message == NULL)
    return TRUE;

1264
  base_service_name = dbus_bus_get_unique_name (connection);
1265

1266
  if (!dbus_message_append_args (message,
1267
                                 DBUS_TYPE_STRING, &base_service_name,
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
                                 DBUS_TYPE_INVALID))
    {
      dbus_message_unref (message);
      return TRUE;
    }

  if (!dbus_connection_send (connection, message, &serial))
    {
      dbus_message_unref (message);
      return TRUE;
    }

  /* send our message */
1281
  bus_test_run_clients_loop (SEND_PENDING (connection));
1282 1283 1284 1285 1286

  dbus_message_unref (message);
  message = NULL;

  dbus_connection_ref (connection); /* because we may get disconnected */
1287
  block_connection_until_message_from_bus (context, connection, "reply to GetConnectionUnixUser");
1288 1289 1290

  if (!dbus_connection_get_is_connected (connection))
    {
1291
      _dbus_verbose ("connection was disconnected\n");
1292

1293
      dbus_connection_unref (connection);
1294

1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
      return TRUE;
    }

  dbus_connection_unref (connection);

  message = pop_message_waiting_for_memory (connection);
  if (message == NULL)
    {
      _dbus_warn ("Did not receive a reply to %s %d on %p\n",
                  "GetConnectionUnixUser", serial, connection);
      goto out;
    }

  verbose_message_received (connection, message);

  if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR)
    {
      if (dbus_message_is_error (message, DBUS_ERROR_NO_MEMORY))
        {
          ; /* good, this is a valid response */
        }
1316 1317 1318 1319 1320 1321
#ifdef DBUS_WIN
      else if (dbus_message_is_error (message, DBUS_ERROR_FAILED))
        {
          /* this is OK, Unix uids aren't meaningful on Windows */
        }
#endif
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337
      else
        {
          warn_unexpected (connection, message, "not this error");

          goto out;
        }
    }
  else
    {
      if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN)
        {
          ; /* good, expected */
        }
      else
        {
          warn_unexpected (connection, message,
1338
                           "method_return for GetConnectionUnixUser");
1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350

          goto out;
        }

    retry_get_property:

      if (!dbus_message_get_args (message, &error,
                                  DBUS_TYPE_UINT32, &uid,
                                  DBUS_TYPE_INVALID))
        {
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
            {
1351
              _dbus_verbose ("no memory to get uid by GetConnectionUnixUser\n");
1352 1353 1354 1355 1356 1357 1358
              dbus_error_free (&error);
              _dbus_wait_for_memory ();
              goto retry_get_property;
            }
          else
            {
              _dbus_assert (dbus_error_is_set (&error));
1359
              _dbus_warn ("Did not get the expected DBUS_TYPE_UINT32 from GetConnectionUnixUser\n");
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371
              goto out;
            }
        }
    }

  if (!check_no_leftovers (context))
    goto out;

  retval = TRUE;

 out:
  dbus_error_free (&error);
1372

1373 1374
  if (message)
    dbus_message_unref (message);
1375

1376