Delete stale pid file on cygwin.
[platform/upstream/dbus.git] / bus / bus.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* bus.c  message bus context object
3  *
4  * Copyright (C) 2003, 2004 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 "bus.h"
26 #include "activation.h"
27 #include "connection.h"
28 #include "services.h"
29 #include "utils.h"
30 #include "policy.h"
31 #include "config-parser.h"
32 #include "signals.h"
33 #include "selinux.h"
34 #include "dir-watch.h"
35 #include <dbus/dbus-list.h>
36 #include <dbus/dbus-hash.h>
37 #include <dbus/dbus-credentials.h>
38 #include <dbus/dbus-internals.h>
39 #include <signal.h>
40
41 struct BusContext
42 {
43   int refcount;
44   DBusGUID uuid;
45   char *config_file;
46   char *type;
47   char *servicehelper;
48   char *address;
49   char *pidfile;
50   char *user;
51   char *log_prefix;
52   DBusLoop *loop;
53   DBusList *servers;
54   BusConnections *connections;
55   BusActivation *activation;
56   BusRegistry *registry;
57   BusPolicy *policy;
58   BusMatchmaker *matchmaker;
59   BusLimits limits;
60   unsigned int fork : 1;
61   unsigned int syslog : 1;
62   unsigned int keep_umask : 1;
63   unsigned int allow_anonymous : 1;
64   unsigned int systemd_activation : 1;
65 };
66
67 static dbus_int32_t server_data_slot = -1;
68
69 typedef struct
70 {
71   BusContext *context;
72 } BusServerData;
73
74 #define BUS_SERVER_DATA(server) (dbus_server_get_data ((server), server_data_slot))
75
76 static BusContext*
77 server_get_context (DBusServer *server)
78 {
79   BusContext *context;
80   BusServerData *bd;
81
82   if (!dbus_server_allocate_data_slot (&server_data_slot))
83     return NULL;
84
85   bd = BUS_SERVER_DATA (server);
86   if (bd == NULL)
87     {
88       dbus_server_free_data_slot (&server_data_slot);
89       return NULL;
90     }
91
92   context = bd->context;
93
94   dbus_server_free_data_slot (&server_data_slot);
95
96   return context;
97 }
98
99 static dbus_bool_t
100 server_watch_callback (DBusWatch     *watch,
101                        unsigned int   condition,
102                        void          *data)
103 {
104   /* FIXME this can be done in dbus-mainloop.c
105    * if the code in activation.c for the babysitter
106    * watch handler is fixed.
107    */
108
109   return dbus_watch_handle (watch, condition);
110 }
111
112 static dbus_bool_t
113 add_server_watch (DBusWatch  *watch,
114                   void       *data)
115 {
116   DBusServer *server = data;
117   BusContext *context;
118
119   context = server_get_context (server);
120
121   return _dbus_loop_add_watch (context->loop,
122                                watch, server_watch_callback, server,
123                                NULL);
124 }
125
126 static void
127 remove_server_watch (DBusWatch  *watch,
128                      void       *data)
129 {
130   DBusServer *server = data;
131   BusContext *context;
132
133   context = server_get_context (server);
134
135   _dbus_loop_remove_watch (context->loop,
136                            watch, server_watch_callback, server);
137 }
138
139
140 static void
141 server_timeout_callback (DBusTimeout   *timeout,
142                          void          *data)
143 {
144   /* can return FALSE on OOM but we just let it fire again later */
145   dbus_timeout_handle (timeout);
146 }
147
148 static dbus_bool_t
149 add_server_timeout (DBusTimeout *timeout,
150                     void        *data)
151 {
152   DBusServer *server = data;
153   BusContext *context;
154
155   context = server_get_context (server);
156
157   return _dbus_loop_add_timeout (context->loop,
158                                  timeout, server_timeout_callback, server, NULL);
159 }
160
161 static void
162 remove_server_timeout (DBusTimeout *timeout,
163                        void        *data)
164 {
165   DBusServer *server = data;
166   BusContext *context;
167
168   context = server_get_context (server);
169
170   _dbus_loop_remove_timeout (context->loop,
171                              timeout, server_timeout_callback, server);
172 }
173
174 static void
175 new_connection_callback (DBusServer     *server,
176                          DBusConnection *new_connection,
177                          void           *data)
178 {
179   BusContext *context = data;
180
181   if (!bus_connections_setup_connection (context->connections, new_connection))
182     {
183       _dbus_verbose ("No memory to setup new connection\n");
184
185       /* if we don't do this, it will get unref'd without
186        * being disconnected... kind of strange really
187        * that we have to do this, people won't get it right
188        * in general.
189        */
190       dbus_connection_close (new_connection);
191     }
192
193   dbus_connection_set_max_received_size (new_connection,
194                                          context->limits.max_incoming_bytes);
195
196   dbus_connection_set_max_message_size (new_connection,
197                                         context->limits.max_message_size);
198
199   dbus_connection_set_max_received_unix_fds (new_connection,
200                                          context->limits.max_incoming_unix_fds);
201
202   dbus_connection_set_max_message_unix_fds (new_connection,
203                                         context->limits.max_message_unix_fds);
204
205   dbus_connection_set_allow_anonymous (new_connection,
206                                        context->allow_anonymous);
207
208   /* on OOM, we won't have ref'd the connection so it will die. */
209 }
210
211 static void
212 free_server_data (void *data)
213 {
214   BusServerData *bd = data;
215
216   dbus_free (bd);
217 }
218
219 static dbus_bool_t
220 setup_server (BusContext *context,
221               DBusServer *server,
222               char      **auth_mechanisms,
223               DBusError  *error)
224 {
225   BusServerData *bd;
226
227   bd = dbus_new0 (BusServerData, 1);
228   if (bd == NULL || !dbus_server_set_data (server,
229                                            server_data_slot,
230                                            bd, free_server_data))
231     {
232       dbus_free (bd);
233       BUS_SET_OOM (error);
234       return FALSE;
235     }
236
237   bd->context = context;
238
239   if (!dbus_server_set_auth_mechanisms (server, (const char**) auth_mechanisms))
240     {
241       BUS_SET_OOM (error);
242       return FALSE;
243     }
244
245   dbus_server_set_new_connection_function (server,
246                                            new_connection_callback,
247                                            context, NULL);
248
249   if (!dbus_server_set_watch_functions (server,
250                                         add_server_watch,
251                                         remove_server_watch,
252                                         NULL,
253                                         server,
254                                         NULL))
255     {
256       BUS_SET_OOM (error);
257       return FALSE;
258     }
259
260   if (!dbus_server_set_timeout_functions (server,
261                                           add_server_timeout,
262                                           remove_server_timeout,
263                                           NULL,
264                                           server, NULL))
265     {
266       BUS_SET_OOM (error);
267       return FALSE;
268     }
269
270   return TRUE;
271 }
272
273 /* This code only gets executed the first time the
274  * config files are parsed.  It is not executed
275  * when config files are reloaded.
276  */
277 static dbus_bool_t
278 process_config_first_time_only (BusContext       *context,
279                                 BusConfigParser  *parser,
280                                 const DBusString *address,
281                                 dbus_bool_t      systemd_activation,
282                                 DBusError        *error)
283 {
284   DBusString log_prefix;
285   DBusList *link;
286   DBusList **addresses;
287   const char *user, *pidfile;
288   char **auth_mechanisms;
289   DBusList **auth_mechanisms_list;
290   int len;
291   dbus_bool_t retval;
292
293   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
294
295   retval = FALSE;
296   auth_mechanisms = NULL;
297
298   context->systemd_activation = systemd_activation;
299
300   /* Check for an existing pid file. Of course this is a race;
301    * we'd have to use fcntl() locks on the pid file to
302    * avoid that. But we want to check for the pid file
303    * before overwriting any existing sockets, etc.
304    */
305   pidfile = bus_config_parser_get_pidfile (parser);
306   if (pidfile != NULL)
307     {
308       DBusString u;
309       DBusStat stbuf;
310
311       _dbus_string_init_const (&u, pidfile);
312
313       if (_dbus_stat (&u, &stbuf, NULL))
314         {
315 #ifdef DBUS_CYGWIN
316           DBusString p;
317           long /* int */ pid;
318
319           _dbus_string_init (&p);
320           _dbus_file_get_contents(&p, &u, NULL);
321           _dbus_string_parse_int(&p, 0, &pid, NULL);
322           _dbus_string_free(&p);
323
324           if ((kill((int)pid, 0))) {
325             dbus_set_error(NULL, DBUS_ERROR_FILE_EXISTS,
326                            "pid %ld not running, removing stale pid file\n",
327                            pid);
328             _dbus_delete_file(&u, NULL);
329           } else {
330 #endif
331           dbus_set_error (error, DBUS_ERROR_FAILED,
332                                   "The pid file \"%s\" exists, if the message bus is not running, remove this file",
333                           pidfile);
334               goto failed;
335 #ifdef DBUS_CYGWIN
336           }
337 #endif
338         }
339     }
340
341   /* keep around the pid filename so we can delete it later */
342   context->pidfile = _dbus_strdup (pidfile);
343
344   /* note that type may be NULL */
345   context->type = _dbus_strdup (bus_config_parser_get_type (parser));
346   if (bus_config_parser_get_type (parser) != NULL && context->type == NULL)
347     goto oom;
348
349   user = bus_config_parser_get_user (parser);
350   if (user != NULL)
351     {
352       context->user = _dbus_strdup (user);
353       if (context->user == NULL)
354         goto oom;
355     }
356
357   /* Set up the prefix for syslog messages */
358   if (!_dbus_string_init (&log_prefix))
359     goto oom;
360   if (context->type && !strcmp (context->type, "system"))
361     {
362       if (!_dbus_string_append (&log_prefix, "[system] "))
363         goto oom;
364     }
365   else if (context->type && !strcmp (context->type, "session"))
366     {
367       DBusCredentials *credentials;
368
369       credentials = _dbus_credentials_new_from_current_process ();
370       if (!credentials)
371         goto oom;
372       if (!_dbus_string_append (&log_prefix, "[session "))
373         goto oom;
374       if (!_dbus_credentials_to_string_append (credentials, &log_prefix))
375         goto oom;
376       if (!_dbus_string_append (&log_prefix, "] "))
377         goto oom;
378       _dbus_credentials_unref (credentials);
379     }
380   if (!_dbus_string_steal_data (&log_prefix, &context->log_prefix))
381     goto oom;
382   _dbus_string_free (&log_prefix);
383
384   /* Build an array of auth mechanisms */
385
386   auth_mechanisms_list = bus_config_parser_get_mechanisms (parser);
387   len = _dbus_list_get_length (auth_mechanisms_list);
388
389   if (len > 0)
390     {
391       int i;
392
393       auth_mechanisms = dbus_new0 (char*, len + 1);
394       if (auth_mechanisms == NULL)
395         goto oom;
396
397       i = 0;
398       link = _dbus_list_get_first_link (auth_mechanisms_list);
399       while (link != NULL)
400         {
401           auth_mechanisms[i] = _dbus_strdup (link->data);
402           if (auth_mechanisms[i] == NULL)
403             goto oom;
404           link = _dbus_list_get_next_link (auth_mechanisms_list, link);
405         }
406     }
407   else
408     {
409       auth_mechanisms = NULL;
410     }
411
412   /* Listen on our addresses */
413
414   if (address)
415     {
416       DBusServer *server;
417
418       server = dbus_server_listen (_dbus_string_get_const_data(address), error);
419       if (server == NULL)
420         {
421           _DBUS_ASSERT_ERROR_IS_SET (error);
422           goto failed;
423         }
424       else if (!setup_server (context, server, auth_mechanisms, error))
425         {
426           _DBUS_ASSERT_ERROR_IS_SET (error);
427           goto failed;
428         }
429
430       if (!_dbus_list_append (&context->servers, server))
431         goto oom;
432     }
433   else
434     {
435       addresses = bus_config_parser_get_addresses (parser);
436
437       link = _dbus_list_get_first_link (addresses);
438       while (link != NULL)
439         {
440           DBusServer *server;
441
442           server = dbus_server_listen (link->data, error);
443           if (server == NULL)
444             {
445               _DBUS_ASSERT_ERROR_IS_SET (error);
446               goto failed;
447             }
448           else if (!setup_server (context, server, auth_mechanisms, error))
449             {
450               _DBUS_ASSERT_ERROR_IS_SET (error);
451               goto failed;
452             }
453
454           if (!_dbus_list_append (&context->servers, server))
455             goto oom;
456
457           link = _dbus_list_get_next_link (addresses, link);
458         }
459     }
460
461   context->fork = bus_config_parser_get_fork (parser);
462   context->syslog = bus_config_parser_get_syslog (parser);
463   context->keep_umask = bus_config_parser_get_keep_umask (parser);
464   context->allow_anonymous = bus_config_parser_get_allow_anonymous (parser);
465
466   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
467   retval = TRUE;
468
469  failed:
470   dbus_free_string_array (auth_mechanisms);
471   return retval;
472
473  oom:
474   BUS_SET_OOM (error);
475   dbus_free_string_array (auth_mechanisms);
476   return FALSE;
477 }
478
479 /* This code gets executed every time the config files
480  * are parsed: both during BusContext construction
481  * and on reloads. This function is slightly screwy
482  * since it can do a "half reload" in out-of-memory
483  * situations. Realistically, unlikely to ever matter.
484  */
485 static dbus_bool_t
486 process_config_every_time (BusContext      *context,
487                            BusConfigParser *parser,
488                            dbus_bool_t      is_reload,
489                            DBusError       *error)
490 {
491   DBusString full_address;
492   DBusList *link;
493   DBusList **dirs;
494   BusActivation *new_activation;
495   char *addr;
496   const char *servicehelper;
497   char *s;
498
499   dbus_bool_t retval;
500
501   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
502
503   addr = NULL;
504   retval = FALSE;
505
506   if (!_dbus_string_init (&full_address))
507     {
508       BUS_SET_OOM (error);
509       return FALSE;
510     }
511
512   /* get our limits and timeout lengths */
513   bus_config_parser_get_limits (parser, &context->limits);
514
515   if (context->policy)
516     bus_policy_unref (context->policy);
517   context->policy = bus_config_parser_steal_policy (parser);
518   _dbus_assert (context->policy != NULL);
519
520   /* We have to build the address backward, so that
521    * <listen> later in the config file have priority
522    */
523   link = _dbus_list_get_last_link (&context->servers);
524   while (link != NULL)
525     {
526       addr = dbus_server_get_address (link->data);
527       if (addr == NULL)
528         {
529           BUS_SET_OOM (error);
530           goto failed;
531         }
532
533       if (_dbus_string_get_length (&full_address) > 0)
534         {
535           if (!_dbus_string_append (&full_address, ";"))
536             {
537               BUS_SET_OOM (error);
538               goto failed;
539             }
540         }
541
542       if (!_dbus_string_append (&full_address, addr))
543         {
544           BUS_SET_OOM (error);
545           goto failed;
546         }
547
548       dbus_free (addr);
549       addr = NULL;
550
551       link = _dbus_list_get_prev_link (&context->servers, link);
552     }
553
554   if (is_reload)
555     dbus_free (context->address);
556
557   if (!_dbus_string_copy_data (&full_address, &context->address))
558     {
559       BUS_SET_OOM (error);
560       goto failed;
561     }
562
563   /* get the service directories */
564   dirs = bus_config_parser_get_service_dirs (parser);
565
566   /* and the service helper */
567   servicehelper = bus_config_parser_get_servicehelper (parser);
568
569   s = _dbus_strdup(servicehelper);
570   if (s == NULL && servicehelper != NULL)
571     {
572       BUS_SET_OOM (error);
573       goto failed;
574     }
575   else
576     {
577       dbus_free(context->servicehelper);
578       context->servicehelper = s;
579     }
580
581   /* Create activation subsystem */
582   if (context->activation)
583     {
584       if (!bus_activation_reload (context->activation, &full_address, dirs, error))
585         goto failed;
586     }
587   else
588     {
589       context->activation = bus_activation_new (context, &full_address, dirs, error);
590     }
591
592   if (context->activation == NULL)
593     {
594       _DBUS_ASSERT_ERROR_IS_SET (error);
595       goto failed;
596     }
597
598   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
599   retval = TRUE;
600
601  failed:
602   _dbus_string_free (&full_address);
603
604   if (addr)
605     dbus_free (addr);
606
607   return retval;
608 }
609
610 static dbus_bool_t
611 list_concat_new (DBusList **a,
612                  DBusList **b,
613                  DBusList **result)
614 {
615   DBusList *link;
616
617   *result = NULL;
618
619   link = _dbus_list_get_first_link (a);
620   for (link = _dbus_list_get_first_link (a); link; link = _dbus_list_get_next_link (a, link))
621     {
622       if (!_dbus_list_append (result, link->data))
623         goto oom;
624     }
625   for (link = _dbus_list_get_first_link (b); link; link = _dbus_list_get_next_link (b, link))
626     {
627       if (!_dbus_list_append (result, link->data))
628         goto oom;
629     }
630
631   return TRUE;
632 oom:
633   _dbus_list_clear (result);
634   return FALSE;
635 }
636
637 static dbus_bool_t
638 process_config_postinit (BusContext      *context,
639                          BusConfigParser *parser,
640                          DBusError       *error)
641 {
642   DBusHashTable *service_context_table;
643   DBusList *watched_dirs = NULL;
644
645   service_context_table = bus_config_parser_steal_service_context_table (parser);
646   if (!bus_registry_set_service_context_table (context->registry,
647                                                service_context_table))
648     {
649       BUS_SET_OOM (error);
650       return FALSE;
651     }
652
653   _dbus_hash_table_unref (service_context_table);
654
655   /* We need to monitor both the configuration directories and directories
656    * containing .service files.
657    */
658   if (!list_concat_new (bus_config_parser_get_conf_dirs (parser),
659                         bus_config_parser_get_service_dirs (parser),
660                         &watched_dirs))
661     {
662       BUS_SET_OOM (error);
663       return FALSE;
664     }
665
666   bus_set_watched_dirs (context, &watched_dirs);
667
668   _dbus_list_clear (&watched_dirs);
669
670   return TRUE;
671 }
672
673 BusContext*
674 bus_context_new (const DBusString *config_file,
675                  ForceForkSetting  force_fork,
676                  DBusPipe         *print_addr_pipe,
677                  DBusPipe         *print_pid_pipe,
678                  const DBusString *address,
679                  dbus_bool_t      systemd_activation,
680                  DBusError        *error)
681 {
682   DBusString log_prefix;
683   BusContext *context;
684   BusConfigParser *parser;
685
686   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
687
688   context = NULL;
689   parser = NULL;
690
691   if (!dbus_server_allocate_data_slot (&server_data_slot))
692     {
693       BUS_SET_OOM (error);
694       return NULL;
695     }
696
697   context = dbus_new0 (BusContext, 1);
698   if (context == NULL)
699     {
700       BUS_SET_OOM (error);
701       goto failed;
702     }
703   context->refcount = 1;
704
705   _dbus_generate_uuid (&context->uuid);
706
707   if (!_dbus_string_copy_data (config_file, &context->config_file))
708     {
709       BUS_SET_OOM (error);
710       goto failed;
711     }
712
713   context->loop = _dbus_loop_new ();
714   if (context->loop == NULL)
715     {
716       BUS_SET_OOM (error);
717       goto failed;
718     }
719
720   context->registry = bus_registry_new (context);
721   if (context->registry == NULL)
722     {
723       BUS_SET_OOM (error);
724       goto failed;
725     }
726
727   parser = bus_config_load (config_file, TRUE, NULL, error);
728   if (parser == NULL)
729     {
730       _DBUS_ASSERT_ERROR_IS_SET (error);
731       goto failed;
732     }
733
734   if (!process_config_first_time_only (context, parser, address, systemd_activation, error))
735     {
736       _DBUS_ASSERT_ERROR_IS_SET (error);
737       goto failed;
738     }
739   if (!process_config_every_time (context, parser, FALSE, error))
740     {
741       _DBUS_ASSERT_ERROR_IS_SET (error);
742       goto failed;
743     }
744
745   /* we need another ref of the server data slot for the context
746    * to own
747    */
748   if (!dbus_server_allocate_data_slot (&server_data_slot))
749     _dbus_assert_not_reached ("second ref of server data slot failed");
750
751   /* Note that we don't know whether the print_addr_pipe is
752    * one of the sockets we're using to listen on, or some
753    * other random thing. But I think the answer is "don't do
754    * that then"
755    */
756   if (print_addr_pipe != NULL && _dbus_pipe_is_valid (print_addr_pipe))
757     {
758       DBusString addr;
759       const char *a = bus_context_get_address (context);
760       int bytes;
761
762       _dbus_assert (a != NULL);
763       if (!_dbus_string_init (&addr))
764         {
765           BUS_SET_OOM (error);
766           goto failed;
767         }
768
769       if (!_dbus_string_append (&addr, a) ||
770           !_dbus_string_append (&addr, "\n"))
771         {
772           _dbus_string_free (&addr);
773           BUS_SET_OOM (error);
774           goto failed;
775         }
776
777       bytes = _dbus_string_get_length (&addr);
778       if (_dbus_pipe_write (print_addr_pipe, &addr, 0, bytes, error) != bytes)
779         {
780           /* pipe write returns an error on failure but not short write */
781           if (error != NULL && !dbus_error_is_set (error))
782             {
783               dbus_set_error (error, DBUS_ERROR_FAILED,
784                               "Printing message bus address: did not write all bytes\n");
785             }
786           _dbus_string_free (&addr);
787           goto failed;
788         }
789
790       if (!_dbus_pipe_is_stdout_or_stderr (print_addr_pipe))
791         _dbus_pipe_close (print_addr_pipe, NULL);
792
793       _dbus_string_free (&addr);
794     }
795
796   context->connections = bus_connections_new (context);
797   if (context->connections == NULL)
798     {
799       BUS_SET_OOM (error);
800       goto failed;
801     }
802
803   context->matchmaker = bus_matchmaker_new ();
804   if (context->matchmaker == NULL)
805     {
806       BUS_SET_OOM (error);
807       goto failed;
808     }
809
810   /* check user before we fork */
811   if (context->user != NULL)
812     {
813       if (!_dbus_verify_daemon_user (context->user))
814         {
815           dbus_set_error (error, DBUS_ERROR_FAILED,
816                           "Could not get UID and GID for username \"%s\"",
817                           context->user);
818           goto failed;
819         }
820     }
821
822   /* Now become a daemon if appropriate and write out pid file in any case */
823   {
824     DBusString u;
825
826     if (context->pidfile)
827       _dbus_string_init_const (&u, context->pidfile);
828
829     if ((force_fork != FORK_NEVER && context->fork) || force_fork == FORK_ALWAYS)
830       {
831         _dbus_verbose ("Forking and becoming daemon\n");
832
833         if (!_dbus_become_daemon (context->pidfile ? &u : NULL,
834                                   print_pid_pipe,
835                                   error,
836                                   context->keep_umask))
837           {
838             _DBUS_ASSERT_ERROR_IS_SET (error);
839             goto failed;
840           }
841       }
842     else
843       {
844         _dbus_verbose ("Fork not requested\n");
845
846         /* Need to write PID file and to PID pipe for ourselves,
847          * not for the child process. This is a no-op if the pidfile
848          * is NULL and print_pid_pipe is NULL.
849          */
850         if (!_dbus_write_pid_to_file_and_pipe (context->pidfile ? &u : NULL,
851                                                print_pid_pipe,
852                                                _dbus_getpid (),
853                                                error))
854           {
855             _DBUS_ASSERT_ERROR_IS_SET (error);
856             goto failed;
857           }
858       }
859   }
860
861   if (print_pid_pipe && _dbus_pipe_is_valid (print_pid_pipe) &&
862       !_dbus_pipe_is_stdout_or_stderr (print_pid_pipe))
863     _dbus_pipe_close (print_pid_pipe, NULL);
864
865   if (!bus_selinux_full_init ())
866     {
867       bus_context_log (context, DBUS_SYSTEM_LOG_FATAL, "SELinux enabled but AVC initialization failed; check system log\n");
868     }
869
870   if (!process_config_postinit (context, parser, error))
871     {
872       _DBUS_ASSERT_ERROR_IS_SET (error);
873       goto failed;
874     }
875
876   if (parser != NULL)
877     {
878       bus_config_parser_unref (parser);
879       parser = NULL;
880     }
881
882   /* Here we change our credentials if required,
883    * as soon as we've set up our sockets and pidfile
884    */
885   if (context->user != NULL)
886     {
887       if (!_dbus_change_to_daemon_user (context->user, error))
888         {
889           _DBUS_ASSERT_ERROR_IS_SET (error);
890           goto failed;
891         }
892
893 #ifdef HAVE_SELINUX
894       /* FIXME - why not just put this in full_init() below? */
895       bus_selinux_audit_init ();
896 #endif
897     }
898
899   dbus_server_free_data_slot (&server_data_slot);
900
901   return context;
902
903  failed:
904   if (parser != NULL)
905     bus_config_parser_unref (parser);
906   if (context != NULL)
907     bus_context_unref (context);
908
909   if (server_data_slot >= 0)
910     dbus_server_free_data_slot (&server_data_slot);
911
912   return NULL;
913 }
914
915 dbus_bool_t
916 bus_context_get_id (BusContext       *context,
917                     DBusString       *uuid)
918 {
919   return _dbus_uuid_encode (&context->uuid, uuid);
920 }
921
922 dbus_bool_t
923 bus_context_reload_config (BusContext *context,
924                            DBusError  *error)
925 {
926   BusConfigParser *parser;
927   DBusString config_file;
928   dbus_bool_t ret;
929
930   /* Flush the user database cache */
931   _dbus_flush_caches ();
932
933   ret = FALSE;
934   _dbus_string_init_const (&config_file, context->config_file);
935   parser = bus_config_load (&config_file, TRUE, NULL, error);
936   if (parser == NULL)
937     {
938       _DBUS_ASSERT_ERROR_IS_SET (error);
939       goto failed;
940     }
941
942   if (!process_config_every_time (context, parser, TRUE, error))
943     {
944       _DBUS_ASSERT_ERROR_IS_SET (error);
945       goto failed;
946     }
947   if (!process_config_postinit (context, parser, error))
948     {
949       _DBUS_ASSERT_ERROR_IS_SET (error);
950       goto failed;
951     }
952   ret = TRUE;
953
954   bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Reloaded configuration");
955  failed:
956   if (!ret)
957     bus_context_log (context, DBUS_SYSTEM_LOG_INFO, "Unable to reload configuration: %s", error->message);
958   if (parser != NULL)
959     bus_config_parser_unref (parser);
960   return ret;
961 }
962
963 static void
964 shutdown_server (BusContext *context,
965                  DBusServer *server)
966 {
967   if (server == NULL ||
968       !dbus_server_get_is_connected (server))
969     return;
970
971   if (!dbus_server_set_watch_functions (server,
972                                         NULL, NULL, NULL,
973                                         context,
974                                         NULL))
975     _dbus_assert_not_reached ("setting watch functions to NULL failed");
976
977   if (!dbus_server_set_timeout_functions (server,
978                                           NULL, NULL, NULL,
979                                           context,
980                                           NULL))
981     _dbus_assert_not_reached ("setting timeout functions to NULL failed");
982
983   dbus_server_disconnect (server);
984 }
985
986 void
987 bus_context_shutdown (BusContext  *context)
988 {
989   DBusList *link;
990
991   link = _dbus_list_get_first_link (&context->servers);
992   while (link != NULL)
993     {
994       shutdown_server (context, link->data);
995
996       link = _dbus_list_get_next_link (&context->servers, link);
997     }
998 }
999
1000 BusContext *
1001 bus_context_ref (BusContext *context)
1002 {
1003   _dbus_assert (context->refcount > 0);
1004   context->refcount += 1;
1005
1006   return context;
1007 }
1008
1009 void
1010 bus_context_unref (BusContext *context)
1011 {
1012   _dbus_assert (context->refcount > 0);
1013   context->refcount -= 1;
1014
1015   if (context->refcount == 0)
1016     {
1017       DBusList *link;
1018
1019       _dbus_verbose ("Finalizing bus context %p\n", context);
1020
1021       bus_context_shutdown (context);
1022
1023       if (context->connections)
1024         {
1025           bus_connections_unref (context->connections);
1026           context->connections = NULL;
1027         }
1028
1029       if (context->registry)
1030         {
1031           bus_registry_unref (context->registry);
1032           context->registry = NULL;
1033         }
1034
1035       if (context->activation)
1036         {
1037           bus_activation_unref (context->activation);
1038           context->activation = NULL;
1039         }
1040
1041       link = _dbus_list_get_first_link (&context->servers);
1042       while (link != NULL)
1043         {
1044           dbus_server_unref (link->data);
1045
1046           link = _dbus_list_get_next_link (&context->servers, link);
1047         }
1048       _dbus_list_clear (&context->servers);
1049
1050       if (context->policy)
1051         {
1052           bus_policy_unref (context->policy);
1053           context->policy = NULL;
1054         }
1055
1056       if (context->loop)
1057         {
1058           _dbus_loop_unref (context->loop);
1059           context->loop = NULL;
1060         }
1061
1062       if (context->matchmaker)
1063         {
1064           bus_matchmaker_unref (context->matchmaker);
1065           context->matchmaker = NULL;
1066         }
1067
1068       dbus_free (context->config_file);
1069       dbus_free (context->log_prefix);
1070       dbus_free (context->type);
1071       dbus_free (context->address);
1072       dbus_free (context->user);
1073       dbus_free (context->servicehelper);
1074
1075       if (context->pidfile)
1076         {
1077           DBusString u;
1078           _dbus_string_init_const (&u, context->pidfile);
1079
1080           /* Deliberately ignore errors here, since there's not much
1081            * we can do about it, and we're exiting anyways.
1082            */
1083           _dbus_delete_file (&u, NULL);
1084
1085           dbus_free (context->pidfile);
1086         }
1087       dbus_free (context);
1088
1089       dbus_server_free_data_slot (&server_data_slot);
1090     }
1091 }
1092
1093 /* type may be NULL */
1094 const char*
1095 bus_context_get_type (BusContext *context)
1096 {
1097   return context->type;
1098 }
1099
1100 const char*
1101 bus_context_get_address (BusContext *context)
1102 {
1103   return context->address;
1104 }
1105
1106 const char*
1107 bus_context_get_servicehelper (BusContext *context)
1108 {
1109   return context->servicehelper;
1110 }
1111
1112 dbus_bool_t
1113 bus_context_get_systemd_activation (BusContext *context)
1114 {
1115   return context->systemd_activation;
1116 }
1117
1118 BusRegistry*
1119 bus_context_get_registry (BusContext  *context)
1120 {
1121   return context->registry;
1122 }
1123
1124 BusConnections*
1125 bus_context_get_connections (BusContext  *context)
1126 {
1127   return context->connections;
1128 }
1129
1130 BusActivation*
1131 bus_context_get_activation (BusContext  *context)
1132 {
1133   return context->activation;
1134 }
1135
1136 BusMatchmaker*
1137 bus_context_get_matchmaker (BusContext  *context)
1138 {
1139   return context->matchmaker;
1140 }
1141
1142 DBusLoop*
1143 bus_context_get_loop (BusContext *context)
1144 {
1145   return context->loop;
1146 }
1147
1148 dbus_bool_t
1149 bus_context_allow_unix_user (BusContext   *context,
1150                              unsigned long uid)
1151 {
1152   return bus_policy_allow_unix_user (context->policy,
1153                                      uid);
1154 }
1155
1156 /* For now this is never actually called because the default
1157  * DBusConnection behavior of 'same user that owns the bus can connect'
1158  * is all it would do.
1159  */
1160 dbus_bool_t
1161 bus_context_allow_windows_user (BusContext       *context,
1162                                 const char       *windows_sid)
1163 {
1164   return bus_policy_allow_windows_user (context->policy,
1165                                         windows_sid);
1166 }
1167
1168 BusPolicy *
1169 bus_context_get_policy (BusContext *context)
1170 {
1171   return context->policy;
1172 }
1173
1174 BusClientPolicy*
1175 bus_context_create_client_policy (BusContext      *context,
1176                                   DBusConnection  *connection,
1177                                   DBusError       *error)
1178 {
1179   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1180   return bus_policy_create_client_policy (context->policy, connection,
1181                                           error);
1182 }
1183
1184 int
1185 bus_context_get_activation_timeout (BusContext *context)
1186 {
1187
1188   return context->limits.activation_timeout;
1189 }
1190
1191 int
1192 bus_context_get_auth_timeout (BusContext *context)
1193 {
1194   return context->limits.auth_timeout;
1195 }
1196
1197 int
1198 bus_context_get_max_completed_connections (BusContext *context)
1199 {
1200   return context->limits.max_completed_connections;
1201 }
1202
1203 int
1204 bus_context_get_max_incomplete_connections (BusContext *context)
1205 {
1206   return context->limits.max_incomplete_connections;
1207 }
1208
1209 int
1210 bus_context_get_max_connections_per_user (BusContext *context)
1211 {
1212   return context->limits.max_connections_per_user;
1213 }
1214
1215 int
1216 bus_context_get_max_pending_activations (BusContext *context)
1217 {
1218   return context->limits.max_pending_activations;
1219 }
1220
1221 int
1222 bus_context_get_max_services_per_connection (BusContext *context)
1223 {
1224   return context->limits.max_services_per_connection;
1225 }
1226
1227 int
1228 bus_context_get_max_match_rules_per_connection (BusContext *context)
1229 {
1230   return context->limits.max_match_rules_per_connection;
1231 }
1232
1233 int
1234 bus_context_get_max_replies_per_connection (BusContext *context)
1235 {
1236   return context->limits.max_replies_per_connection;
1237 }
1238
1239 int
1240 bus_context_get_reply_timeout (BusContext *context)
1241 {
1242   return context->limits.reply_timeout;
1243 }
1244
1245 void
1246 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...) _DBUS_GNUC_PRINTF (3, 4);
1247
1248 void
1249 bus_context_log (BusContext *context, DBusSystemLogSeverity severity, const char *msg, ...)
1250 {
1251   va_list args;
1252
1253   if (!context->syslog)
1254     return;
1255
1256   va_start (args, msg);
1257
1258   if (context->log_prefix)
1259     {
1260       DBusString full_msg;
1261
1262       if (!_dbus_string_init (&full_msg))
1263         goto out;
1264       if (!_dbus_string_append (&full_msg, context->log_prefix))
1265         goto oom_out;
1266       if (!_dbus_string_append_printf_valist (&full_msg, msg, args))
1267         goto oom_out;
1268
1269       _dbus_system_log (severity, "%s", _dbus_string_get_const_data (&full_msg));
1270     oom_out:
1271       _dbus_string_free (&full_msg);
1272     }
1273   else
1274     _dbus_system_logv (severity, msg, args);
1275
1276 out:
1277   va_end (args);
1278 }
1279
1280 /*
1281  * addressed_recipient is the recipient specified in the message.
1282  *
1283  * proposed_recipient is the recipient we're considering sending
1284  * to right this second, and may be an eavesdropper.
1285  *
1286  * sender is the sender of the message.
1287  *
1288  * NULL for proposed_recipient or sender definitely means the bus driver.
1289  *
1290  * NULL for addressed_recipient may mean the bus driver, or may mean
1291  * no destination was specified in the message (e.g. a signal).
1292  */
1293 dbus_bool_t
1294 bus_context_check_security_policy (BusContext     *context,
1295                                    BusTransaction *transaction,
1296                                    DBusConnection *sender,
1297                                    DBusConnection *addressed_recipient,
1298                                    DBusConnection *proposed_recipient,
1299                                    DBusMessage    *message,
1300                                    DBusError      *error)
1301 {
1302   const char *dest;
1303   BusClientPolicy *sender_policy;
1304   BusClientPolicy *recipient_policy;
1305   dbus_int32_t toggles;
1306   dbus_bool_t log;
1307   int type;
1308   dbus_bool_t requested_reply;
1309   const char *sender_name;
1310   const char *sender_loginfo;
1311   const char *proposed_recipient_loginfo;
1312
1313   type = dbus_message_get_type (message);
1314   dest = dbus_message_get_destination (message);
1315
1316   /* dispatch.c was supposed to ensure these invariants */
1317   _dbus_assert (dest != NULL ||
1318                 type == DBUS_MESSAGE_TYPE_SIGNAL ||
1319                 (sender == NULL && !bus_connection_is_active (proposed_recipient)));
1320   _dbus_assert (type == DBUS_MESSAGE_TYPE_SIGNAL ||
1321                 addressed_recipient != NULL ||
1322                 strcmp (dest, DBUS_SERVICE_DBUS) == 0);
1323
1324   /* Used in logging below */
1325   if (sender != NULL)
1326     {
1327       sender_name = bus_connection_get_name (sender);
1328       sender_loginfo = bus_connection_get_loginfo (sender);
1329     }
1330   else
1331     {
1332       sender_name = NULL;
1333       sender_loginfo = "(bus)";
1334     }
1335
1336   if (proposed_recipient != NULL)
1337     proposed_recipient_loginfo = bus_connection_get_loginfo (proposed_recipient);
1338   else
1339     proposed_recipient_loginfo = "bus";
1340
1341   switch (type)
1342     {
1343     case DBUS_MESSAGE_TYPE_METHOD_CALL:
1344     case DBUS_MESSAGE_TYPE_SIGNAL:
1345     case DBUS_MESSAGE_TYPE_METHOD_RETURN:
1346     case DBUS_MESSAGE_TYPE_ERROR:
1347       break;
1348
1349     default:
1350       _dbus_verbose ("security check disallowing message of unknown type %d\n",
1351                      type);
1352
1353       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1354                       "Message bus will not accept messages of unknown type\n");
1355
1356       return FALSE;
1357     }
1358
1359   requested_reply = FALSE;
1360
1361   if (sender != NULL)
1362     {
1363       /* First verify the SELinux access controls.  If allowed then
1364        * go on with the standard checks.
1365        */
1366       if (!bus_selinux_allows_send (sender, proposed_recipient,
1367                                     dbus_message_type_to_string (dbus_message_get_type (message)),
1368                                     dbus_message_get_interface (message),
1369                                     dbus_message_get_member (message),
1370                                     dbus_message_get_error_name (message),
1371                                     dest ? dest : DBUS_SERVICE_DBUS, error))
1372         {
1373           if (error != NULL && !dbus_error_is_set (error))
1374             {
1375               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1376                               "An SELinux policy prevents this sender "
1377                               "from sending this message to this recipient "
1378                               "(rejected message had sender \"%s\" interface \"%s\" "
1379                               "member \"%s\" error name \"%s\" destination \"%s\")",
1380                               sender_name ? sender_name : "(unset)",
1381                               dbus_message_get_interface (message) ?
1382                               dbus_message_get_interface (message) : "(unset)",
1383                               dbus_message_get_member (message) ?
1384                               dbus_message_get_member (message) : "(unset)",
1385                               dbus_message_get_error_name (message) ?
1386                               dbus_message_get_error_name (message) : "(unset)",
1387                               dest ? dest : DBUS_SERVICE_DBUS);
1388               _dbus_verbose ("SELinux security check denying send to service\n");
1389             }
1390
1391           return FALSE;
1392         }
1393
1394       if (bus_connection_is_active (sender))
1395         {
1396           sender_policy = bus_connection_get_policy (sender);
1397           _dbus_assert (sender_policy != NULL);
1398
1399           /* Fill in requested_reply variable with TRUE if this is a
1400            * reply and the reply was pending.
1401            */
1402           if (dbus_message_get_reply_serial (message) != 0)
1403             {
1404               if (proposed_recipient != NULL /* not to the bus driver */ &&
1405                   addressed_recipient == proposed_recipient /* not eavesdropping */)
1406                 {
1407                   DBusError error2;
1408
1409                   dbus_error_init (&error2);
1410                   requested_reply = bus_connections_check_reply (bus_connection_get_connections (sender),
1411                                                                  transaction,
1412                                                                  sender, addressed_recipient, message,
1413                                                                  &error2);
1414                   if (dbus_error_is_set (&error2))
1415                     {
1416                       dbus_move_error (&error2, error);
1417                       return FALSE;
1418                     }
1419                 }
1420             }
1421         }
1422       else
1423         {
1424           /* Policy for inactive connections is that they can only send
1425            * the hello message to the bus driver
1426            */
1427           if (proposed_recipient == NULL &&
1428               dbus_message_is_method_call (message,
1429                                            DBUS_INTERFACE_DBUS,
1430                                            "Hello"))
1431             {
1432               _dbus_verbose ("security check allowing %s message\n",
1433                              "Hello");
1434               return TRUE;
1435             }
1436           else
1437             {
1438               _dbus_verbose ("security check disallowing non-%s message\n",
1439                              "Hello");
1440
1441               dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED,
1442                               "Client tried to send a message other than %s without being registered",
1443                               "Hello");
1444
1445               return FALSE;
1446             }
1447         }
1448     }
1449   else
1450     {
1451       sender_policy = NULL;
1452
1453       /* If the sender is the bus driver, we assume any reply was a
1454        * requested reply as bus driver won't send bogus ones
1455        */
1456       if (addressed_recipient == proposed_recipient /* not eavesdropping */ &&
1457           dbus_message_get_reply_serial (message) != 0)
1458         requested_reply = TRUE;
1459     }
1460
1461   _dbus_assert ((sender != NULL && sender_policy != NULL) ||
1462                 (sender == NULL && sender_policy == NULL));
1463
1464   if (proposed_recipient != NULL)
1465     {
1466       /* only the bus driver can send to an inactive recipient (as it
1467        * owns no services, so other apps can't address it). Inactive
1468        * recipients can receive any message.
1469        */
1470       if (bus_connection_is_active (proposed_recipient))
1471         {
1472           recipient_policy = bus_connection_get_policy (proposed_recipient);
1473           _dbus_assert (recipient_policy != NULL);
1474         }
1475       else if (sender == NULL)
1476         {
1477           _dbus_verbose ("security check using NULL recipient policy for message from bus\n");
1478           recipient_policy = NULL;
1479         }
1480       else
1481         {
1482           _dbus_assert_not_reached ("a message was somehow sent to an inactive recipient from a source other than the message bus\n");
1483           recipient_policy = NULL;
1484         }
1485     }
1486   else
1487     recipient_policy = NULL;
1488
1489   _dbus_assert ((proposed_recipient != NULL && recipient_policy != NULL) ||
1490                 (proposed_recipient != NULL && sender == NULL && recipient_policy == NULL) ||
1491                 (proposed_recipient == NULL && recipient_policy == NULL));
1492
1493   log = FALSE;
1494   if (sender_policy &&
1495       !bus_client_policy_check_can_send (sender_policy,
1496                                          context->registry,
1497                                          requested_reply,
1498                                          proposed_recipient,
1499                                          message, &toggles, &log))
1500     {
1501       const char *msg = "Rejected send message, %d matched rules; "
1502                         "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))";
1503
1504       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1505                       toggles,
1506                       dbus_message_type_to_string (dbus_message_get_type (message)),
1507                       sender_name ? sender_name : "(unset)",
1508                       sender_loginfo,
1509                       dbus_message_get_interface (message) ?
1510                       dbus_message_get_interface (message) : "(unset)",
1511                       dbus_message_get_member (message) ?
1512                       dbus_message_get_member (message) : "(unset)",
1513                       dbus_message_get_error_name (message) ?
1514                       dbus_message_get_error_name (message) : "(unset)",
1515                       requested_reply,
1516                       dest ? dest : DBUS_SERVICE_DBUS,
1517                       proposed_recipient_loginfo);
1518       /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1519       if (addressed_recipient == proposed_recipient)
1520         bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1521                                   toggles,
1522                                   dbus_message_type_to_string (dbus_message_get_type (message)),
1523                                   sender_name ? sender_name : "(unset)",
1524                                   sender_loginfo,
1525                                   dbus_message_get_interface (message) ?
1526                                   dbus_message_get_interface (message) : "(unset)",
1527                                   dbus_message_get_member (message) ?
1528                                   dbus_message_get_member (message) : "(unset)",
1529                                   dbus_message_get_error_name (message) ?
1530                                   dbus_message_get_error_name (message) : "(unset)",
1531                                   requested_reply,
1532                                   dest ? dest : DBUS_SERVICE_DBUS,
1533                                   proposed_recipient_loginfo);
1534       _dbus_verbose ("security policy disallowing message due to sender policy\n");
1535       return FALSE;
1536     }
1537
1538   if (log)
1539     bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY,
1540                               "Would reject message, %d matched rules; "
1541                               "type=\"%s\", sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" requested_reply=%d destination=\"%s\" (%s))",
1542                               toggles,
1543                               dbus_message_type_to_string (dbus_message_get_type (message)),
1544                               sender_name ? sender_name : "(unset)",
1545                               sender_loginfo,
1546                               dbus_message_get_interface (message) ?
1547                               dbus_message_get_interface (message) : "(unset)",
1548                               dbus_message_get_member (message) ?
1549                               dbus_message_get_member (message) : "(unset)",
1550                               dbus_message_get_error_name (message) ?
1551                               dbus_message_get_error_name (message) : "(unset)",
1552                               requested_reply,
1553                               dest ? dest : DBUS_SERVICE_DBUS,
1554                               proposed_recipient_loginfo);
1555
1556   if (recipient_policy &&
1557       !bus_client_policy_check_can_receive (recipient_policy,
1558                                             context->registry,
1559                                             requested_reply,
1560                                             sender,
1561                                             addressed_recipient, proposed_recipient,
1562                                             message, &toggles))
1563     {
1564       const char *msg = "Rejected receive message, %d matched rules; "
1565                         "type=\"%s\" sender=\"%s\" (%s) interface=\"%s\" member=\"%s\" error name=\"%s\" reply serial=%u requested_reply=%d destination=\"%s\" (%s))";
1566
1567       dbus_set_error (error, DBUS_ERROR_ACCESS_DENIED, msg,
1568                       toggles,
1569                       dbus_message_type_to_string (dbus_message_get_type (message)),
1570                       sender_name ? sender_name : "(unset)",
1571                       sender_loginfo,
1572                       dbus_message_get_interface (message) ?
1573                       dbus_message_get_interface (message) : "(unset)",
1574                       dbus_message_get_member (message) ?
1575                       dbus_message_get_member (message) : "(unset)",
1576                       dbus_message_get_error_name (message) ?
1577                       dbus_message_get_error_name (message) : "(unset)",
1578                       dbus_message_get_reply_serial (message),
1579                       requested_reply,
1580                       dest ? dest : DBUS_SERVICE_DBUS,
1581                       proposed_recipient_loginfo);
1582       /* Needs to be duplicated to avoid calling malloc and having to handle OOM */
1583       if (addressed_recipient == proposed_recipient)
1584         bus_context_log (context, DBUS_SYSTEM_LOG_SECURITY, msg,
1585                                   toggles,
1586                                   dbus_message_type_to_string (dbus_message_get_type (message)),
1587                                   sender_name ? sender_name : "(unset)",
1588                                   sender_loginfo,
1589                                   dbus_message_get_interface (message) ?
1590                                   dbus_message_get_interface (message) : "(unset)",
1591                                   dbus_message_get_member (message) ?
1592                                   dbus_message_get_member (message) : "(unset)",
1593                                   dbus_message_get_error_name (message) ?
1594                                   dbus_message_get_error_name (message) : "(unset)",
1595                                   dbus_message_get_reply_serial (message),
1596                                   requested_reply,
1597                                   dest ? dest : DBUS_SERVICE_DBUS,
1598                                   proposed_recipient_loginfo);
1599       _dbus_verbose ("security policy disallowing message due to recipient policy\n");
1600       return FALSE;
1601     }
1602
1603   /* See if limits on size have been exceeded */
1604   if (proposed_recipient &&
1605       ((dbus_connection_get_outgoing_size (proposed_recipient) > context->limits.max_outgoing_bytes) ||
1606        (dbus_connection_get_outgoing_unix_fds (proposed_recipient) > context->limits.max_outgoing_unix_fds)))
1607     {
1608       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1609                       "The destination service \"%s\" has a full message queue",
1610                       dest ? dest : (proposed_recipient ?
1611                                      bus_connection_get_name (proposed_recipient) :
1612                                      DBUS_SERVICE_DBUS));
1613       _dbus_verbose ("security policy disallowing message due to full message queue\n");
1614       return FALSE;
1615     }
1616
1617   /* Record that we will allow a reply here in the future (don't
1618    * bother if the recipient is the bus or this is an eavesdropping
1619    * connection). Only the addressed recipient may reply.
1620    */
1621   if (type == DBUS_MESSAGE_TYPE_METHOD_CALL &&
1622       sender &&
1623       addressed_recipient &&
1624       addressed_recipient == proposed_recipient && /* not eavesdropping */
1625       !bus_connections_expect_reply (bus_connection_get_connections (sender),
1626                                      transaction,
1627                                      sender, addressed_recipient,
1628                                      message, error))
1629     {
1630       _dbus_verbose ("Failed to record reply expectation or problem with the message expecting a reply\n");
1631       return FALSE;
1632     }
1633
1634   _dbus_verbose ("security policy allowing message\n");
1635   return TRUE;
1636 }