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>
34 #include <glib/gstdio.h>
36 #include "gsimpleasyncresult.h"
37 #include "gunixinputstream.h"
38 #include "gcancellable.h"
39 #include "gasynchelper.h"
45 * SECTION:gunixinputstream
46 * @short_description: Streaming input operations for Unix file descriptors
47 * @include: gio/gunixinputstream.h
48 * @see_also: #GInputStream
50 * #GUnixInputStream implements #GInputStream for reading from a
51 * unix file descriptor, including asynchronous operations. The file
52 * descriptor much be selectable, so it doesn't work with opened files.
55 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
57 struct _GUnixInputStreamPrivate {
59 gboolean close_fd_at_close;
62 static gssize g_unix_input_stream_read (GInputStream *stream,
65 GCancellable *cancellable,
67 static gboolean g_unix_input_stream_close (GInputStream *stream,
68 GCancellable *cancellable,
70 static void g_unix_input_stream_read_async (GInputStream *stream,
74 GCancellable *cancellable,
75 GAsyncReadyCallback callback,
77 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
80 static void g_unix_input_stream_skip_async (GInputStream *stream,
83 GCancellable *cancellable,
84 GAsyncReadyCallback callback,
86 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
89 static void g_unix_input_stream_close_async (GInputStream *stream,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
94 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
100 g_unix_input_stream_finalize (GObject *object)
102 GUnixInputStream *stream;
104 stream = G_UNIX_INPUT_STREAM (object);
106 if (G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize)
107 (*G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize) (object);
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 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
118 gobject_class->finalize = g_unix_input_stream_finalize;
120 stream_class->read_fn = g_unix_input_stream_read;
121 stream_class->close_fn = g_unix_input_stream_close;
122 stream_class->read_async = g_unix_input_stream_read_async;
123 stream_class->read_finish = g_unix_input_stream_read_finish;
126 /* TODO: Implement instead of using fallbacks */
127 stream_class->skip_async = g_unix_input_stream_skip_async;
128 stream_class->skip_finish = g_unix_input_stream_skip_finish;
130 stream_class->close_async = g_unix_input_stream_close_async;
131 stream_class->close_finish = g_unix_input_stream_close_finish;
135 g_unix_input_stream_init (GUnixInputStream *unix_stream)
137 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
138 G_TYPE_UNIX_INPUT_STREAM,
139 GUnixInputStreamPrivate);
143 * g_unix_input_stream_new:
144 * @fd: unix file descriptor.
145 * @close_fd_at_close: a #gboolean.
147 * Creates a new #GUnixInputStream for the given @fd. If @close_fd_at_close
148 * is %TRUE, the file descriptor will be closed when the stream is closed.
150 * Returns: a #GUnixInputStream.
153 g_unix_input_stream_new (int fd,
154 gboolean close_fd_at_close)
156 GUnixInputStream *stream;
158 g_return_val_if_fail (fd != -1, NULL);
160 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM, NULL);
162 stream->priv->fd = fd;
163 stream->priv->close_fd_at_close = close_fd_at_close;
165 return G_INPUT_STREAM (stream);
169 g_unix_input_stream_read (GInputStream *stream,
172 GCancellable *cancellable,
175 GUnixInputStream *unix_stream;
177 struct pollfd poll_fds[2];
181 unix_stream = G_UNIX_INPUT_STREAM (stream);
183 cancel_fd = g_cancellable_get_fd (cancellable);
188 poll_fds[0].events = POLLIN;
189 poll_fds[0].fd = unix_stream->priv->fd;
190 poll_fds[1].events = POLLIN;
191 poll_fds[1].fd = cancel_fd;
192 poll_ret = poll (poll_fds, 2, -1);
194 while (poll_ret == -1 && errno == EINTR);
200 g_set_error (error, G_IO_ERROR,
201 g_io_error_from_errno (errsv),
202 _("Error reading from unix: %s"),
210 if (g_cancellable_set_error_if_cancelled (cancellable, error))
212 res = read (unix_stream->priv->fd, buffer, count);
220 g_set_error (error, G_IO_ERROR,
221 g_io_error_from_errno (errsv),
222 _("Error reading from unix: %s"),
233 g_unix_input_stream_close (GInputStream *stream,
234 GCancellable *cancellable,
237 GUnixInputStream *unix_stream;
240 unix_stream = G_UNIX_INPUT_STREAM (stream);
242 if (!unix_stream->priv->close_fd_at_close)
247 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
248 res = close (unix_stream->priv->fd);
253 g_set_error (error, G_IO_ERROR,
254 g_io_error_from_errno (errsv),
255 _("Error closing unix: %s"),
267 GAsyncReadyCallback callback;
269 GCancellable *cancellable;
270 GUnixInputStream *stream;
274 read_async_cb (ReadAsyncData *data,
275 GIOCondition condition,
278 GSimpleAsyncResult *simple;
279 GError *error = NULL;
282 /* We know that we can read from fd once without blocking */
285 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
290 count_read = read (data->stream->priv->fd, data->buffer, data->count);
291 if (count_read == -1)
298 g_set_error (&error, G_IO_ERROR,
299 g_io_error_from_errno (errsv),
300 _("Error reading from unix: %s"),
306 simple = g_simple_async_result_new (G_OBJECT (data->stream),
309 g_unix_input_stream_read_async);
311 g_simple_async_result_set_op_res_gssize (simple, count_read);
313 if (count_read == -1)
315 g_simple_async_result_set_from_error (simple, error);
316 g_error_free (error);
319 /* Complete immediately, not in idle, since we're already in a mainloop callout */
320 g_simple_async_result_complete (simple);
321 g_object_unref (simple);
327 g_unix_input_stream_read_async (GInputStream *stream,
331 GCancellable *cancellable,
332 GAsyncReadyCallback callback,
336 GUnixInputStream *unix_stream;
339 unix_stream = G_UNIX_INPUT_STREAM (stream);
341 data = g_new0 (ReadAsyncData, 1);
343 data->buffer = buffer;
344 data->callback = callback;
345 data->user_data = user_data;
346 data->cancellable = cancellable;
347 data->stream = unix_stream;
349 source = _g_fd_source_new (unix_stream->priv->fd,
353 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
354 g_source_attach (source, NULL);
356 g_source_unref (source);
360 g_unix_input_stream_read_finish (GInputStream *stream,
361 GAsyncResult *result,
364 GSimpleAsyncResult *simple;
367 simple = G_SIMPLE_ASYNC_RESULT (result);
368 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
370 nread = g_simple_async_result_get_op_res_gssize (simple);
375 g_unix_input_stream_skip_async (GInputStream *stream,
378 GCancellable *cancellable,
379 GAsyncReadyCallback callback,
382 g_warn_if_reached ();
383 /* TODO: Not implemented */
387 g_unix_input_stream_skip_finish (GInputStream *stream,
388 GAsyncResult *result,
391 g_warn_if_reached ();
393 /* TODO: Not implemented */
398 GInputStream *stream;
399 GAsyncReadyCallback callback;
404 close_async_data_free (gpointer _data)
406 CloseAsyncData *data = _data;
412 close_async_cb (CloseAsyncData *data)
414 GUnixInputStream *unix_stream;
415 GSimpleAsyncResult *simple;
416 GError *error = NULL;
420 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
422 if (!unix_stream->priv->close_fd_at_close)
430 res = close (unix_stream->priv->fd);
435 g_set_error (&error, G_IO_ERROR,
436 g_io_error_from_errno (errsv),
437 _("Error closing unix: %s"),
446 simple = g_simple_async_result_new (G_OBJECT (data->stream),
449 g_unix_input_stream_close_async);
453 g_simple_async_result_set_from_error (simple, error);
454 g_error_free (error);
457 /* Complete immediately, not in idle, since we're already in a mainloop callout */
458 g_simple_async_result_complete (simple);
459 g_object_unref (simple);
465 g_unix_input_stream_close_async (GInputStream *stream,
467 GCancellable *cancellable,
468 GAsyncReadyCallback callback,
472 CloseAsyncData *data;
474 data = g_new0 (CloseAsyncData, 1);
476 data->stream = stream;
477 data->callback = callback;
478 data->user_data = user_data;
480 idle = g_idle_source_new ();
481 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
482 g_source_attach (idle, NULL);
483 g_source_unref (idle);
487 g_unix_input_stream_close_finish (GInputStream *stream,
488 GAsyncResult *result,
491 /* Failures handled in generic close_finish code */
495 #define __G_UNIX_INPUT_STREAM_C__
496 #include "gioaliasdef.c"