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