~ubuntu-branches/ubuntu/natty/dbus/natty

« back to all changes in this revision

Viewing changes to .pc/0005-activation-implement-upstart-activation.patch/bus/activation.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2010-12-22 17:00:17 UTC
  • Revision ID: james.westby@ubuntu.com-20101222170017-nzyo7q1lvdkgjh7e
Tags: 1.4.1-0ubuntu2
* Apply patches to implement Upstart service activation.
* Switch to using Upstart activation for the system bus.

Show diffs side-by-side

added added

removed removed

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