25#include "dbus-internals.h"
26#include "dbus-server-socket.h"
27#include "dbus-transport-socket.h"
28#include "dbus-connection-internal.h"
29#include "dbus-memory.h"
30#include "dbus-nonce.h"
31#include "dbus-string.h"
68 for (i = 0 ; i < socket_server->
n_fds ; i++)
69 if (socket_server->
watch[i])
84handle_new_client_fd_and_unlock (
DBusServer *server,
90 void *new_connection_data;
92 _dbus_verbose (
"Creating new client connection with fd %" DBUS_SOCKET_FORMAT
"\n",
93 _dbus_socket_printable (client_fd));
95 HAVE_LOCK_CHECK (server);
99 SERVER_UNLOCK (server);
104 if (transport ==
NULL)
107 SERVER_UNLOCK (server);
115 SERVER_UNLOCK (server);
127 if (connection ==
NULL)
129 SERVER_UNLOCK (server);
140 SERVER_UNLOCK (server);
142 if (new_connection_function)
144 (* new_connection_function) (server, connection,
145 new_connection_data);
164#ifndef DBUS_DISABLE_ASSERT
169 SERVER_LOCK (server);
171#ifndef DBUS_DISABLE_ASSERT
172 for (i = 0 ; i < socket_server->
n_fds ; i++)
174 if (socket_server->
watch[i] == watch)
180 _dbus_verbose (
"Handling client connection, flags 0x%x\n", flags);
188 listen_fd = _dbus_watch_get_socket (watch);
191 client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->
noncefile);
195 saved_errno = _dbus_save_socket_errno ();
197 if (!_dbus_socket_is_valid (client_fd))
202 _dbus_verbose (
"No client available to accept after all\n");
204 _dbus_verbose (
"Failed to accept a client connection: %s\n",
205 _dbus_strerror (saved_errno));
207 SERVER_UNLOCK (server);
211 if (!handle_new_client_fd_and_unlock (server, client_fd))
212 _dbus_verbose (
"Rejected client connection due to lack of memory\n");
217 _dbus_verbose (
"Error on server listening socket\n");
220 _dbus_verbose (
"Hangup on server listening socket\n");
231 HAVE_LOCK_CHECK (server);
233 for (i = 0 ; i < socket_server->
n_fds ; i++)
235 if (socket_server->
watch[i])
238 socket_server->
watch[i]);
244 if (_dbus_socket_is_valid (socket_server->
fds[i]))
256 _dbus_daemon_unpublish_session_bus_address();
258 HAVE_LOCK_CHECK (server);
294 if (socket_server ==
NULL)
300 if (!socket_server->
fds)
304 if (!socket_server->
watch)
307 for (i = 0 ; i < n_fds ; i++)
314 socket_handle_watch, socket_server,
319 socket_server->
n_fds++;
320 socket_server->
fds[i] = fds[i];
321 socket_server->
watch[i] = watch;
325 &socket_vtable, address,
331 SERVER_LOCK (server);
333 for (i = 0 ; i < n_fds ; i++)
336 socket_server->
watch[i]))
343 for (j = 0; j < n_fds; j++)
344 _dbus_socket_invalidate (&socket_server->
fds[j]);
349 for (j = i; j < n_fds; j++)
356 _dbus_server_disconnect_unlocked (server);
357 SERVER_UNLOCK (server);
363 SERVER_UNLOCK (server);
365 _dbus_server_trace_ref (&socket_server->
base, 0, 1,
"new_for_socket");
369 if (socket_server !=
NULL)
373 for (i = 0; i < n_fds; i++)
390 _DBUS_SET_OOM (error);
424 int nlisten_fds = 0, i;
425 DBusString address = _DBUS_STRING_INIT_INVALID;
427 DBusString port_str = _DBUS_STRING_INIT_INVALID;
429 const char *family_used =
NULL;
431 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
453 else if (strcmp (bind,
"*") == 0)
460 if (nlisten_fds <= 0)
462 _DBUS_ASSERT_ERROR_IS_SET(error);
485 if (!_dbus_noncefile_create (&noncefile, error))
508 _dbus_noncefile_delete (&noncefile,
NULL);
510 if (listen_fds !=
NULL)
512 for (i = 0; i < nlisten_fds; i++)
534DBusServerListenResult
545 if (strcmp (method,
"tcp") == 0 || strcmp (method,
"nonce-tcp") == 0)
558 family, error, strcmp (method,
"nonce-tcp") == 0 ?
TRUE :
FALSE);
562 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
563 return DBUS_SERVER_LISTEN_OK;
567 _DBUS_ASSERT_ERROR_IS_SET(error);
568 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
573 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
574 return DBUS_SERVER_LISTEN_NOT_HANDLED;
615 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
641 if (path_copy ==
NULL)
650 if (!_dbus_socket_is_valid (listen_fd))
652 _DBUS_ASSERT_ERROR_IS_SET (error);
662 if (path_copy !=
NULL)
757DBusServerListenResult
768 if (strcmp (method,
"unix") == 0)
775 int mutually_exclusive_modes = 0;
777 mutually_exclusive_modes = (path !=
NULL) + (tmpdir !=
NULL) +
780 if (mutually_exclusive_modes < 1)
783 "path or tmpdir or abstract or runtime or dir",
785 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
788 if (mutually_exclusive_modes > 1)
791 "cannot specify two of \"path\", \"tmpdir\", \"abstract\", \"runtime\" and \"dir\" at the same time");
792 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
799 const char *runtimedir;
801 if (strcmp (runtime,
"yes") != 0)
804 "if given, the only value allowed for \"runtime\" is \"yes\"");
805 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
810 if (runtimedir ==
NULL)
814 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
821 _DBUS_SET_OOM (error);
822 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
829 _DBUS_SET_OOM (error);
830 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
837 _dbus_string_get_const_data (&full_path),
842 else if (tmpdir !=
NULL || dir !=
NULL)
868 if (*server_p !=
NULL)
870 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
871 return DBUS_SERVER_LISTEN_OK;
875 _DBUS_ASSERT_ERROR_IS_SET(error);
876 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
884 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
885 return DBUS_SERVER_LISTEN_NOT_HANDLED;
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...
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.
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
DBusConnection * _dbus_connection_new_for_transport(DBusTransport *transport)
Creates a new connection for the given transport.
void _dbus_connection_close_if_only_one_ref(DBusConnection *connection)
Used internally to handle the semantics of dbus_server_set_new_connection_function().
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
@ DBUS_WATCH_READABLE
As in POLLIN.
@ DBUS_WATCH_HANGUP
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
@ DBUS_WATCH_ERROR
As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle(...
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
dbus_bool_t _dbus_delete_file(const DBusString *filename, DBusError *error)
Deletes the given file.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
char * _dbus_strdup(const char *str)
Duplicates a string.
dbus_bool_t _dbus_generate_random_ascii(DBusString *str, int n_bytes, DBusError *error)
Generates the given number of random bytes, where the bytes are chosen from the alphanumeric ASCII su...
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
#define DBUS_ERROR_NOT_SUPPORTED
Requested operation isn't supported (like ENOSYS on UNIX).
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
dbus_bool_t _dbus_server_add_watch(DBusServer *server, DBusWatch *watch)
Adds a watch for this server, chaining out to application-provided watch handlers.
void _dbus_server_remove_watch(DBusServer *server, DBusWatch *watch)
Removes a watch previously added with _dbus_server_remove_watch().
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.
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
DBUS_PRIVATE_EXPORT void _dbus_server_ref_unlocked(DBusServer *server)
Like dbus_server_ref() but does not acquire the lock (must already be held)
DBusServer * _dbus_server_new_for_dir(const char *dir, dbus_bool_t use_abstract, DBusError *error)
Creates a new Unix domain socket server listening under the given directory.
DBusServer * _dbus_server_new_for_domain_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a new server listening on the given Unix domain socket.
DBusServer * _dbus_server_new_for_tcp_socket(const char *host, const char *bind, const char *port, const char *family, DBusError *error, dbus_bool_t use_nonce)
Creates a new server listening on TCP.
DBusServer * _dbus_server_new_for_socket(DBusSocket *fds, int n_fds, const DBusString *address, DBusNonceFile *noncefile, DBusError *error)
Creates a new server listening on the given file descriptor.
DBusServerListenResult _dbus_server_listen_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for various socket-related addresses (well, currently only tcp a...
DBusServerListenResult _dbus_server_listen_unix_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for UNIX socket addresses.
void _dbus_server_socket_own_filename(DBusServer *server, char *filename)
This is a bad hack since it's really unix domain socket specific.
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
void _dbus_string_init_const(DBusString *str, const char *value)
Initializes a constant string.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(int e)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted)
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
DBusSocket _dbus_listen_unix_socket(const char *path, dbus_bool_t abstract, DBusError *error)
Creates a socket and binds it to the given path, then listens on the socket.
dbus_bool_t _dbus_set_socket_nonblocking(DBusSocket fd, DBusError *error)
Sets a file descriptor to be nonblocking.
int _dbus_listen_tcp_socket(const char *host, const char *port, const char *family, DBusString *retport, const char **retfamily, DBusSocket **fds_p, DBusError *error)
Creates a socket and binds it to the given path, then listens on the socket.
dbus_bool_t _dbus_close_socket(DBusSocket *fd, DBusError *error)
Closes a socket and invalidates it.
dbus_bool_t _dbus_concat_dir_and_file(DBusString *dir, const DBusString *next_component)
Appends the given filename to the given directory.
DBusSocket _dbus_accept(DBusSocket listen_fd)
Accepts a connection on a listening 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.
dbus_bool_t _dbus_transport_set_auth_mechanisms(DBusTransport *transport, const char **mechanisms)
Sets the SASL authentication mechanisms supported by this transport.
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
DBusWatch * _dbus_watch_new(DBusPollable fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it.
Internals of DBusAddressEntry.
Implementation details of DBusConnection.
Object representing an exception.
Implementation details of DBusServerSocket.
int n_fds
Number of active file handles.
DBusSocket * fds
File descriptor or DBUS_SOCKET_INVALID if disconnected.
DBusNonceFile * noncefile
Nonce file used to authenticate clients.
DBusWatch ** watch
File descriptor watch.
DBusServer base
Parent class members.
char * socket_name
Name of domain socket, to unlink if appropriate.
Virtual table to be implemented by all server "subclasses".
Internals of DBusServer object.
dbus_bool_t published_address
flag which indicates that server has published its bus address.
DBusString guid_hex
Hex-encoded version of GUID.
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
void * new_connection_data
Data for new connection callback.
char ** auth_mechanisms
Array of allowed authentication mechanisms.
Object representing a transport such as a socket.
Implementation of DBusWatch.