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.
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 G_OBJECT_CLASS (g_unix_input_stream_parent_class)->finalize (object);
109 g_unix_input_stream_class_init (GUnixInputStreamClass *klass)
111 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
112 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
114 g_type_class_add_private (klass, sizeof (GUnixInputStreamPrivate));
116 gobject_class->finalize = g_unix_input_stream_finalize;
118 stream_class->read_fn = g_unix_input_stream_read;
119 stream_class->close_fn = g_unix_input_stream_close;
120 stream_class->read_async = g_unix_input_stream_read_async;
121 stream_class->read_finish = g_unix_input_stream_read_finish;
124 /* TODO: Implement instead of using fallbacks */
125 stream_class->skip_async = g_unix_input_stream_skip_async;
126 stream_class->skip_finish = g_unix_input_stream_skip_finish;
128 stream_class->close_async = g_unix_input_stream_close_async;
129 stream_class->close_finish = g_unix_input_stream_close_finish;
133 g_unix_input_stream_init (GUnixInputStream *unix_stream)
135 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
136 G_TYPE_UNIX_INPUT_STREAM,
137 GUnixInputStreamPrivate);
141 * g_unix_input_stream_new:
142 * @fd: unix file descriptor.
143 * @close_fd_at_close: a #gboolean.
145 * Creates a new #GUnixInputStream for the given @fd. If @close_fd_at_close
146 * is %TRUE, the file descriptor will be closed when the stream is closed.
148 * Returns: a #GUnixInputStream.
151 g_unix_input_stream_new (int fd,
152 gboolean close_fd_at_close)
154 GUnixInputStream *stream;
156 g_return_val_if_fail (fd != -1, NULL);
158 stream = g_object_new (G_TYPE_UNIX_INPUT_STREAM, NULL);
160 stream->priv->fd = fd;
161 stream->priv->close_fd_at_close = close_fd_at_close;
163 return G_INPUT_STREAM (stream);
167 g_unix_input_stream_read (GInputStream *stream,
170 GCancellable *cancellable,
173 GUnixInputStream *unix_stream;
178 unix_stream = G_UNIX_INPUT_STREAM (stream);
182 poll_fds[0].fd = unix_stream->priv->fd;
183 poll_fds[0].events = G_IO_IN;
184 g_cancellable_make_pollfd (cancellable, &poll_fds[1]);
186 poll_ret = g_poll (poll_fds, 2, -1);
187 while (poll_ret == -1 && errno == EINTR);
193 g_set_error (error, G_IO_ERROR,
194 g_io_error_from_errno (errsv),
195 _("Error reading from unix: %s"),
203 if (g_cancellable_set_error_if_cancelled (cancellable, error))
205 res = read (unix_stream->priv->fd, buffer, count);
213 g_set_error (error, G_IO_ERROR,
214 g_io_error_from_errno (errsv),
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);
246 g_set_error (error, G_IO_ERROR,
247 g_io_error_from_errno (errsv),
248 _("Error closing unix: %s"),
260 GAsyncReadyCallback callback;
262 GCancellable *cancellable;
263 GUnixInputStream *stream;
267 read_async_cb (ReadAsyncData *data,
268 GIOCondition condition,
271 GSimpleAsyncResult *simple;
272 GError *error = NULL;
275 /* We know that we can read from fd once without blocking */
278 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
283 count_read = read (data->stream->priv->fd, data->buffer, data->count);
284 if (count_read == -1)
291 g_set_error (&error, G_IO_ERROR,
292 g_io_error_from_errno (errsv),
293 _("Error reading from unix: %s"),
299 simple = g_simple_async_result_new (G_OBJECT (data->stream),
302 g_unix_input_stream_read_async);
304 g_simple_async_result_set_op_res_gssize (simple, count_read);
306 if (count_read == -1)
308 g_simple_async_result_set_from_error (simple, error);
309 g_error_free (error);
312 /* Complete immediately, not in idle, since we're already in a mainloop callout */
313 g_simple_async_result_complete (simple);
314 g_object_unref (simple);
320 g_unix_input_stream_read_async (GInputStream *stream,
324 GCancellable *cancellable,
325 GAsyncReadyCallback callback,
329 GUnixInputStream *unix_stream;
332 unix_stream = G_UNIX_INPUT_STREAM (stream);
334 data = g_new0 (ReadAsyncData, 1);
336 data->buffer = buffer;
337 data->callback = callback;
338 data->user_data = user_data;
339 data->cancellable = cancellable;
340 data->stream = unix_stream;
342 source = _g_fd_source_new (unix_stream->priv->fd,
346 g_source_set_callback (source, (GSourceFunc)read_async_cb, data, g_free);
347 g_source_attach (source, NULL);
349 g_source_unref (source);
353 g_unix_input_stream_read_finish (GInputStream *stream,
354 GAsyncResult *result,
357 GSimpleAsyncResult *simple;
360 simple = G_SIMPLE_ASYNC_RESULT (result);
361 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_input_stream_read_async);
363 nread = g_simple_async_result_get_op_res_gssize (simple);
368 g_unix_input_stream_skip_async (GInputStream *stream,
371 GCancellable *cancellable,
372 GAsyncReadyCallback callback,
375 g_warn_if_reached ();
376 /* TODO: Not implemented */
380 g_unix_input_stream_skip_finish (GInputStream *stream,
381 GAsyncResult *result,
384 g_warn_if_reached ();
386 /* TODO: Not implemented */
391 GInputStream *stream;
392 GAsyncReadyCallback callback;
397 close_async_data_free (gpointer _data)
399 CloseAsyncData *data = _data;
405 close_async_cb (CloseAsyncData *data)
407 GUnixInputStream *unix_stream;
408 GSimpleAsyncResult *simple;
409 GError *error = NULL;
413 unix_stream = G_UNIX_INPUT_STREAM (data->stream);
415 if (!unix_stream->priv->close_fd_at_close)
423 res = close (unix_stream->priv->fd);
428 g_set_error (&error, G_IO_ERROR,
429 g_io_error_from_errno (errsv),
430 _("Error closing unix: %s"),
439 simple = g_simple_async_result_new (G_OBJECT (data->stream),
442 g_unix_input_stream_close_async);
446 g_simple_async_result_set_from_error (simple, error);
447 g_error_free (error);
450 /* Complete immediately, not in idle, since we're already in a mainloop callout */
451 g_simple_async_result_complete (simple);
452 g_object_unref (simple);
458 g_unix_input_stream_close_async (GInputStream *stream,
460 GCancellable *cancellable,
461 GAsyncReadyCallback callback,
465 CloseAsyncData *data;
467 data = g_new0 (CloseAsyncData, 1);
469 data->stream = stream;
470 data->callback = callback;
471 data->user_data = user_data;
473 idle = g_idle_source_new ();
474 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, close_async_data_free);
475 g_source_attach (idle, NULL);
476 g_source_unref (idle);
480 g_unix_input_stream_close_finish (GInputStream *stream,
481 GAsyncResult *result,
484 /* Failures handled in generic close_finish code */
488 #define __G_UNIX_INPUT_STREAM_C__
489 #include "gioaliasdef.c"