1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server-unix.c Server implementation for Unix network protocols.
4 * Copyright (C) 2002 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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 <sys/types.h>
32 * @defgroup DBusServerUnix DBusServer implementations for UNIX
33 * @ingroup DBusInternals
34 * @brief Implementation details of DBusServer on UNIX
40 * Opaque object representing a Unix server implementation.
42 typedef struct DBusServerUnix DBusServerUnix;
45 * Implementation details of DBusServerUnix. All members
50 DBusServer base; /**< Parent class members. */
51 int fd; /**< File descriptor or -1 if disconnected. */
52 DBusWatch *watch; /**< File descriptor watch. */
53 char *socket_name; /**< Name of domain socket, to unlink if appropriate */
57 unix_finalize (DBusServer *server)
59 DBusServerUnix *unix_server = (DBusServerUnix*) server;
61 if (unix_server->watch)
62 _dbus_watch_unref (unix_server->watch);
64 dbus_free (unix_server->socket_name);
66 _dbus_server_finalize_base (server);
72 * @todo unreffing the connection at the end may cause
73 * us to drop the last ref to the connection before
74 * disconnecting it. That is invalid.
76 /* Return value is just for memory, not other failures. */
78 handle_new_client_fd (DBusServer *server,
81 DBusConnection *connection;
82 DBusTransport *transport;
84 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
86 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
89 transport = _dbus_transport_new_for_fd (client_fd, TRUE, NULL);
90 if (transport == NULL)
96 if (!_dbus_transport_set_auth_mechanisms (transport,
97 (const char **) server->auth_mechanisms))
99 _dbus_transport_unref (transport);
103 /* note that client_fd is now owned by the transport, and will be
104 * closed on transport disconnection/finalization
107 connection = _dbus_connection_new_for_transport (transport);
108 _dbus_transport_unref (transport);
110 if (connection == NULL)
113 /* See if someone wants to handle this new connection,
114 * self-referencing for paranoia
116 if (server->new_connection_function)
118 dbus_server_ref (server);
120 (* server->new_connection_function) (server, connection,
121 server->new_connection_data);
122 dbus_server_unref (server);
125 /* If no one grabbed a reference, the connection will die. */
126 dbus_connection_unref (connection);
132 unix_handle_watch (DBusServer *server,
136 DBusServerUnix *unix_server = (DBusServerUnix*) server;
138 _dbus_assert (watch == unix_server->watch);
140 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
142 if (flags & DBUS_WATCH_READABLE)
147 listen_fd = dbus_watch_get_fd (watch);
149 client_fd = _dbus_accept (listen_fd);
153 /* EINTR handled for us */
155 if (errno == EAGAIN || errno == EWOULDBLOCK)
156 _dbus_verbose ("No client available to accept after all\n");
158 _dbus_verbose ("Failed to accept a client connection: %s\n",
159 _dbus_strerror (errno));
163 _dbus_fd_set_close_on_exec (client_fd);
165 if (!handle_new_client_fd (server, client_fd))
166 _dbus_verbose ("Rejected client connection due to lack of memory\n");
170 if (flags & DBUS_WATCH_ERROR)
171 _dbus_verbose ("Error on server listening socket\n");
173 if (flags & DBUS_WATCH_HANGUP)
174 _dbus_verbose ("Hangup on server listening socket\n");
180 unix_disconnect (DBusServer *server)
182 DBusServerUnix *unix_server = (DBusServerUnix*) server;
184 if (unix_server->watch)
186 _dbus_server_remove_watch (server,
188 _dbus_watch_unref (unix_server->watch);
189 unix_server->watch = NULL;
192 close (unix_server->fd);
193 unix_server->fd = -1;
195 if (unix_server->socket_name != NULL)
198 _dbus_string_init_const (&tmp, unix_server->socket_name);
199 _dbus_delete_file (&tmp, NULL);
203 static DBusServerVTable unix_vtable = {
210 * Creates a new server listening on the given file descriptor. The
211 * file descriptor should be nonblocking (use
212 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
213 * should be listening for connections, that is, listen() should have
214 * been successfully invoked on it. The server will use accept() to
215 * accept new client connections.
217 * @param fd the file descriptor.
218 * @param address the server's address
219 * @returns the new server, or #NULL if no memory.
223 _dbus_server_new_for_fd (int fd,
224 const DBusString *address)
226 DBusServerUnix *unix_server;
229 watch = _dbus_watch_new (fd,
235 unix_server = dbus_new0 (DBusServerUnix, 1);
236 if (unix_server == NULL)
238 _dbus_watch_unref (watch);
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 error location to store reason for failure.
270 * @returns the new server, or #NULL on failure.
273 _dbus_server_new_for_domain_socket (const char *path,
277 DBusServerUnix *unix_server;
282 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
284 if (!_dbus_string_init (&address))
286 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
290 if (!_dbus_string_append (&address, "unix:path=") ||
291 !_dbus_string_append (&address, path))
293 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
297 path_copy = _dbus_strdup (path);
298 if (path_copy == NULL)
300 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
304 listen_fd = _dbus_listen_unix_socket (path, error);
305 _dbus_fd_set_close_on_exec (listen_fd);
309 _DBUS_ASSERT_ERROR_IS_SET (error);
313 server = _dbus_server_new_for_fd (listen_fd, &address);
316 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
320 unix_server = (DBusServerUnix*) server;
321 unix_server->socket_name = path_copy;
323 _dbus_string_free (&address);
328 _dbus_close (listen_fd, NULL);
330 dbus_free (path_copy);
332 _dbus_string_free (&address);
338 * Creates a new server listening on the given hostname and port.
339 * If the hostname is NULL, listens on localhost.
341 * @param host the hostname to listen on.
342 * @param port the port to listen on.
343 * @param error location to store reason for failure.
344 * @returns the new server, or #NULL on failure.
347 _dbus_server_new_for_tcp_socket (const char *host,
355 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
357 if (!_dbus_string_init (&address))
359 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
363 if (!_dbus_string_append (&address, "tcp:host=") ||
364 !_dbus_string_append (&address, host) ||
365 !_dbus_string_append (&address, ",port=") ||
366 !_dbus_string_append_int (&address, port))
368 _dbus_string_free (&address);
369 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
373 listen_fd = _dbus_listen_tcp_socket (host, port, error);
374 _dbus_fd_set_close_on_exec (listen_fd);
378 _dbus_string_free (&address);
382 server = _dbus_server_new_for_fd (listen_fd, &address);
385 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
387 _dbus_string_free (&address);
391 _dbus_string_free (&address);