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);
198 g_set_error (error, G_IO_ERROR,
199 g_io_error_from_errno (errno),
200 _("Error reading from unix: %s"),
208 if (g_cancellable_set_error_if_cancelled (cancellable, error))
210 res = read (unix_stream->priv->fd, buffer, count);
216 g_set_error (error, G_IO_ERROR,
217 g_io_error_from_errno (errno),
218 _("Error reading from unix: %s"),
229 g_unix_input_stream_close (GInputStream *stream,
230 GCancellable *cancellable,
233 GUnixInputStream *unix_stream;
236 unix_stream = G_UNIX_INPUT_STREAM (stream);
238 if (!unix_stream->priv->close_fd_at_close)
243 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
244 res = close (unix_stream->priv->fd);
246 g_set_error (error, G_IO_ERROR,
247 g_io_error_from_errno (errno),
248 _("Error closing unix: %s"),
259 GAsyncReadyCallback callback;
261 GCancellable *cancellable;
262 GUnixInputStream *stream;
266 read_async_cb (ReadAsyncData *data,
267 GIOCondition condition,
270 GSimpleAsyncResult *simple;
271 GError *error = NULL;
274 /* We know that we can read from fd once without blocking */
277 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
282 count_read = read (data->stream->priv->fd, data->buffer, data->count);
283 if (count_read == -1)
288 g_set_error (&error, G_IO_ERROR,
289 g_io_error_from_errno (errno),
290 _("Error reading from unix: %s"),
296 simple = g_simple_async_result_new (G_OBJECT (data->stream),
299 g_unix_input_stream_read_async);
301 g_simple_async_result_set_op_res_gssize (simple, count_read);
303 if (count_read == -1)
305 g_simple_async_result_set_from_error (simple, error);
306 g_error_free (error);
309 /* Complete immediately, not in idle, since we're already in a mainloop callout */
310 g_simple_async_result_complete (simple);
311 g_object_unref (simple);
317 g_unix_input_stream_read_async (GInputStream *stream,
321 GCancellable *cancellable,
322 GAsyncReadyCallback callback,
326 GUnixInputStream *unix_stream;
329 unix_stream = G_UNIX_INPUT_STREAM (stream);
331 data = g_new0 (ReadAsyncData, 1);
333 data->buffer = buffer;
334 data->callback = callback;
335 data->user_data = user_data;
336 data->cancellable = cancellable;
337 data->stream = unix_stream;
339 source = _g_fd_source_new (unix_stream->priv->fd,
343 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
344 g_source_attach (source, NULL);
346 g_source_unref (source);
350 g_unix_input_stream_read_finish (GInputStream *stream,
351 GAsyncResult *result,
354 GSimpleAsyncResult *simple;
357 simple = G_SIMPLE_ASYNC_RESULT (result);
358 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
360 nread = g_simple_async_result_get_op_res_gssize (simple);
365 g_unix_input_stream_skip_async (GInputStream *stream,
368 GCancellable *cancellable,
369 GAsyncReadyCallback callback,
372 g_warn_if_reached ();
373 /* TODO: Not implemented */
377 g_unix_input_stream_skip_finish (GInputStream *stream,
378 GAsyncResult *result,
381 g_warn_if_reached ();
382 /* TODO: Not implemented */
387 GInputStream *stream;
388 GAsyncReadyCallback callback;
393 close_async_data_free (gpointer _data)
395 CloseAsyncData *data = _data;
401 close_async_cb (CloseAsyncData *data)
403 GUnixInputStream *unix_stream;
404 GSimpleAsyncResult *simple;
405 GError *error = NULL;
409 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
411 if (!unix_stream->priv->close_fd_at_close)
419 res = close (unix_stream->priv->fd);
421 g_set_error (&error, G_IO_ERROR,
422 g_io_error_from_errno (errno),
423 _("Error closing unix: %s"),
431 simple = g_simple_async_result_new (G_OBJECT (data->stream),
434 g_unix_input_stream_close_async);
438 g_simple_async_result_set_from_error (simple, error);
439 g_error_free (error);
442 /* Complete immediately, not in idle, since we're already in a mainloop callout */
443 g_simple_async_result_complete (simple);
444 g_object_unref (simple);
450 g_unix_input_stream_close_async (GInputStream *stream,
452 GCancellable *cancellable,
453 GAsyncReadyCallback callback,
457 CloseAsyncData *data;
459 data = g_new0 (CloseAsyncData, 1);
461 data->stream = stream;
462 data->callback = callback;
463 data->user_data = user_data;
465 idle = g_idle_source_new ();
466 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
467 g_source_attach (idle, NULL);
468 g_source_unref (idle);
472 g_unix_input_stream_close_finish (GInputStream *stream,
473 GAsyncResult *result,
476 /* Failures handled in generic close_finish code */
480 #define __G_UNIX_INPUT_STREAM_C__
481 #include "gioaliasdef.c"