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