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.)
55 * Note that <filename><gio/gunixoutputstream.h></filename> belongs
56 * to the UNIX-specific GIO interfaces, thus you have to use the
57 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
66 static void g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
67 static void g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
69 G_DEFINE_TYPE_WITH_CODE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM,
70 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
71 g_unix_output_stream_pollable_iface_init)
72 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
73 g_unix_output_stream_file_descriptor_based_iface_init)
76 struct _GUnixOutputStreamPrivate {
79 guint is_pipe_or_socket : 1;
82 static void g_unix_output_stream_set_property (GObject *object,
86 static void g_unix_output_stream_get_property (GObject *object,
90 static gssize g_unix_output_stream_write (GOutputStream *stream,
93 GCancellable *cancellable,
95 static gboolean g_unix_output_stream_close (GOutputStream *stream,
96 GCancellable *cancellable,
98 static void g_unix_output_stream_write_async (GOutputStream *stream,
102 GCancellable *cancellable,
103 GAsyncReadyCallback callback,
105 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
106 GAsyncResult *result,
108 static void g_unix_output_stream_close_async (GOutputStream *stream,
110 GCancellable *cancellable,
111 GAsyncReadyCallback callback,
113 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
114 GAsyncResult *result,
117 static gboolean g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream);
118 static GSource *g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
119 GCancellable *cancellable);
122 g_unix_output_stream_finalize (GObject *object)
124 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
128 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
130 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
131 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
133 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
135 gobject_class->get_property = g_unix_output_stream_get_property;
136 gobject_class->set_property = g_unix_output_stream_set_property;
137 gobject_class->finalize = g_unix_output_stream_finalize;
139 stream_class->write_fn = g_unix_output_stream_write;
140 stream_class->close_fn = g_unix_output_stream_close;
141 stream_class->write_async = g_unix_output_stream_write_async;
142 stream_class->write_finish = g_unix_output_stream_write_finish;
143 stream_class->close_async = g_unix_output_stream_close_async;
144 stream_class->close_finish = g_unix_output_stream_close_finish;
147 * GUnixOutputStream:fd:
149 * The file descriptor that the stream writes to.
153 g_object_class_install_property (gobject_class,
155 g_param_spec_int ("fd",
156 P_("File descriptor"),
157 P_("The file descriptor to write to"),
158 G_MININT, G_MAXINT, -1,
159 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
162 * GUnixOutputStream:close-fd:
164 * Whether to close the file descriptor when the stream is closed.
168 g_object_class_install_property (gobject_class,
170 g_param_spec_boolean ("close-fd",
171 P_("Close file descriptor"),
172 P_("Whether to close the file descriptor when the stream is closed"),
174 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
178 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
180 iface->is_writable = g_unix_output_stream_pollable_is_writable;
181 iface->create_source = g_unix_output_stream_pollable_create_source;
185 g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
187 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd;
191 g_unix_output_stream_set_property (GObject *object,
196 GUnixOutputStream *unix_stream;
198 unix_stream = G_UNIX_OUTPUT_STREAM (object);
203 unix_stream->priv->fd = g_value_get_int (value);
204 if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
205 unix_stream->priv->is_pipe_or_socket = TRUE;
207 unix_stream->priv->is_pipe_or_socket = FALSE;
210 unix_stream->priv->close_fd = g_value_get_boolean (value);
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 g_unix_output_stream_get_property (GObject *object,
224 GUnixOutputStream *unix_stream;
226 unix_stream = G_UNIX_OUTPUT_STREAM (object);
231 g_value_set_int (value, unix_stream->priv->fd);
234 g_value_set_boolean (value, unix_stream->priv->close_fd);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
244 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
245 G_TYPE_UNIX_OUTPUT_STREAM,
246 GUnixOutputStreamPrivate);
248 unix_stream->priv->fd = -1;
249 unix_stream->priv->close_fd = TRUE;
253 * g_unix_output_stream_new:
254 * @fd: a UNIX file descriptor
255 * @close_fd: %TRUE to close the file descriptor when done
257 * Creates a new #GUnixOutputStream for the given @fd.
259 * If @close_fd, is %TRUE, the file descriptor will be closed when
260 * the output stream is destroyed.
262 * Returns: a new #GOutputStream
265 g_unix_output_stream_new (gint fd,
268 GUnixOutputStream *stream;
270 g_return_val_if_fail (fd != -1, NULL);
272 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
274 "close-fd", close_fd,
277 return G_OUTPUT_STREAM (stream);
281 * g_unix_output_stream_set_close_fd:
282 * @stream: a #GUnixOutputStream
283 * @close_fd: %TRUE to close the file descriptor when done
285 * Sets whether the file descriptor of @stream shall be closed
286 * when the stream is closed.
291 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
294 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
296 close_fd = close_fd != FALSE;
297 if (stream->priv->close_fd != close_fd)
299 stream->priv->close_fd = close_fd;
300 g_object_notify (G_OBJECT (stream), "close-fd");
305 * g_unix_output_stream_get_close_fd:
306 * @stream: a #GUnixOutputStream
308 * Returns whether the file descriptor of @stream will be
309 * closed when the stream is closed.
311 * Return value: %TRUE if the file descriptor is closed when done
316 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
318 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
320 return stream->priv->close_fd;
324 * g_unix_output_stream_get_fd:
325 * @stream: a #GUnixOutputStream
327 * Return the UNIX file descriptor that the stream writes to.
329 * Return value: The file descriptor of @stream
334 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
336 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
338 return stream->priv->fd;
342 g_unix_output_stream_write (GOutputStream *stream,
345 GCancellable *cancellable,
348 GUnixOutputStream *unix_stream;
354 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
356 poll_fds[0].fd = unix_stream->priv->fd;
357 poll_fds[0].events = G_IO_OUT;
359 if (unix_stream->priv->is_pipe_or_socket &&
360 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
367 poll_fds[0].revents = poll_fds[1].revents = 0;
369 poll_ret = g_poll (poll_fds, nfds, -1);
370 while (poll_ret == -1 && errno == EINTR);
376 g_set_error (error, G_IO_ERROR,
377 g_io_error_from_errno (errsv),
378 _("Error writing to file descriptor: %s"),
383 if (g_cancellable_set_error_if_cancelled (cancellable, error))
386 if (!poll_fds[0].revents)
389 res = write (unix_stream->priv->fd, buffer, count);
394 if (errsv == EINTR || errsv == EAGAIN)
397 g_set_error (error, G_IO_ERROR,
398 g_io_error_from_errno (errsv),
399 _("Error writing to file descriptor: %s"),
407 g_cancellable_release_fd (cancellable);
412 g_unix_output_stream_close (GOutputStream *stream,
413 GCancellable *cancellable,
416 GUnixOutputStream *unix_stream;
419 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
421 if (!unix_stream->priv->close_fd)
426 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
427 res = close (unix_stream->priv->fd);
432 g_set_error (error, G_IO_ERROR,
433 g_io_error_from_errno (errsv),
434 _("Error closing file descriptor: %s"),
446 GAsyncReadyCallback callback;
448 GCancellable *cancellable;
449 GUnixOutputStream *stream;
453 write_async_cb (int fd,
454 GIOCondition condition,
455 WriteAsyncData *data)
457 GSimpleAsyncResult *simple;
458 GError *error = NULL;
459 gssize count_written;
463 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
469 count_written = write (data->stream->priv->fd, data->buffer, data->count);
470 if (count_written == -1)
474 if (errsv == EINTR || errsv == EAGAIN)
477 g_set_error (&error, G_IO_ERROR,
478 g_io_error_from_errno (errsv),
479 _("Error writing to file descriptor: %s"),
485 simple = g_simple_async_result_new (G_OBJECT (data->stream),
488 g_unix_output_stream_write_async);
490 g_simple_async_result_set_op_res_gssize (simple, count_written);
492 if (count_written == -1)
493 g_simple_async_result_take_error (simple, error);
495 /* Complete immediately, not in idle, since we're already in a mainloop callout */
496 g_simple_async_result_complete (simple);
497 g_object_unref (simple);
503 g_unix_output_stream_write_async (GOutputStream *stream,
507 GCancellable *cancellable,
508 GAsyncReadyCallback callback,
512 GUnixOutputStream *unix_stream;
513 WriteAsyncData *data;
515 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
517 if (!unix_stream->priv->is_pipe_or_socket)
519 G_OUTPUT_STREAM_CLASS (g_unix_output_stream_parent_class)->
520 write_async (stream, buffer, count, io_priority,
521 cancellable, callback, user_data);
525 data = g_new0 (WriteAsyncData, 1);
527 data->buffer = buffer;
528 data->callback = callback;
529 data->user_data = user_data;
530 data->cancellable = cancellable;
531 data->stream = unix_stream;
533 source = _g_fd_source_new (unix_stream->priv->fd,
536 g_source_set_name (source, "GUnixOutputStream");
538 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
539 g_source_attach (source, g_main_context_get_thread_default ());
541 g_source_unref (source);
545 g_unix_output_stream_write_finish (GOutputStream *stream,
546 GAsyncResult *result,
549 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
550 GSimpleAsyncResult *simple;
553 if (!unix_stream->priv->is_pipe_or_socket)
555 return G_OUTPUT_STREAM_CLASS (g_unix_output_stream_parent_class)->
556 write_finish (stream, result, error);
559 simple = G_SIMPLE_ASYNC_RESULT (result);
560 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
562 nwritten = g_simple_async_result_get_op_res_gssize (simple);
567 GOutputStream *stream;
568 GAsyncReadyCallback callback;
573 close_async_cb (CloseAsyncData *data)
575 GUnixOutputStream *unix_stream;
576 GSimpleAsyncResult *simple;
577 GError *error = NULL;
581 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
583 if (!unix_stream->priv->close_fd)
591 res = close (unix_stream->priv->fd);
596 g_set_error (&error, G_IO_ERROR,
597 g_io_error_from_errno (errsv),
598 _("Error closing file descriptor: %s"),
607 simple = g_simple_async_result_new (G_OBJECT (data->stream),
610 g_unix_output_stream_close_async);
613 g_simple_async_result_take_error (simple, error);
615 /* Complete immediately, not in idle, since we're already in a mainloop callout */
616 g_simple_async_result_complete (simple);
617 g_object_unref (simple);
623 g_unix_output_stream_close_async (GOutputStream *stream,
625 GCancellable *cancellable,
626 GAsyncReadyCallback callback,
630 CloseAsyncData *data;
632 data = g_new0 (CloseAsyncData, 1);
634 data->stream = stream;
635 data->callback = callback;
636 data->user_data = user_data;
638 idle = g_idle_source_new ();
639 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
640 g_source_attach (idle, g_main_context_get_thread_default ());
641 g_source_unref (idle);
645 g_unix_output_stream_close_finish (GOutputStream *stream,
646 GAsyncResult *result,
649 /* Failures handled in generic close_finish code */
654 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
656 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
660 poll_fd.fd = unix_stream->priv->fd;
661 poll_fd.events = G_IO_OUT;
664 result = g_poll (&poll_fd, 1, 0);
665 while (result == -1 && errno == EINTR);
667 return poll_fd.revents != 0;
671 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
672 GCancellable *cancellable)
674 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
675 GSource *inner_source, *pollable_source;
677 pollable_source = g_pollable_source_new (G_OBJECT (stream));
679 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_OUT, cancellable);
680 g_source_set_dummy_callback (inner_source);
681 g_source_add_child_source (pollable_source, inner_source);
682 g_source_unref (inner_source);
684 return pollable_source;