activation.c 48.1 KB
Newer Older
1 2 3 4
/* -*- mode: C; c-file-style: "gnu" -*- */
/* 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 22 23 24 25 26
 * 
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#include "activation.h"
#include "desktop-file.h"
27
#include "services.h"
28
#include "test.h"
29 30 31
#include "utils.h"
#include <dbus/dbus-internals.h>
#include <dbus/dbus-hash.h>
32
#include <dbus/dbus-list.h>
33
#include <dbus/dbus-spawn.h>
34
#include <dbus/dbus-timeout.h>
35 36 37 38 39 40 41
#include <dirent.h>
#include <errno.h>

#define DBUS_SERVICE_SECTION "D-BUS Service"
#define DBUS_SERVICE_NAME "Name"
#define DBUS_SERVICE_EXEC "Exec"

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

typedef struct
{
58 59 60 61 62 63 64 65
  int refcount;
  char *dir_c;
  DBusHashTable *entries;
} BusServiceDirectory;

typedef struct
{
  int refcount;
66 67
  char *name;
  char *exec;
68 69 70
  unsigned long mtime;
  BusServiceDirectory *s_dir;
  char *filename;
71 72
} BusActivationEntry;

73 74 75 76 77 78
typedef struct BusPendingActivationEntry BusPendingActivationEntry;

struct BusPendingActivationEntry
{
  DBusMessage *activation_message;
  DBusConnection *connection;
79 80

  dbus_bool_t auto_activation;
81 82 83 84
};

typedef struct
{
85
  int refcount;
86
  BusActivation *activation;
87
  char *service_name;
88
  char *exec;
89
  DBusList *entries;
90
  int n_entries;
91 92 93
  DBusBabysitter *babysitter;
  DBusTimeout *timeout;
  unsigned int timeout_added : 1;
94 95
} BusPendingActivation;

96
#if 0
97 98 99 100 101 102 103 104 105
static BusServiceDirectory *
bus_service_directory_ref (BusServiceDirectory *dir)
{
  _dbus_assert (dir->refcount);
  
  dir->refcount++;

  return dir;
}
106
#endif
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

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

127 128 129 130 131 132 133 134
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);
135
  
136 137 138 139
  dbus_free (entry);
}

static void
140 141 142 143 144 145
handle_timeout_callback (DBusTimeout   *timeout,
                         void          *data)
{
  BusPendingActivation *pending_activation = data;

  while (!dbus_timeout_handle (pending_activation->timeout))
146
    _dbus_wait_for_memory ();
147 148
}

149
static BusPendingActivation * 
150 151 152 153
bus_pending_activation_ref (BusPendingActivation *pending_activation)
{
  _dbus_assert (pending_activation->refcount > 0);
  pending_activation->refcount += 1;
154 155

  return pending_activation;
156 157 158 159
}

static void
bus_pending_activation_unref (BusPendingActivation *pending_activation)
160 161 162
{
  DBusList *link;
  
163
  if (pending_activation == NULL) /* hash table requires this */
164 165
    return;

166 167 168 169 170 171
  _dbus_assert (pending_activation->refcount > 0);
  pending_activation->refcount -= 1;

  if (pending_activation->refcount > 0)
    return;
  
172 173
  if (pending_activation->timeout_added)
    {
174 175 176
      _dbus_loop_remove_timeout (bus_context_get_loop (pending_activation->activation->context),
                                 pending_activation->timeout,
                                 handle_timeout_callback, pending_activation);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
      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);
195
  dbus_free (pending_activation->exec);
196

197
  link = _dbus_list_get_first_link (&pending_activation->entries);
198 199 200 201 202 203 204

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

      bus_pending_activation_entry_free (entry);

205
      link = _dbus_list_get_next_link (&pending_activation->entries, link);
206
    }
207
  _dbus_list_clear (&pending_activation->entries);
208 209 210 211 212

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

  _dbus_assert (pending_activation->activation->n_pending_activations >= 0);
213
  
214
  dbus_free (pending_activation);
215 216
}

217 218 219 220 221 222 223 224 225
static BusActivationEntry *
bus_activation_entry_ref (BusActivationEntry *entry)
{
  _dbus_assert (entry->refcount > 0);
  entry->refcount++;

  return entry;
}

226
static void
227
bus_activation_entry_unref (BusActivationEntry *entry)
228
{
229 230 231 232 233 234 235
  if (entry == NULL) /* hash table requires this */
    return;
  
  _dbus_assert (entry->refcount > 0);
  entry->refcount--;
  
  if (entry->refcount > 0) 
236 237 238 239
    return;
  
  dbus_free (entry->name);
  dbus_free (entry->exec);
240
  dbus_free (entry->filename);
241 242

  dbus_free (entry);
243 244 245
}

static dbus_bool_t
246 247 248 249 250
update_desktop_file_entry (BusActivation       *activation,
			   BusServiceDirectory *s_dir,
			   DBusString          *filename,
			   BusDesktopFile      *desktop_file,
			   DBusError           *error)
251 252 253
{
  char *name, *exec;
  BusActivationEntry *entry;
254 255
  DBusStat stat_buf;
  DBusString file_path;
256

257 258
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
259 260 261
  name = NULL;
  exec = NULL;
  entry = NULL;
262
  
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
  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,
		      "Can't stat the service file\n");
      goto failed;
    }
 
283 284 285 286 287
  if (!bus_desktop_file_get_string (desktop_file,
				    DBUS_SERVICE_SECTION,
				    DBUS_SERVICE_NAME,
				    &name))
    {
288 289 290
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "No \""DBUS_SERVICE_NAME"\" key in .service file\n");
      goto failed;
291 292 293 294 295 296 297
    }

  if (!bus_desktop_file_get_string (desktop_file,
				    DBUS_SERVICE_SECTION,
				    DBUS_SERVICE_EXEC,
				    &exec))
    {
298 299 300
      dbus_set_error (error, DBUS_ERROR_FAILED,
                      "No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
      goto failed;
301 302
    }

303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  entry = _dbus_hash_table_lookup_string (s_dir->entries, 
					  _dbus_string_get_const_data (filename));
  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))
	{
	  dbus_set_error (error, DBUS_ERROR_FAILED,
			  "Service %s already exists in activation entry list\n", name);
	  goto failed;
	}
      
      entry = dbus_new0 (BusActivationEntry, 1);
      if (entry == NULL)
	{
	  BUS_SET_OOM (error);
	  goto failed;
	}
     
      entry->name = name;
      entry->exec = exec;
      entry->refcount = 1;
    
      entry->s_dir = s_dir;
      entry->filename = _dbus_strdup (_dbus_string_get_const_data (filename));
      if (!entry->filename)
	{
	  BUS_SET_OOM (error);
	  goto failed;
	}

      if (!_dbus_hash_table_insert_string (activation->entries, entry->name, bus_activation_entry_ref (entry)))
	{
	  BUS_SET_OOM (error);
	  goto failed;
	}
     
      if (!_dbus_hash_table_insert_string (s_dir->entries, entry->filename, bus_activation_entry_ref (entry)))
	{
	  /* Revert the insertion in the entries table */
	  _dbus_hash_table_remove_string (activation->entries, entry->name);
	  BUS_SET_OOM (error);
	  goto failed;
	}

      _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
    }
  else /* Just update the entry */
353
    {
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      bus_activation_entry_ref (entry);
      _dbus_hash_table_remove_string (activation->entries, entry->name);

      if (_dbus_hash_table_lookup_string (activation->entries, name))
	{
	  _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;
	}
 
      dbus_free (entry->name);
      dbus_free (entry->exec);
      entry->name = name;
      entry->exec = exec;
      if (!_dbus_hash_table_insert_string (activation->entries,
					   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;
	}
379 380
    }
  
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  entry->mtime = stat_buf.mtime;
  
  _dbus_string_free (&file_path);
  bus_activation_entry_unref (entry);

  return TRUE;

failed:
  dbus_free (name);
  dbus_free (exec);
  _dbus_string_free (&file_path);

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

static dbus_bool_t
check_service_file (BusActivation       *activation,
		    BusActivationEntry  *entry,
		    BusActivationEntry **updated_entry,
		    DBusError           *error)
{
  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))
417 418
    {
      BUS_SET_OOM (error);
419
      return FALSE;
420
    }
421 422 423
 
  if (!_dbus_string_append (&file_path, entry->s_dir->dir_c) ||
      !_dbus_concat_dir_and_file (&file_path, &filename))
424 425
    {
      BUS_SET_OOM (error);
426 427
      retval = FALSE;
      goto out;
428
    }
429
  
430 431 432 433
  if (!_dbus_stat (&file_path, &stat_buf, NULL))
    {
      _dbus_verbose ("****** Can't stat file \"%s\", removing from cache\n",
		     _dbus_string_get_const_data (&file_path));
434

435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
      _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) 
	{
	  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;
	    }
	  
	  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;
	}
    }
486
  
487 488 489 490 491 492
out:
  _dbus_string_free (&file_path);

  if (updated_entry != NULL)
    *updated_entry = tmp_entry;
  return retval;
493 494
}

495

496 497 498 499
/* warning: this doesn't fully "undo" itself on failure, i.e. doesn't strip
 * hash entries it already added.
 */
static dbus_bool_t
500 501 502
update_directory (BusActivation       *activation,
		  BusServiceDirectory *s_dir,
		  DBusError           *error)
503 504 505
{
  DBusDirIter *iter;
  DBusString dir, filename;
506 507
  BusDesktopFile *desktop_file;
  DBusError tmp_error;
508
  dbus_bool_t retval;
509 510
  BusActivationEntry *entry;
  DBusString full_path;
511
  
512
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
513 514 515
  
  iter = NULL;
  desktop_file = NULL;
516
  
517 518
  _dbus_string_init_const (&dir, s_dir->dir_c);
  
519
  if (!_dbus_string_init (&filename))
520
    {
521 522
      BUS_SET_OOM (error);
      return FALSE;
523 524
    }

525
  if (!_dbus_string_init (&full_path))
526 527 528 529 530 531
    {
      BUS_SET_OOM (error);
      _dbus_string_free (&filename);
      return FALSE;
    }

532
  retval = FALSE;
533

534
  /* from this point it's safe to "goto out" */
535 536 537 538 539
  
  iter = _dbus_directory_open (&dir, error);
  if (iter == NULL)
    {
      _dbus_verbose ("Failed to open directory %s: %s\n",
540 541
		     s_dir->dir_c, 
		     error ? error->message : "unknown");
542
      goto out;
543
    }
544 545
  
  /* Now read the files */
546 547
  dbus_error_init (&tmp_error);
  while (_dbus_directory_get_next_file (iter, &filename, &tmp_error))
548
    {
549 550 551 552
      _dbus_assert (!dbus_error_is_set (&tmp_error));
      
      _dbus_string_set_length (&full_path, 0);
      
553 554
      if (!_dbus_string_ends_with_c_str (&filename, ".service"))
	{
555
	  _dbus_verbose ("Skipping non-.service file %s\n",
556
                         _dbus_string_get_const_data (&filename));
557 558 559 560 561 562 563 564 565 566
	  continue;
	}

      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 */ 
	{
	  if (!check_service_file (activation, entry, NULL, error))
	    goto out;

	  continue;
567 568
	}
      
569 570 571 572 573 574 575 576
      if (!_dbus_string_append (&full_path, s_dir->dir_c) ||
	  !_dbus_concat_dir_and_file (&full_path, &filename))
        {
	  BUS_SET_OOM (error);
	  goto out;
	}
          
      /* New file */
577 578
      desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
      if (desktop_file == NULL)
579
	{
580 581
	  _dbus_verbose ("Could not load %s: %s\n",
                         _dbus_string_get_const_data (&full_path),
582 583 584 585 586
			 tmp_error.message);

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
587
              goto out;
588 589 590
            }
          
	  dbus_error_free (&tmp_error);
591 592 593
	  continue;
	}

594
      if (!update_desktop_file_entry (activation, s_dir, &filename, desktop_file, &tmp_error))
595
	{
596
	  bus_desktop_file_free (desktop_file);
597
          desktop_file = NULL;
598
	  
599
	  _dbus_verbose ("Could not add %s to activation entry list: %s\n",
600
                         _dbus_string_get_const_data (&full_path), tmp_error.message);
601 602 603 604

          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
605
              goto out;
606 607 608 609
            }

          dbus_error_free (&tmp_error);
	  continue;
610
	}
611 612 613 614 615 616 617
      else
        {
          bus_desktop_file_free (desktop_file);
          desktop_file = NULL;
          continue;
        }
    }
618

619 620 621
  if (dbus_error_is_set (&tmp_error))
    {
      dbus_move_error (&tmp_error, error);
622
      goto out;
623
    }
624
  
625
  retval = TRUE;
626

627 628 629 630 631
 out:
  if (!retval)
    _DBUS_ASSERT_ERROR_IS_SET (error);
  else
    _DBUS_ASSERT_ERROR_IS_CLEAR (error);
632 633 634 635 636 637 638 639
  
  if (iter != NULL)
    _dbus_directory_close (iter);
  if (desktop_file)
    bus_desktop_file_free (desktop_file);
  _dbus_string_free (&filename);
  _dbus_string_free (&full_path);
  
640
  return retval;
641 642
}

643
BusActivation*
644 645
bus_activation_new (BusContext        *context,
		    const DBusString  *address,
646
                    DBusList         **directories,
647
                    DBusError         *error)
648
{
649
  BusActivation *activation;
650 651
  DBusList      *link;
  char          *dir;
652
  
653 654
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
655 656 657 658 659 660 661 662
  activation = dbus_new0 (BusActivation, 1);
  if (activation == NULL)
    {
      BUS_SET_OOM (error);
      return NULL;
    }
  
  activation->refcount = 1;
663
  activation->context = context;
664
  activation->n_pending_activations = 0;
665
  
666
  if (!_dbus_string_copy_data (address, &activation->server_address))
667 668 669 670
    {
      BUS_SET_OOM (error);
      goto failed;
    }
671
  
672
  activation->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
673
                                             (DBusFreeFunction)bus_activation_entry_unref);
674
  if (activation->entries == NULL)
675 676 677 678
    {      
      BUS_SET_OOM (error);
      goto failed;
    }
679

680
  activation->pending_activations = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
681
							  (DBusFreeFunction)bus_pending_activation_unref);
682 683 684 685 686 687

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

  activation->directories = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
						  (DBusFreeFunction)bus_service_directory_unref);
691
  
692 693 694 695 696 697
  if (activation->directories == NULL) 
    {
      BUS_SET_OOM (error);
      goto failed;
    }
 
698
  /* Load service files */
699 700
  link = _dbus_list_get_first_link (directories);
  while (link != NULL)
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 741
      BusServiceDirectory *s_dir;
      
      dir = _dbus_strdup ((const char *) link->data);
      if (!dir)
	{
	  BUS_SET_OOM (error);
	  goto failed;
	}
      
      s_dir = dbus_new0 (BusServiceDirectory, 1);
      if (!s_dir)
	{
	  dbus_free (dir);
	  BUS_SET_OOM (error);
	  goto failed;
	}

      s_dir->refcount = 1;
      s_dir->dir_c = dir;
      
      s_dir->entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
					     (DBusFreeFunction)bus_activation_entry_unref);

      if (!s_dir->entries)
	{
	  bus_service_directory_unref (s_dir);
	  BUS_SET_OOM (error);
	  goto failed;
	}

      if (!_dbus_hash_table_insert_string (activation->directories, s_dir->dir_c, s_dir))
	{
	  bus_service_directory_unref (s_dir);
	  BUS_SET_OOM (error);
	  goto failed;
	}

      if (!update_directory (activation, s_dir, error))
	goto failed;
      
742
      link = _dbus_list_get_next_link (directories, link);
743
    }
744

745
  return activation;
746 747
  
 failed:
748 749 750 751
  bus_activation_unref (activation);  
  return NULL;
}

752
BusActivation *
753 754 755
bus_activation_ref (BusActivation *activation)
{
  _dbus_assert (activation->refcount > 0);
756
  
757
  activation->refcount += 1;
758 759

  return activation;
760 761 762 763 764 765 766 767 768
}

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

  activation->refcount -= 1;

769 770 771 772 773 774 775 776 777 778 779 780
  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);
  
  dbus_free (activation);
781
}
782

783 784 785
static void
child_setup (void *data)
{
786
  BusActivation *activation = data;
787
  const char *type;
788
  
789 790 791
  /* If no memory, we simply have the child exit, so it won't try
   * to connect to the wrong thing.
   */
792
  if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", activation->server_address))
793
    _dbus_exit (1);
794
  
795 796 797 798 799
  type = bus_context_get_type (activation->context);
  if (type != NULL)
    {
      if (!_dbus_setenv ("DBUS_BUS_TYPE", type))
        _dbus_exit (1);
800 801 802 803 804 805 806 807 808 809 810 811 812

      if (strcmp (type, "session") == 0)
        {
          if (!_dbus_setenv ("DBUS_SESSION_BUS_ADDRESS",
                             activation->server_address))
            _dbus_exit (1);
        }
      else if (strcmp (type, "system") == 0)
        {
          if (!_dbus_setenv ("DBUS_SYSTEM_BUS_ADDRESS",
                             activation->server_address))
            _dbus_exit (1);
        }
813
    }
814 815
}

816 817 818 819 820 821 822 823 824 825 826 827 828 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 873 874 875 876 877 878 879 880 881 882 883 884
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;
}

885
dbus_bool_t
886 887 888 889
bus_activation_service_created (BusActivation  *activation,
				const char     *service_name,
                                BusTransaction *transaction,
				DBusError      *error)
890 891 892 893
{
  BusPendingActivation *pending_activation;
  DBusMessage *message;
  DBusList *link;
894 895

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
  
  /* 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))
	{
911 912
	  /* Only send activation replies to regular activation requests. */
	  if (!entry->auto_activation)
913
	    {
914 915
              dbus_uint32_t result;
              
916 917 918 919 920 921
	      message = dbus_message_new_method_return (entry->activation_message);
	      if (!message)
		{
		  BUS_SET_OOM (error);
		  goto error;
		}
922 923 924

              result = DBUS_ACTIVATION_REPLY_ACTIVATED;
              
925
	      if (!dbus_message_append_args (message,
926
					     DBUS_TYPE_UINT32, &result,
927 928 929 930 931 932 933 934 935 936 937 938 939 940
					     DBUS_TYPE_INVALID))
		{
		  dbus_message_unref (message);
		  BUS_SET_OOM (error);
		  goto error;
		}
	      
	      if (!bus_transaction_send_from_driver (transaction, entry->connection, message))
		{
		  dbus_message_unref (message);
		  BUS_SET_OOM (error);
		  goto error;
		}
	      
941 942
	      dbus_message_unref (message);
	    }
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 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
	}
      
      link = next;
    }

  return TRUE;

 error:
  return FALSE;
}

dbus_bool_t
bus_activation_send_pending_auto_activation_messages (BusActivation  *activation,
						      BusService     *service,
						      BusTransaction *transaction,
						      DBusError      *error)
{
  BusPendingActivation *pending_activation;
  DBusList *link;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
  /* Check if it's a pending activation */
  pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations,
						       bus_service_get_name (service));

  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 (entry->auto_activation && dbus_connection_get_is_connected (entry->connection))
	{
	  DBusConnection *addressed_recipient;
	  
	  addressed_recipient = bus_service_get_primary_owner (service);

	  /* Check the security policy, which has the side-effect of adding an
	   * expected pending reply.
	   */
          if (!bus_context_check_security_policy (activation->context, transaction,
						  entry->connection,
						  addressed_recipient,
						  addressed_recipient,
						  entry->activation_message, error))
	    goto error;

	  if (!bus_transaction_send (transaction, addressed_recipient, entry->activation_message))
995 996 997 998 999 1000 1001 1002
	    {
	      BUS_SET_OOM (error);
	      goto error;
	    }
	}

      link = next;
    }
1003 1004 1005 1006 1007 1008 1009

  if (!add_restore_pending_to_transaction (transaction, pending_activation))
    {
      _dbus_verbose ("Could not add cancel hook to transaction to revert removing pending activation\n");
      BUS_SET_OOM (error);
      goto error;
    }
1010
  
1011
  _dbus_hash_table_remove_string (activation->pending_activations, bus_service_get_name (service));
1012 1013 1014 1015 1016 1017 1018

  return TRUE;

 error:
  return FALSE;
}

1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
/**
 * FIXME @todo the error messages here would ideally be preallocated
 * 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;
  
  activation = pending_activation->activation;

  transaction = bus_transaction_new (activation->context);
  if (transaction == NULL)
    return FALSE;
  
  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))
	{
1047 1048 1049 1050
          if (!bus_transaction_send_error_reply (transaction,
                                                 entry->connection,
                                                 how,
                                                 entry->activation_message))
1051 1052
            goto error;
	}
1053
      
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
      link = next;
    }

  bus_transaction_execute_and_free (transaction);
  
  return TRUE;

 error:
  if (transaction)
    bus_transaction_cancel_and_free (transaction);
  return FALSE;
}

/**
 * Free the pending activation and send an error message to all the
 * connections that were waiting for it.
 */
static void
pending_activation_failed (BusPendingActivation *pending_activation,
                           const DBusError      *how)
{
  /* FIXME use preallocated OOM messages instead of bus_wait_for_memory() */
  while (!try_send_activation_failure (pending_activation, how))
1077
    _dbus_wait_for_memory ();
1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096

  /* Destroy this pending activation */
  _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
                                  pending_activation->service_name);
}

static dbus_bool_t
babysitter_watch_callback (DBusWatch     *watch,
                           unsigned int   condition,
                           void          *data)
{
  BusPendingActivation *pending_activation = data;
  dbus_bool_t retval;
  DBusBabysitter *babysitter;

  babysitter = pending_activation->babysitter;
  
  _dbus_babysitter_ref (babysitter);
  
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
  retval = dbus_watch_handle (watch, condition);

  /* FIXME this is broken in the same way that
   * connection watches used to be; there should be
   * a separate callback for status change, instead
   * of doing "if we handled a watch status might
   * have changed"
   *
   * Fixing this lets us move dbus_watch_handle
   * calls into dbus-mainloop.c
   */
  
1109 1110 1111
  if (_dbus_babysitter_get_child_exited (babysitter))
    {
      DBusError error;
1112 1113
      DBusHashIter iter;
      
1114 1115 1116
      dbus_error_init (&error);
      _dbus_babysitter_set_child_exit_error (babysitter, &error);

1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
      /* Destroy all pending activations with the same exec */
      _dbus_hash_iter_init (pending_activation->activation->pending_activations,
			    &iter);
      while (_dbus_hash_iter_next (&iter))
	{
	  BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
	 
	  if (p != pending_activation && strcmp (p->exec, pending_activation->exec) == 0)
	    pending_activation_failed (p, &error);
	}
      
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
      /* Destroys the pending activation */
      pending_activation_failed (pending_activation, &error);

      dbus_error_free (&error);
    }
  
  _dbus_babysitter_unref (babysitter);

  return retval;
}

static dbus_bool_t
add_babysitter_watch (DBusWatch      *watch,
                      void           *data)
{
  BusPendingActivation *pending_activation = data;

1145 1146 1147
  return _dbus_loop_add_watch (bus_context_get_loop (pending_activation->activation->context),
                               watch, babysitter_watch_callback, pending_activation,
                               NULL);
1148 1149 1150 1151 1152 1153 1154 1155
}

static void
remove_babysitter_watch (DBusWatch      *watch,
                         void           *data)
{
  BusPendingActivation *pending_activation = data;
  
1156 1157
  _dbus_loop_remove_watch (bus_context_get_loop (pending_activation->activation->context),
                           watch, babysitter_watch_callback, pending_activation);
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
}

static dbus_bool_t
pending_activation_timed_out (void *data)
{
  BusPendingActivation *pending_activation = data;
  DBusError error;
  
  /* Kill the spawned process, since it sucks
   * (not sure this is what we want to do, but
   * may as well try it for now)
   */
1170 1171
  if (pending_activation->babysitter) 
    _dbus_babysitter_kill_child (pending_activation->babysitter);
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

  dbus_error_init (&error);

  dbus_set_error (&error, DBUS_ERROR_TIMED_OUT,
                  "Activation of %s timed out",
                  pending_activation->service_name);

  pending_activation_failed (pending_activation, &error);

  dbus_error_free (&error);

  return TRUE;
}

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
static void
cancel_pending (void *data)
{
  BusPendingActivation *pending_activation = data;

  _dbus_verbose ("Canceling pending activation of %s\n",
                 pending_activation->service_name);

  if (pending_activation->babysitter)
    _dbus_babysitter_kill_child (pending_activation->babysitter);
  
  _dbus_hash_table_remove_string (pending_activation->activation->pending_activations,
                                  pending_activation->service_name);
}

static void
free_pending_cancel_data (void *data)
{
  BusPendingActivation *pending_activation = data;
  
  bus_pending_activation_unref (pending_activation);
}

static dbus_bool_t
add_cancel_pending_to_transaction (BusTransaction       *transaction,
                                   BusPendingActivation *pending_activation)
{  
  if (!bus_transaction_add_cancel_hook (transaction, cancel_pending,
                                        pending_activation,
                                        free_pending_cancel_data))
    return FALSE;

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

1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
static dbus_bool_t 
update_service_cache (BusActivation *activation, DBusError *error)
{
  DBusHashIter iter;
 
  _dbus_hash_iter_init (activation->directories, &iter);
  while (_dbus_hash_iter_next (&iter))
    {
      DBusError tmp_error;
      BusServiceDirectory *s_dir;

      s_dir = _dbus_hash_iter_get_value (&iter);

      dbus_error_init (&tmp_error);
      if (!update_directory (activation, s_dir, &tmp_error))
	{
	  if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
	    {
	      dbus_move_error (&tmp_error, error);
	      return FALSE;
	    }

	  dbus_error_free (&tmp_error);
	  continue;
	}
    }
  
  return TRUE;
}

static BusActivationEntry *
activation_find_entry (BusActivation *activation, 
		       const char    *service_name,
		       DBusError     *error)
{
  BusActivationEntry *entry;
  
  entry = _dbus_hash_table_lookup_string (activation->entries, service_name);
  if (!entry)
    { 
      if (!update_service_cache (activation, error)) 
	return NULL;

      entry = _dbus_hash_table_lookup_string (activation->entries,
					      service_name);
    }
  else 
    {
      BusActivationEntry *updated_entry;

      if (!check_service_file (activation, entry, &updated_entry, error)) 
	return NULL;

      entry = updated_entry;
    }

  if (!entry) 
    {
      dbus_set_error (error, DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND,
		      "The service %s was not found in the activation entry list",
		      service_name);
      return NULL;
    }

  return entry;
}

1292 1293 1294
dbus_bool_t
bus_activation_activate_service (BusActivation  *activation,
				 DBusConnection *connection,
1295
                                 BusTransaction *transaction,
1296
				 dbus_bool_t     auto_activation,
1297 1298 1299
				 DBusMessage    *activation_message,
                                 const char     *service_name,
				 DBusError      *error)
1300 1301
{
  BusActivationEntry *entry;
1302 1303 1304 1305
  BusPendingActivation *pending_activation;
  BusPendingActivationEntry *pending_activation_entry;
  DBusMessage *message;
  DBusString service_str;
1306
  char *argv[2];
1307
  dbus_bool_t retval;
1308 1309 1310 1311
  DBusHashIter iter;
  dbus_bool_t activated;
  
  activated = TRUE;
1312 1313

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1314 1315 1316 1317 1318 1319 1320 1321 1322

  if (activation->n_pending_activations >=
      bus_context_get_max_pending_activations (activation->context))
    {
      dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
		      "The maximum number of pending activations has been reached, activation of %s failed",
		      service_name);
      return FALSE;
    }
1323

1324 1325 1326
  entry = activation_find_entry (activation, service_name, error);
  if (!entry) 
    return FALSE;
1327

1328 1329 1330 1331
  /* Bypass the registry lookup if we're auto-activating, bus_dispatch would not
   * call us if the service is already active.
   */
  if (!auto_activation)
1332
    {
1333 1334 1335 1336
      /* Check if the service is active */
      _dbus_string_init_const (&service_str, service_name);
      if (bus_registry_lookup (bus_context_get_registry (activation->context), &service_str) != NULL)
        {
1337 1338
          dbus_uint32_t result;
          
1339
          _dbus_verbose ("Service \"%s\" is already active\n", service_name);
1340
      
1341
          message = dbus_message_new_method_return (activation_message);
1342

1343 1344 1345 1346 1347 1348
          if (!message)
            {
              _dbus_verbose ("No memory to create reply to activate message\n");
              BUS_SET_OOM (error);
              return FALSE;
            }
1349

1350 1351
          result = DBUS_ACTIVATION_REPLY_ALREADY_ACTIVE;
          
1352
          if (!dbus_message_append_args (message,
1353
                                         DBUS_TYPE_UINT32, &result,
1354 1355 1356 1357 1358 1359 1360
                                         DBUS_TYPE_INVALID))
            {
              _dbus_verbose ("No memory to set args of reply to activate message\n");
              BUS_SET_OOM (error);
              dbus_message_unref (message);
              return FALSE;
            }
1361

1362 1363 1364 1365 1366 1367 1368
          retval = bus_transaction_send_from_driver (transaction, connection, message);
          dbus_message_unref (message);
          if (!retval)
            {
              _dbus_verbose ("Failed to send reply\n");
              BUS_SET_OOM (error);
            }
1369

1370 1371
          return retval;
        }
1372 1373 1374 1375 1376
    }

  pending_activation_entry = dbus_new0 (BusPendingActivationEntry, 1);
  if (!pending_activation_entry)
    {
1377
      _dbus_verbose ("Failed to create pending activation entry\n");
1378 1379 1380 1381
      BUS_SET_OOM (error);
      return FALSE;
    }

1382 1383
  pending_activation_entry->auto_activation = auto_activation;

1384 1385 1386 1387 1388 1389 1390 1391 1392
  pending_activation_entry->activation_message = activation_message;
  dbus_message_ref (activation_message);
  pending_activation_entry->connection = connection;
  dbus_connection_ref (connection);
  
  /* Check if the service is being activated */
  pending_activation = _dbus_hash_table_lookup_string (activation->pending_activations, service_name);
  if (pending_activation)
    {
1393
      if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1394
	{
1395 1396
          _dbus_verbose ("Failed to append a new entry to pending activation\n");
          
1397 1398 1399 1400
	  BUS_SET_OOM (error);
	  bus_pending_activation_entry_free (pending_activation_entry);
	  return FALSE;
	}
1401 1402 1403

      pending_activation->n_entries += 1;
      pending_activation->activation->n_pending_activations += 1;
1404 1405 1406 1407 1408 1409
    }
  else
    {
      pending_activation = dbus_new0 (BusPendingActivation, 1);
      if (!pending_activation)
	{
1410 1411
          _dbus_verbose ("Failed to create pending activation\n");
          
1412 1413 1414 1415
	  BUS_SET_OOM (error);
	  bus_pending_activation_entry_free (pending_activation_entry);	  
	  return FALSE;
	}
1416 1417

      pending_activation->activation = activation;
1418
      pending_activation->refcount = 1;
1419
      
1420 1421 1422
      pending_activation->service_name = _dbus_strdup (service_name);
      if (!pending_activation->service_name)
	{
1423 1424
          _dbus_verbose ("Failed to copy service name for pending activation\n");
          
1425
	  BUS_SET_OOM (error);
1426
	  bus_pending_activation_unref (pending_activation);
1427 1428 1429 1430
	  bus_pending_activation_entry_free (pending_activation_entry);	  
	  return FALSE;
	}

1431 1432 1433 1434 1435 1436 1437 1438 1439 1440
      pending_activation->exec = _dbus_strdup (entry->exec);
      if (!pending_activation->exec)
	{
	  _dbus_verbose ("Failed to copy service exec for pending activation\n");
	  BUS_SET_OOM (error);
	  bus_pending_activation_unref (pending_activation);
	  bus_pending_activation_entry_free (pending_activation_entry);
	  return FALSE;
	}

1441 1442 1443 1444 1445 1446 1447
      pending_activation->timeout =
        _dbus_timeout_new (bus_context_get_activation_timeout (activation->context),
                           pending_activation_timed_out,
                           pending_activation,
                           NULL);
      if (!pending_activation->timeout)
	{
1448 1449
          _dbus_verbose ("Failed to create timeout for pending activation\n");
          
1450
	  BUS_SET_OOM (error);
1451
	  bus_pending_activation_unref (pending_activation);
1452 1453 1454 1455
	  bus_pending_activation_entry_free (pending_activation_entry);	  
	  return FALSE;
	}

1456 1457 1458 1459 1460
      if (!_dbus_loop_add_timeout (bus_context_get_loop (activation->context),
                                   pending_activation->timeout,
                                   handle_timeout_callback,
                                   pending_activation,
                                   NULL))
1461
	{
1462 1463
          _dbus_verbose ("Failed to add timeout for pending activation\n");
          
1464
	  BUS_SET_OOM (error);
1465
	  bus_pending_activation_unref (pending_activation);
1466 1467 1468 1469 1470 1471
	  bus_pending_activation_entry_free (pending_activation_entry);	  
	  return FALSE;
	}

      pending_activation->timeout_added = TRUE;
      
1472
      if (!_dbus_list_append (&pending_activation->entries, pending_activation_entry))
1473
	{
1474 1475
          _dbus_verbose ("Failed to add entry to just-created pending activation\n");
          
1476
	  BUS_SET_OOM (error);
1477
	  bus_pending_activation_unref (pending_activation);
1478 1479 1480
	  bus_pending_activation_entry_free (pending_activation_entry);	  
	  return FALSE;
	}
1481 1482 1483

      pending_activation->n_entries += 1;
      pending_activation->activation->n_pending_activations += 1;
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497
    
      activated = FALSE;
      _dbus_hash_iter_init (activation->pending_activations, &iter);
      while (_dbus_hash_iter_next (&iter))
	{
	  BusPendingActivation *p = _dbus_hash_iter_get_value (&iter);
	  
	  if (strcmp (p->exec, entry->exec) == 0) 
	    {
	      activated = TRUE;
	      break;
	    }
	}
     
1498
      if (!_dbus_hash_table_insert_string (activation->pending_activations,
1499 1500
					   pending_activation->service_name,
                                           pending_activation))
1501
	{
1502 1503
          _dbus_verbose ("Failed to put pending activation in hash table\n");
          
1504
	  BUS_SET_OOM (error);
1505
	  bus_pending_activation_unref (pending_activation);
1506 1507 1508
	  return FALSE;
	}
    }
1509
  
1510 1511 1512 1513 1514 1515 1516 1517
  if (!add_cancel_pending_to_transaction (transaction, pending_activation))
    {
      _dbus_verbose ("Failed to add pending activation cancel hook to transaction\n");
      BUS_SET_OOM (error);
      _dbus_hash_table_remove_string (activation->pending_activations,
				      pending_activation->service_name);
      return FALSE;
    }
1518
  
1519 1520 1521
  /* FIXME we need to support a full command line, not just a single
   * argv[0]
   */
1522

1523 1524
  if (activated)
    return TRUE;
1525

1526 1527 1528 1529
  /* Now try to spawn the process */
  argv[0] = entry->exec;
  argv[1] = NULL;

1530
  if (!_dbus_spawn_async_with_babysitter (&pending_activation->babysitter, argv,
1531 1532
					  child_setup, activation, 
					  error))
1533
    {
1534 1535
      _dbus_verbose ("Failed to spawn child\n");
      _DBUS_ASSERT_ERROR_IS_SET (error);
1536 1537
      return FALSE;
    }
1538 1539 1540 1541 1542

  _dbus_assert (pending_activation->babysitter != NULL);
  
  if (!_dbus_babysitter_set_watch_functions (pending_activation->babysitter,
                                             add_babysitter_watch,
1543
					     remove_babysitter_watch,
1544 1545 1546 1547 1548
                                             NULL,
                                             pending_activation,
                                             NULL))
    {
      BUS_SET_OOM (error);
1549
      _dbus_verbose ("Failed to set babysitter watch functions\n");
1550 1551
      return FALSE;
    }
1552
  
1553 1554
  return TRUE;
}
1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593