2003-04-06 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / dbus / dbus-server-debug-pipe.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server-debug-pipe.c In-proc debug server implementation 
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2003  Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 1.2
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include "dbus-internals.h"
26 #include "dbus-server-debug-pipe.h"
27 #include "dbus-transport-unix.h"
28 #include "dbus-connection-internal.h"
29 #include "dbus-hash.h"
30
31 #ifdef DBUS_BUILD_TESTS
32
33 /**
34  * @defgroup DBusServerDebugPipe DBusServerDebugPipe
35  * @ingroup  DBusInternals
36  * @brief In-process pipe debug server used in unit tests.
37  *
38  * Types and functions related to DBusServerDebugPipe.
39  * This is used for unit testing.
40  *
41  * @{
42  */
43
44 /**
45  * Opaque object representing a debug server implementation.
46  */
47 typedef struct DBusServerDebugPipe DBusServerDebugPipe;
48
49 /**
50  * Implementation details of DBusServerDebugPipe. All members
51  * are private.
52  */
53 struct DBusServerDebugPipe
54 {
55   DBusServer base;  /**< Parent class members. */
56
57   char *name; /**< Server name. */
58
59   dbus_bool_t disconnected; /**< TRUE if disconnect has been called */
60 };
61
62 /* FIXME not threadsafe (right now the test suite doesn't use threads anyhow ) */
63 static DBusHashTable *server_pipe_hash;
64 static int server_pipe_hash_refcount = 0;
65
66 static dbus_bool_t
67 pipe_hash_ref (void)
68 {
69   if (!server_pipe_hash)
70     {
71       _dbus_assert (server_pipe_hash_refcount == 0);
72       
73       server_pipe_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL);
74
75       if (!server_pipe_hash)
76         return FALSE;
77     }
78
79   server_pipe_hash_refcount = 1;
80
81   return TRUE;
82 }
83
84 static void
85 pipe_hash_unref (void)
86 {
87   _dbus_assert (server_pipe_hash != NULL);
88   _dbus_assert (server_pipe_hash_refcount > 0);
89
90   server_pipe_hash_refcount -= 1;
91   if (server_pipe_hash_refcount == 0)
92     {
93       _dbus_hash_table_unref (server_pipe_hash);
94       server_pipe_hash = NULL;
95     }
96 }
97
98 static void
99 debug_finalize (DBusServer *server)
100 {
101   DBusServerDebugPipe *debug_server = (DBusServerDebugPipe*) server;
102
103   pipe_hash_unref ();
104   
105   _dbus_server_finalize_base (server);
106
107   dbus_free (debug_server->name);
108   dbus_free (server);
109 }
110
111 static dbus_bool_t
112 debug_handle_watch (DBusServer  *server,
113                     DBusWatch   *watch,
114                     unsigned int flags)
115 {
116
117   return TRUE;
118 }
119
120 static void
121 debug_disconnect (DBusServer *server)
122 {
123   ((DBusServerDebugPipe*)server)->disconnected = TRUE;
124 }
125
126 static DBusServerVTable debug_vtable = {
127   debug_finalize,
128   debug_handle_watch,
129   debug_disconnect
130 };
131
132 /**
133  * Creates a new debug server using an in-process pipe
134  *
135  * @param server_name the name of the server.
136  * @param error address where an error can be returned.
137  * @returns a new server, or #NULL on failure.
138  */
139 DBusServer*
140 _dbus_server_debug_pipe_new (const char     *server_name,
141                              DBusError      *error)
142 {
143   DBusServerDebugPipe *debug_server;
144   DBusString address;
145   
146   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
147   
148   if (!pipe_hash_ref ())
149     return NULL;
150   
151   if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL)
152     {
153       dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, NULL);
154       pipe_hash_unref ();
155       return NULL;
156     }
157   
158   debug_server = dbus_new0 (DBusServerDebugPipe, 1);
159   if (debug_server == NULL)
160     goto nomem_0;
161
162   if (!_dbus_string_init (&address))
163     goto nomem_1;
164
165   if (!_dbus_string_append (&address, "debug-pipe:name=") ||
166       !_dbus_string_append (&address, server_name))
167     goto nomem_2;
168   
169   debug_server->name = _dbus_strdup (server_name);
170   if (debug_server->name == NULL)
171     goto nomem_2;
172   
173   if (!_dbus_server_init_base (&debug_server->base,
174                                &debug_vtable, &address))
175     goto nomem_3;
176
177   if (!_dbus_hash_table_insert_string (server_pipe_hash,
178                                        debug_server->name,
179                                        debug_server))
180     goto nomem_4;
181
182   _dbus_string_free (&address);
183
184   /* server keeps the pipe hash ref */
185   
186   return (DBusServer *)debug_server;
187
188  nomem_4:
189   _dbus_server_finalize_base (&debug_server->base);
190  nomem_3:
191   dbus_free (debug_server->name);
192  nomem_2:
193   _dbus_string_free (&address);
194  nomem_1:
195   dbus_free (debug_server);
196  nomem_0:
197   pipe_hash_unref ();
198   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
199   return NULL;
200 }
201
202 /**
203  * Creates the client-side transport for
204  * a debug-pipe connection connected to the
205  * given debug-pipe server name.
206  * 
207  * @param server_name name of server to connect to
208  * @param error address where an error can be returned.
209  * @returns #NULL on no memory or transport
210  */
211 DBusTransport*
212 _dbus_transport_debug_pipe_new (const char     *server_name,
213                                 DBusError      *error)
214 {
215   DBusTransport *client_transport;
216   DBusTransport *server_transport;
217   DBusConnection *connection;
218   int client_fd, server_fd;
219   DBusServer *server;
220   DBusString address;
221   
222   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
223
224   if (server_pipe_hash == NULL)
225     {
226       dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
227       return NULL;
228     }
229   
230   server = _dbus_hash_table_lookup_string (server_pipe_hash,
231                                            server_name);
232   if (server == NULL ||
233       ((DBusServerDebugPipe*)server)->disconnected)
234     {
235       dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
236       return NULL;
237     }
238
239   if (!_dbus_string_init (&address))
240     {
241       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
242       return NULL;
243     }
244
245   if (!_dbus_string_append (&address, "debug-pipe:name=") ||
246       !_dbus_string_append (&address, server_name))
247     {
248       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
249       _dbus_string_free (&address);
250       return NULL;
251     }
252   
253   if (!_dbus_full_duplex_pipe (&client_fd, &server_fd, FALSE,
254                                NULL))
255     {
256       _dbus_verbose ("failed to create full duplex pipe\n");
257       dbus_set_error (error, DBUS_ERROR_FAILED, "Could not create full-duplex pipe");
258       _dbus_string_free (&address);
259       return NULL;
260     }
261
262   _dbus_fd_set_close_on_exec (client_fd);
263   _dbus_fd_set_close_on_exec (server_fd);
264   
265   client_transport = _dbus_transport_new_for_fd (client_fd,
266                                                  FALSE, &address);
267   if (client_transport == NULL)
268     {
269       _dbus_close (client_fd, NULL);
270       _dbus_close (server_fd, NULL);
271       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
272       _dbus_string_free (&address);
273       return NULL;
274     }
275
276   _dbus_string_free (&address);
277   
278   client_fd = -1;
279   
280   server_transport = _dbus_transport_new_for_fd (server_fd,
281                                                  TRUE, NULL);
282   if (server_transport == NULL)
283     {
284       _dbus_transport_unref (client_transport);
285       _dbus_close (server_fd, NULL);
286       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
287       return NULL;
288     }
289
290   server_fd = -1;
291
292   if (!_dbus_transport_set_auth_mechanisms (server_transport,
293                                             (const char**) server->auth_mechanisms))
294     {
295       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
296       _dbus_transport_unref (server_transport);
297       _dbus_transport_unref (client_transport);
298       return FALSE;
299     }
300   
301   connection = _dbus_connection_new_for_transport (server_transport);
302   _dbus_transport_unref (server_transport);
303   server_transport = NULL;
304   
305   if (connection == NULL)
306     {
307       _dbus_transport_unref (client_transport);
308       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
309       return NULL;
310     }
311
312   /* See if someone wants to handle this new connection,
313    * self-referencing for paranoia
314    */
315   if (server->new_connection_function)
316     {
317       dbus_server_ref (server);
318       (* server->new_connection_function) (server, connection,
319                                            server->new_connection_data);
320       dbus_server_unref (server);
321     }
322   
323   /* If no one grabbed a reference, the connection will die,
324    * and the client transport will get an immediate disconnect
325    */
326   dbus_connection_unref (connection);
327
328   return client_transport;
329 }
330
331
332 /** @} */
333
334 #endif /* DBUS_BUILD_TESTS */
335