activation.c 75.5 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 "desktop-file.h"
30
#include "dispatch.h"
31
#include "services.h"
32
#include "test.h"
33 34 35
#include "utils.h"
#include <dbus/dbus-internals.h>
#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>
John Palmieri's avatar
John Palmieri committed
40
#include <dbus/dbus-sysdeps.h>
41
#ifdef HAVE_ERRNO_H
42
#include <errno.h>
43
#endif
44

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

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

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

79 80 81 82
typedef struct BusPendingActivationEntry BusPendingActivationEntry;

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

  dbus_bool_t auto_activation;
94 95 96 97
};

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

110
#if 0
111 112 113 114
static BusServiceDirectory *
bus_service_directory_ref (BusServiceDirectory *dir)
{
  _dbus_assert (dir->refcount);
115

116 117 118 119
  dir->refcount++;

  return dir;
}
120
#endif
121 122 123 124

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

  _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);
}

141 142 143 144 145
static void
bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
{
  if (entry->activation_message)
    dbus_message_unref (entry->activation_message);
146

147 148
  if (entry->connection)
    dbus_connection_unref (entry->connection);
149

150 151 152
  dbus_free (entry);
}

153
static BusPendingActivation *
154 155 156 157
bus_pending_activation_ref (BusPendingActivation *pending_activation)
{
  _dbus_assert (pending_activation->refcount > 0);
  pending_activation->refcount += 1;
158 159

  return pending_activation;
160 161 162 163
}

static void
bus_pending_activation_unref (BusPendingActivation *pending_activation)
164 165
{
  DBusList *link;
166

167
  if (pending_activation == NULL) /* hash table requires this */
168 169
    return;

170 171 172 173 174
  _dbus_assert (pending_activation->refcount > 0);
  pending_activation->refcount -= 1;

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

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

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

186 187 188 189 190 191 192
  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");
193

194 195
      _dbus_babysitter_unref (pending_activation->babysitter);
    }
196

197
  dbus_free (pending_activation->service_name);
198
  dbus_free (pending_activation->exec);
199
  dbus_free (pending_activation->systemd_service);
200

201
  link = _dbus_list_get_first_link (&pending_activation->entries);
202 203 204 205 206 207 208

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

      bus_pending_activation_entry_free (entry);

209
      link = _dbus_list_get_next_link (&pending_activation->entries, link);
210
    }
211
  _dbus_list_clear (&pending_activation->entries);
212 213 214 215 216

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

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

218
  dbus_free (pending_activation);
219 220
}

221 222 223 224 225 226 227 228 229
static BusActivationEntry *
bus_activation_entry_ref (BusActivationEntry *entry)
{
  _dbus_assert (entry->refcount > 0);
  entry->refcount++;

  return entry;
}

230
static void
231
bus_activation_entry_unref (BusActivationEntry *entry)
232
{
233 234
  if (entry == NULL) /* hash table requires this */
    return;
235

236 237
  _dbus_assert (entry->refcount > 0);
  entry->refcount--;
238 239

  if (entry->refcount > 0)
240
    return;
241

242 243
  dbus_free (entry->name);
  dbus_free (entry->exec);
244
  dbus_free (entry->user);
245
  dbus_free (entry->filename);
246
  dbus_free (entry->systemd_service);
247 248

  dbus_free (entry);
249 250 251
}

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

266
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
267

268
  retval = FALSE;
269 270
  name = NULL;
  exec = NULL;
271
  user = NULL;
272
  exec_tmp = NULL;
273
  entry = NULL;
274 275
  systemd_service = NULL;

276 277
  dbus_error_init (&tmp_error);

278 279 280 281 282
  if (!_dbus_string_init (&file_path))
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
283

284 285 286 287
  if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
      !_dbus_concat_dir_and_file (&file_path, filename))
    {
      BUS_SET_OOM (error);
288
      goto out;
289
    }
290 291

  if (!_dbus_stat (&file_path, &stat_buf, NULL))
292 293
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
294
                      "Can't stat the service file\n");
295
      goto out;
296
    }
297

298
  if (!bus_desktop_file_get_string (desktop_file,
299 300
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_NAME,
301 302
                                    &name,
                                    error))
303
    goto out;
304 305

  if (!bus_desktop_file_get_string (desktop_file,
306 307
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_EXEC,
308
                                    &exec_tmp,
309
                                    error))
310
    goto out;
311

312 313 314 315 316 317 318 319 320 321 322 323
  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);
324

325 326 327 328 329 330 331 332 333 334 335
  /* 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);
336
          goto out;
337 338 339 340 341 342 343 344 345 346 347
        }
      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);

348 349 350 351 352 353 354 355 356 357 358
  /* 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);
359
          goto out;
360 361 362 363 364 365 366 367 368 369 370 371 372
        }
      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;
        }
    }

  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);

  entry = _dbus_hash_table_lookup_string (s_dir->entries,
373
                                          _dbus_string_get_const_data (filename));
374

375
  if (entry == NULL) /* New file */
376
    {
377 378 379 380
      /* FIXME we need a better-defined algorithm for which service file to
       * pick than "whichever one is first in the directory listing"
       */
      if (_dbus_hash_table_lookup_string (activation->entries, name))
381 382 383
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "Service %s already exists in activation entry list\n", name);
384
          goto out;
385
        }
386

387 388
      entry = dbus_new0 (BusActivationEntry, 1);
      if (entry == NULL)
389 390
        {
          BUS_SET_OOM (error);
391
          goto out;
392
        }
393

394 395
      entry->name = name;
      entry->exec = exec;
396
      entry->user = user;
397
      entry->systemd_service = systemd_service;
398
      entry->refcount = 1;
399

400 401 402 403 404
      /* ownership has been transferred to entry, do not free separately */
      name = NULL;
      exec = NULL;
      user = NULL;
      systemd_service = NULL;
405

406 407 408
      entry->s_dir = s_dir;
      entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
      if (!entry->filename)
409 410
        {
          BUS_SET_OOM (error);
411
          goto out;
412
        }
413 414

      if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
415 416
        {
          BUS_SET_OOM (error);
417
          goto out;
418
        }
419

420
      if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
421 422 423 424
        {
          /* Revert the insertion in the entries table */
          _dbus_hash_table_remove_string (activation->entries, entry->name);
          BUS_SET_OOM (error);
425
          goto out;
426
        }
427 428 429 430

      _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
    }
  else /* Just update the entry */
431
    {
432 433 434 435
      bus_activation_entry_ref (entry);
      _dbus_hash_table_remove_string (activation->entries, entry->name);

      if (_dbus_hash_table_lookup_string (activation->entries, name))
436
        {
437
          _dbus_verbose ("The new service name \"%s\" of service file \"%s\" is already in cache, ignoring\n",
438
                         name, _dbus_string_get_const_data (&file_path));
439 440 441
          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));
442
          goto out;
443
        }
444

445
      /* ownership has been transferred to entry, do not free separately */
446 447
      dbus_free (entry->name);
      entry->name = name;
448
      name = NULL;
449 450 451

      dbus_free (entry->exec);
      entry->exec = exec;
452
      exec = NULL;
453 454 455

      dbus_free (entry->user);
      entry->user = user;
456
      user = NULL;
457 458 459

      dbus_free (entry->systemd_service);
      entry->systemd_service = systemd_service;
460 461
      systemd_service = NULL;

462
      if (!_dbus_hash_table_insert_string (activation->entries,
463 464 465 466 467
                                           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 */
468
          _dbus_hash_table_remove_string (entry->s_dir->entries,
469
                                          entry->filename);
470
          goto out;
471
        }
472
    }
473

474
  entry->mtime = stat_buf.mtime;
475
  retval = TRUE;
476

477
out:
478
  /* if these have been transferred into entry, the variables will be NULL */
479
  dbus_free (exec_tmp);
480
  dbus_free (name);
481
  dbus_free (exec);
482
  dbus_free (user);
483
  dbus_free (systemd_service);
484 485 486 487
  _dbus_string_free (&file_path);

  if (entry)
    bus_activation_entry_unref (entry);
488

489
  return retval;
490 491 492 493
}

static dbus_bool_t
check_service_file (BusActivation       *activation,
494 495 496
                    BusActivationEntry  *entry,
                    BusActivationEntry **updated_entry,
                    DBusError           *error)
497 498 499 500 501 502 503 504 505
{
  DBusStat stat_buf;
  dbus_bool_t retval;
  BusActivationEntry *tmp_entry;
  DBusString file_path;
  DBusString filename;

  retval = TRUE;
  tmp_entry = entry;
506

507
  _dbus_string_init_const (&filename, entry->filename);
508

509
  if (!_dbus_string_init (&file_path))
510 511
    {
      BUS_SET_OOM (error);
512
      return FALSE;
513
    }
514

515 516
  if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
      !_dbus_concat_dir_and_file (&file_path, &filename))
517 518
    {
      BUS_SET_OOM (error);
519 520
      retval = FALSE;
      goto out;
521
    }
522

523 524 525
  if (!_dbus_stat (&file_path, &stat_buf, NULL))
    {
      _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
526
                     _dbus_string_get_const_data (&file_path));
527

528 529 530 531 532 533 534
      _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;
    }
535
  else
536
    {
537
      if (stat_buf.mtime > entry->mtime)
538 539 540
        {
          BusDesktopFile *desktop_file;
          DBusError tmp_error;
541

542
          dbus_error_init (&tmp_error);
543

544 545 546 547
          desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
          if (desktop_file == NULL)
            {
              _dbus_verbose ("Could not load %s: %s\n",
548
                             _dbus_string_get_const_data (&file_path),
549 550 551 552 553 554 555 556 557 558 559
                             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;
            }
560 561

          /* @todo We can return OOM or a DBUS_ERROR_FAILED error
562
           *       Handle these both better
563
           */
564 565 566 567 568 569 570 571 572 573 574 575 576
          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;
            }
577

578 579 580
          bus_desktop_file_free (desktop_file);
          retval = TRUE;
        }
581
    }
582

583 584 585 586 587 588
out:
  _dbus_string_free (&file_path);

  if (updated_entry != NULL)
    *updated_entry = tmp_entry;
  return retval;
589 590
}

591

592 593 594 595
/* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
 * hash entries it already added.
 */
static dbus_bool_t
596
update_directory (BusActivation       *activation,
597 598
                  BusServiceDirectory *s_dir,
                  DBusError           *error)
599 600 601
{
  DBusDirIter *iter;
  DBusString dir, filename;
602 603
  BusDesktopFile *desktop_file;
  DBusError tmp_error;
604
  dbus_bool_t retval;
605 606
  BusActivationEntry *entry;
  DBusString full_path;
607

608
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
609

610 611
  iter = NULL;
  desktop_file = NULL;
612

613
  _dbus_string_init_const (&dir, s_dir->dir_c);
614

615
  if (!_dbus_string_init (&filename))
616
    {
617 618
      BUS_SET_OOM (error);
      return FALSE;
619 620
    }

621
  if (!_dbus_string_init (&full_path))
622 623 624 625 626 627
    {
      BUS_SET_OOM (error);
      _dbus_string_free (&filename);
      return FALSE;
    }

628
  retval = FALSE;
629

630
  /* from this point it's safe to "goto out" */
631

632 633 634 635
  iter = _dbus_directory_open (&dir, error);
  if (iter == NULL)
    {
      _dbus_verbose ("Failed to open directory %s: %s\n",
636
                     s_dir->dir_c,
637
                     error ? error->message : "unknown");
638
      goto out;
639
    }
640

641
  /* Now read the files */
642 643
  dbus_error_init (&tmp_error);
  while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
644
    {
645
      _dbus_assert (!dbus_error_is_set (&tmp_error));
646

647
      _dbus_string_set_length (&full_path, 0);
648

649
      if (!_dbus_string_ends_with_c_str (&filename, ".service"))
650
        {
651
          _dbus_verbose ("Skipping non-.service file '%s'\n",
652
                         _dbus_string_get_const_data (&filename));
653 654
          continue;
        }
655 656

      entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
657
      if (entry) /* Already has this service file in the cache */
658 659 660
        {
          if (!check_service_file (activation, entry, NULL, error))
            goto out;
661

662 663
          continue;
        }
664

665
      if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
666
          !_dbus_concat_dir_and_file (&full_path, &filename))
667
        {
668 669 670
          BUS_SET_OOM (error);
          goto out;
        }
671

672
      /* New file */
673 674
      desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
      if (desktop_file == NULL)
675 676
        {
          _dbus_verbose ("Could not load %s: %s\n",
677
                         _dbus_string_get_const_data (&full_path),
678
                         tmp_error.message);
679 680 681 682

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
683
              goto out;
684
            }
685

686 687 688
          dbus_error_free (&tmp_error);
          continue;
        }
689

690
      /* @todo We can return OOM or a DBUS_ERROR_FAILED error
691
       *       Handle these both better
692
       */
693
      if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
694 695
        {
          bus_desktop_file_free (desktop_file);
696
          desktop_file = NULL;
697

698
          _dbus_verbose ("Could not add %s to activation entry list: %s\n",
699
                         _dbus_string_get_const_data (&full_path), tmp_error.message);
700 701 702 703

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
704
              goto out;
705 706 707
            }

          dbus_error_free (&tmp_error);
708 709
          continue;
        }
710 711 712 713 714 715 716
      else
        {
          bus_desktop_file_free (desktop_file);
          desktop_file = NULL;
          continue;
        }
    }
717

718 719 720
  if (dbus_error_is_set (&tmp_error))
    {
      dbus_move_error (&tmp_error, error);
721
      goto out;
722
    }
723

724
  retval = TRUE;
725

726 727 728 729 730
 out:
  if (!retval)
    _DBUS_ASSERT_ERROR_IS_SET (error);
  else
    _DBUS_ASSERT_ERROR_IS_CLEAR (error);
731

732 733 734 735
  if (iter != NULL)
    _dbus_directory_close (iter);
  _dbus_string_free (&filename);
  _dbus_string_free (&full_path);
736

737
  return retval;
738 739
}

740 741 742 743 744 745 746
static dbus_bool_t
populate_environment (BusActivation *activation)
{
  DBusString   key;
  DBusString   value;
  int          i;
  char       **environment;
747
  dbus_bool_t  retval = FALSE;
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802

  environment = _dbus_get_environment ();

  if (environment == NULL)
    return FALSE;

  if (!_dbus_string_init (&key))
    {
        dbus_free_string_array (environment);
        return FALSE;
    }

  if (!_dbus_string_init (&value))
    {
      _dbus_string_free (&key);
      dbus_free_string_array (environment);
      return FALSE;
    }

  for (i = 0; environment[i] != NULL; i++)
    {
      if (!_dbus_string_append (&key, environment[i]))
        break;

      if (_dbus_string_split_on_byte (&key, '=', &value))
        {
          char *hash_key, *hash_value;

          if (!_dbus_string_steal_data (&key, &hash_key))
            break;

          if (!_dbus_string_steal_data (&value, &hash_value))
            break;

          if (!_dbus_hash_table_insert_string (activation->environment,
                                               hash_key, hash_value))
            break;
        }
      _dbus_string_set_length (&key, 0);
      _dbus_string_set_length (&value, 0);
    }

  if (environment[i] != NULL)
    goto out;

  retval = TRUE;
out:

  _dbus_string_free (&key);
  _dbus_string_free (&value);
  dbus_free_string_array (environment);

  return retval;
}

803 804 805 806 807
dbus_bool_t
bus_activation_reload (BusActivation     *activation,
                       const DBusString  *address,
                       DBusList         **directories,
                       DBusError         *error)
808
{
809 810
  DBusList      *link;
  char          *dir;
811 812 813

  if (activation->server_address != NULL)
    dbus_free (activation->server_address);
814
  if (!_dbus_string_copy_data (address, &activation->server_address))
815 816 817 818
    {
      BUS_SET_OOM (error);
      goto failed;
    }
819 820 821

  if (activation->entries != NULL)
    _dbus_hash_table_unref (activation->entries);
822
  activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
823
                                             (DBusFreeFunction)bus_activation_entry_unref);
824
  if (activation->entries == NULL)
825 826 827 828
    {
      BUS_SET_OOM (error);
      goto failed;
    }
829

830 831
  if (activation->directories != NULL)
    _dbus_hash_table_unref (activation->directories);
832
  activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
833
                                                  (DBusFreeFunction)bus_service_directory_unref);
834 835

  if (activation->directories == NULL)
836 837 838 839
    {
      BUS_SET_OOM (error);
      goto failed;
    }
840

841 842
  link = _dbus_list_get_first_link (directories);
  while (link != NULL)
843
    {
844
      BusServiceDirectory *s_dir;
845

846 847
      dir = _dbus_strdup ((const char *) link->data);
      if (!dir)
848 849 850 851
        {
          BUS_SET_OOM (error);
          goto failed;
        }
852

853 854
      s_dir = dbus_new0 (BusServiceDirectory, 1);
      if (!s_dir)
855 856 857 858 859
        {
          dbus_free (dir);
          BUS_SET_OOM (error);
          goto failed;
        }
860 861 862

      s_dir->refcount = 1;
      s_dir->dir_c = dir;
863

864
      s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
865
                                             (DBusFreeFunction)bus_activation_entry_unref);
866 867

      if (!s_dir->entries)
868 869 870 871 872
        {
          bus_service_directory_unref (s_dir);
          BUS_SET_OOM (error);
          goto failed;
        }
873 874

      if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
875 876 877 878 879
        {
          bus_service_directory_unref (s_dir);
          BUS_SET_OOM (error);
          goto failed;
        }
880

881
      /* only fail on OOM, it is ok if we can't read the directory */
882
      if (!update_directory (activation, s_dir, error))
883 884
        {
          if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
885 886 887 888 889
            goto failed;
          else
            dbus_error_free (error);
        }

890
      link = _dbus_list_get_next_link (directories, link);
891
    }
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932
  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;
    }

933 934 935
  activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
                                                  (DBusFreeFunction) dbus_free,
                                                  (DBusFreeFunction) dbus_free);
936

937
  if (activation->environment == NULL)
938 939 940 941 942 943 944 945 946 947 948
    {
      BUS_SET_OOM (error);
      goto failed;
    }

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

949
  return activation;
950

951
 failed:
952
  bus_activation_unref (activation);
953 954 955
  return NULL;
}

956
BusActivation *
957 958 959
bus_activation_ref (BusActivation *activation)
{
  _dbus_assert (activation->refcount > 0);
960

961
  activation->refcount += 1;
962 963

  return activation;
964 965 966 967 968 969 970 971 972
}

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

  activation->refcount -= 1;

973 974
  if (activation->refcount > 0)
    return;
975

976 977 978 979 980
  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);
981
  if (activation->directories)
982
    _dbus_hash_table_unref (activation->directories);
983 984 985
  if (activation->environment)
    _dbus_hash_table_unref (activation->environment);

986
  dbus_free (activation);
987
}
988

989 990 991
static dbus_bool_t
add_bus_environment (BusActivation *activation,
                     DBusError     *error)
992
{
993
  const char *type;
994

995 996 997 998 999
  if (!bus_activation_set_environment_variable (activation,
                                                "DBUS_STARTER_ADDRESS",
                                                activation->server_address,
                                                error))
    return FALSE;
1000

1001 1002 1003
  type = bus_context_get_type (activation->context);
  if (type != NULL)
    {
1004 1005 1006 1007
      if (!bus_activation_set_environment_variable (activation,
                                                    "DBUS_STARTER_BUS_TYPE", type,
                                                    error))
        return FALSE;
1008 1009 1010

      if (strcmp (type, "session") == 0)
        {
1011 1012 1013 1014 1015
          if (!bus_activation_set_environment_variable (activation,
                                                        "DBUS_SESSION_BUS_ADDRESS",
                                                        activation->server_address,
                                                        error))
            return FALSE;
1016 1017 1018
        }
      else if (strcmp (type, "system") == 0)
        {
1019 1020 1021 1022 1023
          if (!bus_activation_set_environment_variable (activation,
                                                        "DBUS_SYSTEM_BUS_ADDRESS",
                                                        activation->server_address,
                                                        error))
            return FALSE;
1024
        }
1025
    }
1026 1027

  return TRUE;
1028 1029
}

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
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);
1047

1048 1049 1050 1051 1052
  _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);
1053

1054 1055 1056 1057
  d->hash_entry = NULL;
}

static void
1058
free_restore_pending_data (void *data)
1059 1060 1061 1062 1063 1064 1065 1066
{
  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);
1067

1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
  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;
1080

1081 1082
  d->pending_activation = pending_activation;
  d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
1083

1084
  bus_pending_activation_ref (d->pending_activation);
1085

1086 1087
  if (d->hash_entry == NULL ||
      !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
1088
                                        free_restore_pending_data))
1089
    {
1090
      free_restore_pending_data (d);
1091 1092 1093 1094
      return FALSE;
    }

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

1096 1097 1098
  return TRUE;
}

1099
dbus_bool_t
1100
bus_activation_service_created (BusActivation  *activation,
1101
                                const char     *service_name,
1102
                                BusTransaction *transaction,
1103
                                DBusError      *error)
1104 1105 1106 1107
{
  BusPendingActivation *pending_activation;
  DBusMessage *message;
  DBusList *link;
1108 1109

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1110

1111 1112 1113 1114 1115 1116
  /* Check if it's a pending activation */
  pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);

  if (!pending_activation)
    return TRUE;

1117 1118 1119 1120
  bus_context_log (activation->context,
                   DBUS_SYSTEM_LOG_INFO, "Successfully activated service '%s'",
                   service_name);

1121 1122 1123 1124 1125
  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);
1126

1127 1128
      /* entry->connection is NULL for activating systemd */
      if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1129 1130 1131 1132
        {
          /* Only send activation replies to regular activation requests. */
          if (!entry->auto_activation)
            {
1133
              dbus_uint32_t result;
1134

1135 1136 1137 1138 1139 1140
              message = dbus_message_new_method_return (entry->activation_message);
              if (!message)
                {
                  BUS_SET_OOM (error);
                  goto error;
                }
1141

1142
              result = DBUS_START_REPLY_SUCCESS;
1143

1144 1145 1146 1147 1148 1149 1150 1151
              if (!dbus_message_append_args (message,
                                             DBUS_TYPE_UINT32, &result,
                                             DBUS_TYPE_INVALID))
                {
                  dbus_message_unref (message);
                  BUS_SET_OOM (error);
                  goto error;
                }
1152

1153 1154 1155 1156 1157 1158
              if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
                {
                  dbus_message_unref (message);
                  BUS_SET_OOM (error);
                  goto error;
                }
1159

1160 1161 1162
              dbus_message_unref (message);
            }
        }
1163

1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
      link = next;
    }

  return TRUE;

 error:
  return FALSE;
}

dbus_bool_t
bus_activation_send_pending_auto_activation_messages (BusActivation  *activation,
1175
                                                      BusService     *service,
1176
                                                      BusTransaction *transaction)
1177 1178 1179 1180 1181 1182
{
  BusPendingActivation *pending_activation;
  DBusList *link;

  /* Check if it's a pending activation */
  pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
1183
                                                       bus_service_get_name (service));
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193

  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);

1194
      if (entry->auto_activation && (entry->connection == NULL || dbus_connection_get_is_connected (entry->connection)))
1195 1196
        {
          DBusConnection *addressed_recipient;
1197 1198 1199
          DBusError error;

          dbus_error_init (&error);
1200

1201
          addressed_recipient = bus_service_get_primary_owners_connection (service);
1202

1203 1204 1205 1206
          /* Resume dispatching where we left off in bus_dispatch() */
          if (!bus_dispatch_matches (transaction,
                                     entry->connection,
                                     addressed_recipient,
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
                                     entry->activation_message, &error))
            {
              /* If permission is denied, we just want to return the error
               * to the original method invoker; in particular, we don't
               * want to make the RequestName call fail with that error
               * (see fd.o #78979, CVE-2014-3477). */
              if (!bus_transaction_send_error_reply (transaction, entry->connection,
                                                     &error, entry->activation_message))
                {
                  bus_connection_send_oom_error (entry->connection,
                                                 entry->activation_message);
                }

              link = next;
              continue;
            }
1223
        }
1224 1225 1226

      link = next;
    }
1227 1228 1229 1230 1231 1232

  if (!add_restore_pending_to_transaction (transaction, pending_activation))
    {
      _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
      goto error;
    }
1233

1234
  _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1235 1236 1237 1238 1239 1240 1241

  return TRUE;

 error:
  return FALSE;
}

1242
/**
1243
 * FIXME @todo the error messages here would ideally be preallocated
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
 * so we don't need to allocate memory to send them.
 * Using the usual tactic, prealloc an OOM message, then
 * if we can't alloc the real error send the OOM error instead.
 */
static dbus_bool_t
try_send_activation_failure (BusPendingActivation *pending_activation,
                             const DBusError      *how)
{
  BusActivation *activation;
  DBusList *link;
  BusTransaction *transaction;
1255

1256 1257 1258 1259 1260
  activation = pending_activation->activation;

  transaction = bus_transaction_new (activation->context);
  if (transaction == NULL)
    return FALSE;
1261

1262 1263 1264 1265 1266
  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);
1267

1268
      if (entry->connection && dbus_connection_get_is_connected (entry->connection))
1269
        {
1270 1271 1272 1273
          if (!bus_transaction_send_error_reply (transaction,
                                                 entry->connection,
                                                 how,
                                                 entry->activation_message))
1274
            goto error;
1275
        }
1276

1277 1278 1279 1280
      link = next;
    }

  bus_transaction_execute_and_free (transaction);
1281