dbus-server-debug-pipe.c
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-server-debug-pipe.c In-proc debug server implementation
*
* Copyright (C) 2003 CodeFactory AB
* Copyright (C) 2003, 2004 Red Hat, Inc.
*
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <config.h>
#include "dbus-internals.h"
#include "dbus-server-debug-pipe.h"
#include "dbus-transport-socket.h"
#include "dbus-connection-internal.h"
#include "dbus-hash.h"
#include "dbus-string.h"
#include "dbus-protocol.h"
#ifdef DBUS_ENABLE_EMBEDDED_TESTS
typedef struct DBusServerDebugPipe DBusServerDebugPipe;
struct DBusServerDebugPipe
{
DBusServer base;
char *name;
dbus_bool_t disconnected;
};
/* FIXME not threadsafe (right now the test suite doesn't use threads anyhow ) */
static DBusHashTable *server_pipe_hash;
static int server_pipe_hash_refcount = 0;
static dbus_bool_t
pipe_hash_ref (void)
{
if (!server_pipe_hash)
{
_dbus_assert (server_pipe_hash_refcount == 0);
server_pipe_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL);
if (!server_pipe_hash)
return FALSE;
}
server_pipe_hash_refcount = 1;
return TRUE;
}
static void
pipe_hash_unref (void)
{
_dbus_assert (server_pipe_hash != NULL);
_dbus_assert (server_pipe_hash_refcount > 0);
server_pipe_hash_refcount -= 1;
if (server_pipe_hash_refcount == 0)
{
_dbus_hash_table_unref (server_pipe_hash);
server_pipe_hash = NULL;
}
}
static void
debug_finalize (DBusServer *server)
{
DBusServerDebugPipe *debug_server = (DBusServerDebugPipe*) server;
pipe_hash_unref ();
_dbus_server_finalize_base (server);
dbus_free (debug_server->name);
dbus_free (server);
}
static void
debug_disconnect (DBusServer *server)
{
((DBusServerDebugPipe*)server)->disconnected = TRUE;
}
static DBusServerVTable debug_vtable = {
debug_finalize,
debug_disconnect
};
DBusServer*
_dbus_server_debug_pipe_new (const char *server_name,
DBusError *error)
{
DBusServerDebugPipe *debug_server;
DBusString address;
DBusString name_str;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (!pipe_hash_ref ())
return NULL;
if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL)
{
dbus_set_error (error, DBUS_ERROR_ADDRESS_IN_USE, NULL);
pipe_hash_unref ();
return NULL;
}
debug_server = dbus_new0 (DBusServerDebugPipe, 1);
if (debug_server == NULL)
goto nomem_0;
if (!_dbus_string_init (&address))
goto nomem_1;
_dbus_string_init_const (&name_str, server_name);
if (!_dbus_string_append (&address, "debug-pipe:name=") ||
!_dbus_address_append_escaped (&address, &name_str))
goto nomem_2;
debug_server->name = _dbus_strdup (server_name);
if (debug_server->name == NULL)
goto nomem_2;
if (!_dbus_server_init_base (&debug_server->base,
&debug_vtable, &address,
error))
goto fail_3;
if (!_dbus_hash_table_insert_string (server_pipe_hash,
debug_server->name,
debug_server))
goto nomem_4;
_dbus_string_free (&address);
/* server keeps the pipe hash ref */
_dbus_server_trace_ref (&debug_server->base, 0, 1, "debug_pipe_new");
return (DBusServer *)debug_server;
nomem_4:
_dbus_server_finalize_base (&debug_server->base);
fail_3:
dbus_free (debug_server->name);
nomem_2:
_dbus_string_free (&address);
nomem_1:
dbus_free (debug_server);
nomem_0:
pipe_hash_unref ();
if (error != NULL && !dbus_error_is_set (error))
_DBUS_SET_OOM (error);
return NULL;
}
DBusTransport*
_dbus_transport_debug_pipe_new (const char *server_name,
DBusError *error)
{
DBusTransport *client_transport;
DBusTransport *server_transport;
DBusConnection *connection;
DBusSocket client_fd, server_fd;
DBusServer *server;
DBusString address;
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
if (server_pipe_hash == NULL)
{
dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
return NULL;
}
server = _dbus_hash_table_lookup_string (server_pipe_hash,
server_name);
if (server == NULL ||
((DBusServerDebugPipe*)server)->disconnected)
{
dbus_set_error (error, DBUS_ERROR_NO_SERVER, NULL);
return NULL;
}
if (!_dbus_string_init (&address))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
if (!_dbus_string_append (&address, "debug-pipe:name=") ||
!_dbus_string_append (&address, server_name))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (&address);
return NULL;
}
if (!_dbus_socketpair (&client_fd, &server_fd, FALSE, NULL))
{
_dbus_verbose ("failed to create full duplex pipe\n");
dbus_set_error (error, DBUS_ERROR_FAILED, "Could not create full-duplex pipe");
_dbus_string_free (&address);
return NULL;
}
client_transport = _dbus_transport_new_for_socket (client_fd,
NULL, &address);
if (client_transport == NULL)
{
_dbus_close_socket (&client_fd, NULL);
_dbus_close_socket (&server_fd, NULL);
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_string_free (&address);
return NULL;
}
_dbus_string_free (&address);
_dbus_socket_invalidate (&client_fd);
server_transport = _dbus_transport_new_for_socket (server_fd,
&server->guid_hex, NULL);
if (server_transport == NULL)
{
_dbus_transport_unref (client_transport);
_dbus_close_socket (&server_fd, NULL);
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
_dbus_socket_invalidate (&server_fd);
if (!_dbus_transport_set_auth_mechanisms (server_transport,
(const char**) server->auth_mechanisms))
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_transport_unref (server_transport);
_dbus_transport_unref (client_transport);
return NULL;
}
connection = _dbus_connection_new_for_transport (server_transport);
_dbus_transport_unref (server_transport);
server_transport = NULL;
if (connection == NULL)
{
_dbus_transport_unref (client_transport);
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
return NULL;
}
/* See if someone wants to handle this new connection,
* self-referencing for paranoia
*/
if (server->new_connection_function)
{
dbus_server_ref (server);
(* server->new_connection_function) (server, connection,
server->new_connection_data);
dbus_server_unref (server);
}
/* If no one grabbed a reference, the connection will die,
* and the client transport will get an immediate disconnect
*/
_dbus_connection_close_if_only_one_ref (connection);
dbus_connection_unref (connection);
return client_transport;
}
DBusServerListenResult
_dbus_server_listen_debug_pipe (DBusAddressEntry *entry,
DBusServer **server_p,
DBusError *error)
{
const char *method;
*server_p = NULL;
method = dbus_address_entry_get_method (entry);
if (strcmp (method, "debug-pipe") == 0)
{
const char *name = dbus_address_entry_get_value (entry, "name");
if (name == NULL)
{
_dbus_set_bad_address(error, "debug-pipe", "name",
NULL);
return DBUS_SERVER_LISTEN_BAD_ADDRESS;
}
*server_p = _dbus_server_debug_pipe_new (name, error);
if (*server_p)
{
_DBUS_ASSERT_ERROR_IS_CLEAR(error);
return DBUS_SERVER_LISTEN_OK;
}
else
{
_DBUS_ASSERT_ERROR_IS_SET(error);
return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
}
}
else
{
_DBUS_ASSERT_ERROR_IS_CLEAR(error);
return DBUS_SERVER_LISTEN_NOT_HANDLED;
}
}
DBusTransportOpenResult
_dbus_transport_open_debug_pipe (DBusAddressEntry *entry,
DBusTransport **transport_p,
DBusError *error)
{
const char *method;
method = dbus_address_entry_get_method (entry);
_dbus_assert (method != NULL);
if (strcmp (method, "debug-pipe") == 0)
{
const char *name = dbus_address_entry_get_value (entry, "name");
if (name == NULL)
{
_dbus_set_bad_address (error, "debug-pipe", "name",
NULL);
return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
}
*transport_p = _dbus_transport_debug_pipe_new (name, error);
if (*transport_p == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
}
else
{
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return DBUS_TRANSPORT_OPEN_OK;
}
}
else
{
_DBUS_ASSERT_ERROR_IS_CLEAR (error);
return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
}
}
#endif /* DBUS_ENABLE_EMBEDDED_TESTS */
_dbus_address_append_escaped
dbus_bool_t _dbus_address_append_escaped(DBusString *escaped, const DBusString *unescaped)
Appends an escaped version of one string to another string, using the D-Bus address escaping mechanis...
Definition dbus-address.c:109
_dbus_set_bad_address
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
Definition dbus-address.c:70
dbus_address_entry_get_method
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition dbus-address.c:232
dbus_address_entry_get_value
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition dbus-address.c:249
_dbus_connection_new_for_transport
DBusConnection * _dbus_connection_new_for_transport(DBusTransport *transport)
Creates a new connection for the given transport.
Definition dbus-connection.c:1253
_dbus_connection_close_if_only_one_ref
void _dbus_connection_close_if_only_one_ref(DBusConnection *connection)
Used internally to handle the semantics of dbus_server_set_new_connection_function().
Definition dbus-connection.c:2160
dbus_connection_unref
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
Definition dbus-connection.c:2832
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition dbus-errors.c:356
dbus_error_is_set
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition dbus-errors.c:331
_dbus_hash_table_insert_string
dbus_bool_t _dbus_hash_table_insert_string(DBusHashTable *table, char *key, void *value)
Creates a hash entry with the given key and value.
Definition dbus-hash.c:1278
_dbus_hash_table_unref
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero.
Definition dbus-hash.c:368
_dbus_hash_table_new
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
Definition dbus-hash.c:292
_dbus_hash_table_lookup_string
void * _dbus_hash_table_lookup_string(DBusHashTable *table, const char *key)
Looks up the value for a given string in a hash table of type DBUS_HASH_STRING.
Definition dbus-hash.c:1113
DBUS_HASH_STRING
@ DBUS_HASH_STRING
Hash keys are strings.
Definition dbus-hash.h:69
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition dbus-internals.h:153
_dbus_strdup
char * _dbus_strdup(const char *str)
Duplicates a string.
Definition dbus-internals.c:621
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
Definition dbus-macros.h:51
TRUE
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
FALSE
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition dbus-memory.c:710
dbus_new0
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition dbus-memory.h:60
DBUS_ERROR_ADDRESS_IN_USE
#define DBUS_ERROR_ADDRESS_IN_USE
Can't bind a socket since its address is in use (i.e.
Definition dbus-protocol.h:393
DBUS_ERROR_NO_SERVER
#define DBUS_ERROR_NO_SERVER
Unable to connect to server (probably caused by ECONNREFUSED on a socket).
Definition dbus-protocol.h:383
DBUS_ERROR_FAILED
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
Definition dbus-protocol.h:361
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition dbus-protocol.h:363
_dbus_server_init_base
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address, DBusError *error)
Initializes the members of the DBusServer base class.
Definition dbus-server.c:113
_dbus_server_finalize_base
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
Definition dbus-server.c:202
DBusServer
struct DBusServer DBusServer
An opaque object representing a server that listens for connections from other applications.
Definition dbus-server.h:45
dbus_server_unref
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
Definition dbus-server.c:735
dbus_server_ref
DBusServer * dbus_server_ref(DBusServer *server)
Increments the reference count of a DBusServer.
Definition dbus-server.c:703
_dbus_string_append
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition dbus-string.c:980
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition dbus-string.c:182
_dbus_string_init_const
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
Definition dbus-string.c:197
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as _DBUS_STRING_IN...
Definition dbus-string.c:278
_dbus_socketpair
dbus_bool_t _dbus_socketpair(DBusSocket *fd1, DBusSocket *fd2, dbus_bool_t blocking, DBusError *error)
Creates pair of connect sockets (as in socketpair()).
Definition dbus-sysdeps-unix.c:3884
_dbus_close_socket
dbus_bool_t _dbus_close_socket(DBusSocket *fd, DBusError *error)
Closes a socket and invalidates it.
Definition dbus-sysdeps-unix.c:314
_dbus_transport_new_for_socket
DBusTransport * _dbus_transport_new_for_socket(DBusSocket fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
Definition dbus-transport-socket.c:1295
_dbus_transport_set_auth_mechanisms
dbus_bool_t _dbus_transport_set_auth_mechanisms(DBusTransport *transport, const char **mechanisms)
Sets the SASL authentication mechanisms supported by this transport.
Definition dbus-transport.c:1565
_dbus_transport_unref
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
Definition dbus-transport.c:486
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition dbus-types.h:37
DBusAddressEntry
Internals of DBusAddressEntry.
Definition dbus-address.c:49
DBusConnection
Implementation details of DBusConnection.
Definition dbus-connection.c:259
DBusError
Object representing an exception.
Definition dbus-errors.h:51
DBusHashTable
Internals of DBusHashTable.
Definition dbus-hash.c:175
DBusServerVTable
Virtual table to be implemented by all server "subclasses".
Definition dbus-server-protected.h:46
DBusServer
Internals of DBusServer object.
Definition dbus-server-protected.h:59
DBusSocket
Socket interface.
Definition dbus-sysdeps.h:185
DBusString
Definition dbus-string.h:47
DBusTransport
Object representing a transport such as a socket.
Definition dbus-transport-protected.h:83