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