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