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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include <sys/types.h>
31 #include <glib/gstdio.h>
32 #include <glib/glib-unix.h>
34 #include "gsimpleasyncresult.h"
35 #include "gunixinputstream.h"
36 #include "gcancellable.h"
37 #include "gasynchelper.h"
38 #include "gfiledescriptorbased.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 UNIX
49 * file descriptor, including asynchronous operations. (If the file
50 * descriptor refers to a socket or pipe, this will use poll() to do
51 * asynchronous I/O. If it refers to a regular file, it will fall back
52 * to doing asynchronous I/O in another thread.)
54 * Note that `<gio/gunixinputstream.h>` belongs to the UNIX-specific GIO
55 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
65 struct _GUnixInputStreamPrivate {
68 guint is_pipe_or_socket : 1;
71 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface);
72 static void g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
74 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM,
75 G_ADD_PRIVATE (GUnixInputStream)
76 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM,
77 g_unix_input_stream_pollable_iface_init)
78 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
79 g_unix_input_stream_file_descriptor_based_iface_init)
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_class_init (GUnixInputStreamClass *klass)
124 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
125 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
127 gobject_class->get_property = g_unix_input_stream_get_property;
128 gobject_class->set_property = g_unix_input_stream_set_property;
130 stream_class->read_fn = g_unix_input_stream_read;
131 stream_class->close_fn = g_unix_input_stream_close;
134 /* TODO: Implement instead of using fallbacks */
135 stream_class->skip_async = g_unix_input_stream_skip_async;
136 stream_class->skip_finish = g_unix_input_stream_skip_finish;
138 stream_class->close_async = g_unix_input_stream_close_async;
139 stream_class->close_finish = g_unix_input_stream_close_finish;
142 * GUnixInputStream:fd:
144 * The file descriptor that the stream reads from.
148 g_object_class_install_property (gobject_class,
150 g_param_spec_int ("fd",
151 P_("File descriptor"),
152 P_("The file descriptor to read from"),
153 G_MININT, G_MAXINT, -1,
154 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
157 * GUnixInputStream:close-fd:
159 * Whether to close the file descriptor when the stream is closed.
163 g_object_class_install_property (gobject_class,
165 g_param_spec_boolean ("close-fd",
166 P_("Close file descriptor"),
167 P_("Whether to close the file descriptor when the stream is closed"),
169 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
173 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface *iface)
175 iface->can_poll = g_unix_input_stream_pollable_can_poll;
176 iface->is_readable = g_unix_input_stream_pollable_is_readable;
177 iface->create_source = g_unix_input_stream_pollable_create_source;
181 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
183 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_input_stream_get_fd;
187 g_unix_input_stream_set_property (GObject *object,
192 GUnixInputStream *unix_stream;
194 unix_stream = G_UNIX_INPUT_STREAM (object);
199 unix_stream->priv->fd = g_value_get_int (value);
200 if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
201 unix_stream->priv->is_pipe_or_socket = TRUE;
203 unix_stream->priv->is_pipe_or_socket = FALSE;
206 unix_stream->priv->close_fd = g_value_get_boolean (value);
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215 g_unix_input_stream_get_property (GObject *object,
220 GUnixInputStream *unix_stream;
222 unix_stream = G_UNIX_INPUT_STREAM (object);
227 g_value_set_int (value, unix_stream->priv->fd);
230 g_value_set_boolean (value, unix_stream->priv->close_fd);
233 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238 g_unix_input_stream_init (GUnixInputStream *unix_stream)
240 unix_stream->priv = g_unix_input_stream_get_instance_private (unix_stream);
241 unix_stream->priv->fd = -1;
242 unix_stream->priv->close_fd = TRUE;
246 * g_unix_input_stream_new:
247 * @fd: a UNIX file descriptor
248 * @close_fd: %TRUE to close the file descriptor when done
250 * Creates a new #GUnixInputStream for the given @fd.
252 * If @close_fd is %TRUE, the file descriptor will be closed
253 * when the stream is closed.
255 * Returns: a new #GUnixInputStream
258 g_unix_input_stream_new (gint fd,
261 GUnixInputStream *stream;
263 g_return_val_if_fail (fd != -1, NULL);
265 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
267 "close-fd", close_fd,
270 return G_INPUT_STREAM (stream);
274 * g_unix_input_stream_set_close_fd:
275 * @stream: a #GUnixInputStream
276 * @close_fd: %TRUE to close the file descriptor when done
278 * Sets whether the file descriptor of @stream shall be closed
279 * when the stream is closed.
284 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
287 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
289 close_fd = close_fd != FALSE;
290 if (stream->priv->close_fd != close_fd)
292 stream->priv->close_fd = close_fd;
293 g_object_notify (G_OBJECT (stream), "close-fd");
298 * g_unix_input_stream_get_close_fd:
299 * @stream: a #GUnixInputStream
301 * Returns whether the file descriptor of @stream will be
302 * closed when the stream is closed.
304 * Returns: %TRUE if the file descriptor is closed when done
309 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
311 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
313 return stream->priv->close_fd;
317 * g_unix_input_stream_get_fd:
318 * @stream: a #GUnixInputStream
320 * Return the UNIX file descriptor that the stream reads from.
322 * Returns: The file descriptor of @stream
327 g_unix_input_stream_get_fd (GUnixInputStream *stream)
329 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
331 return stream->priv->fd;
335 g_unix_input_stream_read (GInputStream *stream,
338 GCancellable *cancellable,
341 GUnixInputStream *unix_stream;
347 unix_stream = G_UNIX_INPUT_STREAM (stream);
349 poll_fds[0].fd = unix_stream->priv->fd;
350 poll_fds[0].events = G_IO_IN;
351 if (unix_stream->priv->is_pipe_or_socket &&
352 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
359 poll_fds[0].revents = poll_fds[1].revents = 0;
361 poll_ret = g_poll (poll_fds, nfds, -1);
362 while (poll_ret == -1 && errno == EINTR);
368 g_set_error (error, G_IO_ERROR,
369 g_io_error_from_errno (errsv),
370 _("Error reading from file descriptor: %s"),
375 if (g_cancellable_set_error_if_cancelled (cancellable, error))
378 if (!poll_fds[0].revents)
381 res = read (unix_stream->priv->fd, buffer, count);
386 if (errsv == EINTR || errsv == EAGAIN)
389 g_set_error (error, G_IO_ERROR,
390 g_io_error_from_errno (errsv),
391 _("Error reading from file descriptor: %s"),
399 g_cancellable_release_fd (cancellable);
404 g_unix_input_stream_close (GInputStream *stream,
405 GCancellable *cancellable,
408 GUnixInputStream *unix_stream;
411 unix_stream = G_UNIX_INPUT_STREAM (stream);
413 if (!unix_stream->priv->close_fd)
416 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
417 res = close (unix_stream->priv->fd);
422 g_set_error (error, G_IO_ERROR,
423 g_io_error_from_errno (errsv),
424 _("Error closing file descriptor: %s"),
432 g_unix_input_stream_skip_async (GInputStream *stream,
435 GCancellable *cancellable,
436 GAsyncReadyCallback callback,
439 g_warn_if_reached ();
440 /* TODO: Not implemented */
444 g_unix_input_stream_skip_finish (GInputStream *stream,
445 GAsyncResult *result,
448 g_warn_if_reached ();
450 /* TODO: Not implemented */
454 g_unix_input_stream_close_async (GInputStream *stream,
456 GCancellable *cancellable,
457 GAsyncReadyCallback callback,
461 GError *error = NULL;
463 task = g_task_new (stream, cancellable, callback, user_data);
464 g_task_set_priority (task, io_priority);
466 if (g_unix_input_stream_close (stream, cancellable, &error))
467 g_task_return_boolean (task, TRUE);
469 g_task_return_error (task, error);
470 g_object_unref (task);
474 g_unix_input_stream_close_finish (GInputStream *stream,
475 GAsyncResult *result,
478 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
480 return g_task_propagate_boolean (G_TASK (result), error);
484 g_unix_input_stream_pollable_can_poll (GPollableInputStream *stream)
486 return G_UNIX_INPUT_STREAM (stream)->priv->is_pipe_or_socket;
490 g_unix_input_stream_pollable_is_readable (GPollableInputStream *stream)
492 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
496 poll_fd.fd = unix_stream->priv->fd;
497 poll_fd.events = G_IO_IN;
501 result = g_poll (&poll_fd, 1, 0);
502 while (result == -1 && errno == EINTR);
504 return poll_fd.revents != 0;
508 g_unix_input_stream_pollable_create_source (GPollableInputStream *stream,
509 GCancellable *cancellable)
511 GUnixInputStream *unix_stream = G_UNIX_INPUT_STREAM (stream);
512 GSource *inner_source, *cancellable_source, *pollable_source;
514 pollable_source = g_pollable_source_new (G_OBJECT (stream));
516 inner_source = g_unix_fd_source_new (unix_stream->priv->fd, G_IO_IN);
517 g_source_set_dummy_callback (inner_source);
518 g_source_add_child_source (pollable_source, inner_source);
519 g_source_unref (inner_source);
523 cancellable_source = g_cancellable_source_new (cancellable);
524 g_source_set_dummy_callback (cancellable_source);
525 g_source_add_child_source (pollable_source, cancellable_source);
526 g_source_unref (cancellable_source);
529 return pollable_source;