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