Merge branch 'leak-fixes-33126'
[platform/upstream/dbus.git] / bus / activation.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* activation.c  Activation of services
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2003  Red Hat, Inc.
6  * Copyright (C) 2004  Imendio HB
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
23  *
24  */
25
26 #include <config.h>
27 #include "activation.h"
28 #include "activation-exit-codes.h"
29 #include "desktop-file.h"
30 #include "dispatch.h"
31 #include "services.h"
32 #include "test.h"
33 #include "utils.h"
34 #include <dbus/dbus-internals.h>
35 #include <dbus/dbus-hash.h>
36 #include <dbus/dbus-list.h>
37 #include <dbus/dbus-shell.h>
38 #include <dbus/dbus-spawn.h>
39 #include <dbus/dbus-timeout.h>
40 #include <dbus/dbus-sysdeps.h>
41 #ifdef HAVE_ERRNO_H
42 #include <errno.h>
43 #endif
44
45 struct BusActivation
46 {
47   int refcount;
48   DBusHashTable *entries;
49   DBusHashTable *pending_activations;
50   char *server_address;
51   BusContext *context;
52   int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
53                               * i.e. number of pending activation requests, not pending
54                               * activations per se
55                               */
56   DBusHashTable *directories;
57   DBusHashTable *environment;
58 };
59
60 typedef struct
61 {
62   int refcount;
63   char *dir_c;
64   DBusHashTable *entries;
65 } BusServiceDirectory;
66
67 typedef struct
68 {
69   int refcount;
70   char *name;
71   char *exec;
72   char *user;
73   char *systemd_service;
74   unsigned long mtime;
75   BusServiceDirectory *s_dir;
76   char *filename;
77 } BusActivationEntry;
78
79 typedef struct BusPendingActivationEntry BusPendingActivationEntry;
80
81 struct BusPendingActivationEntry
82 {
83   DBusMessage *activation_message;
84   DBusConnection *connection;
85
86   dbus_bool_t auto_activation;
87 };
88
89 typedef struct
90 {
91   int refcount;
92   BusActivation *activation;
93   char *service_name;
94   char *exec;
95   char *systemd_service;
96   DBusList *entries;
97   int n_entries;
98   DBusBabysitter *babysitter;
99   DBusTimeout *timeout;
100   unsigned int timeout_added : 1;
101 } BusPendingActivation;
102
103 #if 0
104 static BusServiceDirectory *
105 bus_service_directory_ref (BusServiceDirectory *dir)
106 {
107   _dbus_assert (dir->refcount);
108
109   dir->refcount++;
110
111   return dir;
112 }
113 #endif
114
115 static void
116 bus_service_directory_unref (BusServiceDirectory *dir)
117 {
118   if (dir == NULL)
119     return;
120
121   _dbus_assert (dir->refcount > 0);
122   dir->refcount--;
123
124   if (dir->refcount > 0)
125     return;
126
127   if (dir->entries)
128     _dbus_hash_table_unref (dir->entries);
129
130   dbus_free (dir->dir_c);
131   dbus_free (dir);
132 }
133
134 static void
135 bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
136 {
137   if (entry->activation_message)
138     dbus_message_unref (entry->activation_message);
139
140   if (entry->connection)
141     dbus_connection_unref (entry->connection);
142
143   dbus_free (entry);
144 }
145
146 static void
147 handle_timeout_callback (DBusTimeout   *timeout,
148                          void          *data)
149 {
150   BusPendingActivation *pending_activation = data;
151
152   while (!dbus_timeout_handle (pending_activation->timeout))
153     _dbus_wait_for_memory ();
154 }
155
156 static BusPendingActivation *
157 bus_pending_activation_ref (BusPendingActivation *pending_activation)
158 {
159   _dbus_assert (pending_activation->refcount > 0);
160   pending_activation->refcount += 1;
161
162   return pending_activation;
163 }
164
165 static void
166 bus_pending_activation_unref (BusPendingActivation *pending_activation)
167 {
168   DBusList *link;
169
170   if (pending_activation == NULL) /* hash table requires this */
171     return;
172
173   _dbus_assert (pending_activation->refcount > 0);
174   pending_activation->refcount -= 1;
175
176   if (pending_activation->refcount > 0)
177     return;
178
179   if (pending_activation->timeout_added)
180     {
181       _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
182                                  pending_activation->timeout,
183                                  handle_timeout_callback, pending_activation);
184       pending_activation->timeout_added = FALSE;
185     }
186
187   if (pending_activation->timeout)
188     _dbus_timeout_unref (pending_activation->timeout);
189
190   if (pending_activation->babysitter)
191     {
192       if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
193                                                  NULL, NULL, NULL,
194                                                  pending_activation->babysitter,
195                                                  NULL))
196         _dbus_assert_not_reached ("setting watch functions to NULL failed");
197
198       _dbus_babysitter_unref (pending_activation->babysitter);
199     }
200
201   dbus_free (pending_activation->service_name);
202   dbus_free (pending_activation->exec);
203   dbus_free (pending_activation->systemd_service);
204
205   link = _dbus_list_get_first_link (&pending_activation->entries);
206
207   while (link != NULL)
208     {
209       BusPendingActivationEntry *entry = link->data;
210
211       bus_pending_activation_entry_free (entry);
212
213       link = _dbus_list_get_next_link (&pending_activation->entries, link);
214     }
215   _dbus_list_clear (&pending_activation->entries);
216
217   pending_activation->activation->n_pending_activations -=
218     pending_activation->n_entries;
219
220   _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
221
222   dbus_free (pending_activation);
223 }
224
225 static BusActivationEntry *
226 bus_activation_entry_ref (BusActivationEntry *entry)
227 {
228   _dbus_assert (entry->refcount > 0);
229   entry->refcount++;
230
231   return entry;
232 }
233
234 static void
235 bus_activation_entry_unref (BusActivationEntry *entry)
236 {
237   if (entry == NULL) /* hash table requires this */
238     return;
239
240   _dbus_assert (entry->refcount > 0);
241   entry->refcount--;
242
243   if (entry->refcount > 0)
244     return;
245
246   dbus_free (entry->name);
247   dbus_free (entry->exec);
248   dbus_free (entry->user);
249   dbus_free (entry->filename);
250   dbus_free (entry->systemd_service);
251
252   dbus_free (entry);
253 }
254
255 static dbus_bool_t
256 update_desktop_file_entry (BusActivation       *activation,
257                            BusServiceDirectory *s_dir,
258                            DBusString          *filename,
259                            BusDesktopFile      *desktop_file,
260                            DBusError           *error)
261 {
262   char *name, *exec, *user, *exec_tmp, *systemd_service;
263   BusActivationEntry *entry;
264   DBusStat stat_buf;
265   DBusString file_path;
266   DBusError tmp_error;
267   dbus_bool_t retval;
268
269   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
270
271   name = NULL;
272   exec = NULL;
273   user = NULL;
274   exec_tmp = NULL;
275   entry = NULL;
276   systemd_service = NULL;
277
278   dbus_error_init (&tmp_error);
279
280   if (!_dbus_string_init (&file_path))
281     {
282       BUS_SET_OOM (error);
283       return FALSE;
284     }
285
286   if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
287       !_dbus_concat_dir_and_file (&file_path, filename))
288     {
289       BUS_SET_OOM (error);
290       goto out;
291     }
292
293   if (!_dbus_stat (&file_path, &stat_buf, NULL))
294     {
295       dbus_set_error (error, DBUS_ERROR_FAILED,
296                       "Can't stat the service file\n");
297       goto out;
298     }
299
300   if (!bus_desktop_file_get_string (desktop_file,
301                                     DBUS_SERVICE_SECTION,
302                                     DBUS_SERVICE_NAME,
303                                     &name,
304                                     error))
305     goto out;
306
307   if (!bus_desktop_file_get_string (desktop_file,
308                                     DBUS_SERVICE_SECTION,
309                                     DBUS_SERVICE_EXEC,
310                                     &exec_tmp,
311                                     error))
312     goto out;
313
314   exec = _dbus_strdup (_dbus_replace_install_prefix (exec_tmp));
315   dbus_free (exec_tmp);
316   exec_tmp = NULL;
317
318   /* user is not _required_ unless we are using system activation */
319   if (!bus_desktop_file_get_string (desktop_file,
320                                     DBUS_SERVICE_SECTION,
321                                     DBUS_SERVICE_USER,
322                                     &user, &tmp_error))
323     {
324       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
325       /* if we got OOM, then exit */
326       if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
327         {
328           dbus_move_error (&tmp_error, error);
329           goto out;
330         }
331       else
332         {
333           /* if we have error because we didn't find anything then continue */
334           dbus_error_free (&tmp_error);
335           dbus_free (user);
336           user = NULL;
337         }
338     }
339   _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
340
341   /* systemd service is never required */
342   if (!bus_desktop_file_get_string (desktop_file,
343                                     DBUS_SERVICE_SECTION,
344                                     DBUS_SERVICE_SYSTEMD_SERVICE,
345                                     &systemd_service, &tmp_error))
346     {
347       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
348       /* if we got OOM, then exit */
349       if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
350         {
351           dbus_move_error (&tmp_error, error);
352           goto out;
353         }
354       else
355         {
356           /* if we have error because we didn't find anything then continue */
357           dbus_error_free (&tmp_error);
358           dbus_free (systemd_service);
359           systemd_service = NULL;
360         }
361     }
362
363   _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
364
365   entry = _dbus_hash_table_lookup_string (s_dir->entries,
366                                           _dbus_string_get_const_data (filename));
367
368   if (entry == NULL) /* New file */
369     {
370       /* FIXME we need a better-defined algorithm for which service file to
371        * pick than "whichever one is first in the directory listing"
372        */
373       if (_dbus_hash_table_lookup_string (activation->entries, name))
374         {
375           dbus_set_error (error, DBUS_ERROR_FAILED,
376                           "Service %s already exists in activation entry list\n", name);
377           goto out;
378         }
379
380       entry = dbus_new0 (BusActivationEntry, 1);
381       if (entry == NULL)
382         {
383           BUS_SET_OOM (error);
384           goto out;
385         }
386
387       entry->name = name;
388       entry->exec = exec;
389       entry->user = user;
390       entry->systemd_service = systemd_service;
391       entry->refcount = 1;
392
393       /* ownership has been transferred to entry, do not free separately */
394       name = NULL;
395       exec = NULL;
396       user = NULL;
397       systemd_service = NULL;
398
399       entry->s_dir = s_dir;
400       entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
401       if (!entry->filename)
402         {
403           BUS_SET_OOM (error);
404           goto out;
405         }
406
407       if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
408         {
409           BUS_SET_OOM (error);
410           goto out;
411         }
412
413       if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
414         {
415           /* Revert the insertion in the entries table */
416           _dbus_hash_table_remove_string (activation->entries, entry->name);
417           BUS_SET_OOM (error);
418           goto out;
419         }
420
421       _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
422     }
423   else /* Just update the entry */
424     {
425       bus_activation_entry_ref (entry);
426       _dbus_hash_table_remove_string (activation->entries, entry->name);
427
428       if (_dbus_hash_table_lookup_string (activation->entries, name))
429         {
430           _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
431                          name, _dbus_string_get_const_data (&file_path));
432           goto out;
433         }
434
435       /* ownership has been transferred to entry, do not free separately */
436       dbus_free (entry->name);
437       entry->name = name;
438       name = NULL;
439
440       dbus_free (entry->exec);
441       entry->exec = exec;
442       exec = NULL;
443
444       dbus_free (entry->user);
445       entry->user = user;
446       user = NULL;
447
448       dbus_free (entry->systemd_service);
449       entry->systemd_service = systemd_service;
450       systemd_service = NULL;
451
452       if (!_dbus_hash_table_insert_string (activation->entries,
453                                            entry->name, bus_activation_entry_ref(entry)))
454         {
455           BUS_SET_OOM (error);
456           /* Also remove path to entries hash since we want this in sync with
457            * the entries hash table */
458           _dbus_hash_table_remove_string (entry->s_dir->entries,
459                                           entry->filename);
460           bus_activation_entry_unref (entry);
461           return FALSE;
462         }
463     }
464
465   entry->mtime = stat_buf.mtime;
466   retval = TRUE;
467
468 out:
469   /* if these have been transferred into entry, the variables will be NULL */
470   dbus_free (name);
471   dbus_free (exec);
472   dbus_free (user);
473   dbus_free (systemd_service);
474   _dbus_string_free (&file_path);
475
476   if (entry)
477     bus_activation_entry_unref (entry);
478
479   return FALSE;
480 }
481
482 static dbus_bool_t
483 check_service_file (BusActivation       *activation,
484                     BusActivationEntry  *entry,
485                     BusActivationEntry **updated_entry,
486                     DBusError           *error)
487 {
488   DBusStat stat_buf;
489   dbus_bool_t retval;
490   BusActivationEntry *tmp_entry;
491   DBusString file_path;
492   DBusString filename;
493
494   retval = TRUE;
495   tmp_entry = entry;
496
497   _dbus_string_init_const (&filename, entry->filename);
498
499   if (!_dbus_string_init (&file_path))
500     {
501       BUS_SET_OOM (error);
502       return FALSE;
503     }
504
505   if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
506       !_dbus_concat_dir_and_file (&file_path, &filename))
507     {
508       BUS_SET_OOM (error);
509       retval = FALSE;
510       goto out;
511     }
512
513   if (!_dbus_stat (&file_path, &stat_buf, NULL))
514     {
515       _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
516                      _dbus_string_get_const_data (&file_path));
517
518       _dbus_hash_table_remove_string (activation->entries, entry->name);
519       _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);
520
521       tmp_entry = NULL;
522       retval = TRUE;
523       goto out;
524     }
525   else
526     {
527       if (stat_buf.mtime > entry->mtime)
528         {
529           BusDesktopFile *desktop_file;
530           DBusError tmp_error;
531
532           dbus_error_init (&tmp_error);
533
534           desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
535           if (desktop_file == NULL)
536             {
537               _dbus_verbose ("Could not load %s: %s\n",
538                              _dbus_string_get_const_data (&file_path),
539                              tmp_error.message);
540               if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
541                 {
542                   dbus_move_error (&tmp_error, error);
543                   retval = FALSE;
544                   goto out;
545                 }
546               dbus_error_free (&tmp_error);
547               retval = TRUE;
548               goto out;
549             }
550
551           /* @todo We can return OOM or a DBUS_ERROR_FAILED error
552            *       Handle these both better
553            */
554           if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
555             {
556               bus_desktop_file_free (desktop_file);
557               if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
558                 {
559                   dbus_move_error (&tmp_error, error);
560                   retval = FALSE;
561                   goto out;
562                 }
563               dbus_error_free (&tmp_error);
564               retval = TRUE;
565               goto out;
566             }
567
568           bus_desktop_file_free (desktop_file);
569           retval = TRUE;
570         }
571     }
572
573 out:
574   _dbus_string_free (&file_path);
575
576   if (updated_entry != NULL)
577     *updated_entry = tmp_entry;
578   return retval;
579 }
580
581
582 /* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
583  * hash entries it already added.
584  */
585 static dbus_bool_t
586 update_directory (BusActivation       *activation,
587                   BusServiceDirectory *s_dir,
588                   DBusError           *error)
589 {
590   DBusDirIter *iter;
591   DBusString dir, filename;
592   BusDesktopFile *desktop_file;
593   DBusError tmp_error;
594   dbus_bool_t retval;
595   BusActivationEntry *entry;
596   DBusString full_path;
597
598   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
599
600   iter = NULL;
601   desktop_file = NULL;
602
603   _dbus_string_init_const (&dir, s_dir->dir_c);
604
605   if (!_dbus_string_init (&filename))
606     {
607       BUS_SET_OOM (error);
608       return FALSE;
609     }
610
611   if (!_dbus_string_init (&full_path))
612     {
613       BUS_SET_OOM (error);
614       _dbus_string_free (&filename);
615       return FALSE;
616     }
617
618   retval = FALSE;
619
620   /* from this point it's safe to "goto out" */
621
622   iter = _dbus_directory_open (&dir, error);
623   if (iter == NULL)
624     {
625       _dbus_verbose ("Failed to open directory %s: %s\n",
626                      s_dir->dir_c,
627                      error ? error->message : "unknown");
628       goto out;
629     }
630
631   /* Now read the files */
632   dbus_error_init (&tmp_error);
633   while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
634     {
635       _dbus_assert (!dbus_error_is_set (&tmp_error));
636
637       _dbus_string_set_length (&full_path, 0);
638
639       if (!_dbus_string_ends_with_c_str (&filename, ".service"))
640         {
641           _dbus_verbose ("Skipping non-.service file %s\n",
642                          _dbus_string_get_const_data (&filename));
643           continue;
644         }
645
646       entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
647       if (entry) /* Already has this service file in the cache */
648         {
649           if (!check_service_file (activation, entry, NULL, error))
650             goto out;
651
652           continue;
653         }
654
655       if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
656           !_dbus_concat_dir_and_file (&full_path, &filename))
657         {
658           BUS_SET_OOM (error);
659           goto out;
660         }
661
662       /* New file */
663       desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
664       if (desktop_file == NULL)
665         {
666           _dbus_verbose ("Could not load %s: %s\n",
667                          _dbus_string_get_const_data (&full_path),
668                          tmp_error.message);
669
670           if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
671             {
672               dbus_move_error (&tmp_error, error);
673               goto out;
674             }
675
676           dbus_error_free (&tmp_error);
677           continue;
678         }
679
680       /* @todo We can return OOM or a DBUS_ERROR_FAILED error
681        *       Handle these both better
682        */
683       if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
684         {
685           bus_desktop_file_free (desktop_file);
686           desktop_file = NULL;
687
688           _dbus_verbose ("Could not add %s to activation entry list: %s\n",
689                          _dbus_string_get_const_data (&full_path), tmp_error.message);
690
691           if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
692             {
693               dbus_move_error (&tmp_error, error);
694               goto out;
695             }
696
697           dbus_error_free (&tmp_error);
698           continue;
699         }
700       else
701         {
702           bus_desktop_file_free (desktop_file);
703           desktop_file = NULL;
704           continue;
705         }
706     }
707
708   if (dbus_error_is_set (&tmp_error))
709     {
710       dbus_move_error (&tmp_error, error);
711       goto out;
712     }
713
714   retval = TRUE;
715
716  out:
717   if (!retval)
718     _DBUS_ASSERT_ERROR_IS_SET (error);
719   else
720     _DBUS_ASSERT_ERROR_IS_CLEAR (error);
721
722   if (iter != NULL)
723     _dbus_directory_close (iter);
724   _dbus_string_free (&filename);
725   _dbus_string_free (&full_path);
726
727   return retval;
728 }
729
730 static dbus_bool_t
731 populate_environment (BusActivation *activation)
732 {
733   DBusString   key;
734   DBusString   value;
735   int          i;
736   char       **environment;
737   dbus_bool_t  retval = FALSE;
738
739   environment = _dbus_get_environment ();
740
741   if (environment == NULL)
742     return FALSE;
743
744   if (!_dbus_string_init (&key))
745     {
746         dbus_free_string_array (environment);
747         return FALSE;
748     }
749
750   if (!_dbus_string_init (&value))
751     {
752       _dbus_string_free (&key);
753       dbus_free_string_array (environment);
754       return FALSE;
755     }
756
757   for (i = 0; environment[i] != NULL; i++)
758     {
759       if (!_dbus_string_append (&key, environment[i]))
760         break;
761
762       if (_dbus_string_split_on_byte (&key, '=', &value))
763         {
764           char *hash_key, *hash_value;
765
766           if (!_dbus_string_steal_data (&key, &hash_key))
767             break;
768
769           if (!_dbus_string_steal_data (&value, &hash_value))
770             break;
771
772           if (!_dbus_hash_table_insert_string (activation->environment,
773                                                hash_key, hash_value))
774             break;
775         }
776       _dbus_string_set_length (&key, 0);
777       _dbus_string_set_length (&value, 0);
778     }
779
780   if (environment[i] != NULL)
781     goto out;
782
783   retval = TRUE;
784 out:
785
786   _dbus_string_free (&key);
787   _dbus_string_free (&value);
788   dbus_free_string_array (environment);
789
790   return retval;
791 }
792
793 dbus_bool_t
794 bus_activation_reload (BusActivation     *activation,
795                        const DBusString  *address,
796                        DBusList         **directories,
797                        DBusError         *error)
798 {
799   DBusList      *link;
800   char          *dir;
801
802   if (activation->server_address != NULL)
803     dbus_free (activation->server_address);
804   if (!_dbus_string_copy_data (address, &activation->server_address))
805     {
806       BUS_SET_OOM (error);
807       goto failed;
808     }
809
810   if (activation->entries != NULL)
811     _dbus_hash_table_unref (activation->entries);
812   activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
813                                              (DBusFreeFunction)bus_activation_entry_unref);
814   if (activation->entries == NULL)
815     {
816       BUS_SET_OOM (error);
817       goto failed;
818     }
819
820   if (activation->directories != NULL)
821     _dbus_hash_table_unref (activation->directories);
822   activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
823                                                   (DBusFreeFunction)bus_service_directory_unref);
824
825   if (activation->directories == NULL)
826     {
827       BUS_SET_OOM (error);
828       goto failed;
829     }
830
831   link = _dbus_list_get_first_link (directories);
832   while (link != NULL)
833     {
834       BusServiceDirectory *s_dir;
835
836       dir = _dbus_strdup ((const char *) link->data);
837       if (!dir)
838         {
839           BUS_SET_OOM (error);
840           goto failed;
841         }
842
843       s_dir = dbus_new0 (BusServiceDirectory, 1);
844       if (!s_dir)
845         {
846           dbus_free (dir);
847           BUS_SET_OOM (error);
848           goto failed;
849         }
850
851       s_dir->refcount = 1;
852       s_dir->dir_c = dir;
853
854       s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
855                                              (DBusFreeFunction)bus_activation_entry_unref);
856
857       if (!s_dir->entries)
858         {
859           bus_service_directory_unref (s_dir);
860           BUS_SET_OOM (error);
861           goto failed;
862         }
863
864       if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
865         {
866           bus_service_directory_unref (s_dir);
867           BUS_SET_OOM (error);
868           goto failed;
869         }
870
871       /* only fail on OOM, it is ok if we can't read the directory */
872       if (!update_directory (activation, s_dir, error))
873         {
874           if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
875             goto failed;
876           else
877             dbus_error_free (error);
878         }
879
880       link = _dbus_list_get_next_link (directories, link);
881     }
882
883   return TRUE;
884  failed:
885   return FALSE;
886 }
887
888 BusActivation*
889 bus_activation_new (BusContext        *context,
890                     const DBusString  *address,
891                     DBusList         **directories,
892                     DBusError         *error)
893 {
894   BusActivation *activation;
895   DBusList      *link;
896   char          *dir;
897
898   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
899
900   activation = dbus_new0 (BusActivation, 1);
901   if (activation == NULL)
902     {
903       BUS_SET_OOM (error);
904       return NULL;
905     }
906
907   activation->refcount = 1;
908   activation->context = context;
909   activation->n_pending_activations = 0;
910
911   if (!bus_activation_reload (activation, address, directories, error))
912     goto failed;
913
914    /* Initialize this hash table once, we don't want to lose pending
915    * activations on reload. */
916   activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
917                                                           (DBusFreeFunction)bus_pending_activation_unref);
918
919   if (activation->pending_activations == NULL)
920     {
921       BUS_SET_OOM (error);
922       goto failed;
923     }
924
925   activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
926                                                   (DBusFreeFunction) dbus_free,
927                                                   (DBusFreeFunction) dbus_free);
928
929   if (activation->environment == NULL)
930     {
931       BUS_SET_OOM (error);
932       goto failed;
933     }
934
935   if (!populate_environment (activation))
936     {
937       BUS_SET_OOM (error);
938       goto failed;
939     }
940
941   return activation;
942
943  failed:
944   bus_activation_unref (activation);
945   return NULL;
946 }
947
948 BusActivation *
949 bus_activation_ref (BusActivation *activation)
950 {
951   _dbus_assert (activation->refcount > 0);
952
953   activation->refcount += 1;
954
955   return activation;
956 }
957
958 void
959 bus_activation_unref (BusActivation *activation)
960 {
961   _dbus_assert (activation->refcount > 0);
962
963   activation->refcount -= 1;
964
965   if (activation->refcount > 0)
966     return;
967
968   dbus_free (activation->server_address);
969   if (activation->entries)
970     _dbus_hash_table_unref (activation->entries);
971   if (activation->pending_activations)
972     _dbus_hash_table_unref (activation->pending_activations);
973   if (activation->directories)
974     _dbus_hash_table_unref (activation->directories);
975   if (activation->environment)
976     _dbus_hash_table_unref (activation->environment);
977
978   dbus_free (activation);
979 }
980
981 static dbus_bool_t
982 add_bus_environment (BusActivation *activation,
983                      DBusError     *error)
984 {
985   const char *type;
986
987   if (!bus_activation_set_environment_variable (activation,
988                                                 "DBUS_STARTER_ADDRESS",
989                                                 activation->server_address,
990                                                 error))
991     return FALSE;
992
993   type = bus_context_get_type (activation->context);
994   if (type != NULL)
995     {
996       if (!bus_activation_set_environment_variable (activation,
997                                                     "DBUS_STARTER_BUS_TYPE", type,
998                                                     error))
999         return FALSE;
1000
1001       if (strcmp (type, "session") == 0)
1002         {
1003           if (!bus_activation_set_environment_variable (activation,
1004                                                         "DBUS_SESSION_BUS_ADDRESS",
1005                                                         activation->server_address,
1006                                                         error))
1007             return FALSE;
1008         }
1009       else if (strcmp (type, "system") == 0)
1010         {
1011           if (!bus_activation_set_environment_variable (activation,
1012                                                         "DBUS_SYSTEM_BUS_ADDRESS",
1013                                                         activation->server_address,
1014                                                         error))
1015             return FALSE;
1016         }
1017     }
1018
1019   return TRUE;
1020 }
1021
1022 typedef struct
1023 {
1024   BusPendingActivation *pending_activation;
1025   DBusPreallocatedHash *hash_entry;
1026 } RestorePendingData;
1027
1028 static void
1029 restore_pending (void *data)
1030 {
1031   RestorePendingData *d = data;
1032
1033   _dbus_assert (d->pending_activation != NULL);
1034   _dbus_assert (d->hash_entry != NULL);
1035
1036   _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
1037                  d->pending_activation->service_name,
1038                  d->pending_activation->timeout_added);
1039
1040   _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
1041                                                d->hash_entry,
1042                                                d->pending_activation->service_name, d->pending_activation);
1043
1044   bus_pending_activation_ref (d->pending_activation);
1045
1046   d->hash_entry = NULL;
1047 }
1048
1049 static void
1050 free_pending_restore_data (void *data)
1051 {
1052   RestorePendingData *d = data;
1053
1054   if (d->hash_entry)
1055     _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
1056                                               d->hash_entry);
1057
1058   bus_pending_activation_unref (d->pending_activation);
1059
1060   dbus_free (d);
1061 }
1062
1063 static dbus_bool_t
1064 add_restore_pending_to_transaction (BusTransaction       *transaction,
1065                                     BusPendingActivation *pending_activation)
1066 {
1067   RestorePendingData *d;
1068
1069   d = dbus_new (RestorePendingData, 1);
1070   if (d == NULL)
1071     return FALSE;
1072
1073   d->pending_activation = pending_activation;
1074   d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1075
1076   bus_pending_activation_ref (d->pending_activation);
1077
1078   if (d->hash_entry == NULL ||
1079       !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1080                                         free_pending_restore_data))
1081     {
1082       free_pending_restore_data (d);
1083       return FALSE;
1084     }
1085
1086   _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1087
1088   return TRUE;
1089 }
1090
1091 dbus_bool_t
1092 bus_activation_service_created (BusActivation  *activation,
1093                                 const char     *service_name,
1094                                 BusTransaction *transaction,
1095                                 DBusError      *error)
1096 {
1097   BusPendingActivation *pending_activation;
1098   DBusMessage *message;
1099   DBusList *link;
1100
1101   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1102
1103   /* Check if it's a pending activation */
1104   pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1105
1106   if (!pending_activation)
1107     return TRUE;
1108
1109   link = _dbus_list_get_first_link (&pending_activation->entries);
1110   while (link != NULL)
1111     {
1112       BusPendingActivationEntry *entry = link->data;
1113       DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1114
1115       if (dbus_connection_get_is_connected (entry->connection))
1116         {
1117           /* Only send activation replies to regular activation requests. */
1118           if (!entry->auto_activation)
1119             {
1120               dbus_uint32_t result;
1121
1122               message = dbus_message_new_method_return (entry->activation_message);
1123               if (!message)
1124                 {
1125                   BUS_SET_OOM (error);
1126                   goto error;
1127                 }
1128
1129               result = DBUS_START_REPLY_SUCCESS;
1130
1131               if (!dbus_message_append_args (message,
1132                                              DBUS_TYPE_UINT32, &result,
1133                                              DBUS_TYPE_INVALID))
1134                 {
1135                   dbus_message_unref (message);
1136                   BUS_SET_OOM (error);
1137                   goto error;
1138                 }
1139
1140               if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
1141                 {
1142                   dbus_message_unref (message);
1143                   BUS_SET_OOM (error);
1144                   goto error;
1145                 }
1146
1147               dbus_message_unref (message);
1148             }
1149         }
1150
1151       link = next;
1152     }
1153
1154   return TRUE;
1155
1156  error:
1157   return FALSE;
1158 }
1159
1160 dbus_bool_t
1161 bus_activation_send_pending_auto_activation_messages (BusActivation  *activation,
1162                                                       BusService     *service,
1163                                                       BusTransaction *transaction,
1164                                                       DBusError      *error)
1165 {
1166   BusPendingActivation *pending_activation;
1167   DBusList *link;
1168
1169   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1170
1171   /* Check if it's a pending activation */
1172   pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1173                                                        bus_service_get_name (service));
1174
1175   if (!pending_activation)
1176     return TRUE;
1177
1178   link = _dbus_list_get_first_link (&pending_activation->entries);
1179   while (link != NULL)
1180     {
1181       BusPendingActivationEntry *entry = link->data;
1182       DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1183
1184       if (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
1185         {
1186           DBusConnection *addressed_recipient;
1187
1188           addressed_recipient = bus_service_get_primary_owners_connection (service);
1189
1190           /* Resume dispatching where we left off in bus_dispatch() */
1191           if (!bus_dispatch_matches (transaction,
1192                                      entry->connection,
1193                                      addressed_recipient,
1194                                      entry->activation_message, error))
1195             goto error;
1196         }
1197
1198       link = next;
1199     }
1200
1201   if (!add_restore_pending_to_transaction (transaction, pending_activation))
1202     {
1203       _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
1204       BUS_SET_OOM (error);
1205       goto error;
1206     }
1207
1208   _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1209
1210   return TRUE;
1211
1212  error:
1213   return FALSE;
1214 }
1215
1216 /**
1217  * FIXME @todo the error messages here would ideally be preallocated
1218  * so we don't need to allocate memory to send them.
1219  * Using the usual tactic, prealloc an OOM message, then
1220  * if we can't alloc the real error send the OOM error instead.
1221  */
1222 static dbus_bool_t
1223 try_send_activation_failure (BusPendingActivation *pending_activation,
1224                              const DBusError      *how)
1225 {
1226   BusActivation *activation;
1227   DBusList *link;
1228   BusTransaction *transaction;
1229
1230   activation = pending_activation->activation;
1231
1232   transaction = bus_transaction_new (activation->context);
1233   if (transaction == NULL)
1234     return FALSE;
1235
1236   link = _dbus_list_get_first_link (&pending_activation->entries);
1237   while (link != NULL)
1238     {
1239       BusPendingActivationEntry *entry = link->data;
1240       DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1241
1242       if (dbus_connection_get_is_connected (entry->connection))
1243         {
1244           if (!bus_transaction_send_error_reply (transaction,
1245                                                  entry->connection,
1246                                                  how,
1247                                                  entry->activation_message))
1248             goto error;
1249         }
1250
1251       link = next;
1252     }
1253
1254   bus_transaction_execute_and_free (transaction);
1255
1256   return TRUE;
1257
1258  error:
1259   if (transaction)
1260     bus_transaction_cancel_and_free (transaction);
1261   return FALSE;
1262 }
1263
1264 /**
1265  * Free the pending activation and send an error message to all the
1266  * connections that were waiting for it.
1267  */
1268 static void
1269 pending_activation_failed (BusPendingActivation *pending_activation,
1270                            const DBusError      *how)
1271 {
1272   /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
1273   while (!try_send_activation_failure (pending_activation, how))
1274     _dbus_wait_for_memory ();
1275
1276   /* Destroy this pending activation */
1277   _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1278                                   pending_activation->service_name);
1279 }
1280
1281 /**
1282  * Depending on the exit code of the helper, set the error accordingly
1283  */
1284 static void
1285 handle_servicehelper_exit_error (int        exit_code,
1286                                  DBusError *error)
1287 {
1288   switch (exit_code)
1289     {
1290     case BUS_SPAWN_EXIT_CODE_NO_MEMORY:
1291       dbus_set_error (error, DBUS_ERROR_NO_MEMORY,
1292                       "Launcher could not run (out of memory)");
1293       break;
1294     case BUS_SPAWN_EXIT_CODE_SETUP_FAILED:
1295       dbus_set_error (error, DBUS_ERROR_SPAWN_SETUP_FAILED,
1296                       "Failed to setup environment correctly");
1297       break;
1298     case BUS_SPAWN_EXIT_CODE_NAME_INVALID:
1299       dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_INVALID,
1300                       "Bus name is not valid or missing");
1301       break;
1302     case BUS_SPAWN_EXIT_CODE_SERVICE_NOT_FOUND:
1303       dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
1304                       "Bus name not found in system service directory");
1305       break;
1306     case BUS_SPAWN_EXIT_CODE_PERMISSIONS_INVALID:
1307       dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1308                       "The permission of the setuid helper is not correct");
1309       break;
1310     case BUS_SPAWN_EXIT_CODE_FILE_INVALID:
1311       dbus_set_error (error, DBUS_ERROR_SPAWN_PERMISSIONS_INVALID,
1312                       "The service file is incorrect or does not have all required attributes");
1313       break;
1314     case BUS_SPAWN_EXIT_CODE_EXEC_FAILED:
1315       dbus_set_error (error, DBUS_ERROR_SPAWN_EXEC_FAILED,
1316                       "Cannot launch daemon, file not found or permissions invalid");
1317       break;
1318     case BUS_SPAWN_EXIT_CODE_INVALID_ARGS:
1319       dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1320                       "Invalid arguments to command line");
1321       break;
1322     case BUS_SPAWN_EXIT_CODE_CHILD_SIGNALED:
1323       dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_SIGNALED,
1324                       "Launched child was signaled, it probably crashed");
1325       break;
1326     default:
1327       dbus_set_error (error, DBUS_ERROR_SPAWN_CHILD_EXITED,
1328                       "Launch helper exited with unknown return code %i", exit_code);
1329       break;
1330     }
1331 }
1332
1333 static dbus_bool_t
1334 babysitter_watch_callback (DBusWatch     *watch,
1335                            unsigned int   condition,
1336                            void          *data)
1337 {
1338   BusPendingActivation *pending_activation = data;
1339   dbus_bool_t retval;
1340   DBusBabysitter *babysitter;
1341   dbus_bool_t uses_servicehelper;
1342
1343   babysitter = pending_activation->babysitter;
1344
1345   _dbus_babysitter_ref (babysitter);
1346
1347   retval = dbus_watch_handle (watch, condition);
1348
1349   /* There are two major cases here; are we the system bus or the session?  Here this
1350    * is distinguished by whether or not we use a setuid helper launcher.  With the launch helper,
1351    * some process exit codes are meaningful, processed by handle_servicehelper_exit_error.
1352    *
1353    * In both cases though, just ignore when a process exits with status 0; it's possible for
1354    * a program to (misguidedly) "daemonize", and that appears to us as an exit.  This closes a race
1355    * condition between this code and the child process claiming the bus name.
1356    */
1357   uses_servicehelper = bus_context_get_servicehelper (pending_activation->activation->context) != NULL;
1358
1359   /* FIXME this is broken in the same way that
1360    * connection watches used to be; there should be
1361    * a separate callback for status change, instead
1362    * of doing "if we handled a watch status might
1363    * have changed"
1364    *
1365    * Fixing this lets us move dbus_watch_handle
1366    * calls into dbus-mainloop.c
1367    */
1368   if (_dbus_babysitter_get_child_exited (babysitter))
1369     {
1370       DBusError error;
1371       DBusHashIter iter;
1372       dbus_bool_t activation_failed;
1373       int exit_code = 0;
1374
1375       dbus_error_init (&error);
1376
1377       _dbus_babysitter_set_child_exit_error (babysitter, &error);
1378
1379       /* Explicitly check for SPAWN_CHILD_EXITED to avoid overwriting an
1380        * exec error */
1381       if (dbus_error_has_name (&error, DBUS_ERROR_SPAWN_CHILD_EXITED)
1382           && _dbus_babysitter_get_child_exit_status (babysitter, &exit_code))
1383         {
1384           activation_failed = exit_code != 0;
1385
1386           dbus_error_free(&error);
1387
1388           if (activation_failed)
1389             {
1390               if (uses_servicehelper)
1391                 handle_servicehelper_exit_error (exit_code, &error);
1392               else
1393                 _dbus_babysitter_set_child_exit_error (babysitter, &error);
1394             }
1395         }
1396       else
1397         {
1398           activation_failed = TRUE;
1399         }
1400
1401       if (activation_failed)
1402         {
1403           /* Destroy all pending activations with the same exec */
1404           _dbus_hash_iter_init (pending_activation->activation->pending_activations,
1405                                 &iter);
1406           while (_dbus_hash_iter_next (&iter))
1407             {
1408               BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1409
1410               if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
1411                 pending_activation_failed (p, &error);
1412             }
1413
1414           /* Destroys the pending activation */
1415           pending_activation_failed (pending_activation, &error);
1416
1417           dbus_error_free (&error);
1418         }
1419     }
1420
1421   _dbus_babysitter_unref (babysitter);
1422
1423   return retval;
1424 }
1425
1426 static dbus_bool_t
1427 add_babysitter_watch (DBusWatch      *watch,
1428                       void           *data)
1429 {
1430   BusPendingActivation *pending_activation = data;
1431
1432   return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
1433                                watch, babysitter_watch_callback, pending_activation,
1434                                NULL);
1435 }
1436
1437 static void
1438 remove_babysitter_watch (DBusWatch      *watch,
1439                          void           *data)
1440 {
1441   BusPendingActivation *pending_activation = data;
1442
1443   _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
1444                            watch, babysitter_watch_callback, pending_activation);
1445 }
1446
1447 static dbus_bool_t
1448 pending_activation_timed_out (void *data)
1449 {
1450   BusPendingActivation *pending_activation = data;
1451   DBusError error;
1452
1453   /* Kill the spawned process, since it sucks
1454    * (not sure this is what we want to do, but
1455    * may as well try it for now)
1456    */
1457   if (pending_activation->babysitter)
1458     _dbus_babysitter_kill_child (pending_activation->babysitter);
1459
1460   dbus_error_init (&error);
1461
1462   dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
1463                   "Activation of %s timed out",
1464                   pending_activation->service_name);
1465
1466   pending_activation_failed (pending_activation, &error);
1467
1468   dbus_error_free (&error);
1469
1470   return TRUE;
1471 }
1472
1473 static void
1474 cancel_pending (void *data)
1475 {
1476   BusPendingActivation *pending_activation = data;
1477
1478   _dbus_verbose ("Canceling pending activation of %s\n",
1479                  pending_activation->service_name);
1480
1481   if (pending_activation->babysitter)
1482     _dbus_babysitter_kill_child (pending_activation->babysitter);
1483
1484   _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
1485                                   pending_activation->service_name);
1486 }
1487
1488 static void
1489 free_pending_cancel_data (void *data)
1490 {
1491   BusPendingActivation *pending_activation = data;
1492
1493   bus_pending_activation_unref (pending_activation);
1494 }
1495
1496 static dbus_bool_t
1497 add_cancel_pending_to_transaction (BusTransaction       *transaction,
1498                                    BusPendingActivation *pending_activation)
1499 {
1500   if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
1501                                         pending_activation,
1502                                         free_pending_cancel_data))
1503     return FALSE;
1504
1505   bus_pending_activation_ref (pending_activation);
1506
1507   _dbus_verbose ("Saved pending activation to be canceled if the transaction fails\n");
1508
1509   return TRUE;
1510 }
1511
1512 static dbus_bool_t
1513 update_service_cache (BusActivation *activation, DBusError *error)
1514 {
1515   DBusHashIter iter;
1516
1517   _dbus_hash_iter_init (activation->directories, &iter);
1518   while (_dbus_hash_iter_next (&iter))
1519     {
1520       DBusError tmp_error;
1521       BusServiceDirectory *s_dir;
1522
1523       s_dir = _dbus_hash_iter_get_value (&iter);
1524
1525       dbus_error_init (&tmp_error);
1526       if (!update_directory (activation, s_dir, &tmp_error))
1527         {
1528           if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
1529             {
1530               dbus_move_error (&tmp_error, error);
1531               return FALSE;
1532             }
1533
1534           dbus_error_free (&tmp_error);
1535           continue;
1536         }
1537     }
1538
1539   return TRUE;
1540 }
1541
1542 static BusActivationEntry *
1543 activation_find_entry (BusActivation *activation,
1544                        const char    *service_name,
1545                        DBusError     *error)
1546 {
1547   BusActivationEntry *entry;
1548
1549   entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
1550   if (!entry)
1551     {
1552       if (!update_service_cache (activation, error))
1553         return NULL;
1554
1555       entry = _dbus_hash_table_lookup_string (activation->entries,
1556                                               service_name);
1557     }
1558   else
1559     {
1560       BusActivationEntry *updated_entry;
1561
1562       if (!check_service_file (activation, entry, &updated_entry, error))
1563         return NULL;
1564
1565       entry = updated_entry;
1566     }
1567
1568   if (!entry)
1569     {
1570       dbus_set_error (error, DBUS_ERROR_SERVICE_UNKNOWN,
1571                       "The name %s was not provided by any .service files",
1572                       service_name);
1573       return NULL;
1574     }
1575
1576   return entry;
1577 }
1578
1579 static char **
1580 bus_activation_get_environment (BusActivation *activation)
1581 {
1582   char **environment;
1583   int i, length;
1584   DBusString entry;
1585   DBusHashIter iter;
1586
1587   length = _dbus_hash_table_get_n_entries (activation->environment);
1588
1589   environment = dbus_new0 (char *, length + 1);
1590
1591   if (environment == NULL)
1592     return NULL;
1593
1594   i = 0;
1595   _dbus_hash_iter_init (activation->environment, &iter);
1596
1597   if (!_dbus_string_init (&entry))
1598     {
1599       dbus_free_string_array (environment);
1600       return NULL;
1601     }
1602
1603   while (_dbus_hash_iter_next (&iter))
1604     {
1605       const char *key, *value;
1606
1607       key = (const char *) _dbus_hash_iter_get_string_key (&iter);
1608       value = (const char *) _dbus_hash_iter_get_value (&iter);
1609
1610       if (!_dbus_string_append_printf (&entry, "%s=%s", key, value))
1611         break;
1612
1613       if (!_dbus_string_steal_data (&entry, environment + i))
1614         break;
1615       i++;
1616     }
1617
1618   _dbus_string_free (&entry);
1619
1620   if (i != length)
1621     {
1622       dbus_free_string_array (environment);
1623       environment = NULL;
1624     }
1625
1626   return environment;
1627 }
1628
1629 dbus_bool_t
1630 bus_activation_set_environment_variable (BusActivation     *activation,
1631                                          const char        *key,
1632                                          const char        *value,
1633                                          DBusError         *error)
1634 {
1635   char        *hash_key;
1636   char        *hash_value;
1637   dbus_bool_t  retval;
1638
1639   retval = FALSE;
1640   hash_key = NULL;
1641   hash_value = NULL;
1642   hash_key = _dbus_strdup (key);
1643
1644   if (hash_key == NULL)
1645     goto out;
1646
1647   hash_value = _dbus_strdup (value);
1648
1649   if (hash_value == NULL)
1650     goto out;
1651
1652   if (!_dbus_hash_table_insert_string (activation->environment,
1653                                        hash_key, hash_value))
1654     goto out;
1655
1656   retval = TRUE;
1657 out:
1658   if (retval == FALSE)
1659     {
1660       dbus_free (hash_key);
1661       dbus_free (hash_value);
1662       BUS_SET_OOM (error);
1663     }
1664
1665   return retval;
1666 }
1667
1668 dbus_bool_t
1669 bus_activation_activate_service (BusActivation  *activation,
1670                                  DBusConnection *connection,
1671                                  BusTransaction *transaction,
1672                                  dbus_bool_t     auto_activation,
1673                                  DBusMessage    *activation_message,
1674                                  const char     *service_name,
1675                                  DBusError      *error)
1676 {
1677   BusActivationEntry *entry;
1678   BusPendingActivation *pending_activation;
1679   BusPendingActivationEntry *pending_activation_entry;
1680   DBusMessage *message;
1681   DBusString service_str;
1682   const char *servicehelper;
1683   char **argv;
1684   char **envp = NULL;
1685   int argc;
1686   dbus_bool_t retval;
1687   DBusHashIter iter;
1688   dbus_bool_t activated;
1689   DBusString command;
1690
1691   activated = TRUE;
1692
1693   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1694
1695   if (activation->n_pending_activations >=
1696       bus_context_get_max_pending_activations (activation->context))
1697     {
1698       dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1699                       "The maximum number of pending activations has been reached, activation of %s failed",
1700                       service_name);
1701       return FALSE;
1702     }
1703
1704   entry = activation_find_entry (activation, service_name, error);
1705   if (!entry)
1706     return FALSE;
1707
1708   /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
1709    * call us if the service is already active.
1710    */
1711   if (!auto_activation)
1712     {
1713       /* Check if the service is active */
1714       _dbus_string_init_const (&service_str, service_name);
1715       if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
1716         {
1717           dbus_uint32_t result;
1718
1719           _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1720
1721           message = dbus_message_new_method_return (activation_message);
1722
1723           if (!message)
1724             {
1725               _dbus_verbose ("No memory to create reply to activate message\n");
1726               BUS_SET_OOM (error);
1727               return FALSE;
1728             }
1729
1730           result = DBUS_START_REPLY_ALREADY_RUNNING;
1731
1732           if (!dbus_message_append_args (message,
1733                                          DBUS_TYPE_UINT32, &result,
1734                                          DBUS_TYPE_INVALID))
1735             {
1736               _dbus_verbose ("No memory to set args of reply to activate message\n");
1737               BUS_SET_OOM (error);
1738               dbus_message_unref (message);
1739               return FALSE;
1740             }
1741
1742           retval = bus_transaction_send_from_driver (transaction, connection, message);
1743           dbus_message_unref (message);
1744           if (!retval)
1745             {
1746               _dbus_verbose ("Failed to send reply\n");
1747               BUS_SET_OOM (error);
1748             }
1749
1750           return retval;
1751         }
1752     }
1753
1754   pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
1755   if (!pending_activation_entry)
1756     {
1757       _dbus_verbose ("Failed to create pending activation entry\n");
1758       BUS_SET_OOM (error);
1759       return FALSE;
1760     }
1761
1762   pending_activation_entry->auto_activation = auto_activation;
1763
1764   pending_activation_entry->activation_message = activation_message;
1765   dbus_message_ref (activation_message);
1766   pending_activation_entry->connection = connection;
1767   dbus_connection_ref (connection);
1768
1769   /* Check if the service is being activated */
1770   pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
1771   if (pending_activation)
1772     {
1773       if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1774         {
1775           _dbus_verbose ("Failed to append a new entry to pending activation\n");
1776
1777           BUS_SET_OOM (error);
1778           bus_pending_activation_entry_free (pending_activation_entry);
1779           return FALSE;
1780         }
1781
1782       pending_activation->n_entries += 1;
1783       pending_activation->activation->n_pending_activations += 1;
1784     }
1785   else
1786     {
1787       pending_activation = dbus_new0 (BusPendingActivation, 1);
1788       if (!pending_activation)
1789         {
1790           _dbus_verbose ("Failed to create pending activation\n");
1791
1792           BUS_SET_OOM (error);
1793           bus_pending_activation_entry_free (pending_activation_entry);
1794           return FALSE;
1795         }
1796
1797       pending_activation->activation = activation;
1798       pending_activation->refcount = 1;
1799
1800       pending_activation->service_name = _dbus_strdup (service_name);
1801       if (!pending_activation->service_name)
1802         {
1803           _dbus_verbose ("Failed to copy service name for pending activation\n");
1804
1805           BUS_SET_OOM (error);
1806           bus_pending_activation_unref (pending_activation);
1807           bus_pending_activation_entry_free (pending_activation_entry);
1808           return FALSE;
1809         }
1810
1811       pending_activation->exec = _dbus_strdup (entry->exec);
1812       if (!pending_activation->exec)
1813         {
1814           _dbus_verbose ("Failed to copy service exec for pending activation\n");
1815           BUS_SET_OOM (error);
1816           bus_pending_activation_unref (pending_activation);
1817           bus_pending_activation_entry_free (pending_activation_entry);
1818           return FALSE;
1819         }
1820
1821       if (entry->systemd_service)
1822         {
1823           pending_activation->systemd_service = _dbus_strdup (entry->systemd_service);
1824           if (!pending_activation->systemd_service)
1825             {
1826               _dbus_verbose ("Failed to copy systemd service for pending activation\n");
1827               BUS_SET_OOM (error);
1828               bus_pending_activation_unref (pending_activation);
1829               bus_pending_activation_entry_free (pending_activation_entry);
1830               return FALSE;
1831             }
1832         }
1833
1834       pending_activation->timeout =
1835         _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
1836                            pending_activation_timed_out,
1837                            pending_activation,
1838                            NULL);
1839       if (!pending_activation->timeout)
1840         {
1841           _dbus_verbose ("Failed to create timeout for pending activation\n");
1842
1843           BUS_SET_OOM (error);
1844           bus_pending_activation_unref (pending_activation);
1845           bus_pending_activation_entry_free (pending_activation_entry);
1846           return FALSE;
1847         }
1848
1849       if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
1850                                    pending_activation->timeout,
1851                                    handle_timeout_callback,
1852                                    pending_activation,
1853                                    NULL))
1854         {
1855           _dbus_verbose ("Failed to add timeout for pending activation\n");
1856
1857           BUS_SET_OOM (error);
1858           bus_pending_activation_unref (pending_activation);
1859           bus_pending_activation_entry_free (pending_activation_entry);
1860           return FALSE;
1861         }
1862
1863       pending_activation->timeout_added = TRUE;
1864
1865       if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1866         {
1867           _dbus_verbose ("Failed to add entry to just-created pending activation\n");
1868
1869           BUS_SET_OOM (error);
1870           bus_pending_activation_unref (pending_activation);
1871           bus_pending_activation_entry_free (pending_activation_entry);
1872           return FALSE;
1873         }
1874
1875       pending_activation->n_entries += 1;
1876       pending_activation->activation->n_pending_activations += 1;
1877
1878       activated = FALSE;
1879       _dbus_hash_iter_init (activation->pending_activations, &iter);
1880       while (_dbus_hash_iter_next (&iter))
1881         {
1882           BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
1883
1884           if (strcmp (p->exec, entry->exec) == 0)
1885             {
1886               activated = TRUE;
1887               break;
1888             }
1889         }
1890
1891       if (!_dbus_hash_table_insert_string (activation->pending_activations,
1892                                            pending_activation->service_name,
1893                                            pending_activation))
1894         {
1895           _dbus_verbose ("Failed to put pending activation in hash table\n");
1896
1897           BUS_SET_OOM (error);
1898           bus_pending_activation_unref (pending_activation);
1899           return FALSE;
1900         }
1901     }
1902
1903   if (!add_cancel_pending_to_transaction (transaction, pending_activation))
1904     {
1905       _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
1906       BUS_SET_OOM (error);
1907       _dbus_hash_table_remove_string (activation->pending_activations,
1908                                       pending_activation->service_name);
1909
1910       return FALSE;
1911     }
1912
1913   if (activated)
1914     return TRUE;
1915
1916   if (bus_context_get_systemd_activation (activation->context))
1917     {
1918       if (strcmp (service_name, "org.freedesktop.systemd1") == 0)
1919           /* systemd itself is missing apparently. That can happen
1920              only during early startup. Let's just wait until systemd
1921              connects to us and do nothing. */
1922         return TRUE;
1923
1924       if (entry->systemd_service)
1925         {
1926           BusTransaction *activation_transaction;
1927           DBusString service_string;
1928           BusService *service;
1929           BusRegistry *registry;
1930
1931           /* OK, we have a systemd service configured for this entry,
1932              hence let's enqueue an activation request message. This
1933              is implemented as a directed signal, not a method call,
1934              for three reasons: 1) we don't expect a response on
1935              success, where we just expect a name appearing on the
1936              bus; 2) at this time the systemd service might not yet
1937              have connected, so we wouldn't know the message serial at
1938              this point to set up a pending call; 3) it is ugly if the
1939              bus suddenly becomes the caller of a remote method. */
1940
1941           message = dbus_message_new_signal (DBUS_PATH_DBUS,
1942                                              "org.freedesktop.systemd1.Activator",
1943                                              "ActivationRequest");
1944           if (!message)
1945             {
1946               _dbus_verbose ("No memory to create activation message\n");
1947               BUS_SET_OOM (error);
1948               return FALSE;
1949             }
1950
1951           if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS) ||
1952               !dbus_message_set_destination (message, "org.freedesktop.systemd1") ||
1953               !dbus_message_append_args (message,
1954                                          DBUS_TYPE_STRING, &entry->systemd_service,
1955                                          DBUS_TYPE_INVALID))
1956             {
1957               _dbus_verbose ("No memory to set args of activation message\n");
1958               dbus_message_unref (message);
1959               BUS_SET_OOM (error);
1960               return FALSE;
1961             }
1962
1963           /* Create our transaction */
1964           activation_transaction = bus_transaction_new (activation->context);
1965           if (activation_transaction == NULL)
1966             {
1967               _dbus_verbose ("No memory to create activation transaction\n");
1968               dbus_message_unref (message);
1969               BUS_SET_OOM (error);
1970               return FALSE;
1971             }
1972
1973           /* Check whether systemd is already connected */
1974           registry = bus_connection_get_registry (connection);
1975           _dbus_string_init_const (&service_string, "org.freedesktop.systemd1");
1976           service = bus_registry_lookup (registry, &service_string);
1977
1978           if (service != NULL)
1979             /* Wonderful, systemd is connected, let's just send the msg */
1980             retval = bus_dispatch_matches (activation_transaction, NULL, bus_service_get_primary_owners_connection (service),
1981                                            message, error);
1982           else
1983             /* systemd is not around, let's "activate" it. */
1984             retval = bus_activation_activate_service (activation, connection, activation_transaction, TRUE,
1985                                                       message, "org.freedesktop.systemd1", error);
1986
1987           dbus_message_unref (message);
1988
1989           if (!retval)
1990             {
1991               _DBUS_ASSERT_ERROR_IS_SET (error);
1992               _dbus_verbose ("failed to send activation message: %s\n", error->name);
1993               bus_transaction_cancel_and_free (activation_transaction);
1994               return FALSE;
1995             }
1996
1997           bus_transaction_execute_and_free (activation_transaction);
1998           return TRUE;
1999         }
2000
2001       /* OK, we have no configured systemd service, hence let's
2002          proceed with traditional activation. */
2003     }
2004
2005   /* use command as system and session different */
2006   if (!_dbus_string_init (&command))
2007     {
2008       BUS_SET_OOM (error);
2009       return FALSE;
2010     }
2011
2012   /* does the bus use a helper? */
2013   servicehelper = bus_context_get_servicehelper (activation->context);
2014   if (servicehelper != NULL)
2015     {
2016       if (entry->user == NULL)
2017         {
2018           _dbus_string_free (&command);
2019           dbus_set_error (error, DBUS_ERROR_SPAWN_FILE_INVALID,
2020                           "Cannot do system-bus activation with no user\n");
2021           return FALSE;
2022         }
2023
2024       /* join the helper path and the service name */
2025       if (!_dbus_string_append (&command, servicehelper))
2026         {
2027           _dbus_string_free (&command);
2028           BUS_SET_OOM (error);
2029           return FALSE;
2030         }
2031       if (!_dbus_string_append (&command, " "))
2032         {
2033           _dbus_string_free (&command);
2034           BUS_SET_OOM (error);
2035           return FALSE;
2036         }
2037       if (!_dbus_string_append (&command, service_name))
2038         {
2039           _dbus_string_free (&command);
2040           BUS_SET_OOM (error);
2041           return FALSE;
2042         }
2043     }
2044   else
2045     {
2046       /* the bus does not use a helper, so we can append arguments with the exec line */
2047       if (!_dbus_string_append (&command, entry->exec))
2048         {
2049           _dbus_string_free (&command);
2050           BUS_SET_OOM (error);
2051           return FALSE;
2052         }
2053     }
2054
2055   /* convert command into arguments */
2056   if (!_dbus_shell_parse_argv (_dbus_string_get_const_data (&command), &argc, &argv, error))
2057     {
2058       _dbus_verbose ("Failed to parse command line: %s\n", entry->exec);
2059       _DBUS_ASSERT_ERROR_IS_SET (error);
2060
2061       _dbus_hash_table_remove_string (activation->pending_activations,
2062                                       pending_activation->service_name);
2063
2064       _dbus_string_free (&command);
2065       return FALSE;
2066     }
2067   _dbus_string_free (&command);
2068
2069   if (!add_bus_environment (activation, error))
2070     {
2071       _DBUS_ASSERT_ERROR_IS_SET (error);
2072       dbus_free_string_array (argv);
2073       return FALSE;
2074     }
2075
2076   envp = bus_activation_get_environment (activation);
2077
2078   if (envp == NULL)
2079     {
2080       BUS_SET_OOM (error);
2081       dbus_free_string_array (argv);
2082       return FALSE;
2083     }
2084
2085   _dbus_verbose ("Spawning %s ...\n", argv[0]);
2086   if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
2087                                           envp,
2088                                           NULL, activation,
2089                                           error))
2090     {
2091       _dbus_verbose ("Failed to spawn child\n");
2092       _DBUS_ASSERT_ERROR_IS_SET (error);
2093       dbus_free_string_array (argv);
2094       dbus_free_string_array (envp);
2095
2096       return FALSE;
2097     }
2098
2099   dbus_free_string_array (argv);
2100   envp = NULL;
2101
2102   _dbus_assert (pending_activation->babysitter != NULL);
2103
2104   if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
2105                                              add_babysitter_watch,
2106                                              remove_babysitter_watch,
2107                                              NULL,
2108                                              pending_activation,
2109                                              NULL))
2110     {
2111       BUS_SET_OOM (error);
2112       _dbus_verbose ("Failed to set babysitter watch functions\n");
2113       return FALSE;
2114     }
2115
2116   return TRUE;
2117 }
2118
2119 dbus_bool_t
2120 bus_activation_list_services (BusActivation *activation,
2121                               char        ***listp,
2122                               int           *array_len)
2123 {
2124   int i, j, len;
2125   char **retval;
2126   DBusHashIter iter;
2127
2128   len = _dbus_hash_table_get_n_entries (activation->entries);
2129   retval = dbus_new (char *, len + 1);
2130
2131   if (retval == NULL)
2132     return FALSE;
2133
2134   _dbus_hash_iter_init (activation->entries, &iter);
2135   i = 0;
2136   while (_dbus_hash_iter_next (&iter))
2137     {
2138       BusActivationEntry *entry = _dbus_hash_iter_get_value (&iter);
2139
2140       retval[i] = _dbus_strdup (entry->name);
2141       if (retval[i] == NULL)
2142         goto error;
2143
2144       i++;
2145     }
2146
2147   retval[i] = NULL;
2148
2149   if (array_len)
2150     *array_len = len;
2151
2152   *listp = retval;
2153   return TRUE;
2154
2155  error:
2156   for (j = 0; j < i; j++)
2157     dbus_free (retval[i]);
2158   dbus_free (retval);
2159
2160   return FALSE;
2161 }
2162
2163 dbus_bool_t
2164 dbus_activation_systemd_failure (BusActivation *activation,
2165                                  DBusMessage   *message)
2166 {
2167   DBusError error;
2168   const char *code, *str, *unit = NULL;
2169
2170   dbus_error_init(&error);
2171
2172   /* This is called whenever the systemd activator sent us a
2173      response. We'll invalidate all pending activations that match the
2174      unit name. */
2175
2176   if (dbus_message_get_args (message, &error,
2177                              DBUS_TYPE_STRING, &unit,
2178                              DBUS_TYPE_STRING, &code,
2179                              DBUS_TYPE_STRING, &str,
2180                              DBUS_TYPE_INVALID))
2181     dbus_set_error(&error, code, str);
2182
2183   if (unit)
2184     {
2185       DBusHashIter iter;
2186
2187       _dbus_hash_iter_init (activation->pending_activations,
2188                             &iter);
2189
2190       while (_dbus_hash_iter_next (&iter))
2191         {
2192           BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
2193
2194           if (p->systemd_service && strcmp (p->systemd_service, unit) == 0)
2195             pending_activation_failed(p, &error);
2196         }
2197     }
2198
2199   dbus_error_free(&error);
2200
2201   return TRUE;
2202 }
2203
2204 #ifdef DBUS_BUILD_TESTS
2205
2206 #include <stdio.h>
2207
2208 #define SERVICE_NAME_1 "MyService1"
2209 #define SERVICE_NAME_2 "MyService2"
2210 #define SERVICE_NAME_3 "MyService3"
2211
2212 #define SERVICE_FILE_1 "service-1.service"
2213 #define SERVICE_FILE_2 "service-2.service"
2214 #define SERVICE_FILE_3 "service-3.service"
2215
2216 static dbus_bool_t
2217 test_create_service_file (DBusString *dir,
2218                           const char *filename,
2219                           const char *name,
2220                           const char *exec)
2221 {
2222   DBusString  file_name, full_path;
2223   FILE        *file;
2224   dbus_bool_t  ret_val;
2225
2226   ret_val = TRUE;
2227   _dbus_string_init_const (&file_name, filename);
2228
2229   if (!_dbus_string_init (&full_path))
2230     return FALSE;
2231
2232   if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2233       !_dbus_concat_dir_and_file (&full_path, &file_name))
2234     {
2235       ret_val = FALSE;
2236       goto out;
2237     }
2238
2239   file = fopen (_dbus_string_get_const_data (&full_path), "w");
2240   if (!file)
2241     {
2242       ret_val = FALSE;
2243       goto out;
2244     }
2245
2246   fprintf (file, "[D-BUS Service]\nName=%s\nExec=%s\n", name, exec);
2247   fclose (file);
2248
2249 out:
2250   _dbus_string_free (&full_path);
2251   return ret_val;
2252 }
2253
2254 static dbus_bool_t
2255 test_remove_service_file (DBusString *dir, const char *filename)
2256 {
2257   DBusString  file_name, full_path;
2258   dbus_bool_t ret_val;
2259
2260   ret_val = TRUE;
2261
2262   _dbus_string_init_const (&file_name, filename);
2263
2264   if (!_dbus_string_init (&full_path))
2265     return FALSE;
2266
2267   if (!_dbus_string_append (&full_path, _dbus_string_get_const_data (dir)) ||
2268       !_dbus_concat_dir_and_file (&full_path, &file_name))
2269     {
2270       ret_val = FALSE;
2271       goto out;
2272     }
2273
2274   if (!_dbus_delete_file (&full_path, NULL))
2275     {
2276       ret_val = FALSE;
2277       goto out;
2278     }
2279
2280 out:
2281   _dbus_string_free (&full_path);
2282   return ret_val;
2283 }
2284
2285 static dbus_bool_t
2286 test_remove_directory (DBusString *dir)
2287 {
2288   DBusDirIter *iter;
2289   DBusString   filename, full_path;
2290   dbus_bool_t  ret_val;
2291
2292   ret_val = TRUE;
2293
2294   if (!_dbus_string_init (&filename))
2295     return FALSE;
2296
2297   if (!_dbus_string_init (&full_path))
2298     {
2299       _dbus_string_free (&filename);
2300       return FALSE;
2301     }
2302
2303   iter = _dbus_directory_open (dir, NULL);
2304   if (iter == NULL)
2305     {
2306       ret_val = FALSE;
2307       goto out;
2308     }
2309
2310   while (_dbus_directory_get_next_file (iter, &filename, NULL))
2311     {
2312       if (!test_remove_service_file (dir, _dbus_string_get_const_data (&filename)))
2313         {
2314           ret_val = FALSE;
2315           goto out;
2316         }
2317     }
2318   _dbus_directory_close (iter);
2319
2320   if (!_dbus_delete_directory (dir, NULL))
2321     {
2322       ret_val = FALSE;
2323       goto out;
2324     }
2325
2326 out:
2327   _dbus_string_free (&filename);
2328   _dbus_string_free (&full_path);
2329
2330   return ret_val;
2331 }
2332
2333 static dbus_bool_t
2334 init_service_reload_test (DBusString *dir)
2335 {
2336   DBusStat stat_buf;
2337
2338   if (!_dbus_stat (dir, &stat_buf, NULL))
2339     {
2340       if (!_dbus_create_directory (dir, NULL))
2341         return FALSE;
2342     }
2343   else
2344     {
2345       if (!test_remove_directory (dir))
2346         return FALSE;
2347
2348       if (!_dbus_create_directory (dir, NULL))
2349         return FALSE;
2350     }
2351
2352   /* Create one initial file */
2353   if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_1, "exec-1"))
2354     return FALSE;
2355
2356   return TRUE;
2357 }
2358
2359 static dbus_bool_t
2360 cleanup_service_reload_test (DBusString *dir)
2361 {
2362   if (!test_remove_directory (dir))
2363     return FALSE;
2364
2365   return TRUE;
2366 }
2367
2368 typedef struct
2369 {
2370   BusActivation *activation;
2371   const char    *service_name;
2372   dbus_bool_t    expecting_find;
2373 } CheckData;
2374
2375 static dbus_bool_t
2376 check_func (void *data)
2377 {
2378   CheckData          *d;
2379   BusActivationEntry *entry;
2380   DBusError           error;
2381   dbus_bool_t         ret_val;
2382
2383   ret_val = TRUE;
2384   d = data;
2385
2386   dbus_error_init (&error);
2387
2388   entry = activation_find_entry (d->activation, d->service_name, &error);
2389   if (entry == NULL)
2390     {
2391       if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2392         {
2393           ret_val = TRUE;
2394         }
2395       else
2396         {
2397           if (d->expecting_find)
2398             ret_val = FALSE;
2399         }
2400
2401       dbus_error_free (&error);
2402     }
2403   else
2404     {
2405       if (!d->expecting_find)
2406         ret_val = FALSE;
2407     }
2408
2409   return ret_val;
2410 }
2411
2412 static dbus_bool_t
2413 do_test (const char *description, dbus_bool_t oom_test, CheckData *data)
2414 {
2415   dbus_bool_t err;
2416
2417   if (oom_test)
2418     err = !_dbus_test_oom_handling (description, check_func, data);
2419   else
2420     err = !check_func (data);
2421
2422   if (err)
2423     _dbus_assert_not_reached ("Test failed");
2424
2425   return TRUE;
2426 }
2427
2428 static dbus_bool_t
2429 do_service_reload_test (DBusString *dir, dbus_bool_t oom_test)
2430 {
2431   BusActivation *activation;
2432   DBusString     address;
2433   DBusList      *directories;
2434   CheckData      d;
2435
2436   directories = NULL;
2437   _dbus_string_init_const (&address, "");
2438
2439   if (!_dbus_list_append (&directories, _dbus_string_get_data (dir)))
2440     return FALSE;
2441
2442   activation = bus_activation_new (NULL, &address, &directories, NULL);
2443   if (!activation)
2444     return FALSE;
2445
2446   d.activation = activation;
2447
2448   /* Check for existing service file */
2449   d.expecting_find = TRUE;
2450   d.service_name = SERVICE_NAME_1;
2451
2452   if (!do_test ("Existing service file", oom_test, &d))
2453     return FALSE;
2454
2455   /* Check for non-existing service file */
2456   d.expecting_find = FALSE;
2457   d.service_name = SERVICE_NAME_3;
2458
2459   if (!do_test ("Nonexisting service file", oom_test, &d))
2460     return FALSE;
2461
2462   /* Check for added service file */
2463   if (!test_create_service_file (dir, SERVICE_FILE_2, SERVICE_NAME_2, "exec-2"))
2464     return FALSE;
2465
2466   d.expecting_find = TRUE;
2467   d.service_name = SERVICE_NAME_2;
2468
2469   if (!do_test ("Added service file", oom_test, &d))
2470     return FALSE;
2471
2472   /* Check for removed service file */
2473   if (!test_remove_service_file (dir, SERVICE_FILE_2))
2474     return FALSE;
2475
2476   d.expecting_find = FALSE;
2477   d.service_name = SERVICE_FILE_2;
2478
2479   if (!do_test ("Removed service file", oom_test, &d))
2480     return FALSE;
2481
2482   /* Check for updated service file */
2483
2484   _dbus_sleep_milliseconds (1000); /* Sleep a second to make sure the mtime is updated */
2485
2486   if (!test_create_service_file (dir, SERVICE_FILE_1, SERVICE_NAME_3, "exec-3"))
2487     return FALSE;
2488
2489   d.expecting_find = TRUE;
2490   d.service_name = SERVICE_NAME_3;
2491
2492   if (!do_test ("Updated service file, part 1", oom_test, &d))
2493     return FALSE;
2494
2495   d.expecting_find = FALSE;
2496   d.service_name = SERVICE_NAME_1;
2497
2498   if (!do_test ("Updated service file, part 2", oom_test, &d))
2499     return FALSE;
2500
2501   bus_activation_unref (activation);
2502   _dbus_list_clear (&directories);
2503
2504   return TRUE;
2505 }
2506
2507 dbus_bool_t
2508 bus_activation_service_reload_test (const DBusString *test_data_dir)
2509 {
2510   DBusString directory;
2511
2512   if (!_dbus_string_init (&directory))
2513     return FALSE;
2514
2515   if (!_dbus_string_append (&directory, _dbus_get_tmpdir()))
2516     return FALSE;
2517
2518   if (!_dbus_string_append (&directory, "/dbus-reload-test-") ||
2519       !_dbus_generate_random_ascii (&directory, 6))
2520      {
2521        return FALSE;
2522      }
2523
2524   /* Do normal tests */
2525   if (!init_service_reload_test (&directory))
2526     _dbus_assert_not_reached ("could not initiate service reload test");
2527
2528   if (!do_service_reload_test (&directory, FALSE))
2529     ; /* Do nothing? */
2530
2531   /* Do OOM tests */
2532   if (!init_service_reload_test (&directory))
2533     _dbus_assert_not_reached ("could not initiate service reload test");
2534
2535   if (!do_service_reload_test (&directory, TRUE))
2536     ; /* Do nothing? */
2537
2538   /* Cleanup test directory */
2539   if (!cleanup_service_reload_test (&directory))
2540     return FALSE;
2541
2542   _dbus_string_free (&directory);
2543
2544   return TRUE;
2545 }
2546
2547 #endif /* DBUS_BUILD_TESTS */