Add support for abstract unix socket addresses
[platform/upstream/glib.git] / gio / gsocketservice.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2009 Codethink Limited
4  * Copyright © 2009 Red Hat, Inc
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2 of the licence or (at
9  * your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General
17  * Public License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  * Authors: Ryan Lortie <desrt@desrt.ca>
22  *          Alexander Larsson <alexl@redhat.com>
23  */
24
25 /**
26  * SECTION: gsocketservice
27  * @title: GSocketService
28  * @short_description: Make it easy to implement a network service
29  * @see_also: #GThreadedSocketService, #GSocketListener.
30  *
31  * A #GSocketService is an object that represents a service that is
32  * provided to the network or over local sockets.  When a new
33  * connection is made to the service the ::incoming signal is emitted.
34  *
35  * A #GSocketService is a subclass of #GSocketListener and you need
36  * to add the addresses you want to accept connections on to the
37  * with the #GSocketListener APIs.
38  *
39  * There are two options for implementing a network service based on
40  * #GSocketService.  The first is to create the service using
41  * g_socket_service_new() and to connect to the ::incoming signal.
42  * The second is to subclass #GSocketService and override the default
43  * signal handler implementation.
44  *
45  * In either case, the handler must immediately return, or else it
46  * will block additional incoming connections from being serviced.  If
47  * you are interested in writing connection handlers that contain
48  * blocking code then see #GThreadedSocketService.
49  *
50  * The socket service runs on the main loop in the main thread, and is
51  * not threadsafe in general. However, the calls to start and stop
52  * the service are threadsafe so these can be used from threads that
53  * handle incomming clients.
54  *
55  * Since: 2.22
56  */
57
58 #include "config.h"
59 #include "gsocketservice.h"
60
61 #include "gio-marshal.h"
62 #include <gio/gio.h>
63 #include "gsocketlistener.h"
64 #include "gsocketconnection.h"
65
66 #include "gioalias.h"
67
68 static guint g_socket_service_incoming_signal;
69
70 G_DEFINE_TYPE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER);
71
72 G_LOCK_DEFINE_STATIC(active);
73
74 struct _GSocketServicePrivate
75 {
76   GCancellable *cancellable;
77   guint active : 1;
78   guint outstanding_accept : 1;
79 };
80
81 static void g_socket_service_ready (GObject      *object,
82                                     GAsyncResult *result,
83                                     gpointer      user_data);
84
85 static gboolean
86 g_socket_service_real_incoming (GSocketService    *service,
87                                 GSocketConnection *connection,
88                                 GObject           *source_object)
89 {
90   return FALSE;
91 }
92
93 static void
94 g_socket_service_init (GSocketService *service)
95 {
96   service->priv = G_TYPE_INSTANCE_GET_PRIVATE (service,
97                                                G_TYPE_SOCKET_SERVICE,
98                                                GSocketServicePrivate);
99   service->priv->cancellable = g_cancellable_new ();
100   service->priv->active = TRUE;
101 }
102
103 static void
104 g_socket_service_finalize (GObject *object)
105 {
106   GSocketService *service = G_SOCKET_SERVICE (object);
107
108   g_object_unref (service->priv->cancellable);
109
110   G_OBJECT_CLASS (g_socket_service_parent_class)
111     ->finalize (object);
112 }
113
114 static void
115 do_accept (GSocketService  *service)
116 {
117   g_socket_listener_accept_async (G_SOCKET_LISTENER (service),
118                                   service->priv->cancellable,
119                                   g_socket_service_ready, NULL);
120   service->priv->outstanding_accept = TRUE;
121 }
122
123 static void
124 g_socket_service_changed (GSocketListener *listener)
125 {
126   GSocketService  *service = G_SOCKET_SERVICE (listener);
127
128   G_LOCK (active);
129
130   if (service->priv->active)
131     {
132       if (service->priv->outstanding_accept)
133         g_cancellable_cancel (service->priv->cancellable);
134       else
135         {
136           g_socket_listener_accept_async (listener, service->priv->cancellable,
137                                           g_socket_service_ready, NULL);
138           service->priv->outstanding_accept = TRUE;
139         }
140     }
141
142   G_UNLOCK (active);
143 }
144
145 /**
146  * g_socket_service_is_active:
147  * @service: a #GSocketService
148  *
149  * Check whether the service is active or not. An active
150  * service will accept new clients that connect, while
151  * a non-active service will let connecting clients queue
152  * up until the service is started.
153  *
154  * Returns: %TRUE if the service is active, %FALSE otherwise
155  *
156  * Since: 2.22
157  **/
158 gboolean
159 g_socket_service_is_active (GSocketService *service)
160 {
161   gboolean active;
162
163   G_LOCK (active);
164   active = service->priv->active;
165   G_UNLOCK (active);
166   return active;
167 }
168
169 /**
170  * g_socket_service_start:
171  * @service: a #GSocketService
172  *
173  * Starts the service, i.e. start accepting connections
174  * from the added sockets when the mainloop runs.
175  *
176  * This call is threadsafe, so it may be called from a thread
177  * handling an incomming client request.
178  *
179  * Since: 2.22
180  **/
181 void
182 g_socket_service_start (GSocketService *service)
183 {
184   G_LOCK (active);
185
186   if (!service->priv->active)
187     {
188       service->priv->active = TRUE;
189
190       if (service->priv->outstanding_accept)
191         g_cancellable_cancel (service->priv->cancellable);
192       else
193         do_accept (service);
194     }
195
196   G_UNLOCK (active);
197 }
198
199 /**
200  * g_socket_service_stop:
201  * @service: a #GSocketService
202  *
203  * Stops the service, i.e. stops accepting connections
204  * from the added sockets when the mainloop runs.
205  *
206  * This call is threadsafe, so it may be called from a thread
207  * handling an incomming client request.
208  *
209  * Since: 2.22
210  **/
211 void
212 g_socket_service_stop (GSocketService  *service)
213 {
214   G_LOCK (active);
215
216   if (service->priv->active)
217     {
218       service->priv->active = FALSE;
219
220       if (service->priv->outstanding_accept)
221         g_cancellable_cancel (service->priv->cancellable);
222     }
223
224   G_UNLOCK (active);
225 }
226
227
228 static gboolean
229 g_socket_service_incoming (GSocketService    *service,
230                            GSocketConnection *connection,
231                            GObject           *source_object)
232 {
233   gboolean result;
234
235   g_signal_emit (service, g_socket_service_incoming_signal,
236                  0, connection, source_object, &result);
237   return result;
238 }
239
240 static void
241 g_socket_service_class_init (GSocketServiceClass *class)
242 {
243   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
244   GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
245
246   g_type_class_add_private (class, sizeof (GSocketServicePrivate));
247
248   gobject_class->finalize = g_socket_service_finalize;
249   listener_class->changed = g_socket_service_changed;
250   class->incoming = g_socket_service_real_incoming;
251
252   /**
253    * GSocketService::incoming:
254    * @service: the #GSocketService.
255    * @connection: a new #GSocketConnection object.
256    * @source_object: the source_object passed to g_socket_listener_add_address().
257    * @returns: %TRUE if @connection has been handled.
258    *
259    * The ::incoming signal is emitted when a new incoming connection
260    * to @service needs to be handled.  The handler must initiate the
261    * handling of @connection, but may not block; in essence,
262    * asynchronous operations must be used.
263    *
264    * If %TRUE is returned then no other handlers are called.
265    **/
266   g_socket_service_incoming_signal =
267     g_signal_new ("incoming", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
268                   G_STRUCT_OFFSET (GSocketServiceClass, incoming),
269                   g_signal_accumulator_true_handled, NULL,
270                   _gio_marshal_BOOLEAN__OBJECT_OBJECT, G_TYPE_BOOLEAN,
271                   2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
272 }
273
274 static void
275 g_socket_service_ready (GObject      *object,
276                         GAsyncResult *result,
277                         gpointer      user_data)
278 {
279   GSocketListener *listener = G_SOCKET_LISTENER (object);
280   GSocketService *service = G_SOCKET_SERVICE (object);
281   GSocketConnection *connection;
282   GObject *source_object;
283   GError *error = NULL;
284
285   connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
286   if (error)
287     {
288       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
289         g_warning ("fail: %s", error->message);
290       g_error_free (error);
291     }
292   else
293     {
294       g_socket_service_incoming (service, connection, source_object);
295       g_object_unref (connection);
296     }
297
298   G_LOCK (active);
299
300   g_cancellable_reset (service->priv->cancellable);
301
302   /* requeue */
303   service->priv->outstanding_accept = FALSE;
304   if (service->priv->active)
305     do_accept (service);
306
307   G_UNLOCK (active);
308 }
309
310
311 /**
312  * g_socket_service_new:
313  *
314  * Creates a new #GSocketService with no sockets to listen for.
315  * New listeners can be added with e.g. g_socket_listener_add_address()
316  * or g_socket_listener_add_inet_port().
317  *
318  * Returns: a new #GSocketService.
319  *
320  * Since: 2.22
321  **/
322 GSocketService *
323 g_socket_service_new (void)
324 {
325   return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);
326 }
327
328 #define __G_SOCKET_SERVICE_C__
329 #include "gioaliasdef.c"