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 * @see_also: #GInputStream
49 * #GUnixInputStream implements #GInputStream for reading from a
50 * unix file descriptor, including asynchronous operations. The file
51 * descriptor much be selectable, so it doesn't work with opened files.
54 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
56 struct _GUnixInputStreamPrivate {
58 gboolean close_fd_at_close;
61 static gssize g_unix_input_stream_read (GInputStream *stream,
64 GCancellable *cancellable,
66 static gboolean g_unix_input_stream_close (GInputStream *stream,
67 GCancellable *cancellable,
69 static void g_unix_input_stream_read_async (GInputStream *stream,
73 GCancellable *cancellable,
74 GAsyncReadyCallback callback,
76 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
79 static void g_unix_input_stream_skip_async (GInputStream *stream,
82 GCancellable *cancellable,
83 GAsyncReadyCallback callback,
85 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
88 static void g_unix_input_stream_close_async (GInputStream *stream,
90 GCancellable *cancellable,
91 GAsyncReadyCallback callback,
93 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
99 g_unix_input_stream_finalize (GObject *object)
101 GUnixInputStream *stream;
103 stream = G_UNIX_INPUT_STREAM (object);
105 if (G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize)
106 (*G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize) (object);
110 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
112 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
113 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
115 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
117 gobject_class->finalize = g_unix_input_stream_finalize;
119 stream_class->read_fn = g_unix_input_stream_read;
120 stream_class->close_fn = g_unix_input_stream_close;
121 stream_class->read_async = g_unix_input_stream_read_async;
122 stream_class->read_finish = g_unix_input_stream_read_finish;
125 /* TODO: Implement instead of using fallbacks */
126 stream_class->skip_async = g_unix_input_stream_skip_async;
127 stream_class->skip_finish = g_unix_input_stream_skip_finish;
129 stream_class->close_async = g_unix_input_stream_close_async;
130 stream_class->close_finish = g_unix_input_stream_close_finish;
134 g_unix_input_stream_init (GUnixInputStream *unix_stream)
136 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
137 G_TYPE_UNIX_INPUT_STREAM,
138 GUnixInputStreamPrivate);
142 * g_unix_input_stream_new:
143 * @fd: unix file descriptor.
144 * @close_fd_at_close: a #gboolean.
146 * Creates a new #GUnixInputStream for the given @fd. If @close_fd_at_close
147 * is %TRUE, the file descriptor will be closed when the stream is closed.
149 * Returns: a #GUnixInputStream.
152 g_unix_input_stream_new (int fd,
153 gboolean close_fd_at_close)
155 GUnixInputStream *stream;
157 g_return_val_if_fail (fd != -1, NULL);
159 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM, NULL);
161 stream->priv->fd = fd;
162 stream->priv->close_fd_at_close = close_fd_at_close;
164 return G_INPUT_STREAM (stream);
168 g_unix_input_stream_read (GInputStream *stream,
171 GCancellable *cancellable,
174 GUnixInputStream *unix_stream;
176 struct pollfd poll_fds[2];
180 unix_stream = G_UNIX_INPUT_STREAM (stream);
182 cancel_fd = g_cancellable_get_fd (cancellable);
187 poll_fds[0].events = POLLIN;
188 poll_fds[0].fd = unix_stream->priv->fd;
189 poll_fds[1].events = POLLIN;
190 poll_fds[1].fd = cancel_fd;
191 poll_ret = poll (poll_fds, 2, -1);
193 while (poll_ret == -1 && errno == EINTR);
197 g_set_error (error, G_IO_ERROR,
198 g_io_error_from_errno (errno),
199 _("Error reading from unix: %s"),
207 if (g_cancellable_set_error_if_cancelled (cancellable, error))
209 res = read (unix_stream->priv->fd, buffer, count);
215 g_set_error (error, G_IO_ERROR,
216 g_io_error_from_errno (errno),
217 _("Error reading from unix: %s"),
228 g_unix_input_stream_close (GInputStream *stream,
229 GCancellable *cancellable,
232 GUnixInputStream *unix_stream;
235 unix_stream = G_UNIX_INPUT_STREAM (stream);
237 if (!unix_stream->priv->close_fd_at_close)
242 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
243 res = close (unix_stream->priv->fd);
245 g_set_error (error, G_IO_ERROR,
246 g_io_error_from_errno (errno),
247 _("Error closing unix: %s"),
258 GAsyncReadyCallback callback;
260 GCancellable *cancellable;
261 GUnixInputStream *stream;
265 read_async_cb (ReadAsyncData *data,
266 GIOCondition condition,
269 GSimpleAsyncResult *simple;
270 GError *error = NULL;
273 /* We know that we can read from fd once without blocking */
276 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
281 count_read = read (data->stream->priv->fd, data->buffer, data->count);
282 if (count_read == -1)
287 g_set_error (&error, G_IO_ERROR,
288 g_io_error_from_errno (errno),
289 _("Error reading from unix: %s"),
295 simple = g_simple_async_result_new (G_OBJECT (data->stream),
298 g_unix_input_stream_read_async);
300 g_simple_async_result_set_op_res_gssize (simple, count_read);
302 if (count_read == -1)
304 g_simple_async_result_set_from_error (simple, error);
305 g_error_free (error);
308 /* Complete immediately, not in idle, since we're already in a mainloop callout */
309 g_simple_async_result_complete (simple);
310 g_object_unref (simple);
316 g_unix_input_stream_read_async (GInputStream *stream,
320 GCancellable *cancellable,
321 GAsyncReadyCallback callback,
325 GUnixInputStream *unix_stream;
328 unix_stream = G_UNIX_INPUT_STREAM (stream);
330 data = g_new0 (ReadAsyncData, 1);
332 data->buffer = buffer;
333 data->callback = callback;
334 data->user_data = user_data;
335 data->cancellable = cancellable;
336 data->stream = unix_stream;
338 source = _g_fd_source_new (unix_stream->priv->fd,
342 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
343 g_source_attach (source, NULL);
345 g_source_unref (source);
349 g_unix_input_stream_read_finish (GInputStream *stream,
350 GAsyncResult *result,
353 GSimpleAsyncResult *simple;
356 simple = G_SIMPLE_ASYNC_RESULT (result);
357 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
359 nread = g_simple_async_result_get_op_res_gssize (simple);
364 g_unix_input_stream_skip_async (GInputStream *stream,
367 GCancellable *cancellable,
368 GAsyncReadyCallback callback,
371 g_warn_if_reached ();
372 /* TODO: Not implemented */
376 g_unix_input_stream_skip_finish (GInputStream *stream,
377 GAsyncResult *result,
380 g_warn_if_reached ();
381 /* TODO: Not implemented */
386 GInputStream *stream;
387 GAsyncReadyCallback callback;
392 close_async_data_free (gpointer _data)
394 CloseAsyncData *data = _data;
400 close_async_cb (CloseAsyncData *data)
402 GUnixInputStream *unix_stream;
403 GSimpleAsyncResult *simple;
404 GError *error = NULL;
408 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
410 if (!unix_stream->priv->close_fd_at_close)
418 res = close (unix_stream->priv->fd);
420 g_set_error (&error, G_IO_ERROR,
421 g_io_error_from_errno (errno),
422 _("Error closing unix: %s"),
430 simple = g_simple_async_result_new (G_OBJECT (data->stream),
433 g_unix_input_stream_close_async);
437 g_simple_async_result_set_from_error (simple, error);
438 g_error_free (error);
441 /* Complete immediately, not in idle, since we're already in a mainloop callout */
442 g_simple_async_result_complete (simple);
443 g_object_unref (simple);
449 g_unix_input_stream_close_async (GInputStream *stream,
451 GCancellable *cancellable,
452 GAsyncReadyCallback callback,
456 CloseAsyncData *data;
458 data = g_new0 (CloseAsyncData, 1);
460 data->stream = stream;
461 data->callback = callback;
462 data->user_data = user_data;
464 idle = g_idle_source_new ();
465 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
466 g_source_attach (idle, NULL);
467 g_source_unref (idle);
471 g_unix_input_stream_close_finish (GInputStream *stream,
472 GAsyncResult *result,
475 /* Failures handled in generic close_finish code */
479 #define __G_UNIX_INPUT_STREAM_C__
480 #include "gioaliasdef.c"