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"
39 #include "gfiledescriptorbased.h"
44 * SECTION:gunixoutputstream
45 * @short_description: Streaming output operations for UNIX file descriptors
46 * @include: gio/gunixoutputstream.h
47 * @see_also: #GOutputStream
49 * #GUnixOutputStream implements #GOutputStream for writing to a UNIX
50 * file descriptor, including asynchronous operations. (If the file
51 * descriptor refers to a socket or pipe, this will use poll() to do
52 * asynchronous I/O. If it refers to a regular file, it will fall back
53 * to doing asynchronous I/O in another thread like
54 * #GLocalFileOutputStream.)
56 * Note that <filename><gio/gunixoutputstream.h></filename> belongs
57 * to the UNIX-specific GIO interfaces, thus you have to use the
58 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
67 static void g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
68 static void g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
70 G_DEFINE_TYPE_WITH_CODE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM,
71 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
72 g_unix_output_stream_pollable_iface_init)
73 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
74 g_unix_output_stream_file_descriptor_based_iface_init)
77 struct _GUnixOutputStreamPrivate {
80 guint is_pipe_or_socket : 1;
83 static void g_unix_output_stream_set_property (GObject *object,
87 static void g_unix_output_stream_get_property (GObject *object,
91 static gssize g_unix_output_stream_write (GOutputStream *stream,
94 GCancellable *cancellable,
96 static gboolean g_unix_output_stream_close (GOutputStream *stream,
97 GCancellable *cancellable,
99 static void g_unix_output_stream_write_async (GOutputStream *stream,
103 GCancellable *cancellable,
104 GAsyncReadyCallback callback,
106 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
107 GAsyncResult *result,
109 static void g_unix_output_stream_close_async (GOutputStream *stream,
111 GCancellable *cancellable,
112 GAsyncReadyCallback callback,
114 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
115 GAsyncResult *result,
118 static gboolean g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream);
119 static GSource *g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
120 GCancellable *cancellable);
123 g_unix_output_stream_finalize (GObject *object)
125 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
129 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
131 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
132 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
134 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
136 gobject_class->get_property = g_unix_output_stream_get_property;
137 gobject_class->set_property = g_unix_output_stream_set_property;
138 gobject_class->finalize = g_unix_output_stream_finalize;
140 stream_class->write_fn = g_unix_output_stream_write;
141 stream_class->close_fn = g_unix_output_stream_close;
142 stream_class->write_async = g_unix_output_stream_write_async;
143 stream_class->write_finish = g_unix_output_stream_write_finish;
144 stream_class->close_async = g_unix_output_stream_close_async;
145 stream_class->close_finish = g_unix_output_stream_close_finish;
148 * GUnixOutputStream:fd:
150 * The file descriptor that the stream writes to.
154 g_object_class_install_property (gobject_class,
156 g_param_spec_int ("fd",
157 P_("File descriptor"),
158 P_("The file descriptor to write to"),
159 G_MININT, G_MAXINT, -1,
160 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
163 * GUnixOutputStream:close-fd:
165 * Whether to close the file descriptor when the stream is closed.
169 g_object_class_install_property (gobject_class,
171 g_param_spec_boolean ("close-fd",
172 P_("Close file descriptor"),
173 P_("Whether to close the file descriptor when the stream is closed"),
175 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
179 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
181 iface->is_writable = g_unix_output_stream_pollable_is_writable;
182 iface->create_source = g_unix_output_stream_pollable_create_source;
186 g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
188 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd;
192 g_unix_output_stream_set_property (GObject *object,
197 GUnixOutputStream *unix_stream;
199 unix_stream = G_UNIX_OUTPUT_STREAM (object);
204 unix_stream->priv->fd = g_value_get_int (value);
205 if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
206 unix_stream->priv->is_pipe_or_socket = TRUE;
208 unix_stream->priv->is_pipe_or_socket = FALSE;
211 unix_stream->priv->close_fd = g_value_get_boolean (value);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 g_unix_output_stream_get_property (GObject *object,
225 GUnixOutputStream *unix_stream;
227 unix_stream = G_UNIX_OUTPUT_STREAM (object);
232 g_value_set_int (value, unix_stream->priv->fd);
235 g_value_set_boolean (value, unix_stream->priv->close_fd);
238 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
243 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
245 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
246 G_TYPE_UNIX_OUTPUT_STREAM,
247 GUnixOutputStreamPrivate);
249 unix_stream->priv->fd = -1;
250 unix_stream->priv->close_fd = TRUE;
254 * g_unix_output_stream_new:
255 * @fd: a UNIX file descriptor
256 * @close_fd: %TRUE to close the file descriptor when done
258 * Creates a new #GUnixOutputStream for the given @fd.
260 * If @close_fd, is %TRUE, the file descriptor will be closed when
261 * the output stream is destroyed.
263 * Returns: a new #GOutputStream
266 g_unix_output_stream_new (gint fd,
269 GUnixOutputStream *stream;
271 g_return_val_if_fail (fd != -1, NULL);
273 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
275 "close-fd", close_fd,
278 return G_OUTPUT_STREAM (stream);
282 * g_unix_output_stream_set_close_fd:
283 * @stream: a #GUnixOutputStream
284 * @close_fd: %TRUE to close the file descriptor when done
286 * Sets whether the file descriptor of @stream shall be closed
287 * when the stream is closed.
292 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
295 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
297 close_fd = close_fd != FALSE;
298 if (stream->priv->close_fd != close_fd)
300 stream->priv->close_fd = close_fd;
301 g_object_notify (G_OBJECT (stream), "close-fd");
306 * g_unix_output_stream_get_close_fd:
307 * @stream: a #GUnixOutputStream
309 * Returns whether the file descriptor of @stream will be
310 * closed when the stream is closed.
312 * Return value: %TRUE if the file descriptor is closed when done
317 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
319 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
321 return stream->priv->close_fd;
325 * g_unix_output_stream_get_fd:
326 * @stream: a #GUnixOutputStream
328 * Return the UNIX file descriptor that the stream writes to.
330 * Return value: The file descriptor of @stream
335 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
337 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
339 return stream->priv->fd;
343 g_unix_output_stream_write (GOutputStream *stream,
346 GCancellable *cancellable,
349 GUnixOutputStream *unix_stream;
354 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
356 if (unix_stream->priv->is_pipe_or_socket &&
357 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
359 poll_fds[0].fd = unix_stream->priv->fd;
360 poll_fds[0].events = G_IO_OUT;
362 poll_ret = g_poll (poll_fds, 2, -1);
363 while (poll_ret == -1 && errno == EINTR);
364 g_cancellable_release_fd (cancellable);
370 g_set_error (error, G_IO_ERROR,
371 g_io_error_from_errno (errsv),
372 _("Error writing to unix: %s"),
380 if (g_cancellable_set_error_if_cancelled (cancellable, error))
383 res = write (unix_stream->priv->fd, buffer, count);
391 g_set_error (error, G_IO_ERROR,
392 g_io_error_from_errno (errsv),
393 _("Error writing to unix: %s"),
404 g_unix_output_stream_close (GOutputStream *stream,
405 GCancellable *cancellable,
408 GUnixOutputStream *unix_stream;
411 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
413 if (!unix_stream->priv->close_fd)
418 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
419 res = close (unix_stream->priv->fd);
424 g_set_error (error, G_IO_ERROR,
425 g_io_error_from_errno (errsv),
426 _("Error closing unix: %s"),
438 GAsyncReadyCallback callback;
440 GCancellable *cancellable;
441 GUnixOutputStream *stream;
445 write_async_cb (int fd,
446 GIOCondition condition,
447 WriteAsyncData *data)
449 GSimpleAsyncResult *simple;
450 GError *error = NULL;
451 gssize count_written;
455 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
461 count_written = write (data->stream->priv->fd, data->buffer, data->count);
462 if (count_written == -1)
469 g_set_error (&error, G_IO_ERROR,
470 g_io_error_from_errno (errsv),
471 _("Error writing to unix: %s"),
477 simple = g_simple_async_result_new (G_OBJECT (data->stream),
480 g_unix_output_stream_write_async);
482 g_simple_async_result_set_op_res_gssize (simple, count_written);
484 if (count_written == -1)
485 g_simple_async_result_take_error (simple, error);
487 /* Complete immediately, not in idle, since we're already in a mainloop callout */
488 g_simple_async_result_complete (simple);
489 g_object_unref (simple);
495 g_unix_output_stream_write_async (GOutputStream *stream,
499 GCancellable *cancellable,
500 GAsyncReadyCallback callback,
504 GUnixOutputStream *unix_stream;
505 WriteAsyncData *data;
507 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
509 if (!unix_stream->priv->is_pipe_or_socket)
511 G_OUTPUT_STREAM_CLASS (g_unix_output_stream_parent_class)->
512 write_async (stream, buffer, count, io_priority,
513 cancellable, callback, user_data);
517 data = g_new0 (WriteAsyncData, 1);
519 data->buffer = buffer;
520 data->callback = callback;
521 data->user_data = user_data;
522 data->cancellable = cancellable;
523 data->stream = unix_stream;
525 source = _g_fd_source_new (unix_stream->priv->fd,
528 g_source_set_name (source, "GUnixOutputStream");
530 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
531 g_source_attach (source, g_main_context_get_thread_default ());
533 g_source_unref (source);
537 g_unix_output_stream_write_finish (GOutputStream *stream,
538 GAsyncResult *result,
541 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
542 GSimpleAsyncResult *simple;
545 if (!unix_stream->priv->is_pipe_or_socket)
547 return G_OUTPUT_STREAM_CLASS (g_unix_output_stream_parent_class)->
548 write_finish (stream, result, error);
551 simple = G_SIMPLE_ASYNC_RESULT (result);
552 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
554 nwritten = g_simple_async_result_get_op_res_gssize (simple);
559 GOutputStream *stream;
560 GAsyncReadyCallback callback;
565 close_async_cb (CloseAsyncData *data)
567 GUnixOutputStream *unix_stream;
568 GSimpleAsyncResult *simple;
569 GError *error = NULL;
573 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
575 if (!unix_stream->priv->close_fd)
583 res = close (unix_stream->priv->fd);
588 g_set_error (&error, G_IO_ERROR,
589 g_io_error_from_errno (errsv),
590 _("Error closing unix: %s"),
599 simple = g_simple_async_result_new (G_OBJECT (data->stream),
602 g_unix_output_stream_close_async);
605 g_simple_async_result_take_error (simple, error);
607 /* Complete immediately, not in idle, since we're already in a mainloop callout */
608 g_simple_async_result_complete (simple);
609 g_object_unref (simple);
615 g_unix_output_stream_close_async (GOutputStream *stream,
617 GCancellable *cancellable,
618 GAsyncReadyCallback callback,
622 CloseAsyncData *data;
624 data = g_new0 (CloseAsyncData, 1);
626 data->stream = stream;
627 data->callback = callback;
628 data->user_data = user_data;
630 idle = g_idle_source_new ();
631 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
632 g_source_attach (idle, g_main_context_get_thread_default ());
633 g_source_unref (idle);
637 g_unix_output_stream_close_finish (GOutputStream *stream,
638 GAsyncResult *result,
641 /* Failures handled in generic close_finish code */
646 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
648 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
652 poll_fd.fd = unix_stream->priv->fd;
653 poll_fd.events = G_IO_OUT;
656 result = g_poll (&poll_fd, 1, 0);
657 while (result == -1 && errno == EINTR);
659 return poll_fd.revents != 0;
663 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
664 GCancellable *cancellable)
666 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
667 GSource *inner_source, *pollable_source;
669 pollable_source = g_pollable_source_new (G_OBJECT (stream));
671 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_OUT, cancellable);
672 g_source_set_dummy_callback (inner_source);
673 g_source_add_child_source (pollable_source, inner_source);
674 g_source_unref (inner_source);
676 return pollable_source;