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 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, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 #define _POSIX_SOURCE /* for SSIZE_MAX */
38 #include <sys/types.h>
45 #include <glib/gstdio.h>
47 #include "giochannel.h"
50 #include "gfileutils.h"
51 #include "gstrfuncs.h"
52 #include "gtestutils.h"
58 typedef struct _GIOUnixChannel GIOUnixChannel
;
59 typedef struct _GIOUnixWatch GIOUnixWatch
;
61 struct _GIOUnixChannel
72 GIOCondition condition
;
76 static GIOStatus
g_io_unix_read (GIOChannel
*channel
,
81 static GIOStatus
g_io_unix_write (GIOChannel
*channel
,
86 static GIOStatus
g_io_unix_seek (GIOChannel
*channel
,
90 static GIOStatus
g_io_unix_close (GIOChannel
*channel
,
92 static void g_io_unix_free (GIOChannel
*channel
);
93 static GSource
* g_io_unix_create_watch (GIOChannel
*channel
,
94 GIOCondition condition
);
95 static GIOStatus
g_io_unix_set_flags (GIOChannel
*channel
,
98 static GIOFlags
g_io_unix_get_flags (GIOChannel
*channel
);
100 static gboolean
g_io_unix_prepare (GSource
*source
,
102 static gboolean
g_io_unix_check (GSource
*source
);
103 static gboolean
g_io_unix_dispatch (GSource
*source
,
104 GSourceFunc callback
,
106 static void g_io_unix_finalize (GSource
*source
);
108 GSourceFuncs g_io_watch_funcs
= {
115 static GIOFuncs unix_channel_funcs
= {
120 g_io_unix_create_watch
,
127 g_io_unix_prepare (GSource
*source
,
130 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
131 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
135 /* Only return TRUE here if _all_ bits in watch->condition will be set
137 return ((watch
->condition
& buffer_condition
) == watch
->condition
);
141 g_io_unix_check (GSource
*source
)
143 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
144 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
145 GIOCondition poll_condition
= watch
->pollfd
.revents
;
147 return ((poll_condition
| buffer_condition
) & watch
->condition
);
151 g_io_unix_dispatch (GSource
*source
,
152 GSourceFunc callback
,
156 GIOFunc func
= (GIOFunc
)callback
;
157 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
158 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
162 g_warning ("IO watch dispatched without callback\n"
163 "You must call g_source_connect().");
167 return (*func
) (watch
->channel
,
168 (watch
->pollfd
.revents
| buffer_condition
) & watch
->condition
,
173 g_io_unix_finalize (GSource
*source
)
175 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
177 g_io_channel_unref (watch
->channel
);
181 g_io_unix_read (GIOChannel
*channel
,
187 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
190 if (count
> SSIZE_MAX
) /* At least according to the Debian manpage for read */
194 result
= read (unix_channel
->fd
, buf
, count
);
209 return G_IO_STATUS_AGAIN
;
212 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
213 g_io_channel_error_from_errno (errsv
),
215 return G_IO_STATUS_ERROR
;
219 *bytes_read
= result
;
221 return (result
> 0) ? G_IO_STATUS_NORMAL
: G_IO_STATUS_EOF
;
225 g_io_unix_write (GIOChannel
*channel
,
228 gsize
*bytes_written
,
231 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
235 result
= write (unix_channel
->fd
, buf
, count
);
250 return G_IO_STATUS_AGAIN
;
253 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
254 g_io_channel_error_from_errno (errsv
),
256 return G_IO_STATUS_ERROR
;
260 *bytes_written
= result
;
262 return G_IO_STATUS_NORMAL
;
266 g_io_unix_seek (GIOChannel
*channel
,
271 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
288 whence
= -1; /* Shut the compiler up */
289 g_assert_not_reached ();
293 if (tmp_offset
!= offset
)
295 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
296 g_io_channel_error_from_errno (EINVAL
),
297 g_strerror (EINVAL
));
298 return G_IO_STATUS_ERROR
;
301 result
= lseek (unix_channel
->fd
, tmp_offset
, whence
);
306 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
307 g_io_channel_error_from_errno (errsv
),
309 return G_IO_STATUS_ERROR
;
312 return G_IO_STATUS_NORMAL
;
317 g_io_unix_close (GIOChannel
*channel
,
320 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
322 if (close (unix_channel
->fd
) < 0)
325 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
326 g_io_channel_error_from_errno (errsv
),
328 return G_IO_STATUS_ERROR
;
331 return G_IO_STATUS_NORMAL
;
335 g_io_unix_free (GIOChannel
*channel
)
337 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
339 g_free (unix_channel
);
343 g_io_unix_create_watch (GIOChannel
*channel
,
344 GIOCondition condition
)
346 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
351 source
= g_source_new (&g_io_watch_funcs
, sizeof (GIOUnixWatch
));
352 g_source_set_name (source
, "GIOChannel (Unix)");
353 watch
= (GIOUnixWatch
*)source
;
355 watch
->channel
= channel
;
356 g_io_channel_ref (channel
);
358 watch
->condition
= condition
;
360 watch
->pollfd
.fd
= unix_channel
->fd
;
361 watch
->pollfd
.events
= condition
;
363 g_source_add_poll (source
, &watch
->pollfd
);
369 g_io_unix_set_flags (GIOChannel
*channel
,
374 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
378 if (flags
& G_IO_FLAG_APPEND
)
379 fcntl_flags
|= O_APPEND
;
380 if (flags
& G_IO_FLAG_NONBLOCK
)
382 fcntl_flags
|= O_NONBLOCK
;
384 fcntl_flags
|= O_NDELAY
;
387 if (fcntl (unix_channel
->fd
, F_SETFL
, fcntl_flags
) == -1)
390 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
391 g_io_channel_error_from_errno (errsv
),
393 return G_IO_STATUS_ERROR
;
396 return G_IO_STATUS_NORMAL
;
400 g_io_unix_get_flags (GIOChannel
*channel
)
404 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
406 fcntl_flags
= fcntl (unix_channel
->fd
, F_GETFL
);
408 if (fcntl_flags
== -1)
411 g_warning (G_STRLOC
"Error while getting flags for FD: %s (%d)\n",
412 g_strerror (err
), err
);
416 if (fcntl_flags
& O_APPEND
)
417 flags
|= G_IO_FLAG_APPEND
;
419 if (fcntl_flags
& O_NONBLOCK
)
421 if (fcntl_flags
& O_NDELAY
)
423 flags
|= G_IO_FLAG_NONBLOCK
;
425 switch (fcntl_flags
& (O_RDONLY
| O_WRONLY
| O_RDWR
))
428 channel
->is_readable
= TRUE
;
429 channel
->is_writeable
= FALSE
;
432 channel
->is_readable
= FALSE
;
433 channel
->is_writeable
= TRUE
;
436 channel
->is_readable
= TRUE
;
437 channel
->is_writeable
= TRUE
;
440 g_assert_not_reached ();
447 g_io_channel_new_file (const gchar
*filename
,
454 enum { /* Cheesy hack */
459 MODE_R_PLUS
= MODE_R
| MODE_PLUS
,
460 MODE_W_PLUS
= MODE_W
| MODE_PLUS
,
461 MODE_A_PLUS
= MODE_A
| MODE_PLUS
465 g_return_val_if_fail (filename
!= NULL
, NULL
);
466 g_return_val_if_fail (mode
!= NULL
, NULL
);
467 g_return_val_if_fail ((error
== NULL
) || (*error
== NULL
), NULL
);
481 g_warning ("Invalid GIOFileMode %s.\n", mode
);
492 mode_num
|= MODE_PLUS
;
497 g_warning ("Invalid GIOFileMode %s.\n", mode
);
507 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
510 flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
516 flags
= O_RDWR
| O_TRUNC
| O_CREAT
;
519 flags
= O_RDWR
| O_APPEND
| O_CREAT
;
523 g_assert_not_reached ();
527 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
529 fid
= g_open (filename
, flags
, create_mode
);
533 g_set_error_literal (error
, G_FILE_ERROR
,
534 g_file_error_from_errno (err
),
536 return (GIOChannel
*)NULL
;
539 if (fstat (fid
, &buffer
) == -1) /* In case someone opens a FIFO */
543 g_set_error_literal (error
, G_FILE_ERROR
,
544 g_file_error_from_errno (err
),
546 return (GIOChannel
*)NULL
;
549 channel
= (GIOChannel
*) g_new (GIOUnixChannel
, 1);
551 channel
->is_seekable
= S_ISREG (buffer
.st_mode
) || S_ISCHR (buffer
.st_mode
)
552 || S_ISBLK (buffer
.st_mode
);
557 channel
->is_readable
= TRUE
;
558 channel
->is_writeable
= FALSE
;
562 channel
->is_readable
= FALSE
;
563 channel
->is_writeable
= TRUE
;
568 channel
->is_readable
= TRUE
;
569 channel
->is_writeable
= TRUE
;
573 g_assert_not_reached ();
576 g_io_channel_init (channel
);
577 channel
->close_on_unref
= TRUE
; /* must be after g_io_channel_init () */
578 channel
->funcs
= &unix_channel_funcs
;
580 ((GIOUnixChannel
*) channel
)->fd
= fid
;
585 * g_io_channel_unix_new:
586 * @fd: a file descriptor.
588 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
589 * this works for plain files, pipes, and sockets.
591 * The returned #GIOChannel has a reference count of 1.
593 * The default encoding for #GIOChannel is UTF-8. If your application
594 * is reading output from a command using via pipe, you may need to set
595 * the encoding to the encoding of the current locale (see
596 * g_get_charset()) with the g_io_channel_set_encoding() function.
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
;