2003-03-31 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-server-unix.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-server-unix.c Server implementation for Unix network protocols.
3  *
4  * Copyright (C) 2002  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
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>
29 #include <unistd.h>
30
31 /**
32  * @defgroup DBusServerUnix DBusServer implementations for UNIX
33  * @ingroup  DBusInternals
34  * @brief Implementation details of DBusServer on UNIX
35  *
36  * @{
37  */
38 /**
39  * 
40  * Opaque object representing a Unix server implementation.
41  */
42 typedef struct DBusServerUnix DBusServerUnix;
43
44 /**
45  * Implementation details of DBusServerUnix. All members
46  * are private.
47  */
48 struct DBusServerUnix
49 {
50   DBusServer base;  /**< Parent class members. */
51   int fd;           /**< File descriptor or -1 if disconnected. */
52   DBusWatch *watch; /**< File descriptor watch. */
53 };
54
55 static void
56 unix_finalize (DBusServer *server)
57 {
58   DBusServerUnix *unix_server = (DBusServerUnix*) server;
59   
60   _dbus_server_finalize_base (server);
61
62   if (unix_server->watch)
63     _dbus_watch_unref (unix_server->watch);
64   
65   dbus_free (server);
66 }
67
68 /**
69  * @todo unreffing the connection at the end may cause
70  * us to drop the last ref to the connection before
71  * disconnecting it. That is invalid.
72  */
73 /* Return value is just for memory, not other failures. */
74 static dbus_bool_t
75 handle_new_client_fd (DBusServer *server,
76                       int         client_fd)
77 {
78   DBusConnection *connection;
79   DBusTransport *transport;
80   
81   _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
82           
83   if (!_dbus_set_fd_nonblocking (client_fd, NULL))
84     return TRUE;
85   
86   transport = _dbus_transport_new_for_fd (client_fd, TRUE, NULL);
87   if (transport == NULL)
88     {
89       close (client_fd);
90       return FALSE;
91     }
92
93   /* note that client_fd is now owned by the transport, and will be
94    * closed on transport disconnection/finalization
95    */
96   
97   connection = _dbus_connection_new_for_transport (transport);
98   _dbus_transport_unref (transport);
99   
100   if (connection == NULL)
101     return FALSE;
102
103   _dbus_connection_set_connection_counter (connection,
104                                            server->connection_counter);
105   
106   /* See if someone wants to handle this new connection,
107    * self-referencing for paranoia
108    */
109   if (server->new_connection_function)
110     {
111       dbus_server_ref (server);
112       
113       (* server->new_connection_function) (server, connection,
114                                            server->new_connection_data);
115       dbus_server_unref (server);
116     }
117   
118   /* If no one grabbed a reference, the connection will die. */
119   dbus_connection_unref (connection);
120
121   return TRUE;
122 }
123
124 static dbus_bool_t
125 unix_handle_watch (DBusServer  *server,
126                    DBusWatch   *watch,
127                    unsigned int flags)
128 {
129   DBusServerUnix *unix_server = (DBusServerUnix*) server;
130
131   _dbus_assert (watch == unix_server->watch);
132
133   _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
134   
135   if (flags & DBUS_WATCH_READABLE)
136     {
137       int client_fd;
138       int listen_fd;
139       
140       listen_fd = dbus_watch_get_fd (watch);
141
142       client_fd = _dbus_accept (listen_fd);
143       
144       if (client_fd < 0)
145         {
146           /* EINTR handled for us */
147           
148           if (errno == EAGAIN || errno == EWOULDBLOCK)
149             _dbus_verbose ("No client available to accept after all\n");
150           else
151             _dbus_verbose ("Failed to accept a client connection: %s\n",
152                            _dbus_strerror (errno));
153         }
154       else
155         {
156           _dbus_fd_set_close_on_exec (client_fd);         
157
158           if (!handle_new_client_fd (server, client_fd))
159             _dbus_verbose ("Rejected client connection due to lack of memory\n");
160         }
161     }
162
163   if (flags & DBUS_WATCH_ERROR)
164     _dbus_verbose ("Error on server listening socket\n");
165
166   if (flags & DBUS_WATCH_HANGUP)
167     _dbus_verbose ("Hangup on server listening socket\n");
168
169   return TRUE;
170 }
171   
172 static void
173 unix_disconnect (DBusServer *server)
174 {
175   DBusServerUnix *unix_server = (DBusServerUnix*) server;
176
177   if (unix_server->watch)
178     {
179       _dbus_server_remove_watch (server,
180                                  unix_server->watch);
181       _dbus_watch_unref (unix_server->watch);
182       unix_server->watch = NULL;
183     }
184   
185   close (unix_server->fd);
186   unix_server->fd = -1;
187 }
188
189 static DBusServerVTable unix_vtable = {
190   unix_finalize,
191   unix_handle_watch,
192   unix_disconnect
193 };
194
195 /**
196  * Creates a new server listening on the given file descriptor.  The
197  * file descriptor should be nonblocking (use
198  * _dbus_set_fd_nonblocking() to make it so). The file descriptor
199  * should be listening for connections, that is, listen() should have
200  * been successfully invoked on it. The server will use accept() to
201  * accept new client connections.
202  *
203  * @param fd the file descriptor.
204  * @param address the server's address
205  * @returns the new server, or #NULL if no memory.
206  * 
207  */
208 DBusServer*
209 _dbus_server_new_for_fd (int               fd,
210                          const DBusString *address)
211 {
212   DBusServerUnix *unix_server;
213   DBusWatch *watch;
214
215   watch = _dbus_watch_new (fd,
216                            DBUS_WATCH_READABLE,
217                            TRUE);
218   if (watch == NULL)
219     return NULL;
220   
221   unix_server = dbus_new0 (DBusServerUnix, 1);
222   if (unix_server == NULL)
223     {
224       _dbus_watch_unref (watch);
225       return NULL;
226     }
227   
228   if (!_dbus_server_init_base (&unix_server->base,
229                                &unix_vtable, address))
230     {
231       _dbus_watch_unref (watch);
232       dbus_free (unix_server);
233       return NULL;
234     }
235
236   if (!_dbus_server_add_watch (&unix_server->base,
237                                watch))
238     {
239       _dbus_server_finalize_base (&unix_server->base);
240       _dbus_watch_unref (watch);
241       dbus_free (unix_server);
242       return NULL;
243     }
244   
245   unix_server->fd = fd;
246   unix_server->watch = watch;
247
248   return (DBusServer*) unix_server;
249 }
250
251 /**
252  * Creates a new server listening on the given Unix domain socket.
253  *
254  * @param path the path for the domain socket.
255  * @param error location to store reason for failure.
256  * @returns the new server, or #NULL on failure.
257  */
258 DBusServer*
259 _dbus_server_new_for_domain_socket (const char     *path,
260                                     DBusError      *error)
261 {
262   DBusServer *server;
263   int listen_fd;
264   DBusString address;
265   
266   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
267
268   if (!_dbus_string_init (&address))
269     {
270       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
271       return NULL;
272     }
273
274   if (!_dbus_string_append (&address, "unix:path=") ||
275       !_dbus_string_append (&address, path))
276     {
277       _dbus_string_free (&address);
278       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
279       return NULL;
280     }
281   
282   listen_fd = _dbus_listen_unix_socket (path, error);
283   _dbus_fd_set_close_on_exec (listen_fd);
284   
285   if (listen_fd < 0)
286     {
287       _dbus_string_free (&address);
288       return NULL;
289     }
290   
291   server = _dbus_server_new_for_fd (listen_fd, &address);
292   if (server == NULL)
293     {
294       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
295       close (listen_fd);
296       _dbus_string_free (&address);
297       return NULL;
298     }
299
300   _dbus_string_free (&address);
301   
302   return server;
303 }
304
305 /**
306  * Creates a new server listening on the given hostname and port.
307  * If the hostname is NULL, listens on localhost.
308  *
309  * @param host the hostname to listen on.
310  * @param port the port to listen on.
311  * @param error location to store reason for failure.
312  * @returns the new server, or #NULL on failure.
313  */
314 DBusServer*
315 _dbus_server_new_for_tcp_socket (const char     *host,
316                                  dbus_uint32_t   port,
317                                  DBusError      *error)
318 {
319   DBusServer *server;
320   int listen_fd;
321   DBusString address;
322   
323   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
324
325   if (!_dbus_string_init (&address))
326     {
327       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
328       return NULL;
329     }
330
331   if (!_dbus_string_append (&address, "tcp:host=") ||
332       !_dbus_string_append (&address, host) ||
333       !_dbus_string_append (&address, ",port=") ||
334       !_dbus_string_append_int (&address, port))
335     {
336       _dbus_string_free (&address);
337       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
338       return NULL;
339     }
340   
341   listen_fd = _dbus_listen_tcp_socket (host, port, error);
342   _dbus_fd_set_close_on_exec (listen_fd);
343   
344   if (listen_fd < 0)
345     {
346       _dbus_string_free (&address);
347       return NULL;
348     }
349   
350   server = _dbus_server_new_for_fd (listen_fd, &address);
351   if (server == NULL)
352     {
353       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
354       close (listen_fd);
355       _dbus_string_free (&address);
356       return NULL;
357     }
358
359   _dbus_string_free (&address);
360   
361   return server;
362
363
364 }
365
366 /** @} */
367