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
50 * UNIX file descriptor, including asynchronous operations. The file
51 * descriptor must be selectable, so it doesn't work with opened files.
53 * Note that <filename><gio/gunixoutputstream.h></filename> belongs
54 * to the UNIX-specific GIO interfaces, thus you have to use the
55 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
64 static void g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
65 static void g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
67 G_DEFINE_TYPE_WITH_CODE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM,
68 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
69 g_unix_output_stream_pollable_iface_init)
70 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
71 g_unix_output_stream_file_descriptor_based_iface_init)
74 struct _GUnixOutputStreamPrivate {
79 static void g_unix_output_stream_set_property (GObject *object,
83 static void g_unix_output_stream_get_property (GObject *object,
87 static gssize g_unix_output_stream_write (GOutputStream *stream,
90 GCancellable *cancellable,
92 static gboolean g_unix_output_stream_close (GOutputStream *stream,
93 GCancellable *cancellable,
95 static void g_unix_output_stream_write_async (GOutputStream *stream,
99 GCancellable *cancellable,
100 GAsyncReadyCallback callback,
102 static gssize g_unix_output_stream_write_finish (GOutputStream *stream,
103 GAsyncResult *result,
105 static void g_unix_output_stream_close_async (GOutputStream *stream,
107 GCancellable *cancellable,
108 GAsyncReadyCallback callback,
110 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
111 GAsyncResult *result,
114 static gboolean g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream);
115 static GSource *g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
116 GCancellable *cancellable);
119 g_unix_output_stream_finalize (GObject *object)
121 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
125 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
127 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
128 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
130 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
132 gobject_class->get_property = g_unix_output_stream_get_property;
133 gobject_class->set_property = g_unix_output_stream_set_property;
134 gobject_class->finalize = g_unix_output_stream_finalize;
136 stream_class->write_fn = g_unix_output_stream_write;
137 stream_class->close_fn = g_unix_output_stream_close;
138 stream_class->write_async = g_unix_output_stream_write_async;
139 stream_class->write_finish = g_unix_output_stream_write_finish;
140 stream_class->close_async = g_unix_output_stream_close_async;
141 stream_class->close_finish = g_unix_output_stream_close_finish;
144 * GUnixOutputStream:fd:
146 * The file descriptor that the stream writes to.
150 g_object_class_install_property (gobject_class,
152 g_param_spec_int ("fd",
153 P_("File descriptor"),
154 P_("The file descriptor to write to"),
155 G_MININT, G_MAXINT, -1,
156 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
159 * GUnixOutputStream:close-fd:
161 * Whether to close the file descriptor when the stream is closed.
165 g_object_class_install_property (gobject_class,
167 g_param_spec_boolean ("close-fd",
168 P_("Close file descriptor"),
169 P_("Whether to close the file descriptor when the stream is closed"),
171 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
175 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
177 iface->is_writable = g_unix_output_stream_pollable_is_writable;
178 iface->create_source = g_unix_output_stream_pollable_create_source;
182 g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
184 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd;
188 g_unix_output_stream_set_property (GObject *object,
193 GUnixOutputStream *unix_stream;
195 unix_stream = G_UNIX_OUTPUT_STREAM (object);
200 unix_stream->priv->fd = g_value_get_int (value);
203 unix_stream->priv->close_fd = g_value_get_boolean (value);
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 g_unix_output_stream_get_property (GObject *object,
217 GUnixOutputStream *unix_stream;
219 unix_stream = G_UNIX_OUTPUT_STREAM (object);
224 g_value_set_int (value, unix_stream->priv->fd);
227 g_value_set_boolean (value, unix_stream->priv->close_fd);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
235 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
237 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
238 G_TYPE_UNIX_OUTPUT_STREAM,
239 GUnixOutputStreamPrivate);
241 unix_stream->priv->fd = -1;
242 unix_stream->priv->close_fd = TRUE;
246 * g_unix_output_stream_new:
247 * @fd: a UNIX file descriptor
248 * @close_fd: %TRUE to close the file descriptor when done
250 * Creates a new #GUnixOutputStream for the given @fd.
252 * If @close_fd, is %TRUE, the file descriptor will be closed when
253 * the output stream is destroyed.
255 * Returns: a new #GOutputStream
258 g_unix_output_stream_new (gint fd,
261 GUnixOutputStream *stream;
263 g_return_val_if_fail (fd != -1, NULL);
265 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
267 "close-fd", close_fd,
270 return G_OUTPUT_STREAM (stream);
274 * g_unix_output_stream_set_close_fd:
275 * @stream: a #GUnixOutputStream
276 * @close_fd: %TRUE to close the file descriptor when done
278 * Sets whether the file descriptor of @stream shall be closed
279 * when the stream is closed.
284 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
287 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
289 close_fd = close_fd != FALSE;
290 if (stream->priv->close_fd != close_fd)
292 stream->priv->close_fd = close_fd;
293 g_object_notify (G_OBJECT (stream), "close-fd");
298 * g_unix_output_stream_get_close_fd:
299 * @stream: a #GUnixOutputStream
301 * Returns whether the file descriptor of @stream will be
302 * closed when the stream is closed.
304 * Return value: %TRUE if the file descriptor is closed when done
309 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
311 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
313 return stream->priv->close_fd;
317 * g_unix_output_stream_get_fd:
318 * @stream: a #GUnixOutputStream
320 * Return the UNIX file descriptor that the stream writes to.
322 * Return value: The file descriptor of @stream
327 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
329 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
331 return stream->priv->fd;
335 g_unix_output_stream_write (GOutputStream *stream,
338 GCancellable *cancellable,
341 GUnixOutputStream *unix_stream;
346 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
348 if (g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
350 poll_fds[0].fd = unix_stream->priv->fd;
351 poll_fds[0].events = G_IO_OUT;
353 poll_ret = g_poll (poll_fds, 2, -1);
354 while (poll_ret == -1 && errno == EINTR);
355 g_cancellable_release_fd (cancellable);
361 g_set_error (error, G_IO_ERROR,
362 g_io_error_from_errno (errsv),
363 _("Error writing to unix: %s"),
371 if (g_cancellable_set_error_if_cancelled (cancellable, error))
374 res = write (unix_stream->priv->fd, buffer, count);
382 g_set_error (error, G_IO_ERROR,
383 g_io_error_from_errno (errsv),
384 _("Error writing to unix: %s"),
395 g_unix_output_stream_close (GOutputStream *stream,
396 GCancellable *cancellable,
399 GUnixOutputStream *unix_stream;
402 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
404 if (!unix_stream->priv->close_fd)
409 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
410 res = close (unix_stream->priv->fd);
415 g_set_error (error, G_IO_ERROR,
416 g_io_error_from_errno (errsv),
417 _("Error closing unix: %s"),
429 GAsyncReadyCallback callback;
431 GCancellable *cancellable;
432 GUnixOutputStream *stream;
436 write_async_cb (int fd,
437 GIOCondition condition,
438 WriteAsyncData *data)
440 GSimpleAsyncResult *simple;
441 GError *error = NULL;
442 gssize count_written;
446 if (g_cancellable_set_error_if_cancelled (data->cancellable, &error))
452 count_written = write (data->stream->priv->fd, data->buffer, data->count);
453 if (count_written == -1)
460 g_set_error (&error, G_IO_ERROR,
461 g_io_error_from_errno (errsv),
462 _("Error writing to unix: %s"),
468 simple = g_simple_async_result_new (G_OBJECT (data->stream),
471 g_unix_output_stream_write_async);
473 g_simple_async_result_set_op_res_gssize (simple, count_written);
475 if (count_written == -1)
476 g_simple_async_result_take_error (simple, error);
478 /* Complete immediately, not in idle, since we're already in a mainloop callout */
479 g_simple_async_result_complete (simple);
480 g_object_unref (simple);
486 g_unix_output_stream_write_async (GOutputStream *stream,
490 GCancellable *cancellable,
491 GAsyncReadyCallback callback,
495 GUnixOutputStream *unix_stream;
496 WriteAsyncData *data;
498 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
500 data = g_new0 (WriteAsyncData, 1);
502 data->buffer = buffer;
503 data->callback = callback;
504 data->user_data = user_data;
505 data->cancellable = cancellable;
506 data->stream = unix_stream;
508 source = _g_fd_source_new (unix_stream->priv->fd,
511 g_source_set_name (source, "GUnixOutputStream");
513 g_source_set_callback (source, (GSourceFunc)write_async_cb, data, g_free);
514 g_source_attach (source, g_main_context_get_thread_default ());
516 g_source_unref (source);
520 g_unix_output_stream_write_finish (GOutputStream *stream,
521 GAsyncResult *result,
524 GSimpleAsyncResult *simple;
527 simple = G_SIMPLE_ASYNC_RESULT (result);
528 g_warn_if_fail (g_simple_async_result_get_source_tag (simple) == g_unix_output_stream_write_async);
530 nwritten = g_simple_async_result_get_op_res_gssize (simple);
535 GOutputStream *stream;
536 GAsyncReadyCallback callback;
541 close_async_cb (CloseAsyncData *data)
543 GUnixOutputStream *unix_stream;
544 GSimpleAsyncResult *simple;
545 GError *error = NULL;
549 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
551 if (!unix_stream->priv->close_fd)
559 res = close (unix_stream->priv->fd);
564 g_set_error (&error, G_IO_ERROR,
565 g_io_error_from_errno (errsv),
566 _("Error closing unix: %s"),
575 simple = g_simple_async_result_new (G_OBJECT (data->stream),
578 g_unix_output_stream_close_async);
581 g_simple_async_result_take_error (simple, error);
583 /* Complete immediately, not in idle, since we're already in a mainloop callout */
584 g_simple_async_result_complete (simple);
585 g_object_unref (simple);
591 g_unix_output_stream_close_async (GOutputStream *stream,
593 GCancellable *cancellable,
594 GAsyncReadyCallback callback,
598 CloseAsyncData *data;
600 data = g_new0 (CloseAsyncData, 1);
602 data->stream = stream;
603 data->callback = callback;
604 data->user_data = user_data;
606 idle = g_idle_source_new ();
607 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
608 g_source_attach (idle, g_main_context_get_thread_default ());
609 g_source_unref (idle);
613 g_unix_output_stream_close_finish (GOutputStream *stream,
614 GAsyncResult *result,
617 /* Failures handled in generic close_finish code */
622 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
624 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
628 poll_fd.fd = unix_stream->priv->fd;
629 poll_fd.events = G_IO_OUT;
632 result = g_poll (&poll_fd, 1, 0);
633 while (result == -1 && errno == EINTR);
635 return poll_fd.revents != 0;
639 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
640 GCancellable *cancellable)
642 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
643 GSource *inner_source, *pollable_source;
645 pollable_source = g_pollable_source_new (G_OBJECT (stream));
647 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_OUT, cancellable);
648 g_source_set_dummy_callback (inner_source);
649 g_source_add_child_source (pollable_source, inner_source);
650 g_source_unref (inner_source);
652 return pollable_source;