1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
5 * SPDX-License-Identifier: LGPL-2.1-or-later
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General
18 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 * Author: Alexander Larsson <alexl@redhat.com>
31 #include <glib/gstdio.h>
32 #include <glib/glib-unix.h>
34 #include "gunixoutputstream.h"
35 #include "gcancellable.h"
36 #include "gasynchelper.h"
37 #include "gfiledescriptorbased.h"
39 #include "gioprivate.h"
40 #include "giounix-private.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 `<gio/gunixoutputstream.h>` belongs to the UNIX-specific GIO
56 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config file
66 struct _GUnixOutputStreamPrivate {
72 static void g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface);
73 static void g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface);
75 G_DEFINE_TYPE_WITH_CODE (GUnixOutputStream, g_unix_output_stream, G_TYPE_OUTPUT_STREAM,
76 G_ADD_PRIVATE (GUnixOutputStream)
77 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_OUTPUT_STREAM,
78 g_unix_output_stream_pollable_iface_init)
79 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED,
80 g_unix_output_stream_file_descriptor_based_iface_init)
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_writev (GOutputStream *stream,
97 const GOutputVector *vectors,
100 GCancellable *cancellable,
102 static gboolean g_unix_output_stream_close (GOutputStream *stream,
103 GCancellable *cancellable,
106 static gboolean g_unix_output_stream_pollable_can_poll (GPollableOutputStream *stream);
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);
110 static GPollableReturn g_unix_output_stream_pollable_writev_nonblocking (GPollableOutputStream *stream,
111 const GOutputVector *vectors,
113 gsize *bytes_written,
117 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
119 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
120 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
122 gobject_class->get_property = g_unix_output_stream_get_property;
123 gobject_class->set_property = g_unix_output_stream_set_property;
125 stream_class->write_fn = g_unix_output_stream_write;
126 stream_class->writev_fn = g_unix_output_stream_writev;
127 stream_class->close_fn = g_unix_output_stream_close;
130 * GUnixOutputStream:fd:
132 * The file descriptor that the stream writes to.
136 g_object_class_install_property (gobject_class,
138 g_param_spec_int ("fd",
139 P_("File descriptor"),
140 P_("The file descriptor to write to"),
141 G_MININT, G_MAXINT, -1,
142 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
145 * GUnixOutputStream:close-fd:
147 * Whether to close the file descriptor when the stream is closed.
151 g_object_class_install_property (gobject_class,
153 g_param_spec_boolean ("close-fd",
154 P_("Close file descriptor"),
155 P_("Whether to close the file descriptor when the stream is closed"),
157 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
161 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
163 iface->can_poll = g_unix_output_stream_pollable_can_poll;
164 iface->is_writable = g_unix_output_stream_pollable_is_writable;
165 iface->create_source = g_unix_output_stream_pollable_create_source;
166 iface->writev_nonblocking = g_unix_output_stream_pollable_writev_nonblocking;
170 g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
172 iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd;
176 g_unix_output_stream_set_property (GObject *object,
181 GUnixOutputStream *unix_stream;
183 unix_stream = G_UNIX_OUTPUT_STREAM (object);
188 unix_stream->priv->fd = g_value_get_int (value);
189 unix_stream->priv->can_poll = _g_fd_is_pollable (unix_stream->priv->fd);
192 unix_stream->priv->close_fd = g_value_get_boolean (value);
195 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201 g_unix_output_stream_get_property (GObject *object,
206 GUnixOutputStream *unix_stream;
208 unix_stream = G_UNIX_OUTPUT_STREAM (object);
213 g_value_set_int (value, unix_stream->priv->fd);
216 g_value_set_boolean (value, unix_stream->priv->close_fd);
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
224 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
226 unix_stream->priv = g_unix_output_stream_get_instance_private (unix_stream);
227 unix_stream->priv->fd = -1;
228 unix_stream->priv->close_fd = TRUE;
232 * g_unix_output_stream_new:
233 * @fd: a UNIX file descriptor
234 * @close_fd: %TRUE to close the file descriptor when done
236 * Creates a new #GUnixOutputStream for the given @fd.
238 * If @close_fd, is %TRUE, the file descriptor will be closed when
239 * the output stream is destroyed.
241 * Returns: a new #GOutputStream
244 g_unix_output_stream_new (gint fd,
247 GUnixOutputStream *stream;
249 g_return_val_if_fail (fd != -1, NULL);
251 stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
253 "close-fd", close_fd,
256 return G_OUTPUT_STREAM (stream);
260 * g_unix_output_stream_set_close_fd:
261 * @stream: a #GUnixOutputStream
262 * @close_fd: %TRUE to close the file descriptor when done
264 * Sets whether the file descriptor of @stream shall be closed
265 * when the stream is closed.
270 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
273 g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
275 close_fd = close_fd != FALSE;
276 if (stream->priv->close_fd != close_fd)
278 stream->priv->close_fd = close_fd;
279 g_object_notify (G_OBJECT (stream), "close-fd");
284 * g_unix_output_stream_get_close_fd:
285 * @stream: a #GUnixOutputStream
287 * Returns whether the file descriptor of @stream will be
288 * closed when the stream is closed.
290 * Returns: %TRUE if the file descriptor is closed when done
295 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
297 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
299 return stream->priv->close_fd;
303 * g_unix_output_stream_get_fd:
304 * @stream: a #GUnixOutputStream
306 * Return the UNIX file descriptor that the stream writes to.
308 * Returns: The file descriptor of @stream
313 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
315 g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
317 return stream->priv->fd;
321 g_unix_output_stream_write (GOutputStream *stream,
324 GCancellable *cancellable,
327 GUnixOutputStream *unix_stream;
333 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
335 poll_fds[0].fd = unix_stream->priv->fd;
336 poll_fds[0].events = G_IO_OUT;
339 if (unix_stream->priv->can_poll &&
340 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
347 poll_fds[0].revents = poll_fds[1].revents = 0;
350 poll_ret = g_poll (poll_fds, nfds, -1);
353 while (poll_ret == -1 && errsv == EINTR);
357 g_set_error (error, G_IO_ERROR,
358 g_io_error_from_errno (errsv),
359 _("Error writing to file descriptor: %s"),
364 if (g_cancellable_set_error_if_cancelled (cancellable, error))
367 if (!poll_fds[0].revents)
370 res = write (unix_stream->priv->fd, buffer, count);
374 if (errsv == EINTR || errsv == EAGAIN)
377 g_set_error (error, G_IO_ERROR,
378 g_io_error_from_errno (errsv),
379 _("Error writing to file descriptor: %s"),
387 g_cancellable_release_fd (cancellable);
391 /* Macro to check if struct iovec and GOutputVector have the same ABI */
392 #define G_OUTPUT_VECTOR_IS_IOVEC (sizeof (struct iovec) == sizeof (GOutputVector) && \
393 G_SIZEOF_MEMBER (struct iovec, iov_base) == G_SIZEOF_MEMBER (GOutputVector, buffer) && \
394 G_STRUCT_OFFSET (struct iovec, iov_base) == G_STRUCT_OFFSET (GOutputVector, buffer) && \
395 G_SIZEOF_MEMBER (struct iovec, iov_len) == G_SIZEOF_MEMBER (GOutputVector, size) && \
396 G_STRUCT_OFFSET (struct iovec, iov_len) == G_STRUCT_OFFSET (GOutputVector, size))
399 g_unix_output_stream_writev (GOutputStream *stream,
400 const GOutputVector *vectors,
402 gsize *bytes_written,
403 GCancellable *cancellable,
406 GUnixOutputStream *unix_stream;
416 /* Clamp the number of vectors if more given than we can write in one go.
417 * The caller has to handle short writes anyway.
419 if (n_vectors > G_IOV_MAX)
420 n_vectors = G_IOV_MAX;
422 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
424 if (G_OUTPUT_VECTOR_IS_IOVEC)
426 /* ABI is compatible */
427 iov = (struct iovec *) vectors;
433 /* ABI is incompatible */
434 iov = g_newa (struct iovec, n_vectors);
435 for (i = 0; i < n_vectors; i++)
437 iov[i].iov_base = (void *)vectors[i].buffer;
438 iov[i].iov_len = vectors[i].size;
442 poll_fds[0].fd = unix_stream->priv->fd;
443 poll_fds[0].events = G_IO_OUT;
446 if (unix_stream->priv->can_poll &&
447 g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
454 poll_fds[0].revents = poll_fds[1].revents = 0;
457 poll_ret = g_poll (poll_fds, nfds, -1);
460 while (poll_ret == -1 && errsv == EINTR);
464 g_set_error (error, G_IO_ERROR,
465 g_io_error_from_errno (errsv),
466 _("Error writing to file descriptor: %s"),
471 if (g_cancellable_set_error_if_cancelled (cancellable, error))
474 if (!poll_fds[0].revents)
477 res = writev (unix_stream->priv->fd, iov, n_vectors);
481 if (errsv == EINTR || errsv == EAGAIN)
484 g_set_error (error, G_IO_ERROR,
485 g_io_error_from_errno (errsv),
486 _("Error writing to file descriptor: %s"),
491 *bytes_written = res;
497 g_cancellable_release_fd (cancellable);
502 g_unix_output_stream_close (GOutputStream *stream,
503 GCancellable *cancellable,
506 GUnixOutputStream *unix_stream;
509 unix_stream = G_UNIX_OUTPUT_STREAM (stream);
511 if (!unix_stream->priv->close_fd)
514 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
515 res = close (unix_stream->priv->fd);
520 g_set_error (error, G_IO_ERROR,
521 g_io_error_from_errno (errsv),
522 _("Error closing file descriptor: %s"),
530 g_unix_output_stream_pollable_can_poll (GPollableOutputStream *stream)
532 return G_UNIX_OUTPUT_STREAM (stream)->priv->can_poll;
536 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
538 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
542 poll_fd.fd = unix_stream->priv->fd;
543 poll_fd.events = G_IO_OUT;
547 result = g_poll (&poll_fd, 1, 0);
548 while (result == -1 && errno == EINTR);
550 return poll_fd.revents != 0;
554 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
555 GCancellable *cancellable)
557 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
558 GSource *inner_source, *cancellable_source, *pollable_source;
560 pollable_source = g_pollable_source_new (G_OBJECT (stream));
562 inner_source = g_unix_fd_source_new (unix_stream->priv->fd, G_IO_OUT);
563 g_source_set_dummy_callback (inner_source);
564 g_source_add_child_source (pollable_source, inner_source);
565 g_source_unref (inner_source);
569 cancellable_source = g_cancellable_source_new (cancellable);
570 g_source_set_dummy_callback (cancellable_source);
571 g_source_add_child_source (pollable_source, cancellable_source);
572 g_source_unref (cancellable_source);
575 return pollable_source;
578 static GPollableReturn
579 g_unix_output_stream_pollable_writev_nonblocking (GPollableOutputStream *stream,
580 const GOutputVector *vectors,
582 gsize *bytes_written,
585 GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
589 if (!g_pollable_output_stream_is_writable (stream))
592 return G_POLLABLE_RETURN_WOULD_BLOCK;
595 /* Clamp the number of vectors if more given than we can write in one go.
596 * The caller has to handle short writes anyway.
598 if (n_vectors > G_IOV_MAX)
599 n_vectors = G_IOV_MAX;
601 if (G_OUTPUT_VECTOR_IS_IOVEC)
603 /* ABI is compatible */
604 iov = (struct iovec *) vectors;
610 /* ABI is incompatible */
611 iov = g_newa (struct iovec, n_vectors);
612 for (i = 0; i < n_vectors; i++)
614 iov[i].iov_base = (void *)vectors[i].buffer;
615 iov[i].iov_len = vectors[i].size;
623 res = writev (unix_stream->priv->fd, iov, n_vectors);
630 g_set_error (error, G_IO_ERROR,
631 g_io_error_from_errno (errsv),
632 _("Error writing to file descriptor: %s"),
637 *bytes_written = res;
642 return res != -1 ? G_POLLABLE_RETURN_OK : G_POLLABLE_RETURN_FAILED;