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"
39 #include "gfiledescriptorbased.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 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.)
55 * Note that <filename><gio/gunixinputstream.h></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.
66 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
67 static void g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
69 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM,
70 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
71 g_unix_input_stream_pollable_iface_init)
72 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
73 g_unix_input_stream_file_descriptor_based_iface_init)
76 struct _GUnixInputStreamPrivate {
79 guint is_pipe_or_socket : 1;
82 static void g_unix_input_stream_set_property (GObject *object,
86 static void g_unix_input_stream_get_property (GObject *object,
90 static gssize g_unix_input_stream_read (GInputStream *stream,
93 GCancellable *cancellable,
95 static gboolean g_unix_input_stream_close (GInputStream *stream,
96 GCancellable *cancellable,
98 static void g_unix_input_stream_read_async (GInputStream *stream,
102 GCancellable *cancellable,
103 GAsyncReadyCallback callback,
105 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
106 GAsyncResult *result,
108 static void g_unix_input_stream_skip_async (GInputStream *stream,
111 GCancellable *cancellable,
112 GAsyncReadyCallback callback,
114 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
115 GAsyncResult *result,
117 static void g_unix_input_stream_close_async (GInputStream *stream,
119 GCancellable *cancellable,
120 GAsyncReadyCallback callback,
122 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
123 GAsyncResult *result,
126 static gboolean g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream);
127 static GSource *g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
128 GCancellable *cancellable);
131 g_unix_input_stream_finalize (GObject *object)
133 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
137 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
139 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
140 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
142 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
144 gobject_class->get_property = g_unix_input_stream_get_property;
145 gobject_class->set_property = g_unix_input_stream_set_property;
146 gobject_class->finalize = g_unix_input_stream_finalize;
148 stream_class->read_fn = g_unix_input_stream_read;
149 stream_class->close_fn = g_unix_input_stream_close;
150 stream_class->read_async = g_unix_input_stream_read_async;
151 stream_class->read_finish = g_unix_input_stream_read_finish;
154 /* TODO: Implement instead of using fallbacks */
155 stream_class->skip_async = g_unix_input_stream_skip_async;
156 stream_class->skip_finish = g_unix_input_stream_skip_finish;
158 stream_class->close_async = g_unix_input_stream_close_async;
159 stream_class->close_finish = g_unix_input_stream_close_finish;
162 * GUnixInputStream:fd:
164 * The file descriptor that the stream reads from.
168 g_object_class_install_property (gobject_class,
170 g_param_spec_int ("fd",
171 P_("File descriptor"),
172 P_("The file descriptor to read from"),
173 G_MININT, G_MAXINT, -1,
174 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
177 * GUnixInputStream:close-fd:
179 * Whether to close the file descriptor when the stream is closed.
183 g_object_class_install_property (gobject_class,
185 g_param_spec_boolean ("close-fd",
186 P_("Close file descriptor"),
187 P_("Whether to close the file descriptor when the stream is closed"),
189 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
193 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
195 iface->is_readable = g_unix_input_stream_pollable_is_readable;
196 iface->create_source = g_unix_input_stream_pollable_create_source;
200 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
202 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_input_stream_get_fd;
206 g_unix_input_stream_set_property (GObject *object,
211 GUnixInputStream *unix_stream;
213 unix_stream = G_UNIX_INPUT_STREAM (object);
218 unix_stream->priv->fd = g_value_get_int (value);
219 if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
220 unix_stream->priv->is_pipe_or_socket = TRUE;
222 unix_stream->priv->is_pipe_or_socket = FALSE;
225 unix_stream->priv->close_fd = g_value_get_boolean (value);
228 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
234 g_unix_input_stream_get_property (GObject *object,
239 GUnixInputStream *unix_stream;
241 unix_stream = G_UNIX_INPUT_STREAM (object);
246 g_value_set_int (value, unix_stream->priv->fd);
249 g_value_set_boolean (value, unix_stream->priv->close_fd);
252 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
257 g_unix_input_stream_init (GUnixInputStream *unix_stream)
259 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
260 G_TYPE_UNIX_INPUT_STREAM,
261 GUnixInputStreamPrivate);
263 unix_stream->priv->fd = -1;
264 unix_stream->priv->close_fd = TRUE;
268 * g_unix_input_stream_new:
269 * @fd: a UNIX file descriptor
270 * @close_fd: %TRUE to close the file descriptor when done
272 * Creates a new #GUnixInputStream for the given @fd.
274 * If @close_fd is %TRUE, the file descriptor will be closed
275 * when the stream is closed.
277 * Returns: a new #GUnixInputStream
280 g_unix_input_stream_new (gint fd,
283 GUnixInputStream *stream;
285 g_return_val_if_fail (fd != -1, NULL);
287 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
289 "close-fd", close_fd,
292 return G_INPUT_STREAM (stream);
296 * g_unix_input_stream_set_close_fd:
297 * @stream: a #GUnixInputStream
298 * @close_fd: %TRUE to close the file descriptor when done
300 * Sets whether the file descriptor of @stream shall be closed
301 * when the stream is closed.
306 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
309 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
311 close_fd = close_fd != FALSE;
312 if (stream->priv->close_fd != close_fd)
314 stream->priv->close_fd = close_fd;
315 g_object_notify (G_OBJECT (stream), "close-fd");
320 * g_unix_input_stream_get_close_fd:
321 * @stream: a #GUnixInputStream
323 * Returns whether the file descriptor of @stream will be
324 * closed when the stream is closed.
326 * Return value: %TRUE if the file descriptor is closed when done
331 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
333 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
335 return stream->priv->close_fd;
339 * g_unix_input_stream_get_fd:
340 * @stream: a #GUnixInputStream
342 * Return the UNIX file descriptor that the stream reads from.
344 * Return value: The file descriptor of @stream
349 g_unix_input_stream_get_fd (GUnixInputStream *stream)
351 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
353 return stream->priv->fd;
357 g_unix_input_stream_read (GInputStream *stream,
360 GCancellable *cancellable,
363 GUnixInputStream *unix_stream;
368 unix_stream = G_UNIX_INPUT_STREAM (stream);
370 if (unix_stream->priv->is_pipe_or_socket &&
371 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
373 poll_fds[0].fd = unix_stream->priv->fd;
374 poll_fds[0].events = G_IO_IN;
376 poll_ret = g_poll (poll_fds, 2, -1);
377 while (poll_ret == -1 && errno == EINTR);
378 g_cancellable_release_fd (cancellable);
384 g_set_error (error, G_IO_ERROR,
385 g_io_error_from_errno (errsv),
386 _("Error reading from unix: %s"),
394 if (g_cancellable_set_error_if_cancelled (cancellable, error))
396 res = read (unix_stream->priv->fd, buffer, count);
404 g_set_error (error, G_IO_ERROR,
405 g_io_error_from_errno (errsv),
406 _("Error reading from unix: %s"),
417 g_unix_input_stream_close (GInputStream *stream,
418 GCancellable *cancellable,
421 GUnixInputStream *unix_stream;
424 unix_stream = G_UNIX_INPUT_STREAM (stream);
426 if (!unix_stream->priv->close_fd)
431 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
432 res = close (unix_stream->priv->fd);
437 g_set_error (error, G_IO_ERROR,
438 g_io_error_from_errno (errsv),
439 _("Error closing unix: %s"),
451 GAsyncReadyCallback callback;
453 GCancellable *cancellable;
454 GUnixInputStream *stream;
458 read_async_cb (int fd,
459 GIOCondition condition,
462 GSimpleAsyncResult *simple;
463 GError *error = NULL;
466 /* We know that we can read from fd once without blocking */
469 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
474 count_read = read (data->stream->priv->fd, data->buffer, data->count);
475 if (count_read == -1)
482 g_set_error (&error, G_IO_ERROR,
483 g_io_error_from_errno (errsv),
484 _("Error reading from unix: %s"),
490 simple = g_simple_async_result_new (G_OBJECT (data->stream),
493 g_unix_input_stream_read_async);
495 g_simple_async_result_set_op_res_gssize (simple, count_read);
497 if (count_read == -1)
498 g_simple_async_result_take_error (simple, error);
500 /* Complete immediately, not in idle, since we're already in a mainloop callout */
501 g_simple_async_result_complete (simple);
502 g_object_unref (simple);
508 g_unix_input_stream_read_async (GInputStream *stream,
512 GCancellable *cancellable,
513 GAsyncReadyCallback callback,
517 GUnixInputStream *unix_stream;
520 unix_stream = G_UNIX_INPUT_STREAM (stream);
522 if (!unix_stream->priv->is_pipe_or_socket)
524 G_INPUT_STREAM_CLASS (g_unix_input_stream_parent_class)->
525 read_async (stream, buffer, count, io_priority,
526 cancellable, callback, user_data);
530 data = g_new0 (ReadAsyncData, 1);
532 data->buffer = buffer;
533 data->callback = callback;
534 data->user_data = user_data;
535 data->cancellable = cancellable;
536 data->stream = unix_stream;
538 source = _g_fd_source_new (unix_stream->priv->fd,
541 g_source_set_name (source, "GUnixInputStream");
543 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
544 g_source_attach (source, g_main_context_get_thread_default ());
546 g_source_unref (source);
550 g_unix_input_stream_read_finish (GInputStream *stream,
551 GAsyncResult *result,
554 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
555 GSimpleAsyncResult *simple;
558 if (!unix_stream->priv->is_pipe_or_socket)
560 return G_INPUT_STREAM_CLASS (g_unix_input_stream_parent_class)->
561 read_finish (stream, result, error);
564 simple = G_SIMPLE_ASYNC_RESULT (result);
565 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
567 nread = g_simple_async_result_get_op_res_gssize (simple);
572 g_unix_input_stream_skip_async (GInputStream *stream,
575 GCancellable *cancellable,
576 GAsyncReadyCallback callback,
579 g_warn_if_reached ();
580 /* TODO: Not implemented */
584 g_unix_input_stream_skip_finish (GInputStream *stream,
585 GAsyncResult *result,
588 g_warn_if_reached ();
590 /* TODO: Not implemented */
595 GInputStream *stream;
596 GAsyncReadyCallback callback;
601 close_async_data_free (gpointer _data)
603 CloseAsyncData *data = _data;
609 close_async_cb (CloseAsyncData *data)
611 GUnixInputStream *unix_stream;
612 GSimpleAsyncResult *simple;
613 GError *error = NULL;
617 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
619 if (!unix_stream->priv->close_fd)
627 res = close (unix_stream->priv->fd);
632 g_set_error (&error, G_IO_ERROR,
633 g_io_error_from_errno (errsv),
634 _("Error closing unix: %s"),
643 simple = g_simple_async_result_new (G_OBJECT (data->stream),
646 g_unix_input_stream_close_async);
649 g_simple_async_result_take_error (simple, error);
651 /* Complete immediately, not in idle, since we're already in a mainloop callout */
652 g_simple_async_result_complete (simple);
653 g_object_unref (simple);
659 g_unix_input_stream_close_async (GInputStream *stream,
661 GCancellable *cancellable,
662 GAsyncReadyCallback callback,
666 CloseAsyncData *data;
668 data = g_new0 (CloseAsyncData, 1);
670 data->stream = stream;
671 data->callback = callback;
672 data->user_data = user_data;
674 idle = g_idle_source_new ();
675 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
676 g_source_attach (idle, g_main_context_get_thread_default ());
677 g_source_unref (idle);
681 g_unix_input_stream_close_finish (GInputStream *stream,
682 GAsyncResult *result,
685 /* Failures handled in generic close_finish code */
690 g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream)
692 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
696 poll_fd.fd = unix_stream->priv->fd;
697 poll_fd.events = G_IO_IN;
700 result = g_poll (&poll_fd, 1, 0);
701 while (result == -1 && errno == EINTR);
703 return poll_fd.revents != 0;
707 g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
708 GCancellable *cancellable)
710 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
711 GSource *inner_source, *pollable_source;
713 pollable_source = g_pollable_source_new (G_OBJECT (stream));
715 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_IN, cancellable);
716 g_source_set_dummy_callback (inner_source);
717 g_source_add_child_source (pollable_source, inner_source);
718 g_source_unref (inner_source);
720 return pollable_source;