2003-02-16 Anders Carlsson <andersca@codefactory.se>
[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 };
54
55 static void
56 unix_finalize (DBusServer *server)
57 {
58   DBusServerUnix *unix_server = (DBusServerUnix*) server;
59   
60   _dbus_server_finalize_base (server);
61
62   if (unix_server->watch)
63     _dbus_watch_unref (unix_server->watch);
64   
65   dbus_free (server);
66 }
67
68 static void
69 handle_new_client_fd (DBusServer *server,
70                       int         client_fd)
71 {
72   DBusConnection *connection;
73   DBusTransport *transport;
74   
75   _dbus_verbose ("Creating new client connection with fd %d\n", client_fd);
76           
77   if (!_dbus_set_fd_nonblocking (client_fd, NULL))
78     return;
79   
80   transport = _dbus_transport_new_for_fd (client_fd, TRUE);
81   if (transport == NULL)
82     {
83       close (client_fd);
84       return;
85     }
86
87   /* note that client_fd is now owned by the transport, and will be
88    * closed on transport disconnection/finalization
89    */
90   
91   connection = _dbus_connection_new_for_transport (transport);
92   _dbus_transport_unref (transport);
93   
94   if (connection == NULL)
95     return;
96
97   _dbus_connection_set_connection_counter (connection,
98                                            server->connection_counter);
99   
100   /* See if someone wants to handle this new connection,
101    * self-referencing for paranoia
102    */
103   if (server->new_connection_function)
104     {
105       dbus_server_ref (server);
106       
107       (* server->new_connection_function) (server, connection,
108                                            server->new_connection_data);
109       dbus_server_unref (server);
110     }
111   
112   /* If no one grabbed a reference, the connection will die. */
113   dbus_connection_unref (connection);
114 }
115
116 static void
117 unix_handle_watch (DBusServer  *server,
118                    DBusWatch   *watch,
119                    unsigned int flags)
120 {
121   DBusServerUnix *unix_server = (DBusServerUnix*) server;
122
123   _dbus_assert (watch == unix_server->watch);
124
125   _dbus_verbose ("Handling client connection, flags 0x%x\n", flags);
126   
127   if (flags & DBUS_WATCH_READABLE)
128     {
129       int client_fd;
130       int listen_fd;
131       
132       listen_fd = dbus_watch_get_fd (watch);
133
134       client_fd = _dbus_accept (listen_fd);
135       
136       if (client_fd < 0)
137         {
138           /* EINTR handled for us */
139           
140           if (errno == EAGAIN || errno == EWOULDBLOCK)
141             _dbus_verbose ("No client available to accept after all\n");
142           else
143             _dbus_verbose ("Failed to accept a client connection: %s\n",
144                            _dbus_strerror (errno));
145         }
146       else
147         {
148           _dbus_fd_set_close_on_exec (client_fd);         
149           handle_new_client_fd (server, client_fd);
150         }
151     }
152
153   if (flags & DBUS_WATCH_ERROR)
154     _dbus_verbose ("Error on server listening socket\n");
155
156   if (flags & DBUS_WATCH_HANGUP)
157     _dbus_verbose ("Hangup on server listening socket\n");
158 }
159   
160 static void
161 unix_disconnect (DBusServer *server)
162 {
163   DBusServerUnix *unix_server = (DBusServerUnix*) server;
164
165   if (unix_server->watch)
166     {
167       _dbus_server_remove_watch (server,
168                                  unix_server->watch);
169       _dbus_watch_unref (unix_server->watch);
170       unix_server->watch = NULL;
171     }
172   
173   close (unix_server->fd);
174   unix_server->fd = -1;
175 }
176
177 static DBusServerVTable unix_vtable = {
178   unix_finalize,
179   unix_handle_watch,
180   unix_disconnect
181 };
182
183 /**
184  * Creates a new server listening on the given file descriptor.  The
185  * file descriptor should be nonblocking (use
186  * _dbus_set_fd_nonblocking() to make it so). The file descriptor
187  * should be listening for connections, that is, listen() should have
188  * been successfully invoked on it. The server will use accept() to
189  * accept new client connections.
190  *
191  * @param fd the file descriptor.
192  * @returns the new server, or #NULL if no memory.
193  * 
194  */
195 DBusServer*
196 _dbus_server_new_for_fd (int fd)
197 {
198   DBusServerUnix *unix_server;
199   DBusWatch *watch;
200
201   watch = _dbus_watch_new (fd,
202                            DBUS_WATCH_READABLE);
203   if (watch == NULL)
204     return NULL;
205   
206   unix_server = dbus_new0 (DBusServerUnix, 1);
207   if (unix_server == NULL)
208     {
209       _dbus_watch_unref (watch);
210       return NULL;
211     }
212   
213   if (!_dbus_server_init_base (&unix_server->base,
214                                &unix_vtable))
215     {
216       _dbus_watch_unref (watch);
217       dbus_free (unix_server);
218       return NULL;
219     }
220
221   if (!_dbus_server_add_watch (&unix_server->base,
222                                watch))
223     {
224       _dbus_server_finalize_base (&unix_server->base);
225       _dbus_watch_unref (watch);
226       dbus_free (unix_server);
227       return NULL;
228     }
229   
230   unix_server->fd = fd;
231   unix_server->watch = watch;
232
233   return (DBusServer*) unix_server;
234 }
235
236 /**
237  * Creates a new server listening on the given Unix domain socket.
238  *
239  * @param path the path for the domain socket.
240  * @param result location to store reason for failure.
241  * @returns the new server, or #NULL on failure.
242  */
243 DBusServer*
244 _dbus_server_new_for_domain_socket (const char     *path,
245                                     DBusResultCode *result)
246 {
247   DBusServer *server;
248   int listen_fd;
249
250   listen_fd = _dbus_listen_unix_socket (path, result);
251   _dbus_fd_set_close_on_exec (listen_fd);
252   
253   if (listen_fd < 0)
254     return NULL;
255   
256   server = _dbus_server_new_for_fd (listen_fd);
257   if (server == NULL)
258     {
259       dbus_set_result (result, DBUS_RESULT_NO_MEMORY);
260       close (listen_fd);
261       return NULL;
262     }
263
264   return server;
265 }
266
267 /** @} */
268