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