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 "gunixoutputstream.h"
36 #include "gcancellable.h"
37 #include "gsimpleasyncresult.h"
38 #include "gasynchelper.h"
43 * SECTION:gunixoutputstream
44 * @short_description: Streaming output operations for UNIX file descriptors
45 * @include: gio/gunixoutputstream.h
46 * @see_also: #GOutputStream
48 * #GUnixOutputStream implements #GOutputStream for writing to a
49 * UNIX file descriptor, including asynchronous operations. The file
50 * descriptor must be selectable, so it doesn't work with opened files.
52 * Note that <filename><gio/gunixoutputstream.h></filename> belongs
53 * to the UNIX-specific GIO interfaces, thus you have to use the
54 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
63 static void g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
65 G_DEFINE_TYPE_WITH_CODE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM,
66 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
67 g_unix_output_stream_pollable_iface_init)
70 struct _GUnixOutputStreamPrivate {
75 static void g_unix_output_stream_set_property (GObject *object,
79 static void g_unix_output_stream_get_property (GObject *object,
83 static gssize g_unix_output_stream_write (GOutputStream *stream,
86 GCancellable *cancellable,
88 static gboolean g_unix_output_stream_close (GOutputStream *stream,
89 GCancellable *cancellable,
91 static void g_unix_output_stream_write_async (GOutputStream *stream,
95 GCancellable *cancellable,
96 GAsyncReadyCallback callback,
98 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
101 static void g_unix_output_stream_close_async (GOutputStream *stream,
103 GCancellable *cancellable,
104 GAsyncReadyCallback callback,
106 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
107 GAsyncResult *result,
110 static gboolean g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream);
111 static GSource *g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
112 GCancellable *cancellable);
115 g_unix_output_stream_finalize (GObject *object)
117 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
121 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
123 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
124 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
126 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
128 gobject_class->get_property = g_unix_output_stream_get_property;
129 gobject_class->set_property = g_unix_output_stream_set_property;
130 gobject_class->finalize = g_unix_output_stream_finalize;
132 stream_class->write_fn = g_unix_output_stream_write;
133 stream_class->close_fn = g_unix_output_stream_close;
134 stream_class->write_async = g_unix_output_stream_write_async;
135 stream_class->write_finish = g_unix_output_stream_write_finish;
136 stream_class->close_async = g_unix_output_stream_close_async;
137 stream_class->close_finish = g_unix_output_stream_close_finish;
140 * GUnixOutputStream:fd:
142 * The file descriptor that the stream writes to.
146 g_object_class_install_property (gobject_class,
148 g_param_spec_int ("fd",
149 P_("File descriptor"),
150 P_("The file descriptor to write to"),
151 G_MININT, G_MAXINT, -1,
152 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
155 * GUnixOutputStream:close-fd:
157 * Whether to close the file descriptor when the stream is closed.
161 g_object_class_install_property (gobject_class,
163 g_param_spec_boolean ("close-fd",
164 P_("Close file descriptor"),
165 P_("Whether to close the file descriptor when the stream is closed"),
167 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
171 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
173 iface->is_writable = g_unix_output_stream_pollable_is_writable;
174 iface->create_source = g_unix_output_stream_pollable_create_source;
178 g_unix_output_stream_set_property (GObject *object,
183 GUnixOutputStream *unix_stream;
185 unix_stream = G_UNIX_OUTPUT_STREAM (object);
190 unix_stream->priv->fd = g_value_get_int (value);
193 unix_stream->priv->close_fd = g_value_get_boolean (value);
196 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
202 g_unix_output_stream_get_property (GObject *object,
207 GUnixOutputStream *unix_stream;
209 unix_stream = G_UNIX_OUTPUT_STREAM (object);
214 g_value_set_int (value, unix_stream->priv->fd);
217 g_value_set_boolean (value, unix_stream->priv->close_fd);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
227 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
228 G_TYPE_UNIX_OUTPUT_STREAM,
229 GUnixOutputStreamPrivate);
231 unix_stream->priv->fd = -1;
232 unix_stream->priv->close_fd = TRUE;
236 * g_unix_output_stream_new:
237 * @fd: a UNIX file descriptor
238 * @close_fd: %TRUE to close the file descriptor when done
240 * Creates a new #GUnixOutputStream for the given @fd.
242 * If @close_fd, is %TRUE, the file descriptor will be closed when
243 * the output stream is destroyed.
245 * Returns: a new #GOutputStream
248 g_unix_output_stream_new (gint fd,
251 GUnixOutputStream *stream;
253 g_return_val_if_fail (fd != -1, NULL);
255 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
257 "close-fd", close_fd,
260 return G_OUTPUT_STREAM (stream);
264 * g_unix_output_stream_set_close_fd:
265 * @stream: a #GUnixOutputStream
266 * @close_fd: %TRUE to close the file descriptor when done
268 * Sets whether the file descriptor of @stream shall be closed
269 * when the stream is closed.
274 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
277 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
279 close_fd = close_fd != FALSE;
280 if (stream->priv->close_fd != close_fd)
282 stream->priv->close_fd = close_fd;
283 g_object_notify (G_OBJECT (stream), "close-fd");
288 * g_unix_output_stream_get_close_fd:
289 * @stream: a #GUnixOutputStream
291 * Returns whether the file descriptor of @stream will be
292 * closed when the stream is closed.
294 * Return value: %TRUE if the file descriptor is closed when done
299 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
301 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
303 return stream->priv->close_fd;
307 * g_unix_output_stream_get_fd:
308 * @stream: a #GUnixOutputStream
310 * Return the UNIX file descriptor that the stream writes to.
312 * Return value: The file descriptor of @stream
317 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
319 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
321 return stream->priv->fd;
325 g_unix_output_stream_write (GOutputStream *stream,
328 GCancellable *cancellable,
331 GUnixOutputStream *unix_stream;
336 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
338 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
340 poll_fds[0].fd = unix_stream->priv->fd;
341 poll_fds[0].events = G_IO_OUT;
343 poll_ret = g_poll (poll_fds, 2, -1);
344 while (poll_ret == -1 && errno == EINTR);
345 g_cancellable_release_fd (cancellable);
351 g_set_error (error, G_IO_ERROR,
352 g_io_error_from_errno (errsv),
353 _("Error writing to unix: %s"),
361 if (g_cancellable_set_error_if_cancelled (cancellable, error))
364 res = write (unix_stream->priv->fd, buffer, count);
372 g_set_error (error, G_IO_ERROR,
373 g_io_error_from_errno (errsv),
374 _("Error writing to unix: %s"),
385 g_unix_output_stream_close (GOutputStream *stream,
386 GCancellable *cancellable,
389 GUnixOutputStream *unix_stream;
392 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
394 if (!unix_stream->priv->close_fd)
399 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
400 res = close (unix_stream->priv->fd);
405 g_set_error (error, G_IO_ERROR,
406 g_io_error_from_errno (errsv),
407 _("Error closing unix: %s"),
419 GAsyncReadyCallback callback;
421 GCancellable *cancellable;
422 GUnixOutputStream *stream;
426 write_async_cb (int fd,
427 GIOCondition condition,
428 WriteAsyncData *data)
430 GSimpleAsyncResult *simple;
431 GError *error = NULL;
432 gssize count_written;
436 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
442 count_written = write (data->stream->priv->fd, data->buffer, data->count);
443 if (count_written == -1)
450 g_set_error (&error, G_IO_ERROR,
451 g_io_error_from_errno (errsv),
452 _("Error writing to unix: %s"),
458 simple = g_simple_async_result_new (G_OBJECT (data->stream),
461 g_unix_output_stream_write_async);
463 g_simple_async_result_set_op_res_gssize (simple, count_written);
465 if (count_written == -1)
466 g_simple_async_result_take_error (simple, error);
468 /* Complete immediately, not in idle, since we're already in a mainloop callout */
469 g_simple_async_result_complete (simple);
470 g_object_unref (simple);
476 g_unix_output_stream_write_async (GOutputStream *stream,
480 GCancellable *cancellable,
481 GAsyncReadyCallback callback,
485 GUnixOutputStream *unix_stream;
486 WriteAsyncData *data;
488 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
490 data = g_new0 (WriteAsyncData, 1);
492 data->buffer = buffer;
493 data->callback = callback;
494 data->user_data = user_data;
495 data->cancellable = cancellable;
496 data->stream = unix_stream;
498 source = _g_fd_source_new (unix_stream->priv->fd,
501 g_source_set_name (source, "GUnixOutputStream");
503 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
504 g_source_attach (source, g_main_context_get_thread_default ());
506 g_source_unref (source);
510 g_unix_output_stream_write_finish (GOutputStream *stream,
511 GAsyncResult *result,
514 GSimpleAsyncResult *simple;
517 simple = G_SIMPLE_ASYNC_RESULT (result);
518 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
520 nwritten = g_simple_async_result_get_op_res_gssize (simple);
525 GOutputStream *stream;
526 GAsyncReadyCallback callback;
531 close_async_cb (CloseAsyncData *data)
533 GUnixOutputStream *unix_stream;
534 GSimpleAsyncResult *simple;
535 GError *error = NULL;
539 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
541 if (!unix_stream->priv->close_fd)
549 res = close (unix_stream->priv->fd);
554 g_set_error (&error, G_IO_ERROR,
555 g_io_error_from_errno (errsv),
556 _("Error closing unix: %s"),
565 simple = g_simple_async_result_new (G_OBJECT (data->stream),
568 g_unix_output_stream_close_async);
571 g_simple_async_result_take_error (simple, error);
573 /* Complete immediately, not in idle, since we're already in a mainloop callout */
574 g_simple_async_result_complete (simple);
575 g_object_unref (simple);
581 g_unix_output_stream_close_async (GOutputStream *stream,
583 GCancellable *cancellable,
584 GAsyncReadyCallback callback,
588 CloseAsyncData *data;
590 data = g_new0 (CloseAsyncData, 1);
592 data->stream = stream;
593 data->callback = callback;
594 data->user_data = user_data;
596 idle = g_idle_source_new ();
597 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
598 g_source_attach (idle, g_main_context_get_thread_default ());
599 g_source_unref (idle);
603 g_unix_output_stream_close_finish (GOutputStream *stream,
604 GAsyncResult *result,
607 /* Failures handled in generic close_finish code */
612 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
614 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
618 poll_fd.fd = unix_stream->priv->fd;
619 poll_fd.events = G_IO_OUT;
622 result = g_poll (&poll_fd, 1, 0);
623 while (result == -1 && errno == EINTR);
625 return poll_fd.revents != 0;
629 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
630 GCancellable *cancellable)
632 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
633 GSource *inner_source, *pollable_source;
635 pollable_source = g_pollable_source_new (G_OBJECT (stream));
637 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_OUT, cancellable);
638 g_source_set_dummy_callback (inner_source);
639 g_source_add_child_source (pollable_source, inner_source);
640 g_source_unref (inner_source);
642 return pollable_source;