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 1.2
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"
31 #ifdef DBUS_BUILD_TESTS
34 * @defgroup DBusServerDebugPipe DBusServerDebugPipe
35 * @ingroup DBusInternals
36 * @brief In-process pipe debug server used in unit tests.
38 * Types and functions related to DBusServerDebugPipe.
39 * This is used for unit testing.
45 * Opaque object representing a debug server implementation.
47 typedef struct DBusServerDebugPipe DBusServerDebugPipe;
50 * Implementation details of DBusServerDebugPipe. All members
53 struct DBusServerDebugPipe
55 DBusServer base; /**< Parent class members. */
57 char *name; /**< Server name. */
59 dbus_bool_t disconnected; /**< TRUE if disconnect has been called */
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;
69 if (!server_pipe_hash)
71 _dbus_assert (server_pipe_hash_refcount == 0);
73 server_pipe_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL);
75 if (!server_pipe_hash)
79 server_pipe_hash_refcount = 1;
85 pipe_hash_unref (void)
87 _dbus_assert (server_pipe_hash != NULL);
88 _dbus_assert (server_pipe_hash_refcount > 0);
90 server_pipe_hash_refcount -= 1;
91 if (server_pipe_hash_refcount == 0)
93 _dbus_hash_table_unref (server_pipe_hash);
94 server_pipe_hash = NULL;
99 debug_finalize (DBusServer *server)
101 DBusServerDebugPipe *debug_server = (DBusServerDebugPipe*) server;
105 _dbus_server_finalize_base (server);
107 dbus_free (debug_server->name);
112 debug_handle_watch (DBusServer *server,
121 debug_disconnect (DBusServer *server)
123 ((DBusServerDebugPipe*)server)->disconnected = TRUE;
126 static DBusServerVTable debug_vtable = {
133 * Creates a new debug server using an in-process pipe
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.
140 _dbus_server_debug_pipe_new (const char *server_name,
143 DBusServerDebugPipe *debug_server;
146 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
148 if (!pipe_hash_ref ())
151 if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL)
153 dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, NULL);
158 debug_server = dbus_new0 (DBusServerDebugPipe, 1);
159 if (debug_server == NULL)
162 if (!_dbus_string_init (&address))
165 if (!_dbus_string_append (&address, "debug-pipe:name=") ||
166 !_dbus_string_append (&address, server_name))
169 debug_server->name = _dbus_strdup (server_name);
170 if (debug_server->name == NULL)
173 if (!_dbus_server_init_base (&debug_server->base,
174 &debug_vtable, &address))
177 if (!_dbus_hash_table_insert_string (server_pipe_hash,
182 _dbus_string_free (&address);
184 /* server keeps the pipe hash ref */
186 return (DBusServer *)debug_server;
189 _dbus_server_finalize_base (&debug_server->base);
191 dbus_free (debug_server->name);
193 _dbus_string_free (&address);
195 dbus_free (debug_server);
198 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
203 * Creates the client-side transport for
204 * a debug-pipe connection connected to the
205 * given debug-pipe server name.
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
212 _dbus_transport_debug_pipe_new (const char *server_name,
215 DBusTransport *client_transport;
216 DBusTransport *server_transport;
217 DBusConnection *connection;
218 int client_fd, server_fd;
222 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
224 server = _dbus_hash_table_lookup_string (server_pipe_hash,
226 if (server == NULL ||
227 ((DBusServerDebugPipe*)server)->disconnected)
229 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, NULL);
233 if (!_dbus_string_init (&address))
235 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
239 if (!_dbus_string_append (&address, "debug-pipe:name=") ||
240 !_dbus_string_append (&address, server_name))
242 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
243 _dbus_string_free (&address);
247 if (!_dbus_full_duplex_pipe (&client_fd, &server_fd, FALSE,
250 _dbus_verbose ("failed to create full duplex pipe\n");
251 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not create full-duplex pipe");
252 _dbus_string_free (&address);
256 _dbus_fd_set_close_on_exec (client_fd);
257 _dbus_fd_set_close_on_exec (server_fd);
259 client_transport = _dbus_transport_new_for_fd (client_fd,
261 if (client_transport == NULL)
263 _dbus_close (client_fd, NULL);
264 _dbus_close (server_fd, NULL);
265 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
266 _dbus_string_free (&address);
270 _dbus_string_free (&address);
274 server_transport = _dbus_transport_new_for_fd (server_fd,
276 if (server_transport == NULL)
278 _dbus_transport_unref (client_transport);
279 _dbus_close (server_fd, NULL);
280 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
286 if (!_dbus_transport_set_auth_mechanisms (server_transport,
287 (const char**) server->auth_mechanisms))
289 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
290 _dbus_transport_unref (server_transport);
291 _dbus_transport_unref (client_transport);
295 connection = _dbus_connection_new_for_transport (server_transport);
296 _dbus_transport_unref (server_transport);
297 server_transport = NULL;
299 if (connection == NULL)
301 _dbus_transport_unref (client_transport);
302 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
306 /* See if someone wants to handle this new connection,
307 * self-referencing for paranoia
309 if (server->new_connection_function)
311 dbus_server_ref (server);
312 (* server->new_connection_function) (server, connection,
313 server->new_connection_data);
314 dbus_server_unref (server);
317 /* If no one grabbed a reference, the connection will die,
318 * and the client transport will get an immediate disconnect
320 dbus_connection_unref (connection);
322 return client_transport;
328 #endif /* DBUS_BUILD_TESTS */