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;
45 bus_activation_entry_free (BusActivationEntry *entry)
50 dbus_free (entry->name);
51 dbus_free (entry->exec);
55 add_desktop_file_entry (BusDesktopFile *desktop_file)
58 BusActivationEntry *entry;
60 if (!bus_desktop_file_get_string (desktop_file,
65 _dbus_verbose ("No \""DBUS_SERVICE_NAME"\" key in .service file\n");
69 if (!bus_desktop_file_get_string (desktop_file,
74 _dbus_verbose ("No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
80 if (_dbus_hash_table_lookup_string (activation_entries, name))
82 _dbus_verbose ("Service %s already exists in activation entry list\n", name);
89 BUS_HANDLE_OOM (entry = dbus_malloc0 (sizeof (BusActivationEntry)));
93 BUS_HANDLE_OOM (_dbus_hash_table_insert_string (activation_entries, entry->name, entry));
95 _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
101 load_directory (const char *directory)
104 DBusString dir, filename;
105 DBusResultCode result;
107 _dbus_string_init_const (&dir, directory);
109 iter = _dbus_directory_open (&dir, &result);
112 _dbus_verbose ("Failed to open directory %s: &s\n", directory,
117 BUS_HANDLE_OOM (_dbus_string_init (&filename, _DBUS_INT_MAX));
119 /* Now read the files */
120 while (_dbus_directory_get_next_file (iter, &filename, &result))
122 DBusString full_path;
123 BusDesktopFile *desktop_file;
126 if (!_dbus_string_ends_with_c_str (&filename, ".service"))
128 const char *filename_c;
129 _dbus_string_get_const_data (&filename, &filename_c);
130 _dbus_verbose ("Skipping non-.service file %s\n",
135 BUS_HANDLE_OOM (_dbus_string_init (&full_path, _DBUS_INT_MAX));
136 BUS_HANDLE_OOM (_dbus_string_append (&full_path, directory));
138 BUS_HANDLE_OOM (_dbus_concat_dir_and_file (&full_path, &filename));
140 desktop_file = bus_desktop_file_load (&full_path, &error);
144 const char *full_path_c;
146 _dbus_string_get_const_data (&full_path, &full_path_c);
148 _dbus_verbose ("Could not load %s: %s\n", full_path_c,
150 dbus_error_free (&error);
151 _dbus_string_free (&full_path);
155 if (!add_desktop_file_entry (desktop_file))
157 const char *full_path_c;
159 _dbus_string_get_const_data (&full_path, &full_path_c);
161 _dbus_verbose ("Could not add %s to activation entry list.\n", full_path_c);
164 bus_desktop_file_free (desktop_file);
165 _dbus_string_free (&full_path);
169 while ((directory_entry = readdir (directory_handle)))
171 DBusString path, filename;
172 BusDesktopFile *desktop_file;
174 const char *filename_c;
177 _dbus_string_init_const (&filename, directory_entry->d_name);
180 _dbus_string_get_const_data (&path, &filename_c);
184 _dbus_verbose ("Could not load %s: %s\n", filename_c,
186 dbus_error_free (&error);
187 _dbus_string_free (&path);
191 if (!add_desktop_file_entry (desktop_file))
193 _dbus_verbose ("Could not add %s to activation entry list.\n", filename_c);
201 bus_activation_init (const char **directories)
205 BUS_HANDLE_OOM (activation_entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
206 (DBusFreeFunction)bus_activation_entry_free));
210 /* Load service files */
211 while (directories[i] != NULL)
213 load_directory (directories[i]);