bus: Assign a serial number for messages from the driver
[platform/upstream/dbus.git] / dbus / dbus-server-debug-pipe.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22  *
23  */
24
25 #include <config.h>
26 #include "dbus-internals.h"
27 #include "dbus-server-debug-pipe.h"
28 #include "dbus-transport-socket.h"
29 #include "dbus-connection-internal.h"
30 #include "dbus-hash.h"
31 #include "dbus-string.h"
32 #include "dbus-protocol.h"
33
34 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
35
36 /**
37  * @defgroup DBusServerDebugPipe DBusServerDebugPipe
38  * @ingroup  DBusInternals
39  * @brief In-process pipe debug server used in unit tests.
40  *
41  * Types and functions related to DBusServerDebugPipe.
42  * This is used for unit testing.
43  *
44  * @{
45  */
46
47 /**
48  * Opaque object representing a debug server implementation.
49  */
50 typedef struct DBusServerDebugPipe DBusServerDebugPipe;
51
52 /**
53  * Implementation details of DBusServerDebugPipe. All members
54  * are private.
55  */
56 struct DBusServerDebugPipe
57 {
58   DBusServer base;  /**< Parent class members. */
59
60   char *name; /**< Server name. */
61
62   dbus_bool_t disconnected; /**< TRUE if disconnect has been called */
63 };
64
65 /* FIXME not threadsafe (right now the test suite doesn't use threads anyhow ) */
66 static DBusHashTable *server_pipe_hash;
67 static int server_pipe_hash_refcount = 0;
68
69 static dbus_bool_t
70 pipe_hash_ref (void)
71 {
72   if (!server_pipe_hash)
73     {
74       _dbus_assert (server_pipe_hash_refcount == 0);
75       
76       server_pipe_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL);
77
78       if (!server_pipe_hash)
79         return FALSE;
80     }
81
82   server_pipe_hash_refcount = 1;
83
84   return TRUE;
85 }
86
87 static void
88 pipe_hash_unref (void)
89 {
90   _dbus_assert (server_pipe_hash != NULL);
91   _dbus_assert (server_pipe_hash_refcount > 0);
92
93   server_pipe_hash_refcount -= 1;
94   if (server_pipe_hash_refcount == 0)
95     {
96       _dbus_hash_table_unref (server_pipe_hash);
97       server_pipe_hash = NULL;
98     }
99 }
100
101 static void
102 debug_finalize (DBusServer *server)
103 {
104   DBusServerDebugPipe *debug_server = (DBusServerDebugPipe*) server;
105
106   pipe_hash_unref ();
107   
108   _dbus_server_finalize_base (server);
109
110   dbus_free (debug_server->name);
111   dbus_free (server);
112 }
113
114 static void
115 debug_disconnect (DBusServer *server)
116 {
117   ((DBusServerDebugPipe*)server)->disconnected = TRUE;
118 }
119
120 static DBusServerVTable debug_vtable = {
121   debug_finalize,
122   debug_disconnect
123 };
124
125 /**
126  * Creates a new debug server using an in-process pipe
127  *
128  * @param server_name the name of the server.
129  * @param error address where an error can be returned.
130  * @returns a new server, or #NULL on failure.
131  */
132 DBusServer*
133 _dbus_server_debug_pipe_new (const char     *server_name,
134                              DBusError      *error)
135 {
136   DBusServerDebugPipe *debug_server;
137   DBusString address;
138   DBusString name_str;
139   
140   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
141   
142   if (!pipe_hash_ref ())
143     return NULL;
144   
145   if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL)
146     {
147       dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, NULL);
148       pipe_hash_unref ();
149       return NULL;
150     }
151   
152   debug_server = dbus_new0 (DBusServerDebugPipe, 1);
153   if (debug_server == NULL)
154     goto nomem_0;
155
156   if (!_dbus_string_init (&address))
157     goto nomem_1;
158
159   _dbus_string_init_const (&name_str, server_name);
160   if (!_dbus_string_append (&address, "debug-pipe:name=") ||
161       !_dbus_address_append_escaped (&address, &name_str))
162     goto nomem_2;
163   
164   debug_server->name = _dbus_strdup (server_name);
165   if (debug_server->name == NULL)
166     goto nomem_2;
167   
168   if (!_dbus_server_init_base (&debug_server->base,
169                                &debug_vtable, &address,
170                                error))
171     goto fail_3;
172
173   if (!_dbus_hash_table_insert_string (server_pipe_hash,
174                                        debug_server->name,
175                                        debug_server))
176     goto nomem_4;
177
178   _dbus_string_free (&address);
179
180   /* server keeps the pipe hash ref */
181
182   _dbus_server_trace_ref (&debug_server->base, 0, 1, "debug_pipe_new");
183   return (DBusServer *)debug_server;
184
185  nomem_4:
186   _dbus_server_finalize_base (&debug_server->base);
187  fail_3:
188   dbus_free (debug_server->name);
189  nomem_2:
190   _dbus_string_free (&address);
191  nomem_1:
192   dbus_free (debug_server);
193  nomem_0:
194   pipe_hash_unref ();
195   if (error != NULL && !dbus_error_is_set (error))
196     _DBUS_SET_OOM (error);
197   return NULL;
198 }
199
200 /**
201  * Creates the client-side transport for
202  * a debug-pipe connection connected to the
203  * given debug-pipe server name.
204  * 
205  * @param server_name name of server to connect to
206  * @param error address where an error can be returned.
207  * @returns #NULL on no memory or transport
208  */
209 DBusTransport*
210 _dbus_transport_debug_pipe_new (const char     *server_name,
211                                 DBusError      *error)
212 {
213   DBusTransport *client_transport;
214   DBusTransport *server_transport;
215   DBusConnection *connection;
216   DBusSocket client_fd, server_fd;
217   DBusServer *server;
218   DBusString address;
219   
220   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
221
222   if (server_pipe_hash == NULL)
223     {
224       dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
225       return NULL;
226     }
227   
228   server = _dbus_hash_table_lookup_string (server_pipe_hash,
229                                            server_name);
230   if (server == NULL ||
231       ((DBusServerDebugPipe*)server)->disconnected)
232     {
233       dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
234       return NULL;
235     }
236
237   if (!_dbus_string_init (&address))
238     {
239       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
240       return NULL;
241     }
242
243   if (!_dbus_string_append (&address, "debug-pipe:name=") ||
244       !_dbus_string_append (&address, server_name))
245     {
246       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
247       _dbus_string_free (&address);
248       return NULL;
249     }
250   
251   if (!_dbus_socketpair (&client_fd, &server_fd, FALSE, NULL))
252     {
253       _dbus_verbose ("failed to create full duplex pipe\n");
254       dbus_set_error (error, DBUS_ERROR_FAILED, "Could not create full-duplex pipe");
255       _dbus_string_free (&address);
256       return NULL;
257     }
258
259   client_transport = _dbus_transport_new_for_socket (client_fd,
260                                                      NULL, &address);
261   if (client_transport == NULL)
262     {
263       _dbus_close_socket (client_fd, NULL);
264       _dbus_close_socket (server_fd, NULL);
265       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
266       _dbus_string_free (&address);
267       return NULL;
268     }
269
270   _dbus_string_free (&address);
271   
272   _dbus_socket_invalidate (&client_fd);
273
274   server_transport = _dbus_transport_new_for_socket (server_fd,
275                                                      &server->guid_hex, NULL);
276   if (server_transport == NULL)
277     {
278       _dbus_transport_unref (client_transport);
279       _dbus_close_socket (server_fd, NULL);
280       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
281       return NULL;
282     }
283
284   _dbus_socket_invalidate (&server_fd);
285
286   if (!_dbus_transport_set_auth_mechanisms (server_transport,
287                                             (const char**) server->auth_mechanisms))
288     {
289       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
290       _dbus_transport_unref (server_transport);
291       _dbus_transport_unref (client_transport);
292       return NULL;
293     }
294   
295   connection = _dbus_connection_new_for_transport (server_transport);
296   _dbus_transport_unref (server_transport);
297   server_transport = NULL;
298   
299   if (connection == NULL)
300     {
301       _dbus_transport_unref (client_transport);
302       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
303       return NULL;
304     }
305
306   /* See if someone wants to handle this new connection,
307    * self-referencing for paranoia
308    */
309   if (server->new_connection_function)
310     {
311       dbus_server_ref (server);
312       (* server->new_connection_function) (server, connection,
313                                            server->new_connection_data);
314       dbus_server_unref (server);
315     }
316   
317   /* If no one grabbed a reference, the connection will die,
318    * and the client transport will get an immediate disconnect
319    */
320   _dbus_connection_close_if_only_one_ref (connection);
321   dbus_connection_unref (connection);
322
323   return client_transport;
324 }
325
326 /**
327  * Tries to interpret the address entry as a debug pipe entry.
328  * 
329  * Sets error if the result is not OK.
330  * 
331  * @param entry an address entry
332  * @param server_p location to store a new DBusServer, or #NULL on failure.
333  * @param error location to store rationale for failure on bad address
334  * @returns the outcome
335  * 
336  */
337 DBusServerListenResult
338 _dbus_server_listen_debug_pipe (DBusAddressEntry *entry,
339                                 DBusServer      **server_p,
340                                 DBusError        *error)
341 {
342   const char *method;
343
344   *server_p = NULL;
345   
346   method = dbus_address_entry_get_method (entry);
347   
348   if (strcmp (method, "debug-pipe") == 0)
349     {
350       const char *name = dbus_address_entry_get_value (entry, "name");
351       
352       if (name == NULL)
353         {
354           _dbus_set_bad_address(error, "debug-pipe", "name",
355                                 NULL);
356           return DBUS_SERVER_LISTEN_BAD_ADDRESS;
357         }
358
359       *server_p = _dbus_server_debug_pipe_new (name, error);
360       
361       if (*server_p)
362         {
363           _DBUS_ASSERT_ERROR_IS_CLEAR(error);
364           return DBUS_SERVER_LISTEN_OK;
365         }
366       else
367         {
368           _DBUS_ASSERT_ERROR_IS_SET(error);
369           return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
370         }
371     }
372   else
373     {
374       _DBUS_ASSERT_ERROR_IS_CLEAR(error);
375       return DBUS_SERVER_LISTEN_NOT_HANDLED;
376     }
377 }
378
379 /**
380  * Opens a debug pipe transport, used in the test suite.
381  * 
382  * @param entry the address entry to try opening as debug-pipe
383  * @param transport_p return location for the opened transport
384  * @param error error to be set
385  * @returns result of the attempt
386  */
387 DBusTransportOpenResult
388 _dbus_transport_open_debug_pipe (DBusAddressEntry  *entry,
389                                  DBusTransport    **transport_p,
390                                  DBusError         *error)
391 {
392   const char *method;
393   
394   method = dbus_address_entry_get_method (entry);
395   _dbus_assert (method != NULL);
396
397   if (strcmp (method, "debug-pipe") == 0)
398     {
399       const char *name = dbus_address_entry_get_value (entry, "name");
400
401       if (name == NULL)
402         {
403           _dbus_set_bad_address (error, "debug-pipe", "name",
404                                  NULL);
405           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
406         }
407           
408       *transport_p = _dbus_transport_debug_pipe_new (name, error);
409
410       if (*transport_p == NULL)
411         {
412           _DBUS_ASSERT_ERROR_IS_SET (error);
413           return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
414         }
415       else
416         {
417           _DBUS_ASSERT_ERROR_IS_CLEAR (error);
418           return DBUS_TRANSPORT_OPEN_OK;
419         }      
420     }
421   else
422     {
423       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
424       return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
425     }
426 }
427
428
429 /** @} */
430
431 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
432