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"
43 * SECTION:gunixinputstream
44 * @short_description: Unix Input Stream
45 * @see_also: #GInputStream.
47 * #GUnixInputStream implements #GInputStream for reading from a
48 * unix file descriptor, including asynchronous operations. The file
49 * descriptor much be selectable, so it doesn't work with opened files.
52 G_DEFINE_TYPE (GUnixInputStream, g_unix_input_stream, G_TYPE_INPUT_STREAM);
54 struct _GUnixInputStreamPrivate {
56 gboolean close_fd_at_close;
59 static gssize g_unix_input_stream_read (GInputStream *stream,
62 GCancellable *cancellable,
64 static gboolean g_unix_input_stream_close (GInputStream *stream,
65 GCancellable *cancellable,
67 static void g_unix_input_stream_read_async (GInputStream *stream,
71 GCancellable *cancellable,
72 GAsyncReadyCallback callback,
74 static gssize g_unix_input_stream_read_finish (GInputStream *stream,
77 static void g_unix_input_stream_skip_async (GInputStream *stream,
80 GCancellable *cancellable,
81 GAsyncReadyCallback callback,
83 static gssize g_unix_input_stream_skip_finish (GInputStream *stream,
86 static void g_unix_input_stream_close_async (GInputStream *stream,
88 GCancellable *cancellable,
89 GAsyncReadyCallback callback,
91 static gboolean g_unix_input_stream_close_finish (GInputStream *stream,
97 g_unix_input_stream_finalize (GObject *object)
99 GUnixInputStream *stream;
101 stream = G_UNIX_INPUT_STREAM (object);
103 if (G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize)
104 (*G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize) (object);
108 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
110 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
111 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
113 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
115 gobject_class->finalize = g_unix_input_stream_finalize;
117 stream_class->read = g_unix_input_stream_read;
118 stream_class->close = g_unix_input_stream_close;
119 stream_class->read_async = g_unix_input_stream_read_async;
120 stream_class->read_finish = g_unix_input_stream_read_finish;
123 /* TODO: Implement instead of using fallbacks */
124 stream_class->skip_async = g_unix_input_stream_skip_async;
125 stream_class->skip_finish = g_unix_input_stream_skip_finish;
127 stream_class->close_async = g_unix_input_stream_close_async;
128 stream_class->close_finish = g_unix_input_stream_close_finish;
132 g_unix_input_stream_init (GUnixInputStream *unix_stream)
134 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
135 G_TYPE_UNIX_INPUT_STREAM,
136 GUnixInputStreamPrivate);
140 * g_unix_input_stream_new:
141 * @fd: unix file descriptor.
142 * @close_fd_at_close: a #gboolean.
144 * Creates a new #GUnixInputStream for the given @fd. If @close_fd_at_close
145 * is %TRUE, the file descriptor will be closed when the stream is closed.
147 * Returns: a #GUnixInputStream.
150 g_unix_input_stream_new (int fd,
151 gboolean close_fd_at_close)
153 GUnixInputStream *stream;
155 g_return_val_if_fail (fd != -1, NULL);
157 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM, NULL);
159 stream->priv->fd = fd;
160 stream->priv->close_fd_at_close = close_fd_at_close;
162 return G_INPUT_STREAM (stream);
166 g_unix_input_stream_read (GInputStream *stream,
169 GCancellable *cancellable,
172 GUnixInputStream *unix_stream;
174 struct pollfd poll_fds[2];
178 unix_stream = G_UNIX_INPUT_STREAM (stream);
180 cancel_fd = g_cancellable_get_fd (cancellable);
185 poll_fds[0].events = POLLIN;
186 poll_fds[0].fd = unix_stream->priv->fd;
187 poll_fds[1].events = POLLIN;
188 poll_fds[1].fd = cancel_fd;
189 poll_ret = poll (poll_fds, 2, -1);
191 while (poll_ret == -1 && errno == EINTR);
195 g_set_error (error, G_IO_ERROR,
196 g_io_error_from_errno (errno),
197 _("Error reading from unix: %s"),
205 if (g_cancellable_set_error_if_cancelled (cancellable, error))
207 res = read (unix_stream->priv->fd, buffer, count);
213 g_set_error (error, G_IO_ERROR,
214 g_io_error_from_errno (errno),
215 _("Error reading from unix: %s"),
226 g_unix_input_stream_close (GInputStream *stream,
227 GCancellable *cancellable,
230 GUnixInputStream *unix_stream;
233 unix_stream = G_UNIX_INPUT_STREAM (stream);
235 if (!unix_stream->priv->close_fd_at_close)
240 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
241 res = close (unix_stream->priv->fd);
244 g_set_error (error, G_IO_ERROR,
245 g_io_error_from_errno (errno),
246 _("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_assert (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_assert_not_reached ();
372 /* TODO: Not implemented */
376 g_unix_input_stream_skip_finish (GInputStream *stream,
377 GAsyncResult *result,
380 g_assert_not_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);
421 g_set_error (&error, G_IO_ERROR,
422 g_io_error_from_errno (errno),
423 _("Error closing unix: %s"),
432 simple = g_simple_async_result_new (G_OBJECT (data->stream),
435 g_unix_input_stream_close_async);
439 g_simple_async_result_set_from_error (simple, error);
440 g_error_free (error);
443 /* Complete immediately, not in idle, since we're already in a mainloop callout */
444 g_simple_async_result_complete (simple);
445 g_object_unref (simple);
451 g_unix_input_stream_close_async (GInputStream *stream,
453 GCancellable *cancellable,
454 GAsyncReadyCallback callback,
458 CloseAsyncData *data;
460 data = g_new0 (CloseAsyncData, 1);
462 data->stream = stream;
463 data->callback = callback;
464 data->user_data = user_data;
466 idle = g_idle_source_new ();
467 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
468 g_source_attach (idle, NULL);
469 g_source_unref (idle);
473 g_unix_input_stream_close_finish (GInputStream *stream,
474 GAsyncResult *result,
477 /* Failures handled in generic close_finish code */