• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
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.1 of the License, or (at your option) any later version.
9  *
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.
14  *
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/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20 
21 #include "config.h"
22 
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <sys/uio.h>
30 
31 #include <glib.h>
32 #include <glib/gstdio.h>
33 #include <glib/glib-unix.h>
34 #include "gioerror.h"
35 #include "gunixoutputstream.h"
36 #include "gcancellable.h"
37 #include "gasynchelper.h"
38 #include "gfiledescriptorbased.h"
39 #include "glibintl.h"
40 #include "gioprivate.h"
41 
42 
43 /**
44  * SECTION:gunixoutputstream
45  * @short_description: Streaming output operations for UNIX file descriptors
46  * @include: gio/gunixoutputstream.h
47  * @see_also: #GOutputStream
48  *
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.)
54  *
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
57  * when using it.
58  */
59 
60 enum {
61   PROP_0,
62   PROP_FD,
63   PROP_CLOSE_FD
64 };
65 
66 struct _GUnixOutputStreamPrivate {
67   int fd;
68   guint close_fd : 1;
69   guint is_pipe_or_socket : 1;
70 };
71 
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);
74 
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)
81 			 )
82 
83 static void     g_unix_output_stream_set_property (GObject              *object,
84 						   guint                 prop_id,
85 						   const GValue         *value,
86 						   GParamSpec           *pspec);
87 static void     g_unix_output_stream_get_property (GObject              *object,
88 						   guint                 prop_id,
89 						   GValue               *value,
90 						   GParamSpec           *pspec);
91 static gssize   g_unix_output_stream_write        (GOutputStream        *stream,
92 						   const void           *buffer,
93 						   gsize                 count,
94 						   GCancellable         *cancellable,
95 						   GError              **error);
96 static gboolean g_unix_output_stream_writev       (GOutputStream        *stream,
97 						   const GOutputVector  *vectors,
98 						   gsize                 n_vectors,
99 						   gsize                *bytes_written,
100 						   GCancellable         *cancellable,
101 						   GError              **error);
102 static gboolean g_unix_output_stream_close        (GOutputStream        *stream,
103 						   GCancellable         *cancellable,
104 						   GError              **error);
105 static void     g_unix_output_stream_close_async  (GOutputStream        *stream,
106 						   int                   io_priority,
107 						   GCancellable         *cancellable,
108 						   GAsyncReadyCallback   callback,
109 						   gpointer              data);
110 static gboolean g_unix_output_stream_close_finish (GOutputStream        *stream,
111 						   GAsyncResult         *result,
112 						   GError              **error);
113 
114 static gboolean g_unix_output_stream_pollable_can_poll      (GPollableOutputStream *stream);
115 static gboolean g_unix_output_stream_pollable_is_writable   (GPollableOutputStream *stream);
116 static GSource *g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
117 							     GCancellable         *cancellable);
118 static GPollableReturn g_unix_output_stream_pollable_writev_nonblocking (GPollableOutputStream  *stream,
119 									 const GOutputVector    *vectors,
120 									 gsize                   n_vectors,
121 									 gsize                  *bytes_written,
122 									 GError                **error);
123 
124 static void
g_unix_output_stream_class_init(GUnixOutputStreamClass * klass)125 g_unix_output_stream_class_init (GUnixOutputStreamClass *klass)
126 {
127   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
128   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
129 
130   gobject_class->get_property = g_unix_output_stream_get_property;
131   gobject_class->set_property = g_unix_output_stream_set_property;
132 
133   stream_class->write_fn = g_unix_output_stream_write;
134   stream_class->writev_fn = g_unix_output_stream_writev;
135   stream_class->close_fn = g_unix_output_stream_close;
136   stream_class->close_async = g_unix_output_stream_close_async;
137   stream_class->close_finish = g_unix_output_stream_close_finish;
138 
139    /**
140    * GUnixOutputStream:fd:
141    *
142    * The file descriptor that the stream writes to.
143    *
144    * Since: 2.20
145    */
146   g_object_class_install_property (gobject_class,
147 				   PROP_FD,
148 				   g_param_spec_int ("fd",
149 						     P_("File descriptor"),
150 						     P_("The file descriptor to write to"),
151 						     G_MININT, G_MAXINT, -1,
152 						     G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
153 
154   /**
155    * GUnixOutputStream:close-fd:
156    *
157    * Whether to close the file descriptor when the stream is closed.
158    *
159    * Since: 2.20
160    */
161   g_object_class_install_property (gobject_class,
162 				   PROP_CLOSE_FD,
163 				   g_param_spec_boolean ("close-fd",
164 							 P_("Close file descriptor"),
165 							 P_("Whether to close the file descriptor when the stream is closed"),
166 							 TRUE,
167 							 G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
168 }
169 
170 static void
g_unix_output_stream_pollable_iface_init(GPollableOutputStreamInterface * iface)171 g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface)
172 {
173   iface->can_poll = g_unix_output_stream_pollable_can_poll;
174   iface->is_writable = g_unix_output_stream_pollable_is_writable;
175   iface->create_source = g_unix_output_stream_pollable_create_source;
176   iface->writev_nonblocking = g_unix_output_stream_pollable_writev_nonblocking;
177 }
178 
179 static void
g_unix_output_stream_file_descriptor_based_iface_init(GFileDescriptorBasedIface * iface)180 g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface)
181 {
182   iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd;
183 }
184 
185 static void
g_unix_output_stream_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)186 g_unix_output_stream_set_property (GObject         *object,
187 				   guint            prop_id,
188 				   const GValue    *value,
189 				   GParamSpec      *pspec)
190 {
191   GUnixOutputStream *unix_stream;
192 
193   unix_stream = G_UNIX_OUTPUT_STREAM (object);
194 
195   switch (prop_id)
196     {
197     case PROP_FD:
198       unix_stream->priv->fd = g_value_get_int (value);
199       if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE)
200 	unix_stream->priv->is_pipe_or_socket = TRUE;
201       else
202 	unix_stream->priv->is_pipe_or_socket = FALSE;
203       break;
204     case PROP_CLOSE_FD:
205       unix_stream->priv->close_fd = g_value_get_boolean (value);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210     }
211 }
212 
213 static void
g_unix_output_stream_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)214 g_unix_output_stream_get_property (GObject    *object,
215 				   guint       prop_id,
216 				   GValue     *value,
217 				   GParamSpec *pspec)
218 {
219   GUnixOutputStream *unix_stream;
220 
221   unix_stream = G_UNIX_OUTPUT_STREAM (object);
222 
223   switch (prop_id)
224     {
225     case PROP_FD:
226       g_value_set_int (value, unix_stream->priv->fd);
227       break;
228     case PROP_CLOSE_FD:
229       g_value_set_boolean (value, unix_stream->priv->close_fd);
230       break;
231     default:
232       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233     }
234 }
235 
236 static void
g_unix_output_stream_init(GUnixOutputStream * unix_stream)237 g_unix_output_stream_init (GUnixOutputStream *unix_stream)
238 {
239   unix_stream->priv = g_unix_output_stream_get_instance_private (unix_stream);
240   unix_stream->priv->fd = -1;
241   unix_stream->priv->close_fd = TRUE;
242 }
243 
244 /**
245  * g_unix_output_stream_new:
246  * @fd: a UNIX file descriptor
247  * @close_fd: %TRUE to close the file descriptor when done
248  *
249  * Creates a new #GUnixOutputStream for the given @fd.
250  *
251  * If @close_fd, is %TRUE, the file descriptor will be closed when
252  * the output stream is destroyed.
253  *
254  * Returns: a new #GOutputStream
255  **/
256 GOutputStream *
g_unix_output_stream_new(gint fd,gboolean close_fd)257 g_unix_output_stream_new (gint     fd,
258 			  gboolean close_fd)
259 {
260   GUnixOutputStream *stream;
261 
262   g_return_val_if_fail (fd != -1, NULL);
263 
264   stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM,
265 			 "fd", fd,
266 			 "close-fd", close_fd,
267 			 NULL);
268 
269   return G_OUTPUT_STREAM (stream);
270 }
271 
272 /**
273  * g_unix_output_stream_set_close_fd:
274  * @stream: a #GUnixOutputStream
275  * @close_fd: %TRUE to close the file descriptor when done
276  *
277  * Sets whether the file descriptor of @stream shall be closed
278  * when the stream is closed.
279  *
280  * Since: 2.20
281  */
282 void
g_unix_output_stream_set_close_fd(GUnixOutputStream * stream,gboolean close_fd)283 g_unix_output_stream_set_close_fd (GUnixOutputStream *stream,
284                                    gboolean           close_fd)
285 {
286   g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream));
287 
288   close_fd = close_fd != FALSE;
289   if (stream->priv->close_fd != close_fd)
290     {
291       stream->priv->close_fd = close_fd;
292       g_object_notify (G_OBJECT (stream), "close-fd");
293     }
294 }
295 
296 /**
297  * g_unix_output_stream_get_close_fd:
298  * @stream: a #GUnixOutputStream
299  *
300  * Returns whether the file descriptor of @stream will be
301  * closed when the stream is closed.
302  *
303  * Returns: %TRUE if the file descriptor is closed when done
304  *
305  * Since: 2.20
306  */
307 gboolean
g_unix_output_stream_get_close_fd(GUnixOutputStream * stream)308 g_unix_output_stream_get_close_fd (GUnixOutputStream *stream)
309 {
310   g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE);
311 
312   return stream->priv->close_fd;
313 }
314 
315 /**
316  * g_unix_output_stream_get_fd:
317  * @stream: a #GUnixOutputStream
318  *
319  * Return the UNIX file descriptor that the stream writes to.
320  *
321  * Returns: The file descriptor of @stream
322  *
323  * Since: 2.20
324  */
325 gint
g_unix_output_stream_get_fd(GUnixOutputStream * stream)326 g_unix_output_stream_get_fd (GUnixOutputStream *stream)
327 {
328   g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1);
329 
330   return stream->priv->fd;
331 }
332 
333 static gssize
g_unix_output_stream_write(GOutputStream * stream,const void * buffer,gsize count,GCancellable * cancellable,GError ** error)334 g_unix_output_stream_write (GOutputStream  *stream,
335 			    const void     *buffer,
336 			    gsize           count,
337 			    GCancellable   *cancellable,
338 			    GError        **error)
339 {
340   GUnixOutputStream *unix_stream;
341   gssize res = -1;
342   GPollFD poll_fds[2];
343   int nfds = 0;
344   int poll_ret;
345 
346   unix_stream = G_UNIX_OUTPUT_STREAM (stream);
347 
348   poll_fds[0].fd = unix_stream->priv->fd;
349   poll_fds[0].events = G_IO_OUT;
350   nfds++;
351 
352   if (unix_stream->priv->is_pipe_or_socket &&
353       g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
354     nfds++;
355 
356   while (1)
357     {
358       int errsv;
359 
360       poll_fds[0].revents = poll_fds[1].revents = 0;
361       do
362         {
363           poll_ret = g_poll (poll_fds, nfds, -1);
364           errsv = errno;
365         }
366       while (poll_ret == -1 && errsv == EINTR);
367 
368       if (poll_ret == -1)
369 	{
370 	  g_set_error (error, G_IO_ERROR,
371 		       g_io_error_from_errno (errsv),
372 		       _("Error writing to file descriptor: %s"),
373 		       g_strerror (errsv));
374 	  break;
375 	}
376 
377       if (g_cancellable_set_error_if_cancelled (cancellable, error))
378 	break;
379 
380       if (!poll_fds[0].revents)
381 	continue;
382 
383       res = write (unix_stream->priv->fd, buffer, count);
384       errsv = errno;
385       if (res == -1)
386 	{
387 	  if (errsv == EINTR || errsv == EAGAIN)
388 	    continue;
389 
390 	  g_set_error (error, G_IO_ERROR,
391 		       g_io_error_from_errno (errsv),
392 		       _("Error writing to file descriptor: %s"),
393 		       g_strerror (errsv));
394 	}
395 
396       break;
397     }
398 
399   if (nfds == 2)
400     g_cancellable_release_fd (cancellable);
401   return res;
402 }
403 
404 /* Macro to check if struct iovec and GOutputVector have the same ABI */
405 #define G_OUTPUT_VECTOR_IS_IOVEC (sizeof (struct iovec) == sizeof (GOutputVector) && \
406       sizeof ((struct iovec *) 0)->iov_base == sizeof ((GOutputVector *) 0)->buffer && \
407       G_STRUCT_OFFSET (struct iovec, iov_base) == G_STRUCT_OFFSET (GOutputVector, buffer) && \
408       sizeof ((struct iovec *) 0)->iov_len == sizeof((GOutputVector *) 0)->size && \
409       G_STRUCT_OFFSET (struct iovec, iov_len) == G_STRUCT_OFFSET (GOutputVector, size))
410 
411 static gboolean
g_unix_output_stream_writev(GOutputStream * stream,const GOutputVector * vectors,gsize n_vectors,gsize * bytes_written,GCancellable * cancellable,GError ** error)412 g_unix_output_stream_writev (GOutputStream        *stream,
413 			     const GOutputVector  *vectors,
414 			     gsize                 n_vectors,
415 			     gsize                *bytes_written,
416 			     GCancellable         *cancellable,
417 			     GError              **error)
418 {
419   GUnixOutputStream *unix_stream;
420   gssize res = -1;
421   GPollFD poll_fds[2];
422   int nfds = 0;
423   int poll_ret;
424   struct iovec *iov;
425 
426   if (bytes_written)
427     *bytes_written = 0;
428 
429   /* Clamp the number of vectors if more given than we can write in one go.
430    * The caller has to handle short writes anyway.
431    */
432   if (n_vectors > G_IOV_MAX)
433     n_vectors = G_IOV_MAX;
434 
435   unix_stream = G_UNIX_OUTPUT_STREAM (stream);
436 
437   if (G_OUTPUT_VECTOR_IS_IOVEC)
438     {
439       /* ABI is compatible */
440       iov = (struct iovec *) vectors;
441     }
442   else
443     {
444       gsize i;
445 
446       /* ABI is incompatible */
447       iov = g_newa (struct iovec, n_vectors);
448       for (i = 0; i < n_vectors; i++)
449         {
450           iov[i].iov_base = (void *)vectors[i].buffer;
451           iov[i].iov_len = vectors[i].size;
452         }
453     }
454 
455   poll_fds[0].fd = unix_stream->priv->fd;
456   poll_fds[0].events = G_IO_OUT;
457   nfds++;
458 
459   if (unix_stream->priv->is_pipe_or_socket &&
460       g_cancellable_make_pollfd (cancellable, &poll_fds[1]))
461     nfds++;
462 
463   while (1)
464     {
465       int errsv;
466 
467       poll_fds[0].revents = poll_fds[1].revents = 0;
468       do
469         {
470           poll_ret = g_poll (poll_fds, nfds, -1);
471           errsv = errno;
472         }
473       while (poll_ret == -1 && errsv == EINTR);
474 
475       if (poll_ret == -1)
476 	{
477 	  g_set_error (error, G_IO_ERROR,
478 		       g_io_error_from_errno (errsv),
479 		       _("Error writing to file descriptor: %s"),
480 		       g_strerror (errsv));
481 	  break;
482 	}
483 
484       if (g_cancellable_set_error_if_cancelled (cancellable, error))
485 	break;
486 
487       if (!poll_fds[0].revents)
488 	continue;
489 
490       res = writev (unix_stream->priv->fd, iov, n_vectors);
491       errsv = errno;
492       if (res == -1)
493 	{
494 	  if (errsv == EINTR || errsv == EAGAIN)
495 	    continue;
496 
497 	  g_set_error (error, G_IO_ERROR,
498 		       g_io_error_from_errno (errsv),
499 		       _("Error writing to file descriptor: %s"),
500 		       g_strerror (errsv));
501 	}
502 
503       if (bytes_written)
504         *bytes_written = res;
505 
506       break;
507     }
508 
509   if (nfds == 2)
510     g_cancellable_release_fd (cancellable);
511   return res != -1;
512 }
513 
514 static gboolean
g_unix_output_stream_close(GOutputStream * stream,GCancellable * cancellable,GError ** error)515 g_unix_output_stream_close (GOutputStream  *stream,
516 			    GCancellable   *cancellable,
517 			    GError        **error)
518 {
519   GUnixOutputStream *unix_stream;
520   int res;
521 
522   unix_stream = G_UNIX_OUTPUT_STREAM (stream);
523 
524   if (!unix_stream->priv->close_fd)
525     return TRUE;
526 
527   /* This might block during the close. Doesn't seem to be a way to avoid it though. */
528   res = close (unix_stream->priv->fd);
529   if (res == -1)
530     {
531       int errsv = errno;
532 
533       g_set_error (error, G_IO_ERROR,
534 		   g_io_error_from_errno (errsv),
535 		   _("Error closing file descriptor: %s"),
536 		   g_strerror (errsv));
537     }
538 
539   return res != -1;
540 }
541 
542 static void
g_unix_output_stream_close_async(GOutputStream * stream,int io_priority,GCancellable * cancellable,GAsyncReadyCallback callback,gpointer user_data)543 g_unix_output_stream_close_async (GOutputStream       *stream,
544 				  int                  io_priority,
545 				  GCancellable        *cancellable,
546 				  GAsyncReadyCallback  callback,
547 				  gpointer             user_data)
548 {
549   GTask *task;
550   GError *error = NULL;
551 
552   task = g_task_new (stream, cancellable, callback, user_data);
553   g_task_set_source_tag (task, g_unix_output_stream_close_async);
554   g_task_set_priority (task, io_priority);
555 
556   if (g_unix_output_stream_close (stream, cancellable, &error))
557     g_task_return_boolean (task, TRUE);
558   else
559     g_task_return_error (task, error);
560   g_object_unref (task);
561 }
562 
563 static gboolean
g_unix_output_stream_close_finish(GOutputStream * stream,GAsyncResult * result,GError ** error)564 g_unix_output_stream_close_finish (GOutputStream  *stream,
565 				   GAsyncResult   *result,
566 				   GError        **error)
567 {
568   g_return_val_if_fail (g_task_is_valid (result, stream), FALSE);
569 
570   return g_task_propagate_boolean (G_TASK (result), error);
571 }
572 
573 static gboolean
g_unix_output_stream_pollable_can_poll(GPollableOutputStream * stream)574 g_unix_output_stream_pollable_can_poll (GPollableOutputStream *stream)
575 {
576   return G_UNIX_OUTPUT_STREAM (stream)->priv->is_pipe_or_socket;
577 }
578 
579 static gboolean
g_unix_output_stream_pollable_is_writable(GPollableOutputStream * stream)580 g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream)
581 {
582   GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
583   GPollFD poll_fd;
584   gint result;
585 
586   poll_fd.fd = unix_stream->priv->fd;
587   poll_fd.events = G_IO_OUT;
588   poll_fd.revents = 0;
589 
590   do
591     result = g_poll (&poll_fd, 1, 0);
592   while (result == -1 && errno == EINTR);
593 
594   return poll_fd.revents != 0;
595 }
596 
597 static GSource *
g_unix_output_stream_pollable_create_source(GPollableOutputStream * stream,GCancellable * cancellable)598 g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream,
599 					     GCancellable          *cancellable)
600 {
601   GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
602   GSource *inner_source, *cancellable_source, *pollable_source;
603 
604   pollable_source = g_pollable_source_new (G_OBJECT (stream));
605 
606   inner_source = g_unix_fd_source_new (unix_stream->priv->fd, G_IO_OUT);
607   g_source_set_dummy_callback (inner_source);
608   g_source_add_child_source (pollable_source, inner_source);
609   g_source_unref (inner_source);
610 
611   if (cancellable)
612     {
613       cancellable_source = g_cancellable_source_new (cancellable);
614       g_source_set_dummy_callback (cancellable_source);
615       g_source_add_child_source (pollable_source, cancellable_source);
616       g_source_unref (cancellable_source);
617     }
618 
619   return pollable_source;
620 }
621 
622 static GPollableReturn
g_unix_output_stream_pollable_writev_nonblocking(GPollableOutputStream * stream,const GOutputVector * vectors,gsize n_vectors,gsize * bytes_written,GError ** error)623 g_unix_output_stream_pollable_writev_nonblocking (GPollableOutputStream  *stream,
624 						  const GOutputVector    *vectors,
625 						  gsize                   n_vectors,
626 						  gsize                  *bytes_written,
627 						  GError                **error)
628 {
629   GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream);
630   struct iovec *iov;
631   gssize res = -1;
632 
633   if (!g_pollable_output_stream_is_writable (stream))
634     {
635       *bytes_written = 0;
636       return G_POLLABLE_RETURN_WOULD_BLOCK;
637     }
638 
639   /* Clamp the number of vectors if more given than we can write in one go.
640    * The caller has to handle short writes anyway.
641    */
642   if (n_vectors > G_IOV_MAX)
643     n_vectors = G_IOV_MAX;
644 
645   if (G_OUTPUT_VECTOR_IS_IOVEC)
646     {
647       /* ABI is compatible */
648       iov = (struct iovec *) vectors;
649     }
650   else
651     {
652       gsize i;
653 
654       /* ABI is incompatible */
655       iov = g_newa (struct iovec, n_vectors);
656       for (i = 0; i < n_vectors; i++)
657         {
658           iov[i].iov_base = (void *)vectors[i].buffer;
659           iov[i].iov_len = vectors[i].size;
660         }
661     }
662 
663   while (1)
664     {
665       int errsv;
666 
667       res = writev (unix_stream->priv->fd, iov, n_vectors);
668       errsv = errno;
669       if (res == -1)
670 	{
671 	  if (errsv == EINTR)
672 	    continue;
673 
674 	  g_set_error (error, G_IO_ERROR,
675 		       g_io_error_from_errno (errsv),
676 		       _("Error writing to file descriptor: %s"),
677 		       g_strerror (errsv));
678 	}
679 
680       if (bytes_written)
681         *bytes_written = res;
682 
683       break;
684     }
685 
686   return res != -1 ? G_POLLABLE_RETURN_OK : G_POLLABLE_RETURN_FAILED;
687 }
688