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"
43 * SECTION:gunixinputstream
44 * @short_description: Streaming input operations for UNIX file descriptors
45 * @include: gio/gunixinputstream.h
46 * @see_also: #GInputStream
48 * #GUnixInputStream implements #GInputStream for reading from a
49 * UNIX file descriptor, including asynchronous operations. The file
50 * descriptor must be selectable, so it doesn't work with opened files.
52 * Note that <filename><gio/gunixinputstream.h></filename> belongs
53 * to the UNIX-specific GIO interfaces, thus you have to use the
54 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
63 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
65 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM,
66 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
67 g_unix_input_stream_pollable_iface_init)
70 struct _GUnixInputStreamPrivate {
75 static void g_unix_input_stream_set_property (GObject *object,
79 static void g_unix_input_stream_get_property (GObject *object,
83 static gssize g_unix_input_stream_read (GInputStream *stream,
86 GCancellable *cancellable,
88 static gboolean g_unix_input_stream_close (GInputStream *stream,
89 GCancellable *cancellable,
91 static void g_unix_input_stream_read_async (GInputStream *stream,
95 GCancellable *cancellable,
96 GAsyncReadyCallback callback,
98 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
101 static void g_unix_input_stream_skip_async (GInputStream *stream,
104 GCancellable *cancellable,
105 GAsyncReadyCallback callback,
107 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
108 GAsyncResult *result,
110 static void g_unix_input_stream_close_async (GInputStream *stream,
112 GCancellable *cancellable,
113 GAsyncReadyCallback callback,
115 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
116 GAsyncResult *result,
119 static gboolean g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream);
120 static GSource *g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
121 GCancellable *cancellable);
124 g_unix_input_stream_finalize (GObject *object)
126 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
130 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
133 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
135 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
137 gobject_class->get_property = g_unix_input_stream_get_property;
138 gobject_class->set_property = g_unix_input_stream_set_property;
139 gobject_class->finalize = g_unix_input_stream_finalize;
141 stream_class->read_fn = g_unix_input_stream_read;
142 stream_class->close_fn = g_unix_input_stream_close;
143 stream_class->read_async = g_unix_input_stream_read_async;
144 stream_class->read_finish = g_unix_input_stream_read_finish;
147 /* TODO: Implement instead of using fallbacks */
148 stream_class->skip_async = g_unix_input_stream_skip_async;
149 stream_class->skip_finish = g_unix_input_stream_skip_finish;
151 stream_class->close_async = g_unix_input_stream_close_async;
152 stream_class->close_finish = g_unix_input_stream_close_finish;
155 * GUnixInputStream:fd:
157 * The file descriptor that the stream reads from.
161 g_object_class_install_property (gobject_class,
163 g_param_spec_int ("fd",
164 P_("File descriptor"),
165 P_("The file descriptor to read from"),
166 G_MININT, G_MAXINT, -1,
167 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
170 * GUnixInputStream:close-fd:
172 * Whether to close the file descriptor when the stream is closed.
176 g_object_class_install_property (gobject_class,
178 g_param_spec_boolean ("close-fd",
179 P_("Close file descriptor"),
180 P_("Whether to close the file descriptor when the stream is closed"),
182 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
186 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
188 iface->is_readable = g_unix_input_stream_pollable_is_readable;
189 iface->create_source = g_unix_input_stream_pollable_create_source;
193 g_unix_input_stream_set_property (GObject *object,
198 GUnixInputStream *unix_stream;
200 unix_stream = G_UNIX_INPUT_STREAM (object);
205 unix_stream->priv->fd = g_value_get_int (value);
208 unix_stream->priv->close_fd = g_value_get_boolean (value);
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217 g_unix_input_stream_get_property (GObject *object,
222 GUnixInputStream *unix_stream;
224 unix_stream = G_UNIX_INPUT_STREAM (object);
229 g_value_set_int (value, unix_stream->priv->fd);
232 g_value_set_boolean (value, unix_stream->priv->close_fd);
235 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 g_unix_input_stream_init (GUnixInputStream *unix_stream)
242 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
243 G_TYPE_UNIX_INPUT_STREAM,
244 GUnixInputStreamPrivate);
246 unix_stream->priv->fd = -1;
247 unix_stream->priv->close_fd = TRUE;
251 * g_unix_input_stream_new:
252 * @fd: a UNIX file descriptor
253 * @close_fd: %TRUE to close the file descriptor when done
255 * Creates a new #GUnixInputStream for the given @fd.
257 * If @close_fd is %TRUE, the file descriptor will be closed
258 * when the stream is closed.
260 * Returns: a new #GUnixInputStream
263 g_unix_input_stream_new (gint fd,
266 GUnixInputStream *stream;
268 g_return_val_if_fail (fd != -1, NULL);
270 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
272 "close-fd", close_fd,
275 return G_INPUT_STREAM (stream);
279 * g_unix_input_stream_set_close_fd:
280 * @stream: a #GUnixInputStream
281 * @close_fd: %TRUE to close the file descriptor when done
283 * Sets whether the file descriptor of @stream shall be closed
284 * when the stream is closed.
289 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
292 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
294 close_fd = close_fd != FALSE;
295 if (stream->priv->close_fd != close_fd)
297 stream->priv->close_fd = close_fd;
298 g_object_notify (G_OBJECT (stream), "close-fd");
303 * g_unix_input_stream_get_close_fd:
304 * @stream: a #GUnixInputStream
306 * Returns whether the file descriptor of @stream will be
307 * closed when the stream is closed.
309 * Return value: %TRUE if the file descriptor is closed when done
314 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
316 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
318 return stream->priv->close_fd;
322 * g_unix_input_stream_get_fd:
323 * @stream: a #GUnixInputStream
325 * Return the UNIX file descriptor that the stream reads from.
327 * Return value: The file descriptor of @stream
332 g_unix_input_stream_get_fd (GUnixInputStream *stream)
334 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
336 return stream->priv->fd;
340 g_unix_input_stream_read (GInputStream *stream,
343 GCancellable *cancellable,
346 GUnixInputStream *unix_stream;
351 unix_stream = G_UNIX_INPUT_STREAM (stream);
353 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
355 poll_fds[0].fd = unix_stream->priv->fd;
356 poll_fds[0].events = G_IO_IN;
358 poll_ret = g_poll (poll_fds, 2, -1);
359 while (poll_ret == -1 && errno == EINTR);
360 g_cancellable_release_fd (cancellable);
366 g_set_error (error, G_IO_ERROR,
367 g_io_error_from_errno (errsv),
368 _("Error reading from unix: %s"),
376 if (g_cancellable_set_error_if_cancelled (cancellable, error))
378 res = read (unix_stream->priv->fd, buffer, count);
386 g_set_error (error, G_IO_ERROR,
387 g_io_error_from_errno (errsv),
388 _("Error reading from unix: %s"),
399 g_unix_input_stream_close (GInputStream *stream,
400 GCancellable *cancellable,
403 GUnixInputStream *unix_stream;
406 unix_stream = G_UNIX_INPUT_STREAM (stream);
408 if (!unix_stream->priv->close_fd)
413 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
414 res = close (unix_stream->priv->fd);
419 g_set_error (error, G_IO_ERROR,
420 g_io_error_from_errno (errsv),
421 _("Error closing unix: %s"),
433 GAsyncReadyCallback callback;
435 GCancellable *cancellable;
436 GUnixInputStream *stream;
440 read_async_cb (int fd,
441 GIOCondition condition,
444 GSimpleAsyncResult *simple;
445 GError *error = NULL;
448 /* We know that we can read from fd once without blocking */
451 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
456 count_read = read (data->stream->priv->fd, data->buffer, data->count);
457 if (count_read == -1)
464 g_set_error (&error, G_IO_ERROR,
465 g_io_error_from_errno (errsv),
466 _("Error reading from unix: %s"),
472 simple = g_simple_async_result_new (G_OBJECT (data->stream),
475 g_unix_input_stream_read_async);
477 g_simple_async_result_set_op_res_gssize (simple, count_read);
479 if (count_read == -1)
480 g_simple_async_result_take_error (simple, error);
482 /* Complete immediately, not in idle, since we're already in a mainloop callout */
483 g_simple_async_result_complete (simple);
484 g_object_unref (simple);
490 g_unix_input_stream_read_async (GInputStream *stream,
494 GCancellable *cancellable,
495 GAsyncReadyCallback callback,
499 GUnixInputStream *unix_stream;
502 unix_stream = G_UNIX_INPUT_STREAM (stream);
504 data = g_new0 (ReadAsyncData, 1);
506 data->buffer = buffer;
507 data->callback = callback;
508 data->user_data = user_data;
509 data->cancellable = cancellable;
510 data->stream = unix_stream;
512 source = _g_fd_source_new (unix_stream->priv->fd,
515 g_source_set_name (source, "GUnixInputStream");
517 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
518 g_source_attach (source, g_main_context_get_thread_default ());
520 g_source_unref (source);
524 g_unix_input_stream_read_finish (GInputStream *stream,
525 GAsyncResult *result,
528 GSimpleAsyncResult *simple;
531 simple = G_SIMPLE_ASYNC_RESULT (result);
532 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
534 nread = g_simple_async_result_get_op_res_gssize (simple);
539 g_unix_input_stream_skip_async (GInputStream *stream,
542 GCancellable *cancellable,
543 GAsyncReadyCallback callback,
546 g_warn_if_reached ();
547 /* TODO: Not implemented */
551 g_unix_input_stream_skip_finish (GInputStream *stream,
552 GAsyncResult *result,
555 g_warn_if_reached ();
557 /* TODO: Not implemented */
562 GInputStream *stream;
563 GAsyncReadyCallback callback;
568 close_async_data_free (gpointer _data)
570 CloseAsyncData *data = _data;
576 close_async_cb (CloseAsyncData *data)
578 GUnixInputStream *unix_stream;
579 GSimpleAsyncResult *simple;
580 GError *error = NULL;
584 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
586 if (!unix_stream->priv->close_fd)
594 res = close (unix_stream->priv->fd);
599 g_set_error (&error, G_IO_ERROR,
600 g_io_error_from_errno (errsv),
601 _("Error closing unix: %s"),
610 simple = g_simple_async_result_new (G_OBJECT (data->stream),
613 g_unix_input_stream_close_async);
616 g_simple_async_result_take_error (simple, error);
618 /* Complete immediately, not in idle, since we're already in a mainloop callout */
619 g_simple_async_result_complete (simple);
620 g_object_unref (simple);
626 g_unix_input_stream_close_async (GInputStream *stream,
628 GCancellable *cancellable,
629 GAsyncReadyCallback callback,
633 CloseAsyncData *data;
635 data = g_new0 (CloseAsyncData, 1);
637 data->stream = stream;
638 data->callback = callback;
639 data->user_data = user_data;
641 idle = g_idle_source_new ();
642 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
643 g_source_attach (idle, g_main_context_get_thread_default ());
644 g_source_unref (idle);
648 g_unix_input_stream_close_finish (GInputStream *stream,
649 GAsyncResult *result,
652 /* Failures handled in generic close_finish code */
657 g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream)
659 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
663 poll_fd.fd = unix_stream->priv->fd;
664 poll_fd.events = G_IO_IN;
667 result = g_poll (&poll_fd, 1, 0);
668 while (result == -1 && errno == EINTR);
670 return poll_fd.revents != 0;
674 g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
675 GCancellable *cancellable)
677 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
678 GSource *inner_source, *pollable_source;
680 pollable_source = g_pollable_source_new (G_OBJECT (stream));
682 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_IN, cancellable);
683 g_source_set_dummy_callback (inner_source);
684 g_source_add_child_source (pollable_source, inner_source);
685 g_source_unref (inner_source);
687 return pollable_source;