1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server-unix.c Server implementation for Unix network protocols.
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.0
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_free (unix_server->socket_name);
64 _dbus_server_finalize_base (server);
70 * @todo unreffing the connection at the end may cause
71 * us to drop the last ref to the connection before
72 * disconnecting it. That is invalid.
74 /* Return value is just for memory, not other failures. */
76 handle_new_client_fd (DBusServer *server,
79 DBusConnection *connection;
80 DBusTransport *transport;
82 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
84 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
87 transport = _dbus_transport_new_for_fd (client_fd, TRUE, NULL);
88 if (transport == NULL)
94 if (!_dbus_transport_set_auth_mechanisms (transport,
95 (const char **) server->auth_mechanisms))
97 _dbus_transport_unref (transport);
101 /* note that client_fd is now owned by the transport, and will be
102 * closed on transport disconnection/finalization
105 connection = _dbus_connection_new_for_transport (transport);
106 _dbus_transport_unref (transport);
108 if (connection == NULL)
111 /* See if someone wants to handle this new connection,
112 * self-referencing for paranoia
114 if (server->new_connection_function)
116 dbus_server_ref (server);
118 (* server->new_connection_function) (server, connection,
119 server->new_connection_data);
120 dbus_server_unref (server);
123 /* If no one grabbed a reference, the connection will die. */
124 dbus_connection_unref (connection);
130 unix_handle_watch (DBusWatch *watch,
134 DBusServer *server = data;
135 DBusServerUnix *unix_server = data;
137 _dbus_assert (watch == unix_server->watch);
139 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
141 if (flags & DBUS_WATCH_READABLE)
146 listen_fd = dbus_watch_get_fd (watch);
148 client_fd = _dbus_accept (listen_fd);
152 /* EINTR handled for us */
154 if (errno == EAGAIN || errno == EWOULDBLOCK)
155 _dbus_verbose ("No client available to accept after all\n");
157 _dbus_verbose ("Failed to accept a client connection: %s\n",
158 _dbus_strerror (errno));
162 _dbus_fd_set_close_on_exec (client_fd);
164 if (!handle_new_client_fd (server, client_fd))
165 _dbus_verbose ("Rejected client connection due to lack of memory\n");
169 if (flags & DBUS_WATCH_ERROR)
170 _dbus_verbose ("Error on server listening socket\n");
172 if (flags & DBUS_WATCH_HANGUP)
173 _dbus_verbose ("Hangup on server listening socket\n");
179 unix_disconnect (DBusServer *server)
181 DBusServerUnix *unix_server = (DBusServerUnix*) server;
183 if (unix_server->watch)
185 _dbus_server_remove_watch (server,
187 _dbus_watch_unref (unix_server->watch);
188 unix_server->watch = NULL;
191 close (unix_server->fd);
192 unix_server->fd = -1;
194 if (unix_server->socket_name != NULL)
197 _dbus_string_init_const (&tmp, unix_server->socket_name);
198 _dbus_delete_file (&tmp, NULL);
202 static DBusServerVTable unix_vtable = {
208 * Creates a new server listening on the given file descriptor. The
209 * file descriptor should be nonblocking (use
210 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
211 * should be listening for connections, that is, listen() should have
212 * been successfully invoked on it. The server will use accept() to
213 * accept new client connections.
215 * @param fd the file descriptor.
216 * @param address the server's address
217 * @returns the new server, or #NULL if no memory.
221 _dbus_server_new_for_fd (int fd,
222 const DBusString *address)
224 DBusServerUnix *unix_server;
227 unix_server = dbus_new0 (DBusServerUnix, 1);
228 if (unix_server == NULL)
231 watch = _dbus_watch_new (fd,
234 unix_handle_watch, unix_server,
238 dbus_free (unix_server);
242 if (!_dbus_server_init_base (&unix_server->base,
243 &unix_vtable, address))
245 _dbus_watch_unref (watch);
246 dbus_free (unix_server);
250 if (!_dbus_server_add_watch (&unix_server->base,
253 _dbus_server_finalize_base (&unix_server->base);
254 _dbus_watch_unref (watch);
255 dbus_free (unix_server);
259 unix_server->fd = fd;
260 unix_server->watch = watch;
262 return (DBusServer*) unix_server;
266 * Creates a new server listening on the given Unix domain socket.
268 * @param path the path for the domain socket.
269 * @param abstract #TRUE to use abstract socket namespace
270 * @param error location to store reason for failure.
271 * @returns the new server, or #NULL on failure.
274 _dbus_server_new_for_domain_socket (const char *path,
275 dbus_bool_t abstract,
279 DBusServerUnix *unix_server;
284 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
286 if (!_dbus_string_init (&address))
288 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
293 !_dbus_string_append (&address, "unix:abstract=")) ||
295 !_dbus_string_append (&address, "unix:path=")) ||
296 !_dbus_string_append (&address, path))
298 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
302 path_copy = _dbus_strdup (path);
303 if (path_copy == NULL)
305 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
309 listen_fd = _dbus_listen_unix_socket (path, abstract, error);
310 _dbus_fd_set_close_on_exec (listen_fd);
314 _DBUS_ASSERT_ERROR_IS_SET (error);
318 server = _dbus_server_new_for_fd (listen_fd, &address);
321 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
325 unix_server = (DBusServerUnix*) server;
326 unix_server->socket_name = path_copy;
328 _dbus_string_free (&address);
333 _dbus_close (listen_fd, NULL);
335 dbus_free (path_copy);
337 _dbus_string_free (&address);
343 * Creates a new server listening on the given hostname and port.
344 * If the hostname is NULL, listens on localhost.
346 * @param host the hostname to listen on.
347 * @param port the port to listen on.
348 * @param error location to store reason for failure.
349 * @returns the new server, or #NULL on failure.
352 _dbus_server_new_for_tcp_socket (const char *host,
360 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
362 if (!_dbus_string_init (&address))
364 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
371 if (!_dbus_string_append (&address, "tcp:host=") ||
372 !_dbus_string_append (&address, host) ||
373 !_dbus_string_append (&address, ",port=") ||
374 !_dbus_string_append_int (&address, port))
376 _dbus_string_free (&address);
377 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
381 listen_fd = _dbus_listen_tcp_socket (host, port, error);
382 _dbus_fd_set_close_on_exec (listen_fd);
386 _dbus_string_free (&address);
390 server = _dbus_server_new_for_fd (listen_fd, &address);
393 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
395 _dbus_string_free (&address);
399 _dbus_string_free (&address);