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_skip_async (GInputStream *stream,
101 GCancellable *cancellable,
102 GAsyncReadyCallback callback,
104 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
105 GAsyncResult *result,
107 static void g_unix_input_stream_close_async (GInputStream *stream,
109 GCancellable *cancellable,
110 GAsyncReadyCallback callback,
112 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
113 GAsyncResult *result,
116 static gboolean g_unix_input_stream_pollable_can_poll (GPollableInputStream *stream);
117 static gboolean g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream);
118 static GSource *g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
119 GCancellable *cancellable);
122 g_unix_input_stream_finalize (GObject *object)
124 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
128 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
133 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
135 gobject_class->get_property = g_unix_input_stream_get_property;
136 gobject_class->set_property = g_unix_input_stream_set_property;
137 gobject_class->finalize = g_unix_input_stream_finalize;
139 stream_class->read_fn = g_unix_input_stream_read;
140 stream_class->close_fn = g_unix_input_stream_close;
143 /* TODO: Implement instead of using fallbacks */
144 stream_class->skip_async = g_unix_input_stream_skip_async;
145 stream_class->skip_finish = g_unix_input_stream_skip_finish;
147 stream_class->close_async = g_unix_input_stream_close_async;
148 stream_class->close_finish = g_unix_input_stream_close_finish;
151 * GUnixInputStream:fd:
153 * The file descriptor that the stream reads from.
157 g_object_class_install_property (gobject_class,
159 g_param_spec_int ("fd",
160 P_("File descriptor"),
161 P_("The file descriptor to read from"),
162 G_MININT, G_MAXINT, -1,
163 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
166 * GUnixInputStream:close-fd:
168 * Whether to close the file descriptor when the stream is closed.
172 g_object_class_install_property (gobject_class,
174 g_param_spec_boolean ("close-fd",
175 P_("Close file descriptor"),
176 P_("Whether to close the file descriptor when the stream is closed"),
178 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
182 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
184 iface->can_poll = g_unix_input_stream_pollable_can_poll;
185 iface->is_readable = g_unix_input_stream_pollable_is_readable;
186 iface->create_source = g_unix_input_stream_pollable_create_source;
190 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
192 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_input_stream_get_fd;
196 g_unix_input_stream_set_property (GObject *object,
201 GUnixInputStream *unix_stream;
203 unix_stream = G_UNIX_INPUT_STREAM (object);
208 unix_stream->priv->fd = g_value_get_int (value);
209 if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
210 unix_stream->priv->is_pipe_or_socket = TRUE;
212 unix_stream->priv->is_pipe_or_socket = FALSE;
215 unix_stream->priv->close_fd = g_value_get_boolean (value);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 g_unix_input_stream_get_property (GObject *object,
229 GUnixInputStream *unix_stream;
231 unix_stream = G_UNIX_INPUT_STREAM (object);
236 g_value_set_int (value, unix_stream->priv->fd);
239 g_value_set_boolean (value, unix_stream->priv->close_fd);
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 g_unix_input_stream_init (GUnixInputStream *unix_stream)
249 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
250 G_TYPE_UNIX_INPUT_STREAM,
251 GUnixInputStreamPrivate);
253 unix_stream->priv->fd = -1;
254 unix_stream->priv->close_fd = TRUE;
258 * g_unix_input_stream_new:
259 * @fd: a UNIX file descriptor
260 * @close_fd: %TRUE to close the file descriptor when done
262 * Creates a new #GUnixInputStream for the given @fd.
264 * If @close_fd is %TRUE, the file descriptor will be closed
265 * when the stream is closed.
267 * Returns: a new #GUnixInputStream
270 g_unix_input_stream_new (gint fd,
273 GUnixInputStream *stream;
275 g_return_val_if_fail (fd != -1, NULL);
277 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
279 "close-fd", close_fd,
282 return G_INPUT_STREAM (stream);
286 * g_unix_input_stream_set_close_fd:
287 * @stream: a #GUnixInputStream
288 * @close_fd: %TRUE to close the file descriptor when done
290 * Sets whether the file descriptor of @stream shall be closed
291 * when the stream is closed.
296 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
299 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
301 close_fd = close_fd != FALSE;
302 if (stream->priv->close_fd != close_fd)
304 stream->priv->close_fd = close_fd;
305 g_object_notify (G_OBJECT (stream), "close-fd");
310 * g_unix_input_stream_get_close_fd:
311 * @stream: a #GUnixInputStream
313 * Returns whether the file descriptor of @stream will be
314 * closed when the stream is closed.
316 * Return value: %TRUE if the file descriptor is closed when done
321 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
323 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
325 return stream->priv->close_fd;
329 * g_unix_input_stream_get_fd:
330 * @stream: a #GUnixInputStream
332 * Return the UNIX file descriptor that the stream reads from.
334 * Return value: The file descriptor of @stream
339 g_unix_input_stream_get_fd (GUnixInputStream *stream)
341 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
343 return stream->priv->fd;
347 g_unix_input_stream_read (GInputStream *stream,
350 GCancellable *cancellable,
353 GUnixInputStream *unix_stream;
359 unix_stream = G_UNIX_INPUT_STREAM (stream);
361 poll_fds[0].fd = unix_stream->priv->fd;
362 poll_fds[0].events = G_IO_IN;
363 if (unix_stream->priv->is_pipe_or_socket &&
364 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
371 poll_fds[0].revents = poll_fds[1].revents = 0;
373 poll_ret = g_poll (poll_fds, nfds, -1);
374 while (poll_ret == -1 && errno == EINTR);
380 g_set_error (error, G_IO_ERROR,
381 g_io_error_from_errno (errsv),
382 _("Error reading from file descriptor: %s"),
387 if (g_cancellable_set_error_if_cancelled (cancellable, error))
390 if (!poll_fds[0].revents)
393 res = read (unix_stream->priv->fd, buffer, count);
398 if (errsv == EINTR || errsv == EAGAIN)
401 g_set_error (error, G_IO_ERROR,
402 g_io_error_from_errno (errsv),
403 _("Error reading from file descriptor: %s"),
411 g_cancellable_release_fd (cancellable);
416 g_unix_input_stream_close (GInputStream *stream,
417 GCancellable *cancellable,
420 GUnixInputStream *unix_stream;
423 unix_stream = G_UNIX_INPUT_STREAM (stream);
425 if (!unix_stream->priv->close_fd)
430 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
431 res = close (unix_stream->priv->fd);
436 g_set_error (error, G_IO_ERROR,
437 g_io_error_from_errno (errsv),
438 _("Error closing file descriptor: %s"),
448 g_unix_input_stream_skip_async (GInputStream *stream,
451 GCancellable *cancellable,
452 GAsyncReadyCallback callback,
455 g_warn_if_reached ();
456 /* TODO: Not implemented */
460 g_unix_input_stream_skip_finish (GInputStream *stream,
461 GAsyncResult *result,
464 g_warn_if_reached ();
466 /* TODO: Not implemented */
471 GInputStream *stream;
472 GAsyncReadyCallback callback;
477 close_async_data_free (gpointer _data)
479 CloseAsyncData *data = _data;
485 close_async_cb (CloseAsyncData *data)
487 GUnixInputStream *unix_stream;
488 GSimpleAsyncResult *simple;
489 GError *error = NULL;
493 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
495 if (!unix_stream->priv->close_fd)
503 res = close (unix_stream->priv->fd);
508 g_set_error (&error, G_IO_ERROR,
509 g_io_error_from_errno (errsv),
510 _("Error closing file descriptor: %s"),
519 simple = g_simple_async_result_new (G_OBJECT (data->stream),
522 g_unix_input_stream_close_async);
525 g_simple_async_result_take_error (simple, error);
527 /* Complete immediately, not in idle, since we're already in a mainloop callout */
528 g_simple_async_result_complete (simple);
529 g_object_unref (simple);
535 g_unix_input_stream_close_async (GInputStream *stream,
537 GCancellable *cancellable,
538 GAsyncReadyCallback callback,
542 CloseAsyncData *data;
544 data = g_new0 (CloseAsyncData, 1);
546 data->stream = stream;
547 data->callback = callback;
548 data->user_data = user_data;
550 idle = g_idle_source_new ();
551 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
552 g_source_attach (idle, g_main_context_get_thread_default ());
553 g_source_unref (idle);
557 g_unix_input_stream_close_finish (GInputStream *stream,
558 GAsyncResult *result,
561 /* Failures handled in generic close_finish code */
566 g_unix_input_stream_pollable_can_poll (GPollableInputStream *stream)
568 return G_UNIX_INPUT_STREAM (stream)->priv->is_pipe_or_socket;
572 g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream)
574 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
578 poll_fd.fd = unix_stream->priv->fd;
579 poll_fd.events = G_IO_IN;
582 result = g_poll (&poll_fd, 1, 0);
583 while (result == -1 && errno == EINTR);
585 return poll_fd.revents != 0;
589 g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
590 GCancellable *cancellable)
592 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
593 GSource *inner_source, *pollable_source;
595 pollable_source = g_pollable_source_new (G_OBJECT (stream));
597 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_IN, cancellable);
598 g_source_set_dummy_callback (inner_source);
599 g_source_add_child_source (pollable_source, inner_source);
600 g_source_unref (inner_source);
602 return pollable_source;