1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
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.
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.
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.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
33 #include <glib/gstdio.h>
35 #include "gsimpleasyncresult.h"
36 #include "gunixinputstream.h"
37 #include "gcancellable.h"
38 #include "gasynchelper.h"
44 * SECTION:gunixinputstream
45 * @short_description: Streaming input operations for UNIX file descriptors
46 * @include: gio/gunixinputstream.h
47 * @see_also: #GInputStream
49 * #GUnixInputStream implements #GInputStream for reading from a
50 * UNIX file descriptor, including asynchronous operations. The file
51 * descriptor must be selectable, so it doesn't work with opened files.
60 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
62 struct _GUnixInputStreamPrivate {
67 static void g_unix_input_stream_set_property (GObject *object,
71 static void g_unix_input_stream_get_property (GObject *object,
75 static gssize g_unix_input_stream_read (GInputStream *stream,
78 GCancellable *cancellable,
80 static gboolean g_unix_input_stream_close (GInputStream *stream,
81 GCancellable *cancellable,
83 static void g_unix_input_stream_read_async (GInputStream *stream,
87 GCancellable *cancellable,
88 GAsyncReadyCallback callback,
90 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
93 static void g_unix_input_stream_skip_async (GInputStream *stream,
96 GCancellable *cancellable,
97 GAsyncReadyCallback callback,
99 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
100 GAsyncResult *result,
102 static void g_unix_input_stream_close_async (GInputStream *stream,
104 GCancellable *cancellable,
105 GAsyncReadyCallback callback,
107 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
108 GAsyncResult *result,
113 g_unix_input_stream_finalize (GObject *object)
115 GUnixInputStream *stream;
117 stream = G_UNIX_INPUT_STREAM (object);
119 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
123 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
125 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
126 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
128 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
130 gobject_class->get_property = g_unix_input_stream_get_property;
131 gobject_class->set_property = g_unix_input_stream_set_property;
132 gobject_class->finalize = g_unix_input_stream_finalize;
134 stream_class->read_fn = g_unix_input_stream_read;
135 stream_class->close_fn = g_unix_input_stream_close;
136 stream_class->read_async = g_unix_input_stream_read_async;
137 stream_class->read_finish = g_unix_input_stream_read_finish;
140 /* TODO: Implement instead of using fallbacks */
141 stream_class->skip_async = g_unix_input_stream_skip_async;
142 stream_class->skip_finish = g_unix_input_stream_skip_finish;
144 stream_class->close_async = g_unix_input_stream_close_async;
145 stream_class->close_finish = g_unix_input_stream_close_finish;
148 * GUnixInputStream:fd:
150 * The file descriptor that the stream reads from.
154 g_object_class_install_property (gobject_class,
156 g_param_spec_int ("fd",
157 _("File descriptor"),
158 _("The file descriptor to read from"),
159 G_MININT, G_MAXINT, -1,
160 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
163 * GUnixInputStream:close-fd:
165 * Whether to close the file descriptor when the stream is closed.
169 g_object_class_install_property (gobject_class,
171 g_param_spec_boolean ("close-fd",
172 _("Close file descriptor"),
173 _("Whether to close the file descriptor when the stream is closed"),
175 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
179 g_unix_input_stream_set_property (GObject *object,
184 GUnixInputStream *unix_stream;
186 unix_stream = G_UNIX_INPUT_STREAM (object);
191 unix_stream->priv->fd = g_value_get_int (value);
194 unix_stream->priv->close_fd = g_value_get_boolean (value);
197 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203 g_unix_input_stream_get_property (GObject *object,
208 GUnixInputStream *unix_stream;
210 unix_stream = G_UNIX_INPUT_STREAM (object);
215 g_value_set_int (value, unix_stream->priv->fd);
218 g_value_set_boolean (value, unix_stream->priv->close_fd);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 g_unix_input_stream_init (GUnixInputStream *unix_stream)
228 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
229 G_TYPE_UNIX_INPUT_STREAM,
230 GUnixInputStreamPrivate);
232 unix_stream->priv->fd = -1;
233 unix_stream->priv->close_fd = TRUE;
237 * g_unix_input_stream_new:
238 * @fd: a UNIX file descriptor
239 * @close_fd: %TRUE to close the file descriptor when done
241 * Creates a new #GUnixInputStream for the given @fd.
243 * If @close_fd is %TRUE, the file descriptor will be closed
244 * when the stream is closed.
246 * Returns: a new #GUnixInputStream
249 g_unix_input_stream_new (gint fd,
252 GUnixInputStream *stream;
254 g_return_val_if_fail (fd != -1, NULL);
256 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
258 "close-fd", close_fd,
261 return G_INPUT_STREAM (stream);
265 * g_unix_input_stream_set_close_fd:
266 * @stream: a #GUnixInputStream
267 * @close_fd: %TRUE to close the file descriptor when done
269 * Sets whether the file descriptor of @stream shall be closed
270 * when the stream is closed.
275 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
278 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
280 close_fd = close_fd != FALSE;
281 if (stream->priv->close_fd != close_fd)
283 stream->priv->close_fd = close_fd;
284 g_object_notify (G_OBJECT (stream), "close-fd");
289 * g_unix_input_stream_get_close_fd:
290 * @stream: a #GUnixInputStream
292 * Returns whether the file descriptor of @stream will be
293 * closed when the stream is closed.
295 * Return value: %TRUE if the file descriptor is closed when done
300 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
302 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
304 return stream->priv->close_fd;
308 * g_unix_input_stream_get_fd:
309 * @stream: a #GUnixInputStream
311 * Return the UNIX file descriptor that the stream reads from.
313 * Return value: The file descriptor of @stream
318 g_unix_input_stream_get_fd (GUnixInputStream *stream)
320 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
322 return stream->priv->fd;
326 g_unix_input_stream_read (GInputStream *stream,
329 GCancellable *cancellable,
332 GUnixInputStream *unix_stream;
337 unix_stream = G_UNIX_INPUT_STREAM (stream);
341 poll_fds[0].fd = unix_stream->priv->fd;
342 poll_fds[0].events = G_IO_IN;
343 g_cancellable_make_pollfd (cancellable, &poll_fds[1]);
345 poll_ret = g_poll (poll_fds, 2, -1);
346 while (poll_ret == -1 && errno == EINTR);
352 g_set_error (error, G_IO_ERROR,
353 g_io_error_from_errno (errsv),
354 _("Error reading from unix: %s"),
362 if (g_cancellable_set_error_if_cancelled (cancellable, error))
364 res = read (unix_stream->priv->fd, buffer, count);
372 g_set_error (error, G_IO_ERROR,
373 g_io_error_from_errno (errsv),
374 _("Error reading from unix: %s"),
385 g_unix_input_stream_close (GInputStream *stream,
386 GCancellable *cancellable,
389 GUnixInputStream *unix_stream;
392 unix_stream = G_UNIX_INPUT_STREAM (stream);
394 if (!unix_stream->priv->close_fd)
399 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
400 res = close (unix_stream->priv->fd);
405 g_set_error (error, G_IO_ERROR,
406 g_io_error_from_errno (errsv),
407 _("Error closing unix: %s"),
419 GAsyncReadyCallback callback;
421 GCancellable *cancellable;
422 GUnixInputStream *stream;
426 read_async_cb (ReadAsyncData *data,
427 GIOCondition condition,
430 GSimpleAsyncResult *simple;
431 GError *error = NULL;
434 /* We know that we can read from fd once without blocking */
437 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
442 count_read = read (data->stream->priv->fd, data->buffer, data->count);
443 if (count_read == -1)
450 g_set_error (&error, G_IO_ERROR,
451 g_io_error_from_errno (errsv),
452 _("Error reading from unix: %s"),
458 simple = g_simple_async_result_new (G_OBJECT (data->stream),
461 g_unix_input_stream_read_async);
463 g_simple_async_result_set_op_res_gssize (simple, count_read);
465 if (count_read == -1)
467 g_simple_async_result_set_from_error (simple, error);
468 g_error_free (error);
471 /* Complete immediately, not in idle, since we're already in a mainloop callout */
472 g_simple_async_result_complete (simple);
473 g_object_unref (simple);
479 g_unix_input_stream_read_async (GInputStream *stream,
483 GCancellable *cancellable,
484 GAsyncReadyCallback callback,
488 GUnixInputStream *unix_stream;
491 unix_stream = G_UNIX_INPUT_STREAM (stream);
493 data = g_new0 (ReadAsyncData, 1);
495 data->buffer = buffer;
496 data->callback = callback;
497 data->user_data = user_data;
498 data->cancellable = cancellable;
499 data->stream = unix_stream;
501 source = _g_fd_source_new (unix_stream->priv->fd,
505 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
506 g_source_attach (source, NULL);
508 g_source_unref (source);
512 g_unix_input_stream_read_finish (GInputStream *stream,
513 GAsyncResult *result,
516 GSimpleAsyncResult *simple;
519 simple = G_SIMPLE_ASYNC_RESULT (result);
520 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
522 nread = g_simple_async_result_get_op_res_gssize (simple);
527 g_unix_input_stream_skip_async (GInputStream *stream,
530 GCancellable *cancellable,
531 GAsyncReadyCallback callback,
534 g_warn_if_reached ();
535 /* TODO: Not implemented */
539 g_unix_input_stream_skip_finish (GInputStream *stream,
540 GAsyncResult *result,
543 g_warn_if_reached ();
545 /* TODO: Not implemented */
550 GInputStream *stream;
551 GAsyncReadyCallback callback;
556 close_async_data_free (gpointer _data)
558 CloseAsyncData *data = _data;
564 close_async_cb (CloseAsyncData *data)
566 GUnixInputStream *unix_stream;
567 GSimpleAsyncResult *simple;
568 GError *error = NULL;
572 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
574 if (!unix_stream->priv->close_fd)
582 res = close (unix_stream->priv->fd);
587 g_set_error (&error, G_IO_ERROR,
588 g_io_error_from_errno (errsv),
589 _("Error closing unix: %s"),
598 simple = g_simple_async_result_new (G_OBJECT (data->stream),
601 g_unix_input_stream_close_async);
605 g_simple_async_result_set_from_error (simple, error);
606 g_error_free (error);
609 /* Complete immediately, not in idle, since we're already in a mainloop callout */
610 g_simple_async_result_complete (simple);
611 g_object_unref (simple);
617 g_unix_input_stream_close_async (GInputStream *stream,
619 GCancellable *cancellable,
620 GAsyncReadyCallback callback,
624 CloseAsyncData *data;
626 data = g_new0 (CloseAsyncData, 1);
628 data->stream = stream;
629 data->callback = callback;
630 data->user_data = user_data;
632 idle = g_idle_source_new ();
633 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
634 g_source_attach (idle, NULL);
635 g_source_unref (idle);
639 g_unix_input_stream_close_finish (GInputStream *stream,
640 GAsyncResult *result,
643 /* Failures handled in generic close_finish code */
647 #define __G_UNIX_INPUT_STREAM_C__
648 #include "gioaliasdef.c"