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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Alexander Larsson <alexl@redhat.com>
25 #include <sys/types.h>
33 #include <glib/gstdio.h>
35 #include "gsimpleasyncresult.h"
36 #include "gunixinputstream.h"
37 #include "gcancellable.h"
38 #include "gasynchelper.h"
39 #include "gfiledescriptorbased.h"
44 * SECTION:gunixinputstream
45 * @short_description: Streaming input operations for UNIX file descriptors
46 * @include: gio/gunixinputstream.h
47 * @see_also: #GInputStream
49 * #GUnixInputStream implements #GInputStream for reading from 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 <filename><gio/gunixinputstream.h></filename> belongs
56 * to the UNIX-specific GIO interfaces, thus you have to use the
57 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
66 static void g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface
*iface
);
67 static void g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface
*iface
);
69 G_DEFINE_TYPE_WITH_CODE (GUnixInputStream
, g_unix_input_stream
, G_TYPE_INPUT_STREAM
,
70 G_IMPLEMENT_INTERFACE (G_TYPE_POLLABLE_INPUT_STREAM
,
71 g_unix_input_stream_pollable_iface_init
)
72 G_IMPLEMENT_INTERFACE (G_TYPE_FILE_DESCRIPTOR_BASED
,
73 g_unix_input_stream_file_descriptor_based_iface_init
)
76 struct _GUnixInputStreamPrivate
{
79 guint is_pipe_or_socket
: 1;
82 static void g_unix_input_stream_set_property (GObject
*object
,
86 static void g_unix_input_stream_get_property (GObject
*object
,
90 static gssize
g_unix_input_stream_read (GInputStream
*stream
,
93 GCancellable
*cancellable
,
95 static gboolean
g_unix_input_stream_close (GInputStream
*stream
,
96 GCancellable
*cancellable
,
98 static void g_unix_input_stream_skip_async (GInputStream
*stream
,
101 GCancellable
*cancellable
,
102 GAsyncReadyCallback callback
,
104 static gssize
g_unix_input_stream_skip_finish (GInputStream
*stream
,
105 GAsyncResult
*result
,
107 static void g_unix_input_stream_close_async (GInputStream
*stream
,
109 GCancellable
*cancellable
,
110 GAsyncReadyCallback callback
,
112 static gboolean
g_unix_input_stream_close_finish (GInputStream
*stream
,
113 GAsyncResult
*result
,
116 static gboolean
g_unix_input_stream_pollable_can_poll (GPollableInputStream
*stream
);
117 static gboolean
g_unix_input_stream_pollable_is_readable (GPollableInputStream
*stream
);
118 static GSource
*g_unix_input_stream_pollable_create_source (GPollableInputStream
*stream
,
119 GCancellable
*cancellable
);
122 g_unix_input_stream_finalize (GObject
*object
)
124 G_OBJECT_CLASS (g_unix_input_stream_parent_class
)->finalize (object
);
128 g_unix_input_stream_class_init (GUnixInputStreamClass
*klass
)
130 GObjectClass
*gobject_class
= G_OBJECT_CLASS (klass
);
131 GInputStreamClass
*stream_class
= G_INPUT_STREAM_CLASS (klass
);
133 g_type_class_add_private (klass
, sizeof (GUnixInputStreamPrivate
));
135 gobject_class
->get_property
= g_unix_input_stream_get_property
;
136 gobject_class
->set_property
= g_unix_input_stream_set_property
;
137 gobject_class
->finalize
= g_unix_input_stream_finalize
;
139 stream_class
->read_fn
= g_unix_input_stream_read
;
140 stream_class
->close_fn
= g_unix_input_stream_close
;
143 /* TODO: Implement instead of using fallbacks */
144 stream_class
->skip_async
= g_unix_input_stream_skip_async
;
145 stream_class
->skip_finish
= g_unix_input_stream_skip_finish
;
147 stream_class
->close_async
= g_unix_input_stream_close_async
;
148 stream_class
->close_finish
= g_unix_input_stream_close_finish
;
151 * GUnixInputStream:fd:
153 * The file descriptor that the stream reads from.
157 g_object_class_install_property (gobject_class
,
159 g_param_spec_int ("fd",
160 P_("File descriptor"),
161 P_("The file descriptor to read from"),
162 G_MININT
, G_MAXINT
, -1,
163 G_PARAM_READABLE
| G_PARAM_WRITABLE
| G_PARAM_CONSTRUCT_ONLY
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
166 * GUnixInputStream:close-fd:
168 * Whether to close the file descriptor when the stream is closed.
172 g_object_class_install_property (gobject_class
,
174 g_param_spec_boolean ("close-fd",
175 P_("Close file descriptor"),
176 P_("Whether to close the file descriptor when the stream is closed"),
178 G_PARAM_READABLE
| G_PARAM_WRITABLE
| G_PARAM_STATIC_NAME
| G_PARAM_STATIC_NICK
| G_PARAM_STATIC_BLURB
));
182 g_unix_input_stream_pollable_iface_init (GPollableInputStreamInterface
*iface
)
184 iface
->can_poll
= g_unix_input_stream_pollable_can_poll
;
185 iface
->is_readable
= g_unix_input_stream_pollable_is_readable
;
186 iface
->create_source
= g_unix_input_stream_pollable_create_source
;
190 g_unix_input_stream_file_descriptor_based_iface_init (GFileDescriptorBasedIface
*iface
)
192 iface
->get_fd
= (int (*) (GFileDescriptorBased
*))g_unix_input_stream_get_fd
;
196 g_unix_input_stream_set_property (GObject
*object
,
201 GUnixInputStream
*unix_stream
;
203 unix_stream
= G_UNIX_INPUT_STREAM (object
);
208 unix_stream
->priv
->fd
= g_value_get_int (value
);
209 if (lseek (unix_stream
->priv
->fd
, 0, SEEK_CUR
) == -1 && errno
== ESPIPE
)
210 unix_stream
->priv
->is_pipe_or_socket
= TRUE
;
212 unix_stream
->priv
->is_pipe_or_socket
= FALSE
;
215 unix_stream
->priv
->close_fd
= g_value_get_boolean (value
);
218 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
224 g_unix_input_stream_get_property (GObject
*object
,
229 GUnixInputStream
*unix_stream
;
231 unix_stream
= G_UNIX_INPUT_STREAM (object
);
236 g_value_set_int (value
, unix_stream
->priv
->fd
);
239 g_value_set_boolean (value
, unix_stream
->priv
->close_fd
);
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, prop_id
, pspec
);
247 g_unix_input_stream_init (GUnixInputStream
*unix_stream
)
249 unix_stream
->priv
= G_TYPE_INSTANCE_GET_PRIVATE (unix_stream
,
250 G_TYPE_UNIX_INPUT_STREAM
,
251 GUnixInputStreamPrivate
);
253 unix_stream
->priv
->fd
= -1;
254 unix_stream
->priv
->close_fd
= TRUE
;
258 * g_unix_input_stream_new:
259 * @fd: a UNIX file descriptor
260 * @close_fd: %TRUE to close the file descriptor when done
262 * Creates a new #GUnixInputStream for the given @fd.
264 * If @close_fd is %TRUE, the file descriptor will be closed
265 * when the stream is closed.
267 * Returns: a new #GUnixInputStream
270 g_unix_input_stream_new (gint fd
,
273 GUnixInputStream
*stream
;
275 g_return_val_if_fail (fd
!= -1, NULL
);
277 stream
= g_object_new (G_TYPE_UNIX_INPUT_STREAM
,
279 "close-fd", close_fd
,
282 return G_INPUT_STREAM (stream
);
286 * g_unix_input_stream_set_close_fd:
287 * @stream: a #GUnixInputStream
288 * @close_fd: %TRUE to close the file descriptor when done
290 * Sets whether the file descriptor of @stream shall be closed
291 * when the stream is closed.
296 g_unix_input_stream_set_close_fd (GUnixInputStream
*stream
,
299 g_return_if_fail (G_IS_UNIX_INPUT_STREAM (stream
));
301 close_fd
= close_fd
!= FALSE
;
302 if (stream
->priv
->close_fd
!= close_fd
)
304 stream
->priv
->close_fd
= close_fd
;
305 g_object_notify (G_OBJECT (stream
), "close-fd");
310 * g_unix_input_stream_get_close_fd:
311 * @stream: a #GUnixInputStream
313 * Returns whether the file descriptor of @stream will be
314 * closed when the stream is closed.
316 * Return value: %TRUE if the file descriptor is closed when done
321 g_unix_input_stream_get_close_fd (GUnixInputStream
*stream
)
323 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream
), FALSE
);
325 return stream
->priv
->close_fd
;
329 * g_unix_input_stream_get_fd:
330 * @stream: a #GUnixInputStream
332 * Return the UNIX file descriptor that the stream reads from.
334 * Return value: The file descriptor of @stream
339 g_unix_input_stream_get_fd (GUnixInputStream
*stream
)
341 g_return_val_if_fail (G_IS_UNIX_INPUT_STREAM (stream
), -1);
343 return stream
->priv
->fd
;
347 g_unix_input_stream_read (GInputStream
*stream
,
350 GCancellable
*cancellable
,
353 GUnixInputStream
*unix_stream
;
359 unix_stream
= G_UNIX_INPUT_STREAM (stream
);
361 poll_fds
[0].fd
= unix_stream
->priv
->fd
;
362 poll_fds
[0].events
= G_IO_IN
;
363 if (unix_stream
->priv
->is_pipe_or_socket
&&
364 g_cancellable_make_pollfd (cancellable
, &poll_fds
[1]))
371 poll_fds
[0].revents
= poll_fds
[1].revents
= 0;
373 poll_ret
= g_poll (poll_fds
, nfds
, -1);
374 while (poll_ret
== -1 && errno
== EINTR
);
380 g_set_error (error
, G_IO_ERROR
,
381 g_io_error_from_errno (errsv
),
382 _("Error reading from file descriptor: %s"),
387 if (g_cancellable_set_error_if_cancelled (cancellable
, error
))
390 if (!poll_fds
[0].revents
)
393 res
= read (unix_stream
->priv
->fd
, buffer
, count
);
398 if (errsv
== EINTR
|| errsv
== EAGAIN
)
401 g_set_error (error
, G_IO_ERROR
,
402 g_io_error_from_errno (errsv
),
403 _("Error reading from file descriptor: %s"),
411 g_cancellable_release_fd (cancellable
);
416 g_unix_input_stream_close (GInputStream
*stream
,
417 GCancellable
*cancellable
,
420 GUnixInputStream
*unix_stream
;
423 unix_stream
= G_UNIX_INPUT_STREAM (stream
);
425 if (!unix_stream
->priv
->close_fd
)
428 /* This might block during the close. Doesn't seem to be a way to avoid it though. */
429 res
= close (unix_stream
->priv
->fd
);
434 g_set_error (error
, G_IO_ERROR
,
435 g_io_error_from_errno (errsv
),
436 _("Error closing file descriptor: %s"),
444 g_unix_input_stream_skip_async (GInputStream
*stream
,
447 GCancellable
*cancellable
,
448 GAsyncReadyCallback callback
,
451 g_warn_if_reached ();
452 /* TODO: Not implemented */
456 g_unix_input_stream_skip_finish (GInputStream
*stream
,
457 GAsyncResult
*result
,
460 g_warn_if_reached ();
462 /* TODO: Not implemented */
466 g_unix_input_stream_close_async (GInputStream
*stream
,
468 GCancellable
*cancellable
,
469 GAsyncReadyCallback callback
,
473 GError
*error
= NULL
;
475 task
= g_task_new (stream
, cancellable
, callback
, user_data
);
476 g_task_set_priority (task
, io_priority
);
478 if (g_unix_input_stream_close (stream
, cancellable
, &error
))
479 g_task_return_boolean (task
, TRUE
);
481 g_task_return_error (task
, error
);
482 g_object_unref (task
);
486 g_unix_input_stream_close_finish (GInputStream
*stream
,
487 GAsyncResult
*result
,
490 g_return_val_if_fail (g_task_is_valid (result
, stream
), FALSE
);
492 return g_task_propagate_boolean (G_TASK (result
), error
);
496 g_unix_input_stream_pollable_can_poll (GPollableInputStream
*stream
)
498 return G_UNIX_INPUT_STREAM (stream
)->priv
->is_pipe_or_socket
;
502 g_unix_input_stream_pollable_is_readable (GPollableInputStream
*stream
)
504 GUnixInputStream
*unix_stream
= G_UNIX_INPUT_STREAM (stream
);
508 poll_fd
.fd
= unix_stream
->priv
->fd
;
509 poll_fd
.events
= G_IO_IN
;
513 result
= g_poll (&poll_fd
, 1, 0);
514 while (result
== -1 && errno
== EINTR
);
516 return poll_fd
.revents
!= 0;
520 g_unix_input_stream_pollable_create_source (GPollableInputStream
*stream
,
521 GCancellable
*cancellable
)
523 GUnixInputStream
*unix_stream
= G_UNIX_INPUT_STREAM (stream
);
524 GSource
*inner_source
, *pollable_source
;
526 pollable_source
= g_pollable_source_new (G_OBJECT (stream
));
528 inner_source
= _g_fd_source_new (unix_stream
->priv
->fd
, G_IO_IN
, cancellable
);
529 g_source_set_dummy_callback (inner_source
);
530 g_source_add_child_source (pollable_source
, inner_source
);
531 g_source_unref (inner_source
);
533 return pollable_source
;