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