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