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. */
56 unix_finalize (DBusServer *server)
58 DBusServerUnix *unix_server = (DBusServerUnix*) server;
60 _dbus_server_finalize_base (server);
62 if (unix_server->watch)
63 _dbus_watch_unref (unix_server->watch);
69 handle_new_client_fd (DBusServer *server,
72 DBusConnection *connection;
73 DBusTransport *transport;
75 _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
77 if (!_dbus_set_fd_nonblocking (client_fd, NULL))
80 transport = _dbus_transport_new_for_fd (client_fd, TRUE);
81 if (transport == NULL)
87 /* note that client_fd is now owned by the transport, and will be
88 * closed on transport disconnection/finalization
91 connection = _dbus_connection_new_for_transport (transport);
92 _dbus_transport_unref (transport);
94 if (connection == NULL)
97 /* See if someone wants to handle this new connection,
98 * self-referencing for paranoia
100 if (server->new_connection_function)
102 dbus_server_ref (server);
104 (* server->new_connection_function) (server, connection,
105 server->new_connection_data);
106 dbus_server_unref (server);
109 /* If no one grabbed a reference, the connection will die. */
110 dbus_connection_unref (connection);
114 unix_handle_watch (DBusServer *server,
118 DBusServerUnix *unix_server = (DBusServerUnix*) server;
120 _dbus_assert (watch == unix_server->watch);
122 _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
124 if (flags & DBUS_WATCH_READABLE)
129 listen_fd = dbus_watch_get_fd (watch);
131 client_fd = _dbus_accept (listen_fd);
135 /* EINTR handled for us */
137 if (errno == EAGAIN || errno == EWOULDBLOCK)
138 _dbus_verbose ("No client available to accept after all\n");
140 _dbus_verbose ("Failed to accept a client connection: %s\n",
141 _dbus_strerror (errno));
145 handle_new_client_fd (server, client_fd);
149 if (flags & DBUS_WATCH_ERROR)
150 _dbus_verbose ("Error on server listening socket\n");
152 if (flags & DBUS_WATCH_HANGUP)
153 _dbus_verbose ("Hangup on server listening socket\n");
157 unix_disconnect (DBusServer *server)
159 DBusServerUnix *unix_server = (DBusServerUnix*) server;
161 if (unix_server->watch)
163 _dbus_server_remove_watch (server,
165 _dbus_watch_unref (unix_server->watch);
166 unix_server->watch = NULL;
169 close (unix_server->fd);
170 unix_server->fd = -1;
173 static DBusServerVTable unix_vtable = {
180 * Creates a new server listening on the given file descriptor. The
181 * file descriptor should be nonblocking (use
182 * _dbus_set_fd_nonblocking() to make it so). The file descriptor
183 * should be listening for connections, that is, listen() should have
184 * been successfully invoked on it. The server will use accept() to
185 * accept new client connections.
187 * @param fd the file descriptor.
188 * @returns the new server, or #NULL if no memory.
192 _dbus_server_new_for_fd (int fd)
194 DBusServerUnix *unix_server;
197 watch = _dbus_watch_new (fd,
198 DBUS_WATCH_READABLE);
202 unix_server = dbus_new0 (DBusServerUnix, 1);
203 if (unix_server == NULL)
205 _dbus_watch_unref (watch);
209 if (!_dbus_server_init_base (&unix_server->base,
212 _dbus_watch_unref (watch);
213 dbus_free (unix_server);
217 if (!_dbus_server_add_watch (&unix_server->base,
220 _dbus_server_finalize_base (&unix_server->base);
221 _dbus_watch_unref (watch);
222 dbus_free (unix_server);
226 unix_server->fd = fd;
227 unix_server->watch = watch;
229 return (DBusServer*) unix_server;
233 * Creates a new server listening on the given Unix domain socket.
235 * @param path the path for the domain socket.
236 * @param result location to store reason for failure.
237 * @returns the new server, or #NULL on failure.
240 _dbus_server_new_for_domain_socket (const char *path,
241 DBusResultCode *result)
246 listen_fd = _dbus_listen_unix_socket (path, result);
250 server = _dbus_server_new_for_fd (listen_fd);
253 dbus_set_result (result, DBUS_RESULT_NO_MEMORY);