Rename the generated private data getter function
[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
32 #include <glib.h>
33 #include <glib/gstdio.h>
34 #include "gioerror.h"
35 #include "gsimpleasyncresult.h"
36 #include "gunixinputstream.h"
37 #include "gcancellable.h"
38 #include "gasynchelper.h"
39 #include "gfiledescriptorbased.h"
40 #include "glibintl.h"
41
42
43 /**
44  * SECTION:gunixinputstream
45  * @short_description: Streaming input operations for UNIX file descriptors
46  * @include: gio/gunixinputstream.h
47  * @see_also: #GInputStream
48  *
49  * #GUnixInputStream implements #GInputStream for reading from a UNIX
50  * file descriptor, including asynchronous operations. (If the file
51  * descriptor refers to a socket or pipe, this will use poll() to do
52  * asynchronous I/O. If it refers to a regular file, it will fall back
53  * to doing asynchronous I/O in another thread.)
54  *
55  * Note that <filename>&lt;gio/gunixinputstream.h&gt;</filename> belongs
56  * to the UNIX-specific GIO interfaces, thus you have to use the
57  * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
58  */
59
60 enum {
61   PROP_0,
62   PROP_FD,
63   PROP_CLOSE_FD
64 };
65
66 struct _GUnixInputStreamPrivate {
67   int fd;
68   guint close_fd : 1;
69   guint is_pipe_or_socket : 1;
70 };
71
72 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
73 static void g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
74
75 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM,
76                          G_ADD_PRIVATE (GUnixInputStream)
77                          G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
78                                                 g_unix_input_stream_pollable_iface_init)
79                          G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
80                                                 g_unix_input_stream_file_descriptor_based_iface_init)
81                          )
82
83 static void     g_unix_input_stream_set_property (GObject              *object,
84                                                   guint                 prop_id,
85                                                   const GValue         *value,
86                                                   GParamSpec           *pspec);
87 static void     g_unix_input_stream_get_property (GObject              *object,
88                                                   guint                 prop_id,
89                                                   GValue               *value,
90                                                   GParamSpec           *pspec);
91 static gssize   g_unix_input_stream_read         (GInputStream         *stream,
92                                                   void                 *buffer,
93                                                   gsize                 count,
94                                                   GCancellable         *cancellable,
95                                                   GError              **error);
96 static gboolean g_unix_input_stream_close        (GInputStream         *stream,
97                                                   GCancellable         *cancellable,
98                                                   GError              **error);
99 static void     g_unix_input_stream_skip_async   (GInputStream         *stream,
100                                                   gsize                 count,
101                                                   int                   io_priority,
102                                                   GCancellable         *cancellable,
103                                                   GAsyncReadyCallback   callback,
104                                                   gpointer              data);
105 static gssize   g_unix_input_stream_skip_finish  (GInputStream         *stream,
106                                                   GAsyncResult         *result,
107                                                   GError              **error);
108 static void     g_unix_input_stream_close_async  (GInputStream         *stream,
109                                                   int                   io_priority,
110                                                   GCancellable         *cancellable,
111                                                   GAsyncReadyCallback   callback,
112                                                   gpointer              data);
113 static gboolean g_unix_input_stream_close_finish (GInputStream         *stream,
114                                                   GAsyncResult         *result,
115                                                   GError              **error);
116
117 static gboolean g_unix_input_stream_pollable_can_poll      (GPollableInputStream *stream);
118 static gboolean g_unix_input_stream_pollable_is_readable   (GPollableInputStream *stream);
119 static GSource *g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
120                                                             GCancellable         *cancellable);
121
122 static void
123 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
124 {
125   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
127
128   gobject_class->get_property = g_unix_input_stream_get_property;
129   gobject_class->set_property = g_unix_input_stream_set_property;
130
131   stream_class->read_fn = g_unix_input_stream_read;
132   stream_class->close_fn = g_unix_input_stream_close;
133   if (0)
134     {
135       /* TODO: Implement instead of using fallbacks */
136       stream_class->skip_async = g_unix_input_stream_skip_async;
137       stream_class->skip_finish = g_unix_input_stream_skip_finish;
138     }
139   stream_class->close_async = g_unix_input_stream_close_async;
140   stream_class->close_finish = g_unix_input_stream_close_finish;
141
142   /**
143    * GUnixInputStream:fd:
144    *
145    * The file descriptor that the stream reads from.
146    *
147    * Since: 2.20
148    */
149   g_object_class_install_property (gobject_class,
150                                    PROP_FD,
151                                    g_param_spec_int ("fd",
152                                                      P_("File descriptor"),
153                                                      P_("The file descriptor to read from"),
154                                                      G_MININT, G_MAXINT, -1,
155                                                      G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
156
157   /**
158    * GUnixInputStream:close-fd:
159    *
160    * Whether to close the file descriptor when the stream is closed.
161    *
162    * Since: 2.20
163    */
164   g_object_class_install_property (gobject_class,
165                                    PROP_CLOSE_FD,
166                                    g_param_spec_boolean ("close-fd",
167                                                          P_("Close file descriptor"),
168                                                          P_("Whether to close the file descriptor when the stream is closed"),
169                                                          TRUE,
170                                                          G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
171 }
172
173 static void
174 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
175 {
176   iface->can_poll = g_unix_input_stream_pollable_can_poll;
177   iface->is_readable = g_unix_input_stream_pollable_is_readable;
178   iface->create_source = g_unix_input_stream_pollable_create_source;
179 }
180
181 static void
182 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
183 {
184   iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_input_stream_get_fd;
185 }
186
187 static void
188 g_unix_input_stream_set_property (GObject         *object,
189                                   guint            prop_id,
190                                   const GValue    *value,
191                                   GParamSpec      *pspec)
192 {
193   GUnixInputStream *unix_stream;
194   
195   unix_stream = G_UNIX_INPUT_STREAM (object);
196
197   switch (prop_id)
198     {
199     case PROP_FD:
200       unix_stream->priv->fd = g_value_get_int (value);
201       if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
202         unix_stream->priv->is_pipe_or_socket = TRUE;
203       else
204         unix_stream->priv->is_pipe_or_socket = FALSE;
205       break;
206     case PROP_CLOSE_FD:
207       unix_stream->priv->close_fd = g_value_get_boolean (value);
208       break;
209     default:
210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211       break;
212     }
213 }
214
215 static void
216 g_unix_input_stream_get_property (GObject    *object,
217                                   guint       prop_id,
218                                   GValue     *value,
219                                   GParamSpec *pspec)
220 {
221   GUnixInputStream *unix_stream;
222
223   unix_stream = G_UNIX_INPUT_STREAM (object);
224
225   switch (prop_id)
226     {
227     case PROP_FD:
228       g_value_set_int (value, unix_stream->priv->fd);
229       break;
230     case PROP_CLOSE_FD:
231       g_value_set_boolean (value, unix_stream->priv->close_fd);
232       break;
233     default:
234       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235     }
236 }
237
238 static void
239 g_unix_input_stream_init (GUnixInputStream *unix_stream)
240 {
241   unix_stream->priv = g_unix_input_stream_get_instance_private (unix_stream);
242   unix_stream->priv->fd = -1;
243   unix_stream->priv->close_fd = TRUE;
244 }
245
246 /**
247  * g_unix_input_stream_new:
248  * @fd: a UNIX file descriptor
249  * @close_fd: %TRUE to close the file descriptor when done
250  * 
251  * Creates a new #GUnixInputStream for the given @fd. 
252  *
253  * If @close_fd is %TRUE, the file descriptor will be closed 
254  * when the stream is closed.
255  * 
256  * Returns: a new #GUnixInputStream
257  **/
258 GInputStream *
259 g_unix_input_stream_new (gint     fd,
260                          gboolean close_fd)
261 {
262   GUnixInputStream *stream;
263
264   g_return_val_if_fail (fd != -1, NULL);
265
266   stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
267                          "fd", fd,
268                          "close-fd", close_fd,
269                          NULL);
270
271   return G_INPUT_STREAM (stream);
272 }
273
274 /**
275  * g_unix_input_stream_set_close_fd:
276  * @stream: a #GUnixInputStream
277  * @close_fd: %TRUE to close the file descriptor when done
278  *
279  * Sets whether the file descriptor of @stream shall be closed
280  * when the stream is closed.
281  *
282  * Since: 2.20
283  */
284 void
285 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
286                                   gboolean          close_fd)
287 {
288   g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
289
290   close_fd = close_fd != FALSE;
291   if (stream->priv->close_fd != close_fd)
292     {
293       stream->priv->close_fd = close_fd;
294       g_object_notify (G_OBJECT (stream), "close-fd");
295     }
296 }
297
298 /**
299  * g_unix_input_stream_get_close_fd:
300  * @stream: a #GUnixInputStream
301  *
302  * Returns whether the file descriptor of @stream will be
303  * closed when the stream is closed.
304  *
305  * Return value: %TRUE if the file descriptor is closed when done
306  *
307  * Since: 2.20
308  */
309 gboolean
310 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
311 {
312   g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
313
314   return stream->priv->close_fd;
315 }
316
317 /**
318  * g_unix_input_stream_get_fd:
319  * @stream: a #GUnixInputStream
320  *
321  * Return the UNIX file descriptor that the stream reads from.
322  *
323  * Return value: The file descriptor of @stream
324  *
325  * Since: 2.20
326  */
327 gint
328 g_unix_input_stream_get_fd (GUnixInputStream *stream)
329 {
330   g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
331   
332   return stream->priv->fd;
333 }
334
335 static gssize
336 g_unix_input_stream_read (GInputStream  *stream,
337                           void          *buffer,
338                           gsize          count,
339                           GCancellable  *cancellable,
340                           GError       **error)
341 {
342   GUnixInputStream *unix_stream;
343   gssize res = -1;
344   GPollFD poll_fds[2];
345   int nfds;
346   int poll_ret;
347
348   unix_stream = G_UNIX_INPUT_STREAM (stream);
349
350   poll_fds[0].fd = unix_stream->priv->fd;
351   poll_fds[0].events = G_IO_IN;
352   if (unix_stream->priv->is_pipe_or_socket &&
353       g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
354     nfds = 2;
355   else
356     nfds = 1;
357
358   while (1)
359     {
360       poll_fds[0].revents = poll_fds[1].revents = 0;
361       do
362         poll_ret = g_poll (poll_fds, nfds, -1);
363       while (poll_ret == -1 && errno == EINTR);
364
365       if (poll_ret == -1)
366         {
367           int errsv = errno;
368
369           g_set_error (error, G_IO_ERROR,
370                        g_io_error_from_errno (errsv),
371                        _("Error reading from file descriptor: %s"),
372                        g_strerror (errsv));
373           break;
374         }
375
376       if (g_cancellable_set_error_if_cancelled (cancellable, error))
377         break;
378
379       if (!poll_fds[0].revents)
380         continue;
381
382       res = read (unix_stream->priv->fd, buffer, count);
383       if (res == -1)
384         {
385           int errsv = errno;
386
387           if (errsv == EINTR || errsv == EAGAIN)
388             continue;
389
390           g_set_error (error, G_IO_ERROR,
391                        g_io_error_from_errno (errsv),
392                        _("Error reading from file descriptor: %s"),
393                        g_strerror (errsv));
394         }
395
396       break;
397     }
398
399   if (nfds == 2)
400     g_cancellable_release_fd (cancellable);
401   return res;
402 }
403
404 static gboolean
405 g_unix_input_stream_close (GInputStream  *stream,
406                            GCancellable  *cancellable,
407                            GError       **error)
408 {
409   GUnixInputStream *unix_stream;
410   int res;
411
412   unix_stream = G_UNIX_INPUT_STREAM (stream);
413
414   if (!unix_stream->priv->close_fd)
415     return TRUE;
416   
417   /* This might block during the close. Doesn't seem to be a way to avoid it though. */
418   res = close (unix_stream->priv->fd);
419   if (res == -1)
420     {
421       int errsv = errno;
422
423       g_set_error (error, G_IO_ERROR,
424                    g_io_error_from_errno (errsv),
425                    _("Error closing file descriptor: %s"),
426                    g_strerror (errsv));
427     }
428   
429   return res != -1;
430 }
431
432 static void
433 g_unix_input_stream_skip_async (GInputStream        *stream,
434                                 gsize                count,
435                                 int                  io_priority,
436                                 GCancellable        *cancellable,
437                                 GAsyncReadyCallback  callback,
438                                 gpointer             data)
439 {
440   g_warn_if_reached ();
441   /* TODO: Not implemented */
442 }
443
444 static gssize
445 g_unix_input_stream_skip_finish  (GInputStream  *stream,
446                                   GAsyncResult  *result,
447                                   GError       **error)
448 {
449   g_warn_if_reached ();
450   return 0;
451   /* TODO: Not implemented */
452 }
453
454 static void
455 g_unix_input_stream_close_async (GInputStream        *stream,
456                                  int                  io_priority,
457                                  GCancellable        *cancellable,
458                                  GAsyncReadyCallback  callback,
459                                  gpointer             user_data)
460 {
461   GTask *task;
462   GError *error = NULL;
463
464   task = g_task_new (stream, cancellable, callback, user_data);
465   g_task_set_priority (task, io_priority);
466
467   if (g_unix_input_stream_close (stream, cancellable, &error))
468     g_task_return_boolean (task, TRUE);
469   else
470     g_task_return_error (task, error);
471   g_object_unref (task);
472 }
473
474 static gboolean
475 g_unix_input_stream_close_finish (GInputStream  *stream,
476                                   GAsyncResult  *result,
477                                   GError       **error)
478 {
479   g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
480
481   return g_task_propagate_boolean (G_TASK (result), error);
482 }
483
484 static gboolean
485 g_unix_input_stream_pollable_can_poll (GPollableInputStream *stream)
486 {
487   return G_UNIX_INPUT_STREAM (stream)->priv->is_pipe_or_socket;
488 }
489
490 static gboolean
491 g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream)
492 {
493   GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
494   GPollFD poll_fd;
495   gint result;
496
497   poll_fd.fd = unix_stream->priv->fd;
498   poll_fd.events = G_IO_IN;
499   poll_fd.revents = 0;
500
501   do
502     result = g_poll (&poll_fd, 1, 0);
503   while (result == -1 && errno == EINTR);
504
505   return poll_fd.revents != 0;
506 }
507
508 static GSource *
509 g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
510                                             GCancellable         *cancellable)
511 {
512   GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
513   GSource *inner_source, *pollable_source;
514
515   pollable_source = g_pollable_source_new (G_OBJECT (stream));
516
517   inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_IN, cancellable);
518   g_source_set_dummy_callback (inner_source);
519   g_source_add_child_source (pollable_source, inner_source);
520   g_source_unref (inner_source);
521
522   return pollable_source;
523 }