1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-server-socket.c Server implementation for sockets
4 * Copyright (C) 2002, 2003, 2004, 2006 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-server-socket.h"
26 #include "dbus-transport-socket.h"
27 #include "dbus-connection-internal.h"
28 #include "dbus-string.h"
31 * @defgroup DBusServerSocket DBusServer implementations for SOCKET
32 * @ingroup DBusInternals
33 * @brief Implementation details of DBusServer on SOCKET
39 * Opaque object representing a Socket server implementation.
41 typedef struct DBusServerSocket DBusServerSocket;
44 * Implementation details of DBusServerSocket. All members
47 struct DBusServerSocket
49 DBusServer base; /**< Parent class members. */
50 int n_fds; /**< Number of active file handles */
51 int *fds; /**< File descriptor or -1 if disconnected. */
52 DBusWatch **watch; /**< File descriptor watch. */
53 char *socket_name; /**< Name of domain socket, to unlink if appropriate */
57 socket_finalize (DBusServer *server)
59 DBusServerSocket *socket_server = (DBusServerSocket*) server;
62 _dbus_server_finalize_base (server);
64 for (i = 0 ; i < socket_server->n_fds ; i++)
65 if (socket_server->watch[i])
67 _dbus_watch_unref (socket_server->watch[i]);
68 socket_server->watch[i] = NULL;
71 dbus_free (socket_server->fds);
72 dbus_free (socket_server->watch);
73 dbus_free (socket_server->socket_name);
77 /* Return value is just for memory, not other failures. */
79 handle_new_client_fd_and_unlock (DBusServer *server,
82 DBusConnection *connection;
83 DBusTransport *transport;
84 DBusNewConnectionFunction new_connection_function;
85 void *new_connection_data;
87 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
89 HAVE_LOCK_CHECK (server);
91 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
93 SERVER_UNLOCK (server);
97 transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, NULL);
98 if (transport == NULL)
100 _dbus_close_socket (client_fd, NULL);
101 SERVER_UNLOCK (server);
105 if (!_dbus_transport_set_auth_mechanisms (transport,
106 (const char **) server->auth_mechanisms))
108 _dbus_transport_unref (transport);
109 SERVER_UNLOCK (server);
113 /* note that client_fd is now owned by the transport, and will be
114 * closed on transport disconnection/finalization
117 connection = _dbus_connection_new_for_transport (transport);
118 _dbus_transport_unref (transport);
119 transport = NULL; /* now under the connection lock */
121 if (connection == NULL)
123 SERVER_UNLOCK (server);
127 /* See if someone wants to handle this new connection, self-referencing
130 new_connection_function = server->new_connection_function;
131 new_connection_data = server->new_connection_data;
133 _dbus_server_ref_unlocked (server);
134 SERVER_UNLOCK (server);
136 if (new_connection_function)
138 (* new_connection_function) (server, connection,
139 new_connection_data);
141 dbus_server_unref (server);
143 /* If no one grabbed a reference, the connection will die. */
144 _dbus_connection_close_if_only_one_ref (connection);
145 dbus_connection_unref (connection);
151 socket_handle_watch (DBusWatch *watch,
155 DBusServer *server = data;
156 #ifndef DBUS_DISABLE_ASSERT
157 DBusServerSocket *socket_server = data;
159 dbus_bool_t found = FALSE;
162 SERVER_LOCK (server);
164 #ifndef DBUS_DISABLE_ASSERT
165 for (i = 0 ; i < socket_server->n_fds ; i++)
167 if (socket_server->watch[i] == watch)
170 _dbus_assert (found);
173 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
175 if (flags & DBUS_WATCH_READABLE)
180 listen_fd = dbus_watch_get_socket (watch);
182 client_fd = _dbus_accept (listen_fd);
186 /* EINTR handled for us */
188 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
189 _dbus_verbose ("No client available to accept after all\n");
191 _dbus_verbose ("Failed to accept a client connection: %s\n",
192 _dbus_strerror_from_errno ());
194 SERVER_UNLOCK (server);
198 _dbus_fd_set_close_on_exec (client_fd);
200 if (!handle_new_client_fd_and_unlock (server, client_fd))
201 _dbus_verbose ("Rejected client connection due to lack of memory\n");
205 if (flags & DBUS_WATCH_ERROR)
206 _dbus_verbose ("Error on server listening socket\n");
208 if (flags & DBUS_WATCH_HANGUP)
209 _dbus_verbose ("Hangup on server listening socket\n");
215 socket_disconnect (DBusServer *server)
217 DBusServerSocket *socket_server = (DBusServerSocket*) server;
220 HAVE_LOCK_CHECK (server);
222 for (i = 0 ; i < socket_server->n_fds ; i++)
224 if (socket_server->watch[i])
226 _dbus_server_remove_watch (server,
227 socket_server->watch[i]);
228 _dbus_watch_unref (socket_server->watch[i]);
229 socket_server->watch[i] = NULL;
232 _dbus_close_socket (socket_server->fds[i], NULL);
233 socket_server->fds[i] = -1;
236 if (socket_server->socket_name != NULL)
239 _dbus_string_init_const (&tmp, socket_server->socket_name);
240 _dbus_delete_file (&tmp, NULL);
243 HAVE_LOCK_CHECK (server);
246 static const DBusServerVTable socket_vtable = {
252 * Creates a new server listening on the given file descriptor. The
253 * file descriptor should be nonblocking (use
254 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
255 * should be listening for connections, that is, listen() should have
256 * been successfully invoked on it. The server will use accept() to
257 * accept new client connections.
259 * @param fds list of file descriptors.
260 * @param n_fds number of file descriptors
261 * @param address the server's address
262 * @returns the new server, or #NULL if no memory.
266 _dbus_server_new_for_socket (int *fds,
268 const DBusString *address)
270 DBusServerSocket *socket_server;
274 socket_server = dbus_new0 (DBusServerSocket, 1);
275 if (socket_server == NULL)
278 socket_server->fds = dbus_new (int, n_fds);
279 if (!socket_server->fds)
282 socket_server->watch = dbus_new0 (DBusWatch *, n_fds);
283 if (!socket_server->watch)
286 for (i = 0 ; i < n_fds ; i++)
290 watch = _dbus_watch_new (fds[i],
293 socket_handle_watch, socket_server,
298 socket_server->n_fds++;
299 socket_server->fds[i] = fds[i];
300 socket_server->watch[i] = watch;
303 if (!_dbus_server_init_base (&socket_server->base,
304 &socket_vtable, address))
307 server = (DBusServer*)socket_server;
309 SERVER_LOCK (server);
311 for (i = 0 ; i < n_fds ; i++)
313 if (!_dbus_server_add_watch (&socket_server->base,
314 socket_server->watch[i]))
317 for (j = 0 ; j < i ; j++)
318 _dbus_server_remove_watch (server,
319 socket_server->watch[j]);
321 SERVER_UNLOCK (server);
322 _dbus_server_finalize_base (&socket_server->base);
327 SERVER_UNLOCK (server);
329 return (DBusServer*) socket_server;
332 for (i = 0 ; i < n_fds ; i++)
334 if (socket_server->watch[i] != NULL)
336 _dbus_watch_unref (socket_server->watch[i]);
337 socket_server->watch[i] = NULL;
340 dbus_free (socket_server->watch);
343 dbus_free (socket_server->fds);
346 dbus_free (socket_server);
351 * Creates a new server listening on TCP.
352 * If host is NULL, it will default to localhost.
353 * If bind is NULL, it will default to the value for the host
354 * parameter, and if that is NULL, then localhost
355 * If bind is a hostname, it will be resolved and will listen
356 * on all returned addresses.
357 * If family is NULL, hostname resolution will try all address
358 * families, otherwise it can be ipv4 or ipv6 to restrict the
359 * addresses considered.
361 * @param host the hostname to report for the listen address
362 * @param bind the hostname to listen on
363 * @param port the port to listen on or 0 to let the OS choose
365 * @param error location to store reason for failure.
366 * @returns the new server, or #NULL on failure.
369 _dbus_server_new_for_tcp_socket (const char *host,
376 int *listen_fds = NULL;
377 int nlisten_fds = 0, i;
382 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
384 if (!_dbus_string_init (&address))
386 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
390 if (!_dbus_string_init (&port_str))
392 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
404 else if (strcmp (bind, "*") == 0)
407 nlisten_fds =_dbus_listen_tcp_socket (bind, port, family,
410 if (nlisten_fds <= 0)
412 _DBUS_ASSERT_ERROR_IS_SET(error);
416 for (i = 0 ; i < nlisten_fds ; i++)
417 _dbus_fd_set_close_on_exec (listen_fds[i]);
419 _dbus_string_init_const (&host_str, host);
420 if (!_dbus_string_append (&address, "tcp:host=") ||
421 !_dbus_address_append_escaped (&address, &host_str) ||
422 !_dbus_string_append (&address, ",port=") ||
423 !_dbus_string_append (&address, _dbus_string_get_const_data(&port_str)))
425 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
429 (!_dbus_string_append (&address, ",family=") ||
430 !_dbus_string_append (&address, family)))
432 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
436 server = _dbus_server_new_for_socket (listen_fds, nlisten_fds, &address);
439 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
443 _dbus_string_free (&port_str);
444 _dbus_string_free (&address);
445 dbus_free(listen_fds);
450 for (i = 0 ; i < nlisten_fds ; i++)
451 _dbus_close_socket (listen_fds[i], NULL);
452 dbus_free(listen_fds);
455 _dbus_string_free (&port_str);
458 _dbus_string_free (&address);
464 * Tries to interpret the address entry for various socket-related
465 * addresses (well, currently only tcp).
467 * Sets error if the result is not OK.
469 * @param entry an address entry
470 * @param server_p a new DBusServer, or #NULL on failure.
471 * @param error location to store rationale for failure on bad address
472 * @returns the outcome
475 DBusServerListenResult
476 _dbus_server_listen_socket (DBusAddressEntry *entry,
477 DBusServer **server_p,
484 method = dbus_address_entry_get_method (entry);
486 if (strcmp (method, "tcp") == 0)
493 host = dbus_address_entry_get_value (entry, "host");
494 bind = dbus_address_entry_get_value (entry, "bind");
495 port = dbus_address_entry_get_value (entry, "port");
496 family = dbus_address_entry_get_value (entry, "family");
498 *server_p = _dbus_server_new_for_tcp_socket (host, bind, port,
503 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
504 return DBUS_SERVER_LISTEN_OK;
508 _DBUS_ASSERT_ERROR_IS_SET(error);
509 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
514 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
515 return DBUS_SERVER_LISTEN_NOT_HANDLED;
520 * This is a bad hack since it's really unix domain socket
521 * specific. Also, the function weirdly adopts ownership
522 * of the passed-in string.
524 * @param server a socket server
525 * @param filename socket filename to report/delete
529 _dbus_server_socket_own_filename (DBusServer *server,
532 DBusServerSocket *socket_server = (DBusServerSocket*) server;
534 socket_server->socket_name = filename;