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.1 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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
28 #include <glib/gstdio.h>
29 #include <glib/glib-unix.h>
31 #include "gunixinputstream.h"
32 #include "gcancellable.h"
33 #include "gasynchelper.h"
34 #include "gfiledescriptorbased.h"
36 #include "giounix-private.h"
40 * SECTION:gunixinputstream
41 * @short_description: Streaming input operations for UNIX file descriptors
42 * @include: gio/gunixinputstream.h
43 * @see_also: #GInputStream
45 * #GUnixInputStream implements #GInputStream for reading from a UNIX
46 * file descriptor, including asynchronous operations. (If the file
47 * descriptor refers to a socket or pipe, this will use poll() to do
48 * asynchronous I/O. If it refers to a regular file, it will fall back
49 * to doing asynchronous I/O in another thread.)
51 * Note that `<gio/gunixinputstream.h>` belongs to the UNIX-specific GIO
52 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
62 struct _GUnixInputStreamPrivate {
68 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
69 static void g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
71 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM,
72 G_ADD_PRIVATE (GUnixInputStream)
73 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
74 g_unix_input_stream_pollable_iface_init)
75 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
76 g_unix_input_stream_file_descriptor_based_iface_init)
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_skip_async (GInputStream *stream,
98 GCancellable *cancellable,
99 GAsyncReadyCallback callback,
101 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
102 GAsyncResult *result,
105 static gboolean g_unix_input_stream_pollable_can_poll (GPollableInputStream *stream);
106 static gboolean g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream);
107 static GSource *g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
108 GCancellable *cancellable);
111 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
116 gobject_class->get_property = g_unix_input_stream_get_property;
117 gobject_class->set_property = g_unix_input_stream_set_property;
119 stream_class->read_fn = g_unix_input_stream_read;
120 stream_class->close_fn = g_unix_input_stream_close;
123 /* TODO: Implement instead of using fallbacks */
124 stream_class->skip_async = g_unix_input_stream_skip_async;
125 stream_class->skip_finish = g_unix_input_stream_skip_finish;
129 * GUnixInputStream:fd:
131 * The file descriptor that the stream reads from.
135 g_object_class_install_property (gobject_class,
137 g_param_spec_int ("fd",
138 P_("File descriptor"),
139 P_("The file descriptor to read from"),
140 G_MININT, G_MAXINT, -1,
141 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
144 * GUnixInputStream:close-fd:
146 * Whether to close the file descriptor when the stream is closed.
150 g_object_class_install_property (gobject_class,
152 g_param_spec_boolean ("close-fd",
153 P_("Close file descriptor"),
154 P_("Whether to close the file descriptor when the stream is closed"),
156 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
160 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
162 iface->can_poll = g_unix_input_stream_pollable_can_poll;
163 iface->is_readable = g_unix_input_stream_pollable_is_readable;
164 iface->create_source = g_unix_input_stream_pollable_create_source;
168 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
170 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_input_stream_get_fd;
174 g_unix_input_stream_set_property (GObject *object,
179 GUnixInputStream *unix_stream;
181 unix_stream = G_UNIX_INPUT_STREAM (object);
186 unix_stream->priv->fd = g_value_get_int (value);
187 unix_stream->priv->can_poll = _g_fd_is_pollable (unix_stream->priv->fd);
190 unix_stream->priv->close_fd = g_value_get_boolean (value);
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
199 g_unix_input_stream_get_property (GObject *object,
204 GUnixInputStream *unix_stream;
206 unix_stream = G_UNIX_INPUT_STREAM (object);
211 g_value_set_int (value, unix_stream->priv->fd);
214 g_value_set_boolean (value, unix_stream->priv->close_fd);
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 g_unix_input_stream_init (GUnixInputStream *unix_stream)
224 unix_stream->priv = g_unix_input_stream_get_instance_private (unix_stream);
225 unix_stream->priv->fd = -1;
226 unix_stream->priv->close_fd = TRUE;
230 * g_unix_input_stream_new:
231 * @fd: a UNIX file descriptor
232 * @close_fd: %TRUE to close the file descriptor when done
234 * Creates a new #GUnixInputStream for the given @fd.
236 * If @close_fd is %TRUE, the file descriptor will be closed
237 * when the stream is closed.
239 * Returns: a new #GUnixInputStream
242 g_unix_input_stream_new (gint fd,
245 GUnixInputStream *stream;
247 g_return_val_if_fail (fd != -1, NULL);
249 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
251 "close-fd", close_fd,
254 return G_INPUT_STREAM (stream);
258 * g_unix_input_stream_set_close_fd:
259 * @stream: a #GUnixInputStream
260 * @close_fd: %TRUE to close the file descriptor when done
262 * Sets whether the file descriptor of @stream shall be closed
263 * when the stream is closed.
268 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
271 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
273 close_fd = close_fd != FALSE;
274 if (stream->priv->close_fd != close_fd)
276 stream->priv->close_fd = close_fd;
277 g_object_notify (G_OBJECT (stream), "close-fd");
282 * g_unix_input_stream_get_close_fd:
283 * @stream: a #GUnixInputStream
285 * Returns whether the file descriptor of @stream will be
286 * closed when the stream is closed.
288 * Returns: %TRUE if the file descriptor is closed when done
293 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
295 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
297 return stream->priv->close_fd;
301 * g_unix_input_stream_get_fd:
302 * @stream: a #GUnixInputStream
304 * Return the UNIX file descriptor that the stream reads from.
306 * Returns: The file descriptor of @stream
311 g_unix_input_stream_get_fd (GUnixInputStream *stream)
313 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
315 return stream->priv->fd;
319 g_unix_input_stream_read (GInputStream *stream,
322 GCancellable *cancellable,
325 GUnixInputStream *unix_stream;
331 unix_stream = G_UNIX_INPUT_STREAM (stream);
333 poll_fds[0].fd = unix_stream->priv->fd;
334 poll_fds[0].events = G_IO_IN;
335 if (unix_stream->priv->can_poll &&
336 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
345 poll_fds[0].revents = poll_fds[1].revents = 0;
348 poll_ret = g_poll (poll_fds, nfds, -1);
351 while (poll_ret == -1 && errsv == EINTR);
355 g_set_error (error, G_IO_ERROR,
356 g_io_error_from_errno (errsv),
357 _("Error reading from file descriptor: %s"),
362 if (g_cancellable_set_error_if_cancelled (cancellable, error))
365 if (!poll_fds[0].revents)
368 res = read (unix_stream->priv->fd, buffer, count);
373 if (errsv == EINTR || errsv == EAGAIN)
376 g_set_error (error, G_IO_ERROR,
377 g_io_error_from_errno (errsv),
378 _("Error reading from file descriptor: %s"),
386 g_cancellable_release_fd (cancellable);
391 g_unix_input_stream_close (GInputStream *stream,
392 GCancellable *cancellable,
395 GUnixInputStream *unix_stream;
398 unix_stream = G_UNIX_INPUT_STREAM (stream);
400 if (!unix_stream->priv->close_fd)
403 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
404 res = close (unix_stream->priv->fd);
409 g_set_error (error, G_IO_ERROR,
410 g_io_error_from_errno (errsv),
411 _("Error closing file descriptor: %s"),
419 g_unix_input_stream_skip_async (GInputStream *stream,
422 GCancellable *cancellable,
423 GAsyncReadyCallback callback,
426 g_warn_if_reached ();
427 /* TODO: Not implemented */
431 g_unix_input_stream_skip_finish (GInputStream *stream,
432 GAsyncResult *result,
435 g_warn_if_reached ();
437 /* TODO: Not implemented */
441 g_unix_input_stream_pollable_can_poll (GPollableInputStream *stream)
443 return G_UNIX_INPUT_STREAM (stream)->priv->can_poll;
447 g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream)
449 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
453 poll_fd.fd = unix_stream->priv->fd;
454 poll_fd.events = G_IO_IN;
458 result = g_poll (&poll_fd, 1, 0);
459 while (result == -1 && errno == EINTR);
461 return poll_fd.revents != 0;
465 g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
466 GCancellable *cancellable)
468 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
469 GSource *inner_source, *cancellable_source, *pollable_source;
471 pollable_source = g_pollable_source_new (G_OBJECT (stream));
473 inner_source = g_unix_fd_source_new (unix_stream->priv->fd, G_IO_IN);
474 g_source_set_dummy_callback (inner_source);
475 g_source_add_child_source (pollable_source, inner_source);
476 g_source_unref (inner_source);
480 cancellable_source = g_cancellable_source_new (cancellable);
481 g_source_set_dummy_callback (cancellable_source);
482 g_source_add_child_source (pollable_source, cancellable_source);
483 g_source_unref (cancellable_source);
486 return pollable_source;