534ef6faf765dac70c3f99be6cdd6112ad542ca7
[platform/upstream/dbus.git] / bus / connection.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* connection.c  Client connections
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "connection.h"
26 #include "dispatch.h"
27 #include "policy.h"
28 #include "services.h"
29 #include "utils.h"
30 #include "signals.h"
31 #include "expirelist.h"
32 #include "selinux.h"
33 #include <dbus/dbus-list.h>
34 #include <dbus/dbus-hash.h>
35 #include <dbus/dbus-timeout.h>
36
37 /* Trim executed commands to this length; we want to keep logs readable */
38 #define MAX_LOG_COMMAND_LEN 50
39
40 static void bus_connection_remove_transactions (DBusConnection *connection);
41
42 typedef struct
43 {
44   BusExpireItem expire_item;
45
46   DBusConnection *will_get_reply;
47   DBusConnection *will_send_reply;
48
49   dbus_uint32_t reply_serial;
50   
51 } BusPendingReply;
52
53 struct BusConnections
54 {
55   int refcount;
56   DBusList *completed;  /**< List of all completed connections */
57   int n_completed;      /**< Length of completed list */
58   DBusList *incomplete; /**< List of all not-yet-active connections */
59   int n_incomplete;     /**< Length of incomplete list */
60   BusContext *context;
61   DBusHashTable *completed_by_user; /**< Number of completed connections for each UID */
62   DBusTimeout *expire_timeout; /**< Timeout for expiring incomplete connections. */
63   int stamp;                   /**< Incrementing number */
64   BusExpireList *pending_replies; /**< List of pending replies */
65
66 #ifdef DBUS_ENABLE_STATS
67   int total_match_rules;
68   int peak_match_rules;
69   int peak_match_rules_per_conn;
70
71   int total_bus_names;
72   int peak_bus_names;
73   int peak_bus_names_per_conn;
74 #endif
75 };
76
77 static dbus_int32_t connection_data_slot = -1;
78
79 typedef struct
80 {
81   BusConnections *connections;
82   DBusList *link_in_connection_list;
83   DBusConnection *connection;
84   DBusList *services_owned;
85   int n_services_owned;
86   DBusList *match_rules;
87   int n_match_rules;
88   char *name;
89   DBusList *transaction_messages; /**< Stuff we need to send as part of a transaction */
90   DBusMessage *oom_message;
91   DBusPreallocatedSend *oom_preallocated;
92   BusClientPolicy *policy;
93
94   char *cached_loginfo_string;
95   BusSELinuxID *selinux_id;
96
97   long connection_tv_sec;  /**< Time when we connected (seconds component) */
98   long connection_tv_usec; /**< Time when we connected (microsec component) */
99   int stamp;               /**< connections->stamp last time we were traversed */
100
101 #ifdef DBUS_ENABLE_STATS
102   int peak_match_rules;
103   int peak_bus_names;
104 #endif
105 } BusConnectionData;
106
107 static dbus_bool_t bus_pending_reply_expired (BusExpireList *list,
108                                               DBusList      *link,
109                                               void          *data);
110
111 static void bus_connection_drop_pending_replies (BusConnections  *connections,
112                                                  DBusConnection  *connection);
113
114 static dbus_bool_t expire_incomplete_timeout (void *data);
115
116 #define BUS_CONNECTION_DATA(connection) (dbus_connection_get_data ((connection), connection_data_slot))
117
118 static DBusLoop*
119 connection_get_loop (DBusConnection *connection)
120 {
121   BusConnectionData *d;
122
123   d = BUS_CONNECTION_DATA (connection);
124
125   return bus_context_get_loop (d->connections->context);
126 }
127
128
129 static int
130 get_connections_for_uid (BusConnections *connections,
131                          dbus_uid_t      uid)
132 {
133   void *val;
134   int current_count;
135
136   /* val is NULL is 0 when it isn't in the hash yet */
137   
138   val = _dbus_hash_table_lookup_uintptr (connections->completed_by_user,
139                                        uid);
140
141   current_count = _DBUS_POINTER_TO_INT (val);
142
143   return current_count;
144 }
145
146 static dbus_bool_t
147 adjust_connections_for_uid (BusConnections *connections,
148                             dbus_uid_t      uid,
149                             int             adjustment)
150 {
151   int current_count;
152
153   current_count = get_connections_for_uid (connections, uid);
154
155   _dbus_verbose ("Adjusting connection count for UID " DBUS_UID_FORMAT
156                  ": was %d adjustment %d making %d\n",
157                  uid, current_count, adjustment, current_count + adjustment);
158   
159   _dbus_assert (current_count >= 0);
160   
161   current_count += adjustment;
162
163   _dbus_assert (current_count >= 0);
164
165   if (current_count == 0)
166     {
167       _dbus_hash_table_remove_uintptr (connections->completed_by_user, uid);
168       return TRUE;
169     }
170   else
171     {
172       dbus_bool_t retval;
173       
174       retval = _dbus_hash_table_insert_uintptr (connections->completed_by_user,
175                                               uid, _DBUS_INT_TO_POINTER (current_count));
176
177       /* only positive adjustment can fail as otherwise
178        * a hash entry should already exist
179        */
180       _dbus_assert (adjustment > 0 ||
181                     (adjustment <= 0 && retval));
182
183       return retval;
184     }
185 }
186
187 void
188 bus_connection_disconnected (DBusConnection *connection)
189 {
190   BusConnectionData *d;
191   BusService *service;
192   BusMatchmaker *matchmaker;
193   dbus_bool_t is_phantom = FALSE;
194   
195   d = BUS_CONNECTION_DATA (connection);
196   _dbus_assert (d != NULL);
197
198   _dbus_verbose ("%s disconnected, dropping all service ownership and releasing\n",
199                  d->name ? d->name : "(inactive)");
200
201   if(bus_context_is_kdbus(d->connections->context) && (strcmp(bus_connection_get_name(connection), ":1.1")))
202       is_phantom = TRUE;
203
204   /* Delete our match rules */
205   if (d->n_match_rules > 0)
206     {
207       matchmaker = bus_context_get_matchmaker (d->connections->context);
208       bus_matchmaker_disconnected (matchmaker, connection);
209     }
210   
211   /* Drop any service ownership. Unfortunately, this requires
212    * memory allocation and there doesn't seem to be a good way to
213    * handle it other than sleeping; we can't "fail" the operation of
214    * disconnecting a client, and preallocating a broadcast "service is
215    * now gone" message for every client-service pair seems kind of
216    * involved.
217    */
218   while ((service = _dbus_list_get_last (&d->services_owned)))
219     {
220       BusTransaction *transaction;
221       DBusError error;
222
223     retry:
224       
225       dbus_error_init (&error);
226         
227       while ((transaction = bus_transaction_new (d->connections->context)) == NULL)
228         _dbus_wait_for_memory ();
229         
230       if (!bus_service_remove_owner (service, connection,
231                                      transaction, &error))
232         {
233           _DBUS_ASSERT_ERROR_IS_SET (&error);
234           
235           if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
236             {
237               dbus_error_free (&error);
238               bus_transaction_cancel_and_free (transaction);
239               _dbus_wait_for_memory ();
240               goto retry;
241             }
242           else
243             {
244               _dbus_verbose ("Failed to remove service owner: %s %s\n",
245                              error.name, error.message);
246               _dbus_assert_not_reached ("Removing service owner failed for non-memory-related reason");
247             }
248         }
249         
250       bus_transaction_execute_and_free (transaction);
251     }
252
253   bus_dispatch_remove_connection (connection);
254   
255   /* no more watching */
256   if (!dbus_connection_set_watch_functions (connection,
257                                             NULL, NULL, NULL,
258                                             connection,
259                                             NULL))
260     _dbus_assert_not_reached ("setting watch functions to NULL failed");
261
262   if (!dbus_connection_set_timeout_functions (connection,
263                                               NULL, NULL, NULL,
264                                               connection,
265                                               NULL))
266     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
267   
268   dbus_connection_set_unix_user_function (connection,
269                                           NULL, NULL, NULL);
270   dbus_connection_set_windows_user_function (connection,
271                                              NULL, NULL, NULL);
272   
273   dbus_connection_set_dispatch_status_function (connection,
274                                                 NULL, NULL, NULL);
275   
276   bus_connection_remove_transactions (connection);
277
278   if (d->link_in_connection_list != NULL)
279     {
280       if (d->name != NULL)
281         {
282           unsigned long uid;
283           
284           _dbus_list_remove_link (&d->connections->completed, d->link_in_connection_list);
285           d->link_in_connection_list = NULL;
286           d->connections->n_completed -= 1;
287
288           if (dbus_connection_get_unix_user (connection, &uid))
289             {
290               if (!adjust_connections_for_uid (d->connections,
291                                                uid, -1))
292                 _dbus_assert_not_reached ("adjusting downward should never fail");
293             }
294         }
295       else
296         {
297           _dbus_list_remove_link (&d->connections->incomplete, d->link_in_connection_list);
298           d->link_in_connection_list = NULL;
299           d->connections->n_incomplete -= 1;
300         }
301       
302       _dbus_assert (d->connections->n_incomplete >= 0);
303       _dbus_assert (d->connections->n_completed >= 0);
304     }
305
306   bus_connection_drop_pending_replies (d->connections, connection);
307   
308   /* frees "d" as side effect */
309   dbus_connection_set_data (connection,
310                             connection_data_slot,
311                             NULL, NULL);
312   if(is_phantom)
313       dbus_connection_unref_phantom(connection);
314   else
315       dbus_connection_unref (connection);
316 }
317
318 static dbus_bool_t
319 add_connection_watch (DBusWatch      *watch,
320                       void           *data)
321 {
322   DBusConnection *connection = data;
323
324   return _dbus_loop_add_watch (connection_get_loop (connection), watch);
325 }
326
327 static void
328 remove_connection_watch (DBusWatch      *watch,
329                          void           *data)
330 {
331   DBusConnection *connection = data;
332   
333   _dbus_loop_remove_watch (connection_get_loop (connection), watch);
334 }
335
336 static void
337 toggle_connection_watch (DBusWatch      *watch,
338                          void           *data)
339 {
340   DBusConnection *connection = data;
341
342   _dbus_loop_toggle_watch (connection_get_loop (connection), watch);
343 }
344
345 static dbus_bool_t
346 add_connection_timeout (DBusTimeout    *timeout,
347                         void           *data)
348 {
349   DBusConnection *connection = data;
350   
351   return _dbus_loop_add_timeout (connection_get_loop (connection), timeout);
352 }
353
354 static void
355 remove_connection_timeout (DBusTimeout    *timeout,
356                            void           *data)
357 {
358   DBusConnection *connection = data;
359   
360   _dbus_loop_remove_timeout (connection_get_loop (connection), timeout);
361 }
362
363 static void
364 dispatch_status_function (DBusConnection    *connection,
365                           DBusDispatchStatus new_status,
366                           void              *data)
367 {
368   DBusLoop *loop = data;
369   
370   if (new_status != DBUS_DISPATCH_COMPLETE)
371     {
372       while (!_dbus_loop_queue_dispatch (loop, connection))
373         _dbus_wait_for_memory ();
374     }
375 }
376
377 static dbus_bool_t
378 allow_unix_user_function (DBusConnection *connection,
379                           unsigned long   uid,
380                           void           *data)
381 {
382   BusConnectionData *d;
383     
384   d = BUS_CONNECTION_DATA (connection);
385
386   _dbus_assert (d != NULL);
387   
388   return bus_context_allow_unix_user (d->connections->context, uid);
389 }
390
391 static void
392 free_connection_data (void *data)
393 {
394   BusConnectionData *d = data;
395
396   /* services_owned should be NULL since we should be disconnected */
397   _dbus_assert (d->services_owned == NULL);
398   _dbus_assert (d->n_services_owned == 0);
399   /* similarly */
400   _dbus_assert (d->transaction_messages == NULL);
401
402   if (d->oom_preallocated)
403     dbus_connection_free_preallocated_send (d->connection, d->oom_preallocated);
404
405   if (d->oom_message)
406     dbus_message_unref (d->oom_message);
407
408   if (d->policy)
409     bus_client_policy_unref (d->policy);
410
411   if (d->selinux_id)
412     bus_selinux_id_unref (d->selinux_id);
413   
414   dbus_free (d->cached_loginfo_string);
415   
416   dbus_free (d->name);
417   
418   dbus_free (d);
419 }
420
421 BusConnections*
422 bus_connections_new (BusContext *context)
423 {
424   BusConnections *connections;
425
426   if (!dbus_connection_allocate_data_slot (&connection_data_slot))
427     goto failed_0;
428
429   connections = dbus_new0 (BusConnections, 1);
430   if (connections == NULL)
431     goto failed_1;
432
433   connections->completed_by_user = _dbus_hash_table_new (DBUS_HASH_UINTPTR,
434                                                          NULL, NULL);
435   if (connections->completed_by_user == NULL)
436     goto failed_2;
437
438   connections->expire_timeout = _dbus_timeout_new (100, /* irrelevant */
439                                                    expire_incomplete_timeout,
440                                                    connections, NULL);
441   if (connections->expire_timeout == NULL)
442     goto failed_3;
443
444   _dbus_timeout_set_enabled (connections->expire_timeout, FALSE);
445
446   connections->pending_replies = bus_expire_list_new (bus_context_get_loop (context),
447                                                       bus_context_get_reply_timeout (context),
448                                                       bus_pending_reply_expired,
449                                                       connections);
450   if (connections->pending_replies == NULL)
451     goto failed_4;
452   
453   if (!_dbus_loop_add_timeout (bus_context_get_loop (context),
454                                connections->expire_timeout))
455     goto failed_5;
456   
457   connections->refcount = 1;
458   connections->context = context;
459   
460   return connections;
461
462  failed_5:
463   bus_expire_list_free (connections->pending_replies);
464  failed_4:
465   _dbus_timeout_unref (connections->expire_timeout);
466  failed_3:
467   _dbus_hash_table_unref (connections->completed_by_user);
468  failed_2:
469   dbus_free (connections);
470  failed_1:
471   dbus_connection_free_data_slot (&connection_data_slot);
472  failed_0:
473   return NULL;
474 }
475
476 BusConnections *
477 bus_connections_ref (BusConnections *connections)
478 {
479   _dbus_assert (connections->refcount > 0);
480   connections->refcount += 1;
481
482   return connections;
483 }
484
485 void
486 bus_connections_unref (BusConnections *connections)
487 {
488   _dbus_assert (connections->refcount > 0);
489   connections->refcount -= 1;
490   if (connections->refcount == 0)
491     {
492       /* drop all incomplete */
493       while (connections->incomplete != NULL)
494         {
495           DBusConnection *connection;
496
497           connection = connections->incomplete->data;
498
499           dbus_connection_ref (connection);
500           dbus_connection_close (connection);
501           bus_connection_disconnected (connection);
502           dbus_connection_unref (connection);
503         }
504
505       _dbus_assert (connections->n_incomplete == 0);
506       
507       /* drop all real connections */
508       while (connections->completed != NULL)
509         {
510           DBusConnection *connection;
511
512           connection = connections->completed->data;
513
514           dbus_connection_ref (connection);
515           dbus_connection_close (connection);
516           bus_connection_disconnected (connection);
517           dbus_connection_unref (connection);
518         }
519
520       _dbus_assert (connections->n_completed == 0);
521
522       bus_expire_list_free (connections->pending_replies);
523       
524       _dbus_loop_remove_timeout (bus_context_get_loop (connections->context),
525                                  connections->expire_timeout);
526       
527       _dbus_timeout_unref (connections->expire_timeout);
528       
529       _dbus_hash_table_unref (connections->completed_by_user);
530       
531       dbus_free (connections);
532
533       dbus_connection_free_data_slot (&connection_data_slot);
534     }
535 }
536
537 /* Used for logging */
538 static dbus_bool_t
539 cache_peer_loginfo_string (BusConnectionData *d, 
540                            DBusConnection    *connection)
541 {
542   DBusString loginfo_buf;
543   unsigned long uid;
544   unsigned long pid;
545   char *windows_sid;
546   dbus_bool_t prev_added;
547
548   if (!_dbus_string_init (&loginfo_buf))
549     return FALSE;
550   
551   prev_added = FALSE;
552   if (dbus_connection_get_unix_user (connection, &uid))
553     {
554       if (!_dbus_string_append_printf (&loginfo_buf, "uid=%ld", uid))
555         goto oom;
556       else
557         prev_added = TRUE;
558     }
559
560   if (dbus_connection_get_unix_process_id (connection, &pid))
561     {
562       if (prev_added)
563         {
564           if (!_dbus_string_append_byte (&loginfo_buf, ' '))
565             goto oom;
566         }
567       if (!_dbus_string_append_printf (&loginfo_buf, "pid=%ld comm=\"", pid))
568         goto oom;
569       /* Ignore errors here; we may not have permissions to read the
570        * proc file. */
571       _dbus_command_for_pid (pid, &loginfo_buf, MAX_LOG_COMMAND_LEN, NULL);
572       if (!_dbus_string_append_byte (&loginfo_buf, '"'))
573         goto oom;
574     }
575
576   if (dbus_connection_get_windows_user (connection, &windows_sid))
577     {
578       dbus_bool_t did_append;
579       did_append = _dbus_string_append_printf (&loginfo_buf,
580                                                "sid=\"%s\" ", windows_sid);
581       dbus_free (windows_sid);
582       if (!did_append)
583         goto oom;
584     }
585
586   if (!_dbus_string_steal_data (&loginfo_buf, &(d->cached_loginfo_string)))
587     goto oom;
588
589   _dbus_string_free (&loginfo_buf); 
590
591   return TRUE;
592 oom:
593    _dbus_string_free (&loginfo_buf);
594    return FALSE;
595 }
596
597 dbus_bool_t
598 bus_connections_setup_connection (BusConnections *connections,
599                                   DBusConnection *connection)
600 {
601
602   BusConnectionData *d;
603   dbus_bool_t retval;
604   DBusError error;
605
606   
607   d = dbus_new0 (BusConnectionData, 1);
608   
609   if (d == NULL)
610     return FALSE;
611
612   d->connections = connections;
613   d->connection = connection;
614   
615   _dbus_get_monotonic_time (&d->connection_tv_sec,
616                             &d->connection_tv_usec);
617   
618   _dbus_assert (connection_data_slot >= 0);
619   
620   if (!dbus_connection_set_data (connection,
621                                  connection_data_slot,
622                                  d, free_connection_data))
623     {
624       dbus_free (d);
625       return FALSE;
626     }
627
628   dbus_connection_set_route_peer_messages (connection, TRUE);
629   
630   retval = FALSE;
631
632   dbus_error_init (&error);
633   d->selinux_id = bus_selinux_init_connection_id (connection,
634                                                   &error);
635   if (dbus_error_is_set (&error))
636     {
637       /* This is a bit bogus because we pretend all errors
638        * are OOM; this is done because we know that in bus.c
639        * an OOM error disconnects the connection, which is
640        * the same thing we want on any other error.
641        */
642       dbus_error_free (&error);
643       goto out;
644     }
645
646   if (!dbus_connection_set_watch_functions (connection,
647                                             add_connection_watch,
648                                             remove_connection_watch,
649                                             toggle_connection_watch,
650                                             connection,
651                                             NULL))
652     goto out;
653   
654   if (!dbus_connection_set_timeout_functions (connection,
655                                               add_connection_timeout,
656                                               remove_connection_timeout,
657                                               NULL,
658                                               connection, NULL))
659     goto out;
660
661   /* For now we don't need to set a Windows user function because
662    * there are no policies in the config file controlling what
663    * Windows users can connect. The default 'same user that owns the
664    * bus can connect' behavior of DBusConnection is fine on Windows.
665    */
666   dbus_connection_set_unix_user_function (connection,
667                                           allow_unix_user_function,
668                                           NULL, NULL);
669
670   dbus_connection_set_dispatch_status_function (connection,
671                                                 dispatch_status_function,
672                                                 bus_context_get_loop (connections->context),
673                                                 NULL);
674
675   d->link_in_connection_list = _dbus_list_alloc_link (connection);
676   if (d->link_in_connection_list == NULL)
677     goto out;
678   
679   /* Setup the connection with the dispatcher */
680   if (!bus_dispatch_add_connection (connection))
681     goto out;
682
683   if (dbus_connection_get_dispatch_status (connection) != DBUS_DISPATCH_COMPLETE)
684     {
685       if (!_dbus_loop_queue_dispatch (bus_context_get_loop (connections->context), connection))
686         {
687           bus_dispatch_remove_connection (connection);
688           goto out;
689         }
690     }
691
692   _dbus_list_append_link (&connections->incomplete, d->link_in_connection_list);
693   connections->n_incomplete += 1;
694   
695   dbus_connection_ref (connection);
696
697   /* Note that we might disconnect ourselves here, but it only takes
698    * effect on return to the main loop. We call this to free up
699    * expired connections if possible, and to queue the timeout for our
700    * own expiration.
701    */
702   bus_connections_expire_incomplete (connections);
703   
704   /* And we might also disconnect ourselves here, but again it
705    * only takes effect on return to main loop.
706    */
707   if (connections->n_incomplete >
708       bus_context_get_max_incomplete_connections (connections->context))
709     {
710       _dbus_verbose ("Number of incomplete connections exceeds max, dropping oldest one\n");
711       
712       _dbus_assert (connections->incomplete != NULL);
713       /* Disconnect the oldest unauthenticated connection.  FIXME
714        * would it be more secure to drop a *random* connection?  This
715        * algorithm seems to mean that if someone can create new
716        * connections quickly enough, they can keep anyone else from
717        * completing authentication. But random may or may not really
718        * help with that, a more elaborate solution might be required.
719        */
720       dbus_connection_close (connections->incomplete->data);
721     }
722   
723   retval = TRUE;
724
725  out:
726   if (!retval)
727     {
728       if (d->selinux_id)
729         bus_selinux_id_unref (d->selinux_id);
730       d->selinux_id = NULL;
731       
732       if (!dbus_connection_set_watch_functions (connection,
733                                                 NULL, NULL, NULL,
734                                                 connection,
735                                                 NULL))
736         _dbus_assert_not_reached ("setting watch functions to NULL failed");
737       
738       if (!dbus_connection_set_timeout_functions (connection,
739                                                   NULL, NULL, NULL,
740                                                   connection,
741                                                   NULL))
742         _dbus_assert_not_reached ("setting timeout functions to NULL failed");
743
744       dbus_connection_set_unix_user_function (connection,
745                                               NULL, NULL, NULL);
746
747       dbus_connection_set_windows_user_function (connection,
748                                                  NULL, NULL, NULL);
749       
750       dbus_connection_set_dispatch_status_function (connection,
751                                                     NULL, NULL, NULL);
752
753       if (d->link_in_connection_list != NULL)
754         {
755           _dbus_assert (d->link_in_connection_list->next == NULL);
756           _dbus_assert (d->link_in_connection_list->prev == NULL);
757           _dbus_list_free_link (d->link_in_connection_list);
758           d->link_in_connection_list = NULL;
759         }
760       
761       if (!dbus_connection_set_data (connection,
762                                      connection_data_slot,
763                                      NULL, NULL))
764         _dbus_assert_not_reached ("failed to set connection data to null");
765
766       /* "d" has now been freed */
767     }
768   
769   return retval;
770 }
771
772 void
773 bus_connections_expire_incomplete (BusConnections *connections)
774 {    
775   int next_interval;
776
777   next_interval = -1;
778   
779   if (connections->incomplete != NULL)
780     {
781       long tv_sec, tv_usec;
782       DBusList *link;
783       int auth_timeout;
784       
785       _dbus_get_monotonic_time (&tv_sec, &tv_usec);
786       auth_timeout = bus_context_get_auth_timeout (connections->context);
787   
788       link = _dbus_list_get_first_link (&connections->incomplete);
789       while (link != NULL)
790         {
791           DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
792           DBusConnection *connection;
793           BusConnectionData *d;
794           double elapsed;
795       
796           connection = link->data;
797       
798           d = BUS_CONNECTION_DATA (connection);
799       
800           _dbus_assert (d != NULL);
801       
802           elapsed = ELAPSED_MILLISECONDS_SINCE (d->connection_tv_sec,
803                                                 d->connection_tv_usec,
804                                                 tv_sec, tv_usec);
805
806           if (elapsed >= (double) auth_timeout)
807             {
808               _dbus_verbose ("Timing out authentication for connection %p\n", connection);
809               dbus_connection_close (connection);
810             }
811           else
812             {
813               /* We can end the loop, since the connections are in oldest-first order */
814               next_interval = ((double)auth_timeout) - elapsed;
815               _dbus_verbose ("Connection %p authentication expires in %d milliseconds\n",
816                              connection, next_interval);
817           
818               break;
819             }
820       
821           link = next;
822         }
823     }
824
825   bus_expire_timeout_set_interval (connections->expire_timeout,
826                                    next_interval);
827 }
828
829 static dbus_bool_t
830 expire_incomplete_timeout (void *data)
831 {
832   BusConnections *connections = data;
833
834   _dbus_verbose ("Running\n");
835   
836   /* note that this may remove the timeout */
837   bus_connections_expire_incomplete (connections);
838
839   return TRUE;
840 }
841
842 dbus_bool_t
843 bus_connection_get_unix_groups  (DBusConnection   *connection,
844                                  unsigned long   **groups,
845                                  int              *n_groups,
846                                  DBusError        *error)
847 {
848   unsigned long uid;
849
850   *groups = NULL;
851   *n_groups = 0;
852
853   if (dbus_connection_get_unix_user (connection, &uid))
854     {
855       if (!_dbus_unix_groups_from_uid (uid, groups, n_groups))
856         {
857           _dbus_verbose ("Did not get any groups for UID %lu\n",
858                          uid);
859           return FALSE;
860         }
861       else
862         {
863           _dbus_verbose ("Got %d groups for UID %lu\n",
864                          *n_groups, uid);
865           return TRUE;
866         }
867     }
868   else
869     return TRUE; /* successfully got 0 groups */
870 }
871
872 dbus_bool_t
873 bus_connection_is_in_unix_group (DBusConnection *connection,
874                                  unsigned long   gid)
875 {
876   int i;
877   unsigned long *group_ids;
878   int n_group_ids;
879
880   if (!bus_connection_get_unix_groups (connection, &group_ids, &n_group_ids,
881                                        NULL))
882     return FALSE;
883
884   i = 0;
885   while (i < n_group_ids)
886     {
887       if (group_ids[i] == gid)
888         {
889           dbus_free (group_ids);
890           return TRUE;
891         }
892       ++i;
893     }
894
895   dbus_free (group_ids);
896   return FALSE;
897 }
898
899 const char *
900 bus_connection_get_loginfo (DBusConnection        *connection)
901 {
902   BusConnectionData *d;
903     
904   d = BUS_CONNECTION_DATA (connection);
905
906   if (!bus_connection_is_active (connection))
907     return "inactive";
908   return d->cached_loginfo_string;  
909 }
910
911 BusClientPolicy*
912 bus_connection_get_policy (DBusConnection *connection)
913 {
914   BusConnectionData *d;
915     
916   d = BUS_CONNECTION_DATA (connection);
917
918   _dbus_assert (d != NULL);
919   _dbus_assert (d->policy != NULL);
920   
921   return d->policy;
922 }
923
924 static dbus_bool_t
925 foreach_active (BusConnections               *connections,
926                 BusConnectionForeachFunction  function,
927                 void                         *data)
928 {
929   DBusList *link;
930   
931   link = _dbus_list_get_first_link (&connections->completed);
932   while (link != NULL)
933     {
934       DBusConnection *connection = link->data;
935       DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
936
937       if (!(* function) (connection, data))
938         return FALSE;
939       
940       link = next;
941     }
942
943   return TRUE;
944 }
945
946 static dbus_bool_t
947 foreach_inactive (BusConnections               *connections,
948                   BusConnectionForeachFunction  function,
949                   void                         *data)
950 {
951   DBusList *link;
952   
953   link = _dbus_list_get_first_link (&connections->incomplete);
954   while (link != NULL)
955     {
956       DBusConnection *connection = link->data;
957       DBusList *next = _dbus_list_get_next_link (&connections->incomplete, link);
958
959       if (!(* function) (connection, data))
960         return FALSE;
961       
962       link = next;
963     }
964
965   return TRUE;
966 }
967
968 /**
969  * Calls function on each active connection; if the function returns
970  * #FALSE, stops iterating. Active connections are authenticated
971  * and have sent a Hello message.
972  *
973  * @param connections the connections object
974  * @param function the function
975  * @param data data to pass to it as a second arg
976  */
977 void
978 bus_connections_foreach_active (BusConnections               *connections,
979                                 BusConnectionForeachFunction  function,
980                                 void                         *data)
981 {
982   foreach_active (connections, function, data);
983 }
984
985 /**
986  * Calls function on each connection; if the function returns
987  * #FALSE, stops iterating.
988  *
989  * @param connections the connections object
990  * @param function the function
991  * @param data data to pass to it as a second arg
992  */
993 void
994 bus_connections_foreach (BusConnections               *connections,
995                          BusConnectionForeachFunction  function,
996                          void                         *data)
997 {
998   if (!foreach_active (connections, function, data))
999     return;
1000
1001   foreach_inactive (connections, function, data);
1002 }
1003
1004 DBusConnection*
1005 bus_connections_find_conn_by_name(BusConnections *connections, const char* name)
1006 {
1007     DBusList *link;
1008
1009     link = _dbus_list_get_first_link (&connections->completed);
1010     while (link != NULL)
1011       {
1012         DBusConnection *connection = link->data;
1013         DBusList *next = _dbus_list_get_next_link (&connections->completed, link);
1014
1015         if (!strcmp(bus_connection_get_name(connection), name))
1016           return connection;
1017
1018         link = next;
1019       }
1020
1021     return NULL;
1022 }
1023
1024 BusContext*
1025 bus_connections_get_context (BusConnections *connections)
1026 {
1027   return connections->context;
1028 }
1029
1030 /*
1031  * This is used to avoid covering the same connection twice when
1032  * traversing connections. Note that it assumes we will
1033  * bus_connection_mark_stamp() each connection at least once per
1034  * INT_MAX increments of the global stamp, or wraparound would break
1035  * things.
1036  */
1037 void
1038 bus_connections_increment_stamp (BusConnections *connections)
1039 {
1040   connections->stamp += 1;
1041 }
1042
1043 /* Mark connection with current stamp, return TRUE if it
1044  * didn't already have that stamp
1045  */
1046 dbus_bool_t
1047 bus_connection_mark_stamp (DBusConnection *connection)
1048 {
1049   BusConnectionData *d;
1050   
1051   d = BUS_CONNECTION_DATA (connection);
1052   
1053   _dbus_assert (d != NULL);
1054
1055   if (d->stamp == d->connections->stamp)
1056     return FALSE;
1057   else
1058     {
1059       d->stamp = d->connections->stamp;
1060       return TRUE;
1061     }
1062 }
1063
1064 BusContext*
1065 bus_connection_get_context (DBusConnection *connection)
1066 {
1067   BusConnectionData *d;
1068
1069   d = BUS_CONNECTION_DATA (connection);
1070
1071   _dbus_assert (d != NULL);
1072
1073   return d->connections->context;
1074 }
1075
1076 BusConnections*
1077 bus_connection_get_connections (DBusConnection *connection)
1078 {
1079   BusConnectionData *d;
1080     
1081   d = BUS_CONNECTION_DATA (connection);
1082
1083   _dbus_assert (d != NULL);
1084
1085   return d->connections;
1086 }
1087
1088 BusRegistry*
1089 bus_connection_get_registry (DBusConnection *connection)
1090 {
1091   BusConnectionData *d;
1092
1093   d = BUS_CONNECTION_DATA (connection);
1094
1095   _dbus_assert (d != NULL);
1096
1097   return bus_context_get_registry (d->connections->context);
1098 }
1099
1100 BusActivation*
1101 bus_connection_get_activation (DBusConnection *connection)
1102 {
1103   BusConnectionData *d;
1104
1105   d = BUS_CONNECTION_DATA (connection);
1106
1107   _dbus_assert (d != NULL);
1108
1109   return bus_context_get_activation (d->connections->context);
1110 }
1111
1112 BusMatchmaker*
1113 bus_connection_get_matchmaker (DBusConnection *connection)
1114 {
1115   BusConnectionData *d;
1116
1117   d = BUS_CONNECTION_DATA (connection);
1118
1119   _dbus_assert (d != NULL);
1120
1121   return bus_context_get_matchmaker (d->connections->context);
1122 }
1123
1124 BusSELinuxID*
1125 bus_connection_get_selinux_id (DBusConnection *connection)
1126 {
1127   BusConnectionData *d;
1128
1129   d = BUS_CONNECTION_DATA (connection);
1130
1131   _dbus_assert (d != NULL);
1132
1133   return d->selinux_id;
1134 }
1135
1136 /**
1137  * Checks whether the connection is registered with the message bus.
1138  *
1139  * @param connection the connection
1140  * @returns #TRUE if we're an active message bus participant
1141  */
1142 dbus_bool_t
1143 bus_connection_is_active (DBusConnection *connection)
1144 {
1145   BusConnectionData *d;
1146
1147   d = BUS_CONNECTION_DATA (connection);
1148   
1149   return d != NULL && d->name != NULL;
1150 }
1151
1152 dbus_bool_t
1153 bus_connection_preallocate_oom_error (DBusConnection *connection)
1154 {
1155   DBusMessage *message;
1156   DBusPreallocatedSend *preallocated;
1157   BusConnectionData *d;
1158
1159   d = BUS_CONNECTION_DATA (connection);  
1160
1161   _dbus_assert (d != NULL);
1162
1163   if (d->oom_preallocated != NULL)
1164     return TRUE;
1165   
1166   preallocated = dbus_connection_preallocate_send (connection);
1167   if (preallocated == NULL)
1168     return FALSE;
1169
1170   message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1171
1172   if (message == NULL)
1173     {
1174       dbus_connection_free_preallocated_send (connection, preallocated);
1175       return FALSE;
1176     }
1177
1178   /* d->name may be NULL, but that is OK */
1179   if (!dbus_message_set_error_name (message, DBUS_ERROR_NO_MEMORY) ||
1180       !dbus_message_set_destination (message, d->name) ||
1181       !dbus_message_set_sender (message,
1182                                 DBUS_SERVICE_DBUS))
1183     {
1184       dbus_connection_free_preallocated_send (connection, preallocated);
1185       dbus_message_unref (message);
1186       return FALSE;
1187     }
1188   
1189   /* set reply serial to placeholder value just so space is already allocated
1190    * for it.
1191    */
1192   if (!dbus_message_set_reply_serial (message, 14))
1193     {
1194       dbus_connection_free_preallocated_send (connection, preallocated);
1195       dbus_message_unref (message);
1196       return FALSE;
1197     }
1198
1199   d->oom_message = message;
1200   d->oom_preallocated = preallocated;
1201   
1202   return TRUE;
1203 }
1204
1205 void
1206 bus_connection_send_oom_error (DBusConnection *connection,
1207                                DBusMessage    *in_reply_to)
1208 {
1209   BusConnectionData *d;
1210
1211   d = BUS_CONNECTION_DATA (connection);  
1212
1213   _dbus_assert (d != NULL);  
1214   _dbus_assert (d->oom_message != NULL);
1215
1216   /* should always succeed since we set it to a placeholder earlier */
1217   if (!dbus_message_set_reply_serial (d->oom_message,
1218                                       dbus_message_get_serial (in_reply_to)))
1219     _dbus_assert_not_reached ("Failed to set reply serial for preallocated oom message");
1220
1221   _dbus_assert (dbus_message_get_sender (d->oom_message) != NULL);
1222   
1223   dbus_connection_send_preallocated (connection, d->oom_preallocated,
1224                                      d->oom_message, NULL);
1225
1226   dbus_message_unref (d->oom_message);
1227   d->oom_message = NULL;
1228   d->oom_preallocated = NULL;
1229 }
1230
1231 #ifdef DBUS_ENABLE_STATS
1232 static void
1233 update_peak (int *peak,
1234              int n)
1235 {
1236   if (*peak < n)
1237     *peak = n;
1238 }
1239 #endif
1240
1241 void
1242 bus_connection_add_match_rule_link (DBusConnection *connection,
1243                                     DBusList       *link)
1244 {
1245   BusConnectionData *d;
1246
1247   d = BUS_CONNECTION_DATA (connection);
1248   _dbus_assert (d != NULL);
1249
1250   _dbus_list_append_link (&d->match_rules, link);
1251
1252   d->n_match_rules += 1;
1253
1254 #ifdef DBUS_ENABLE_STATS
1255   update_peak (&d->peak_match_rules, d->n_match_rules);
1256   update_peak (&d->connections->peak_match_rules_per_conn, d->n_match_rules);
1257
1258   d->connections->total_match_rules += 1;
1259   update_peak (&d->connections->peak_match_rules,
1260                d->connections->total_match_rules);
1261 #endif
1262 }
1263
1264 dbus_bool_t
1265 bus_connection_add_match_rule (DBusConnection *connection,
1266                                BusMatchRule   *rule)
1267 {
1268     DBusList *link;
1269
1270   link = _dbus_list_alloc_link (rule);
1271
1272   if (link == NULL)
1273     return FALSE;
1274
1275   bus_connection_add_match_rule_link (connection, link);
1276
1277   return TRUE;
1278 }
1279
1280 void
1281 bus_connection_remove_match_rule (DBusConnection *connection,
1282                                   BusMatchRule   *rule)
1283 {
1284   BusConnectionData *d;
1285
1286   d = BUS_CONNECTION_DATA (connection);
1287   _dbus_assert (d != NULL);
1288
1289   _dbus_list_remove_last (&d->match_rules, rule);
1290
1291   d->n_match_rules -= 1;
1292   _dbus_assert (d->n_match_rules >= 0);
1293
1294 #ifdef DBUS_ENABLE_STATS
1295   d->connections->total_match_rules -= 1;
1296 #endif
1297 }
1298
1299 int
1300 bus_connection_get_n_match_rules (DBusConnection *connection)
1301 {
1302   BusConnectionData *d;
1303
1304   d = BUS_CONNECTION_DATA (connection);
1305   _dbus_assert (d != NULL);
1306   
1307   return d->n_match_rules;
1308 }
1309
1310 void
1311 bus_connection_add_owned_service_link (DBusConnection *connection,
1312                                        DBusList       *link)
1313 {
1314   BusConnectionData *d;
1315
1316   d = BUS_CONNECTION_DATA (connection);
1317   _dbus_assert (d != NULL);
1318
1319   _dbus_list_append_link (&d->services_owned, link);
1320
1321   d->n_services_owned += 1;
1322
1323 #ifdef DBUS_ENABLE_STATS
1324   update_peak (&d->peak_bus_names, d->n_services_owned);
1325   update_peak (&d->connections->peak_bus_names_per_conn,
1326                d->n_services_owned);
1327
1328   d->connections->total_bus_names += 1;
1329   update_peak (&d->connections->peak_bus_names,
1330                d->connections->total_bus_names);
1331 #endif
1332 }
1333
1334 dbus_bool_t
1335 bus_connection_add_owned_service (DBusConnection *connection,
1336                                   BusService     *service)
1337 {
1338   DBusList *link;
1339
1340   link = _dbus_list_alloc_link (service);
1341
1342   if (link == NULL)
1343     return FALSE;
1344
1345   bus_connection_add_owned_service_link (connection, link);
1346
1347   return TRUE;
1348 }
1349
1350 void
1351 bus_connection_remove_owned_service (DBusConnection *connection,
1352                                      BusService     *service)
1353 {
1354   BusConnectionData *d;
1355
1356   d = BUS_CONNECTION_DATA (connection);
1357   _dbus_assert (d != NULL);
1358
1359   _dbus_list_remove_last (&d->services_owned, service);
1360
1361   d->n_services_owned -= 1;
1362   _dbus_assert (d->n_services_owned >= 0);
1363
1364 #ifdef DBUS_ENABLE_STATS
1365   d->connections->total_bus_names -= 1;
1366 #endif
1367 }
1368
1369 int
1370 bus_connection_get_n_services_owned (DBusConnection *connection)
1371 {
1372   BusConnectionData *d;
1373
1374   d = BUS_CONNECTION_DATA (connection);
1375   _dbus_assert (d != NULL);
1376   
1377   return d->n_services_owned;
1378 }
1379
1380 DBusList**
1381 bus_connection_get_services_owned (DBusConnection *connection)
1382 {
1383     BusConnectionData *d;
1384     d = BUS_CONNECTION_DATA (connection);
1385     _dbus_assert (d != NULL);
1386
1387     return &d->services_owned;
1388 }
1389
1390 dbus_bool_t
1391 bus_connection_complete (DBusConnection   *connection,
1392                          const DBusString *name,
1393                          DBusError        *error)
1394 {
1395   BusConnectionData *d;
1396   unsigned long uid;
1397   
1398   d = BUS_CONNECTION_DATA (connection);
1399   _dbus_assert (d != NULL);
1400   _dbus_assert (d->name == NULL);
1401   _dbus_assert (d->policy == NULL);
1402
1403   _dbus_assert (!bus_connection_is_active (connection));
1404   
1405   if (!_dbus_string_copy_data (name, &d->name))
1406     {
1407       BUS_SET_OOM (error);
1408       return FALSE;
1409     }
1410
1411   _dbus_assert (d->name != NULL);
1412   
1413   _dbus_verbose ("Name %s assigned to %p\n", d->name, connection);
1414
1415   d->policy = bus_context_create_client_policy (d->connections->context,
1416                                                 connection,
1417                                                 error);
1418
1419   /* we may have a NULL policy on OOM or error getting list of
1420    * groups for a user. In the latter case we don't handle it so
1421    * well currently, as it will just keep failing over and over.
1422    */
1423
1424   if (d->policy == NULL)
1425     {
1426       _dbus_verbose ("Failed to create security policy for connection %p\n",
1427                      connection);
1428       _DBUS_ASSERT_ERROR_IS_SET (error);
1429       dbus_free (d->name);
1430       d->name = NULL;
1431       return FALSE;
1432     }
1433   
1434   if (dbus_connection_get_unix_user (connection, &uid))
1435     {
1436       if (!adjust_connections_for_uid (d->connections,
1437                                        uid, 1))
1438         goto fail;
1439     }
1440
1441   /* Create and cache a string which holds information about the 
1442    * peer process; used for logging purposes.
1443    */
1444   if (!cache_peer_loginfo_string (d, connection))
1445     goto fail;
1446
1447   /* Now the connection is active, move it between lists */
1448   _dbus_list_unlink (&d->connections->incomplete,
1449                      d->link_in_connection_list);
1450   d->connections->n_incomplete -= 1;
1451   _dbus_list_append_link (&d->connections->completed,
1452                           d->link_in_connection_list);
1453   d->connections->n_completed += 1;
1454
1455   _dbus_assert (d->connections->n_incomplete >= 0);
1456   _dbus_assert (d->connections->n_completed > 0);
1457
1458   /* See if we can remove the timeout */
1459   bus_connections_expire_incomplete (d->connections);
1460
1461   _dbus_assert (bus_connection_is_active (connection));
1462   
1463   return TRUE;
1464 fail:
1465   BUS_SET_OOM (error);
1466   dbus_free (d->name);
1467   d->name = NULL;
1468   if (d->policy)
1469     bus_client_policy_unref (d->policy);
1470   d->policy = NULL;
1471   return FALSE;
1472 }
1473
1474 const char *
1475 bus_connection_get_name (DBusConnection *connection)
1476 {
1477   BusConnectionData *d;
1478   
1479   d = BUS_CONNECTION_DATA (connection);
1480   _dbus_assert (d != NULL);
1481   
1482   return d->name;
1483 }
1484
1485 /**
1486  * Check whether completing the passed-in connection would
1487  * exceed limits, and if so set error and return #FALSE
1488  */
1489 dbus_bool_t
1490 bus_connections_check_limits (BusConnections  *connections,
1491                               DBusConnection  *requesting_completion,
1492                               DBusError       *error)
1493 {
1494   unsigned long uid;
1495
1496   if (connections->n_completed >=
1497       bus_context_get_max_completed_connections (connections->context))
1498     {
1499       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1500                       "The maximum number of active connections has been reached");
1501       return FALSE;
1502     }
1503   
1504   if (dbus_connection_get_unix_user (requesting_completion, &uid))
1505     {
1506       if (get_connections_for_uid (connections, uid) >=
1507           bus_context_get_max_connections_per_user (connections->context))
1508         {
1509           dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1510                           "The maximum number of active connections for UID %lu has been reached",
1511                           uid);
1512           return FALSE;
1513         }
1514     }
1515   
1516   return TRUE;
1517 }
1518
1519 static void
1520 bus_pending_reply_free (BusPendingReply *pending)
1521 {
1522   _dbus_verbose ("Freeing pending reply %p, replier %p receiver %p serial %u\n",
1523                  pending,
1524                  pending->will_send_reply,
1525                  pending->will_get_reply,
1526                  pending->reply_serial);
1527
1528   dbus_free (pending);
1529 }
1530
1531 static dbus_bool_t
1532 bus_pending_reply_send_no_reply (BusConnections  *connections,
1533                                  BusTransaction  *transaction,
1534                                  BusPendingReply *pending)
1535 {
1536   DBusMessage *message;
1537   DBusMessageIter iter;
1538   dbus_bool_t retval;
1539   const char *errmsg;
1540
1541   retval = FALSE;
1542   
1543   message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
1544   if (message == NULL)
1545     return FALSE;
1546   
1547   dbus_message_set_no_reply (message, TRUE);
1548   
1549   if (!dbus_message_set_reply_serial (message,
1550                                       pending->reply_serial))
1551     goto out;
1552
1553   if (!dbus_message_set_error_name (message,
1554                                     DBUS_ERROR_NO_REPLY))
1555     goto out;
1556
1557   errmsg = "Message did not receive a reply (timeout by message bus)";
1558   dbus_message_iter_init_append (message, &iter);
1559   if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &errmsg))
1560     goto out;
1561     
1562   if (!bus_transaction_send_from_driver (transaction, pending->will_get_reply,
1563                                          message))
1564     goto out;
1565
1566   retval = TRUE;
1567
1568  out:
1569   dbus_message_unref (message);
1570   return retval;
1571 }
1572
1573 static dbus_bool_t
1574 bus_pending_reply_expired (BusExpireList *list,
1575                            DBusList      *link,
1576                            void          *data)
1577 {
1578   BusPendingReply *pending = link->data;
1579   BusConnections *connections = data;
1580   BusTransaction *transaction;
1581   
1582   /* No reply is forthcoming. So nuke it if we can. If not,
1583    * leave it in the list to try expiring again later when we
1584    * get more memory.
1585    */
1586
1587   _dbus_verbose ("Expiring pending reply %p, replier %p receiver %p serial %u\n",
1588                  pending,
1589                  pending->will_send_reply,
1590                  pending->will_get_reply,
1591                  pending->reply_serial);
1592   
1593   transaction = bus_transaction_new (connections->context);
1594   if (transaction == NULL)
1595     return FALSE;
1596   
1597   if (!bus_pending_reply_send_no_reply (connections,
1598                                         transaction,
1599                                         pending))
1600     {
1601       bus_transaction_cancel_and_free (transaction);
1602       return FALSE;
1603     }
1604
1605   bus_expire_list_remove_link (connections->pending_replies, link);
1606
1607   bus_pending_reply_free (pending);
1608   bus_transaction_execute_and_free (transaction);
1609
1610   return TRUE;
1611 }
1612
1613 static void
1614 bus_connection_drop_pending_replies (BusConnections  *connections,
1615                                      DBusConnection  *connection)
1616 {
1617   /* The DBusConnection is almost 100% finalized here, so you can't
1618    * do anything with it except check for pointer equality
1619    */
1620   DBusList *link;
1621
1622   _dbus_verbose ("Dropping pending replies that involve connection %p\n",
1623                  connection);
1624   
1625   link = bus_expire_list_get_first_link (connections->pending_replies);
1626   while (link != NULL)
1627     {
1628       DBusList *next;
1629       BusPendingReply *pending;
1630
1631       next = bus_expire_list_get_next_link (connections->pending_replies,
1632                                             link);
1633       pending = link->data;
1634
1635       if (pending->will_get_reply == connection)
1636         {
1637           /* We don't need to track this pending reply anymore */
1638
1639           _dbus_verbose ("Dropping pending reply %p, replier %p receiver %p serial %u\n",
1640                          pending,
1641                          pending->will_send_reply,
1642                          pending->will_get_reply,
1643                          pending->reply_serial);
1644           
1645           bus_expire_list_remove_link (connections->pending_replies,
1646                                        link);
1647           bus_pending_reply_free (pending);
1648         }
1649       else if (pending->will_send_reply == connection)
1650         {
1651           /* The reply isn't going to be sent, so set things
1652            * up so it will be expired right away
1653            */
1654           _dbus_verbose ("Will expire pending reply %p, replier %p receiver %p serial %u\n",
1655                          pending,
1656                          pending->will_send_reply,
1657                          pending->will_get_reply,
1658                          pending->reply_serial);
1659           
1660           pending->will_send_reply = NULL;
1661           pending->expire_item.added_tv_sec = 0;
1662           pending->expire_item.added_tv_usec = 0;
1663
1664           bus_expire_list_recheck_immediately (connections->pending_replies);
1665         }
1666       
1667       link = next;
1668     }
1669 }
1670
1671
1672 typedef struct
1673 {
1674   BusPendingReply *pending;
1675   BusConnections  *connections;
1676 } CancelPendingReplyData;
1677
1678 static void
1679 cancel_pending_reply (void *data)
1680 {
1681   CancelPendingReplyData *d = data;
1682
1683   _dbus_verbose ("d = %p\n", d);
1684   
1685   if (!bus_expire_list_remove (d->connections->pending_replies,
1686                                &d->pending->expire_item))
1687     _dbus_assert_not_reached ("pending reply did not exist to be cancelled");
1688
1689   bus_pending_reply_free (d->pending); /* since it's been cancelled */
1690 }
1691
1692 static void
1693 cancel_pending_reply_data_free (void *data)
1694 {
1695   CancelPendingReplyData *d = data;
1696
1697   _dbus_verbose ("d = %p\n", d);
1698   
1699   /* d->pending should be either freed or still
1700    * in the list of pending replies (owned by someone
1701    * else)
1702    */
1703   
1704   dbus_free (d);
1705 }
1706
1707 /*
1708  * Record that a reply is allowed; return TRUE on success.
1709  */
1710 dbus_bool_t
1711 bus_connections_expect_reply (BusConnections  *connections,
1712                               BusTransaction  *transaction,
1713                               DBusConnection  *will_get_reply,
1714                               DBusConnection  *will_send_reply,
1715                               DBusMessage     *reply_to_this,
1716                               DBusError       *error)
1717 {
1718   BusPendingReply *pending;
1719   dbus_uint32_t reply_serial;
1720   DBusList *link;
1721   CancelPendingReplyData *cprd;
1722   int count;
1723
1724   _dbus_assert (will_get_reply != NULL);
1725   _dbus_assert (will_send_reply != NULL);
1726   _dbus_assert (reply_to_this != NULL);
1727   
1728   if (dbus_message_get_no_reply (reply_to_this))
1729     return TRUE; /* we won't allow a reply, since client doesn't care for one. */
1730   
1731   reply_serial = dbus_message_get_serial (reply_to_this);
1732
1733   link = bus_expire_list_get_first_link (connections->pending_replies);
1734   count = 0;
1735   while (link != NULL)
1736     {
1737       pending = link->data;
1738
1739       if (pending->reply_serial == reply_serial &&
1740           pending->will_get_reply == will_get_reply &&
1741           pending->will_send_reply == will_send_reply)
1742         {
1743           dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1744                           "Message has the same reply serial as a currently-outstanding existing method call");
1745           return FALSE;
1746         }
1747       
1748       link = bus_expire_list_get_next_link (connections->pending_replies,
1749                                             link);
1750       if (pending->will_get_reply == will_get_reply)
1751         ++count;
1752     }
1753   
1754   if (count >=
1755       bus_context_get_max_replies_per_connection (connections->context))
1756     {
1757       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1758                       "The maximum number of pending replies per connection has been reached");
1759       return FALSE;
1760     }
1761
1762   pending = dbus_new0 (BusPendingReply, 1);
1763   if (pending == NULL)
1764     {
1765       BUS_SET_OOM (error);
1766       return FALSE;
1767     }
1768
1769 #ifdef DBUS_ENABLE_VERBOSE_MODE
1770   /* so we can see a not-yet-added pending reply */
1771   pending->expire_item.added_tv_sec = 1;
1772   pending->expire_item.added_tv_usec = 1;
1773 #endif
1774
1775   pending->will_get_reply = will_get_reply;
1776   pending->will_send_reply = will_send_reply;
1777   pending->reply_serial = reply_serial;
1778   
1779   cprd = dbus_new0 (CancelPendingReplyData, 1);
1780   if (cprd == NULL)
1781     {
1782       BUS_SET_OOM (error);
1783       bus_pending_reply_free (pending);
1784       return FALSE;
1785     }
1786   
1787   if (!bus_expire_list_add (connections->pending_replies,
1788                             &pending->expire_item))
1789     {
1790       BUS_SET_OOM (error);
1791       dbus_free (cprd);
1792       bus_pending_reply_free (pending);
1793       return FALSE;
1794     }
1795
1796   if (!bus_transaction_add_cancel_hook (transaction,
1797                                         cancel_pending_reply,
1798                                         cprd,
1799                                         cancel_pending_reply_data_free))
1800     {
1801       BUS_SET_OOM (error);
1802       bus_expire_list_remove (connections->pending_replies, &pending->expire_item);
1803       dbus_free (cprd);
1804       bus_pending_reply_free (pending);
1805       return FALSE;
1806     }
1807                                         
1808   cprd->pending = pending;
1809   cprd->connections = connections;
1810   
1811   _dbus_get_monotonic_time (&pending->expire_item.added_tv_sec,
1812                             &pending->expire_item.added_tv_usec);
1813
1814   _dbus_verbose ("Added pending reply %p, replier %p receiver %p serial %u\n",
1815                  pending,
1816                  pending->will_send_reply,
1817                  pending->will_get_reply,
1818                  pending->reply_serial);
1819   
1820   return TRUE;
1821 }
1822
1823 typedef struct
1824 {
1825   DBusList        *link;
1826   BusConnections  *connections;
1827 } CheckPendingReplyData;
1828
1829 static void
1830 cancel_check_pending_reply (void *data)
1831 {
1832   CheckPendingReplyData *d = data;
1833
1834   _dbus_verbose ("d = %p\n",d);
1835
1836   bus_expire_list_add_link (d->connections->pending_replies,
1837                             d->link);
1838   d->link = NULL;
1839 }
1840
1841 static void
1842 check_pending_reply_data_free (void *data)
1843 {
1844   CheckPendingReplyData *d = data;
1845
1846   _dbus_verbose ("d = %p\n",d);
1847   
1848   if (d->link != NULL)
1849     {
1850       BusPendingReply *pending = d->link->data;
1851       
1852       _dbus_assert (!bus_expire_list_contains_item (d->connections->pending_replies,
1853                                                     &pending->expire_item));
1854       
1855       bus_pending_reply_free (pending);
1856       _dbus_list_free_link (d->link);
1857     }
1858   
1859   dbus_free (d);
1860 }
1861
1862 /*
1863  * Check whether a reply is allowed, remove BusPendingReply
1864  * if so, return TRUE if so.
1865  */
1866 dbus_bool_t
1867 bus_connections_check_reply (BusConnections *connections,
1868                              BusTransaction *transaction,
1869                              DBusConnection *sending_reply,
1870                              DBusConnection *receiving_reply,
1871                              DBusMessage    *reply,
1872                              DBusError      *error)
1873 {
1874   CheckPendingReplyData *cprd;
1875   DBusList *link;
1876   dbus_uint32_t reply_serial;
1877   
1878   _dbus_assert (sending_reply != NULL);
1879   _dbus_assert (receiving_reply != NULL);
1880
1881   reply_serial = dbus_message_get_reply_serial (reply);
1882
1883   link = bus_expire_list_get_first_link (connections->pending_replies);
1884   while (link != NULL)
1885     {
1886       BusPendingReply *pending = link->data;
1887
1888       if (pending->reply_serial == reply_serial &&
1889           pending->will_get_reply == receiving_reply &&
1890           pending->will_send_reply == sending_reply)
1891         {
1892           _dbus_verbose ("Found pending reply with serial %u\n", reply_serial);
1893           break;
1894         }
1895       
1896       link = bus_expire_list_get_next_link (connections->pending_replies,
1897                                             link);
1898     }
1899
1900   if (link == NULL)
1901     {
1902       _dbus_verbose ("No pending reply expected\n");
1903
1904       return FALSE;
1905     }
1906
1907   cprd = dbus_new0 (CheckPendingReplyData, 1);
1908   if (cprd == NULL)
1909     {
1910       BUS_SET_OOM (error);
1911       return FALSE;
1912     }
1913   
1914   if (!bus_transaction_add_cancel_hook (transaction,
1915                                         cancel_check_pending_reply,
1916                                         cprd,
1917                                         check_pending_reply_data_free))
1918     {
1919       BUS_SET_OOM (error);
1920       dbus_free (cprd);
1921       return FALSE;
1922     }
1923
1924   cprd->link = link;
1925   cprd->connections = connections;
1926   
1927   bus_expire_list_unlink (connections->pending_replies,
1928                           link);
1929   
1930   _dbus_assert (!bus_expire_list_contains_item (connections->pending_replies, link->data));
1931
1932   return TRUE;
1933 }
1934
1935 /*
1936  * Transactions
1937  *
1938  * Note that this is fairly fragile; in particular, don't try to use
1939  * one transaction across any main loop iterations.
1940  */
1941
1942 typedef struct
1943 {
1944   BusTransaction *transaction;
1945   DBusMessage    *message;
1946   DBusPreallocatedSend *preallocated;
1947 } MessageToSend;
1948
1949 typedef struct
1950 {
1951   BusTransactionCancelFunction cancel_function;
1952   DBusFreeFunction free_data_function;
1953   void *data;
1954 } CancelHook;
1955
1956 struct BusTransaction
1957 {
1958   DBusList *connections;
1959   BusContext *context;
1960   DBusList *cancel_hooks;
1961 };
1962
1963 static void
1964 message_to_send_free (DBusConnection *connection,
1965                       MessageToSend  *to_send)
1966 {
1967   if (to_send->message)
1968     dbus_message_unref (to_send->message);
1969
1970   if (to_send->preallocated)
1971     dbus_connection_free_preallocated_send (connection, to_send->preallocated);
1972
1973   dbus_free (to_send);
1974 }
1975
1976 static void
1977 cancel_hook_cancel (void *element,
1978                     void *data)
1979 {
1980   CancelHook *ch = element;
1981
1982   _dbus_verbose ("Running transaction cancel hook\n");
1983   
1984   if (ch->cancel_function)
1985     (* ch->cancel_function) (ch->data);  
1986 }
1987
1988 static void
1989 cancel_hook_free (void *element,
1990                   void *data)
1991 {
1992   CancelHook *ch = element;
1993
1994   if (ch->free_data_function)
1995     (* ch->free_data_function) (ch->data);
1996
1997   dbus_free (ch);
1998 }
1999
2000 static void
2001 free_cancel_hooks (BusTransaction *transaction)
2002 {
2003   _dbus_list_foreach (&transaction->cancel_hooks,
2004                       cancel_hook_free, NULL);
2005   
2006   _dbus_list_clear (&transaction->cancel_hooks);
2007 }
2008
2009 BusTransaction*
2010 bus_transaction_new (BusContext *context)
2011 {
2012   BusTransaction *transaction;
2013
2014   transaction = dbus_new0 (BusTransaction, 1);
2015   if (transaction == NULL)
2016     return NULL;
2017
2018   transaction->context = context;
2019   
2020   return transaction;
2021 }
2022
2023 BusContext*
2024 bus_transaction_get_context (BusTransaction  *transaction)
2025 {
2026   return transaction->context;
2027 }
2028
2029 BusConnections*
2030 bus_transaction_get_connections (BusTransaction  *transaction)
2031 {
2032   return bus_context_get_connections (transaction->context);
2033 }
2034
2035 dbus_bool_t
2036 bus_transaction_send_from_driver (BusTransaction *transaction,
2037                                   DBusConnection *connection,
2038                                   DBusMessage    *message)
2039 {
2040   /* We have to set the sender to the driver, and have
2041    * to check security policy since it was not done in
2042    * dispatch.c
2043    */
2044   _dbus_verbose ("Sending %s %s %s from driver\n",
2045                  dbus_message_get_interface (message) ?
2046                  dbus_message_get_interface (message) : "(no interface)",
2047                  dbus_message_get_member (message) ?
2048                  dbus_message_get_member (message) : "(no member)",
2049                  dbus_message_get_error_name (message) ?
2050                  dbus_message_get_error_name (message) : "(no error name)");
2051                  
2052   if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
2053     return FALSE;
2054
2055   if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) /* we can't set destination on the basis of connection when on kdbus*/
2056     if (bus_connection_is_active (connection))
2057     {
2058       if (!dbus_message_set_destination (message,
2059                                          bus_connection_get_name (connection)))
2060         return FALSE;
2061     }
2062   
2063   /* bus driver never wants a reply */
2064   dbus_message_set_no_reply (message, TRUE);
2065   
2066   /* If security policy doesn't allow the message, we silently
2067    * eat it; the driver doesn't care about getting a reply.
2068    */
2069   if (!bus_context_check_security_policy (bus_transaction_get_context (transaction),
2070                                           transaction,
2071                                           NULL, connection, connection, message, NULL))
2072     return TRUE;
2073
2074   return bus_transaction_send (transaction, connection, message);
2075 }
2076
2077 dbus_bool_t
2078 bus_transaction_send (BusTransaction *transaction,
2079                       DBusConnection *connection,
2080                       DBusMessage    *message)
2081 {
2082   MessageToSend *to_send;
2083   BusConnectionData *d;
2084   DBusList *link;
2085
2086   _dbus_verbose ("  trying to add %s interface=%s member=%s error=%s to transaction%s\n",
2087                  dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? "error" :
2088                  dbus_message_get_reply_serial (message) != 0 ? "reply" :
2089                  "message",
2090                  dbus_message_get_interface (message) ?
2091                  dbus_message_get_interface (message) : "(unset)",
2092                  dbus_message_get_member (message) ?
2093                  dbus_message_get_member (message) : "(unset)",
2094                  dbus_message_get_error_name (message) ?
2095                  dbus_message_get_error_name (message) : "(unset)",
2096                  dbus_connection_get_is_connected (connection) ?
2097                  "" : " (disconnected)");
2098
2099   _dbus_assert (dbus_message_get_sender (message) != NULL);
2100   
2101   if (!dbus_connection_get_is_connected (connection))
2102     return TRUE; /* silently ignore disconnected connections */
2103   
2104   d = BUS_CONNECTION_DATA (connection);
2105   _dbus_assert (d != NULL);
2106   
2107   to_send = dbus_new (MessageToSend, 1);
2108   if (to_send == NULL)
2109     {
2110       return FALSE;
2111     }
2112
2113   to_send->preallocated = dbus_connection_preallocate_send (connection);
2114   if (to_send->preallocated == NULL)
2115     {
2116       dbus_free (to_send);
2117       return FALSE;
2118     }  
2119   
2120   dbus_message_ref (message);
2121   to_send->message = message;
2122   to_send->transaction = transaction;
2123
2124   _dbus_verbose ("about to prepend message\n");
2125   
2126   if (!_dbus_list_prepend (&d->transaction_messages, to_send))
2127     {
2128       message_to_send_free (connection, to_send);
2129       return FALSE;
2130     }
2131
2132   _dbus_verbose ("prepended message\n");
2133   
2134   /* See if we already had this connection in the list
2135    * for this transaction. If we have a pending message,
2136    * then we should already be in transaction->connections
2137    */
2138   link = _dbus_list_get_first_link (&d->transaction_messages);
2139   _dbus_assert (link->data == to_send);
2140   link = _dbus_list_get_next_link (&d->transaction_messages, link);
2141   while (link != NULL)
2142     {
2143       MessageToSend *m = link->data;
2144       DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2145       
2146       if (m->transaction == transaction)
2147         break;
2148         
2149       link = next;
2150     }
2151
2152   if (link == NULL)
2153     {
2154       if (!_dbus_list_prepend (&transaction->connections, connection))
2155         {
2156           _dbus_list_remove (&d->transaction_messages, to_send);
2157           message_to_send_free (connection, to_send);
2158           return FALSE;
2159         }
2160     }
2161
2162   return TRUE;
2163 }
2164
2165 static void
2166 connection_cancel_transaction (DBusConnection *connection,
2167                                BusTransaction *transaction)
2168 {
2169   DBusList *link;
2170   BusConnectionData *d;
2171   
2172   d = BUS_CONNECTION_DATA (connection);
2173   _dbus_assert (d != NULL);
2174   
2175   link = _dbus_list_get_first_link (&d->transaction_messages);
2176   while (link != NULL)
2177     {
2178       MessageToSend *m = link->data;
2179       DBusList *next = _dbus_list_get_next_link (&d->transaction_messages, link);
2180       
2181       if (m->transaction == transaction)
2182         {
2183           _dbus_list_remove_link (&d->transaction_messages,
2184                                   link);
2185           
2186           message_to_send_free (connection, m);
2187         }
2188         
2189       link = next;
2190     }
2191 }
2192
2193 void
2194 bus_transaction_cancel_and_free (BusTransaction *transaction)
2195 {
2196   DBusConnection *connection;
2197
2198   _dbus_verbose ("TRANSACTION: cancelled\n");
2199   
2200   while ((connection = _dbus_list_pop_first (&transaction->connections)))
2201     connection_cancel_transaction (connection, transaction);
2202
2203   _dbus_assert (transaction->connections == NULL);
2204
2205   _dbus_list_foreach (&transaction->cancel_hooks,
2206                       cancel_hook_cancel, NULL);
2207
2208   free_cancel_hooks (transaction);
2209   
2210   dbus_free (transaction);
2211 }
2212
2213 static void
2214 connection_execute_transaction (DBusConnection *connection,
2215                                 BusTransaction *transaction)
2216 {
2217   DBusList *link;
2218   BusConnectionData *d;
2219   
2220   d = BUS_CONNECTION_DATA (connection);
2221   _dbus_assert (d != NULL);
2222
2223   /* Send the queue in order (FIFO) */
2224   link = _dbus_list_get_last_link (&d->transaction_messages);
2225   while (link != NULL)
2226     {
2227       MessageToSend *m = link->data;
2228       DBusList *prev = _dbus_list_get_prev_link (&d->transaction_messages, link);
2229       
2230       if (m->transaction == transaction)
2231         {
2232           _dbus_list_remove_link (&d->transaction_messages,
2233                                   link);
2234
2235           _dbus_assert (dbus_message_get_sender (m->message) != NULL);
2236           
2237           dbus_connection_send_preallocated (connection,
2238                                              m->preallocated,
2239                                              m->message,
2240                                              NULL);
2241
2242           m->preallocated = NULL; /* so we don't double-free it */
2243           
2244           message_to_send_free (connection, m);
2245         }
2246         
2247       link = prev;
2248     }
2249 }
2250
2251 void
2252 bus_transaction_execute_and_free (BusTransaction *transaction)
2253 {
2254   /* For each connection in transaction->connections
2255    * send the messages
2256    */
2257   DBusConnection *connection;
2258
2259   _dbus_verbose ("TRANSACTION: executing\n");
2260   
2261   while ((connection = _dbus_list_pop_first (&transaction->connections)))
2262     connection_execute_transaction (connection, transaction);
2263
2264   _dbus_assert (transaction->connections == NULL);
2265
2266   free_cancel_hooks (transaction);
2267   
2268   dbus_free (transaction);
2269 }
2270
2271 void
2272 bus_transaction_free (BusTransaction *transaction)
2273 {
2274           free_cancel_hooks (transaction);
2275           dbus_free (transaction);
2276 }
2277
2278 static void
2279 bus_connection_remove_transactions (DBusConnection *connection)
2280 {
2281   MessageToSend *to_send;
2282   BusConnectionData *d;
2283   
2284   d = BUS_CONNECTION_DATA (connection);
2285   _dbus_assert (d != NULL);
2286   
2287   while ((to_send = _dbus_list_get_first (&d->transaction_messages)))
2288     {
2289       /* only has an effect for the first MessageToSend listing this transaction */
2290       _dbus_list_remove (&to_send->transaction->connections,
2291                          connection);
2292
2293       _dbus_list_remove (&d->transaction_messages, to_send);
2294       message_to_send_free (connection, to_send);
2295     }
2296 }
2297
2298 /**
2299  * Converts the DBusError to a message reply
2300  */
2301 dbus_bool_t
2302 bus_transaction_send_error_reply (BusTransaction  *transaction,
2303                                   DBusConnection  *connection,
2304                                   const DBusError *error,
2305                                   DBusMessage     *in_reply_to)
2306 {
2307   DBusMessage *reply;
2308   
2309   _dbus_assert (error != NULL);
2310   _DBUS_ASSERT_ERROR_IS_SET (error);
2311   
2312   _dbus_verbose ("Sending error reply %s \"%s\"\n",
2313                  error->name, error->message);
2314
2315   reply = dbus_message_new_error (in_reply_to,
2316                                   error->name,
2317                                   error->message);
2318   if (reply == NULL)
2319     return FALSE;
2320
2321   if (!bus_transaction_send_from_driver (transaction, connection, reply))
2322     {
2323       dbus_message_unref (reply);
2324       return FALSE;
2325     }
2326
2327   dbus_message_unref (reply);
2328   
2329   return TRUE;
2330 }
2331
2332 dbus_bool_t
2333 bus_transaction_add_cancel_hook (BusTransaction               *transaction,
2334                                  BusTransactionCancelFunction  cancel_function,
2335                                  void                         *data,
2336                                  DBusFreeFunction              free_data_function)
2337 {
2338   CancelHook *ch;
2339
2340   ch = dbus_new (CancelHook, 1);
2341   if (ch == NULL)
2342     return FALSE;
2343
2344   _dbus_verbose ("     adding cancel hook function = %p data = %p\n",
2345                  cancel_function, data);
2346   
2347   ch->cancel_function = cancel_function;
2348   ch->data = data;
2349   ch->free_data_function = free_data_function;
2350
2351   /* It's important that the hooks get run in reverse order that they
2352    * were added
2353    */
2354   if (!_dbus_list_prepend (&transaction->cancel_hooks, ch))
2355     {
2356       dbus_free (ch);
2357       return FALSE;
2358     }
2359
2360   return TRUE;
2361 }
2362
2363 #ifdef DBUS_ENABLE_STATS
2364 int
2365 bus_connections_get_n_active (BusConnections *connections)
2366 {
2367   return connections->n_completed;
2368 }
2369
2370 int
2371 bus_connections_get_n_incomplete (BusConnections *connections)
2372 {
2373   return connections->n_incomplete;
2374 }
2375
2376 int
2377 bus_connections_get_total_match_rules (BusConnections *connections)
2378 {
2379   return connections->total_match_rules;
2380 }
2381
2382 int
2383 bus_connections_get_peak_match_rules (BusConnections *connections)
2384 {
2385   return connections->peak_match_rules;
2386 }
2387
2388 int
2389 bus_connections_get_peak_match_rules_per_conn (BusConnections *connections)
2390 {
2391   return connections->peak_match_rules_per_conn;
2392 }
2393
2394 int
2395 bus_connections_get_total_bus_names (BusConnections *connections)
2396 {
2397   return connections->total_bus_names;
2398 }
2399
2400 int
2401 bus_connections_get_peak_bus_names (BusConnections *connections)
2402 {
2403   return connections->peak_bus_names;
2404 }
2405
2406 int
2407 bus_connections_get_peak_bus_names_per_conn (BusConnections *connections)
2408 {
2409   return connections->peak_bus_names_per_conn;
2410 }
2411
2412 int
2413 bus_connection_get_peak_match_rules (DBusConnection *connection)
2414 {
2415   BusConnectionData *d;
2416
2417   d = BUS_CONNECTION_DATA (connection);
2418   return d->peak_match_rules;
2419 }
2420
2421 int
2422 bus_connection_get_peak_bus_names (DBusConnection *connection)
2423 {
2424   BusConnectionData *d;
2425
2426   d = BUS_CONNECTION_DATA (connection);
2427   return d->peak_bus_names;
2428 }
2429 #endif /* DBUS_ENABLE_STATS */