chain up unconditionally in finalize() and dispose(). Also don't
[platform/upstream/glib.git] / gio / gunixinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  * 
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22
23 #include <config.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <poll.h>
32
33 #include <glib.h>
34 #include <glib/gstdio.h>
35 #include "gioerror.h"
36 #include "gsimpleasyncresult.h"
37 #include "gunixinputstream.h"
38 #include "gcancellable.h"
39 #include "gasynchelper.h"
40 #include "glibintl.h"
41
42 #include "gioalias.h"
43
44 /**
45  * SECTION:gunixinputstream
46  * @short_description: Streaming input operations for Unix file descriptors
47  * @include: gio/gunixinputstream.h
48  * @see_also: #GInputStream
49  *
50  * #GUnixInputStream implements #GInputStream for reading from a
51  * unix file descriptor, including asynchronous operations. The file
52  * descriptor must be selectable, so it doesn't work with opened files.
53  **/
54
55 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
56
57 struct _GUnixInputStreamPrivate {
58   int fd;
59   gboolean close_fd_at_close;
60 };
61
62 static gssize   g_unix_input_stream_read         (GInputStream         *stream,
63                                                   void                 *buffer,
64                                                   gsize                 count,
65                                                   GCancellable         *cancellable,
66                                                   GError              **error);
67 static gboolean g_unix_input_stream_close        (GInputStream         *stream,
68                                                   GCancellable         *cancellable,
69                                                   GError              **error);
70 static void     g_unix_input_stream_read_async   (GInputStream         *stream,
71                                                   void                 *buffer,
72                                                   gsize                 count,
73                                                   int                   io_priority,
74                                                   GCancellable         *cancellable,
75                                                   GAsyncReadyCallback   callback,
76                                                   gpointer              data);
77 static gssize   g_unix_input_stream_read_finish  (GInputStream         *stream,
78                                                   GAsyncResult         *result,
79                                                   GError              **error);
80 static void     g_unix_input_stream_skip_async   (GInputStream         *stream,
81                                                   gsize                 count,
82                                                   int                   io_priority,
83                                                   GCancellable         *cancellable,
84                                                   GAsyncReadyCallback   callback,
85                                                   gpointer              data);
86 static gssize   g_unix_input_stream_skip_finish  (GInputStream         *stream,
87                                                   GAsyncResult         *result,
88                                                   GError              **error);
89 static void     g_unix_input_stream_close_async  (GInputStream         *stream,
90                                                   int                   io_priority,
91                                                   GCancellable         *cancellable,
92                                                   GAsyncReadyCallback   callback,
93                                                   gpointer              data);
94 static gboolean g_unix_input_stream_close_finish (GInputStream         *stream,
95                                                   GAsyncResult         *result,
96                                                   GError              **error);
97
98
99 static void
100 g_unix_input_stream_finalize (GObject *object)
101 {
102   GUnixInputStream *stream;
103   
104   stream = G_UNIX_INPUT_STREAM (object);
105
106   G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
107 }
108
109 static void
110 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
111 {
112   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
114   
115   g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
116   
117   gobject_class->finalize = g_unix_input_stream_finalize;
118
119   stream_class->read_fn = g_unix_input_stream_read;
120   stream_class->close_fn = g_unix_input_stream_close;
121   stream_class->read_async = g_unix_input_stream_read_async;
122   stream_class->read_finish = g_unix_input_stream_read_finish;
123   if (0)
124     {
125       /* TODO: Implement instead of using fallbacks */
126       stream_class->skip_async = g_unix_input_stream_skip_async;
127       stream_class->skip_finish = g_unix_input_stream_skip_finish;
128     }
129   stream_class->close_async = g_unix_input_stream_close_async;
130   stream_class->close_finish = g_unix_input_stream_close_finish;
131 }
132
133 static void
134 g_unix_input_stream_init (GUnixInputStream *unix_stream)
135 {
136   unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
137                                                    G_TYPE_UNIX_INPUT_STREAM,
138                                                    GUnixInputStreamPrivate);
139 }
140
141 /**
142  * g_unix_input_stream_new:
143  * @fd: unix file descriptor.
144  * @close_fd_at_close: a #gboolean.
145  * 
146  * Creates a new #GUnixInputStream for the given @fd. If @close_fd_at_close
147  * is %TRUE, the file descriptor will be closed when the stream is closed.
148  * 
149  * Returns: a #GUnixInputStream. 
150  **/
151 GInputStream *
152 g_unix_input_stream_new (int      fd,
153                          gboolean close_fd_at_close)
154 {
155   GUnixInputStream *stream;
156
157   g_return_val_if_fail (fd != -1, NULL);
158
159   stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM, NULL);
160
161   stream->priv->fd = fd;
162   stream->priv->close_fd_at_close = close_fd_at_close;
163   
164   return G_INPUT_STREAM (stream);
165 }
166
167 static gssize
168 g_unix_input_stream_read (GInputStream  *stream,
169                           void          *buffer,
170                           gsize          count,
171                           GCancellable  *cancellable,
172                           GError       **error)
173 {
174   GUnixInputStream *unix_stream;
175   gssize res;
176   struct pollfd poll_fds[2];
177   int poll_ret;
178   int cancel_fd;
179
180   unix_stream = G_UNIX_INPUT_STREAM (stream);
181
182   cancel_fd = g_cancellable_get_fd (cancellable);
183   if (cancel_fd != -1)
184     {
185       do
186         {
187           poll_fds[0].events = POLLIN;
188           poll_fds[0].fd = unix_stream->priv->fd;
189           poll_fds[1].events = POLLIN;
190           poll_fds[1].fd = cancel_fd;
191           poll_ret = poll (poll_fds, 2, -1);
192         }
193       while (poll_ret == -1 && errno == EINTR);
194       
195       if (poll_ret == -1)
196         {
197           int errsv = errno;
198
199           g_set_error (error, G_IO_ERROR,
200                        g_io_error_from_errno (errsv),
201                        _("Error reading from unix: %s"),
202                        g_strerror (errsv));
203           return -1;
204         }
205     }
206
207   while (1)
208     {
209       if (g_cancellable_set_error_if_cancelled (cancellable, error))
210         break;
211       res = read (unix_stream->priv->fd, buffer, count);
212       if (res == -1)
213         {
214           int errsv = errno;
215
216           if (errsv == EINTR)
217             continue;
218           
219           g_set_error (error, G_IO_ERROR,
220                        g_io_error_from_errno (errsv),
221                        _("Error reading from unix: %s"),
222                        g_strerror (errsv));
223         }
224       
225       break;
226     }
227
228   return res;
229 }
230
231 static gboolean
232 g_unix_input_stream_close (GInputStream  *stream,
233                            GCancellable  *cancellable,
234                            GError       **error)
235 {
236   GUnixInputStream *unix_stream;
237   int res;
238
239   unix_stream = G_UNIX_INPUT_STREAM (stream);
240
241   if (!unix_stream->priv->close_fd_at_close)
242     return TRUE;
243   
244   while (1)
245     {
246       /* This might block during the close. Doesn't seem to be a way to avoid it though. */
247       res = close (unix_stream->priv->fd);
248       if (res == -1)
249         {
250           int errsv = errno;
251
252           g_set_error (error, G_IO_ERROR,
253                        g_io_error_from_errno (errsv),
254                        _("Error closing unix: %s"),
255                        g_strerror (errsv));
256         }
257       break;
258     }
259   
260   return res != -1;
261 }
262
263 typedef struct {
264   gsize count;
265   void *buffer;
266   GAsyncReadyCallback callback;
267   gpointer user_data;
268   GCancellable *cancellable;
269   GUnixInputStream *stream;
270 } ReadAsyncData;
271
272 static gboolean
273 read_async_cb (ReadAsyncData *data,
274                GIOCondition   condition,
275                int            fd)
276 {
277   GSimpleAsyncResult *simple;
278   GError *error = NULL;
279   gssize count_read;
280
281   /* We know that we can read from fd once without blocking */
282   while (1)
283     {
284       if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
285         {
286           count_read = -1;
287           break;
288         }
289       count_read = read (data->stream->priv->fd, data->buffer, data->count);
290       if (count_read == -1)
291         {
292           int errsv = errno;
293
294           if (errsv == EINTR)
295             continue;
296           
297           g_set_error (&error, G_IO_ERROR,
298                        g_io_error_from_errno (errsv),
299                        _("Error reading from unix: %s"),
300                        g_strerror (errsv));
301         }
302       break;
303     }
304
305   simple = g_simple_async_result_new (G_OBJECT (data->stream),
306                                       data->callback,
307                                       data->user_data,
308                                       g_unix_input_stream_read_async);
309
310   g_simple_async_result_set_op_res_gssize (simple, count_read);
311
312   if (count_read == -1)
313     {
314       g_simple_async_result_set_from_error (simple, error);
315       g_error_free (error);
316     }
317
318   /* Complete immediately, not in idle, since we're already in a mainloop callout */
319   g_simple_async_result_complete (simple);
320   g_object_unref (simple);
321
322   return FALSE;
323 }
324
325 static void
326 g_unix_input_stream_read_async (GInputStream        *stream,
327                                 void                *buffer,
328                                 gsize                count,
329                                 int                  io_priority,
330                                 GCancellable        *cancellable,
331                                 GAsyncReadyCallback  callback,
332                                 gpointer             user_data)
333 {
334   GSource *source;
335   GUnixInputStream *unix_stream;
336   ReadAsyncData *data;
337
338   unix_stream = G_UNIX_INPUT_STREAM (stream);
339
340   data = g_new0 (ReadAsyncData, 1);
341   data->count = count;
342   data->buffer = buffer;
343   data->callback = callback;
344   data->user_data = user_data;
345   data->cancellable = cancellable;
346   data->stream = unix_stream;
347
348   source = _g_fd_source_new (unix_stream->priv->fd,
349                              POLLIN,
350                              cancellable);
351   
352   g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
353   g_source_attach (source, NULL);
354  
355   g_source_unref (source);
356 }
357
358 static gssize
359 g_unix_input_stream_read_finish (GInputStream  *stream,
360                                  GAsyncResult  *result,
361                                  GError       **error)
362 {
363   GSimpleAsyncResult *simple;
364   gssize nread;
365
366   simple = G_SIMPLE_ASYNC_RESULT (result);
367   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
368   
369   nread = g_simple_async_result_get_op_res_gssize (simple);
370   return nread;
371 }
372
373 static void
374 g_unix_input_stream_skip_async (GInputStream        *stream,
375                                 gsize                count,
376                                 int                  io_priority,
377                                 GCancellable        *cancellable,
378                                 GAsyncReadyCallback  callback,
379                                 gpointer             data)
380 {
381   g_warn_if_reached ();
382   /* TODO: Not implemented */
383 }
384
385 static gssize
386 g_unix_input_stream_skip_finish  (GInputStream  *stream,
387                                   GAsyncResult  *result,
388                                   GError       **error)
389 {
390   g_warn_if_reached ();
391   return 0;
392   /* TODO: Not implemented */
393 }
394
395
396 typedef struct {
397   GInputStream *stream;
398   GAsyncReadyCallback callback;
399   gpointer user_data;
400 } CloseAsyncData;
401
402 static void
403 close_async_data_free (gpointer _data)
404 {
405   CloseAsyncData *data = _data;
406
407   g_free (data);
408 }
409
410 static gboolean
411 close_async_cb (CloseAsyncData *data)
412 {
413   GUnixInputStream *unix_stream;
414   GSimpleAsyncResult *simple;
415   GError *error = NULL;
416   gboolean result;
417   int res;
418
419   unix_stream = G_UNIX_INPUT_STREAM (data->stream);
420
421   if (!unix_stream->priv->close_fd_at_close)
422     {
423       result = TRUE;
424       goto out;
425     }
426   
427   while (1)
428     {
429       res = close (unix_stream->priv->fd);
430       if (res == -1)
431         {
432           int errsv = errno;
433
434           g_set_error (&error, G_IO_ERROR,
435                        g_io_error_from_errno (errsv),
436                        _("Error closing unix: %s"),
437                        g_strerror (errsv));
438         }
439       break;
440     }
441   
442   result = res != -1;
443
444  out:
445   simple = g_simple_async_result_new (G_OBJECT (data->stream),
446                                       data->callback,
447                                       data->user_data,
448                                       g_unix_input_stream_close_async);
449
450   if (!result)
451     {
452       g_simple_async_result_set_from_error (simple, error);
453       g_error_free (error);
454     }
455
456   /* Complete immediately, not in idle, since we're already in a mainloop callout */
457   g_simple_async_result_complete (simple);
458   g_object_unref (simple);
459   
460   return FALSE;
461 }
462
463 static void
464 g_unix_input_stream_close_async (GInputStream        *stream,
465                                  int                  io_priority,
466                                  GCancellable        *cancellable,
467                                  GAsyncReadyCallback  callback,
468                                  gpointer             user_data)
469 {
470   GSource *idle;
471   CloseAsyncData *data;
472
473   data = g_new0 (CloseAsyncData, 1);
474
475   data->stream = stream;
476   data->callback = callback;
477   data->user_data = user_data;
478   
479   idle = g_idle_source_new ();
480   g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
481   g_source_attach (idle, NULL);
482   g_source_unref (idle);
483 }
484
485 static gboolean
486 g_unix_input_stream_close_finish (GInputStream  *stream,
487                                   GAsyncResult  *result,
488                                   GError       **error)
489 {
490   /* Failures handled in generic close_finish code */
491   return TRUE;
492 }
493
494 #define __G_UNIX_INPUT_STREAM_C__
495 #include "gioaliasdef.c"