activation.c 64.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 19 20 21
 * 
 * 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.
 * 
 * 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 74 75
  unsigned long mtime;
  BusServiceDirectory *s_dir;
  char *filename;
76 77
} BusActivationEntry;

78 79 80 81 82 83
typedef struct BusPendingActivationEntry BusPendingActivationEntry;

struct BusPendingActivationEntry
{
  DBusMessage *activation_message;
  DBusConnection *connection;
84 85

  dbus_bool_t auto_activation;
86 87 88 89
};

typedef struct
{
90
  int refcount;
91
  BusActivation *activation;
92
  char *service_name;
93
  char *exec;
94
  DBusList *entries;
95
  int n_entries;
96 97 98
  DBusBabysitter *babysitter;
  DBusTimeout *timeout;
  unsigned int timeout_added : 1;
99 100
} BusPendingActivation;

101
#if 0
102 103 104 105 106 107 108 109 110
static BusServiceDirectory *
bus_service_directory_ref (BusServiceDirectory *dir)
{
  _dbus_assert (dir->refcount);
  
  dir->refcount++;

  return dir;
}
111
#endif
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

static void
bus_service_directory_unref (BusServiceDirectory *dir)
{
  if (dir == NULL) 
    return; 

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

132 133 134 135 136 137 138 139
static void
bus_pending_activation_entry_free (BusPendingActivationEntry *entry)
{
  if (entry->activation_message)
    dbus_message_unref (entry->activation_message);
  
  if (entry->connection)
    dbus_connection_unref (entry->connection);
140
  
141 142 143 144
  dbus_free (entry);
}

static void
145 146 147 148 149 150
handle_timeout_callback (DBusTimeout   *timeout,
                         void          *data)
{
  BusPendingActivation *pending_activation = data;

  while (!dbus_timeout_handle (pending_activation->timeout))
151
    _dbus_wait_for_memory ();
152 153
}

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

  return pending_activation;
161 162 163 164
}

static void
bus_pending_activation_unref (BusPendingActivation *pending_activation)
165 166 167
{
  DBusList *link;
  
168
  if (pending_activation == NULL) /* hash table requires this */
169 170
    return;

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

  if (pending_activation->refcount > 0)
    return;
  
177 178
  if (pending_activation->timeout_added)
    {
179 180 181
      _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
                                 pending_activation->timeout,
                                 handle_timeout_callback, pending_activation);
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
      pending_activation->timeout_added = FALSE;
    }

  if (pending_activation->timeout)
    _dbus_timeout_unref (pending_activation->timeout);
  
  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");
      
      _dbus_babysitter_unref (pending_activation->babysitter);
    }
  
  dbus_free (pending_activation->service_name);
200
  dbus_free (pending_activation->exec);
201

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

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

      bus_pending_activation_entry_free (entry);

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

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

  _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
218
  
219
  dbus_free (pending_activation);
220 221
}

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

  return entry;
}

231
static void
232
bus_activation_entry_unref (BusActivationEntry *entry)
233
{
234 235 236 237 238 239 240
  if (entry == NULL) /* hash table requires this */
    return;
  
  _dbus_assert (entry->refcount > 0);
  entry->refcount--;
  
  if (entry->refcount > 0) 
241 242 243 244
    return;
  
  dbus_free (entry->name);
  dbus_free (entry->exec);
245
  dbus_free (entry->user);
246
  dbus_free (entry->filename);
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;
259
  BusActivationEntry *entry;
260 261
  DBusStat stat_buf;
  DBusString file_path;
262
  DBusError tmp_error;
263

264 265
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
266 267
  name = NULL;
  exec = NULL;
268
  user = NULL;
269
  entry = NULL;
270
  
271 272
  dbus_error_init (&tmp_error);

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  if (!_dbus_string_init (&file_path))
    {
      BUS_SET_OOM (error);
      return FALSE;
    }
 
  if (!_dbus_string_append (&file_path, s_dir->dir_c) ||
      !_dbus_concat_dir_and_file (&file_path, filename))
    {
      BUS_SET_OOM (error);
      goto failed;
    }
 
  if (!_dbus_stat (&file_path, &stat_buf, NULL)) 
    {
      dbus_set_error (error, DBUS_ERROR_FAILED,
289
                      "Can't stat the service file\n");
290 291 292
      goto failed;
    }
 
293
  if (!bus_desktop_file_get_string (desktop_file,
294 295
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_NAME,
296 297 298
                                    &name,
                                    error))
    goto failed;
299 300

  if (!bus_desktop_file_get_string (desktop_file,
301 302
                                    DBUS_SERVICE_SECTION,
                                    DBUS_SERVICE_EXEC,
303 304 305
                                    &exec,
                                    error))
    goto failed;
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  /* 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);
          goto failed;
        }
      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);

330
  entry = _dbus_hash_table_lookup_string (s_dir->entries, 
331
                                          _dbus_string_get_const_data (filename));
332 333 334 335 336 337
  if (entry == NULL) /* New file */
    { 
      /* 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))
338 339 340 341 342
        {
          dbus_set_error (error, DBUS_ERROR_FAILED,
                          "Service %s already exists in activation entry list\n", name);
          goto failed;
        }
343 344 345
      
      entry = dbus_new0 (BusActivationEntry, 1);
      if (entry == NULL)
346 347 348 349
        {
          BUS_SET_OOM (error);
          goto failed;
        }
350 351 352
     
      entry->name = name;
      entry->exec = exec;
353
      entry->user = user;
354 355 356 357 358
      entry->refcount = 1;
    
      entry->s_dir = s_dir;
      entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
      if (!entry->filename)
359 360 361 362
        {
          BUS_SET_OOM (error);
          goto failed;
        }
363 364

      if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
365 366 367 368
        {
          BUS_SET_OOM (error);
          goto failed;
        }
369 370
     
      if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
371 372 373 374 375 376
        {
          /* Revert the insertion in the entries table */
          _dbus_hash_table_remove_string (activation->entries, entry->name);
          BUS_SET_OOM (error);
          goto failed;
        }
377 378 379 380

      _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
    }
  else /* Just update the entry */
381
    {
382 383 384 385
      bus_activation_entry_ref (entry);
      _dbus_hash_table_remove_string (activation->entries, entry->name);

      if (_dbus_hash_table_lookup_string (activation->entries, name))
386 387 388 389 390
        {
          _dbus_verbose ("The new service name \"%s\" of service file \"%s\" already in cache, ignoring\n",
                         name, _dbus_string_get_const_data (&file_path));
          goto failed;
        }
391 392 393
 
      dbus_free (entry->name);
      dbus_free (entry->exec);
394
      dbus_free (entry->user);
395 396
      entry->name = name;
      entry->exec = exec;
397
      entry->user = user;
398
      if (!_dbus_hash_table_insert_string (activation->entries,
399 400 401 402 403 404 405 406 407 408
                                           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 */
          _dbus_hash_table_remove_string (entry->s_dir->entries, 
                                          entry->filename);
          bus_activation_entry_unref (entry);
          return FALSE;
        }
409 410
    }
  
411 412 413 414 415 416 417 418 419 420
  entry->mtime = stat_buf.mtime;
  
  _dbus_string_free (&file_path);
  bus_activation_entry_unref (entry);

  return TRUE;

failed:
  dbus_free (name);
  dbus_free (exec);
421
  dbus_free (user);
422 423 424 425 426 427 428 429 430 431
  _dbus_string_free (&file_path);

  if (entry)
    bus_activation_entry_unref (entry);
  
  return FALSE;
}

static dbus_bool_t
check_service_file (BusActivation       *activation,
432 433 434
                    BusActivationEntry  *entry,
                    BusActivationEntry **updated_entry,
                    DBusError           *error)
435 436 437 438 439 440 441 442 443 444 445 446 447
{
  DBusStat stat_buf;
  dbus_bool_t retval;
  BusActivationEntry *tmp_entry;
  DBusString file_path;
  DBusString filename;

  retval = TRUE;
  tmp_entry = entry;
  
  _dbus_string_init_const (&filename, entry->filename);
  
  if (!_dbus_string_init (&file_path))
448 449
    {
      BUS_SET_OOM (error);
450
      return FALSE;
451
    }
452 453 454
 
  if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
      !_dbus_concat_dir_and_file (&file_path, &filename))
455 456
    {
      BUS_SET_OOM (error);
457 458
      retval = FALSE;
      goto out;
459
    }
460
  
461 462 463
  if (!_dbus_stat (&file_path, &stat_buf, NULL))
    {
      _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
464
                     _dbus_string_get_const_data (&file_path));
465

466 467 468 469 470 471 472 473 474 475
      _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;
    }
  else 
    {
      if (stat_buf.mtime > entry->mtime) 
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
        {
          BusDesktopFile *desktop_file;
          DBusError tmp_error;
          
          dbus_error_init (&tmp_error);
          
          desktop_file = bus_desktop_file_load (&file_path, &tmp_error);
          if (desktop_file == NULL)
            {
              _dbus_verbose ("Could not load %s: %s\n",
                             _dbus_string_get_const_data (&file_path), 
                             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;
            }
498 499 500 501
         
          /* @todo We can return OOM or a DBUS_ERROR_FAILED error 
           *       Handle these both better
           */ 
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
          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;
            }
         
          bus_desktop_file_free (desktop_file);
          retval = TRUE;
        }
519
    }
520
  
521 522 523 524 525 526
out:
  _dbus_string_free (&file_path);

  if (updated_entry != NULL)
    *updated_entry = tmp_entry;
  return retval;
527 528
}

529

530 531 532 533
/* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
 * hash entries it already added.
 */
static dbus_bool_t
534
update_directory (BusActivation       *activation,
535 536
                  BusServiceDirectory *s_dir,
                  DBusError           *error)
537 538 539
{
  DBusDirIter *iter;
  DBusString dir, filename;
540 541
  BusDesktopFile *desktop_file;
  DBusError tmp_error;
542
  dbus_bool_t retval;
543 544
  BusActivationEntry *entry;
  DBusString full_path;
545
  
546
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
547 548 549
  
  iter = NULL;
  desktop_file = NULL;
550
  
551 552
  _dbus_string_init_const (&dir, s_dir->dir_c);
  
553
  if (!_dbus_string_init (&filename))
554
    {
555 556
      BUS_SET_OOM (error);
      return FALSE;
557 558
    }

559
  if (!_dbus_string_init (&full_path))
560 561 562 563 564 565
    {
      BUS_SET_OOM (error);
      _dbus_string_free (&filename);
      return FALSE;
    }

566
  retval = FALSE;
567

568
  /* from this point it's safe to "goto out" */
569 570 571 572 573
  
  iter = _dbus_directory_open (&dir, error);
  if (iter == NULL)
    {
      _dbus_verbose ("Failed to open directory %s: %s\n",
574 575
                     s_dir->dir_c, 
                     error ? error->message : "unknown");
576
      goto out;
577
    }
578 579
  
  /* Now read the files */
580 581
  dbus_error_init (&tmp_error);
  while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
582
    {
583 584 585 586
      _dbus_assert (!dbus_error_is_set (&tmp_error));
      
      _dbus_string_set_length (&full_path, 0);
      
587
      if (!_dbus_string_ends_with_c_str (&filename, ".service"))
588 589
        {
          _dbus_verbose ("Skipping non-.service file %s\n",
590
                         _dbus_string_get_const_data (&filename));
591 592
          continue;
        }
593 594 595

      entry = _dbus_hash_table_lookup_string (s_dir->entries, _dbus_string_get_const_data (&filename));
      if (entry) /* Already has this service file in the cache */ 
596 597 598
        {
          if (!check_service_file (activation, entry, NULL, error))
            goto out;
599

600 601
          continue;
        }
602
      
603
      if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
604
          !_dbus_concat_dir_and_file (&full_path, &filename))
605
        {
606 607 608
          BUS_SET_OOM (error);
          goto out;
        }
609 610
          
      /* New file */
611 612
      desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
      if (desktop_file == NULL)
613 614
        {
          _dbus_verbose ("Could not load %s: %s\n",
615
                         _dbus_string_get_const_data (&full_path),
616
                         tmp_error.message);
617 618 619 620

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
621
              goto out;
622 623
            }
          
624 625 626
          dbus_error_free (&tmp_error);
          continue;
        }
627

628 629 630
      /* @todo We can return OOM or a DBUS_ERROR_FAILED error 
       *       Handle these both better
       */ 
631
      if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
632 633
        {
          bus_desktop_file_free (desktop_file);
634
          desktop_file = NULL;
635 636
          
          _dbus_verbose ("Could not add %s to activation entry list: %s\n",
637
                         _dbus_string_get_const_data (&full_path), tmp_error.message);
638 639 640 641

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
642
              goto out;
643 644 645
            }

          dbus_error_free (&tmp_error);
646 647
          continue;
        }
648 649 650 651 652 653 654
      else
        {
          bus_desktop_file_free (desktop_file);
          desktop_file = NULL;
          continue;
        }
    }
655

656 657 658
  if (dbus_error_is_set (&tmp_error))
    {
      dbus_move_error (&tmp_error, error);
659
      goto out;
660
    }
661
  
662
  retval = TRUE;
663

664 665 666 667 668
 out:
  if (!retval)
    _DBUS_ASSERT_ERROR_IS_SET (error);
  else
    _DBUS_ASSERT_ERROR_IS_CLEAR (error);
669 670 671 672 673 674
  
  if (iter != NULL)
    _dbus_directory_close (iter);
  _dbus_string_free (&filename);
  _dbus_string_free (&full_path);
  
675
  return retval;
676 677
}

678 679 680 681 682 683 684
static dbus_bool_t
populate_environment (BusActivation *activation)
{
  DBusString   key;
  DBusString   value;
  int          i;
  char       **environment;
685
  dbus_bool_t  retval = FALSE;
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740

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

741 742 743 744 745
dbus_bool_t
bus_activation_reload (BusActivation     *activation,
                       const DBusString  *address,
                       DBusList         **directories,
                       DBusError         *error)
746
{
747 748
  DBusList      *link;
  char          *dir;
749 750 751

  if (activation->server_address != NULL)
    dbus_free (activation->server_address);
752
  if (!_dbus_string_copy_data (address, &activation->server_address))
753 754 755 756
    {
      BUS_SET_OOM (error);
      goto failed;
    }
757 758 759

  if (activation->entries != NULL)
    _dbus_hash_table_unref (activation->entries);
760
  activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
761
                                             (DBusFreeFunction)bus_activation_entry_unref);
762
  if (activation->entries == NULL)
763 764 765 766
    {
      BUS_SET_OOM (error);
      goto failed;
    }
767

768 769
  if (activation->directories != NULL)
    _dbus_hash_table_unref (activation->directories);
770
  activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
771
                                                  (DBusFreeFunction)bus_service_directory_unref);
772 773

  if (activation->directories == NULL)
774 775 776 777
    {
      BUS_SET_OOM (error);
      goto failed;
    }
778

779 780
  link = _dbus_list_get_first_link (directories);
  while (link != NULL)
781
    {
782
      BusServiceDirectory *s_dir;
783

784 785
      dir = _dbus_strdup ((const char *) link->data);
      if (!dir)
786 787 788 789
        {
          BUS_SET_OOM (error);
          goto failed;
        }
790

791 792
      s_dir = dbus_new0 (BusServiceDirectory, 1);
      if (!s_dir)
793 794 795 796 797
        {
          dbus_free (dir);
          BUS_SET_OOM (error);
          goto failed;
        }
798 799 800

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

802
      s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
803
                                             (DBusFreeFunction)bus_activation_entry_unref);
804 805

      if (!s_dir->entries)
806 807 808 809 810
        {
          bus_service_directory_unref (s_dir);
          BUS_SET_OOM (error);
          goto failed;
        }
811 812

      if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
813 814 815 816 817
        {
          bus_service_directory_unref (s_dir);
          BUS_SET_OOM (error);
          goto failed;
        }
818

819
      /* only fail on OOM, it is ok if we can't read the directory */
820
      if (!update_directory (activation, s_dir, error))
821 822
        {
          if (dbus_error_has_name (error, DBUS_ERROR_NO_MEMORY))
823 824 825 826 827
            goto failed;
          else
            dbus_error_free (error);
        }

828
      link = _dbus_list_get_next_link (directories, link);
829
    }
830

831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
  return TRUE;
 failed:
  return FALSE;
}

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

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

873 874 875
  activation->environment = _dbus_hash_table_new (DBUS_HASH_STRING,
                                                  (DBusFreeFunction) dbus_free,
                                                  (DBusFreeFunction) dbus_free);
876

877 878 879 880 881 882 883 884 885 886 887 888
  if (activation->environment == NULL) 
    {
      BUS_SET_OOM (error);
      goto failed;
    }

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

889
  return activation;
890 891
  
 failed:
892 893 894 895
  bus_activation_unref (activation);  
  return NULL;
}

896
BusActivation *
897 898 899
bus_activation_ref (BusActivation *activation)
{
  _dbus_assert (activation->refcount > 0);
900
  
901
  activation->refcount += 1;
902 903

  return activation;
904 905 906 907 908 909 910 911 912
}

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

  activation->refcount -= 1;

913 914 915 916 917 918 919 920 921 922
  if (activation->refcount > 0)
    return;
  
  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);
  if (activation->directories)  
    _dbus_hash_table_unref (activation->directories);
923 924 925
  if (activation->environment)
    _dbus_hash_table_unref (activation->environment);

926
  dbus_free (activation);
927
}
928

929 930 931
static dbus_bool_t
add_bus_environment (BusActivation *activation,
                     DBusError     *error)
932
{
933
  const char *type;
934
  
935 936 937 938 939
  if (!bus_activation_set_environment_variable (activation,
                                                "DBUS_STARTER_ADDRESS",
                                                activation->server_address,
                                                error))
    return FALSE;
940
  
941 942 943
  type = bus_context_get_type (activation->context);
  if (type != NULL)
    {
944 945 946 947
      if (!bus_activation_set_environment_variable (activation,
                                                    "DBUS_STARTER_BUS_TYPE", type,
                                                    error))
        return FALSE;
948 949 950

      if (strcmp (type, "session") == 0)
        {
951 952 953 954 955
          if (!bus_activation_set_environment_variable (activation,
                                                        "DBUS_SESSION_BUS_ADDRESS",
                                                        activation->server_address,
                                                        error))
            return FALSE;
956 957 958
        }
      else if (strcmp (type, "system") == 0)
        {
959 960 961 962 963
          if (!bus_activation_set_environment_variable (activation,
                                                        "DBUS_SYSTEM_BUS_ADDRESS",
                                                        activation->server_address,
                                                        error))
            return FALSE;
964
        }
965
    }
966 967

  return TRUE;
968 969
}

970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038
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);
  
  _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);
  
  d->hash_entry = NULL;
}

static void
free_pending_restore_data (void *data)
{
  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);
  
  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;
  
  d->pending_activation = pending_activation;
  d->hash_entry = _dbus_hash_table_preallocate_entry (d->pending_activation->activation->pending_activations);
  
  bus_pending_activation_ref (d->pending_activation);
  
  if (d->hash_entry == NULL ||
      !bus_transaction_add_cancel_hook (transaction, restore_pending, d,
                                        free_pending_restore_data))
    {
      free_pending_restore_data (d);
      return FALSE;
    }

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

1039
dbus_bool_t
1040
bus_activation_service_created (BusActivation  *activation,
1041
                                const char     *service_name,
1042
                                BusTransaction *transaction,
1043
                                DBusError      *error)
1044 1045 1046 1047
{
  BusPendingActivation *pending_activation;
  DBusMessage *message;
  DBusList *link;
1048 1049

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
  
  /* Check if it's a pending activation */
  pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);

  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);
      
      if (dbus_connection_get_is_connected (entry->connection))
1064 1065 1066 1067
        {
          /* Only send activation replies to regular activation requests. */
          if (!entry->auto_activation)
            {
1068 1069
              dbus_uint32_t result;
              
1070 1071 1072 1073 1074 1075
              message = dbus_message_new_method_return (entry->activation_message);
              if (!message)
                {
                  BUS_SET_OOM (error);
                  goto error;
                }
1076

1077
              result = DBUS_START_REPLY_SUCCESS;
1078
              
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093