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"
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
49 * UNIX file descriptor, including asynchronous operations. The file
50 * descriptor must be selectable, so it doesn't work with opened files.
52 * Note that <filename><gio/gunixinputstream.h></filename> belongs
53 * to the UNIX-specific GIO interfaces, thus you have to use the
54 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
63 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
65 struct _GUnixInputStreamPrivate {
70 static void g_unix_input_stream_set_property (GObject *object,
74 static void g_unix_input_stream_get_property (GObject *object,
78 static gssize g_unix_input_stream_read (GInputStream *stream,
81 GCancellable *cancellable,
83 static gboolean g_unix_input_stream_close (GInputStream *stream,
84 GCancellable *cancellable,
86 static void g_unix_input_stream_read_async (GInputStream *stream,
90 GCancellable *cancellable,
91 GAsyncReadyCallback callback,
93 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
96 static void g_unix_input_stream_skip_async (GInputStream *stream,
99 GCancellable *cancellable,
100 GAsyncReadyCallback callback,
102 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
103 GAsyncResult *result,
105 static void g_unix_input_stream_close_async (GInputStream *stream,
107 GCancellable *cancellable,
108 GAsyncReadyCallback callback,
110 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
111 GAsyncResult *result,
116 g_unix_input_stream_finalize (GObject *object)
118 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
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 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
129 gobject_class->get_property = g_unix_input_stream_get_property;
130 gobject_class->set_property = g_unix_input_stream_set_property;
131 gobject_class->finalize = g_unix_input_stream_finalize;
133 stream_class->read_fn = g_unix_input_stream_read;
134 stream_class->close_fn = g_unix_input_stream_close;
135 stream_class->read_async = g_unix_input_stream_read_async;
136 stream_class->read_finish = g_unix_input_stream_read_finish;
139 /* TODO: Implement instead of using fallbacks */
140 stream_class->skip_async = g_unix_input_stream_skip_async;
141 stream_class->skip_finish = g_unix_input_stream_skip_finish;
143 stream_class->close_async = g_unix_input_stream_close_async;
144 stream_class->close_finish = g_unix_input_stream_close_finish;
147 * GUnixInputStream:fd:
149 * The file descriptor that the stream reads from.
153 g_object_class_install_property (gobject_class,
155 g_param_spec_int ("fd",
156 P_("File descriptor"),
157 P_("The file descriptor to read from"),
158 G_MININT, G_MAXINT, -1,
159 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
162 * GUnixInputStream:close-fd:
164 * Whether to close the file descriptor when the stream is closed.
168 g_object_class_install_property (gobject_class,
170 g_param_spec_boolean ("close-fd",
171 P_("Close file descriptor"),
172 P_("Whether to close the file descriptor when the stream is closed"),
174 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
178 g_unix_input_stream_set_property (GObject *object,
183 GUnixInputStream *unix_stream;
185 unix_stream = G_UNIX_INPUT_STREAM (object);
190 unix_stream->priv->fd = g_value_get_int (value);
193 unix_stream->priv->close_fd = g_value_get_boolean (value);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 g_unix_input_stream_get_property (GObject *object,
207 GUnixInputStream *unix_stream;
209 unix_stream = G_UNIX_INPUT_STREAM (object);
214 g_value_set_int (value, unix_stream->priv->fd);
217 g_value_set_boolean (value, unix_stream->priv->close_fd);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 g_unix_input_stream_init (GUnixInputStream *unix_stream)
227 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
228 G_TYPE_UNIX_INPUT_STREAM,
229 GUnixInputStreamPrivate);
231 unix_stream->priv->fd = -1;
232 unix_stream->priv->close_fd = TRUE;
236 * g_unix_input_stream_new:
237 * @fd: a UNIX file descriptor
238 * @close_fd: %TRUE to close the file descriptor when done
240 * Creates a new #GUnixInputStream for the given @fd.
242 * If @close_fd is %TRUE, the file descriptor will be closed
243 * when the stream is closed.
245 * Returns: a new #GUnixInputStream
248 g_unix_input_stream_new (gint fd,
251 GUnixInputStream *stream;
253 g_return_val_if_fail (fd != -1, NULL);
255 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
257 "close-fd", close_fd,
260 return G_INPUT_STREAM (stream);
264 * g_unix_input_stream_set_close_fd:
265 * @stream: a #GUnixInputStream
266 * @close_fd: %TRUE to close the file descriptor when done
268 * Sets whether the file descriptor of @stream shall be closed
269 * when the stream is closed.
274 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
277 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
279 close_fd = close_fd != FALSE;
280 if (stream->priv->close_fd != close_fd)
282 stream->priv->close_fd = close_fd;
283 g_object_notify (G_OBJECT (stream), "close-fd");
288 * g_unix_input_stream_get_close_fd:
289 * @stream: a #GUnixInputStream
291 * Returns whether the file descriptor of @stream will be
292 * closed when the stream is closed.
294 * Return value: %TRUE if the file descriptor is closed when done
299 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
301 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
303 return stream->priv->close_fd;
307 * g_unix_input_stream_get_fd:
308 * @stream: a #GUnixInputStream
310 * Return the UNIX file descriptor that the stream reads from.
312 * Return value: The file descriptor of @stream
317 g_unix_input_stream_get_fd (GUnixInputStream *stream)
319 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
321 return stream->priv->fd;
325 g_unix_input_stream_read (GInputStream *stream,
328 GCancellable *cancellable,
331 GUnixInputStream *unix_stream;
336 unix_stream = G_UNIX_INPUT_STREAM (stream);
338 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
340 poll_fds[0].fd = unix_stream->priv->fd;
341 poll_fds[0].events = G_IO_IN;
343 poll_ret = g_poll (poll_fds, 2, -1);
344 while (poll_ret == -1 && errno == EINTR);
345 g_cancellable_release_fd (cancellable);
351 g_set_error (error, G_IO_ERROR,
352 g_io_error_from_errno (errsv),
353 _("Error reading from unix: %s"),
361 if (g_cancellable_set_error_if_cancelled (cancellable, error))
363 res = read (unix_stream->priv->fd, buffer, count);
371 g_set_error (error, G_IO_ERROR,
372 g_io_error_from_errno (errsv),
373 _("Error reading from unix: %s"),
384 g_unix_input_stream_close (GInputStream *stream,
385 GCancellable *cancellable,
388 GUnixInputStream *unix_stream;
391 unix_stream = G_UNIX_INPUT_STREAM (stream);
393 if (!unix_stream->priv->close_fd)
398 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
399 res = close (unix_stream->priv->fd);
404 g_set_error (error, G_IO_ERROR,
405 g_io_error_from_errno (errsv),
406 _("Error closing unix: %s"),
418 GAsyncReadyCallback callback;
420 GCancellable *cancellable;
421 GUnixInputStream *stream;
425 read_async_cb (ReadAsyncData *data,
426 GIOCondition condition,
429 GSimpleAsyncResult *simple;
430 GError *error = NULL;
433 /* We know that we can read from fd once without blocking */
436 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
441 count_read = read (data->stream->priv->fd, data->buffer, data->count);
442 if (count_read == -1)
449 g_set_error (&error, G_IO_ERROR,
450 g_io_error_from_errno (errsv),
451 _("Error reading from unix: %s"),
457 simple = g_simple_async_result_new (G_OBJECT (data->stream),
460 g_unix_input_stream_read_async);
462 g_simple_async_result_set_op_res_gssize (simple, count_read);
464 if (count_read == -1)
466 g_simple_async_result_set_from_error (simple, error);
467 g_error_free (error);
470 /* Complete immediately, not in idle, since we're already in a mainloop callout */
471 g_simple_async_result_complete (simple);
472 g_object_unref (simple);
478 g_unix_input_stream_read_async (GInputStream *stream,
482 GCancellable *cancellable,
483 GAsyncReadyCallback callback,
487 GUnixInputStream *unix_stream;
490 unix_stream = G_UNIX_INPUT_STREAM (stream);
492 data = g_new0 (ReadAsyncData, 1);
494 data->buffer = buffer;
495 data->callback = callback;
496 data->user_data = user_data;
497 data->cancellable = cancellable;
498 data->stream = unix_stream;
500 source = _g_fd_source_new (unix_stream->priv->fd,
503 g_source_set_name (source, "GUnixInputStream");
505 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
506 g_source_attach (source, g_main_context_get_thread_default ());
508 g_source_unref (source);
512 g_unix_input_stream_read_finish (GInputStream *stream,
513 GAsyncResult *result,
516 GSimpleAsyncResult *simple;
519 simple = G_SIMPLE_ASYNC_RESULT (result);
520 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
522 nread = g_simple_async_result_get_op_res_gssize (simple);
527 g_unix_input_stream_skip_async (GInputStream *stream,
530 GCancellable *cancellable,
531 GAsyncReadyCallback callback,
534 g_warn_if_reached ();
535 /* TODO: Not implemented */
539 g_unix_input_stream_skip_finish (GInputStream *stream,
540 GAsyncResult *result,
543 g_warn_if_reached ();
545 /* TODO: Not implemented */
550 GInputStream *stream;
551 GAsyncReadyCallback callback;
556 close_async_data_free (gpointer _data)
558 CloseAsyncData *data = _data;
564 close_async_cb (CloseAsyncData *data)
566 GUnixInputStream *unix_stream;
567 GSimpleAsyncResult *simple;
568 GError *error = NULL;
572 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
574 if (!unix_stream->priv->close_fd)
582 res = close (unix_stream->priv->fd);
587 g_set_error (&error, G_IO_ERROR,
588 g_io_error_from_errno (errsv),
589 _("Error closing unix: %s"),
598 simple = g_simple_async_result_new (G_OBJECT (data->stream),
601 g_unix_input_stream_close_async);
605 g_simple_async_result_set_from_error (simple, error);
606 g_error_free (error);
609 /* Complete immediately, not in idle, since we're already in a mainloop callout */
610 g_simple_async_result_complete (simple);
611 g_object_unref (simple);
617 g_unix_input_stream_close_async (GInputStream *stream,
619 GCancellable *cancellable,
620 GAsyncReadyCallback callback,
624 CloseAsyncData *data;
626 data = g_new0 (CloseAsyncData, 1);
628 data->stream = stream;
629 data->callback = callback;
630 data->user_data = user_data;
632 idle = g_idle_source_new ();
633 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
634 g_source_attach (idle, g_main_context_get_thread_default ());
635 g_source_unref (idle);
639 g_unix_input_stream_close_finish (GInputStream *stream,
640 GAsyncResult *result,
643 /* Failures handled in generic close_finish code */