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>
46 #include "giochannel.h"
49 #include "gfileutils.h"
50 #include "gstrfuncs.h"
51 #include "gtestutils.h"
57 typedef struct _GIOUnixChannel GIOUnixChannel
;
58 typedef struct _GIOUnixWatch GIOUnixWatch
;
60 struct _GIOUnixChannel
71 GIOCondition condition
;
75 static GIOStatus
g_io_unix_read (GIOChannel
*channel
,
80 static GIOStatus
g_io_unix_write (GIOChannel
*channel
,
85 static GIOStatus
g_io_unix_seek (GIOChannel
*channel
,
89 static GIOStatus
g_io_unix_close (GIOChannel
*channel
,
91 static void g_io_unix_free (GIOChannel
*channel
);
92 static GSource
* g_io_unix_create_watch (GIOChannel
*channel
,
93 GIOCondition condition
);
94 static GIOStatus
g_io_unix_set_flags (GIOChannel
*channel
,
97 static GIOFlags
g_io_unix_get_flags (GIOChannel
*channel
);
99 static gboolean
g_io_unix_prepare (GSource
*source
,
101 static gboolean
g_io_unix_check (GSource
*source
);
102 static gboolean
g_io_unix_dispatch (GSource
*source
,
103 GSourceFunc callback
,
105 static void g_io_unix_finalize (GSource
*source
);
107 GSourceFuncs g_io_watch_funcs
= {
114 static GIOFuncs unix_channel_funcs
= {
119 g_io_unix_create_watch
,
126 g_io_unix_prepare (GSource
*source
,
129 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
130 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
134 /* Only return TRUE here if _all_ bits in watch->condition will be set
136 return ((watch
->condition
& buffer_condition
) == watch
->condition
);
140 g_io_unix_check (GSource
*source
)
142 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
143 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
144 GIOCondition poll_condition
= watch
->pollfd
.revents
;
146 return ((poll_condition
| buffer_condition
) & watch
->condition
);
150 g_io_unix_dispatch (GSource
*source
,
151 GSourceFunc callback
,
155 GIOFunc func
= (GIOFunc
)callback
;
156 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
157 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
161 g_warning ("IO watch dispatched without callback\n"
162 "You must call g_source_connect().");
166 return (*func
) (watch
->channel
,
167 (watch
->pollfd
.revents
| buffer_condition
) & watch
->condition
,
172 g_io_unix_finalize (GSource
*source
)
174 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
176 g_io_channel_unref (watch
->channel
);
180 g_io_unix_read (GIOChannel
*channel
,
186 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
189 if (count
> SSIZE_MAX
) /* At least according to the Debian manpage for read */
193 result
= read (unix_channel
->fd
, buf
, count
);
208 return G_IO_STATUS_AGAIN
;
211 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
212 g_io_channel_error_from_errno (errsv
),
214 return G_IO_STATUS_ERROR
;
218 *bytes_read
= result
;
220 return (result
> 0) ? G_IO_STATUS_NORMAL
: G_IO_STATUS_EOF
;
224 g_io_unix_write (GIOChannel
*channel
,
227 gsize
*bytes_written
,
230 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
234 result
= write (unix_channel
->fd
, buf
, count
);
249 return G_IO_STATUS_AGAIN
;
252 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
253 g_io_channel_error_from_errno (errsv
),
255 return G_IO_STATUS_ERROR
;
259 *bytes_written
= result
;
261 return G_IO_STATUS_NORMAL
;
265 g_io_unix_seek (GIOChannel
*channel
,
270 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
287 whence
= -1; /* Shut the compiler up */
288 g_assert_not_reached ();
292 if (tmp_offset
!= offset
)
294 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
295 g_io_channel_error_from_errno (EINVAL
),
296 g_strerror (EINVAL
));
297 return G_IO_STATUS_ERROR
;
300 result
= lseek (unix_channel
->fd
, tmp_offset
, whence
);
305 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
306 g_io_channel_error_from_errno (errsv
),
308 return G_IO_STATUS_ERROR
;
311 return G_IO_STATUS_NORMAL
;
316 g_io_unix_close (GIOChannel
*channel
,
319 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
321 if (close (unix_channel
->fd
) < 0)
324 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
325 g_io_channel_error_from_errno (errsv
),
327 return G_IO_STATUS_ERROR
;
330 return G_IO_STATUS_NORMAL
;
334 g_io_unix_free (GIOChannel
*channel
)
336 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
338 g_free (unix_channel
);
342 g_io_unix_create_watch (GIOChannel
*channel
,
343 GIOCondition condition
)
345 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
350 source
= g_source_new (&g_io_watch_funcs
, sizeof (GIOUnixWatch
));
351 g_source_set_name (source
, "GIOChannel (Unix)");
352 watch
= (GIOUnixWatch
*)source
;
354 watch
->channel
= channel
;
355 g_io_channel_ref (channel
);
357 watch
->condition
= condition
;
359 watch
->pollfd
.fd
= unix_channel
->fd
;
360 watch
->pollfd
.events
= condition
;
362 g_source_add_poll (source
, &watch
->pollfd
);
368 g_io_unix_set_flags (GIOChannel
*channel
,
373 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
377 if (flags
& G_IO_FLAG_APPEND
)
378 fcntl_flags
|= O_APPEND
;
379 if (flags
& G_IO_FLAG_NONBLOCK
)
381 fcntl_flags
|= O_NONBLOCK
;
383 fcntl_flags
|= O_NDELAY
;
386 if (fcntl (unix_channel
->fd
, F_SETFL
, fcntl_flags
) == -1)
389 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
390 g_io_channel_error_from_errno (errsv
),
392 return G_IO_STATUS_ERROR
;
395 return G_IO_STATUS_NORMAL
;
399 g_io_unix_get_flags (GIOChannel
*channel
)
403 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
405 fcntl_flags
= fcntl (unix_channel
->fd
, F_GETFL
);
407 if (fcntl_flags
== -1)
410 g_warning (G_STRLOC
"Error while getting flags for FD: %s (%d)\n",
411 g_strerror (err
), err
);
415 if (fcntl_flags
& O_APPEND
)
416 flags
|= G_IO_FLAG_APPEND
;
418 if (fcntl_flags
& O_NONBLOCK
)
420 if (fcntl_flags
& O_NDELAY
)
422 flags
|= G_IO_FLAG_NONBLOCK
;
424 switch (fcntl_flags
& (O_RDONLY
| O_WRONLY
| O_RDWR
))
427 channel
->is_readable
= TRUE
;
428 channel
->is_writeable
= FALSE
;
431 channel
->is_readable
= FALSE
;
432 channel
->is_writeable
= TRUE
;
435 channel
->is_readable
= TRUE
;
436 channel
->is_writeable
= TRUE
;
439 g_assert_not_reached ();
446 g_io_channel_new_file (const gchar
*filename
,
453 enum { /* Cheesy hack */
461 g_return_val_if_fail (filename
!= NULL
, NULL
);
462 g_return_val_if_fail (mode
!= NULL
, NULL
);
463 g_return_val_if_fail ((error
== NULL
) || (*error
== NULL
), NULL
);
477 g_warning ("Invalid GIOFileMode %s.\n", mode
);
488 mode_num
|= MODE_PLUS
;
493 g_warning ("Invalid GIOFileMode %s.\n", mode
);
503 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
506 flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
508 case MODE_R
| MODE_PLUS
:
511 case MODE_W
| MODE_PLUS
:
512 flags
= O_RDWR
| O_TRUNC
| O_CREAT
;
514 case MODE_A
| MODE_PLUS
:
515 flags
= O_RDWR
| O_APPEND
| O_CREAT
;
518 g_assert_not_reached ();
522 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
523 fid
= open (filename
, flags
, create_mode
);
527 g_set_error_literal (error
, G_FILE_ERROR
,
528 g_file_error_from_errno (err
),
530 return (GIOChannel
*)NULL
;
533 if (fstat (fid
, &buffer
) == -1) /* In case someone opens a FIFO */
537 g_set_error_literal (error
, G_FILE_ERROR
,
538 g_file_error_from_errno (err
),
540 return (GIOChannel
*)NULL
;
543 channel
= (GIOChannel
*) g_new (GIOUnixChannel
, 1);
545 channel
->is_seekable
= S_ISREG (buffer
.st_mode
) || S_ISCHR (buffer
.st_mode
)
546 || S_ISBLK (buffer
.st_mode
);
551 channel
->is_readable
= TRUE
;
552 channel
->is_writeable
= FALSE
;
556 channel
->is_readable
= FALSE
;
557 channel
->is_writeable
= TRUE
;
559 case MODE_R
| MODE_PLUS
:
560 case MODE_W
| MODE_PLUS
:
561 case MODE_A
| MODE_PLUS
:
562 channel
->is_readable
= TRUE
;
563 channel
->is_writeable
= TRUE
;
566 g_assert_not_reached ();
569 g_io_channel_init (channel
);
570 channel
->close_on_unref
= TRUE
; /* must be after g_io_channel_init () */
571 channel
->funcs
= &unix_channel_funcs
;
573 ((GIOUnixChannel
*) channel
)->fd
= fid
;
578 * g_io_channel_unix_new:
579 * @fd: a file descriptor.
580 * @Returns: a new #GIOChannel.
582 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
583 * this works for plain files, pipes, and sockets.
585 * The returned #GIOChannel has a reference count of 1.
587 * The default encoding for #GIOChannel is UTF-8. If your application
588 * is reading output from a command using via pipe, you may need to set
589 * the encoding to the encoding of the current locale (see
590 * g_get_charset()) with the g_io_channel_set_encoding() function.
592 * If you want to read raw binary data without interpretation, then
593 * call the g_io_channel_set_encoding() function with %NULL for the
596 * This function is available in GLib on Windows, too, but you should
597 * avoid using it on Windows. The domain of file descriptors and
598 * sockets overlap. There is no way for GLib to know which one you mean
599 * in case the argument you pass to this function happens to be both a
600 * valid file descriptor and socket. If that happens a warning is
601 * issued, and GLib assumes that it is the file descriptor you mean.
604 g_io_channel_unix_new (gint fd
)
607 GIOUnixChannel
*unix_channel
= g_new (GIOUnixChannel
, 1);
608 GIOChannel
*channel
= (GIOChannel
*)unix_channel
;
610 g_io_channel_init (channel
);
611 channel
->funcs
= &unix_channel_funcs
;
613 unix_channel
->fd
= fd
;
615 /* I'm not sure if fstat on a non-file (e.g., socket) works
616 * it should be safe to say if it fails, the fd isn't seekable.
618 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
619 * succeed in most cases.
621 if (fstat (unix_channel
->fd
, &buffer
) == 0)
622 channel
->is_seekable
= S_ISREG (buffer
.st_mode
) || S_ISCHR (buffer
.st_mode
)
623 || S_ISBLK (buffer
.st_mode
);
624 else /* Assume not seekable */
625 channel
->is_seekable
= FALSE
;
627 g_io_unix_get_flags (channel
); /* Sets is_readable, is_writeable */
633 * g_io_channel_unix_get_fd:
634 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
635 * @Returns: the file descriptor of the #GIOChannel.
637 * Returns the file descriptor of the #GIOChannel.
639 * On Windows this function returns the file descriptor or socket of
643 g_io_channel_unix_get_fd (GIOChannel
*channel
)
645 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
646 return unix_channel
->fd
;