connection.c 27.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* -*- mode: C; c-file-style: "gnu" -*- */
/* connection.c  Client connections
 *
 * Copyright (C) 2003  Red Hat, Inc.
 *
 * Licensed under the Academic Free License version 1.2
 * 
 * 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.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "connection.h"
24
#include "dispatch.h"
25
#include "loop.h"
26
#include "policy.h"
27
#include "services.h"
28
#include "utils.h"
29 30
#include <dbus/dbus-list.h>

31 32
static void bus_connection_remove_transactions (DBusConnection *connection);

33 34 35 36 37 38 39 40
struct BusConnections
{
  int refcount;
  DBusList *list; /**< List of all the connections */
  BusContext *context;
};

static int connection_data_slot = -1;
41
static int connection_data_slot_refcount = 0;
42 43 44

typedef struct
{
45
  BusConnections *connections;
46
  DBusConnection *connection;
47
  DBusList *services_owned;
48
  char *name;
49 50 51
  DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
  DBusMessage *oom_message;
  DBusPreallocatedSend *oom_preallocated;
52 53 54
  unsigned long *group_ids;
  int n_group_ids;
  BusPolicy *policy;
55 56 57 58
} BusConnectionData;

#define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static dbus_bool_t
connection_data_slot_ref (void)
{
  if (connection_data_slot < 0)
    {
      connection_data_slot = dbus_connection_allocate_data_slot ();
      
      if (connection_data_slot < 0)
        return FALSE;

      _dbus_assert (connection_data_slot_refcount == 0);
    }  

  connection_data_slot_refcount += 1;

  return TRUE;

}

static void
connection_data_slot_unref (void)
{
  _dbus_assert (connection_data_slot_refcount > 0);

  connection_data_slot_refcount -= 1;
  
  if (connection_data_slot_refcount == 0)
    {
      dbus_connection_free_data_slot (connection_data_slot);
      connection_data_slot = -1;
    }
}

92 93 94 95 96 97 98 99 100 101
static BusLoop*
connection_get_loop (DBusConnection *connection)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);

  return bus_context_get_loop (d->connections->context);
}

102
void
103
bus_connection_disconnected (DBusConnection *connection)
104 105 106 107 108
{
  BusConnectionData *d;
  BusService *service;

  d = BUS_CONNECTION_DATA (connection);
109
  _dbus_assert (d != NULL);
110

111 112 113
  _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
                 d->name ? d->name : "(inactive)");
  
114 115 116 117 118 119 120 121 122 123
  /* Drop any service ownership. FIXME Unfortunately, this requires
   * memory allocation and there doesn't seem to be a good way to
   * handle it other than sleeping; we can't "fail" the operation of
   * disconnecting a client, and preallocating a broadcast "service is
   * now gone" message for every client-service pair seems kind of
   * involved. Probably we need to do that though, and also
   * extend BusTransaction to be able to revert generic
   * stuff, not just sending a message (so we can e.g. revert
   * removal of service owners).
   */
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  while ((service = _dbus_list_get_last (&d->services_owned)))
    {
      BusTransaction *transaction;
      DBusError error;

    retry:
      
      dbus_error_init (&error);
        
      transaction = NULL;
      while (transaction == NULL)
        {
          transaction = bus_transaction_new (d->connections->context);
          bus_wait_for_memory ();
        }
        
      if (!bus_service_remove_owner (service, connection,
                                     transaction, &error))
        {
          if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_error_free (&error);
              bus_transaction_cancel_and_free (transaction);
              bus_wait_for_memory ();
              goto retry;
            }
          else
            _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
        }
        
      bus_transaction_execute_and_free (transaction);
    }
156

157
  bus_dispatch_remove_connection (connection);
158
  
159
  /* no more watching */
160
  if (!dbus_connection_set_watch_functions (connection,
161
                                            NULL, NULL, NULL,
162 163 164 165 166
                                            connection,
                                            NULL))
    _dbus_assert_not_reached ("setting watch functions to NULL failed");

  if (!dbus_connection_set_timeout_functions (connection,
167
                                              NULL, NULL, NULL,
168 169 170 171
                                              connection,
                                              NULL))
    _dbus_assert_not_reached ("setting timeout functions to NULL failed");
  
172 173 174
  dbus_connection_set_unix_user_function (connection,
                                          NULL, NULL, NULL);
  
175
  bus_connection_remove_transactions (connection);
176 177 178 179

  _dbus_list_remove (&d->connections->list, connection);

  /* frees "d" as side effect */
180 181 182
  dbus_connection_set_data (connection,
                            connection_data_slot,
                            NULL, NULL);
183

184 185 186
  dbus_connection_unref (connection);
}

187
static dbus_bool_t
188 189 190 191 192
connection_watch_callback (DBusWatch     *watch,
                           unsigned int   condition,
                           void          *data)
{
  DBusConnection *connection = data;
193
  dbus_bool_t retval;
194

195 196
  dbus_connection_ref (connection);
  
197
  retval = dbus_connection_handle_watch (connection, watch, condition);
198

199 200
  bus_connection_dispatch_all_messages (connection);
  
201
  dbus_connection_unref (connection);
202 203

  return retval;
204 205
}

206
static dbus_bool_t
207
add_connection_watch (DBusWatch      *watch,
208
                      void           *data)
209
{
210 211 212 213
  DBusConnection *connection = data;

  return bus_loop_add_watch (connection_get_loop (connection),
                             watch, connection_watch_callback, connection,
214
                             NULL);
215 216 217 218
}

static void
remove_connection_watch (DBusWatch      *watch,
219
                         void           *data)
220
{
221 222 223 224
  DBusConnection *connection = data;
  
  bus_loop_remove_watch (connection_get_loop (connection),
                         watch, connection_watch_callback, connection);
225 226
}

227 228 229 230
static void
connection_timeout_callback (DBusTimeout   *timeout,
                             void          *data)
{
231 232 233
  DBusConnection *connection = data;

  dbus_connection_ref (connection);
234 235

  /* can return FALSE on OOM but we just let it fire again later */
236
  dbus_timeout_handle (timeout);
237

238 239
  bus_connection_dispatch_all_messages (connection);
  
240
  dbus_connection_unref (connection);
241 242 243 244
}

static dbus_bool_t
add_connection_timeout (DBusTimeout    *timeout,
245
                        void           *data)
246
{
247 248 249 250
  DBusConnection *connection = data;
  
  return bus_loop_add_timeout (connection_get_loop (connection),
                               timeout, connection_timeout_callback, connection, NULL);
251 252 253 254
}

static void
remove_connection_timeout (DBusTimeout    *timeout,
255
                           void           *data)
256
{
257 258 259 260
  DBusConnection *connection = data;
  
  bus_loop_remove_timeout (connection_get_loop (connection),
                           timeout, connection_timeout_callback, connection);
261 262
}

263 264 265 266 267 268 269 270 271 272
static dbus_bool_t
allow_user_function (DBusConnection *connection,
                     unsigned long   uid,
                     void           *data)
{
  BusConnectionData *d;
    
  d = BUS_CONNECTION_DATA (connection);

  _dbus_assert (d != NULL);
273 274

  return TRUE; /* FIXME - this is just until we can parse a config file */
275 276 277 278
  
  return bus_context_allow_user (d->connections->context, uid);
}

279 280 281 282 283 284 285
static void
free_connection_data (void *data)
{
  BusConnectionData *d = data;

  /* services_owned should be NULL since we should be disconnected */
  _dbus_assert (d->services_owned == NULL);
286 287
  /* similarly */
  _dbus_assert (d->transaction_messages == NULL);
288

289 290
  if (d->oom_preallocated)
    dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
291

292 293
  if (d->oom_message)
    dbus_message_unref (d->oom_message);
294 295 296 297 298

  if (d->policy)
    bus_policy_unref (d->policy);
  
  dbus_free (d->group_ids);
299
  
300
  dbus_free (d->name);
301 302 303 304
  
  dbus_free (d);
}

305 306
BusConnections*
bus_connections_new (BusContext *context)
307
{
308
  BusConnections *connections;
309

310 311
  if (!connection_data_slot_ref ())
    return NULL;
312

313 314
  connections = dbus_new0 (BusConnections, 1);
  if (connections == NULL)
315 316 317 318
    {
      connection_data_slot_unref ();
      return NULL;
    }
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  
  connections->refcount = 1;
  connections->context = context;
  
  return connections;
}

void
bus_connections_ref (BusConnections *connections)
{
  _dbus_assert (connections->refcount > 0);
  connections->refcount += 1;
}

void
bus_connections_unref (BusConnections *connections)
{
  _dbus_assert (connections->refcount > 0);
  connections->refcount -= 1;
  if (connections->refcount == 0)
    {
340 341 342 343 344 345 346 347 348 349 350
      while (connections->list != NULL)
        {
          DBusConnection *connection;

          connection = connections->list->data;

          dbus_connection_ref (connection);
          dbus_connection_disconnect (connection);
          bus_connection_disconnected (connection);
          dbus_connection_unref (connection);
        }
351 352 353
      
      _dbus_list_clear (&connections->list);
      
354 355 356
      dbus_free (connections);

      connection_data_slot_unref ();
357
    }
358 359 360
}

dbus_bool_t
361 362
bus_connections_setup_connection (BusConnections *connections,
                                  DBusConnection *connection)
363 364
{
  BusConnectionData *d;
365 366
  dbus_bool_t retval;
  
367 368
  d = dbus_new0 (BusConnectionData, 1);
  
369 370
  if (d == NULL)
    return FALSE;
371

372
  d->connections = connections;
373
  d->connection = connection;
374 375

  _dbus_assert (connection_data_slot >= 0);
376 377 378 379 380 381 382 383
  
  if (!dbus_connection_set_data (connection,
                                 connection_data_slot,
                                 d, free_connection_data))
    {
      dbus_free (d);
      return FALSE;
    }
384 385

  retval = FALSE;
386 387 388

  d->n_group_ids = 0;
  d->group_ids = NULL;
389
  
390
  if (!dbus_connection_set_watch_functions (connection,
391 392
                                            add_connection_watch,
                                            remove_connection_watch,
393
                                            NULL,
394 395
                                            connection,
                                            NULL))
396
    goto out;
397 398
  
  if (!dbus_connection_set_timeout_functions (connection,
399 400
                                              add_connection_timeout,
                                              remove_connection_timeout,
401
                                              NULL,
402
                                              connection, NULL))
403
    goto out;
404

405 406 407 408

  dbus_connection_set_unix_user_function (connection,
                                          allow_user_function,
                                          NULL, NULL);
409
  
410 411
  /* Setup the connection with the dispatcher */
  if (!bus_dispatch_add_connection (connection))
412 413 414
    goto out;
  
  if (!_dbus_list_append (&connections->list, connection))
415
    {
416 417
      bus_dispatch_remove_connection (connection);
      goto out;
418
    }
419
  
420
  dbus_connection_ref (connection);
421 422 423 424
  retval = TRUE;

 out:
  if (!retval)
425
    {        
426 427 428 429 430 431 432 433 434 435 436
      if (!dbus_connection_set_watch_functions (connection,
                                                NULL, NULL, NULL,
                                                connection,
                                                NULL))
        _dbus_assert_not_reached ("setting watch functions to NULL failed");
      
      if (!dbus_connection_set_timeout_functions (connection,
                                                  NULL, NULL, NULL,
                                                  connection,
                                                  NULL))
        _dbus_assert_not_reached ("setting timeout functions to NULL failed");
437 438 439

      dbus_connection_set_unix_user_function (connection,
                                              NULL, NULL, NULL);
440 441 442 443 444

      if (!dbus_connection_set_data (connection,
                                     connection_data_slot,
                                     NULL, NULL))
        _dbus_assert_not_reached ("failed to set connection data to null");
445
    }
446
  
447
  return retval;
448 449
}

450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
dbus_bool_t
bus_connection_get_groups  (DBusConnection       *connection,
                            const unsigned long **groups,
                            int                  *n_groups)
{
  BusConnectionData *d;
    
  d = BUS_CONNECTION_DATA (connection);

  _dbus_assert (d != NULL);

  *groups = NULL;
  *n_groups = 0;

  /* we do a lazy lookup on groups a user is in for two reasons:
   * 1) we can't do it on connection setup since the user
   * hasn't authenticated and 2) it might be expensive
   * and we don't need to do it if there are no group-based
   * rules in the config file
   */
  
  if (d->n_group_ids == 0)
    {
      unsigned long uid;
      
      if (dbus_connection_get_unix_user (connection, &uid))
        {
          if (!_dbus_get_groups (uid, &d->group_ids, &d->n_group_ids))
            {
              _dbus_verbose ("Did not get any groups for UID %lu\n",
                             uid);
              return FALSE;
            }
        }
    }

  *groups = d->group_ids;
  *n_groups = d->n_group_ids;

  return TRUE;
}

dbus_bool_t
bus_connection_is_in_group (DBusConnection *connection,
                            unsigned long   gid)
{
  int i;
  const unsigned long *group_ids;
  int n_group_ids;

  if (!bus_connection_get_groups (connection, &group_ids, &n_group_ids))
    return FALSE;

  i = 0;
  while (i < n_group_ids)
    {
      if (group_ids[i] == gid)
        return TRUE;
      ++i;
    }

  return FALSE;
}

BusPolicy*
bus_connection_get_policy (DBusConnection *connection)
{
  BusConnectionData *d;
    
  d = BUS_CONNECTION_DATA (connection);

  _dbus_assert (d != NULL);

  if (!dbus_connection_get_is_authenticated (connection))
    {
      _dbus_verbose ("Tried to get policy for unauthenticated connection!\n");
      return NULL;
    }
  
  /* We do lazy creation of the policy because
   * it can only be done post-authentication.
   */
  if (d->policy == NULL)
    {
      d->policy =
        bus_context_create_connection_policy (d->connections->context,
                                              connection);

      /* we may have a NULL policy on OOM or error getting list of
       * groups for a user. In the latter case we don't handle it so
       * well currently, just keep pretending we're out of memory,
       * which is kind of bizarre.
       */
    }

  return d->policy;
}
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

/**
 * Calls function on each connection; if the function returns
 * #FALSE, stops iterating.
 *
 * @param connections the connections object
 * @param function the function
 * @param data data to pass to it as a second arg
 */
void
bus_connections_foreach (BusConnections               *connections,
                         BusConnectionForeachFunction  function,
			void                          *data)
{
  DBusList *link;
  
  link = _dbus_list_get_first_link (&connections->list);
  while (link != NULL)
    {
      DBusConnection *connection = link->data;
      DBusList *next = _dbus_list_get_next_link (&connections->list, link);

      if (!(* function) (connection, data))
        break;
      
      link = next;
    }
}

BusContext*
bus_connections_get_context (BusConnections *connections)
{
  return connections->context;
}

BusContext*
bus_connection_get_context (DBusConnection *connection)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);

  _dbus_assert (d != NULL);

  return d->connections->context;
}

BusConnections*
bus_connection_get_connections (DBusConnection *connection)
{
  BusConnectionData *d;
    
  d = BUS_CONNECTION_DATA (connection);

  _dbus_assert (d != NULL);

  return d->connections;
}

BusRegistry*
bus_connection_get_registry (DBusConnection *connection)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);

  _dbus_assert (d != NULL);

  return bus_context_get_registry (d->connections->context);
}

BusActivation*
bus_connection_get_activation (DBusConnection *connection)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);

  _dbus_assert (d != NULL);

  return bus_context_get_activation (d->connections->context);
}

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
/**
 * Checks whether the connection is registered with the message bus.
 *
 * @param connection the connection
 * @returns #TRUE if we're an active message bus participant
 */
dbus_bool_t
bus_connection_is_active (DBusConnection *connection)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);
  
  return d != NULL && d->name != NULL;
}

dbus_bool_t
bus_connection_preallocate_oom_error (DBusConnection *connection)
{
  DBusMessage *message;
  DBusPreallocatedSend *preallocated;
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);  

  _dbus_assert (d != NULL);

  if (d->oom_preallocated != NULL)
    return TRUE;
  
  preallocated = dbus_connection_preallocate_send (connection);
  if (preallocated == NULL)
    return FALSE;

664 665
  /* d->name may be NULL, but that should be OK */
  message = dbus_message_new (d->name,
666 667 668 669 670 671 672
                              DBUS_ERROR_NO_MEMORY);
  if (message == NULL)
    {
      dbus_connection_free_preallocated_send (connection, preallocated);
      return FALSE;
    }

673
  dbus_message_set_is_error (message, TRUE);
674 675 676 677 678 679 680 681

  if (!dbus_message_set_sender (message,
                                DBUS_SERVICE_DBUS))
    {
      dbus_connection_free_preallocated_send (connection, preallocated);
      dbus_message_unref (message);
      return FALSE;
    }
682
  
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  /* set reply serial to placeholder value just so space is already allocated
   * for it.
   */
  if (!dbus_message_set_reply_serial (message, 14))
    {
      dbus_connection_free_preallocated_send (connection, preallocated);
      dbus_message_unref (message);
      return FALSE;
    }

  d->oom_message = message;
  d->oom_preallocated = preallocated;
  
  return TRUE;
}

void
bus_connection_send_oom_error (DBusConnection *connection,
                               DBusMessage    *in_reply_to)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);  

  _dbus_assert (d != NULL);  
  _dbus_assert (d->oom_message != NULL);

  /* should always succeed since we set it to a placeholder earlier */
  if (!dbus_message_set_reply_serial (d->oom_message,
                                      dbus_message_get_serial (in_reply_to)))
    _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");

715 716
  _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
  
717 718 719 720 721 722 723 724
  dbus_connection_send_preallocated (connection, d->oom_preallocated,
                                     d->oom_message, NULL);

  dbus_message_unref (d->oom_message);
  d->oom_message = NULL;
  d->oom_preallocated = NULL;
}

725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
dbus_bool_t
bus_connection_add_owned_service (DBusConnection *connection,
                                  BusService     *service)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);

  if (!_dbus_list_append (&d->services_owned,
                          service))
    return FALSE;

  return TRUE;
}

void
bus_connection_remove_owned_service (DBusConnection *connection,
                                     BusService     *service)
{
  BusConnectionData *d;

  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);

  _dbus_list_remove_last (&d->services_owned, service);
}
752 753 754 755 756 757 758 759 760 761 762

dbus_bool_t
bus_connection_set_name (DBusConnection   *connection,
			 const DBusString *name)
{
  BusConnectionData *d;
  
  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);
  _dbus_assert (d->name == NULL);

763
  if (!_dbus_string_copy_data (name, &d->name))
764 765
    return FALSE;

766 767
  _dbus_assert (d->name != NULL);
  
768 769
  _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
  
770 771 772 773 774 775 776 777 778 779 780 781 782
  return TRUE;
}

const char *
bus_connection_get_name (DBusConnection *connection)
{
  BusConnectionData *d;
  
  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);
  
  return d->name;
}
783

784 785 786 787 788 789 790 791 792 793
typedef struct
{
  BusTransaction *transaction;
  DBusMessage    *message;
  DBusPreallocatedSend *preallocated;
} MessageToSend;

struct BusTransaction
{
  DBusList *connections;
794
  BusContext *context;
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
};

static void
message_to_send_free (DBusConnection *connection,
                      MessageToSend  *to_send)
{
  if (to_send->message)
    dbus_message_unref (to_send->message);

  if (to_send->preallocated)
    dbus_connection_free_preallocated_send (connection, to_send->preallocated);

  dbus_free (to_send);
}

BusTransaction*
811
bus_transaction_new (BusContext *context)
812 813 814 815 816 817 818
{
  BusTransaction *transaction;

  transaction = dbus_new0 (BusTransaction, 1);
  if (transaction == NULL)
    return NULL;

819 820
  transaction->context = context;
  
821 822 823
  return transaction;
}

824 825 826 827 828 829 830 831 832 833 834 835
BusContext*
bus_transaction_get_context (BusTransaction  *transaction)
{
  return transaction->context;
}

BusConnections*
bus_transaction_get_connections (BusTransaction  *transaction)
{
  return bus_context_get_connections (transaction->context);
}

836 837 838 839 840 841 842 843 844
dbus_bool_t
bus_transaction_send_message (BusTransaction *transaction,
                              DBusConnection *connection,
                              DBusMessage    *message)
{
  MessageToSend *to_send;
  BusConnectionData *d;
  DBusList *link;

845 846 847 848
  _dbus_verbose ("  trying to add message %s to transaction%s\n",
                 dbus_message_get_name (message),
                 dbus_connection_get_is_connected (connection) ?
                 "" : " (disconnected)");
849 850

  _dbus_assert (dbus_message_get_sender (message) != NULL);
851
  
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
  if (!dbus_connection_get_is_connected (connection))
    return TRUE; /* silently ignore disconnected connections */
  
  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);
  
  to_send = dbus_new (MessageToSend, 1);
  if (to_send == NULL)
    {
      return FALSE;
    }

  to_send->preallocated = dbus_connection_preallocate_send (connection);
  if (to_send->preallocated == NULL)
    {
      dbus_free (to_send);
      return FALSE;
    }  
  
  dbus_message_ref (message);
  to_send->message = message;
  to_send->transaction = transaction;

875 876
  _dbus_verbose ("about to prepend message\n");
  
877 878 879 880 881
  if (!_dbus_list_prepend (&d->transaction_messages, to_send))
    {
      message_to_send_free (connection, to_send);
      return FALSE;
    }
882 883

  _dbus_verbose ("prepended message\n");
884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
  
  /* See if we already had this connection in the list
   * for this transaction. If we have a pending message,
   * then we should already be in transaction->connections
   */
  link = _dbus_list_get_first_link (&d->transaction_messages);
  _dbus_assert (link->data == to_send);
  link = _dbus_list_get_next_link (&d->transaction_messages, link);
  while (link != NULL)
    {
      MessageToSend *m = link->data;
      DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
      
      if (m->transaction == transaction)
        break;
        
      link = next;
    }

  if (link == NULL)
    {
      if (!_dbus_list_prepend (&transaction->connections, connection))
        {
          _dbus_list_remove (&d->transaction_messages, to_send);
          message_to_send_free (connection, to_send);
          return FALSE;
        }
    }

  return TRUE;
}

static void
connection_cancel_transaction (DBusConnection *connection,
                               BusTransaction *transaction)
{
  DBusList *link;
  BusConnectionData *d;
  
  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);
  
  link = _dbus_list_get_first_link (&d->transaction_messages);
  while (link != NULL)
    {
      MessageToSend *m = link->data;
      DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
      
      if (m->transaction == transaction)
        {
          _dbus_list_remove_link (&d->transaction_messages,
                                  link);
          
          message_to_send_free (connection, m);
        }
        
      link = next;
    }
}

void
bus_transaction_cancel_and_free (BusTransaction *transaction)
{
  DBusConnection *connection;
948 949

  _dbus_verbose ("TRANSACTION: cancelled\n");
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980
  
  while ((connection = _dbus_list_pop_first (&transaction->connections)))
    connection_cancel_transaction (connection, transaction);

  _dbus_assert (transaction->connections == NULL);

  dbus_free (transaction);
}

static void
connection_execute_transaction (DBusConnection *connection,
                                BusTransaction *transaction)
{
  DBusList *link;
  BusConnectionData *d;
  
  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);

  /* Send the queue in order (FIFO) */
  link = _dbus_list_get_last_link (&d->transaction_messages);
  while (link != NULL)
    {
      MessageToSend *m = link->data;
      DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
      
      if (m->transaction == transaction)
        {
          _dbus_list_remove_link (&d->transaction_messages,
                                  link);

981 982
          _dbus_assert (dbus_message_get_sender (m->message) != NULL);
          
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
          dbus_connection_send_preallocated (connection,
                                             m->preallocated,
                                             m->message,
                                             NULL);

          m->preallocated = NULL; /* so we don't double-free it */
          
          message_to_send_free (connection, m);
        }
        
      link = prev;
    }
}

void
bus_transaction_execute_and_free (BusTransaction *transaction)
{
  /* For each connection in transaction->connections
   * send the messages
   */
  DBusConnection *connection;
1004 1005

  _dbus_verbose ("TRANSACTION: executing\n");
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  
  while ((connection = _dbus_list_pop_first (&transaction->connections)))
    connection_execute_transaction (connection, transaction);

  _dbus_assert (transaction->connections == NULL);

  dbus_free (transaction);
}

static void
bus_connection_remove_transactions (DBusConnection *connection)
{
  MessageToSend *to_send;
  BusConnectionData *d;
  
  d = BUS_CONNECTION_DATA (connection);
  _dbus_assert (d != NULL);
  
  while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
    {
      /* only has an effect for the first MessageToSend listing this transaction */
      _dbus_list_remove (&to_send->transaction->connections,
                         connection);

      _dbus_list_remove (&d->transaction_messages, to_send);
      message_to_send_free (connection, to_send);
    }
}

/**
 * Converts the DBusError to a message reply
 */
dbus_bool_t
bus_transaction_send_error_reply (BusTransaction  *transaction,
                                  DBusConnection  *connection,
                                  const DBusError *error,
                                  DBusMessage     *in_reply_to)
{
  DBusMessage *reply;
1045
  
1046 1047 1048 1049 1050 1051 1052 1053 1054
  _dbus_assert (error != NULL);
  _DBUS_ASSERT_ERROR_IS_SET (error);
  
  reply = dbus_message_new_error_reply (in_reply_to,
                                        error->name,
                                        error->message);
  if (reply == NULL)
    return FALSE;

1055 1056
  if (!dbus_message_set_sender (reply, DBUS_SERVICE_DBUS) ||
      !bus_transaction_send_message (transaction, connection, reply))
1057 1058 1059 1060 1061
    {
      dbus_message_unref (reply);
      return FALSE;
    }

1062 1063
  dbus_message_unref (reply);
  
1064
  return TRUE;
1065
}