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 "gunixinputstream.h"
35 #include "gcancellable.h"
36 #include "gasynchelper.h"
37 #include "gfiledescriptorbased.h"
42 * SECTION:gunixinputstream
43 * @short_description: Streaming input operations for UNIX file descriptors
44 * @include: gio/gunixinputstream.h
45 * @see_also: #GInputStream
47 * #GUnixInputStream implements #GInputStream for reading from 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.)
53 * Note that `<gio/gunixinputstream.h>` belongs to the UNIX-specific GIO
54 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
64 struct _GUnixInputStreamPrivate
{
67 guint is_pipe_or_socket
: 1;
70 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface
*iface
);
71 static void g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface
*iface
);
73 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream
, g_unix_input_stream
, G_TYPE_INPUT_STREAM
,
74 G_ADD_PRIVATE (GUnixInputStream
)
75 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM
,
76 g_unix_input_stream_pollable_iface_init
)
77 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED
,
78 g_unix_input_stream_file_descriptor_based_iface_init
)
81 static void g_unix_input_stream_set_property (GObject
*object
,
85 static void g_unix_input_stream_get_property (GObject
*object
,
89 static gssize
g_unix_input_stream_read (GInputStream
*stream
,
92 GCancellable
*cancellable
,
94 static gboolean
g_unix_input_stream_close (GInputStream
*stream
,
95 GCancellable
*cancellable
,
97 static void g_unix_input_stream_skip_async (GInputStream
*stream
,
100 GCancellable
*cancellable
,
101 GAsyncReadyCallback callback
,
103 static gssize
g_unix_input_stream_skip_finish (GInputStream
*stream
,
104 GAsyncResult
*result
,
106 static void g_unix_input_stream_close_async (GInputStream
*stream
,
108 GCancellable
*cancellable
,
109 GAsyncReadyCallback callback
,
111 static gboolean
g_unix_input_stream_close_finish (GInputStream
*stream
,
112 GAsyncResult
*result
,
115 static gboolean
g_unix_input_stream_pollable_can_poll (GPollableInputStream
*stream
);
116 static gboolean
g_unix_input_stream_pollable_is_readable (GPollableInputStream
*stream
);
117 static GSource
*g_unix_input_stream_pollable_create_source (GPollableInputStream
*stream
,
118 GCancellable
*cancellable
);
121 g_unix_input_stream_class_init (GUnixInputStreamClass
*klass
)
123 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
124 GInputStreamClass
*stream_class
= G_INPUT_STREAM_CLASS (klass
);
126 gobject_class
->get_property
= g_unix_input_stream_get_property
;
127 gobject_class
->set_property
= g_unix_input_stream_set_property
;
129 stream_class
->read_fn
= g_unix_input_stream_read
;
130 stream_class
->close_fn
= g_unix_input_stream_close
;
133 /* TODO: Implement instead of using fallbacks */
134 stream_class
->skip_async
= g_unix_input_stream_skip_async
;
135 stream_class
->skip_finish
= g_unix_input_stream_skip_finish
;
137 stream_class
->close_async
= g_unix_input_stream_close_async
;
138 stream_class
->close_finish
= g_unix_input_stream_close_finish
;
141 * GUnixInputStream:fd:
143 * The file descriptor that the stream reads from.
147 g_object_class_install_property (gobject_class
,
149 g_param_spec_int ("fd",
150 P_("File descriptor"),
151 P_("The file descriptor to read from"),
152 G_MININT
, G_MAXINT
, -1,
153 G_PARAM_READABLE
| G_PARAM_WRITABLE
| G_PARAM_CONSTRUCT_ONLY
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
156 * GUnixInputStream:close-fd:
158 * Whether to close the file descriptor when the stream is closed.
162 g_object_class_install_property (gobject_class
,
164 g_param_spec_boolean ("close-fd",
165 P_("Close file descriptor"),
166 P_("Whether to close the file descriptor when the stream is closed"),
168 G_PARAM_READABLE
| G_PARAM_WRITABLE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
172 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface
*iface
)
174 iface
->can_poll
= g_unix_input_stream_pollable_can_poll
;
175 iface
->is_readable
= g_unix_input_stream_pollable_is_readable
;
176 iface
->create_source
= g_unix_input_stream_pollable_create_source
;
180 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface
*iface
)
182 iface
->get_fd
= (int (*) (GFileDescriptorBased
*))g_unix_input_stream_get_fd
;
186 g_unix_input_stream_set_property (GObject
*object
,
191 GUnixInputStream
*unix_stream
;
193 unix_stream
= G_UNIX_INPUT_STREAM (object
);
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
;
202 unix_stream
->priv
->is_pipe_or_socket
= FALSE
;
205 unix_stream
->priv
->close_fd
= g_value_get_boolean (value
);
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
214 g_unix_input_stream_get_property (GObject
*object
,
219 GUnixInputStream
*unix_stream
;
221 unix_stream
= G_UNIX_INPUT_STREAM (object
);
226 g_value_set_int (value
, unix_stream
->priv
->fd
);
229 g_value_set_boolean (value
, unix_stream
->priv
->close_fd
);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
237 g_unix_input_stream_init (GUnixInputStream
*unix_stream
)
239 unix_stream
->priv
= g_unix_input_stream_get_instance_private (unix_stream
);
240 unix_stream
->priv
->fd
= -1;
241 unix_stream
->priv
->close_fd
= TRUE
;
245 * g_unix_input_stream_new:
246 * @fd: a UNIX file descriptor
247 * @close_fd: %TRUE to close the file descriptor when done
249 * Creates a new #GUnixInputStream for the given @fd.
251 * If @close_fd is %TRUE, the file descriptor will be closed
252 * when the stream is closed.
254 * Returns: a new #GUnixInputStream
257 g_unix_input_stream_new (gint fd
,
260 GUnixInputStream
*stream
;
262 g_return_val_if_fail (fd
!= -1, NULL
);
264 stream
= g_object_new (G_TYPE_UNIX_INPUT_STREAM
,
266 "close-fd", close_fd
,
269 return G_INPUT_STREAM (stream
);
273 * g_unix_input_stream_set_close_fd:
274 * @stream: a #GUnixInputStream
275 * @close_fd: %TRUE to close the file descriptor when done
277 * Sets whether the file descriptor of @stream shall be closed
278 * when the stream is closed.
283 g_unix_input_stream_set_close_fd (GUnixInputStream
*stream
,
286 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream
));
288 close_fd
= close_fd
!= FALSE
;
289 if (stream
->priv
->close_fd
!= close_fd
)
291 stream
->priv
->close_fd
= close_fd
;
292 g_object_notify (G_OBJECT (stream
), "close-fd");
297 * g_unix_input_stream_get_close_fd:
298 * @stream: a #GUnixInputStream
300 * Returns whether the file descriptor of @stream will be
301 * closed when the stream is closed.
303 * Returns: %TRUE if the file descriptor is closed when done
308 g_unix_input_stream_get_close_fd (GUnixInputStream
*stream
)
310 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream
), FALSE
);
312 return stream
->priv
->close_fd
;
316 * g_unix_input_stream_get_fd:
317 * @stream: a #GUnixInputStream
319 * Return the UNIX file descriptor that the stream reads from.
321 * Returns: The file descriptor of @stream
326 g_unix_input_stream_get_fd (GUnixInputStream
*stream
)
328 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream
), -1);
330 return stream
->priv
->fd
;
334 g_unix_input_stream_read (GInputStream
*stream
,
337 GCancellable
*cancellable
,
340 GUnixInputStream
*unix_stream
;
346 unix_stream
= G_UNIX_INPUT_STREAM (stream
);
348 poll_fds
[0].fd
= unix_stream
->priv
->fd
;
349 poll_fds
[0].events
= G_IO_IN
;
350 if (unix_stream
->priv
->is_pipe_or_socket
&&
351 g_cancellable_make_pollfd (cancellable
, &poll_fds
[1]))
358 poll_fds
[0].revents
= poll_fds
[1].revents
= 0;
360 poll_ret
= g_poll (poll_fds
, nfds
, -1);
361 while (poll_ret
== -1 && errno
== EINTR
);
367 g_set_error (error
, G_IO_ERROR
,
368 g_io_error_from_errno (errsv
),
369 _("Error reading from file descriptor: %s"),
374 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
377 if (!poll_fds
[0].revents
)
380 res
= read (unix_stream
->priv
->fd
, buffer
, count
);
385 if (errsv
== EINTR
|| errsv
== EAGAIN
)
388 g_set_error (error
, G_IO_ERROR
,
389 g_io_error_from_errno (errsv
),
390 _("Error reading from file descriptor: %s"),
398 g_cancellable_release_fd (cancellable
);
403 g_unix_input_stream_close (GInputStream
*stream
,
404 GCancellable
*cancellable
,
407 GUnixInputStream
*unix_stream
;
410 unix_stream
= G_UNIX_INPUT_STREAM (stream
);
412 if (!unix_stream
->priv
->close_fd
)
415 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
416 res
= close (unix_stream
->priv
->fd
);
421 g_set_error (error
, G_IO_ERROR
,
422 g_io_error_from_errno (errsv
),
423 _("Error closing file descriptor: %s"),
431 g_unix_input_stream_skip_async (GInputStream
*stream
,
434 GCancellable
*cancellable
,
435 GAsyncReadyCallback callback
,
438 g_warn_if_reached ();
439 /* TODO: Not implemented */
443 g_unix_input_stream_skip_finish (GInputStream
*stream
,
444 GAsyncResult
*result
,
447 g_warn_if_reached ();
449 /* TODO: Not implemented */
453 g_unix_input_stream_close_async (GInputStream
*stream
,
455 GCancellable
*cancellable
,
456 GAsyncReadyCallback callback
,
460 GError
*error
= NULL
;
462 task
= g_task_new (stream
, cancellable
, callback
, user_data
);
463 g_task_set_source_tag (task
, g_unix_input_stream_close_async
);
464 g_task_set_priority (task
, io_priority
);
466 if (g_unix_input_stream_close (stream
, cancellable
, &error
))
467 g_task_return_boolean (task
, TRUE
);
469 g_task_return_error (task
, error
);
470 g_object_unref (task
);
474 g_unix_input_stream_close_finish (GInputStream
*stream
,
475 GAsyncResult
*result
,
478 g_return_val_if_fail (g_task_is_valid (result
, stream
), FALSE
);
480 return g_task_propagate_boolean (G_TASK (result
), error
);
484 g_unix_input_stream_pollable_can_poll (GPollableInputStream
*stream
)
486 return G_UNIX_INPUT_STREAM (stream
)->priv
->is_pipe_or_socket
;
490 g_unix_input_stream_pollable_is_readable (GPollableInputStream
*stream
)
492 GUnixInputStream
*unix_stream
= G_UNIX_INPUT_STREAM (stream
);
496 poll_fd
.fd
= unix_stream
->priv
->fd
;
497 poll_fd
.events
= G_IO_IN
;
501 result
= g_poll (&poll_fd
, 1, 0);
502 while (result
== -1 && errno
== EINTR
);
504 return poll_fd
.revents
!= 0;
508 g_unix_input_stream_pollable_create_source (GPollableInputStream
*stream
,
509 GCancellable
*cancellable
)
511 GUnixInputStream
*unix_stream
= G_UNIX_INPUT_STREAM (stream
);
512 GSource
*inner_source
, *cancellable_source
, *pollable_source
;
514 pollable_source
= g_pollable_source_new (G_OBJECT (stream
));
516 inner_source
= g_unix_fd_source_new (unix_stream
->priv
->fd
, G_IO_IN
);
517 g_source_set_dummy_callback (inner_source
);
518 g_source_add_child_source (pollable_source
, inner_source
);
519 g_source_unref (inner_source
);
523 cancellable_source
= g_cancellable_source_new (cancellable
);
524 g_source_set_dummy_callback (cancellable_source
);
525 g_source_add_child_source (pollable_source
, cancellable_source
);
526 g_source_unref (cancellable_source
);
529 return pollable_source
;