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_close_async (GOutputStream *stream,
100 GCancellable *cancellable,
101 GAsyncReadyCallback callback,
103 static gboolean g_unix_output_stream_close_finish (GOutputStream *stream,
104 GAsyncResult *result,
107 static gboolean g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream);
108 static GSource *g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
109 GCancellable *cancellable);
112 g_unix_output_stream_finalize (GObject *object)
114 G_OBJECT_CLASS (g_unix_output_stream_parent_class)->finalize (object);
118 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
120 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
121 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
123 g_type_class_add_private (klass, sizeof (GUnixOutputStreamPrivate));
125 gobject_class->get_property = g_unix_output_stream_get_property;
126 gobject_class->set_property = g_unix_output_stream_set_property;
127 gobject_class->finalize = g_unix_output_stream_finalize;
129 stream_class->write_fn = g_unix_output_stream_write;
130 stream_class->close_fn = g_unix_output_stream_close;
131 stream_class->close_async = g_unix_output_stream_close_async;
132 stream_class->close_finish = g_unix_output_stream_close_finish;
135 * GUnixOutputStream:fd:
137 * The file descriptor that the stream writes to.
141 g_object_class_install_property (gobject_class,
143 g_param_spec_int ("fd",
144 P_("File descriptor"),
145 P_("The file descriptor to write to"),
146 G_MININT, G_MAXINT, -1,
147 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
150 * GUnixOutputStream:close-fd:
152 * Whether to close the file descriptor when the stream is closed.
156 g_object_class_install_property (gobject_class,
158 g_param_spec_boolean ("close-fd",
159 P_("Close file descriptor"),
160 P_("Whether to close the file descriptor when the stream is closed"),
162 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
166 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
168 iface->is_writable = g_unix_output_stream_pollable_is_writable;
169 iface->create_source = g_unix_output_stream_pollable_create_source;
173 g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
175 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd;
179 g_unix_output_stream_set_property (GObject *object,
184 GUnixOutputStream *unix_stream;
186 unix_stream = G_UNIX_OUTPUT_STREAM (object);
191 unix_stream->priv->fd = g_value_get_int (value);
192 if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
193 unix_stream->priv->is_pipe_or_socket = TRUE;
195 unix_stream->priv->is_pipe_or_socket = FALSE;
198 unix_stream->priv->close_fd = g_value_get_boolean (value);
201 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
207 g_unix_output_stream_get_property (GObject *object,
212 GUnixOutputStream *unix_stream;
214 unix_stream = G_UNIX_OUTPUT_STREAM (object);
219 g_value_set_int (value, unix_stream->priv->fd);
222 g_value_set_boolean (value, unix_stream->priv->close_fd);
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
232 unix_stream->priv = G_TYPE_INSTANCE_GET_PRIVATE (unix_stream,
233 G_TYPE_UNIX_OUTPUT_STREAM,
234 GUnixOutputStreamPrivate);
236 unix_stream->priv->fd = -1;
237 unix_stream->priv->close_fd = TRUE;
241 * g_unix_output_stream_new:
242 * @fd: a UNIX file descriptor
243 * @close_fd: %TRUE to close the file descriptor when done
245 * Creates a new #GUnixOutputStream for the given @fd.
247 * If @close_fd, is %TRUE, the file descriptor will be closed when
248 * the output stream is destroyed.
250 * Returns: a new #GOutputStream
253 g_unix_output_stream_new (gint fd,
256 GUnixOutputStream *stream;
258 g_return_val_if_fail (fd != -1, NULL);
260 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
262 "close-fd", close_fd,
265 return G_OUTPUT_STREAM (stream);
269 * g_unix_output_stream_set_close_fd:
270 * @stream: a #GUnixOutputStream
271 * @close_fd: %TRUE to close the file descriptor when done
273 * Sets whether the file descriptor of @stream shall be closed
274 * when the stream is closed.
279 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
282 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
284 close_fd = close_fd != FALSE;
285 if (stream->priv->close_fd != close_fd)
287 stream->priv->close_fd = close_fd;
288 g_object_notify (G_OBJECT (stream), "close-fd");
293 * g_unix_output_stream_get_close_fd:
294 * @stream: a #GUnixOutputStream
296 * Returns whether the file descriptor of @stream will be
297 * closed when the stream is closed.
299 * Return value: %TRUE if the file descriptor is closed when done
304 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
306 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
308 return stream->priv->close_fd;
312 * g_unix_output_stream_get_fd:
313 * @stream: a #GUnixOutputStream
315 * Return the UNIX file descriptor that the stream writes to.
317 * Return value: The file descriptor of @stream
322 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
324 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
326 return stream->priv->fd;
330 g_unix_output_stream_write (GOutputStream *stream,
333 GCancellable *cancellable,
336 GUnixOutputStream *unix_stream;
342 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
344 poll_fds[0].fd = unix_stream->priv->fd;
345 poll_fds[0].events = G_IO_OUT;
347 if (unix_stream->priv->is_pipe_or_socket &&
348 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
355 poll_fds[0].revents = poll_fds[1].revents = 0;
357 poll_ret = g_poll (poll_fds, nfds, -1);
358 while (poll_ret == -1 && errno == EINTR);
364 g_set_error (error, G_IO_ERROR,
365 g_io_error_from_errno (errsv),
366 _("Error writing to file descriptor: %s"),
371 if (g_cancellable_set_error_if_cancelled (cancellable, error))
374 if (!poll_fds[0].revents)
377 res = write (unix_stream->priv->fd, buffer, count);
382 if (errsv == EINTR || errsv == EAGAIN)
385 g_set_error (error, G_IO_ERROR,
386 g_io_error_from_errno (errsv),
387 _("Error writing to file descriptor: %s"),
395 g_cancellable_release_fd (cancellable);
400 g_unix_output_stream_close (GOutputStream *stream,
401 GCancellable *cancellable,
404 GUnixOutputStream *unix_stream;
407 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
409 if (!unix_stream->priv->close_fd)
414 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
415 res = close (unix_stream->priv->fd);
420 g_set_error (error, G_IO_ERROR,
421 g_io_error_from_errno (errsv),
422 _("Error closing file descriptor: %s"),
432 GOutputStream *stream;
433 GAsyncReadyCallback callback;
438 close_async_cb (CloseAsyncData *data)
440 GUnixOutputStream *unix_stream;
441 GSimpleAsyncResult *simple;
442 GError *error = NULL;
446 unix_stream = G_UNIX_OUTPUT_STREAM (data->stream);
448 if (!unix_stream->priv->close_fd)
456 res = close (unix_stream->priv->fd);
461 g_set_error (&error, G_IO_ERROR,
462 g_io_error_from_errno (errsv),
463 _("Error closing file descriptor: %s"),
472 simple = g_simple_async_result_new (G_OBJECT (data->stream),
475 g_unix_output_stream_close_async);
478 g_simple_async_result_take_error (simple, error);
480 /* Complete immediately, not in idle, since we're already in a mainloop callout */
481 g_simple_async_result_complete (simple);
482 g_object_unref (simple);
488 g_unix_output_stream_close_async (GOutputStream *stream,
490 GCancellable *cancellable,
491 GAsyncReadyCallback callback,
495 CloseAsyncData *data;
497 data = g_new0 (CloseAsyncData, 1);
499 data->stream = stream;
500 data->callback = callback;
501 data->user_data = user_data;
503 idle = g_idle_source_new ();
504 g_source_set_callback (idle, (GSourceFunc)close_async_cb, data, g_free);
505 g_source_attach (idle, g_main_context_get_thread_default ());
506 g_source_unref (idle);
510 g_unix_output_stream_close_finish (GOutputStream *stream,
511 GAsyncResult *result,
514 /* Failures handled in generic close_finish code */
519 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
521 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
525 poll_fd.fd = unix_stream->priv->fd;
526 poll_fd.events = G_IO_OUT;
529 result = g_poll (&poll_fd, 1, 0);
530 while (result == -1 && errno == EINTR);
532 return poll_fd.revents != 0;
536 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
537 GCancellable *cancellable)
539 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
540 GSource *inner_source, *pollable_source;
542 pollable_source = g_pollable_source_new (G_OBJECT (stream));
544 inner_source = _g_fd_source_new (unix_stream->priv->fd, G_IO_OUT, cancellable);
545 g_source_set_dummy_callback (inner_source);
546 g_source_add_child_source (pollable_source, inner_source);
547 g_source_unref (inner_source);
549 return pollable_source;