1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server-debug-pipe.c In-proc debug server implementation
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.0
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.
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.
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
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"
32 #ifdef DBUS_BUILD_TESTS
35 * @defgroup DBusServerDebugPipe DBusServerDebugPipe
36 * @ingroup DBusInternals
37 * @brief In-process pipe debug server used in unit tests.
39 * Types and functions related to DBusServerDebugPipe.
40 * This is used for unit testing.
46 * Opaque object representing a debug server implementation.
48 typedef struct DBusServerDebugPipe DBusServerDebugPipe;
51 * Implementation details of DBusServerDebugPipe. All members
54 struct DBusServerDebugPipe
56 DBusServer base; /**< Parent class members. */
58 char *name; /**< Server name. */
60 dbus_bool_t disconnected; /**< TRUE if disconnect has been called */
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;
70 if (!server_pipe_hash)
72 _dbus_assert (server_pipe_hash_refcount == 0);
74 server_pipe_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL);
76 if (!server_pipe_hash)
80 server_pipe_hash_refcount = 1;
86 pipe_hash_unref (void)
88 _dbus_assert (server_pipe_hash != NULL);
89 _dbus_assert (server_pipe_hash_refcount > 0);
91 server_pipe_hash_refcount -= 1;
92 if (server_pipe_hash_refcount == 0)
94 _dbus_hash_table_unref (server_pipe_hash);
95 server_pipe_hash = NULL;
100 debug_finalize (DBusServer *server)
102 DBusServerDebugPipe *debug_server = (DBusServerDebugPipe*) server;
106 _dbus_server_finalize_base (server);
108 dbus_free (debug_server->name);
113 debug_disconnect (DBusServer *server)
115 ((DBusServerDebugPipe*)server)->disconnected = TRUE;
118 static DBusServerVTable debug_vtable = {
124 * Creates a new debug server using an in-process pipe
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.
131 _dbus_server_debug_pipe_new (const char *server_name,
134 DBusServerDebugPipe *debug_server;
137 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
139 if (!pipe_hash_ref ())
142 if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL)
144 dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, NULL);
149 debug_server = dbus_new0 (DBusServerDebugPipe, 1);
150 if (debug_server == NULL)
153 if (!_dbus_string_init (&address))
156 if (!_dbus_string_append (&address, "debug-pipe:name=") ||
157 !_dbus_string_append (&address, server_name))
160 debug_server->name = _dbus_strdup (server_name);
161 if (debug_server->name == NULL)
164 if (!_dbus_server_init_base (&debug_server->base,
165 &debug_vtable, &address))
168 if (!_dbus_hash_table_insert_string (server_pipe_hash,
173 _dbus_string_free (&address);
175 /* server keeps the pipe hash ref */
177 return (DBusServer *)debug_server;
180 _dbus_server_finalize_base (&debug_server->base);
182 dbus_free (debug_server->name);
184 _dbus_string_free (&address);
186 dbus_free (debug_server);
189 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
194 * Creates the client-side transport for
195 * a debug-pipe connection connected to the
196 * given debug-pipe server name.
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
203 _dbus_transport_debug_pipe_new (const char *server_name,
206 DBusTransport *client_transport;
207 DBusTransport *server_transport;
208 DBusConnection *connection;
209 int client_fd, server_fd;
213 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
215 if (server_pipe_hash == NULL)
217 dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
221 server = _dbus_hash_table_lookup_string (server_pipe_hash,
223 if (server == NULL ||
224 ((DBusServerDebugPipe*)server)->disconnected)
226 dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
230 if (!_dbus_string_init (&address))
232 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
236 if (!_dbus_string_append (&address, "debug-pipe:name=") ||
237 !_dbus_string_append (&address, server_name))
239 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
240 _dbus_string_free (&address);
244 if (!_dbus_full_duplex_pipe (&client_fd, &server_fd, FALSE,
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);
253 _dbus_fd_set_close_on_exec (client_fd);
254 _dbus_fd_set_close_on_exec (server_fd);
256 client_transport = _dbus_transport_new_for_fd (client_fd,
258 if (client_transport == NULL)
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);
267 _dbus_string_free (&address);
271 server_transport = _dbus_transport_new_for_fd (server_fd,
273 if (server_transport == NULL)
275 _dbus_transport_unref (client_transport);
276 _dbus_close (server_fd, NULL);
277 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
283 if (!_dbus_transport_set_auth_mechanisms (server_transport,
284 (const char**) server->auth_mechanisms))
286 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
287 _dbus_transport_unref (server_transport);
288 _dbus_transport_unref (client_transport);
292 connection = _dbus_connection_new_for_transport (server_transport);
293 _dbus_transport_unref (server_transport);
294 server_transport = NULL;
296 if (connection == NULL)
298 _dbus_transport_unref (client_transport);
299 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
303 /* See if someone wants to handle this new connection,
304 * self-referencing for paranoia
306 if (server->new_connection_function)
308 dbus_server_ref (server);
309 (* server->new_connection_function) (server, connection,
310 server->new_connection_data);
311 dbus_server_unref (server);
314 /* If no one grabbed a reference, the connection will die,
315 * and the client transport will get an immediate disconnect
317 dbus_connection_unref (connection);
319 return client_transport;
325 #endif /* DBUS_BUILD_TESTS */