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
50 * UNIX file descriptor, including asynchronous operations. The file
51 * descriptor must be selectable, so it doesn't work with opened files.
53 * Note that <filename><gio/gunixinputstream.h></filename> belongs
54 * to the UNIX-specific GIO interfaces, thus you have to use the
55 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
64 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
65 static void g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
67 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM,
68 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
69 g_unix_input_stream_pollable_iface_init)
70 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
71 g_unix_input_stream_file_descriptor_based_iface_init)
74 struct _GUnixInputStreamPrivate {
79 static void g_unix_input_stream_set_property (GObject *object,
83 static void g_unix_input_stream_get_property (GObject *object,
87 static gssize g_unix_input_stream_read (GInputStream *stream,
90 GCancellable *cancellable,
92 static gboolean g_unix_input_stream_close (GInputStream *stream,
93 GCancellable *cancellable,
95 static void g_unix_input_stream_read_async (GInputStream *stream,
99 GCancellable *cancellable,
100 GAsyncReadyCallback callback,
102 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
103 GAsyncResult *result,
105 static void g_unix_input_stream_skip_async (GInputStream *stream,
108 GCancellable *cancellable,
109 GAsyncReadyCallback callback,
111 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
112 GAsyncResult *result,
114 static void g_unix_input_stream_close_async (GInputStream *stream,
116 GCancellable *cancellable,
117 GAsyncReadyCallback callback,
119 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
120 GAsyncResult *result,
123 static gboolean g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream);
124 static GSource *g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
125 GCancellable *cancellable);
128 g_unix_input_stream_finalize (GObject *object)
130 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
134 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
136 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
137 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
139 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
141 gobject_class->get_property = g_unix_input_stream_get_property;
142 gobject_class->set_property = g_unix_input_stream_set_property;
143 gobject_class->finalize = g_unix_input_stream_finalize;
145 stream_class->read_fn = g_unix_input_stream_read;
146 stream_class->close_fn = g_unix_input_stream_close;
147 stream_class->read_async = g_unix_input_stream_read_async;
148 stream_class->read_finish = g_unix_input_stream_read_finish;
151 /* TODO: Implement instead of using fallbacks */
152 stream_class->skip_async = g_unix_input_stream_skip_async;
153 stream_class->skip_finish = g_unix_input_stream_skip_finish;
155 stream_class->close_async = g_unix_input_stream_close_async;
156 stream_class->close_finish = g_unix_input_stream_close_finish;
159 * GUnixInputStream:fd:
161 * The file descriptor that the stream reads from.
165 g_object_class_install_property (gobject_class,
167 g_param_spec_int ("fd",
168 P_("File descriptor"),
169 P_("The file descriptor to read from"),
170 G_MININT, G_MAXINT, -1,
171 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
174 * GUnixInputStream:close-fd:
176 * Whether to close the file descriptor when the stream is closed.
180 g_object_class_install_property (gobject_class,
182 g_param_spec_boolean ("close-fd",
183 P_("Close file descriptor"),
184 P_("Whether to close the file descriptor when the stream is closed"),
186 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
190 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
192 iface->is_readable = g_unix_input_stream_pollable_is_readable;
193 iface->create_source = g_unix_input_stream_pollable_create_source;
197 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
199 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_input_stream_get_fd;
203 g_unix_input_stream_set_property (GObject *object,
208 GUnixInputStream *unix_stream;
210 unix_stream = G_UNIX_INPUT_STREAM (object);
215 unix_stream->priv->fd = g_value_get_int (value);
218 unix_stream->priv->close_fd = g_value_get_boolean (value);
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
227 g_unix_input_stream_get_property (GObject *object,
232 GUnixInputStream *unix_stream;
234 unix_stream = G_UNIX_INPUT_STREAM (object);
239 g_value_set_int (value, unix_stream->priv->fd);
242 g_value_set_boolean (value, unix_stream->priv->close_fd);
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 g_unix_input_stream_init (GUnixInputStream *unix_stream)
252 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
253 G_TYPE_UNIX_INPUT_STREAM,
254 GUnixInputStreamPrivate);
256 unix_stream->priv->fd = -1;
257 unix_stream->priv->close_fd = TRUE;
261 * g_unix_input_stream_new:
262 * @fd: a UNIX file descriptor
263 * @close_fd: %TRUE to close the file descriptor when done
265 * Creates a new #GUnixInputStream for the given @fd.
267 * If @close_fd is %TRUE, the file descriptor will be closed
268 * when the stream is closed.
270 * Returns: a new #GUnixInputStream
273 g_unix_input_stream_new (gint fd,
276 GUnixInputStream *stream;
278 g_return_val_if_fail (fd != -1, NULL);
280 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
282 "close-fd", close_fd,
285 return G_INPUT_STREAM (stream);
289 * g_unix_input_stream_set_close_fd:
290 * @stream: a #GUnixInputStream
291 * @close_fd: %TRUE to close the file descriptor when done
293 * Sets whether the file descriptor of @stream shall be closed
294 * when the stream is closed.
299 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
302 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
304 close_fd = close_fd != FALSE;
305 if (stream->priv->close_fd != close_fd)
307 stream->priv->close_fd = close_fd;
308 g_object_notify (G_OBJECT (stream), "close-fd");
313 * g_unix_input_stream_get_close_fd:
314 * @stream: a #GUnixInputStream
316 * Returns whether the file descriptor of @stream will be
317 * closed when the stream is closed.
319 * Return value: %TRUE if the file descriptor is closed when done
324 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
326 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
328 return stream->priv->close_fd;
332 * g_unix_input_stream_get_fd:
333 * @stream: a #GUnixInputStream
335 * Return the UNIX file descriptor that the stream reads from.
337 * Return value: The file descriptor of @stream
342 g_unix_input_stream_get_fd (GUnixInputStream *stream)
344 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
346 return stream->priv->fd;
350 g_unix_input_stream_read (GInputStream *stream,
353 GCancellable *cancellable,
356 GUnixInputStream *unix_stream;
361 unix_stream = G_UNIX_INPUT_STREAM (stream);
363 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
365 poll_fds[0].fd = unix_stream->priv->fd;
366 poll_fds[0].events = G_IO_IN;
368 poll_ret = g_poll (poll_fds, 2, -1);
369 while (poll_ret == -1 && errno == EINTR);
370 g_cancellable_release_fd (cancellable);
376 g_set_error (error, G_IO_ERROR,
377 g_io_error_from_errno (errsv),
378 _("Error reading from unix: %s"),
386 if (g_cancellable_set_error_if_cancelled (cancellable, error))
388 res = read (unix_stream->priv->fd, buffer, count);
396 g_set_error (error, G_IO_ERROR,
397 g_io_error_from_errno (errsv),
398 _("Error reading from unix: %s"),
409 g_unix_input_stream_close (GInputStream *stream,
410 GCancellable *cancellable,
413 GUnixInputStream *unix_stream;
416 unix_stream = G_UNIX_INPUT_STREAM (stream);
418 if (!unix_stream->priv->close_fd)
423 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
424 res = close (unix_stream->priv->fd);
429 g_set_error (error, G_IO_ERROR,
430 g_io_error_from_errno (errsv),
431 _("Error closing unix: %s"),
443 GAsyncReadyCallback callback;
445 GCancellable *cancellable;
446 GUnixInputStream *stream;
450 read_async_cb (int fd,
451 GIOCondition condition,
454 GSimpleAsyncResult *simple;
455 GError *error = NULL;
458 /* We know that we can read from fd once without blocking */
461 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
466 count_read = read (data->stream->priv->fd, data->buffer, data->count);
467 if (count_read == -1)
474 g_set_error (&error, G_IO_ERROR,
475 g_io_error_from_errno (errsv),
476 _("Error reading from unix: %s"),
482 simple = g_simple_async_result_new (G_OBJECT (data->stream),
485 g_unix_input_stream_read_async);
487 g_simple_async_result_set_op_res_gssize (simple, count_read);
489 if (count_read == -1)
490 g_simple_async_result_take_error (simple, error);
492 /* Complete immediately, not in idle, since we're already in a mainloop callout */
493 g_simple_async_result_complete (simple);
494 g_object_unref (simple);
500 g_unix_input_stream_read_async (GInputStream *stream,
504 GCancellable *cancellable,
505 GAsyncReadyCallback callback,
509 GUnixInputStream *unix_stream;
512 unix_stream = G_UNIX_INPUT_STREAM (stream);
514 data = g_new0 (ReadAsyncData, 1);
516 data->buffer = buffer;
517 data->callback = callback;
518 data->user_data = user_data;
519 data->cancellable = cancellable;
520 data->stream = unix_stream;
522 source = _g_fd_source_new (unix_stream->priv->fd,
525 g_source_set_name (source, "GUnixInputStream");
527 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
528 g_source_attach (source, g_main_context_get_thread_default ());
530 g_source_unref (source);
534 g_unix_input_stream_read_finish (GInputStream *stream,
535 GAsyncResult *result,
538 GSimpleAsyncResult *simple;
541 simple = G_SIMPLE_ASYNC_RESULT (result);
542 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
544 nread = g_simple_async_result_get_op_res_gssize (simple);
549 g_unix_input_stream_skip_async (GInputStream *stream,
552 GCancellable *cancellable,
553 GAsyncReadyCallback callback,
556 g_warn_if_reached ();
557 /* TODO: Not implemented */
561 g_unix_input_stream_skip_finish (GInputStream *stream,
562 GAsyncResult *result,
565 g_warn_if_reached ();
567 /* TODO: Not implemented */
572 GInputStream *stream;
573 GAsyncReadyCallback callback;
578 close_async_data_free (gpointer _data)
580 CloseAsyncData *data = _data;
586 close_async_cb (CloseAsyncData *data)
588 GUnixInputStream *unix_stream;
589 GSimpleAsyncResult *simple;
590 GError *error = NULL;
594 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
596 if (!unix_stream->priv->close_fd)
604 res = close (unix_stream->priv->fd);
609 g_set_error (&error, G_IO_ERROR,
610 g_io_error_from_errno (errsv),
611 _("Error closing unix: %s"),
620 simple = g_simple_async_result_new (G_OBJECT (data->stream),
623 g_unix_input_stream_close_async);
626 g_simple_async_result_take_error (simple, error);
628 /* Complete immediately, not in idle, since we're already in a mainloop callout */
629 g_simple_async_result_complete (simple);
630 g_object_unref (simple);
636 g_unix_input_stream_close_async (GInputStream *stream,
638 GCancellable *cancellable,
639 GAsyncReadyCallback callback,
643 CloseAsyncData *data;
645 data = g_new0 (CloseAsyncData, 1);
647 data->stream = stream;
648 data->callback = callback;
649 data->user_data = user_data;
651 idle = g_idle_source_new ();
652 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
653 g_source_attach (idle, g_main_context_get_thread_default ());
654 g_source_unref (idle);
658 g_unix_input_stream_close_finish (GInputStream *stream,
659 GAsyncResult *result,
662 /* Failures handled in generic close_finish code */
667 g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream)
669 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
673 poll_fd.fd = unix_stream->priv->fd;
674 poll_fd.events = G_IO_IN;
677 result = g_poll (&poll_fd, 1, 0);
678 while (result == -1 && errno == EINTR);
680 return poll_fd.revents != 0;
684 g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
685 GCancellable *cancellable)
687 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
688 GSource *inner_source, *pollable_source;
690 pollable_source = g_pollable_source_new (G_OBJECT (stream));
692 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_IN, cancellable);
693 g_source_set_dummy_callback (inner_source);
694 g_source_add_child_source (pollable_source, inner_source);
695 g_source_unref (inner_source);
697 return pollable_source;