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