diff options
author | Havoc Pennington <hp@redhat.com> | 2006-09-16 17:38:24 +0000 |
---|---|---|
committer | Havoc Pennington <hp@redhat.com> | 2006-09-16 17:38:24 +0000 |
commit | 8027efc97b4bec85f674570f878919cb72456745 (patch) | |
tree | 441ffafa5367bdb93c8bfa7f8ec51acfeb432b0b /dbus/dbus-server-unix.c | |
parent | a0a12210e1f5aa5bd185a57d4394b8eb51cbe472 (diff) |
voc Pennington <hp@redhat.com>
* dbus/dbus-server.c (dbus_server_listen): change how this works
to be able to delegate to a set of handlers that can succeed,
fail, or choose not to handle. Allows us to have
dbus_server_listen_platform_specific.
* dbus/dbus-server-socket.c (_dbus_server_new_for_tcp_socket):
factor out the tcp socket stuff to be used on windows, leaving
unix domain socket only in dbus-socket-unix.c
* dbus/dbus-transport-socket.c
(_dbus_transport_new_for_tcp_socket): factor out the tcp socket
stuff to be used on windows, leaving unix domain socket only
in dbus-transport-unix.c
* dbus/dbus-connection.c (dbus_connection_get_unix_user): insert
temporary hack to be sure this fails on windows
(dbus_connection_get_unix_process_id): ditto
Diffstat (limited to 'dbus/dbus-server-unix.c')
-rw-r--r-- | dbus/dbus-server-unix.c | 418 |
1 files changed, 101 insertions, 317 deletions
diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c index 377dfb03..873307ec 100644 --- a/dbus/dbus-server-unix.c +++ b/dbus/dbus-server-unix.c @@ -23,11 +23,11 @@ #include "dbus-internals.h" #include "dbus-server-unix.h" +#include "dbus-server-socket.h" #include "dbus-transport-unix.h" #include "dbus-connection-internal.h" +#include "dbus-sysdeps-unix.h" #include "dbus-string.h" -#include <sys/types.h> -#include <unistd.h> /** * @defgroup DBusServerUnix DBusServer implementations for UNIX @@ -36,271 +36,123 @@ * * @{ */ -/** - * - * Opaque object representing a Unix server implementation. - */ -typedef struct DBusServerUnix DBusServerUnix; - -/** - * Implementation details of DBusServerUnix. All members - * are private. - */ -struct DBusServerUnix -{ - DBusServer base; /**< Parent class members. */ - int fd; /**< File descriptor or -1 if disconnected. */ - DBusWatch *watch; /**< File descriptor watch. */ - char *socket_name; /**< Name of domain socket, to unlink if appropriate */ -}; - -static void -unix_finalize (DBusServer *server) -{ - DBusServerUnix *unix_server = (DBusServerUnix*) server; - - _dbus_server_finalize_base (server); - - if (unix_server->watch) - { - _dbus_watch_unref (unix_server->watch); - unix_server->watch = NULL; - } - - dbus_free (unix_server->socket_name); - dbus_free (server); -} /** - * @todo unreffing the connection at the end may cause - * us to drop the last ref to the connection before - * disconnecting it. That is invalid. - * - * @todo doesn't this leak a server refcount if - * new_connection_function is NULL? + * Tries to interpret the address entry in a platform-specific + * way, creating a platform-specific server type if appropriate. + * Sets error if the result is not OK. + * + * @param entry an address entry + * @param a new DBusServer, or #NULL on failure. + * @param error location to store rationale for failure on bad address + * @returns the outcome + * */ -/* Return value is just for memory, not other failures. */ -static dbus_bool_t -handle_new_client_fd_and_unlock (DBusServer *server, - int client_fd) +DBusServerListenResult +_dbus_server_listen_platform_specific (DBusAddressEntry *entry, + DBusServer **server_p, + DBusError *error) { - DBusConnection *connection; - DBusTransport *transport; - DBusNewConnectionFunction new_connection_function; - void *new_connection_data; - - _dbus_verbose ("Creating new client connection with fd %d\n", client_fd); + const char *method; - HAVE_LOCK_CHECK (server); - - if (!_dbus_set_fd_nonblocking (client_fd, NULL)) - { - SERVER_UNLOCK (server); - return TRUE; - } - - transport = _dbus_transport_new_for_fd (client_fd, &server->guid_hex, NULL); - if (transport == NULL) - { - _dbus_close_socket (client_fd, NULL); - SERVER_UNLOCK (server); - return FALSE; - } - - if (!_dbus_transport_set_auth_mechanisms (transport, - (const char **) server->auth_mechanisms)) - { - _dbus_transport_unref (transport); - SERVER_UNLOCK (server); - return FALSE; - } - - /* note that client_fd is now owned by the transport, and will be - * closed on transport disconnection/finalization - */ - - connection = _dbus_connection_new_for_transport (transport); - _dbus_transport_unref (transport); - transport = NULL; /* now under the connection lock */ + *server_p = NULL; - if (connection == NULL) - { - SERVER_UNLOCK (server); - return FALSE; - } - - /* See if someone wants to handle this new connection, self-referencing - * for paranoia. - */ - new_connection_function = server->new_connection_function; - new_connection_data = server->new_connection_data; + method = dbus_address_entry_get_method (entry); - _dbus_server_ref_unlocked (server); - SERVER_UNLOCK (server); - - if (new_connection_function) + if (strcmp (method, "unix") == 0) { - (* new_connection_function) (server, connection, - new_connection_data); - dbus_server_unref (server); - } - - /* If no one grabbed a reference, the connection will die. */ - dbus_connection_unref (connection); - - return TRUE; -} - -static dbus_bool_t -unix_handle_watch (DBusWatch *watch, - unsigned int flags, - void *data) -{ - DBusServer *server = data; - DBusServerUnix *unix_server = data; - - SERVER_LOCK (server); - - _dbus_assert (watch == unix_server->watch); + const char *path = dbus_address_entry_get_value (entry, "path"); + const char *tmpdir = dbus_address_entry_get_value (entry, "tmpdir"); + const char *abstract = dbus_address_entry_get_value (entry, "abstract"); + + if (path == NULL && tmpdir == NULL && abstract == NULL) + { + _dbus_server_set_bad_address(error, "unix", + "path or tmpdir or abstract", + NULL); + return DBUS_SERVER_LISTEN_BAD_ADDRESS; + } - _dbus_verbose ("Handling client connection, flags 0x%x\n", flags); - - if (flags & DBUS_WATCH_READABLE) - { - int client_fd; - int listen_fd; - - listen_fd = dbus_watch_get_fd (watch); + if ((path && tmpdir) || + (path && abstract) || + (tmpdir && abstract)) + { + _dbus_server_set_bad_address(error, NULL, NULL, + "cannot specify two of \"path\" and \"tmpdir\" and \"abstract\" at the same time"); + return DBUS_SERVER_LISTEN_BAD_ADDRESS; + } - client_fd = _dbus_accept (listen_fd); - - if (client_fd < 0) + if (tmpdir != NULL) { - /* EINTR handled for us */ - - if (errno == EAGAIN || errno == EWOULDBLOCK) - _dbus_verbose ("No client available to accept after all\n"); + DBusString full_path; + DBusString filename; + + if (!_dbus_string_init (&full_path)) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; + } + + if (!_dbus_string_init (&filename)) + { + _dbus_string_free (&full_path); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; + } + + if (!_dbus_string_append (&filename, + "dbus-") || + !_dbus_generate_random_ascii (&filename, 10) || + !_dbus_string_append (&full_path, tmpdir) || + !_dbus_concat_dir_and_file (&full_path, &filename)) + { + _dbus_string_free (&full_path); + _dbus_string_free (&filename); + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; + } + + /* Always use abstract namespace if possible with tmpdir */ + + *server_p = + _dbus_server_new_for_domain_socket (_dbus_string_get_const_data (&full_path), +#ifdef HAVE_ABSTRACT_SOCKETS + TRUE, +#else + FALSE, +#endif + error); + + _dbus_string_free (&full_path); + _dbus_string_free (&filename); + } + else + { + if (path) + *server_p = _dbus_server_new_for_domain_socket (path, FALSE, error); else - _dbus_verbose ("Failed to accept a client connection: %s\n", - _dbus_strerror (errno)); + *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, error); + } - SERVER_UNLOCK (server); + if (*server_p != NULL) + { + _DBUS_ASSERT_ERROR_IS_CLEAR(error); + return DBUS_SERVER_LISTEN_OK; } else { - _dbus_fd_set_close_on_exec (client_fd); - - if (!handle_new_client_fd_and_unlock (server, client_fd)) - _dbus_verbose ("Rejected client connection due to lack of memory\n"); + _DBUS_ASSERT_ERROR_IS_SET(error); + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; } } - - if (flags & DBUS_WATCH_ERROR) - _dbus_verbose ("Error on server listening socket\n"); - - if (flags & DBUS_WATCH_HANGUP) - _dbus_verbose ("Hangup on server listening socket\n"); - - return TRUE; -} - -static void -unix_disconnect (DBusServer *server) -{ - DBusServerUnix *unix_server = (DBusServerUnix*) server; - - HAVE_LOCK_CHECK (server); - - if (unix_server->watch) - { - _dbus_server_remove_watch (server, - unix_server->watch); - _dbus_watch_unref (unix_server->watch); - unix_server->watch = NULL; - } - - _dbus_close_socket (unix_server->fd, NULL); - unix_server->fd = -1; - - if (unix_server->socket_name != NULL) - { - DBusString tmp; - _dbus_string_init_const (&tmp, unix_server->socket_name); - _dbus_delete_file (&tmp, NULL); - } - - HAVE_LOCK_CHECK (server); -} - -static const DBusServerVTable unix_vtable = { - unix_finalize, - unix_disconnect -}; - -/** - * Creates a new server listening on the given file descriptor. The - * file descriptor should be nonblocking (use - * _dbus_set_fd_nonblocking() to make it so). The file descriptor - * should be listening for connections, that is, listen() should have - * been successfully invoked on it. The server will use accept() to - * accept new client connections. - * - * @param fd the file descriptor. - * @param address the server's address - * @returns the new server, or #NULL if no memory. - * - */ -DBusServer* -_dbus_server_new_for_fd (int fd, - const DBusString *address) -{ - DBusServerUnix *unix_server; - DBusServer *server; - DBusWatch *watch; - - unix_server = dbus_new0 (DBusServerUnix, 1); - if (unix_server == NULL) - return NULL; - - watch = _dbus_watch_new (fd, - DBUS_WATCH_READABLE, - TRUE, - unix_handle_watch, unix_server, - NULL); - if (watch == NULL) - { - dbus_free (unix_server); - return NULL; - } - - if (!_dbus_server_init_base (&unix_server->base, - &unix_vtable, address)) - { - _dbus_watch_unref (watch); - dbus_free (unix_server); - return NULL; - } - - server = (DBusServer*) unix_server; - - SERVER_LOCK (server); - - if (!_dbus_server_add_watch (&unix_server->base, - watch)) + else { - SERVER_UNLOCK (server); - _dbus_server_finalize_base (&unix_server->base); - _dbus_watch_unref (watch); - dbus_free (unix_server); - return NULL; + /* If we don't handle the method, we return NULL with the + * error unset + */ + _DBUS_ASSERT_ERROR_IS_CLEAR(error); + return DBUS_SERVER_LISTEN_NOT_HANDLED; } - - unix_server->fd = fd; - unix_server->watch = watch; - - SERVER_UNLOCK (server); - - return (DBusServer*) unix_server; } /** @@ -317,7 +169,6 @@ _dbus_server_new_for_domain_socket (const char *path, DBusError *error) { DBusServer *server; - DBusServerUnix *unix_server; int listen_fd; DBusString address; char *path_copy; @@ -358,15 +209,14 @@ _dbus_server_new_for_domain_socket (const char *path, goto failed_1; } - server = _dbus_server_new_for_fd (listen_fd, &address); + server = _dbus_server_new_for_socket (listen_fd, &address); if (server == NULL) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto failed_2; } - unix_server = (DBusServerUnix*) server; - unix_server->socket_name = path_copy; + _dbus_server_socket_own_filename(server, path_copy); _dbus_string_free (&address); @@ -382,71 +232,5 @@ _dbus_server_new_for_domain_socket (const char *path, return NULL; } -/** - * Creates a new server listening on the given hostname and port. - * If the hostname is NULL, listens on localhost. - * - * @param host the hostname to listen on. - * @param port the port to listen on. - * @param error location to store reason for failure. - * @returns the new server, or #NULL on failure. - */ -DBusServer* -_dbus_server_new_for_tcp_socket (const char *host, - dbus_uint32_t port, - DBusError *error) -{ - DBusServer *server; - int listen_fd; - DBusString address; - DBusString host_str; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - if (!_dbus_string_init (&address)) - { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return NULL; - } - - if (host == NULL) - host = "localhost"; - - _dbus_string_init_const (&host_str, host); - if (!_dbus_string_append (&address, "tcp:host=") || - !_dbus_address_append_escaped (&address, &host_str) || - !_dbus_string_append (&address, ",port=") || - !_dbus_string_append_int (&address, port)) - { - _dbus_string_free (&address); - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return NULL; - } - - listen_fd = _dbus_listen_tcp_socket (host, port, error); - _dbus_fd_set_close_on_exec (listen_fd); - - if (listen_fd < 0) - { - _dbus_string_free (&address); - return NULL; - } - - server = _dbus_server_new_for_fd (listen_fd, &address); - if (server == NULL) - { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - _dbus_close_socket (listen_fd, NULL); - _dbus_string_free (&address); - return NULL; - } - - _dbus_string_free (&address); - - return server; - - -} - /** @} */ |