1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* activation.c Activation of services
4 * Copyright (C) 2003 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "activation.h"
24 #include "desktop-file.h"
26 #include <dbus/dbus-internals.h>
27 #include <dbus/dbus-hash.h>
28 #include <sys/types.h>
32 #define DBUS_SERVICE_SECTION "D-BUS Service"
33 #define DBUS_SERVICE_NAME "Name"
34 #define DBUS_SERVICE_EXEC "Exec"
36 static DBusHashTable *activation_entries = NULL;
44 static DBusHashTable *pending_activations = NULL;
48 } BusPendingActivation;
51 bus_activation_entry_free (BusActivationEntry *entry)
56 dbus_free (entry->name);
57 dbus_free (entry->exec);
61 add_desktop_file_entry (BusDesktopFile *desktop_file)
64 BusActivationEntry *entry;
66 if (!bus_desktop_file_get_string (desktop_file,
71 _dbus_verbose ("No \""DBUS_SERVICE_NAME"\" key in .service file\n");
75 if (!bus_desktop_file_get_string (desktop_file,
80 _dbus_verbose ("No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
86 if (_dbus_hash_table_lookup_string (activation_entries, name))
88 _dbus_verbose ("Service %s already exists in activation entry list\n", name);
95 BUS_HANDLE_OOM (entry = dbus_malloc0 (sizeof (BusActivationEntry)));
99 BUS_HANDLE_OOM (_dbus_hash_table_insert_string (activation_entries, entry->name, entry));
101 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
107 load_directory (const char *directory)
110 DBusString dir, filename;
111 DBusResultCode result;
113 _dbus_string_init_const (&dir, directory);
115 iter = _dbus_directory_open (&dir, &result);
118 _dbus_verbose ("Failed to open directory %s: &s\n", directory,
123 BUS_HANDLE_OOM (_dbus_string_init (&filename, _DBUS_INT_MAX));
125 /* Now read the files */
126 while (_dbus_directory_get_next_file (iter, &filename, &result))
128 DBusString full_path;
129 BusDesktopFile *desktop_file;
132 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
134 const char *filename_c;
135 _dbus_string_get_const_data (&filename, &filename_c);
136 _dbus_verbose ("Skipping non-.service file %s\n",
141 BUS_HANDLE_OOM (_dbus_string_init (&full_path, _DBUS_INT_MAX));
142 BUS_HANDLE_OOM (_dbus_string_append (&full_path, directory));
144 BUS_HANDLE_OOM (_dbus_concat_dir_and_file (&full_path, &filename));
146 desktop_file = bus_desktop_file_load (&full_path, &error);
150 const char *full_path_c;
152 _dbus_string_get_const_data (&full_path, &full_path_c);
154 _dbus_verbose ("Could not load %s: %s\n", full_path_c,
156 dbus_error_free (&error);
157 _dbus_string_free (&full_path);
161 if (!add_desktop_file_entry (desktop_file))
163 const char *full_path_c;
165 _dbus_string_get_const_data (&full_path, &full_path_c);
167 _dbus_verbose ("Could not add %s to activation entry list.\n", full_path_c);
170 bus_desktop_file_free (desktop_file);
171 _dbus_string_free (&full_path);
177 bus_activation_init (const char **directories)
181 BUS_HANDLE_OOM (activation_entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
182 (DBusFreeFunction)bus_activation_entry_free));
186 /* Load service files */
187 while (directories[i] != NULL)
189 load_directory (directories[i]);
195 bus_activation_activate_service (const char *service_name,
198 BusActivationEntry *entry;
201 entry = _dbus_hash_table_lookup_string (activation_entries, service_name);
205 dbus_set_error (error, DBUS_ERROR_ACTIVATE_SERVICE_NOT_FOUND,
206 "The service %s was not found in the activation entry list",
211 /* Now try to spawn the process */
212 argv[0] = entry->exec;
215 if (!_dbus_spawn_async (argv, error))