2002-11-24 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 #include <sys/socket.h>
31 #include <sys/un.h>
32 #include <errno.h>
33 #include <fcntl.h>
34
35 /**
36  * @defgroup DBusServerUnix DBusServer implementations for UNIX
37  * @ingroup  DBusInternals
38  * @brief Implementation details of DBusServer on UNIX
39  *
40  * @{
41  */
42 /**
43  * 
44  * Opaque object representing a Unix server implementation.
45  */
46 typedef struct DBusServerUnix DBusServerUnix;
47
48 /**
49  * Implementation details of DBusServerUnix. All members
50  * are private.
51  */
52 struct DBusServerUnix
53 {
54   DBusServer base;  /**< Parent class members. */
55   int fd;           /**< File descriptor or -1 if disconnected. */
56   DBusWatch *watch; /**< File descriptor watch. */
57 };
58
59 static void
60 unix_finalize (DBusServer *server)
61 {
62   DBusServerUnix *unix_server = (DBusServerUnix*) server;
63   
64   _dbus_server_finalize_base (server);
65
66   if (unix_server->watch)
67     _dbus_watch_unref (unix_server->watch);
68   
69   dbus_free (server);
70 }
71
72 static void
73 handle_new_client_fd (DBusServer *server,
74                       int         client_fd)
75 {
76   DBusConnection *connection;
77   DBusTransport *transport;
78   
79   _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
80           
81   if (!_dbus_set_fd_nonblocking (client_fd, NULL))
82     return;
83   
84   transport = _dbus_transport_new_for_fd (client_fd);
85   if (transport == NULL)
86     {
87       close (client_fd);
88       return;
89     }
90
91   /* note that client_fd is now owned by the transport, and will be
92    * closed on transport disconnection/finalization
93    */
94   
95   connection = _dbus_connection_new_for_transport (transport);
96   _dbus_transport_unref (transport);
97   
98   if (connection == NULL)
99     return;
100
101   /* See if someone wants to handle this new connection,
102    * self-referencing for paranoia
103    */
104   if (server->new_connection_function)
105     {
106       dbus_server_ref (server);
107       
108       (* server->new_connection_function) (server, connection,
109                                            server->new_connection_data);
110       dbus_server_unref (server);
111     }
112   
113   /* If no one grabbed a reference, the connection will die. */
114   dbus_connection_unref (connection);
115 }
116
117 static void
118 unix_handle_watch (DBusServer  *server,
119                    DBusWatch   *watch,
120                    unsigned int flags)
121 {
122   DBusServerUnix *unix_server = (DBusServerUnix*) server;
123
124   _dbus_assert (watch == unix_server->watch);
125
126   _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
127   
128   if (flags & DBUS_WATCH_READABLE)
129     {
130       int client_fd;
131       int listen_fd;
132       
133       listen_fd = dbus_watch_get_fd (watch);
134
135     retry:
136       client_fd = accept (listen_fd, NULL, NULL);
137       
138       if (client_fd < 0)
139         {
140           if (errno == EINTR)
141             goto retry;
142           else if (errno == EAGAIN || errno == EWOULDBLOCK)
143             _dbus_verbose ("No client available to accept after all\n");
144           else
145             _dbus_verbose ("Failed to accept a client connection: %s\n",
146                            _dbus_strerror (errno));
147         }
148       else
149         {
150           handle_new_client_fd (server, client_fd);
151         }
152     }
153
154   if (flags & DBUS_WATCH_ERROR)
155     _dbus_verbose ("Error on server listening socket\n");
156
157   if (flags & DBUS_WATCH_HANGUP)
158     _dbus_verbose ("Hangup on server listening socket\n");
159 }
160   
161 static void
162 unix_disconnect (DBusServer *server)
163 {
164   DBusServerUnix *unix_server = (DBusServerUnix*) server;
165
166   if (unix_server->watch)
167     {
168       _dbus_server_remove_watch (server,
169                                  unix_server->watch);
170       _dbus_watch_unref (unix_server->watch);
171       unix_server->watch = NULL;
172     }
173   
174   close (unix_server->fd);
175   unix_server->fd = -1;
176 }
177
178 static DBusServerVTable unix_vtable = {
179   unix_finalize,
180   unix_handle_watch,
181   unix_disconnect
182 };
183
184 /**
185  * Creates a new server listening on the given file descriptor.  The
186  * file descriptor should be nonblocking (use
187  * _dbus_set_fd_nonblocking() to make it so). The file descriptor
188  * should be listening for connections, that is, listen() should have
189  * been successfully invoked on it. The server will use accept() to
190  * accept new client connections.
191  *
192  * @param fd the file descriptor.
193  * @returns the new server, or #NULL if no memory.
194  * 
195  */
196 DBusServer*
197 _dbus_server_new_for_fd (int fd)
198 {
199   DBusServerUnix *unix_server;
200   DBusWatch *watch;
201
202   watch = _dbus_watch_new (fd,
203                            DBUS_WATCH_READABLE);
204   if (watch == NULL)
205     return NULL;
206   
207   unix_server = dbus_new0 (DBusServerUnix, 1);
208   if (unix_server == NULL)
209     {
210       _dbus_watch_unref (watch);
211       return NULL;
212     }
213   
214   if (!_dbus_server_init_base (&unix_server->base,
215                                &unix_vtable))
216     {
217       _dbus_watch_unref (watch);
218       dbus_free (unix_server);
219       return NULL;
220     }
221
222   if (!_dbus_server_add_watch (&unix_server->base,
223                                watch))
224     {
225       _dbus_server_finalize_base (&unix_server->base);
226       _dbus_watch_unref (watch);
227       dbus_free (unix_server);
228       return NULL;
229     }
230   
231   unix_server->fd = fd;
232   unix_server->watch = watch;
233
234   return (DBusServer*) unix_server;
235 }
236
237 /**
238  * Creates a new server listening on the given Unix domain socket.
239  *
240  * @param path the path for the domain socket.
241  * @param result location to store reason for failure.
242  * @returns the new server, or #NULL on failure.
243  */
244 DBusServer*
245 _dbus_server_new_for_domain_socket (const char     *path,
246                                     DBusResultCode *result)
247 {
248   DBusServer *server;
249   int listen_fd;
250   struct sockaddr_un addr;
251
252   listen_fd = socket (AF_LOCAL, SOCK_STREAM, 0);
253
254   if (listen_fd < 0)
255     {
256       dbus_set_result (result, _dbus_result_from_errno (errno));
257       _dbus_verbose ("Failed to create socket \"%s\": %s\n",
258                      path, _dbus_strerror (errno));
259       return NULL;
260     }
261
262   if (!_dbus_set_fd_nonblocking (listen_fd, result))
263     {
264       close (listen_fd);
265       return NULL;
266     }
267
268   _DBUS_ZERO (addr);
269   addr.sun_family = AF_LOCAL;
270   strncpy (addr.sun_path, path, _DBUS_MAX_SUN_PATH_LENGTH);
271   addr.sun_path[_DBUS_MAX_SUN_PATH_LENGTH] = '\0';
272   
273   if (bind (listen_fd, (struct sockaddr*) &addr, SUN_LEN (&addr)) < 0)
274     {
275       dbus_set_result (result, _dbus_result_from_errno (errno));
276       _dbus_verbose ("Failed to bind socket \"%s\": %s\n",
277                      path, _dbus_strerror (errno));
278       close (listen_fd);
279       return NULL;
280     }
281
282   if (listen (listen_fd, 30 /* backlog */) < 0)
283     {
284       dbus_set_result (result, _dbus_result_from_errno (errno));      
285       _dbus_verbose ("Failed to listen on socket \"%s\": %s\n",
286                      path, _dbus_strerror (errno));
287       close (listen_fd);
288       return NULL;
289     }
290   
291   server = _dbus_server_new_for_fd (listen_fd);
292   if (server == NULL)
293     {
294       dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
295       close (listen_fd);
296       return NULL;
297     }
298
299   return server;
300 }
301
302 /** @} */
303