D-Bus
1.10.12
|
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 00002 /* dbus-server-debug-pipe.c In-proc debug server implementation 00003 * 00004 * Copyright (C) 2003 CodeFactory AB 00005 * Copyright (C) 2003, 2004 Red Hat, Inc. 00006 * 00007 * Licensed under the Academic Free License version 2.1 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 * 00023 */ 00024 00025 #include <config.h> 00026 #include "dbus-internals.h" 00027 #include "dbus-server-debug-pipe.h" 00028 #include "dbus-transport-socket.h" 00029 #include "dbus-connection-internal.h" 00030 #include "dbus-hash.h" 00031 #include "dbus-string.h" 00032 #include "dbus-protocol.h" 00033 00034 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 00035 00050 typedef struct DBusServerDebugPipe DBusServerDebugPipe; 00051 00056 struct DBusServerDebugPipe 00057 { 00058 DBusServer base; 00060 char *name; 00062 dbus_bool_t disconnected; 00063 }; 00064 00065 /* FIXME not threadsafe (right now the test suite doesn't use threads anyhow ) */ 00066 static DBusHashTable *server_pipe_hash; 00067 static int server_pipe_hash_refcount = 0; 00068 00069 static dbus_bool_t 00070 pipe_hash_ref (void) 00071 { 00072 if (!server_pipe_hash) 00073 { 00074 _dbus_assert (server_pipe_hash_refcount == 0); 00075 00076 server_pipe_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL); 00077 00078 if (!server_pipe_hash) 00079 return FALSE; 00080 } 00081 00082 server_pipe_hash_refcount = 1; 00083 00084 return TRUE; 00085 } 00086 00087 static void 00088 pipe_hash_unref (void) 00089 { 00090 _dbus_assert (server_pipe_hash != NULL); 00091 _dbus_assert (server_pipe_hash_refcount > 0); 00092 00093 server_pipe_hash_refcount -= 1; 00094 if (server_pipe_hash_refcount == 0) 00095 { 00096 _dbus_hash_table_unref (server_pipe_hash); 00097 server_pipe_hash = NULL; 00098 } 00099 } 00100 00101 static void 00102 debug_finalize (DBusServer *server) 00103 { 00104 DBusServerDebugPipe *debug_server = (DBusServerDebugPipe*) server; 00105 00106 pipe_hash_unref (); 00107 00108 _dbus_server_finalize_base (server); 00109 00110 dbus_free (debug_server->name); 00111 dbus_free (server); 00112 } 00113 00114 static void 00115 debug_disconnect (DBusServer *server) 00116 { 00117 ((DBusServerDebugPipe*)server)->disconnected = TRUE; 00118 } 00119 00120 static DBusServerVTable debug_vtable = { 00121 debug_finalize, 00122 debug_disconnect 00123 }; 00124 00132 DBusServer* 00133 _dbus_server_debug_pipe_new (const char *server_name, 00134 DBusError *error) 00135 { 00136 DBusServerDebugPipe *debug_server; 00137 DBusString address; 00138 DBusString name_str; 00139 00140 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00141 00142 if (!pipe_hash_ref ()) 00143 return NULL; 00144 00145 if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL) 00146 { 00147 dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, NULL); 00148 pipe_hash_unref (); 00149 return NULL; 00150 } 00151 00152 debug_server = dbus_new0 (DBusServerDebugPipe, 1); 00153 if (debug_server == NULL) 00154 goto nomem_0; 00155 00156 if (!_dbus_string_init (&address)) 00157 goto nomem_1; 00158 00159 _dbus_string_init_const (&name_str, server_name); 00160 if (!_dbus_string_append (&address, "debug-pipe:name=") || 00161 !_dbus_address_append_escaped (&address, &name_str)) 00162 goto nomem_2; 00163 00164 debug_server->name = _dbus_strdup (server_name); 00165 if (debug_server->name == NULL) 00166 goto nomem_2; 00167 00168 if (!_dbus_server_init_base (&debug_server->base, 00169 &debug_vtable, &address, 00170 error)) 00171 goto fail_3; 00172 00173 if (!_dbus_hash_table_insert_string (server_pipe_hash, 00174 debug_server->name, 00175 debug_server)) 00176 goto nomem_4; 00177 00178 _dbus_string_free (&address); 00179 00180 /* server keeps the pipe hash ref */ 00181 00182 _dbus_server_trace_ref (&debug_server->base, 0, 1, "debug_pipe_new"); 00183 return (DBusServer *)debug_server; 00184 00185 nomem_4: 00186 _dbus_server_finalize_base (&debug_server->base); 00187 fail_3: 00188 dbus_free (debug_server->name); 00189 nomem_2: 00190 _dbus_string_free (&address); 00191 nomem_1: 00192 dbus_free (debug_server); 00193 nomem_0: 00194 pipe_hash_unref (); 00195 if (error != NULL && !dbus_error_is_set (error)) 00196 _DBUS_SET_OOM (error); 00197 return NULL; 00198 } 00199 00209 DBusTransport* 00210 _dbus_transport_debug_pipe_new (const char *server_name, 00211 DBusError *error) 00212 { 00213 DBusTransport *client_transport; 00214 DBusTransport *server_transport; 00215 DBusConnection *connection; 00216 DBusSocket client_fd, server_fd; 00217 DBusServer *server; 00218 DBusString address; 00219 00220 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00221 00222 if (server_pipe_hash == NULL) 00223 { 00224 dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL); 00225 return NULL; 00226 } 00227 00228 server = _dbus_hash_table_lookup_string (server_pipe_hash, 00229 server_name); 00230 if (server == NULL || 00231 ((DBusServerDebugPipe*)server)->disconnected) 00232 { 00233 dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL); 00234 return NULL; 00235 } 00236 00237 if (!_dbus_string_init (&address)) 00238 { 00239 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00240 return NULL; 00241 } 00242 00243 if (!_dbus_string_append (&address, "debug-pipe:name=") || 00244 !_dbus_string_append (&address, server_name)) 00245 { 00246 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00247 _dbus_string_free (&address); 00248 return NULL; 00249 } 00250 00251 if (!_dbus_socketpair (&client_fd, &server_fd, FALSE, NULL)) 00252 { 00253 _dbus_verbose ("failed to create full duplex pipe\n"); 00254 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not create full-duplex pipe"); 00255 _dbus_string_free (&address); 00256 return NULL; 00257 } 00258 00259 client_transport = _dbus_transport_new_for_socket (client_fd, 00260 NULL, &address); 00261 if (client_transport == NULL) 00262 { 00263 _dbus_close_socket (client_fd, NULL); 00264 _dbus_close_socket (server_fd, NULL); 00265 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00266 _dbus_string_free (&address); 00267 return NULL; 00268 } 00269 00270 _dbus_string_free (&address); 00271 00272 _dbus_socket_invalidate (&client_fd); 00273 00274 server_transport = _dbus_transport_new_for_socket (server_fd, 00275 &server->guid_hex, NULL); 00276 if (server_transport == NULL) 00277 { 00278 _dbus_transport_unref (client_transport); 00279 _dbus_close_socket (server_fd, NULL); 00280 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00281 return NULL; 00282 } 00283 00284 _dbus_socket_invalidate (&server_fd); 00285 00286 if (!_dbus_transport_set_auth_mechanisms (server_transport, 00287 (const char**) server->auth_mechanisms)) 00288 { 00289 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00290 _dbus_transport_unref (server_transport); 00291 _dbus_transport_unref (client_transport); 00292 return NULL; 00293 } 00294 00295 connection = _dbus_connection_new_for_transport (server_transport); 00296 _dbus_transport_unref (server_transport); 00297 server_transport = NULL; 00298 00299 if (connection == NULL) 00300 { 00301 _dbus_transport_unref (client_transport); 00302 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 00303 return NULL; 00304 } 00305 00306 /* See if someone wants to handle this new connection, 00307 * self-referencing for paranoia 00308 */ 00309 if (server->new_connection_function) 00310 { 00311 dbus_server_ref (server); 00312 (* server->new_connection_function) (server, connection, 00313 server->new_connection_data); 00314 dbus_server_unref (server); 00315 } 00316 00317 /* If no one grabbed a reference, the connection will die, 00318 * and the client transport will get an immediate disconnect 00319 */ 00320 _dbus_connection_close_if_only_one_ref (connection); 00321 dbus_connection_unref (connection); 00322 00323 return client_transport; 00324 } 00325 00337 DBusServerListenResult 00338 _dbus_server_listen_debug_pipe (DBusAddressEntry *entry, 00339 DBusServer **server_p, 00340 DBusError *error) 00341 { 00342 const char *method; 00343 00344 *server_p = NULL; 00345 00346 method = dbus_address_entry_get_method (entry); 00347 00348 if (strcmp (method, "debug-pipe") == 0) 00349 { 00350 const char *name = dbus_address_entry_get_value (entry, "name"); 00351 00352 if (name == NULL) 00353 { 00354 _dbus_set_bad_address(error, "debug-pipe", "name", 00355 NULL); 00356 return DBUS_SERVER_LISTEN_BAD_ADDRESS; 00357 } 00358 00359 *server_p = _dbus_server_debug_pipe_new (name, error); 00360 00361 if (*server_p) 00362 { 00363 _DBUS_ASSERT_ERROR_IS_CLEAR(error); 00364 return DBUS_SERVER_LISTEN_OK; 00365 } 00366 else 00367 { 00368 _DBUS_ASSERT_ERROR_IS_SET(error); 00369 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; 00370 } 00371 } 00372 else 00373 { 00374 _DBUS_ASSERT_ERROR_IS_CLEAR(error); 00375 return DBUS_SERVER_LISTEN_NOT_HANDLED; 00376 } 00377 } 00378 00387 DBusTransportOpenResult 00388 _dbus_transport_open_debug_pipe (DBusAddressEntry *entry, 00389 DBusTransport **transport_p, 00390 DBusError *error) 00391 { 00392 const char *method; 00393 00394 method = dbus_address_entry_get_method (entry); 00395 _dbus_assert (method != NULL); 00396 00397 if (strcmp (method, "debug-pipe") == 0) 00398 { 00399 const char *name = dbus_address_entry_get_value (entry, "name"); 00400 00401 if (name == NULL) 00402 { 00403 _dbus_set_bad_address (error, "debug-pipe", "name", 00404 NULL); 00405 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS; 00406 } 00407 00408 *transport_p = _dbus_transport_debug_pipe_new (name, error); 00409 00410 if (*transport_p == NULL) 00411 { 00412 _DBUS_ASSERT_ERROR_IS_SET (error); 00413 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT; 00414 } 00415 else 00416 { 00417 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00418 return DBUS_TRANSPORT_OPEN_OK; 00419 } 00420 } 00421 else 00422 { 00423 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 00424 return DBUS_TRANSPORT_OPEN_NOT_HANDLED; 00425 } 00426 } 00427 00428 00431 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */ 00432