summaryrefslogtreecommitdiffstats
path: root/dbus
diff options
context:
space:
mode:
authorHavoc Pennington <hp@redhat.com>2003-03-15 04:25:09 +0000
committerHavoc Pennington <hp@redhat.com>2003-03-15 04:25:09 +0000
commit169238e99a4a163c89eb053250daeedf5f73e5cd (patch)
tree4b8f9af81765077fd9a227bae974924d905f182f /dbus
parent4c95a9782c65f88e2904c44abeb734a1b00f6353 (diff)
2003-03-14 Havoc Pennington <hp@pobox.com>
* bus/dispatch.c (bus_dispatch_test): do test using debug-pipe transport, tests more of the real codepath. Set up clients with bus_setup_debug_client. * bus/test.c (bus_setup_debug_client): function to set up debug "clients" on the main loop * dbus/dbus-transport.c (_dbus_transport_open): add debug-pipe support * dbus/dbus-server.c (dbus_server_listen): add debug-pipe server type * dbus/dbus-server-debug.c: support a debug server based on pipes * dbus/dbus-sysdeps.c (_dbus_full_duplex_pipe): new function (_dbus_close): new function * configure.in: check for socketpair
Diffstat (limited to 'dbus')
-rw-r--r--dbus/Makefile.am2
-rw-r--r--dbus/dbus-auth.c5
-rw-r--r--dbus/dbus-memory.c9
-rw-r--r--dbus/dbus-server-debug-pipe.c260
-rw-r--r--dbus/dbus-server-debug-pipe.h40
-rw-r--r--dbus/dbus-server.c13
-rw-r--r--dbus/dbus-sysdeps.c74
-rw-r--r--dbus/dbus-sysdeps.h11
-rw-r--r--dbus/dbus-transport-unix.c6
-rw-r--r--dbus/dbus-transport.c12
10 files changed, 423 insertions, 9 deletions
diff --git a/dbus/Makefile.am b/dbus/Makefile.am
index 2f2a2bdc..e9b08d13 100644
--- a/dbus/Makefile.am
+++ b/dbus/Makefile.am
@@ -44,6 +44,8 @@ libdbus_1_la_SOURCES= \
dbus-server-protected.h \
dbus-server-debug.c \
dbus-server-debug.h \
+ dbus-server-debug-pipe.c \
+ dbus-server-debug-pipe.h \
dbus-server-unix.c \
dbus-server-unix.h \
dbus-sha.c \
diff --git a/dbus/dbus-auth.c b/dbus/dbus-auth.c
index 8dfdc766..73454bc1 100644
--- a/dbus/dbus-auth.c
+++ b/dbus/dbus-auth.c
@@ -428,8 +428,9 @@ sha1_compute_hash (DBusAuth *auth,
return retval;
}
-/* http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
- * entropy, we use 128
+/** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
+ * entropy, we use 128. This is the number of bytes in the random
+ * challenge.
*/
#define N_CHALLENGE_BYTES (128/8)
diff --git a/dbus/dbus-memory.c b/dbus/dbus-memory.c
index 5f0d9b8e..83d17e6c 100644
--- a/dbus/dbus-memory.c
+++ b/dbus/dbus-memory.c
@@ -77,11 +77,17 @@ static dbus_bool_t inited = FALSE;
static int fail_counts = -1;
static size_t fail_size = 0;
static dbus_bool_t guards = FALSE;
+/** value stored in guard padding for debugging buffer overrun */
#define GUARD_VALUE 0xdeadbeef
+/** size of the information about the block stored in guard mode */
#define GUARD_INFO_SIZE 8
+/** size of the GUARD_VALUE-filled padding after the header info */
#define GUARD_START_PAD 16
+/** size of the GUARD_VALUE-filled padding at the end of the block */
#define GUARD_END_PAD 16
+/** size of stuff at start of block */
#define GUARD_START_OFFSET (GUARD_START_PAD + GUARD_INFO_SIZE)
+/** total extra size over the requested allocation for guard stuff */
#define GUARD_EXTRA_SIZE (GUARD_START_OFFSET + GUARD_END_PAD)
#endif
@@ -107,6 +113,9 @@ initialize_malloc_debug (void)
}
}
+/**
+ * Where the block came from.
+ */
typedef enum
{
SOURCE_UNKNOWN,
diff --git a/dbus/dbus-server-debug-pipe.c b/dbus/dbus-server-debug-pipe.c
new file mode 100644
index 00000000..ceba7659
--- /dev/null
+++ b/dbus/dbus-server-debug-pipe.c
@@ -0,0 +1,260 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-server-debug-pipe.c In-proc debug server implementation
+ *
+ * Copyright (C) 2003 CodeFactory AB
+ * Copyright (C) 2003 Red Hat, Inc.
+ *
+ * Licensed under the Academic Free License version 1.2
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+
+#include "dbus-internals.h"
+#include "dbus-server-debug-pipe.h"
+#include "dbus-transport-unix.h"
+#include "dbus-connection-internal.h"
+#include "dbus-hash.h"
+
+#ifdef DBUS_BUILD_TESTS
+
+/**
+ * @defgroup DBusServerDebugPipe DBusServerDebugPipe
+ * @ingroup DBusInternals
+ * @brief In-process pipe debug server used in unit tests.
+ *
+ * Types and functions related to DBusServerDebugPipe.
+ * This is used for unit testing.
+ *
+ * @{
+ */
+
+/**
+ * Opaque object representing a debug server implementation.
+ */
+typedef struct DBusServerDebugPipe DBusServerDebugPipe;
+
+/**
+ * Implementation details of DBusServerDebugPipe. All members
+ * are private.
+ */
+struct DBusServerDebugPipe
+{
+ DBusServer base; /**< Parent class members. */
+
+ char *name; /**< Server name. */
+
+ dbus_bool_t disconnected; /**< TRUE if disconnect has been called */
+};
+
+static DBusHashTable *server_pipe_hash;
+
+
+static void
+debug_finalize (DBusServer *server)
+{
+ _dbus_server_finalize_base (server);
+
+ dbus_free (server);
+}
+
+static void
+debug_handle_watch (DBusServer *server,
+ DBusWatch *watch,
+ unsigned int flags)
+{
+
+}
+
+static void
+debug_disconnect (DBusServer *server)
+{
+ ((DBusServerDebugPipe*)server)->disconnected = TRUE;
+}
+
+static DBusServerVTable debug_vtable = {
+ debug_finalize,
+ debug_handle_watch,
+ debug_disconnect
+};
+
+/**
+ * Creates a new debug server using an in-process pipe
+ *
+ * @param server_name the name of the server.
+ * @param result address where a result code can be returned.
+ * @returns a new server, or #NULL on failure.
+ */
+DBusServer*
+_dbus_server_debug_pipe_new (const char *server_name,
+ DBusResultCode *result)
+{
+ DBusServerDebugPipe *debug_server;
+
+ if (!server_pipe_hash)
+ {
+ server_pipe_hash = _dbus_hash_table_new (DBUS_HASH_STRING, NULL, NULL);
+
+ if (!server_pipe_hash)
+ {
+ dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
+ return NULL;
+ }
+ }
+
+ if (_dbus_hash_table_lookup_string (server_pipe_hash, server_name) != NULL)
+ {
+ dbus_set_result (result, DBUS_RESULT_ADDRESS_IN_USE);
+ return NULL;
+ }
+
+ debug_server = dbus_new0 (DBusServerDebugPipe, 1);
+
+ if (debug_server == NULL)
+ return NULL;
+
+ debug_server->name = _dbus_strdup (server_name);
+ if (debug_server->name == NULL)
+ {
+ dbus_free (debug_server->name);
+ dbus_free (debug_server);
+
+ dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
+ }
+
+ if (!_dbus_server_init_base (&debug_server->base,
+ &debug_vtable))
+ {
+ dbus_free (debug_server->name);
+ dbus_free (debug_server);
+
+ dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
+
+ return NULL;
+ }
+
+ if (!_dbus_hash_table_insert_string (server_pipe_hash,
+ debug_server->name,
+ debug_server))
+ {
+ _dbus_server_finalize_base (&debug_server->base);
+ dbus_free (debug_server->name);
+ dbus_free (debug_server);
+
+ dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
+
+ return NULL;
+ }
+
+ dbus_set_result (result, DBUS_RESULT_SUCCESS);
+
+ return (DBusServer *)debug_server;
+}
+
+/**
+ * Creates the client-side transport for
+ * a debug-pipe connection connected to the
+ * given debug-pipe server name.
+ *
+ * @param server_name name of server to connect to
+ * @param result return location for result
+ * @returns #NULL on no memory or transport
+ */
+DBusTransport*
+_dbus_transport_debug_pipe_new (const char *server_name,
+ DBusResultCode *result)
+{
+ DBusTransport *client_transport;
+ DBusTransport *server_transport;
+ DBusConnection *connection;
+ int client_fd, server_fd;
+ DBusServer *server;
+
+ server = _dbus_hash_table_lookup_string (server_pipe_hash,
+ server_name);
+ if (server == NULL ||
+ ((DBusServerDebugPipe*)server)->disconnected)
+ {
+ dbus_set_result (result, DBUS_RESULT_BAD_ADDRESS);
+ return NULL;
+ }
+
+ if (!_dbus_full_duplex_pipe (&client_fd, &server_fd,
+ NULL))
+ {
+ _dbus_verbose ("failed to create full duplex pipe\n");
+ dbus_set_result (result, DBUS_RESULT_FAILED);
+ return NULL;
+ }
+
+ _dbus_fd_set_close_on_exec (client_fd);
+ _dbus_fd_set_close_on_exec (server_fd);
+
+ client_transport = _dbus_transport_new_for_fd (client_fd,
+ FALSE);
+ if (client_transport == NULL)
+ {
+ _dbus_close (client_fd, NULL);
+ _dbus_close (server_fd, NULL);
+ return NULL;
+ }
+
+ client_fd = -1;
+
+ server_transport = _dbus_transport_new_for_fd (server_fd,
+ TRUE);
+ if (server_transport == NULL)
+ {
+ _dbus_transport_unref (client_transport);
+ _dbus_close (server_fd, NULL);
+ return NULL;
+ }
+
+ server_fd = -1;
+
+ connection = _dbus_connection_new_for_transport (server_transport);
+ _dbus_transport_unref (server_transport);
+ server_transport = NULL;
+
+ if (connection == NULL)
+ {
+ _dbus_transport_unref (client_transport);
+ 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_unref (connection);
+
+ return client_transport;
+}
+
+
+/** @} */
+
+#endif /* DBUS_BUILD_TESTS */
+
diff --git a/dbus/dbus-server-debug-pipe.h b/dbus/dbus-server-debug-pipe.h
new file mode 100644
index 00000000..7de6957d
--- /dev/null
+++ b/dbus/dbus-server-debug-pipe.h
@@ -0,0 +1,40 @@
+/* -*- mode: C; c-file-style: "gnu" -*- */
+/* dbus-server-debug-pipe.h In-proc debug server implementation
+ *
+ * Copyright (C) 2003 CodeFactory AB
+ * Copyright (C) 2003 Red Hat, Inc.
+ *
+ * Licensed under the Academic Free License version 1.2
+ *
+ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ */
+#ifndef DBUS_SERVER_DEBUG_PIPE_H
+#define DBUS_SERVER_DEBUG_PIPE_H
+
+#include <dbus/dbus-internals.h>
+#include <dbus/dbus-server-protected.h>
+#include <dbus/dbus-transport.h>
+
+DBUS_BEGIN_DECLS;
+
+DBusServer* _dbus_server_debug_pipe_new (const char *server_name,
+ DBusResultCode *result);
+DBusTransport* _dbus_transport_debug_pipe_new (const char *server_name,
+ DBusResultCode *result);
+
+DBUS_END_DECLS;
+
+#endif /* DBUS_SERVER_DEBUG_PIPE_H */
diff --git a/dbus/dbus-server.c b/dbus/dbus-server.c
index a5e5e2b7..80ee6fc2 100644
--- a/dbus/dbus-server.c
+++ b/dbus/dbus-server.c
@@ -25,6 +25,7 @@
#include "dbus-server-unix.h"
#ifdef DBUS_BUILD_TESTS
#include "dbus-server-debug.h"
+#include "dbus-server-debug-pipe.h"
#endif
#include "dbus-address.h"
@@ -275,6 +276,18 @@ dbus_server_listen (const char *address,
if (server)
break;
}
+ else if (strcmp (method, "debug-pipe") == 0)
+ {
+ const char *name = dbus_address_entry_get_value (entries[i], "name");
+
+ if (name == NULL)
+ goto bad_address;
+
+ server = _dbus_server_debug_pipe_new (name, result);
+
+ if (server)
+ break;
+ }
#endif
else
goto bad_address;
diff --git a/dbus/dbus-sysdeps.c b/dbus/dbus-sysdeps.c
index 5d0be321..7117e8fb 100644
--- a/dbus/dbus-sysdeps.c
+++ b/dbus/dbus-sysdeps.c
@@ -2754,4 +2754,78 @@ _dbus_stat (const DBusString *filename,
return TRUE;
}
+/**
+ * Creates a full-duplex pipe (as in socketpair()).
+ * Sets both ends of the pipe nonblocking.
+ *
+ * @param fd1 return location for one end
+ * @param fd2 return location for the other end
+ * @param error error return
+ * @returns #FALSE on failure (if error is set)
+ */
+dbus_bool_t
+_dbus_full_duplex_pipe (int *fd1,
+ int *fd2,
+ DBusError *error)
+{
+#ifdef HAVE_SOCKETPAIR
+ int fds[2];
+
+ if (socketpair (AF_UNIX, SOCK_STREAM, 0, fds) < 0)
+ {
+ dbus_set_error (error, _dbus_error_from_errno (errno),
+ "Could not create full-duplex pipe");
+ return FALSE;
+ }
+
+ if (!_dbus_set_fd_nonblocking (fds[0], NULL) ||
+ !_dbus_set_fd_nonblocking (fds[1], NULL))
+ {
+ dbus_set_error (error, _dbus_error_from_errno (errno),
+ "Could not set full-duplex pipe nonblocking");
+
+ close (fds[0]);
+ close (fds[1]);
+
+ return FALSE;
+ }
+
+ *fd1 = fds[0];
+ *fd2 = fds[1];
+
+ return TRUE;
+#else
+ _dbus_warn ("_dbus_full_duplex_pipe() not implemented on this OS\n");
+ dbus_set_error (error, DBUS_ERROR_FAILED,
+ "_dbus_full_duplex_pipe() not implemented on this OS");
+ return FALSE;
+#endif
+}
+
+/**
+ * Closes a file descriptor.
+ *
+ * @param fd the file descriptor
+ * @param error error object
+ * @returns #FALSE if error set
+ */
+dbus_bool_t
+_dbus_close (int fd,
+ DBusError *error)
+{
+ again:
+ if (close (fd) < 0)
+ {
+ if (errno == EINTR)
+ goto again;
+
+ dbus_set_error (error, _dbus_error_from_errno (errno),
+ "Could not close fd %d", fd);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+
/** @} end of sysdeps */
diff --git a/dbus/dbus-sysdeps.h b/dbus/dbus-sysdeps.h
index f1ac47c8..8cd62207 100644
--- a/dbus/dbus-sysdeps.h
+++ b/dbus/dbus-sysdeps.h
@@ -196,9 +196,14 @@ typedef struct
unsigned long ctime;
} DBusStat;
-dbus_bool_t _dbus_stat (const DBusString *filename,
- DBusStat *statbuf,
- DBusError *error);
+dbus_bool_t _dbus_stat (const DBusString *filename,
+ DBusStat *statbuf,
+ DBusError *error);
+dbus_bool_t _dbus_full_duplex_pipe (int *fd1,
+ int *fd2,
+ DBusError *error);
+dbus_bool_t _dbus_close (int fd,
+ DBusError *error);
DBUS_END_DECLS;
diff --git a/dbus/dbus-transport-unix.c b/dbus/dbus-transport-unix.c
index a2b8a384..dfaeb1ad 100644
--- a/dbus/dbus-transport-unix.c
+++ b/dbus/dbus-transport-unix.c
@@ -888,7 +888,7 @@ unix_disconnect (DBusTransport *transport)
free_watches (transport);
- close (unix_transport->fd);
+ _dbus_close (unix_transport->fd, NULL);
unix_transport->fd = -1;
}
@@ -1161,7 +1161,7 @@ _dbus_transport_new_for_domain_socket (const char *path,
if (transport == NULL)
{
dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
- close (fd);
+ _dbus_close (fd, NULL);
fd = -1;
}
@@ -1199,7 +1199,7 @@ _dbus_transport_new_for_tcp_socket (const char *host,
if (transport == NULL)
{
dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
- close (fd);
+ _dbus_close (fd, NULL);
fd = -1;
}
diff --git a/dbus/dbus-transport.c b/dbus/dbus-transport.c
index 30da077f..03fea75b 100644
--- a/dbus/dbus-transport.c
+++ b/dbus/dbus-transport.c
@@ -29,6 +29,7 @@
#include "dbus-address.h"
#ifdef DBUS_BUILD_TESTS
#include "dbus-transport-debug.h"
+#include "dbus-server-debug-pipe.h"
#endif
/**
@@ -229,7 +230,16 @@ _dbus_transport_open (const char *address,
transport = _dbus_transport_debug_client_new (name, result);
}
-#endif
+ else if (strcmp (method, "debug-pipe") == 0)
+ {
+ const char *name = dbus_address_entry_get_value (entries[i], "name");
+
+ if (name == NULL)
+ goto bad_address;
+
+ transport = _dbus_transport_debug_pipe_new (name, result);
+ }
+#endif
else
goto bad_address;