activation.c 80.3 KB
Newer Older
1
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 3 4
/* activation.c  Activation of services
 *
 * 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 11 12 13 14 15 16 17 18
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
19
 *
20 21
 * You should have received a copy of the GNU General Public License
 * 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 "config-parser.h"
30
#include "desktop-file.h"
31
#include "dispatch.h"
32
#include "services.h"
33
#include "test.h"
34 35 36
#include "utils.h"
#include <dbus/dbus-internals.h>
#include <dbus/dbus-hash.h>
37
#include <dbus/dbus-list.h>
38
#include <dbus/dbus-shell.h>
39
#include <dbus/dbus-spawn.h>
40
#include <dbus/dbus-timeout.h>
John Palmieri's avatar
John Palmieri committed
41
#include <dbus/dbus-sysdeps.h>
42
#ifdef HAVE_ERRNO_H
43
#include <errno.h>
44
#endif
45

46 47 48 49
struct BusActivation
{
  int refcount;
  DBusHashTable *entries;
50
  DBusHashTable *pending_activations;
51
  char *server_address;
52
  BusContext *context;
53 54 55
  int n_pending_activations; /**< This is in fact the number of BusPendingActivationEntry,
                              * i.e. number of pending activation requests, not pending
                              * activations per se
56
                              */
57
  DBusList *directories;
58
  DBusHashTable *environment;
59
};
60 61 62

typedef struct
{
63 64
  int refcount;
  char *dir_c;
65
  BusServiceDirFlags flags;
66 67 68
  DBusHashTable *entries;
} BusServiceDirectory;

69
struct BusActivationEntry
70 71
{
  int refcount;
72 73
  char *name;
  char *exec;
74
  char *user;
75
  char *systemd_service;
76
  char *assumed_apparmor_label;
77 78 79
  unsigned long mtime;
  BusServiceDirectory *s_dir;
  char *filename;
80
};
81

82 83 84 85
typedef struct BusPendingActivationEntry BusPendingActivationEntry;

struct BusPendingActivationEntry
{
86 87 88
  /* Normally a method call, but if connection is NULL, this is a signal
   * instead.
   */
89
  DBusMessage *activation_message;
90 91 92 93
  /* NULL if this activation entry is for the dbus-daemon itself,
   * waiting for systemd to start. In this case, auto_activation is always
   * TRUE.
   */
94
  DBusConnection *connection;
95 96

  dbus_bool_t auto_activation;
97 98 99 100
};

typedef struct
{
101
  int refcount;
102
  BusActivation *activation;
103
  char *service_name;
104
  char *exec;
105
  char *systemd_service;
106
  DBusList *entries;
107
  int n_entries;
108 109 110
  DBusBabysitter *babysitter;
  DBusTimeout *timeout;
  unsigned int timeout_added : 1;
111 112
} BusPendingActivation;

113
#if 0
114 115 116 117
static BusServiceDirectory *
bus_service_directory_ref (BusServiceDirectory *dir)
{
  _dbus_assert (dir->refcount);
118

119 120 121 122
  dir->refcount++;

  return dir;
}
123
#endif
124 125 126 127

static void
bus_service_directory_unref (BusServiceDirectory *dir)
{
128 129
  if (dir == NULL)
    return;
130 131 132 133 134 135 136 137 138 139 140 141 142 143

  _dbus_assert (dir->refcount > 0);
  dir->refcount--;

  if (dir->refcount > 0)
    return;

  if (dir->entries)
    _dbus_hash_table_unref (dir->entries);

  dbus_free (dir->dir_c);
  dbus_free (dir);
}

144 145 146 147 148
static void
bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
{
  if (entry->activation_message)
    dbus_message_unref (entry->activation_message);
149

150 151
  if (entry->connection)
    dbus_connection_unref (entry->connection);
152

153 154 155
  dbus_free (entry);
}

156
static BusPendingActivation *
157 158 159 160
bus_pending_activation_ref (BusPendingActivation *pending_activation)
{
  _dbus_assert (pending_activation->refcount > 0);
  pending_activation->refcount += 1;
161 162

  return pending_activation;
163 164 165 166
}

static void
bus_pending_activation_unref (BusPendingActivation *pending_activation)
167 168
{
  DBusList *link;
169

170
  if (pending_activation == NULL) /* hash table requires this */
171 172
    return;

173 174 175 176 177
  _dbus_assert (pending_activation->refcount > 0);
  pending_activation->refcount -= 1;

  if (pending_activation->refcount > 0)
    return;
178

179 180
  if (pending_activation->timeout_added)
    {
181
      _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
182
                                 pending_activation->timeout);
183 184 185 186 187
      pending_activation->timeout_added = FALSE;
    }

  if (pending_activation->timeout)
    _dbus_timeout_unref (pending_activation->timeout);
188

189 190 191 192 193 194 195
  if (pending_activation->babysitter)
    {
      if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
                                                 NULL, NULL, NULL,
                                                 pending_activation->babysitter,
                                                 NULL))
        _dbus_assert_not_reached ("setting watch functions to NULL failed");
196

197 198
      _dbus_babysitter_unref (pending_activation->babysitter);
    }
199

200
  dbus_free (pending_activation->service_name);
201
  dbus_free (pending_activation->exec);
202
  dbus_free (pending_activation->systemd_service);
203

204
  link = _dbus_list_get_first_link (&pending_activation->entries);
205 206 207 208 209 210 211

  while (link != NULL)
    {
      BusPendingActivationEntry *entry = link->data;

      bus_pending_activation_entry_free (entry);

212
      link = _dbus_list_get_next_link (&pending_activation->entries, link);
213
    }
214
  _dbus_list_clear (&pending_activation->entries);
215 216 217 218 219

  pending_activation->activation->n_pending_activations -=
    pending_activation->n_entries;

  _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
220

221
  dbus_free (pending_activation);
222 223
}

224 225 226 227 228 229 230 231 232
static BusActivationEntry *
bus_activation_entry_ref (BusActivationEntry *entry)
{
  _dbus_assert (entry->refcount > 0);
  entry->refcount++;

  return entry;
}

233
static void
234
bus_activation_entry_unref (BusActivationEntry *entry)
235
{
236 237
  if (entry == NULL) /* hash table requires this */
    return;
238

239 240
  _dbus_assert (entry->refcount > 0);
  entry->refcount--;
241 242

  if (entry->refcount > 0)
243
    return;
244

245 246
  dbus_free (entry->name);
  dbus_free (entry->exec);
247
  dbus_free (entry->user);
248
  dbus_free (entry->filename);
249
  dbus_free (entry->systemd_service);
250
  dbus_free (entry->assumed_apparmor_label);
251 252

  dbus_free (entry);
253 254 255
}

static dbus_bool_t
256
update_desktop_file_entry (BusActivation       *activation,
257 258 259 260
                           BusServiceDirectory *s_dir,
                           DBusString          *filename,
                           BusDesktopFile      *desktop_file,
                           DBusError           *error)
261
{
262
  char *name, *exec, *user, *exec_tmp, *systemd_service;
263
  char *assumed_apparmor_label;
264
  BusActivationEntry *entry;
265 266
  DBusStat stat_buf;
  DBusString file_path;
267
  DBusError tmp_error;
268
  dbus_bool_t retval;
269
  DBusString str;
270

271
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
272

273
  retval = FALSE;
274 275
  name = NULL;
  exec = NULL;
276
  user = NULL;
277
  exec_tmp = NULL;
278
  entry = NULL;
279
  systemd_service = NULL;
280
  assumed_apparmor_label = NULL;
281

282 283
  dbus_error_init (&tmp_error);

284 285 286 287 288
  if (!_dbus_string_init (&file_path))
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
289

290 291 292 293
  if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
      !_dbus_concat_dir_and_file (&file_path, filename))
    {
      BUS_SET_OOM (error);
294
      goto out;
295
    }
296 297

  if (!_dbus_stat (&file_path, &stat_buf, NULL))
298 299
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
300
                      "Can't stat the service file\n");
301
      goto out;
302
    }
303

304
  if (!bus_desktop_file_get_string (desktop_file,
305 306
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_NAME,
307 308
                                    &name,
                                    error))
309
    goto out;
310 311

  if (!bus_desktop_file_get_string (desktop_file,
312 313
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_EXEC,
314
                                    &exec_tmp,
315
                                    error))
316
    goto out;
317

318 319 320 321 322 323 324 325 326 327 328 329
  if (!_dbus_string_init (&str))
    goto out;

  if (!_dbus_string_append (&str, exec_tmp) ||
      !_dbus_replace_install_prefix (&str) ||
      !_dbus_string_steal_data (&str, &exec))
    {
      _dbus_string_free (&str);
      goto out;
    }

  _dbus_string_free (&str);
330

331 332 333 334 335 336 337 338 339 340 341
  /* user is not _required_ unless we are using system activation */
  if (!bus_desktop_file_get_string (desktop_file,
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_USER,
                                    &user, &tmp_error))
    {
      _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
      /* if we got OOM, then exit */
      if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
        {
          dbus_move_error (&tmp_error, error);
342
          goto out;
343 344 345 346 347 348 349 350 351 352 353
        }
      else
        {
          /* if we have error because we didn't find anything then continue */
          dbus_error_free (&tmp_error);
          dbus_free (user);
          user = NULL;
        }
    }
  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);

354 355 356 357 358 359 360 361 362 363 364
  /* systemd service is never required */
  if (!bus_desktop_file_get_string (desktop_file,
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_SYSTEMD_SERVICE,
                                    &systemd_service, &tmp_error))
    {
      _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
      /* if we got OOM, then exit */
      if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
        {
          dbus_move_error (&tmp_error, error);
365
          goto out;
366 367 368 369 370 371 372 373 374 375
        }
      else
        {
          /* if we have error because we didn't find anything then continue */
          dbus_error_free (&tmp_error);
          dbus_free (systemd_service);
          systemd_service = NULL;
        }
    }

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  /* assumed AppArmor label is never required */
  if (!bus_desktop_file_get_string (desktop_file,
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_ASSUMED_APPARMOR_LABEL,
                                    &assumed_apparmor_label, &tmp_error))
    {
      _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
      /* if we got OOM, then exit */
      if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
        {
          dbus_move_error (&tmp_error, error);
          goto out;
        }
      else
        {
          /* if we have error because we didn't find anything then continue */
          dbus_error_free (&tmp_error);
          dbus_free (assumed_apparmor_label);
          assumed_apparmor_label = NULL;
        }
    }

398 399 400
  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);

  entry = _dbus_hash_table_lookup_string (s_dir->entries,
401
                                          _dbus_string_get_const_data (filename));
402

403
  if (entry == NULL) /* New file */
404
    {
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440
      DBusString expected_name;

      if (!_dbus_string_init (&expected_name))
        {
          BUS_SET_OOM (error);
          goto out;
        }

      if (!_dbus_string_append (&expected_name, name) ||
          !_dbus_string_append (&expected_name, ".service"))
        {
          _dbus_string_free (&expected_name);
          BUS_SET_OOM (error);
          goto out;
        }

      if (_dbus_string_equal (&expected_name, filename))
        {
          _dbus_verbose ("Name of \"%s\" is as expected\n",
                         _dbus_string_get_const_data (&file_path));
        }
      else if (s_dir->flags & BUS_SERVICE_DIR_FLAGS_STRICT_NAMING)
        {
          bus_context_log_and_set_error (activation->context,
                                         DBUS_SYSTEM_LOG_WARNING, error,
                                         DBUS_ERROR_FAILED,
                                         "Service file \"%s\" should have "
                                         "been named \"%s\": not loading it",
                                         _dbus_string_get_const_data (&file_path),
                                         _dbus_string_get_const_data (&expected_name));
          _dbus_string_free (&expected_name);
          goto out;
        }
      else if (bus_context_get_servicehelper (activation->context) != NULL)
        {
          bus_context_log (activation->context, DBUS_SYSTEM_LOG_WARNING,
441
                           "Service file \"%s\" should have been named \"%s\" "
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
                           "and will not work with system bus activation",
                           _dbus_string_get_const_data (&file_path),
                           _dbus_string_get_const_data (&expected_name));
          /* We don't actually error out here, because *technically* it could
           * still work on systemd systems, where we tell systemd to start the
           * SystemdService instead of launching dbus-daemon-launch-helper
           * ourselves. But maybe we should:
           * https://bugs.freedesktop.org/show_bug.cgi?id=99874 */
        }
      else
        {
          /* We could maybe log mismatched names for session services in
           * a user-visible way too, but not until
           * https://lintian.debian.org/tags/dbus-session-service-wrong-name.html
           * is a bit shorter.
           * https://bugs.freedesktop.org/show_bug.cgi?id=99873 */
          _dbus_verbose ("Name of \"%s\" should canonically be \"%s\"\n",
                         _dbus_string_get_const_data (&file_path),
                         _dbus_string_get_const_data (&expected_name));
        }

      _dbus_string_free (&expected_name);

465 466
      /* FIXME we need a better-defined algorithm for which service file to
       * pick than "whichever one is first in the directory listing"
467
       * See also https://bugs.freedesktop.org/show_bug.cgi?id=99874
468 469
       */
      if (_dbus_hash_table_lookup_string (activation->entries, name))
470 471 472
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "Service %s already exists in activation entry list\n", name);
473
          goto out;
474
        }
475

476 477
      entry = dbus_new0 (BusActivationEntry, 1);
      if (entry == NULL)
478 479
        {
          BUS_SET_OOM (error);
480
          goto out;
481
        }
482

483 484
      entry->name = name;
      entry->exec = exec;
485
      entry->user = user;
486
      entry->systemd_service = systemd_service;
487
      entry->assumed_apparmor_label = assumed_apparmor_label;
488
      entry->refcount = 1;
489

490 491 492 493 494
      /* ownership has been transferred to entry, do not free separately */
      name = NULL;
      exec = NULL;
      user = NULL;
      systemd_service = NULL;
495
      assumed_apparmor_label = NULL;
496

497 498 499
      entry->s_dir = s_dir;
      entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
      if (!entry->filename)
500 501
        {
          BUS_SET_OOM (error);
502
          goto out;
503
        }
504 505

      if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
506 507
        {
          BUS_SET_OOM (error);
508
          goto out;
509
        }
510

511
      if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
512 513 514 515
        {
          /* Revert the insertion in the entries table */
          _dbus_hash_table_remove_string (activation->entries, entry->name);
          BUS_SET_OOM (error);
516
          goto out;
517
        }
518 519 520 521

      _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
    }
  else /* Just update the entry */
522
    {
523 524 525 526
      bus_activation_entry_ref (entry);
      _dbus_hash_table_remove_string (activation->entries, entry->name);

      if (_dbus_hash_table_lookup_string (activation->entries, name))
527
        {
528
          _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
529
                         name, _dbus_string_get_const_data (&file_path));
530 531 532
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
                          name, _dbus_string_get_const_data (&file_path));
533
          goto out;
534
        }
535

536
      /* ownership has been transferred to entry, do not free separately */
537 538
      dbus_free (entry->name);
      entry->name = name;
539
      name = NULL;
540 541 542

      dbus_free (entry->exec);
      entry->exec = exec;
543
      exec = NULL;
544 545 546

      dbus_free (entry->user);
      entry->user = user;
547
      user = NULL;
548 549 550

      dbus_free (entry->systemd_service);
      entry->systemd_service = systemd_service;
551 552
      systemd_service = NULL;

553 554 555 556
      dbus_free (entry->assumed_apparmor_label);
      entry->assumed_apparmor_label = assumed_apparmor_label;
      assumed_apparmor_label = NULL;

557
      if (!_dbus_hash_table_insert_string (activation->entries,
558 559 560 561 562
                                           entry->name, bus_activation_entry_ref(entry)))
        {
          BUS_SET_OOM (error);
          /* Also remove path to entries hash since we want this in sync with
           * the entries hash table */
563
          _dbus_hash_table_remove_string (entry->s_dir->entries,
564
                                          entry->filename);
565
          goto out;
566
        }
567
    }
568

569
  entry->mtime = stat_buf.mtime;
570
  retval = TRUE;
571

572
out:
573
  /* if these have been transferred into entry, the variables will be NULL */
574
  dbus_free (exec_tmp);
575
  dbus_free (name);
576
  dbus_free (exec);
577
  dbus_free (user);
578
  dbus_free (systemd_service);
579
  dbus_free (assumed_apparmor_label);
580 581 582 583
  _dbus_string_free (&file_path);

  if (entry)
    bus_activation_entry_unref (entry);
584

585
  return retval;
586 587 588 589
}

static dbus_bool_t
check_service_file (BusActivation       *activation,
590 591 592
                    BusActivationEntry  *entry,
                    BusActivationEntry **updated_entry,
                    DBusError           *error)
593 594 595 596 597 598 599 600 601
{
  DBusStat stat_buf;
  dbus_bool_t retval;
  BusActivationEntry *tmp_entry;
  DBusString file_path;
  DBusString filename;

  retval = TRUE;
  tmp_entry = entry;
602

603
  _dbus_string_init_const (&filename, entry->filename);
604

605
  if (!_dbus_string_init (&file_path))
606 607
    {
      BUS_SET_OOM (error);
608
      return FALSE;
609
    }
610

611 612
  if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
      !_dbus_concat_dir_and_file (&file_path, &filename))
613 614
    {
      BUS_SET_OOM (error);
615 616
      retval = FALSE;
      goto out;
617
    }
618

619 620 621
  if (!_dbus_stat (&file_path, &stat_buf, NULL))
    {
      _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
622
                     _dbus_string_get_const_data (&file_path));
623

624 625 626 627 628 629 630
      _dbus_hash_table_remove_string (activation->entries, entry->name);
      _dbus_hash_table_remove_string (entry->s_dir->entries, entry->filename);

      tmp_entry = NULL;
      retval = TRUE;
      goto out;
    }
631
  else
632
    {
633
      if (stat_buf.mtime > entry->mtime)
634 635 636
        {
          BusDesktopFile *desktop_file;
          DBusError tmp_error;
637

638
          dbus_error_init (&tmp_error);
639

640 641 642 643
          desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
          if (desktop_file == NULL)
            {
              _dbus_verbose ("Could not load %s: %s\n",
644
                             _dbus_string_get_const_data (&file_path),
645 646 647 648 649 650 651 652 653 654 655
                             tmp_error.message);
              if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
                {
                  dbus_move_error (&tmp_error, error);
                  retval = FALSE;
                  goto out;
                }
              dbus_error_free (&tmp_error);
              retval = TRUE;
              goto out;
            }
656 657

          /* @todo We can return OOM or a DBUS_ERROR_FAILED error
658
           *       Handle these both better
659
           */
660 661 662 663 664 665 666 667 668 669 670 671 672
          if (!update_desktop_file_entry (activation, entry->s_dir, &filename, desktop_file, &tmp_error))
            {
              bus_desktop_file_free (desktop_file);
              if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
                {
                  dbus_move_error (&tmp_error, error);
                  retval = FALSE;
                  goto out;
                }
              dbus_error_free (&tmp_error);
              retval = TRUE;
              goto out;
            }
673

674 675 676
          bus_desktop_file_free (desktop_file);
          retval = TRUE;
        }
677
    }
678

679 680 681 682 683 684
out:
  _dbus_string_free (&file_path);

  if (updated_entry != NULL)
    *updated_entry = tmp_entry;
  return retval;
685 686
}

687

688 689 690 691
/* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
 * hash entries it already added.
 */
static dbus_bool_t
692
update_directory (BusActivation       *activation,
693 694
                  BusServiceDirectory *s_dir,
                  DBusError           *error)
695 696 697
{
  DBusDirIter *iter;
  DBusString dir, filename;
698 699
  BusDesktopFile *desktop_file;
  DBusError tmp_error;
700
  dbus_bool_t retval;
701 702
  BusActivationEntry *entry;
  DBusString full_path;
703

704
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
705

706 707
  iter = NULL;
  desktop_file = NULL;
708

709
  _dbus_string_init_const (&dir, s_dir->dir_c);
710

711
  if (!_dbus_string_init (&filename))
712
    {
713 714
      BUS_SET_OOM (error);
      return FALSE;
715 716
    }

717
  if (!_dbus_string_init (&full_path))
718 719 720 721 722 723
    {
      BUS_SET_OOM (error);
      _dbus_string_free (&filename);
      return FALSE;
    }

724
  retval = FALSE;
725

726
  /* from this point it's safe to "goto out" */
727

728 729 730 731
  iter = _dbus_directory_open (&dir, error);
  if (iter == NULL)
    {
      _dbus_verbose ("Failed to open directory %s: %s\n",
732
                     s_dir->dir_c,
733
                     error ? error->message : "unknown");
734
      goto out;
735
    }
736

737
  /* Now read the files */
738 739
  dbus_error_init (&tmp_error);
  while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
740
    {
741
      _dbus_assert (!dbus_error_is_set (&tmp_error));
742

743
      _dbus_string_set_length (&full_path, 0);
744

745
      if (!_dbus_string_ends_with_c_str (&filename, ".service"))
746
        {
747
          _dbus_verbose ("Skipping non-.service file '%s'\n",
748
                         _dbus_string_get_const_data (&filename));
749 750
          continue;
        }
751 752

      entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
753
      if (entry) /* Already has this service file in the cache */
754 755 756
        {
          if (!check_service_file (activation, entry, NULL, error))
            goto out;
757

758 759
          continue;
        }
760

761
      if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
762
          !_dbus_concat_dir_and_file (&full_path, &filename))
763
        {
764 765 766
          BUS_SET_OOM (error);
          goto out;
        }
767

768
      /* New file */
769 770
      desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
      if (desktop_file == NULL)
771 772
        {
          _dbus_verbose ("Could not load %s: %s\n",
773
                         _dbus_string_get_const_data (&full_path),
774
                         tmp_error.message);
775 776 777 778

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
779
              goto out;
780
            }
781

782 783 784
          dbus_error_free (&tmp_error);
          continue;
        }
785

786
      /* @todo We can return OOM or a DBUS_ERROR_FAILED error
787
       *       Handle these both better
788
       */
789
      if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
790 791
        {
          bus_desktop_file_free (desktop_file);
792
          desktop_file = NULL;
793

794
          _dbus_verbose ("Could not add %s to activation entry list: %s\n",
795
                         _dbus_string_get_const_data (&full_path), tmp_error.message);
796 797 798 799

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
800
              goto out;
801 802 803
            }

          dbus_error_free (&tmp_error);
804 805
          continue;
        }
806 807 808 809 810 811 812
      else
        {
          bus_desktop_file_free (desktop_file);
          desktop_file = NULL;
          continue;
        }
    }
813

814 815 816
  if (dbus_error_is_set (&tmp_error))
    {
      dbus_move_error (&tmp_error, error);
817
      goto out;
818
    }
819

820
  retval = TRUE;
821

822 823 824 825 826
 out:
  if (!retval)
    _DBUS_ASSERT_ERROR_IS_SET (error);
  else
    _DBUS_ASSERT_ERROR_IS_CLEAR (error);
827

828 829 830 831
  if (iter != NULL)
    _dbus_directory_close (iter);
  _dbus_string_free (&filename);
  _dbus_string_free (&full_path);
832

833
  return retval;
834 835
}

836 837 838 839
static dbus_bool_t
populate_environment (BusActivation *activation)
{
  char       **environment;
840
  dbus_bool_t  retval = FALSE;
841 842 843 844 845 846

  environment = _dbus_get_environment ();

  if (environment == NULL)
    return FALSE;

847
  retval = _dbus_hash_table_from_array (activation->environment, environment, '=');
848 849 850 851 852
  dbus_free_string_array (environment);

  return retval;
}

853 854 855 856 857
dbus_bool_t
bus_activation_reload (BusActivation     *activation,
                       const DBusString  *address,
                       DBusList         **directories,
                       DBusError         *error)
858
{
859 860
  DBusList      *link;
  char          *dir;
861 862 863

  if (activation->server_address != NULL)
    dbus_free (activation->server_address);
864
  if (!_dbus_string_copy_data (address, &activation->server_address))
865 866 867 868
    {
      BUS_SET_OOM (error);
      goto failed;
    }
869 870 871

  if (activation->entries != NULL)
    _dbus_hash_table_unref (activation->entries);
872
  activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
873
                                             (DBusFreeFunction)bus_activation_entry_unref);
874
  if (activation->entries == NULL)
875 876 877 878
    {
      BUS_SET_OOM (error);
      goto failed;
    }
879

880 881 882
  _dbus_list_foreach (&activation->directories,
                      (DBusForeachFunction) bus_service_directory_unref, NULL);
  _dbus_list_clear (&activation->directories);
883

884 885
  link = _dbus_list_get_first_link (directories);
  while (link != NULL)
886
    {
887
      BusConfigServiceDir *config = link->data;
888
      BusServiceDirectory *s_dir;
889

890 891 892
      _dbus_assert (config->path != NULL);

      dir = _dbus_strdup (config->path);
893
      if (!dir)
894 895 896 897
        {
          BUS_SET_OOM (error);
          goto failed;
        }
898

899 900
      s_dir = dbus_new0 (BusServiceDirectory, 1);
      if (!s_dir)
901 902 903 904 905
        {
          dbus_free (dir);
          BUS_SET_OOM (error);
          goto failed;
        }
906 907 908

      s_dir->refcount = 1;
      s_dir->dir_c = dir;
909
      s_dir->flags = config->flags;
910

911
      s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
912
                                             (DBusFreeFunction)bus_activation_entry_unref);
913 914

      if (!s_dir->entries)
915 916 917 918 919
        {
          bus_service_directory_unref (s_dir);
          BUS_SET_OOM (error);
          goto failed;
        }
920

921
      if (!_dbus_list_append (&activation->directories, s_dir))
922 923 924 925 926
        {
          bus_service_directory_unref (s_dir);
          BUS_SET_OOM (error);
          goto failed;
        }
927

928
      /* only fail on OOM, it is ok if we can't read the directory */
929
      if (!update_directory (activation, s_dir, error))
930 931
        {
          if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
932 933 934 935 936
            goto failed;
          else
            dbus_error_free (error);
        }

937
      link = _dbus_list_get_next_link (directories, link);
938
    }
939

940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
  return TRUE;
 failed:
  return FALSE;
}

BusActivation*
bus_activation_new (BusContext        *context,
                    const DBusString  *address,
                    DBusList         **directories,
                    DBusError         *error)
{
  BusActivation *activation;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  activation = dbus_new0 (BusActivation, 1);
  if (activation == NULL)
    {
      BUS_SET_OOM (error);
      return NULL;
    }

  activation->refcount = 1;
  activation->context = context;
  activation->n_pending_activations = 0;

  if (!bus_activation_reload (activation, address, directories, error))
    goto failed;

   /* Initialize this hash table once, we don't want to lose pending
   * activations on reload. */
  activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
                                                          (DBusFreeFunction)bus_pending_activation_unref);

  if (activation->pending_activations == NULL)
    {
      BUS_SET_OOM (error);
      goto failed;
    }

980 981 982
  activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
                                                  (DBusFreeFunction) dbus_free,
                                                  (DBusFreeFunction) dbus_free);
983

984
  if (activation->environment == NULL)
985 986 987 988 989 990 991 992 993 994 995
    {
      BUS_SET_OOM (error);
      goto failed;
    }

  if (!populate_environment (activation))
    {
      BUS_SET_OOM (error);
      goto failed;
    }

996
  return activation;
997

998
 failed:
999
  bus_activation_unref (activation);
1000 1001 1002
  return NULL;
}

1003
BusActivation *
1004 1005 1006
bus_activation_ref (BusActivation *activation)
{
  _dbus_assert (activation->refcount > 0);
1007

1008
  activation->refcount += 1;
1009 1010

  return activation;
1011 1012 1013 1014 1015 1016 1017 1018 1019
}

void
bus_activation_unref (BusActivation *activation)
{
  _dbus_assert (activation->refcount > 0);

  activation->refcount -= 1;

1020 1021
  if (activation->refcount > 0)
    return;
1022

1023 1024 1025 1026 1027
  dbus_free (activation->server_address);
  if (activation->entries)
    _dbus_hash_table_unref (activation->entries);
  if (activation->pending_activations)
    _dbus_hash_table_unref (activation->pending_activations);
1028 1029 1030 1031 1032

  _dbus_list_foreach (&activation->directories,
                      (DBusForeachFunction) bus_service_directory_unref, NULL);
  _dbus_list_clear (&activation->directories);

1033 1034 1035
  if (activation->environment)
    _dbus_hash_table_unref (activation->environment);

1036
  dbus_free (activation);
1037
}
1038

1039 1040 1041
static dbus_bool_t
add_bus_environment (BusActivation *activation,
                     DBusError     *error)
1042
{
1043
  const char *type;
1044

1045 1046 1047 1048 1049
  if (!bus_activation_set_environment_variable (activation,
                                                "DBUS_STARTER_ADDRESS",
                                                activation->server_address,
                                                error))
    return FALSE;
1050

1051 1052 1053
  type = bus_context_get_type (activation->context);
  if (type != NULL)
    {
1054 1055 1056 1057
      if (!bus_activation_set_environment_variable (activation,
                                                    "DBUS_STARTER_BUS_TYPE", type,
                                                    error))
        return FALSE;
1058 1059 1060

      if (strcmp (type, "session") == 0)
        {
1061 1062 1063 1064 1065
          if (!bus_activation_set_environment_variable (activation,
                                                        "DBUS_SESSION_BUS_ADDRESS",
                                                        activation->server_address,
                                                        error))
            return FALSE;
1066 1067 1068
        }
      else if (strcmp (type, "system") == 0)
        {
1069 1070 1071 1072 1073
          if (!bus_activation_set_environment_variable (activation,
                                                        "DBUS_SYSTEM_BUS_ADDRESS",
                                                        activation->server_address,
                                                        error))
            return FALSE;
1074
        }
1075
    }
1076 1077

  return TRUE;
1078 1079
}

1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096
typedef struct
{
  BusPendingActivation *pending_activation;
  DBusPreallocatedHash *hash_entry;
} RestorePendingData;

static void
restore_pending (void *data)
{
  RestorePendingData *d = data;

  _dbus_assert (d->pending_activation != NULL);
  _dbus_assert (d->hash_entry != NULL);

  _dbus_verbose ("Restoring pending activation for service %s, has timeout = %d\n",
                 d->pending_activation->service_name,
                 d->pending_activation->timeout_added);
1097

1098 1099 1100 1101 1102
  _dbus_hash_table_insert_string_preallocated (d->pending_activation->activation->pending_activations,
                                               d->hash_entry,
                                               d->pending_activation->service_name, d->pending_activation);

  bus_pending_activation_ref (d->pending_activation);
1103

1104 1105 1106 1107
  d->hash_entry = NULL;
}

static void
1108
free_restore_pending_data (void *data)
1109 1110 1111 1112 1113 1114 1115 1116
{
  RestorePendingData *d = data;

  if (d->hash_entry)
    _dbus_hash_table_free_preallocated_entry (d->pending_activation->activation->pending_activations,
                                              d->hash_entry);

  bus_pending_activation_unref (d->pending_activation);
1117

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
  dbus_free (d);
}

static dbus_bool_t
add_restore_pending_to_transaction (BusTransaction       *transaction,
                                    BusPendingActivation *pending_activation)
{
  RestorePendingData *d;

  d = dbus_new (RestorePendingData, 1);
  if (d == NULL)
    return FALSE;
1130

1131 1132
  d->pending_activation = pending_activation;
  d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1133

1134
  bus_pending_activation_ref (d->pending_activation);
1135

1136 1137
  if (d->hash_entry == NULL ||
      !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1138
                                        free_restore_pending_data))
1139
    {
1140
      free_restore_pending_data (d);
1141 1142 1143 1144
      return FALSE;
    }

  _dbus_verbose ("Saved pending activation to be restored if the transaction fails\n");
1145

1146 1147 1148
  return TRUE;
}

1149
dbus_bool_t
1150
bus_activation_service_created (BusActivation  *activation,
1151
                                const char     *service_name,
1152
                                BusTransaction *transaction,
1153
                                DBusError      *error)
1154 1155 1156 1157
{
  BusPendingActivation *pending_activation;
  DBusMessage *message;
  DBusList *link;
1158 1159

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1160

1161 1162 1163 1164 1165 1166
  /* Check if it's a pending activation */
  pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);

  if (!pending_activation)
    return TRUE;

1167 1168 1169 1170
  bus_context_log (activation->context,
                   DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
                   service_name);

1171 1172 1173 1174 1175
  link = _dbus_list_get_first_link (&pending_activation->entries);
  while (link != NULL)
    {
      BusPendingActivationEntry *entry = link->data;
      DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);
1176

1177 1178
      /* entry->connection is NULL for activating systemd */
      if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1179 1180 1181 1182
        {
          /* Only send activation replies to regular activation requests. */
          if (!entry->auto_activation)
            {
1183
              dbus_uint32_t result;
1184

1185 1186 1187 1188 1189 1190
              message = dbus_message_new_method_return (entry->activation_message);
              if (!message)
                {
                  BUS_SET_OOM (error);
                  goto error;
                }
1191

1192
              result = DBUS_START_REPLY_SUCCESS;
1193

1194 1195 1196 1197 1198 1199 1200 1201
              if (!dbus_message_append_args (message,
                                             DBUS_TYPE_UINT32, &result,
                                             DBUS_TYPE_INVALID))
                {
                  dbus_message_unref (message);
                  BUS_SET_OOM (error);
                  goto error;
                }
1202

1203 1204 1205 1206 1207 1208
              if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
                {
                  dbus_message_unref (message);
                  BUS_SET_OOM (error);
                  goto error;
                }
1209

1210 1211 1212
              dbus_message_unref (message);
            }
        }
1213

1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
      link = next;
    }

  return TRUE;

 error:
  return FALSE;
}

dbus_bool_t
bus_activation_send_pending_auto_activation_messages (BusActivation  *activation,
1225
                                                      BusService     *service,
1226
                                                      BusTransaction *transaction)
1227 1228 1229 1230 1231 1232
{
  BusPendingActivation *pending_activation;
  DBusList *link;

  /* Check if it's a pending activation */
  pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1233
                                                       bus_service_get_name (service));
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243

  if (!pending_activation)
    return TRUE;

  link = _dbus_list_get_first_link (&pending_activation->entries);
  while (link != NULL)
    {
      BusPendingActivationEntry *entry = link->data;
      DBusList *next = _dbus_list_get_next_link (&pending_activation->entries, link);

1244
      if (entry->auto_activation && (entry->connection == NULL || dbus_connection_get_is_connected (entry->connection)))
1245 1246
        {
          DBusConnection *addressed_recipient;
1247 1248 1249
          DBusError error;

          dbus_error_init (&error);
1250

1251
          addressed_recipient = bus_service_get_primary_owners_connection (service);
1252

1253 1254 1255 1256
          /* Resume dispatching where we left off in bus_dispatch() */
          if (!bus_dispatch_matches (transaction,
                                     entry->connection,
                                     addressed_recipient,
1257 1258 1259 1260 1261