1 /* -*- mode: C; c-file-style: "gnu" -*- */
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 fd; /**< File descriptor or -1 if disconnected. */
51 DBusWatch *watch; /**< File descriptor watch. */
52 char *socket_name; /**< Name of domain socket, to unlink if appropriate */
56 socket_finalize (DBusServer *server)
58 DBusServerSocket *socket_server = (DBusServerSocket*) server;
60 _dbus_server_finalize_base (server);
62 if (socket_server->watch)
64 _dbus_watch_unref (socket_server->watch);
65 socket_server->watch = NULL;
68 dbus_free (socket_server->socket_name);
72 /* Return value is just for memory, not other failures. */
74 handle_new_client_fd_and_unlock (DBusServer *server,
77 DBusConnection *connection;
78 DBusTransport *transport;
79 DBusNewConnectionFunction new_connection_function;
80 void *new_connection_data;
82 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
84 HAVE_LOCK_CHECK (server);
86 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
88 SERVER_UNLOCK (server);
92 transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, NULL);
93 if (transport == NULL)
95 _dbus_close_socket (client_fd, NULL);
96 SERVER_UNLOCK (server);
100 if (!_dbus_transport_set_auth_mechanisms (transport,
101 (const char **) server->auth_mechanisms))
103 _dbus_transport_unref (transport);
104 SERVER_UNLOCK (server);
108 /* note that client_fd is now owned by the transport, and will be
109 * closed on transport disconnection/finalization
112 connection = _dbus_connection_new_for_transport (transport);
113 _dbus_transport_unref (transport);
114 transport = NULL; /* now under the connection lock */
116 if (connection == NULL)
118 SERVER_UNLOCK (server);
122 /* See if someone wants to handle this new connection, self-referencing
125 new_connection_function = server->new_connection_function;
126 new_connection_data = server->new_connection_data;
128 _dbus_server_ref_unlocked (server);
129 SERVER_UNLOCK (server);
131 if (new_connection_function)
133 (* new_connection_function) (server, connection,
134 new_connection_data);
136 dbus_server_unref (server);
138 /* If no one grabbed a reference, the connection will die. */
139 _dbus_connection_close_if_only_one_ref (connection);
140 dbus_connection_unref (connection);
146 socket_handle_watch (DBusWatch *watch,
150 DBusServer *server = data;
151 DBusServerSocket *socket_server = data;
153 SERVER_LOCK (server);
155 _dbus_assert (watch == socket_server->watch);
157 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
159 if (flags & DBUS_WATCH_READABLE)
164 listen_fd = dbus_watch_get_fd (watch);
166 client_fd = _dbus_accept (listen_fd);
170 /* EINTR handled for us */
172 if (errno == EAGAIN || errno == EWOULDBLOCK)
173 _dbus_verbose ("No client available to accept after all\n");
175 _dbus_verbose ("Failed to accept a client connection: %s\n",
176 _dbus_strerror (errno));
178 SERVER_UNLOCK (server);
182 _dbus_fd_set_close_on_exec (client_fd);
184 if (!handle_new_client_fd_and_unlock (server, client_fd))
185 _dbus_verbose ("Rejected client connection due to lack of memory\n");
189 if (flags & DBUS_WATCH_ERROR)
190 _dbus_verbose ("Error on server listening socket\n");
192 if (flags & DBUS_WATCH_HANGUP)
193 _dbus_verbose ("Hangup on server listening socket\n");
199 socket_disconnect (DBusServer *server)
201 DBusServerSocket *socket_server = (DBusServerSocket*) server;
203 HAVE_LOCK_CHECK (server);
205 if (socket_server->watch)
207 _dbus_server_remove_watch (server,
208 socket_server->watch);
209 _dbus_watch_unref (socket_server->watch);
210 socket_server->watch = NULL;
213 _dbus_close_socket (socket_server->fd, NULL);
214 socket_server->fd = -1;
216 if (socket_server->socket_name != NULL)
219 _dbus_string_init_const (&tmp, socket_server->socket_name);
220 _dbus_delete_file (&tmp, NULL);
223 HAVE_LOCK_CHECK (server);
226 static const DBusServerVTable socket_vtable = {
232 * Creates a new server listening on the given file descriptor. The
233 * file descriptor should be nonblocking (use
234 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
235 * should be listening for connections, that is, listen() should have
236 * been successfully invoked on it. The server will use accept() to
237 * accept new client connections.
239 * @param fd the file descriptor.
240 * @param address the server's address
241 * @returns the new server, or #NULL if no memory.
245 _dbus_server_new_for_socket (int fd,
246 const DBusString *address)
248 DBusServerSocket *socket_server;
252 socket_server = dbus_new0 (DBusServerSocket, 1);
253 if (socket_server == NULL)
256 watch = _dbus_watch_new (fd,
259 socket_handle_watch, socket_server,
263 dbus_free (socket_server);
267 if (!_dbus_server_init_base (&socket_server->base,
268 &socket_vtable, address))
270 _dbus_watch_unref (watch);
271 dbus_free (socket_server);
275 server = (DBusServer*) socket_server;
277 SERVER_LOCK (server);
279 if (!_dbus_server_add_watch (&socket_server->base,
282 SERVER_UNLOCK (server);
283 _dbus_server_finalize_base (&socket_server->base);
284 _dbus_watch_unref (watch);
285 dbus_free (socket_server);
289 socket_server->fd = fd;
290 socket_server->watch = watch;
292 SERVER_UNLOCK (server);
294 return (DBusServer*) socket_server;
298 * Creates a new server listening on TCP.
299 * If inaddr_any is TRUE, listens on all local interfaces.
300 * Otherwise, it resolves the hostname and listens only on
301 * the resolved address of the hostname. The hostname is used
302 * even if inaddr_any is TRUE, as the hostname to report when
303 * dbus_server_get_address() is called. If the hostname is #NULL,
306 * @param host the hostname to listen on.
307 * @param port the port to listen on or 0 to let the OS choose
308 * @param inaddr_any #TRUE to listen on all local interfaces
309 * @param error location to store reason for failure.
310 * @returns the new server, or #NULL on failure.
313 _dbus_server_new_for_tcp_socket (const char *host,
315 dbus_bool_t inaddr_any,
323 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
325 if (!_dbus_string_init (&address))
327 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
334 listen_fd = _dbus_listen_tcp_socket (host, &port, inaddr_any, error);
335 _dbus_fd_set_close_on_exec (listen_fd);
337 _dbus_string_init_const (&host_str, host);
338 if (!_dbus_string_append (&address, "tcp:host=") ||
339 !_dbus_address_append_escaped (&address, &host_str) ||
340 !_dbus_string_append (&address, ",port=") ||
341 !_dbus_string_append_int (&address, port))
343 _dbus_string_free (&address);
344 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
351 _dbus_string_free (&address);
355 server = _dbus_server_new_for_socket (listen_fd, &address);
358 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
359 _dbus_close_socket (listen_fd, NULL);
360 _dbus_string_free (&address);
364 _dbus_string_free (&address);
372 * Tries to interpret the address entry for various socket-related
373 * addresses (well, currently only tcp).
375 * Sets error if the result is not OK.
377 * @param entry an address entry
378 * @param server_p a new DBusServer, or #NULL on failure.
379 * @param error location to store rationale for failure on bad address
380 * @returns the outcome
383 DBusServerListenResult
384 _dbus_server_listen_socket (DBusAddressEntry *entry,
385 DBusServer **server_p,
392 method = dbus_address_entry_get_method (entry);
394 if (strcmp (method, "tcp") == 0)
398 const char *all_interfaces;
399 dbus_bool_t inaddr_any;
402 host = dbus_address_entry_get_value (entry, "host");
403 port = dbus_address_entry_get_value (entry, "port");
404 all_interfaces = dbus_address_entry_get_value (entry, "all_interfaces");
407 if (all_interfaces != NULL)
409 if (strcmp (all_interfaces, "true") == 0)
413 else if (strcmp (all_interfaces, "false") == 0)
419 _dbus_set_bad_address(error, NULL, NULL,
420 "all_interfaces flag in tcp: address should be 'true' or 'false'");
421 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
434 _dbus_string_init_const (&str, port);
435 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
436 _dbus_string_free (&str);
438 if (sresult == FALSE || lport < 0 || lport > 65535)
440 _dbus_set_bad_address(error, NULL, NULL,
441 "Port is not an integer between 0 and 65535");
442 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
446 *server_p = _dbus_server_new_for_tcp_socket (host, lport, inaddr_any, error);
450 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
451 return DBUS_SERVER_LISTEN_OK;
455 _DBUS_ASSERT_ERROR_IS_SET(error);
456 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
461 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
462 return DBUS_SERVER_LISTEN_NOT_HANDLED;
467 * This is a bad hack since it's really unix domain socket
468 * specific. Also, the function weirdly adopts ownership
469 * of the passed-in string.
471 * @param server a socket server
472 * @param filename socket filename to report/delete
476 _dbus_server_socket_own_filename (DBusServer *server,
479 DBusServerSocket *socket_server = (DBusServerSocket*) server;
481 socket_server->socket_name = filename;