X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dbus%2Fdbus-server-unix.c;h=d995240480b23e8aed56b5173d1025c30d5891eb;hb=1200c464b6c9051340960e07f0d61a51dad71286;hp=487d60ec96bb67c9bae1bc5d169c0e421277b208;hpb=dfd1292d525d01914141cc86013589c6e0ea9d5c;p=platform%2Fupstream%2Fdbus.git diff --git a/dbus/dbus-server-unix.c b/dbus/dbus-server-unix.c index 487d60e..d995240 100644 --- a/dbus/dbus-server-unix.c +++ b/dbus/dbus-server-unix.c @@ -1,10 +1,10 @@ -/* -*- mode: C; c-file-style: "gnu" -*- */ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ /* dbus-server-unix.c Server implementation for Unix network protocols. * - * Copyright (C) 2002, 2003 Red Hat Inc. + * Copyright (C) 2002, 2003, 2004 Red Hat Inc. + * + * Licensed under the Academic Free License version 2.1 * - * 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 @@ -14,20 +14,22 @@ * 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 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * */ +#include #include "dbus-internals.h" #include "dbus-server-unix.h" +#include "dbus-server-socket.h" +#include "dbus-server-launchd.h" #include "dbus-transport-unix.h" #include "dbus-connection-internal.h" +#include "dbus-sysdeps-unix.h" #include "dbus-string.h" -#include -#include /** * @defgroup DBusServerUnix DBusServer implementations for UNIX @@ -36,233 +38,191 @@ * * @{ */ -/** - * - * Opaque object representing a Unix server implementation. - */ -typedef struct DBusServerUnix DBusServerUnix; /** - * Implementation details of DBusServerUnix. All members - * are private. + * 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 server_p location to store a new DBusServer, or #NULL on failure. + * @param error location to store rationale for failure on bad address + * @returns the outcome + * */ -struct DBusServerUnix +DBusServerListenResult +_dbus_server_listen_platform_specific (DBusAddressEntry *entry, + DBusServer **server_p, + DBusError *error) { - 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; + const char *method; - if (unix_server->watch) - _dbus_watch_unref (unix_server->watch); + *server_p = NULL; - dbus_free (unix_server->socket_name); - - _dbus_server_finalize_base (server); - - dbus_free (server); -} + method = dbus_address_entry_get_method (entry); -/** - * @todo unreffing the connection at the end may cause - * us to drop the last ref to the connection before - * disconnecting it. That is invalid. - */ -/* Return value is just for memory, not other failures. */ -static dbus_bool_t -handle_new_client_fd (DBusServer *server, - int client_fd) -{ - DBusConnection *connection; - DBusTransport *transport; - - _dbus_verbose ("Creating new client connection with fd %d\n", client_fd); - - if (!_dbus_set_fd_nonblocking (client_fd, NULL)) - return TRUE; - - transport = _dbus_transport_new_for_fd (client_fd, TRUE, NULL); - if (transport == NULL) - { - close (client_fd); - return FALSE; - } - - if (!_dbus_transport_set_auth_mechanisms (transport, - (const char **) server->auth_mechanisms)) - { - _dbus_transport_unref (transport); - 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); - - if (connection == NULL) - return FALSE; - - /* See if someone wants to handle this new connection, - * self-referencing for paranoia - */ - if (server->new_connection_function) + if (strcmp (method, "unix") == 0) { - 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. */ - dbus_connection_unref (connection); - - return TRUE; -} + 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"); -static dbus_bool_t -unix_handle_watch (DBusWatch *watch, - unsigned int flags, - void *data) -{ - DBusServer *server = data; - DBusServerUnix *unix_server = data; + if (path == NULL && tmpdir == NULL && abstract == NULL) + { + _dbus_set_bad_address(error, "unix", + "path or tmpdir or abstract", + NULL); + return DBUS_SERVER_LISTEN_BAD_ADDRESS; + } - _dbus_assert (watch == unix_server->watch); + if ((path && tmpdir) || + (path && abstract) || + (tmpdir && abstract)) + { + _dbus_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; + } - _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); - - 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"); - else - _dbus_verbose ("Failed to accept a client connection: %s\n", - _dbus_strerror (errno)); + 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 { - _dbus_fd_set_close_on_exec (client_fd); + if (path) + *server_p = _dbus_server_new_for_domain_socket (path, FALSE, error); + else + *server_p = _dbus_server_new_for_domain_socket (abstract, TRUE, error); + } - if (!handle_new_client_fd (server, client_fd)) - _dbus_verbose ("Rejected client connection due to lack of memory\n"); + if (*server_p != NULL) + { + _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 if (strcmp (method, "systemd") == 0) + { + int i, n, *fds; + DBusString address; - if (flags & DBUS_WATCH_ERROR) - _dbus_verbose ("Error on server listening socket\n"); + n = _dbus_listen_systemd_sockets (&fds, error); + if (n < 0) + { + _DBUS_ASSERT_ERROR_IS_SET (error); + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; + } - if (flags & DBUS_WATCH_HANGUP) - _dbus_verbose ("Hangup on server listening socket\n"); + if (!_dbus_string_init (&address)) + goto systemd_oom; - return TRUE; -} - -static void -unix_disconnect (DBusServer *server) -{ - DBusServerUnix *unix_server = (DBusServerUnix*) server; + for (i = 0; i < n; i++) + { + if (i > 0) + { + if (!_dbus_string_append (&address, ";")) + goto systemd_oom; + } + if (!_dbus_append_address_from_socket (fds[i], &address, error)) + goto systemd_err; + } - if (unix_server->watch) - { - _dbus_server_remove_watch (server, - unix_server->watch); - _dbus_watch_unref (unix_server->watch); - unix_server->watch = NULL; - } - - close (unix_server->fd); - unix_server->fd = -1; + *server_p = _dbus_server_new_for_socket (fds, n, &address, NULL); + if (*server_p == NULL) + goto systemd_oom; - if (unix_server->socket_name != NULL) - { - DBusString tmp; - _dbus_string_init_const (&tmp, unix_server->socket_name); - _dbus_delete_file (&tmp, NULL); - } -} + dbus_free (fds); -static DBusServerVTable unix_vtable = { - unix_finalize, - unix_disconnect -}; + return DBUS_SERVER_LISTEN_OK; + systemd_oom: + _DBUS_SET_OOM (error); + systemd_err: + for (i = 0; i < n; i++) + { + _dbus_close_socket (fds[i], NULL); + } + dbus_free (fds); + _dbus_string_free (&address); -/** - * 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; - 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; + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; } - - if (!_dbus_server_init_base (&unix_server->base, - &unix_vtable, address)) +#ifdef DBUS_ENABLE_LAUNCHD + else if (strcmp (method, "launchd") == 0) { - _dbus_watch_unref (watch); - dbus_free (unix_server); - return NULL; - } + const char *launchd_env_var = dbus_address_entry_get_value (entry, "env"); + if (launchd_env_var == NULL) + { + _dbus_set_bad_address (error, "launchd", "env", NULL); + return DBUS_SERVER_LISTEN_DID_NOT_CONNECT; + } + *server_p = _dbus_server_new_for_launchd (launchd_env_var, error); - if (!_dbus_server_add_watch (&unix_server->base, - watch)) + if (*server_p != NULL) + { + _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; + } + } +#endif + else { - _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; - - return (DBusServer*) unix_server; } /** @@ -279,11 +239,11 @@ _dbus_server_new_for_domain_socket (const char *path, DBusError *error) { DBusServer *server; - DBusServerUnix *unix_server; int listen_fd; DBusString address; char *path_copy; - + DBusString path_str; + _DBUS_ASSERT_ERROR_IS_CLEAR (error); if (!_dbus_string_init (&address)) @@ -292,48 +252,55 @@ _dbus_server_new_for_domain_socket (const char *path, return NULL; } + _dbus_string_init_const (&path_str, path); if ((abstract && !_dbus_string_append (&address, "unix:abstract=")) || (!abstract && !_dbus_string_append (&address, "unix:path=")) || - !_dbus_string_append (&address, path)) + !_dbus_address_append_escaped (&address, &path_str)) { dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); goto failed_0; } - path_copy = _dbus_strdup (path); - if (path_copy == NULL) + if (abstract) { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - goto failed_0; + path_copy = NULL; } - + else + { + path_copy = _dbus_strdup (path); + if (path_copy == NULL) + { + dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); + goto failed_0; + } + } + listen_fd = _dbus_listen_unix_socket (path, abstract, error); - _dbus_fd_set_close_on_exec (listen_fd); - + if (listen_fd < 0) { _DBUS_ASSERT_ERROR_IS_SET (error); goto failed_1; } - - server = _dbus_server_new_for_fd (listen_fd, &address); + + server = _dbus_server_new_for_socket (&listen_fd, 1, &address, 0); 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; - + if (path_copy != NULL) + _dbus_server_socket_own_filename(server, path_copy); + _dbus_string_free (&address); - + return server; failed_2: - _dbus_close (listen_fd, NULL); + _dbus_close_socket (listen_fd, NULL); failed_1: dbus_free (path_copy); failed_0: @@ -342,66 +309,4 @@ _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; - - _DBUS_ASSERT_ERROR_IS_CLEAR (error); - - if (!_dbus_string_init (&address)) - { - dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); - return NULL; - } - - if (!_dbus_string_append (&address, "tcp:host=") || - !_dbus_string_append (&address, host) || - !_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); - close (listen_fd); - _dbus_string_free (&address); - return NULL; - } - - _dbus_string_free (&address); - - return server; - - -} - /** @} */ -