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, see <http://www.gnu.org/licenses/>.
18 * Author: Alexander Larsson <alexl@redhat.com>
23 #include <sys/types.h>
31 #include <glib/gstdio.h>
32 #include <glib/glib-unix.h>
34 #include "gunixoutputstream.h"
35 #include "gcancellable.h"
36 #include "gsimpleasyncresult.h"
37 #include "gasynchelper.h"
38 #include "gfiledescriptorbased.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 UNIX
49 * file descriptor, including asynchronous operations. (If the file
50 * descriptor refers to a socket or pipe, this will use poll() to do
51 * asynchronous I/O. If it refers to a regular file, it will fall back
52 * to doing asynchronous I/O in another thread.)
54 * Note that `<gio/gunixoutputstream.h>` belongs to the UNIX-specific GIO
55 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config file
65 struct _GUnixOutputStreamPrivate {
68 guint is_pipe_or_socket : 1;
71 static void g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
72 static void g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
74 G_DEFINE_TYPE_WITH_CODE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM,
75 G_ADD_PRIVATE (GUnixOutputStream)
76 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
77 g_unix_output_stream_pollable_iface_init)
78 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
79 g_unix_output_stream_file_descriptor_based_iface_init)
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_can_poll (GPollableOutputStream *stream);
108 static gboolean g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream);
109 static GSource *g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
110 GCancellable *cancellable);
113 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
115 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
116 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
118 gobject_class->get_property = g_unix_output_stream_get_property;
119 gobject_class->set_property = g_unix_output_stream_set_property;
121 stream_class->write_fn = g_unix_output_stream_write;
122 stream_class->close_fn = g_unix_output_stream_close;
123 stream_class->close_async = g_unix_output_stream_close_async;
124 stream_class->close_finish = g_unix_output_stream_close_finish;
127 * GUnixOutputStream:fd:
129 * The file descriptor that the stream writes to.
133 g_object_class_install_property (gobject_class,
135 g_param_spec_int ("fd",
136 P_("File descriptor"),
137 P_("The file descriptor to write to"),
138 G_MININT, G_MAXINT, -1,
139 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
142 * GUnixOutputStream:close-fd:
144 * Whether to close the file descriptor when the stream is closed.
148 g_object_class_install_property (gobject_class,
150 g_param_spec_boolean ("close-fd",
151 P_("Close file descriptor"),
152 P_("Whether to close the file descriptor when the stream is closed"),
154 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
158 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
160 iface->can_poll = g_unix_output_stream_pollable_can_poll;
161 iface->is_writable = g_unix_output_stream_pollable_is_writable;
162 iface->create_source = g_unix_output_stream_pollable_create_source;
166 g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
168 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd;
172 g_unix_output_stream_set_property (GObject *object,
177 GUnixOutputStream *unix_stream;
179 unix_stream = G_UNIX_OUTPUT_STREAM (object);
184 unix_stream->priv->fd = g_value_get_int (value);
185 if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
186 unix_stream->priv->is_pipe_or_socket = TRUE;
188 unix_stream->priv->is_pipe_or_socket = FALSE;
191 unix_stream->priv->close_fd = g_value_get_boolean (value);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 g_unix_output_stream_get_property (GObject *object,
205 GUnixOutputStream *unix_stream;
207 unix_stream = G_UNIX_OUTPUT_STREAM (object);
212 g_value_set_int (value, unix_stream->priv->fd);
215 g_value_set_boolean (value, unix_stream->priv->close_fd);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
225 unix_stream->priv = g_unix_output_stream_get_instance_private (unix_stream);
226 unix_stream->priv->fd = -1;
227 unix_stream->priv->close_fd = TRUE;
231 * g_unix_output_stream_new:
232 * @fd: a UNIX file descriptor
233 * @close_fd: %TRUE to close the file descriptor when done
235 * Creates a new #GUnixOutputStream for the given @fd.
237 * If @close_fd, is %TRUE, the file descriptor will be closed when
238 * the output stream is destroyed.
240 * Returns: a new #GOutputStream
243 g_unix_output_stream_new (gint fd,
246 GUnixOutputStream *stream;
248 g_return_val_if_fail (fd != -1, NULL);
250 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
252 "close-fd", close_fd,
255 return G_OUTPUT_STREAM (stream);
259 * g_unix_output_stream_set_close_fd:
260 * @stream: a #GUnixOutputStream
261 * @close_fd: %TRUE to close the file descriptor when done
263 * Sets whether the file descriptor of @stream shall be closed
264 * when the stream is closed.
269 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
272 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
274 close_fd = close_fd != FALSE;
275 if (stream->priv->close_fd != close_fd)
277 stream->priv->close_fd = close_fd;
278 g_object_notify (G_OBJECT (stream), "close-fd");
283 * g_unix_output_stream_get_close_fd:
284 * @stream: a #GUnixOutputStream
286 * Returns whether the file descriptor of @stream will be
287 * closed when the stream is closed.
289 * Returns: %TRUE if the file descriptor is closed when done
294 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
296 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
298 return stream->priv->close_fd;
302 * g_unix_output_stream_get_fd:
303 * @stream: a #GUnixOutputStream
305 * Return the UNIX file descriptor that the stream writes to.
307 * Returns: The file descriptor of @stream
312 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
314 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
316 return stream->priv->fd;
320 g_unix_output_stream_write (GOutputStream *stream,
323 GCancellable *cancellable,
326 GUnixOutputStream *unix_stream;
332 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
334 poll_fds[0].fd = unix_stream->priv->fd;
335 poll_fds[0].events = G_IO_OUT;
337 if (unix_stream->priv->is_pipe_or_socket &&
338 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
345 poll_fds[0].revents = poll_fds[1].revents = 0;
347 poll_ret = g_poll (poll_fds, nfds, -1);
348 while (poll_ret == -1 && errno == EINTR);
354 g_set_error (error, G_IO_ERROR,
355 g_io_error_from_errno (errsv),
356 _("Error writing to file descriptor: %s"),
361 if (g_cancellable_set_error_if_cancelled (cancellable, error))
364 if (!poll_fds[0].revents)
367 res = write (unix_stream->priv->fd, buffer, count);
372 if (errsv == EINTR || errsv == EAGAIN)
375 g_set_error (error, G_IO_ERROR,
376 g_io_error_from_errno (errsv),
377 _("Error writing to file descriptor: %s"),
385 g_cancellable_release_fd (cancellable);
390 g_unix_output_stream_close (GOutputStream *stream,
391 GCancellable *cancellable,
394 GUnixOutputStream *unix_stream;
397 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
399 if (!unix_stream->priv->close_fd)
402 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
403 res = close (unix_stream->priv->fd);
408 g_set_error (error, G_IO_ERROR,
409 g_io_error_from_errno (errsv),
410 _("Error closing file descriptor: %s"),
418 g_unix_output_stream_close_async (GOutputStream *stream,
420 GCancellable *cancellable,
421 GAsyncReadyCallback callback,
425 GError *error = NULL;
427 task = g_task_new (stream, cancellable, callback, user_data);
428 g_task_set_priority (task, io_priority);
430 if (g_unix_output_stream_close (stream, cancellable, &error))
431 g_task_return_boolean (task, TRUE);
433 g_task_return_error (task, error);
434 g_object_unref (task);
438 g_unix_output_stream_close_finish (GOutputStream *stream,
439 GAsyncResult *result,
442 g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
444 return g_task_propagate_boolean (G_TASK (result), error);
448 g_unix_output_stream_pollable_can_poll (GPollableOutputStream *stream)
450 return G_UNIX_OUTPUT_STREAM (stream)->priv->is_pipe_or_socket;
454 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
456 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
460 poll_fd.fd = unix_stream->priv->fd;
461 poll_fd.events = G_IO_OUT;
465 result = g_poll (&poll_fd, 1, 0);
466 while (result == -1 && errno == EINTR);
468 return poll_fd.revents != 0;
472 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
473 GCancellable *cancellable)
475 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
476 GSource *inner_source, *cancellable_source, *pollable_source;
478 pollable_source = g_pollable_source_new (G_OBJECT (stream));
480 inner_source = g_unix_fd_source_new (unix_stream->priv->fd, G_IO_OUT);
481 g_source_set_dummy_callback (inner_source);
482 g_source_add_child_source (pollable_source, inner_source);
483 g_source_unref (inner_source);
487 cancellable_source = g_cancellable_source_new (cancellable);
488 g_source_set_dummy_callback (cancellable_source);
489 g_source_add_child_source (pollable_source, cancellable_source);
490 g_source_unref (cancellable_source);
493 return pollable_source;