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"
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 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
66 struct _GUnixInputStreamPrivate {
71 static void g_unix_input_stream_set_property (GObject *object,
75 static void g_unix_input_stream_get_property (GObject *object,
79 static gssize g_unix_input_stream_read (GInputStream *stream,
82 GCancellable *cancellable,
84 static gboolean g_unix_input_stream_close (GInputStream *stream,
85 GCancellable *cancellable,
87 static void g_unix_input_stream_read_async (GInputStream *stream,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
94 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
97 static void g_unix_input_stream_skip_async (GInputStream *stream,
100 GCancellable *cancellable,
101 GAsyncReadyCallback callback,
103 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
104 GAsyncResult *result,
106 static void g_unix_input_stream_close_async (GInputStream *stream,
108 GCancellable *cancellable,
109 GAsyncReadyCallback callback,
111 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
112 GAsyncResult *result,
117 g_unix_input_stream_finalize (GObject *object)
119 GUnixInputStream *stream;
121 stream = G_UNIX_INPUT_STREAM (object);
123 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
127 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
129 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
130 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
132 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
134 gobject_class->get_property = g_unix_input_stream_get_property;
135 gobject_class->set_property = g_unix_input_stream_set_property;
136 gobject_class->finalize = g_unix_input_stream_finalize;
138 stream_class->read_fn = g_unix_input_stream_read;
139 stream_class->close_fn = g_unix_input_stream_close;
140 stream_class->read_async = g_unix_input_stream_read_async;
141 stream_class->read_finish = g_unix_input_stream_read_finish;
144 /* TODO: Implement instead of using fallbacks */
145 stream_class->skip_async = g_unix_input_stream_skip_async;
146 stream_class->skip_finish = g_unix_input_stream_skip_finish;
148 stream_class->close_async = g_unix_input_stream_close_async;
149 stream_class->close_finish = g_unix_input_stream_close_finish;
152 * GUnixInputStream:fd:
154 * The file descriptor that the stream reads from.
158 g_object_class_install_property (gobject_class,
160 g_param_spec_int ("fd",
161 _("File descriptor"),
162 _("The file descriptor to read from"),
163 G_MININT, G_MAXINT, -1,
164 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
167 * GUnixInputStream:close-fd:
169 * Whether to close the file descriptor when the stream is closed.
173 g_object_class_install_property (gobject_class,
175 g_param_spec_boolean ("close-fd",
176 _("Close file descriptor"),
177 _("Whether to close the file descriptor when the stream is closed"),
179 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
183 g_unix_input_stream_set_property (GObject *object,
188 GUnixInputStream *unix_stream;
190 unix_stream = G_UNIX_INPUT_STREAM (object);
195 unix_stream->priv->fd = g_value_get_int (value);
198 unix_stream->priv->close_fd = g_value_get_boolean (value);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 g_unix_input_stream_get_property (GObject *object,
212 GUnixInputStream *unix_stream;
214 unix_stream = G_UNIX_INPUT_STREAM (object);
219 g_value_set_int (value, unix_stream->priv->fd);
222 g_value_set_boolean (value, unix_stream->priv->close_fd);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 g_unix_input_stream_init (GUnixInputStream *unix_stream)
232 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
233 G_TYPE_UNIX_INPUT_STREAM,
234 GUnixInputStreamPrivate);
236 unix_stream->priv->fd = -1;
237 unix_stream->priv->close_fd = TRUE;
241 * g_unix_input_stream_new:
242 * @fd: a UNIX file descriptor
243 * @close_fd: %TRUE to close the file descriptor when done
245 * Creates a new #GUnixInputStream for the given @fd.
247 * If @close_fd is %TRUE, the file descriptor will be closed
248 * when the stream is closed.
250 * Returns: a new #GUnixInputStream
253 g_unix_input_stream_new (gint fd,
256 GUnixInputStream *stream;
258 g_return_val_if_fail (fd != -1, NULL);
260 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM,
262 "close-fd", close_fd,
265 return G_INPUT_STREAM (stream);
269 * g_unix_input_stream_set_close_fd:
270 * @stream: a #GUnixInputStream
271 * @close_fd: %TRUE to close the file descriptor when done
273 * Sets whether the file descriptor of @stream shall be closed
274 * when the stream is closed.
279 g_unix_input_stream_set_close_fd (GUnixInputStream *stream,
282 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream));
284 close_fd = close_fd != FALSE;
285 if (stream->priv->close_fd != close_fd)
287 stream->priv->close_fd = close_fd;
288 g_object_notify (G_OBJECT (stream), "close-fd");
293 * g_unix_input_stream_get_close_fd:
294 * @stream: a #GUnixInputStream
296 * Returns whether the file descriptor of @stream will be
297 * closed when the stream is closed.
299 * Return value: %TRUE if the file descriptor is closed when done
304 g_unix_input_stream_get_close_fd (GUnixInputStream *stream)
306 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), FALSE);
308 return stream->priv->close_fd;
312 * g_unix_input_stream_get_fd:
313 * @stream: a #GUnixInputStream
315 * Return the UNIX file descriptor that the stream reads from.
317 * Return value: The file descriptor of @stream
322 g_unix_input_stream_get_fd (GUnixInputStream *stream)
324 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream), -1);
326 return stream->priv->fd;
330 g_unix_input_stream_read (GInputStream *stream,
333 GCancellable *cancellable,
336 GUnixInputStream *unix_stream;
341 unix_stream = G_UNIX_INPUT_STREAM (stream);
345 poll_fds[0].fd = unix_stream->priv->fd;
346 poll_fds[0].events = G_IO_IN;
347 g_cancellable_make_pollfd (cancellable, &poll_fds[1]);
349 poll_ret = g_poll (poll_fds, 2, -1);
350 while (poll_ret == -1 && errno == EINTR);
356 g_set_error (error, G_IO_ERROR,
357 g_io_error_from_errno (errsv),
358 _("Error reading from unix: %s"),
366 if (g_cancellable_set_error_if_cancelled (cancellable, error))
368 res = read (unix_stream->priv->fd, buffer, count);
376 g_set_error (error, G_IO_ERROR,
377 g_io_error_from_errno (errsv),
378 _("Error reading from unix: %s"),
389 g_unix_input_stream_close (GInputStream *stream,
390 GCancellable *cancellable,
393 GUnixInputStream *unix_stream;
396 unix_stream = G_UNIX_INPUT_STREAM (stream);
398 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 unix: %s"),
423 GAsyncReadyCallback callback;
425 GCancellable *cancellable;
426 GUnixInputStream *stream;
430 read_async_cb (ReadAsyncData *data,
431 GIOCondition condition,
434 GSimpleAsyncResult *simple;
435 GError *error = NULL;
438 /* We know that we can read from fd once without blocking */
441 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
446 count_read = read (data->stream->priv->fd, data->buffer, data->count);
447 if (count_read == -1)
454 g_set_error (&error, G_IO_ERROR,
455 g_io_error_from_errno (errsv),
456 _("Error reading from unix: %s"),
462 simple = g_simple_async_result_new (G_OBJECT (data->stream),
465 g_unix_input_stream_read_async);
467 g_simple_async_result_set_op_res_gssize (simple, count_read);
469 if (count_read == -1)
471 g_simple_async_result_set_from_error (simple, error);
472 g_error_free (error);
475 /* Complete immediately, not in idle, since we're already in a mainloop callout */
476 g_simple_async_result_complete (simple);
477 g_object_unref (simple);
483 g_unix_input_stream_read_async (GInputStream *stream,
487 GCancellable *cancellable,
488 GAsyncReadyCallback callback,
492 GUnixInputStream *unix_stream;
495 unix_stream = G_UNIX_INPUT_STREAM (stream);
497 data = g_new0 (ReadAsyncData, 1);
499 data->buffer = buffer;
500 data->callback = callback;
501 data->user_data = user_data;
502 data->cancellable = cancellable;
503 data->stream = unix_stream;
505 source = _g_fd_source_new (unix_stream->priv->fd,
509 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
510 g_source_attach (source, NULL);
512 g_source_unref (source);
516 g_unix_input_stream_read_finish (GInputStream *stream,
517 GAsyncResult *result,
520 GSimpleAsyncResult *simple;
523 simple = G_SIMPLE_ASYNC_RESULT (result);
524 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
526 nread = g_simple_async_result_get_op_res_gssize (simple);
531 g_unix_input_stream_skip_async (GInputStream *stream,
534 GCancellable *cancellable,
535 GAsyncReadyCallback callback,
538 g_warn_if_reached ();
539 /* TODO: Not implemented */
543 g_unix_input_stream_skip_finish (GInputStream *stream,
544 GAsyncResult *result,
547 g_warn_if_reached ();
549 /* TODO: Not implemented */
554 GInputStream *stream;
555 GAsyncReadyCallback callback;
560 close_async_data_free (gpointer _data)
562 CloseAsyncData *data = _data;
568 close_async_cb (CloseAsyncData *data)
570 GUnixInputStream *unix_stream;
571 GSimpleAsyncResult *simple;
572 GError *error = NULL;
576 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
578 if (!unix_stream->priv->close_fd)
586 res = close (unix_stream->priv->fd);
591 g_set_error (&error, G_IO_ERROR,
592 g_io_error_from_errno (errsv),
593 _("Error closing unix: %s"),
602 simple = g_simple_async_result_new (G_OBJECT (data->stream),
605 g_unix_input_stream_close_async);
609 g_simple_async_result_set_from_error (simple, error);
610 g_error_free (error);
613 /* Complete immediately, not in idle, since we're already in a mainloop callout */
614 g_simple_async_result_complete (simple);
615 g_object_unref (simple);
621 g_unix_input_stream_close_async (GInputStream *stream,
623 GCancellable *cancellable,
624 GAsyncReadyCallback callback,
628 CloseAsyncData *data;
630 data = g_new0 (CloseAsyncData, 1);
632 data->stream = stream;
633 data->callback = callback;
634 data->user_data = user_data;
636 idle = g_idle_source_new ();
637 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
638 g_source_attach (idle, NULL);
639 g_source_unref (idle);
643 g_unix_input_stream_close_finish (GInputStream *stream,
644 GAsyncResult *result,
647 /* Failures handled in generic close_finish code */
651 #define __G_UNIX_INPUT_STREAM_C__
652 #include "gioaliasdef.c"