7f7dd438f3da9a8ff81949ff0f5a7a0beaf13ab0
[platform/upstream/dbus.git] / bus / activation.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* activation.c  Activation of services
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23 #include "activation.h"
24 #include "desktop-file.h"
25 #include "utils.h"
26 #include <dbus/dbus-internals.h>
27 #include <dbus/dbus-hash.h>
28 #include <sys/types.h>
29 #include <dirent.h>
30 #include <errno.h>
31
32 #define DBUS_SERVICE_SECTION "D-BUS Service"
33 #define DBUS_SERVICE_NAME "Name"
34 #define DBUS_SERVICE_EXEC "Exec"
35
36 static DBusHashTable *activation_entries = NULL;
37
38 typedef struct
39 {
40   char *name;
41   char *exec;
42 } BusActivationEntry;
43
44 static void
45 bus_activation_entry_free (BusActivationEntry *entry)
46 {
47   if (!entry)
48     return;
49   
50   dbus_free (entry->name);
51   dbus_free (entry->exec);
52 }
53
54 static dbus_bool_t
55 add_desktop_file_entry (BusDesktopFile *desktop_file)
56 {
57   char *name, *exec;
58   BusActivationEntry *entry;
59   
60   if (!bus_desktop_file_get_string (desktop_file,
61                                     DBUS_SERVICE_SECTION,
62                                     DBUS_SERVICE_NAME,
63                                     &name))
64     {
65       _dbus_verbose ("No \""DBUS_SERVICE_NAME"\" key in .service file\n");
66       return FALSE;
67     }
68
69   if (!bus_desktop_file_get_string (desktop_file,
70                                     DBUS_SERVICE_SECTION,
71                                     DBUS_SERVICE_EXEC,
72                                     &exec))
73     {
74       _dbus_verbose ("No \""DBUS_SERVICE_EXEC"\" key in .service file\n");
75       
76       dbus_free (name);
77       return FALSE;
78     }
79
80   if (_dbus_hash_table_lookup_string (activation_entries, name))
81     {
82       _dbus_verbose ("Service %s already exists in activation entry list\n", name);
83       dbus_free (name);
84       dbus_free (exec);
85
86       return FALSE;
87     }
88   
89   BUS_HANDLE_OOM (entry = dbus_malloc0 (sizeof (BusActivationEntry)));
90   entry->name = name;
91   entry->exec = exec;
92
93   BUS_HANDLE_OOM (_dbus_hash_table_insert_string (activation_entries, entry->name, entry));
94
95   _dbus_verbose ("Added \"%s\" to list of services\n", entry->name);
96   
97   return TRUE;
98 }
99
100 static void
101 load_directory (const char *directory)
102 {
103   DBusDirIter *iter;
104   DBusString dir, filename;
105   DBusResultCode result;
106
107   _dbus_string_init_const (&dir, directory);
108   
109   iter = _dbus_directory_open (&dir, &result);
110   if (iter == NULL)
111     {
112       _dbus_verbose ("Failed to open directory %s: &s\n", directory,
113                      result);
114     return;
115     }
116
117   BUS_HANDLE_OOM (_dbus_string_init (&filename, _DBUS_INT_MAX));
118   
119   /* Now read the files */
120   while (_dbus_directory_get_next_file (iter, &filename, &result))
121     {
122       DBusString full_path;
123       BusDesktopFile *desktop_file;
124       DBusError error;
125       
126       if (!_dbus_string_ends_with_c_str (&filename, ".service"))
127         {
128           const char *filename_c;
129           _dbus_string_get_const_data (&filename, &filename_c);
130           _dbus_verbose ("Skipping non-.service file %s\n",
131                          filename_c);
132           continue;
133         }
134       
135       BUS_HANDLE_OOM (_dbus_string_init (&full_path, _DBUS_INT_MAX));
136       BUS_HANDLE_OOM (_dbus_string_append (&full_path, directory));
137
138       BUS_HANDLE_OOM (_dbus_concat_dir_and_file (&full_path, &filename));
139
140       desktop_file = bus_desktop_file_load (&full_path, &error);
141
142       if (!desktop_file)
143         {
144           const char *full_path_c;
145
146           _dbus_string_get_const_data (&full_path, &full_path_c);
147           
148           _dbus_verbose ("Could not load %s: %s\n", full_path_c,
149                          error.message);
150           dbus_error_free (&error);
151           _dbus_string_free (&full_path);
152           continue;
153         }
154
155       if (!add_desktop_file_entry (desktop_file))
156         {
157           const char *full_path_c;
158
159           _dbus_string_get_const_data (&full_path, &full_path_c);
160           
161           _dbus_verbose ("Could not add %s to activation entry list.\n", full_path_c);
162         }
163
164       bus_desktop_file_free (desktop_file);
165       _dbus_string_free (&full_path);
166     }
167
168 #if 0
169   while ((directory_entry = readdir (directory_handle)))
170     {
171       DBusString path, filename;
172       BusDesktopFile *desktop_file;
173       DBusError error;
174       const char *filename_c;
175
176       
177       _dbus_string_init_const (&filename, directory_entry->d_name);
178
179
180       _dbus_string_get_const_data (&path, &filename_c);      
181
182       if (!desktop_file)
183         {
184           _dbus_verbose ("Could not load %s: %s\n", filename_c,
185                          error.message);
186           dbus_error_free (&error);
187           _dbus_string_free (&path);
188           continue;
189         }
190
191       if (!add_desktop_file_entry (desktop_file))
192         {
193           _dbus_verbose ("Could not add %s to activation entry list.\n", filename_c);
194         }
195     }
196 #endif
197 }
198
199
200 void
201 bus_activation_init (const char **directories)
202 {
203   int i;
204
205   BUS_HANDLE_OOM (activation_entries = _dbus_hash_table_new (DBUS_HASH_STRING, NULL,
206                                                              (DBusFreeFunction)bus_activation_entry_free));
207
208   i = 0;
209
210   /* Load service files */
211   while (directories[i] != NULL)
212     {
213       load_directory (directories[i]);
214       i++;
215     }
216 }