Fix up includes in section docs
[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 much 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   if (G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize)
107     (*G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize) (object);
108 }
109
110 static void
111 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
112 {
113   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
115   
116   g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
117   
118   gobject_class->finalize = g_unix_input_stream_finalize;
119
120   stream_class->read_fn = g_unix_input_stream_read;
121   stream_class->close_fn = g_unix_input_stream_close;
122   stream_class->read_async = g_unix_input_stream_read_async;
123   stream_class->read_finish = g_unix_input_stream_read_finish;
124   if (0)
125     {
126       /* TODO: Implement instead of using fallbacks */
127       stream_class->skip_async = g_unix_input_stream_skip_async;
128       stream_class->skip_finish = g_unix_input_stream_skip_finish;
129     }
130   stream_class->close_async = g_unix_input_stream_close_async;
131   stream_class->close_finish = g_unix_input_stream_close_finish;
132 }
133
134 static void
135 g_unix_input_stream_init (GUnixInputStream *unix_stream)
136 {
137   unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
138                                                    G_TYPE_UNIX_INPUT_STREAM,
139                                                    GUnixInputStreamPrivate);
140 }
141
142 /**
143  * g_unix_input_stream_new:
144  * @fd: unix file descriptor.
145  * @close_fd_at_close: a #gboolean.
146  * 
147  * Creates a new #GUnixInputStream for the given @fd. If @close_fd_at_close
148  * is %TRUE, the file descriptor will be closed when the stream is closed.
149  * 
150  * Returns: a #GUnixInputStream. 
151  **/
152 GInputStream *
153 g_unix_input_stream_new (int      fd,
154                          gboolean close_fd_at_close)
155 {
156   GUnixInputStream *stream;
157
158   g_return_val_if_fail (fd != -1, NULL);
159
160   stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM, NULL);
161
162   stream->priv->fd = fd;
163   stream->priv->close_fd_at_close = close_fd_at_close;
164   
165   return G_INPUT_STREAM (stream);
166 }
167
168 static gssize
169 g_unix_input_stream_read (GInputStream  *stream,
170                           void          *buffer,
171                           gsize          count,
172                           GCancellable  *cancellable,
173                           GError       **error)
174 {
175   GUnixInputStream *unix_stream;
176   gssize res;
177   struct pollfd poll_fds[2];
178   int poll_ret;
179   int cancel_fd;
180
181   unix_stream = G_UNIX_INPUT_STREAM (stream);
182
183   cancel_fd = g_cancellable_get_fd (cancellable);
184   if (cancel_fd != -1)
185     {
186       do
187         {
188           poll_fds[0].events = POLLIN;
189           poll_fds[0].fd = unix_stream->priv->fd;
190           poll_fds[1].events = POLLIN;
191           poll_fds[1].fd = cancel_fd;
192           poll_ret = poll (poll_fds, 2, -1);
193         }
194       while (poll_ret == -1 && errno == EINTR);
195       
196       if (poll_ret == -1)
197         {
198           g_set_error (error, G_IO_ERROR,
199                        g_io_error_from_errno (errno),
200                        _("Error reading from unix: %s"),
201                        g_strerror (errno));
202           return -1;
203         }
204     }
205
206   while (1)
207     {
208       if (g_cancellable_set_error_if_cancelled (cancellable, error))
209         break;
210       res = read (unix_stream->priv->fd, buffer, count);
211       if (res == -1)
212         {
213           if (errno == EINTR)
214             continue;
215           
216           g_set_error (error, G_IO_ERROR,
217                        g_io_error_from_errno (errno),
218                        _("Error reading from unix: %s"),
219                        g_strerror (errno));
220         }
221       
222       break;
223     }
224
225   return res;
226 }
227
228 static gboolean
229 g_unix_input_stream_close (GInputStream  *stream,
230                            GCancellable  *cancellable,
231                            GError       **error)
232 {
233   GUnixInputStream *unix_stream;
234   int res;
235
236   unix_stream = G_UNIX_INPUT_STREAM (stream);
237
238   if (!unix_stream->priv->close_fd_at_close)
239     return TRUE;
240   
241   while (1)
242     {
243       /* This might block during the close. Doesn't seem to be a way to avoid it though. */
244       res = close (unix_stream->priv->fd);
245       if (res == -1)
246         g_set_error (error, G_IO_ERROR,
247                      g_io_error_from_errno (errno),
248                      _("Error closing unix: %s"),
249                      g_strerror (errno));
250       break;
251     }
252   
253   return res != -1;
254 }
255
256 typedef struct {
257   gsize count;
258   void *buffer;
259   GAsyncReadyCallback callback;
260   gpointer user_data;
261   GCancellable *cancellable;
262   GUnixInputStream *stream;
263 } ReadAsyncData;
264
265 static gboolean
266 read_async_cb (ReadAsyncData *data,
267                GIOCondition   condition,
268                int            fd)
269 {
270   GSimpleAsyncResult *simple;
271   GError *error = NULL;
272   gssize count_read;
273
274   /* We know that we can read from fd once without blocking */
275   while (1)
276     {
277       if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
278         {
279           count_read = -1;
280           break;
281         }
282       count_read = read (data->stream->priv->fd, data->buffer, data->count);
283       if (count_read == -1)
284         {
285           if (errno == EINTR)
286             continue;
287           
288           g_set_error (&error, G_IO_ERROR,
289                        g_io_error_from_errno (errno),
290                        _("Error reading from unix: %s"),
291                        g_strerror (errno));
292         }
293       break;
294     }
295
296   simple = g_simple_async_result_new (G_OBJECT (data->stream),
297                                       data->callback,
298                                       data->user_data,
299                                       g_unix_input_stream_read_async);
300
301   g_simple_async_result_set_op_res_gssize (simple, count_read);
302
303   if (count_read == -1)
304     {
305       g_simple_async_result_set_from_error (simple, error);
306       g_error_free (error);
307     }
308
309   /* Complete immediately, not in idle, since we're already in a mainloop callout */
310   g_simple_async_result_complete (simple);
311   g_object_unref (simple);
312
313   return FALSE;
314 }
315
316 static void
317 g_unix_input_stream_read_async (GInputStream        *stream,
318                                 void                *buffer,
319                                 gsize                count,
320                                 int                  io_priority,
321                                 GCancellable        *cancellable,
322                                 GAsyncReadyCallback  callback,
323                                 gpointer             user_data)
324 {
325   GSource *source;
326   GUnixInputStream *unix_stream;
327   ReadAsyncData *data;
328
329   unix_stream = G_UNIX_INPUT_STREAM (stream);
330
331   data = g_new0 (ReadAsyncData, 1);
332   data->count = count;
333   data->buffer = buffer;
334   data->callback = callback;
335   data->user_data = user_data;
336   data->cancellable = cancellable;
337   data->stream = unix_stream;
338
339   source = _g_fd_source_new (unix_stream->priv->fd,
340                              POLLIN,
341                              cancellable);
342   
343   g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
344   g_source_attach (source, NULL);
345  
346   g_source_unref (source);
347 }
348
349 static gssize
350 g_unix_input_stream_read_finish (GInputStream  *stream,
351                                  GAsyncResult  *result,
352                                  GError       **error)
353 {
354   GSimpleAsyncResult *simple;
355   gssize nread;
356
357   simple = G_SIMPLE_ASYNC_RESULT (result);
358   g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
359   
360   nread = g_simple_async_result_get_op_res_gssize (simple);
361   return nread;
362 }
363
364 static void
365 g_unix_input_stream_skip_async (GInputStream        *stream,
366                                 gsize                count,
367                                 int                  io_priority,
368                                 GCancellable        *cancellable,
369                                 GAsyncReadyCallback  callback,
370                                 gpointer             data)
371 {
372   g_warn_if_reached ();
373   /* TODO: Not implemented */
374 }
375
376 static gssize
377 g_unix_input_stream_skip_finish  (GInputStream  *stream,
378                                   GAsyncResult  *result,
379                                   GError       **error)
380 {
381   g_warn_if_reached ();
382   /* TODO: Not implemented */
383 }
384
385
386 typedef struct {
387   GInputStream *stream;
388   GAsyncReadyCallback callback;
389   gpointer user_data;
390 } CloseAsyncData;
391
392 static void
393 close_async_data_free (gpointer _data)
394 {
395   CloseAsyncData *data = _data;
396
397   g_free (data);
398 }
399
400 static gboolean
401 close_async_cb (CloseAsyncData *data)
402 {
403   GUnixInputStream *unix_stream;
404   GSimpleAsyncResult *simple;
405   GError *error = NULL;
406   gboolean result;
407   int res;
408
409   unix_stream = G_UNIX_INPUT_STREAM (data->stream);
410
411   if (!unix_stream->priv->close_fd_at_close)
412     {
413       result = TRUE;
414       goto out;
415     }
416   
417   while (1)
418     {
419       res = close (unix_stream->priv->fd);
420       if (res == -1)
421         g_set_error (&error, G_IO_ERROR,
422                      g_io_error_from_errno (errno),
423                      _("Error closing unix: %s"),
424                      g_strerror (errno));
425       break;
426     }
427   
428   result = res != -1;
429
430  out:
431   simple = g_simple_async_result_new (G_OBJECT (data->stream),
432                                       data->callback,
433                                       data->user_data,
434                                       g_unix_input_stream_close_async);
435
436   if (!result)
437     {
438       g_simple_async_result_set_from_error (simple, error);
439       g_error_free (error);
440     }
441
442   /* Complete immediately, not in idle, since we're already in a mainloop callout */
443   g_simple_async_result_complete (simple);
444   g_object_unref (simple);
445   
446   return FALSE;
447 }
448
449 static void
450 g_unix_input_stream_close_async (GInputStream        *stream,
451                                  int                  io_priority,
452                                  GCancellable        *cancellable,
453                                  GAsyncReadyCallback  callback,
454                                  gpointer             user_data)
455 {
456   GSource *idle;
457   CloseAsyncData *data;
458
459   data = g_new0 (CloseAsyncData, 1);
460
461   data->stream = stream;
462   data->callback = callback;
463   data->user_data = user_data;
464   
465   idle = g_idle_source_new ();
466   g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
467   g_source_attach (idle, NULL);
468   g_source_unref (idle);
469 }
470
471 static gboolean
472 g_unix_input_stream_close_finish (GInputStream  *stream,
473                                   GAsyncResult  *result,
474                                   GError       **error)
475 {
476   /* Failures handled in generic close_finish code */
477   return TRUE;
478 }
479
480 #define __G_UNIX_INPUT_STREAM_C__
481 #include "gioaliasdef.c"