2005-02-13 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, 2003, 2004  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
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   _dbus_server_finalize_base (server);
63
64   dbus_free (unix_server->socket_name);
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_and_unlock (DBusServer *server,
76                                  int         client_fd)
77 {
78   DBusConnection *connection;
79   DBusTransport *transport;
80   DBusNewConnectionFunction new_connection_function;
81   void *new_connection_data;
82   
83   _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
84
85   HAVE_LOCK_CHECK (server);
86   
87   if (!_dbus_set_fd_nonblocking (client_fd, NULL))
88     {
89       SERVER_UNLOCK (server);
90       return TRUE;
91     }
92   
93   transport = _dbus_transport_new_for_fd (client_fd, TRUE, NULL);
94   if (transport == NULL)
95     {
96       close (client_fd);
97       SERVER_UNLOCK (server);
98       return FALSE;
99     }
100
101   if (!_dbus_transport_set_auth_mechanisms (transport,
102                                             (const char **) server->auth_mechanisms))
103     {
104       _dbus_transport_unref (transport);
105       SERVER_UNLOCK (server);
106       return FALSE;
107     }
108   
109   /* note that client_fd is now owned by the transport, and will be
110    * closed on transport disconnection/finalization
111    */
112   
113   connection = _dbus_connection_new_for_transport (transport);
114   _dbus_transport_unref (transport);
115   transport = NULL; /* now under the connection lock */
116   
117   if (connection == NULL)
118     {
119       SERVER_UNLOCK (server);
120       return FALSE;
121     }
122   
123   /* See if someone wants to handle this new connection, self-referencing
124    * for paranoia.
125    */
126   new_connection_function = server->new_connection_function;
127   new_connection_data = server->new_connection_data;
128
129   _dbus_server_ref_unlocked (server);
130   SERVER_UNLOCK (server);
131   
132   if (new_connection_function)
133     {
134       (* new_connection_function) (server, connection,
135                                    new_connection_data);
136       dbus_server_unref (server);
137     }
138   
139   /* If no one grabbed a reference, the connection will die. */
140   dbus_connection_unref (connection);
141
142   return TRUE;
143 }
144
145 static dbus_bool_t
146 unix_handle_watch (DBusWatch    *watch,
147                    unsigned int  flags,
148                    void         *data)
149 {
150   DBusServer *server = data;
151   DBusServerUnix *unix_server = data;
152
153   SERVER_LOCK (server);
154   
155   _dbus_assert (watch == unix_server->watch);
156
157   _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
158   
159   if (flags & DBUS_WATCH_READABLE)
160     {
161       int client_fd;
162       int listen_fd;
163       
164       listen_fd = dbus_watch_get_fd (watch);
165
166       client_fd = _dbus_accept (listen_fd);
167       
168       if (client_fd < 0)
169         {
170           /* EINTR handled for us */
171           
172           if (errno == EAGAIN || errno == EWOULDBLOCK)
173             _dbus_verbose ("No client available to accept after all\n");
174           else
175             _dbus_verbose ("Failed to accept a client connection: %s\n",
176                            _dbus_strerror (errno));
177
178           SERVER_UNLOCK (server);
179         }
180       else
181         {
182           _dbus_fd_set_close_on_exec (client_fd);         
183
184           if (!handle_new_client_fd_and_unlock (server, client_fd))
185             _dbus_verbose ("Rejected client connection due to lack of memory\n");
186         }
187     }
188
189   if (flags & DBUS_WATCH_ERROR)
190     _dbus_verbose ("Error on server listening socket\n");
191
192   if (flags & DBUS_WATCH_HANGUP)
193     _dbus_verbose ("Hangup on server listening socket\n");
194
195   return TRUE;
196 }
197   
198 static void
199 unix_disconnect (DBusServer *server)
200 {
201   DBusServerUnix *unix_server = (DBusServerUnix*) server;
202
203   if (unix_server->watch)
204     {
205       _dbus_server_remove_watch (server,
206                                  unix_server->watch);
207       _dbus_watch_unref (unix_server->watch);
208       unix_server->watch = NULL;
209     }
210   
211   close (unix_server->fd);
212   unix_server->fd = -1;
213
214   if (unix_server->socket_name != NULL)
215     {
216       DBusString tmp;
217       _dbus_string_init_const (&tmp, unix_server->socket_name);
218       _dbus_delete_file (&tmp, NULL);
219     }
220 }
221
222 static DBusServerVTable unix_vtable = {
223   unix_finalize,
224   unix_disconnect
225 };
226
227 /**
228  * Creates a new server listening on the given file descriptor.  The
229  * file descriptor should be nonblocking (use
230  * _dbus_set_fd_nonblocking() to make it so). The file descriptor
231  * should be listening for connections, that is, listen() should have
232  * been successfully invoked on it. The server will use accept() to
233  * accept new client connections.
234  *
235  * @param fd the file descriptor.
236  * @param address the server's address
237  * @returns the new server, or #NULL if no memory.
238  * 
239  */
240 DBusServer*
241 _dbus_server_new_for_fd (int               fd,
242                          const DBusString *address)
243 {
244   DBusServerUnix *unix_server;
245   DBusWatch *watch;
246   
247   unix_server = dbus_new0 (DBusServerUnix, 1);
248   if (unix_server == NULL)
249     return NULL;
250
251   watch = _dbus_watch_new (fd,
252                            DBUS_WATCH_READABLE,
253                            TRUE,
254                            unix_handle_watch, unix_server,
255                            NULL);
256   if (watch == NULL)
257     {
258       dbus_free (unix_server);
259       return NULL;
260     }
261   
262   if (!_dbus_server_init_base (&unix_server->base,
263                                &unix_vtable, address))
264     {
265       _dbus_watch_unref (watch);
266       dbus_free (unix_server);
267       return NULL;
268     }
269
270 #ifndef DBUS_DISABLE_CHECKS
271   unix_server->base.have_server_lock = TRUE;
272 #endif
273   
274   if (!_dbus_server_add_watch (&unix_server->base,
275                                watch))
276     {
277       _dbus_server_finalize_base (&unix_server->base);
278       _dbus_watch_unref (watch);
279       dbus_free (unix_server);
280       return NULL;
281     }
282
283 #ifndef DBUS_DISABLE_CHECKS
284   unix_server->base.have_server_lock = FALSE;
285 #endif
286   
287   unix_server->fd = fd;
288   unix_server->watch = watch;
289
290   return (DBusServer*) unix_server;
291 }
292
293 /**
294  * Creates a new server listening on the given Unix domain socket.
295  *
296  * @param path the path for the domain socket.
297  * @param abstract #TRUE to use abstract socket namespace
298  * @param error location to store reason for failure.
299  * @returns the new server, or #NULL on failure.
300  */
301 DBusServer*
302 _dbus_server_new_for_domain_socket (const char     *path,
303                                     dbus_bool_t     abstract,
304                                     DBusError      *error)
305 {
306   DBusServer *server;
307   DBusServerUnix *unix_server;
308   int listen_fd;
309   DBusString address;
310   char *path_copy;
311   DBusString path_str;
312   
313   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
314
315   if (!_dbus_string_init (&address))
316     {
317       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
318       return NULL;
319     }
320
321   _dbus_string_init_const (&path_str, path);
322   if ((abstract &&
323        !_dbus_string_append (&address, "unix:abstract=")) ||
324       (!abstract &&
325        !_dbus_string_append (&address, "unix:path=")) ||
326       !_dbus_address_append_escaped (&address, &path_str))
327     {
328       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
329       goto failed_0;
330     }
331
332   path_copy = _dbus_strdup (path);
333   if (path_copy == NULL)
334     {
335       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
336       goto failed_0;
337     }
338   
339   listen_fd = _dbus_listen_unix_socket (path, abstract, error);
340   _dbus_fd_set_close_on_exec (listen_fd);
341   
342   if (listen_fd < 0)
343     {
344       _DBUS_ASSERT_ERROR_IS_SET (error);
345       goto failed_1;
346     }
347   
348   server = _dbus_server_new_for_fd (listen_fd, &address);
349   if (server == NULL)
350     {
351       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
352       goto failed_2;
353     }
354
355   unix_server = (DBusServerUnix*) server;
356   unix_server->socket_name = path_copy;
357   
358   _dbus_string_free (&address);
359   
360   return server;
361
362  failed_2:
363   _dbus_close (listen_fd, NULL);
364  failed_1:
365   dbus_free (path_copy);
366  failed_0:
367   _dbus_string_free (&address);
368
369   return NULL;
370 }
371
372 /**
373  * Creates a new server listening on the given hostname and port.
374  * If the hostname is NULL, listens on localhost.
375  *
376  * @param host the hostname to listen on.
377  * @param port the port to listen on.
378  * @param error location to store reason for failure.
379  * @returns the new server, or #NULL on failure.
380  */
381 DBusServer*
382 _dbus_server_new_for_tcp_socket (const char     *host,
383                                  dbus_uint32_t   port,
384                                  DBusError      *error)
385 {
386   DBusServer *server;
387   int listen_fd;
388   DBusString address;
389   DBusString host_str;
390   
391   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
392
393   if (!_dbus_string_init (&address))
394     {
395       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
396       return NULL;
397     }
398
399   if (host == NULL)
400     host = "localhost";
401
402   _dbus_string_init_const (&host_str, host);
403   if (!_dbus_string_append (&address, "tcp:host=") ||
404       !_dbus_address_append_escaped (&address, &host_str) ||
405       !_dbus_string_append (&address, ",port=") ||
406       !_dbus_string_append_int (&address, port))
407     {
408       _dbus_string_free (&address);
409       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
410       return NULL;
411     }
412   
413   listen_fd = _dbus_listen_tcp_socket (host, port, error);
414   _dbus_fd_set_close_on_exec (listen_fd);
415   
416   if (listen_fd < 0)
417     {
418       _dbus_string_free (&address);
419       return NULL;
420     }
421   
422   server = _dbus_server_new_for_fd (listen_fd, &address);
423   if (server == NULL)
424     {
425       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
426       close (listen_fd);
427       _dbus_string_free (&address);
428       return NULL;
429     }
430
431   _dbus_string_free (&address);
432   
433   return server;
434
435
436 }
437
438 /** @} */
439