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);
73 * @todo unreffing the connection at the end may cause
74 * us to drop the last ref to the connection before
75 * disconnecting it. That is invalid.
77 * @todo doesn't this leak a server refcount if
78 * new_connection_function is NULL?
80 /* Return value is just for memory, not other failures. */
82 handle_new_client_fd_and_unlock (DBusServer *server,
85 DBusConnection *connection;
86 DBusTransport *transport;
87 DBusNewConnectionFunction new_connection_function;
88 void *new_connection_data;
90 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
92 HAVE_LOCK_CHECK (server);
94 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
96 SERVER_UNLOCK (server);
100 transport = _dbus_transport_new_for_socket (client_fd, &server->guid_hex, NULL);
101 if (transport == NULL)
103 _dbus_close_socket (client_fd, NULL);
104 SERVER_UNLOCK (server);
108 if (!_dbus_transport_set_auth_mechanisms (transport,
109 (const char **) server->auth_mechanisms))
111 _dbus_transport_unref (transport);
112 SERVER_UNLOCK (server);
116 /* note that client_fd is now owned by the transport, and will be
117 * closed on transport disconnection/finalization
120 connection = _dbus_connection_new_for_transport (transport);
121 _dbus_transport_unref (transport);
122 transport = NULL; /* now under the connection lock */
124 if (connection == NULL)
126 SERVER_UNLOCK (server);
130 /* See if someone wants to handle this new connection, self-referencing
133 new_connection_function = server->new_connection_function;
134 new_connection_data = server->new_connection_data;
136 _dbus_server_ref_unlocked (server);
137 SERVER_UNLOCK (server);
139 if (new_connection_function)
141 (* new_connection_function) (server, connection,
142 new_connection_data);
143 dbus_server_unref (server);
146 /* If no one grabbed a reference, the connection will die. */
147 dbus_connection_unref (connection);
153 socket_handle_watch (DBusWatch *watch,
157 DBusServer *server = data;
158 DBusServerSocket *socket_server = data;
160 SERVER_LOCK (server);
162 _dbus_assert (watch == socket_server->watch);
164 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
166 if (flags & DBUS_WATCH_READABLE)
171 listen_fd = dbus_watch_get_fd (watch);
173 client_fd = _dbus_accept (listen_fd);
177 /* EINTR handled for us */
179 if (errno == EAGAIN || errno == EWOULDBLOCK)
180 _dbus_verbose ("No client available to accept after all\n");
182 _dbus_verbose ("Failed to accept a client connection: %s\n",
183 _dbus_strerror (errno));
185 SERVER_UNLOCK (server);
189 _dbus_fd_set_close_on_exec (client_fd);
191 if (!handle_new_client_fd_and_unlock (server, client_fd))
192 _dbus_verbose ("Rejected client connection due to lack of memory\n");
196 if (flags & DBUS_WATCH_ERROR)
197 _dbus_verbose ("Error on server listening socket\n");
199 if (flags & DBUS_WATCH_HANGUP)
200 _dbus_verbose ("Hangup on server listening socket\n");
206 socket_disconnect (DBusServer *server)
208 DBusServerSocket *socket_server = (DBusServerSocket*) server;
210 HAVE_LOCK_CHECK (server);
212 if (socket_server->watch)
214 _dbus_server_remove_watch (server,
215 socket_server->watch);
216 _dbus_watch_unref (socket_server->watch);
217 socket_server->watch = NULL;
220 _dbus_close_socket (socket_server->fd, NULL);
221 socket_server->fd = -1;
223 if (socket_server->socket_name != NULL)
226 _dbus_string_init_const (&tmp, socket_server->socket_name);
227 _dbus_delete_file (&tmp, NULL);
230 HAVE_LOCK_CHECK (server);
233 static const DBusServerVTable socket_vtable = {
239 * Creates a new server listening on the given file descriptor. The
240 * file descriptor should be nonblocking (use
241 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
242 * should be listening for connections, that is, listen() should have
243 * been successfully invoked on it. The server will use accept() to
244 * accept new client connections.
246 * @param fd the file descriptor.
247 * @param address the server's address
248 * @returns the new server, or #NULL if no memory.
252 _dbus_server_new_for_socket (int fd,
253 const DBusString *address)
255 DBusServerSocket *socket_server;
259 socket_server = dbus_new0 (DBusServerSocket, 1);
260 if (socket_server == NULL)
263 watch = _dbus_watch_new (fd,
266 socket_handle_watch, socket_server,
270 dbus_free (socket_server);
274 if (!_dbus_server_init_base (&socket_server->base,
275 &socket_vtable, address))
277 _dbus_watch_unref (watch);
278 dbus_free (socket_server);
282 server = (DBusServer*) socket_server;
284 SERVER_LOCK (server);
286 if (!_dbus_server_add_watch (&socket_server->base,
289 SERVER_UNLOCK (server);
290 _dbus_server_finalize_base (&socket_server->base);
291 _dbus_watch_unref (watch);
292 dbus_free (socket_server);
296 socket_server->fd = fd;
297 socket_server->watch = watch;
299 SERVER_UNLOCK (server);
301 return (DBusServer*) socket_server;
305 * Creates a new server listening on the given hostname and port.
306 * If the hostname is NULL, listens on localhost.
308 * @param host the hostname to listen on.
309 * @param port the port to listen on.
310 * @param error location to store reason for failure.
311 * @returns the new server, or #NULL on failure.
314 _dbus_server_new_for_tcp_socket (const char *host,
323 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
325 if (!_dbus_string_init (&address))
327 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
334 _dbus_string_init_const (&host_str, host);
335 if (!_dbus_string_append (&address, "tcp:host=") ||
336 !_dbus_address_append_escaped (&address, &host_str) ||
337 !_dbus_string_append (&address, ",port=") ||
338 !_dbus_string_append_int (&address, port))
340 _dbus_string_free (&address);
341 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
345 listen_fd = _dbus_listen_tcp_socket (host, port, error);
346 _dbus_fd_set_close_on_exec (listen_fd);
350 _dbus_string_free (&address);
354 server = _dbus_server_new_for_socket (listen_fd, &address);
357 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
358 _dbus_close_socket (listen_fd, NULL);
359 _dbus_string_free (&address);
363 _dbus_string_free (&address);
371 * Tries to interpret the address entry for various socket-related
372 * addresses (well, currently only tcp).
374 * Sets error if the result is not OK.
376 * @param entry an address entry
377 * @param server_p a new DBusServer, or #NULL on failure.
378 * @param error location to store rationale for failure on bad address
379 * @returns the outcome
382 DBusServerListenResult
383 _dbus_server_listen_socket (DBusAddressEntry *entry,
384 DBusServer **server_p,
391 method = dbus_address_entry_get_method (entry);
393 if (strcmp (method, "tcp") == 0)
395 const char *host = dbus_address_entry_get_value (entry, "host");
396 const char *port = dbus_address_entry_get_value (entry, "port");
403 _dbus_set_bad_address(error, "tcp", "port", NULL);
404 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
407 _dbus_string_init_const (&str, port);
408 sresult = _dbus_string_parse_int (&str, 0, &lport, NULL);
409 _dbus_string_free (&str);
411 if (sresult == FALSE || lport <= 0 || lport > 65535)
413 _dbus_set_bad_address(error, NULL, NULL,
414 "Port is not an integer between 0 and 65535");
415 return DBUS_SERVER_LISTEN_BAD_ADDRESS;
418 *server_p = _dbus_server_new_for_tcp_socket (host, lport, error);
422 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
423 return DBUS_SERVER_LISTEN_OK;
427 _DBUS_ASSERT_ERROR_IS_SET(error);
428 return DBUS_SERVER_LISTEN_DID_NOT_CONNECT;
433 _DBUS_ASSERT_ERROR_IS_CLEAR(error);
434 return DBUS_SERVER_LISTEN_NOT_HANDLED;
439 * This is a bad hack since it's really unix domain socket
440 * specific. Also, the function weirdly adopts ownership
441 * of the passed-in string.
443 * @param server a socket server
444 * @param filename socket filename to report/delete
448 _dbus_server_socket_own_filename (DBusServer *server,
451 DBusServerSocket *socket_server = (DBusServerSocket*) server;
453 socket_server->socket_name = filename;