mem-overflow: test malloc and realloc corner cases
[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
32  * is provided to the network or over local sockets.  When a new
33  * connection is made to the service the #GSocketService::incoming
34  * signal is emitted.
35  *
36  * A #GSocketService is a subclass of #GSocketListener and you need
37  * to add the addresses you want to accept connections on with the
38  * #GSocketListener APIs.
39  *
40  * There are two options for implementing a network service based on
41  * #GSocketService. The first is to create the service using
42  * g_socket_service_new() and to connect to the #GSocketService::incoming
43  * signal. The second is to subclass #GSocketService and override the
44  * default signal handler implementation.
45  *
46  * In either case, the handler must immediately return, or else it
47  * will block additional incoming connections from being serviced.
48  * If you are interested in writing connection handlers that contain
49  * blocking code then see #GThreadedSocketService.
50  *
51  * The socket service runs on the main loop of the <link
52  * linkend="g-main-context-push-thread-default-context">thread-default
53  * context</link> of the thread it is created in, and is not
54  * threadsafe in general. However, the calls to start and stop the
55  * service are thread-safe so these can be used from threads that
56  * handle incoming clients.
57  *
58  * Since: 2.22
59  */
60
61 #include "config.h"
62 #include "gsocketservice.h"
63
64 #include <gio/gio.h>
65 #include "gsocketlistener.h"
66 #include "gsocketconnection.h"
67
68
69 static guint g_socket_service_incoming_signal;
70
71 G_DEFINE_TYPE (GSocketService, g_socket_service, G_TYPE_SOCKET_LISTENER);
72
73 G_LOCK_DEFINE_STATIC(active);
74
75 struct _GSocketServicePrivate
76 {
77   GCancellable *cancellable;
78   guint active : 1;
79   guint outstanding_accept : 1;
80 };
81
82 static void g_socket_service_ready (GObject      *object,
83                                     GAsyncResult *result,
84                                     gpointer      user_data);
85
86 static gboolean
87 g_socket_service_real_incoming (GSocketService    *service,
88                                 GSocketConnection *connection,
89                                 GObject           *source_object)
90 {
91   return FALSE;
92 }
93
94 static void
95 g_socket_service_init (GSocketService *service)
96 {
97   service->priv = G_TYPE_INSTANCE_GET_PRIVATE (service,
98                                                G_TYPE_SOCKET_SERVICE,
99                                                GSocketServicePrivate);
100   service->priv->cancellable = g_cancellable_new ();
101   service->priv->active = TRUE;
102 }
103
104 static void
105 g_socket_service_finalize (GObject *object)
106 {
107   GSocketService *service = G_SOCKET_SERVICE (object);
108
109   g_object_unref (service->priv->cancellable);
110
111   G_OBJECT_CLASS (g_socket_service_parent_class)
112     ->finalize (object);
113 }
114
115 static void
116 do_accept (GSocketService  *service)
117 {
118   g_socket_listener_accept_async (G_SOCKET_LISTENER (service),
119                                   service->priv->cancellable,
120                                   g_socket_service_ready, NULL);
121   service->priv->outstanding_accept = TRUE;
122 }
123
124 static void
125 g_socket_service_changed (GSocketListener *listener)
126 {
127   GSocketService  *service = G_SOCKET_SERVICE (listener);
128
129   G_LOCK (active);
130
131   if (service->priv->active)
132     {
133       if (service->priv->outstanding_accept)
134         g_cancellable_cancel (service->priv->cancellable);
135       else
136         {
137           g_socket_listener_accept_async (listener, service->priv->cancellable,
138                                           g_socket_service_ready, NULL);
139           service->priv->outstanding_accept = TRUE;
140         }
141     }
142
143   G_UNLOCK (active);
144 }
145
146 /**
147  * g_socket_service_is_active:
148  * @service: a #GSocketService
149  *
150  * Check whether the service is active or not. An active
151  * service will accept new clients that connect, while
152  * a non-active service will let connecting clients queue
153  * up until the service is started.
154  *
155  * Returns: %TRUE if the service is active, %FALSE otherwise
156  *
157  * Since: 2.22
158  */
159 gboolean
160 g_socket_service_is_active (GSocketService *service)
161 {
162   gboolean active;
163
164   G_LOCK (active);
165   active = service->priv->active;
166   G_UNLOCK (active);
167   return active;
168 }
169
170 /**
171  * g_socket_service_start:
172  * @service: a #GSocketService
173  *
174  * Starts the service, i.e. start accepting connections
175  * from the added sockets when the mainloop runs.
176  *
177  * This call is thread-safe, so it may be called from a thread
178  * handling an incoming client request.
179  *
180  * Since: 2.22
181  */
182 void
183 g_socket_service_start (GSocketService *service)
184 {
185   G_LOCK (active);
186
187   if (!service->priv->active)
188     {
189       service->priv->active = TRUE;
190
191       if (service->priv->outstanding_accept)
192         g_cancellable_cancel (service->priv->cancellable);
193       else
194         do_accept (service);
195     }
196
197   G_UNLOCK (active);
198 }
199
200 /**
201  * g_socket_service_stop:
202  * @service: a #GSocketService
203  *
204  * Stops the service, i.e. stops accepting connections
205  * from the added sockets when the mainloop runs.
206  *
207  * This call is thread-safe, so it may be called from a thread
208  * handling an incoming client request.
209  *
210  * Since: 2.22
211  */
212 void
213 g_socket_service_stop (GSocketService *service)
214 {
215   G_LOCK (active);
216
217   if (service->priv->active)
218     {
219       service->priv->active = FALSE;
220
221       if (service->priv->outstanding_accept)
222         g_cancellable_cancel (service->priv->cancellable);
223     }
224
225   G_UNLOCK (active);
226 }
227
228
229 static gboolean
230 g_socket_service_incoming (GSocketService    *service,
231                            GSocketConnection *connection,
232                            GObject           *source_object)
233 {
234   gboolean result;
235
236   g_signal_emit (service, g_socket_service_incoming_signal,
237                  0, connection, source_object, &result);
238   return result;
239 }
240
241 static void
242 g_socket_service_class_init (GSocketServiceClass *class)
243 {
244   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
245   GSocketListenerClass *listener_class = G_SOCKET_LISTENER_CLASS (class);
246
247   g_type_class_add_private (class, sizeof (GSocketServicePrivate));
248
249   gobject_class->finalize = g_socket_service_finalize;
250   listener_class->changed = g_socket_service_changed;
251   class->incoming = g_socket_service_real_incoming;
252
253   /**
254    * GSocketService::incoming:
255    * @service: the #GSocketService
256    * @connection: a new #GSocketConnection object
257    * @source_object: (allow-none): the source_object passed to
258    *     g_socket_listener_add_address()
259    *
260    * The ::incoming signal is emitted when a new incoming connection
261    * to @service needs to be handled. The handler must initiate the
262    * handling of @connection, but may not block; in essence,
263    * asynchronous operations must be used.
264    *
265    * @connection will be unreffed once the signal handler returns,
266    * so you need to ref it yourself if you are planning to use it.
267    *
268    * Returns: %TRUE to stop other handlers from being called
269    *
270    * Since: 2.22
271    */
272   g_socket_service_incoming_signal =
273     g_signal_new ("incoming", G_TYPE_FROM_CLASS (class), G_SIGNAL_RUN_LAST,
274                   G_STRUCT_OFFSET (GSocketServiceClass, incoming),
275                   g_signal_accumulator_true_handled, NULL,
276                   NULL, G_TYPE_BOOLEAN,
277                   2, G_TYPE_SOCKET_CONNECTION, G_TYPE_OBJECT);
278 }
279
280 static void
281 g_socket_service_ready (GObject      *object,
282                         GAsyncResult *result,
283                         gpointer      user_data)
284 {
285   GSocketListener *listener = G_SOCKET_LISTENER (object);
286   GSocketService *service = G_SOCKET_SERVICE (object);
287   GSocketConnection *connection;
288   GObject *source_object;
289   GError *error = NULL;
290
291   connection = g_socket_listener_accept_finish (listener, result, &source_object, &error);
292   if (error)
293     {
294       if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
295         g_warning ("fail: %s", error->message);
296       g_error_free (error);
297     }
298   else
299     {
300       g_socket_service_incoming (service, connection, source_object);
301       g_object_unref (connection);
302     }
303
304   G_LOCK (active);
305
306   g_cancellable_reset (service->priv->cancellable);
307
308   /* requeue */
309   service->priv->outstanding_accept = FALSE;
310   if (service->priv->active)
311     do_accept (service);
312
313   G_UNLOCK (active);
314 }
315
316
317 /**
318  * g_socket_service_new:
319  *
320  * Creates a new #GSocketService with no sockets to listen for.
321  * New listeners can be added with e.g. g_socket_listener_add_address()
322  * or g_socket_listener_add_inet_port().
323  *
324  * Returns: a new #GSocketService.
325  *
326  * Since: 2.22
327  */
328 GSocketService *
329 g_socket_service_new (void)
330 {
331   return g_object_new (G_TYPE_SOCKET_SERVICE, NULL);
332 }