nexmon – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 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 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 | |||
30 | #include <glib.h> |
||
31 | #include <glib/gstdio.h> |
||
32 | #include <glib/glib-unix.h> |
||
33 | #include "gioerror.h" |
||
34 | #include "gunixoutputstream.h" |
||
35 | #include "gcancellable.h" |
||
36 | #include "gasynchelper.h" |
||
37 | #include "gfiledescriptorbased.h" |
||
38 | #include "glibintl.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 is_pipe_or_socket : 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_close (GOutputStream *stream, |
||
95 | GCancellable *cancellable, |
||
96 | GError **error); |
||
97 | static void g_unix_output_stream_close_async (GOutputStream *stream, |
||
98 | int io_priority, |
||
99 | GCancellable *cancellable, |
||
100 | GAsyncReadyCallback callback, |
||
101 | gpointer data); |
||
102 | static gboolean g_unix_output_stream_close_finish (GOutputStream *stream, |
||
103 | GAsyncResult *result, |
||
104 | GError **error); |
||
105 | |||
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 | |||
111 | static void |
||
112 | g_unix_output_stream_class_init (GUnixOutputStreamClass *klass) |
||
113 | { |
||
114 | GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
||
115 | GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass); |
||
116 | |||
117 | gobject_class->get_property = g_unix_output_stream_get_property; |
||
118 | gobject_class->set_property = g_unix_output_stream_set_property; |
||
119 | |||
120 | stream_class->write_fn = g_unix_output_stream_write; |
||
121 | stream_class->close_fn = g_unix_output_stream_close; |
||
122 | stream_class->close_async = g_unix_output_stream_close_async; |
||
123 | stream_class->close_finish = g_unix_output_stream_close_finish; |
||
124 | |||
125 | /** |
||
126 | * GUnixOutputStream:fd: |
||
127 | * |
||
128 | * The file descriptor that the stream writes to. |
||
129 | * |
||
130 | * Since: 2.20 |
||
131 | */ |
||
132 | g_object_class_install_property (gobject_class, |
||
133 | PROP_FD, |
||
134 | g_param_spec_int ("fd", |
||
135 | P_("File descriptor"), |
||
136 | P_("The file descriptor to write to"), |
||
137 | G_MININT, G_MAXINT, -1, |
||
138 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); |
||
139 | |||
140 | /** |
||
141 | * GUnixOutputStream:close-fd: |
||
142 | * |
||
143 | * Whether to close the file descriptor when the stream is closed. |
||
144 | * |
||
145 | * Since: 2.20 |
||
146 | */ |
||
147 | g_object_class_install_property (gobject_class, |
||
148 | PROP_CLOSE_FD, |
||
149 | g_param_spec_boolean ("close-fd", |
||
150 | P_("Close file descriptor"), |
||
151 | P_("Whether to close the file descriptor when the stream is closed"), |
||
152 | TRUE, |
||
153 | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB)); |
||
154 | } |
||
155 | |||
156 | static void |
||
157 | g_unix_output_stream_pollable_iface_init (GPollableOutputStreamInterface *iface) |
||
158 | { |
||
159 | iface->can_poll = g_unix_output_stream_pollable_can_poll; |
||
160 | iface->is_writable = g_unix_output_stream_pollable_is_writable; |
||
161 | iface->create_source = g_unix_output_stream_pollable_create_source; |
||
162 | } |
||
163 | |||
164 | static void |
||
165 | g_unix_output_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface *iface) |
||
166 | { |
||
167 | iface->get_fd = (int (*) (GFileDescriptorBased *))g_unix_output_stream_get_fd; |
||
168 | } |
||
169 | |||
170 | static void |
||
171 | g_unix_output_stream_set_property (GObject *object, |
||
172 | guint prop_id, |
||
173 | const GValue *value, |
||
174 | GParamSpec *pspec) |
||
175 | { |
||
176 | GUnixOutputStream *unix_stream; |
||
177 | |||
178 | unix_stream = G_UNIX_OUTPUT_STREAM (object); |
||
179 | |||
180 | switch (prop_id) |
||
181 | { |
||
182 | case PROP_FD: |
||
183 | unix_stream->priv->fd = g_value_get_int (value); |
||
184 | if (lseek (unix_stream->priv->fd, 0, SEEK_CUR) == -1 && errno == ESPIPE) |
||
185 | unix_stream->priv->is_pipe_or_socket = TRUE; |
||
186 | else |
||
187 | unix_stream->priv->is_pipe_or_socket = FALSE; |
||
188 | break; |
||
189 | case PROP_CLOSE_FD: |
||
190 | unix_stream->priv->close_fd = g_value_get_boolean (value); |
||
191 | break; |
||
192 | default: |
||
193 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
||
194 | break; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | static void |
||
199 | g_unix_output_stream_get_property (GObject *object, |
||
200 | guint prop_id, |
||
201 | GValue *value, |
||
202 | GParamSpec *pspec) |
||
203 | { |
||
204 | GUnixOutputStream *unix_stream; |
||
205 | |||
206 | unix_stream = G_UNIX_OUTPUT_STREAM (object); |
||
207 | |||
208 | switch (prop_id) |
||
209 | { |
||
210 | case PROP_FD: |
||
211 | g_value_set_int (value, unix_stream->priv->fd); |
||
212 | break; |
||
213 | case PROP_CLOSE_FD: |
||
214 | g_value_set_boolean (value, unix_stream->priv->close_fd); |
||
215 | break; |
||
216 | default: |
||
217 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); |
||
218 | } |
||
219 | } |
||
220 | |||
221 | static void |
||
222 | g_unix_output_stream_init (GUnixOutputStream *unix_stream) |
||
223 | { |
||
224 | unix_stream->priv = g_unix_output_stream_get_instance_private (unix_stream); |
||
225 | unix_stream->priv->fd = -1; |
||
226 | unix_stream->priv->close_fd = TRUE; |
||
227 | } |
||
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 | { |
||
245 | GUnixOutputStream *stream; |
||
246 | |||
247 | g_return_val_if_fail (fd != -1, NULL); |
||
248 | |||
249 | stream = g_object_new (G_TYPE_UNIX_OUTPUT_STREAM, |
||
250 | "fd", fd, |
||
251 | "close-fd", close_fd, |
||
252 | NULL); |
||
253 | |||
254 | return G_OUTPUT_STREAM (stream); |
||
255 | } |
||
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 | { |
||
271 | g_return_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream)); |
||
272 | |||
273 | close_fd = close_fd != FALSE; |
||
274 | if (stream->priv->close_fd != close_fd) |
||
275 | { |
||
276 | stream->priv->close_fd = close_fd; |
||
277 | g_object_notify (G_OBJECT (stream), "close-fd"); |
||
278 | } |
||
279 | } |
||
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 | { |
||
295 | g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), FALSE); |
||
296 | |||
297 | return stream->priv->close_fd; |
||
298 | } |
||
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 | { |
||
313 | g_return_val_if_fail (G_IS_UNIX_OUTPUT_STREAM (stream), -1); |
||
314 | |||
315 | return stream->priv->fd; |
||
316 | } |
||
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 | { |
||
325 | GUnixOutputStream *unix_stream; |
||
326 | gssize res = -1; |
||
327 | GPollFD poll_fds[2]; |
||
328 | int nfds; |
||
329 | int poll_ret; |
||
330 | |||
331 | unix_stream = G_UNIX_OUTPUT_STREAM (stream); |
||
332 | |||
333 | poll_fds[0].fd = unix_stream->priv->fd; |
||
334 | poll_fds[0].events = G_IO_OUT; |
||
335 | |||
336 | if (unix_stream->priv->is_pipe_or_socket && |
||
337 | g_cancellable_make_pollfd (cancellable, &poll_fds[1])) |
||
338 | nfds = 2; |
||
339 | else |
||
340 | nfds = 1; |
||
341 | |||
342 | while (1) |
||
343 | { |
||
344 | poll_fds[0].revents = poll_fds[1].revents = 0; |
||
345 | do |
||
346 | poll_ret = g_poll (poll_fds, nfds, -1); |
||
347 | while (poll_ret == -1 && errno == EINTR); |
||
348 | |||
349 | if (poll_ret == -1) |
||
350 | { |
||
351 | int errsv = errno; |
||
352 | |||
353 | g_set_error (error, G_IO_ERROR, |
||
354 | g_io_error_from_errno (errsv), |
||
355 | _("Error writing to file descriptor: %s"), |
||
356 | g_strerror (errsv)); |
||
357 | break; |
||
358 | } |
||
359 | |||
360 | if (g_cancellable_set_error_if_cancelled (cancellable, error)) |
||
361 | break; |
||
362 | |||
363 | if (!poll_fds[0].revents) |
||
364 | continue; |
||
365 | |||
366 | res = write (unix_stream->priv->fd, buffer, count); |
||
367 | if (res == -1) |
||
368 | { |
||
369 | int errsv = errno; |
||
370 | |||
371 | if (errsv == EINTR || errsv == EAGAIN) |
||
372 | continue; |
||
373 | |||
374 | g_set_error (error, G_IO_ERROR, |
||
375 | g_io_error_from_errno (errsv), |
||
376 | _("Error writing to file descriptor: %s"), |
||
377 | g_strerror (errsv)); |
||
378 | } |
||
379 | |||
380 | break; |
||
381 | } |
||
382 | |||
383 | if (nfds == 2) |
||
384 | g_cancellable_release_fd (cancellable); |
||
385 | return res; |
||
386 | } |
||
387 | |||
388 | static gboolean |
||
389 | g_unix_output_stream_close (GOutputStream *stream, |
||
390 | GCancellable *cancellable, |
||
391 | GError **error) |
||
392 | { |
||
393 | GUnixOutputStream *unix_stream; |
||
394 | int res; |
||
395 | |||
396 | unix_stream = G_UNIX_OUTPUT_STREAM (stream); |
||
397 | |||
398 | if (!unix_stream->priv->close_fd) |
||
399 | return TRUE; |
||
400 | |||
401 | /* This might block during the close. Doesn't seem to be a way to avoid it though. */ |
||
402 | res = close (unix_stream->priv->fd); |
||
403 | if (res == -1) |
||
404 | { |
||
405 | int errsv = errno; |
||
406 | |||
407 | g_set_error (error, G_IO_ERROR, |
||
408 | g_io_error_from_errno (errsv), |
||
409 | _("Error closing file descriptor: %s"), |
||
410 | g_strerror (errsv)); |
||
411 | } |
||
412 | |||
413 | return res != -1; |
||
414 | } |
||
415 | |||
416 | static void |
||
417 | g_unix_output_stream_close_async (GOutputStream *stream, |
||
418 | int io_priority, |
||
419 | GCancellable *cancellable, |
||
420 | GAsyncReadyCallback callback, |
||
421 | gpointer user_data) |
||
422 | { |
||
423 | GTask *task; |
||
424 | GError *error = NULL; |
||
425 | |||
426 | task = g_task_new (stream, cancellable, callback, user_data); |
||
427 | g_task_set_priority (task, io_priority); |
||
428 | |||
429 | if (g_unix_output_stream_close (stream, cancellable, &error)) |
||
430 | g_task_return_boolean (task, TRUE); |
||
431 | else |
||
432 | g_task_return_error (task, error); |
||
433 | g_object_unref (task); |
||
434 | } |
||
435 | |||
436 | static gboolean |
||
437 | g_unix_output_stream_close_finish (GOutputStream *stream, |
||
438 | GAsyncResult *result, |
||
439 | GError **error) |
||
440 | { |
||
441 | g_return_val_if_fail (g_task_is_valid (result, stream), FALSE); |
||
442 | |||
443 | return g_task_propagate_boolean (G_TASK (result), error); |
||
444 | } |
||
445 | |||
446 | static gboolean |
||
447 | g_unix_output_stream_pollable_can_poll (GPollableOutputStream *stream) |
||
448 | { |
||
449 | return G_UNIX_OUTPUT_STREAM (stream)->priv->is_pipe_or_socket; |
||
450 | } |
||
451 | |||
452 | static gboolean |
||
453 | g_unix_output_stream_pollable_is_writable (GPollableOutputStream *stream) |
||
454 | { |
||
455 | GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream); |
||
456 | GPollFD poll_fd; |
||
457 | gint result; |
||
458 | |||
459 | poll_fd.fd = unix_stream->priv->fd; |
||
460 | poll_fd.events = G_IO_OUT; |
||
461 | poll_fd.revents = 0; |
||
462 | |||
463 | do |
||
464 | result = g_poll (&poll_fd, 1, 0); |
||
465 | while (result == -1 && errno == EINTR); |
||
466 | |||
467 | return poll_fd.revents != 0; |
||
468 | } |
||
469 | |||
470 | static GSource * |
||
471 | g_unix_output_stream_pollable_create_source (GPollableOutputStream *stream, |
||
472 | GCancellable *cancellable) |
||
473 | { |
||
474 | GUnixOutputStream *unix_stream = G_UNIX_OUTPUT_STREAM (stream); |
||
475 | GSource *inner_source, *cancellable_source, *pollable_source; |
||
476 | |||
477 | pollable_source = g_pollable_source_new (G_OBJECT (stream)); |
||
478 | |||
479 | inner_source = g_unix_fd_source_new (unix_stream->priv->fd, G_IO_OUT); |
||
480 | g_source_set_dummy_callback (inner_source); |
||
481 | g_source_add_child_source (pollable_source, inner_source); |
||
482 | g_source_unref (inner_source); |
||
483 | |||
484 | if (cancellable) |
||
485 | { |
||
486 | cancellable_source = g_cancellable_source_new (cancellable); |
||
487 | g_source_set_dummy_callback (cancellable_source); |
||
488 | g_source_add_child_source (pollable_source, cancellable_source); |
||
489 | g_source_unref (cancellable_source); |
||
490 | } |
||
491 | |||
492 | return pollable_source; |
||
493 | } |