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 the given hostname and port.
299 * If the hostname is NULL, listens on localhost.
301 * @param host the hostname to listen on.
302 * @param port the port to listen on.
303 * @param error location to store reason for failure.
304 * @returns the new server, or #NULL on failure.
307 _dbus_server_new_for_tcp_socket (const char *host,
316 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
318 if (!_dbus_string_init (&address))
320 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
327 listen_fd = _dbus_listen_tcp_socket (host, &port, error);
328 _dbus_fd_set_close_on_exec (listen_fd);
330 _dbus_string_init_const (&host_str, host);
331 if (!_dbus_string_append (&address, "tcp:host=") ||
332 !_dbus_address_append_escaped (&address, &host_str) ||
333 !_dbus_string_append (&address, ",port=") ||
334 !_dbus_string_append_int (&address, port))
336 _dbus_string_free (&address);
337 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
344 _dbus_string_free (&address);
348 server = _dbus_server_new_for_socket (listen_fd, &address);
351 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
352 _dbus_close_socket (listen_fd, NULL);
353 _dbus_string_free (&address);
357 _dbus_string_free (&address);
365 * Tries to interpret the address entry for various socket-related
366 * addresses (well, currently only tcp).
368 * Sets error if the result is not OK.
370 * @param entry an address entry
371 * @param server_p a new DBusServer, or #NULL on failure.
372 * @param error location to store rationale for failure on bad address
373 * @returns the outcome
376 DBusServerListenResult
377 _dbus_server_listen_socket (DBusAddressEntry *entry,
378 DBusServer **server_p,
385 method = dbus_address_entry_get_method (entry);
387 if (strcmp (method, "tcp") == 0)
389 const char *host = dbus_address_entry_get_value (entry, "host");
390 const char *port = dbus_address_entry_get_value (entry, "port");
397 _dbus_set_bad_address(error, "tcp", "port", NULL);
398 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
401 _dbus_string_init_const (&str, port);
402 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
403 _dbus_string_free (&str);
405 if (sresult == FALSE || lport < 0 || lport > 65535)
407 _dbus_set_bad_address(error, NULL, NULL,
408 "Port is not an integer between 0 and 65535");
409 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
412 *server_p = _dbus_server_new_for_tcp_socket (host, lport, error);
416 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
417 return DBUS_SERVER_LISTEN_OK;
421 _DBUS_ASSERT_ERROR_IS_SET(error);
422 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
427 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
428 return DBUS_SERVER_LISTEN_NOT_HANDLED;
433 * This is a bad hack since it's really unix domain socket
434 * specific. Also, the function weirdly adopts ownership
435 * of the passed-in string.
437 * @param server a socket server
438 * @param filename socket filename to report/delete
442 _dbus_server_socket_own_filename (DBusServer *server,
445 DBusServerSocket *socket_server = (DBusServerSocket*) server;
447 socket_server->socket_name = filename;