1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server-unix.c Server implementation for Unix network protocols.
4 * Copyright (C) 2002, 2003, 2004 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-unix.h"
26 #include "dbus-transport-unix.h"
27 #include "dbus-connection-internal.h"
28 #include "dbus-string.h"
29 #include <sys/types.h>
33 * @defgroup DBusServerUnix DBusServer implementations for UNIX
34 * @ingroup DBusInternals
35 * @brief Implementation details of DBusServer on UNIX
41 * Opaque object representing a Unix server implementation.
43 typedef struct DBusServerUnix DBusServerUnix;
46 * Implementation details of DBusServerUnix. All members
51 DBusServer base; /**< Parent class members. */
52 int fd; /**< File descriptor or -1 if disconnected. */
53 DBusWatch *watch; /**< File descriptor watch. */
54 char *socket_name; /**< Name of domain socket, to unlink if appropriate */
58 unix_finalize (DBusServer *server)
60 DBusServerUnix *unix_server = (DBusServerUnix*) server;
62 _dbus_server_finalize_base (server);
64 if (unix_server->watch)
66 _dbus_watch_unref (unix_server->watch);
67 unix_server->watch = NULL;
70 dbus_free (unix_server->socket_name);
75 * @todo unreffing the connection at the end may cause
76 * us to drop the last ref to the connection before
77 * disconnecting it. That is invalid.
79 * @todo doesn't this leak a server refcount if
80 * new_connection_function is NULL?
82 /* Return value is just for memory, not other failures. */
84 handle_new_client_fd_and_unlock (DBusServer *server,
87 DBusConnection *connection;
88 DBusTransport *transport;
89 DBusNewConnectionFunction new_connection_function;
90 void *new_connection_data;
92 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
94 HAVE_LOCK_CHECK (server);
96 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
98 SERVER_UNLOCK (server);
102 transport = _dbus_transport_new_for_fd (client_fd, TRUE, NULL);
103 if (transport == NULL)
106 SERVER_UNLOCK (server);
110 if (!_dbus_transport_set_auth_mechanisms (transport,
111 (const char **) server->auth_mechanisms))
113 _dbus_transport_unref (transport);
114 SERVER_UNLOCK (server);
118 /* note that client_fd is now owned by the transport, and will be
119 * closed on transport disconnection/finalization
122 connection = _dbus_connection_new_for_transport (transport);
123 _dbus_transport_unref (transport);
124 transport = NULL; /* now under the connection lock */
126 if (connection == NULL)
128 SERVER_UNLOCK (server);
132 /* See if someone wants to handle this new connection, self-referencing
135 new_connection_function = server->new_connection_function;
136 new_connection_data = server->new_connection_data;
138 _dbus_server_ref_unlocked (server);
139 SERVER_UNLOCK (server);
141 if (new_connection_function)
143 (* new_connection_function) (server, connection,
144 new_connection_data);
145 dbus_server_unref (server);
148 /* If no one grabbed a reference, the connection will die. */
149 dbus_connection_unref (connection);
155 unix_handle_watch (DBusWatch *watch,
159 DBusServer *server = data;
160 DBusServerUnix *unix_server = data;
162 SERVER_LOCK (server);
164 _dbus_assert (watch == unix_server->watch);
166 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
168 if (flags & DBUS_WATCH_READABLE)
173 listen_fd = dbus_watch_get_fd (watch);
175 client_fd = _dbus_accept (listen_fd);
179 /* EINTR handled for us */
181 if (errno == EAGAIN || errno == EWOULDBLOCK)
182 _dbus_verbose ("No client available to accept after all\n");
184 _dbus_verbose ("Failed to accept a client connection: %s\n",
185 _dbus_strerror (errno));
187 SERVER_UNLOCK (server);
191 _dbus_fd_set_close_on_exec (client_fd);
193 if (!handle_new_client_fd_and_unlock (server, client_fd))
194 _dbus_verbose ("Rejected client connection due to lack of memory\n");
198 if (flags & DBUS_WATCH_ERROR)
199 _dbus_verbose ("Error on server listening socket\n");
201 if (flags & DBUS_WATCH_HANGUP)
202 _dbus_verbose ("Hangup on server listening socket\n");
208 unix_disconnect (DBusServer *server)
210 DBusServerUnix *unix_server = (DBusServerUnix*) server;
212 HAVE_LOCK_CHECK (server);
214 if (unix_server->watch)
216 _dbus_server_remove_watch (server,
218 _dbus_watch_unref (unix_server->watch);
219 unix_server->watch = NULL;
222 close (unix_server->fd);
223 unix_server->fd = -1;
225 if (unix_server->socket_name != NULL)
228 _dbus_string_init_const (&tmp, unix_server->socket_name);
229 _dbus_delete_file (&tmp, NULL);
232 HAVE_LOCK_CHECK (server);
235 static DBusServerVTable unix_vtable = {
241 * Creates a new server listening on the given file descriptor. The
242 * file descriptor should be nonblocking (use
243 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
244 * should be listening for connections, that is, listen() should have
245 * been successfully invoked on it. The server will use accept() to
246 * accept new client connections.
248 * @param fd the file descriptor.
249 * @param address the server's address
250 * @returns the new server, or #NULL if no memory.
254 _dbus_server_new_for_fd (int fd,
255 const DBusString *address)
257 DBusServerUnix *unix_server;
261 unix_server = dbus_new0 (DBusServerUnix, 1);
262 if (unix_server == NULL)
265 watch = _dbus_watch_new (fd,
268 unix_handle_watch, unix_server,
272 dbus_free (unix_server);
276 if (!_dbus_server_init_base (&unix_server->base,
277 &unix_vtable, address))
279 _dbus_watch_unref (watch);
280 dbus_free (unix_server);
284 server = (DBusServer*) unix_server;
286 SERVER_LOCK (server);
288 if (!_dbus_server_add_watch (&unix_server->base,
291 SERVER_UNLOCK (server);
292 _dbus_server_finalize_base (&unix_server->base);
293 _dbus_watch_unref (watch);
294 dbus_free (unix_server);
298 unix_server->fd = fd;
299 unix_server->watch = watch;
301 SERVER_UNLOCK (server);
303 return (DBusServer*) unix_server;
307 * Creates a new server listening on the given Unix domain socket.
309 * @param path the path for the domain socket.
310 * @param abstract #TRUE to use abstract socket namespace
311 * @param error location to store reason for failure.
312 * @returns the new server, or #NULL on failure.
315 _dbus_server_new_for_domain_socket (const char *path,
316 dbus_bool_t abstract,
320 DBusServerUnix *unix_server;
326 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
328 if (!_dbus_string_init (&address))
330 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
334 _dbus_string_init_const (&path_str, path);
336 !_dbus_string_append (&address, "unix:abstract=")) ||
338 !_dbus_string_append (&address, "unix:path=")) ||
339 !_dbus_address_append_escaped (&address, &path_str))
341 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
345 path_copy = _dbus_strdup (path);
346 if (path_copy == NULL)
348 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
352 listen_fd = _dbus_listen_unix_socket (path, abstract, error);
353 _dbus_fd_set_close_on_exec (listen_fd);
357 _DBUS_ASSERT_ERROR_IS_SET (error);
361 server = _dbus_server_new_for_fd (listen_fd, &address);
364 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
368 unix_server = (DBusServerUnix*) server;
369 unix_server->socket_name = path_copy;
371 _dbus_string_free (&address);
376 _dbus_close (listen_fd, NULL);
378 dbus_free (path_copy);
380 _dbus_string_free (&address);
386 * Creates a new server listening on the given hostname and port.
387 * If the hostname is NULL, listens on localhost.
389 * @param host the hostname to listen on.
390 * @param port the port to listen on.
391 * @param error location to store reason for failure.
392 * @returns the new server, or #NULL on failure.
395 _dbus_server_new_for_tcp_socket (const char *host,
404 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
406 if (!_dbus_string_init (&address))
408 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
415 _dbus_string_init_const (&host_str, host);
416 if (!_dbus_string_append (&address, "tcp:host=") ||
417 !_dbus_address_append_escaped (&address, &host_str) ||
418 !_dbus_string_append (&address, ",port=") ||
419 !_dbus_string_append_int (&address, port))
421 _dbus_string_free (&address);
422 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
426 listen_fd = _dbus_listen_tcp_socket (host, port, error);
427 _dbus_fd_set_close_on_exec (listen_fd);
431 _dbus_string_free (&address);
435 server = _dbus_server_new_for_fd (listen_fd, &address);
438 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
440 _dbus_string_free (&address);
444 _dbus_string_free (&address);