27#include "dbus-internals.h"
28#include "dbus-server-socket.h"
29#include "dbus-transport-socket.h"
30#include "dbus-connection-internal.h"
31#include "dbus-memory.h"
32#include "dbus-nonce.h"
33#include "dbus-string.h"
70 for (i = 0 ; i < socket_server->
n_fds ; i++)
71 if (socket_server->
watch[i])
86handle_new_client_fd_and_unlock (
DBusServer *server,
92 void *new_connection_data;
94 _dbus_verbose (
"Creating new client connection with fd %" DBUS_SOCKET_FORMAT
"\n",
95 _dbus_socket_printable (client_fd));
97 HAVE_LOCK_CHECK (server);
101 SERVER_UNLOCK (server);
106 if (transport ==
NULL)
109 SERVER_UNLOCK (server);
117 SERVER_UNLOCK (server);
129 if (connection ==
NULL)
131 SERVER_UNLOCK (server);
142 SERVER_UNLOCK (server);
144 if (new_connection_function)
146 (* new_connection_function) (server, connection,
147 new_connection_data);
166#ifndef DBUS_DISABLE_ASSERT
168 dbus_bool_t found =
FALSE;
171 SERVER_LOCK (server);
173#ifndef DBUS_DISABLE_ASSERT
174 for (i = 0 ; i < socket_server->
n_fds ; i++)
176 if (socket_server->
watch[i] == watch)
185 _dbus_verbose (
"Handling client connection, flags 0x%x\n", flags);
193 listen_fd = _dbus_watch_get_socket (watch);
196 client_fd = _dbus_accept_with_noncefile (listen_fd, socket_server->
noncefile);
200 saved_errno = _dbus_save_socket_errno ();
202 if (!_dbus_socket_is_valid (client_fd))
207 _dbus_verbose (
"No client available to accept after all\n");
209 _dbus_verbose (
"Failed to accept a client connection: %s\n",
210 _dbus_strerror (saved_errno));
212 SERVER_UNLOCK (server);
216 if (!handle_new_client_fd_and_unlock (server, client_fd))
217 _dbus_verbose (
"Rejected client connection due to lack of memory\n");
222 _dbus_verbose (
"Error on server listening socket\n");
225 _dbus_verbose (
"Hangup on server listening socket\n");
236 HAVE_LOCK_CHECK (server);
238 for (i = 0 ; i < socket_server->
n_fds ; i++)
240 if (socket_server->
watch[i])
243 socket_server->
watch[i]);
249 if (_dbus_socket_is_valid (socket_server->
fds[i]))
261 _dbus_daemon_unpublish_session_bus_address();
263 HAVE_LOCK_CHECK (server);
299 if (socket_server ==
NULL)
305 if (!socket_server->
fds)
309 if (!socket_server->
watch)
312 for (i = 0 ; i < n_fds ; i++)
319 socket_handle_watch, socket_server,
324 socket_server->
n_fds++;
325 socket_server->
fds[i] = fds[i];
326 socket_server->
watch[i] = watch;
330 &socket_vtable, address,
336 SERVER_LOCK (server);
338 for (i = 0 ; i < n_fds ; i++)
341 socket_server->
watch[i]))
348 for (j = 0; j < n_fds; j++)
349 _dbus_socket_invalidate (&socket_server->
fds[j]);
354 for (j = i; j < n_fds; j++)
361 _dbus_server_disconnect_unlocked (server);
362 SERVER_UNLOCK (server);
368 SERVER_UNLOCK (server);
370 _dbus_server_trace_ref (&socket_server->
base, 0, 1,
"new_for_socket");
374 if (socket_server !=
NULL)
378 for (i = 0; i < n_fds; i++)
395 _DBUS_SET_OOM (error);
425 dbus_bool_t use_nonce)
429 int nlisten_fds = 0, i;
430 DBusString address = _DBUS_STRING_INIT_INVALID;
432 DBusString port_str = _DBUS_STRING_INIT_INVALID;
434 const char *family_used =
NULL;
436 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
458 else if (strcmp (bind,
"*") == 0)
465 if (nlisten_fds <= 0)
467 _DBUS_ASSERT_ERROR_IS_SET(error);
490 if (!_dbus_noncefile_create (&noncefile, error))
513 _dbus_noncefile_delete (&noncefile,
NULL);
515 if (listen_fds !=
NULL)
517 for (i = 0; i < nlisten_fds; i++)
539DBusServerListenResult
550 if (strcmp (method,
"tcp") == 0 || strcmp (method,
"nonce-tcp") == 0)
563 family, error, strcmp (method,
"nonce-tcp") == 0 ?
TRUE :
FALSE);
567 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
568 return DBUS_SERVER_LISTEN_OK;
572 _DBUS_ASSERT_ERROR_IS_SET(error);
573 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
578 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
579 return DBUS_SERVER_LISTEN_NOT_HANDLED;
611 dbus_bool_t abstract,
620 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
646 if (path_copy ==
NULL)
655 if (!_dbus_socket_is_valid (listen_fd))
657 _DBUS_ASSERT_ERROR_IS_SET (error);
667 if (path_copy !=
NULL)
693_dbus_server_new_for_dir (
const char *dir,
760DBusServerListenResult
771 if (strcmp (method,
"unix") == 0)
778 int mutually_exclusive_modes = 0;
780 mutually_exclusive_modes = (path !=
NULL) + (tmpdir !=
NULL) +
783 if (mutually_exclusive_modes < 1)
786 "path or tmpdir or abstract or runtime or dir",
788 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
791 if (mutually_exclusive_modes > 1)
794 "cannot specify two of \"path\", \"tmpdir\", \"abstract\", \"runtime\" and \"dir\" at the same time");
795 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
802 const char *runtimedir;
804 if (strcmp (runtime,
"yes") != 0)
807 "if given, the only value allowed for \"runtime\" is \"yes\"");
808 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
813 if (runtimedir ==
NULL)
817 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
824 _DBUS_SET_OOM (error);
825 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
832 _DBUS_SET_OOM (error);
833 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
840 _dbus_string_get_const_data (&full_path),
845 else if (tmpdir !=
NULL || dir !=
NULL)
852 *server_p = _dbus_server_new_for_dir (dir, error);
862 if (*server_p !=
NULL)
864 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
865 return DBUS_SERVER_LISTEN_OK;
869 _DBUS_ASSERT_ERROR_IS_SET(error);
870 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
878 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
879 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_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_IN...
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.
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.