1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giounix.c: IO Channels using unix file descriptors
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GLib Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GLib at ftp://ftp.gtk.org/pub/gtk/.
34 #define _POSIX_SOURCE /* for SSIZE_MAX */
36 #include <sys/types.h>
43 #include <glib/gstdio.h>
45 #include "giochannel.h"
48 #include "gfileutils.h"
49 #include "gstrfuncs.h"
50 #include "gtestutils.h"
56 typedef struct _GIOUnixChannel GIOUnixChannel
;
57 typedef struct _GIOUnixWatch GIOUnixWatch
;
59 struct _GIOUnixChannel
70 GIOCondition condition
;
74 static GIOStatus
g_io_unix_read (GIOChannel
*channel
,
79 static GIOStatus
g_io_unix_write (GIOChannel
*channel
,
84 static GIOStatus
g_io_unix_seek (GIOChannel
*channel
,
88 static GIOStatus
g_io_unix_close (GIOChannel
*channel
,
90 static void g_io_unix_free (GIOChannel
*channel
);
91 static GSource
* g_io_unix_create_watch (GIOChannel
*channel
,
92 GIOCondition condition
);
93 static GIOStatus
g_io_unix_set_flags (GIOChannel
*channel
,
96 static GIOFlags
g_io_unix_get_flags (GIOChannel
*channel
);
98 static gboolean
g_io_unix_prepare (GSource
*source
,
100 static gboolean
g_io_unix_check (GSource
*source
);
101 static gboolean
g_io_unix_dispatch (GSource
*source
,
102 GSourceFunc callback
,
104 static void g_io_unix_finalize (GSource
*source
);
106 GSourceFuncs g_io_watch_funcs
= {
113 static GIOFuncs unix_channel_funcs
= {
118 g_io_unix_create_watch
,
125 g_io_unix_prepare (GSource
*source
,
128 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
129 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
133 /* Only return TRUE here if _all_ bits in watch->condition will be set
135 return ((watch
->condition
& buffer_condition
) == watch
->condition
);
139 g_io_unix_check (GSource
*source
)
141 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
142 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
143 GIOCondition poll_condition
= watch
->pollfd
.revents
;
145 return ((poll_condition
| buffer_condition
) & watch
->condition
);
149 g_io_unix_dispatch (GSource
*source
,
150 GSourceFunc callback
,
154 GIOFunc func
= (GIOFunc
)callback
;
155 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
156 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
160 g_warning ("IO watch dispatched without callback. "
161 "You must call g_source_connect().");
165 return (*func
) (watch
->channel
,
166 (watch
->pollfd
.revents
| buffer_condition
) & watch
->condition
,
171 g_io_unix_finalize (GSource
*source
)
173 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
175 g_io_channel_unref (watch
->channel
);
179 g_io_unix_read (GIOChannel
*channel
,
185 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
188 if (count
> SSIZE_MAX
) /* At least according to the Debian manpage for read */
192 result
= read (unix_channel
->fd
, buf
, count
);
207 return G_IO_STATUS_AGAIN
;
210 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
211 g_io_channel_error_from_errno (errsv
),
213 return G_IO_STATUS_ERROR
;
217 *bytes_read
= result
;
219 return (result
> 0) ? G_IO_STATUS_NORMAL
: G_IO_STATUS_EOF
;
223 g_io_unix_write (GIOChannel
*channel
,
226 gsize
*bytes_written
,
229 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
233 result
= write (unix_channel
->fd
, buf
, count
);
248 return G_IO_STATUS_AGAIN
;
251 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
252 g_io_channel_error_from_errno (errsv
),
254 return G_IO_STATUS_ERROR
;
258 *bytes_written
= result
;
260 return G_IO_STATUS_NORMAL
;
264 g_io_unix_seek (GIOChannel
*channel
,
269 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
286 whence
= -1; /* Shut the compiler up */
287 g_assert_not_reached ();
291 if (tmp_offset
!= offset
)
293 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
294 g_io_channel_error_from_errno (EINVAL
),
295 g_strerror (EINVAL
));
296 return G_IO_STATUS_ERROR
;
299 result
= lseek (unix_channel
->fd
, tmp_offset
, whence
);
304 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
305 g_io_channel_error_from_errno (errsv
),
307 return G_IO_STATUS_ERROR
;
310 return G_IO_STATUS_NORMAL
;
315 g_io_unix_close (GIOChannel
*channel
,
318 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
320 if (close (unix_channel
->fd
) < 0)
323 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
324 g_io_channel_error_from_errno (errsv
),
326 return G_IO_STATUS_ERROR
;
329 return G_IO_STATUS_NORMAL
;
333 g_io_unix_free (GIOChannel
*channel
)
335 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
337 g_free (unix_channel
);
341 g_io_unix_create_watch (GIOChannel
*channel
,
342 GIOCondition condition
)
344 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
349 source
= g_source_new (&g_io_watch_funcs
, sizeof (GIOUnixWatch
));
350 g_source_set_name (source
, "GIOChannel (Unix)");
351 watch
= (GIOUnixWatch
*)source
;
353 watch
->channel
= channel
;
354 g_io_channel_ref (channel
);
356 watch
->condition
= condition
;
358 watch
->pollfd
.fd
= unix_channel
->fd
;
359 watch
->pollfd
.events
= condition
;
361 g_source_add_poll (source
, &watch
->pollfd
);
367 g_io_unix_set_flags (GIOChannel
*channel
,
372 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
376 if (flags
& G_IO_FLAG_APPEND
)
377 fcntl_flags
|= O_APPEND
;
378 if (flags
& G_IO_FLAG_NONBLOCK
)
380 fcntl_flags
|= O_NONBLOCK
;
382 fcntl_flags
|= O_NDELAY
;
385 if (fcntl (unix_channel
->fd
, F_SETFL
, fcntl_flags
) == -1)
388 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
389 g_io_channel_error_from_errno (errsv
),
391 return G_IO_STATUS_ERROR
;
394 return G_IO_STATUS_NORMAL
;
398 g_io_unix_get_flags (GIOChannel
*channel
)
402 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
404 fcntl_flags
= fcntl (unix_channel
->fd
, F_GETFL
);
406 if (fcntl_flags
== -1)
409 g_warning (G_STRLOC
"Error while getting flags for FD: %s (%d)",
410 g_strerror (err
), err
);
414 if (fcntl_flags
& O_APPEND
)
415 flags
|= G_IO_FLAG_APPEND
;
417 if (fcntl_flags
& O_NONBLOCK
)
419 if (fcntl_flags
& O_NDELAY
)
421 flags
|= G_IO_FLAG_NONBLOCK
;
423 switch (fcntl_flags
& (O_RDONLY
| O_WRONLY
| O_RDWR
))
426 channel
->is_readable
= TRUE
;
427 channel
->is_writeable
= FALSE
;
430 channel
->is_readable
= FALSE
;
431 channel
->is_writeable
= TRUE
;
434 channel
->is_readable
= TRUE
;
435 channel
->is_writeable
= TRUE
;
438 g_assert_not_reached ();
445 g_io_channel_new_file (const gchar
*filename
,
452 enum { /* Cheesy hack */
457 MODE_R_PLUS
= MODE_R
| MODE_PLUS
,
458 MODE_W_PLUS
= MODE_W
| MODE_PLUS
,
459 MODE_A_PLUS
= MODE_A
| MODE_PLUS
463 g_return_val_if_fail (filename
!= NULL
, NULL
);
464 g_return_val_if_fail (mode
!= NULL
, NULL
);
465 g_return_val_if_fail ((error
== NULL
) || (*error
== NULL
), NULL
);
479 g_warning ("Invalid GIOFileMode %s.", mode
);
490 mode_num
|= MODE_PLUS
;
495 g_warning ("Invalid GIOFileMode %s.", mode
);
505 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
508 flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
514 flags
= O_RDWR
| O_TRUNC
| O_CREAT
;
517 flags
= O_RDWR
| O_APPEND
| O_CREAT
;
521 g_assert_not_reached ();
525 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
527 fid
= g_open (filename
, flags
, create_mode
);
531 g_set_error_literal (error
, G_FILE_ERROR
,
532 g_file_error_from_errno (err
),
534 return (GIOChannel
*)NULL
;
537 if (fstat (fid
, &buffer
) == -1) /* In case someone opens a FIFO */
541 g_set_error_literal (error
, G_FILE_ERROR
,
542 g_file_error_from_errno (err
),
544 return (GIOChannel
*)NULL
;
547 channel
= (GIOChannel
*) g_new (GIOUnixChannel
, 1);
549 channel
->is_seekable
= S_ISREG (buffer
.st_mode
) || S_ISCHR (buffer
.st_mode
)
550 || S_ISBLK (buffer
.st_mode
);
555 channel
->is_readable
= TRUE
;
556 channel
->is_writeable
= FALSE
;
560 channel
->is_readable
= FALSE
;
561 channel
->is_writeable
= TRUE
;
566 channel
->is_readable
= TRUE
;
567 channel
->is_writeable
= TRUE
;
571 g_assert_not_reached ();
574 g_io_channel_init (channel
);
575 channel
->close_on_unref
= TRUE
; /* must be after g_io_channel_init () */
576 channel
->funcs
= &unix_channel_funcs
;
578 ((GIOUnixChannel
*) channel
)->fd
= fid
;
583 * g_io_channel_unix_new:
584 * @fd: a file descriptor.
586 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
587 * this works for plain files, pipes, and sockets.
589 * The returned #GIOChannel has a reference count of 1.
591 * The default encoding for #GIOChannel is UTF-8. If your application
592 * is reading output from a command using via pipe, you may need to set
593 * the encoding to the encoding of the current locale (see
594 * g_get_charset()) with the g_io_channel_set_encoding() function.
595 * By default, the fd passed will not be closed when the final reference
596 * to the #GIOChannel data structure is dropped.
598 * If you want to read raw binary data without interpretation, then
599 * call the g_io_channel_set_encoding() function with %NULL for the
602 * This function is available in GLib on Windows, too, but you should
603 * avoid using it on Windows. The domain of file descriptors and
604 * sockets overlap. There is no way for GLib to know which one you mean
605 * in case the argument you pass to this function happens to be both a
606 * valid file descriptor and socket. If that happens a warning is
607 * issued, and GLib assumes that it is the file descriptor you mean.
609 * Returns: a new #GIOChannel.
612 g_io_channel_unix_new (gint fd
)
615 GIOUnixChannel
*unix_channel
= g_new (GIOUnixChannel
, 1);
616 GIOChannel
*channel
= (GIOChannel
*)unix_channel
;
618 g_io_channel_init (channel
);
619 channel
->funcs
= &unix_channel_funcs
;
621 unix_channel
->fd
= fd
;
623 /* I'm not sure if fstat on a non-file (e.g., socket) works
624 * it should be safe to say if it fails, the fd isn't seekable.
626 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
627 * succeed in most cases.
629 if (fstat (unix_channel
->fd
, &buffer
) == 0)
630 channel
->is_seekable
= S_ISREG (buffer
.st_mode
) || S_ISCHR (buffer
.st_mode
)
631 || S_ISBLK (buffer
.st_mode
);
632 else /* Assume not seekable */
633 channel
->is_seekable
= FALSE
;
635 g_io_unix_get_flags (channel
); /* Sets is_readable, is_writeable */
641 * g_io_channel_unix_get_fd:
642 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
644 * Returns the file descriptor of the #GIOChannel.
646 * On Windows this function returns the file descriptor or socket of
649 * Returns: the file descriptor of the #GIOChannel.
652 g_io_channel_unix_get_fd (GIOChannel
*channel
)
654 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
655 return unix_channel
->fd
;