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>
53 typedef struct _GIOUnixChannel GIOUnixChannel
;
54 typedef struct _GIOUnixWatch GIOUnixWatch
;
56 struct _GIOUnixChannel
67 GIOCondition condition
;
71 static GIOStatus
g_io_unix_read (GIOChannel
*channel
,
76 static GIOStatus
g_io_unix_write (GIOChannel
*channel
,
81 static GIOStatus
g_io_unix_seek (GIOChannel
*channel
,
85 static GIOStatus
g_io_unix_close (GIOChannel
*channel
,
87 static void g_io_unix_free (GIOChannel
*channel
);
88 static GSource
* g_io_unix_create_watch (GIOChannel
*channel
,
89 GIOCondition condition
);
90 static GIOStatus
g_io_unix_set_flags (GIOChannel
*channel
,
93 static GIOFlags
g_io_unix_get_flags (GIOChannel
*channel
);
95 static gboolean
g_io_unix_prepare (GSource
*source
,
97 static gboolean
g_io_unix_check (GSource
*source
);
98 static gboolean
g_io_unix_dispatch (GSource
*source
,
101 static void g_io_unix_finalize (GSource
*source
);
103 GSourceFuncs g_io_watch_funcs
= {
110 static GIOFuncs unix_channel_funcs
= {
115 g_io_unix_create_watch
,
122 g_io_unix_prepare (GSource
*source
,
125 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
126 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
130 /* Only return TRUE here if _all_ bits in watch->condition will be set
132 return ((watch
->condition
& buffer_condition
) == watch
->condition
);
136 g_io_unix_check (GSource
*source
)
138 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
139 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
140 GIOCondition poll_condition
= watch
->pollfd
.revents
;
142 return ((poll_condition
| buffer_condition
) & watch
->condition
);
146 g_io_unix_dispatch (GSource
*source
,
147 GSourceFunc callback
,
151 GIOFunc func
= (GIOFunc
)callback
;
152 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
153 GIOCondition buffer_condition
= g_io_channel_get_buffer_condition (watch
->channel
);
157 g_warning ("IO watch dispatched without callback\n"
158 "You must call g_source_connect().");
162 return (*func
) (watch
->channel
,
163 (watch
->pollfd
.revents
| buffer_condition
) & watch
->condition
,
168 g_io_unix_finalize (GSource
*source
)
170 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
172 g_io_channel_unref (watch
->channel
);
176 g_io_unix_read (GIOChannel
*channel
,
182 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
185 if (count
> SSIZE_MAX
) /* At least according to the Debian manpage for read */
189 result
= read (unix_channel
->fd
, buf
, count
);
204 return G_IO_STATUS_AGAIN
;
207 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
208 g_io_channel_error_from_errno (errsv
),
210 return G_IO_STATUS_ERROR
;
214 *bytes_read
= result
;
216 return (result
> 0) ? G_IO_STATUS_NORMAL
: G_IO_STATUS_EOF
;
220 g_io_unix_write (GIOChannel
*channel
,
223 gsize
*bytes_written
,
226 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
230 result
= write (unix_channel
->fd
, buf
, count
);
245 return G_IO_STATUS_AGAIN
;
248 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
249 g_io_channel_error_from_errno (errsv
),
251 return G_IO_STATUS_ERROR
;
255 *bytes_written
= result
;
257 return G_IO_STATUS_NORMAL
;
261 g_io_unix_seek (GIOChannel
*channel
,
266 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
283 whence
= -1; /* Shut the compiler up */
284 g_assert_not_reached ();
288 if (tmp_offset
!= offset
)
290 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
291 g_io_channel_error_from_errno (EINVAL
),
292 g_strerror (EINVAL
));
293 return G_IO_STATUS_ERROR
;
296 result
= lseek (unix_channel
->fd
, tmp_offset
, whence
);
301 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
302 g_io_channel_error_from_errno (errsv
),
304 return G_IO_STATUS_ERROR
;
307 return G_IO_STATUS_NORMAL
;
312 g_io_unix_close (GIOChannel
*channel
,
315 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
317 if (close (unix_channel
->fd
) < 0)
320 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
321 g_io_channel_error_from_errno (errsv
),
323 return G_IO_STATUS_ERROR
;
326 return G_IO_STATUS_NORMAL
;
330 g_io_unix_free (GIOChannel
*channel
)
332 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
334 g_free (unix_channel
);
338 g_io_unix_create_watch (GIOChannel
*channel
,
339 GIOCondition condition
)
341 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
346 source
= g_source_new (&g_io_watch_funcs
, sizeof (GIOUnixWatch
));
347 watch
= (GIOUnixWatch
*)source
;
349 watch
->channel
= channel
;
350 g_io_channel_ref (channel
);
352 watch
->condition
= condition
;
354 watch
->pollfd
.fd
= unix_channel
->fd
;
355 watch
->pollfd
.events
= condition
;
357 g_source_add_poll (source
, &watch
->pollfd
);
363 g_io_unix_set_flags (GIOChannel
*channel
,
368 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
372 if (flags
& G_IO_FLAG_APPEND
)
373 fcntl_flags
|= O_APPEND
;
374 if (flags
& G_IO_FLAG_NONBLOCK
)
376 fcntl_flags
|= O_NONBLOCK
;
378 fcntl_flags
|= O_NDELAY
;
381 if (fcntl (unix_channel
->fd
, F_SETFL
, fcntl_flags
) == -1)
384 g_set_error_literal (err
, G_IO_CHANNEL_ERROR
,
385 g_io_channel_error_from_errno (errsv
),
387 return G_IO_STATUS_ERROR
;
390 return G_IO_STATUS_NORMAL
;
394 g_io_unix_get_flags (GIOChannel
*channel
)
398 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*) channel
;
400 fcntl_flags
= fcntl (unix_channel
->fd
, F_GETFL
);
402 if (fcntl_flags
== -1)
405 g_warning (G_STRLOC
"Error while getting flags for FD: %s (%d)\n",
406 g_strerror (err
), err
);
410 if (fcntl_flags
& O_APPEND
)
411 flags
|= G_IO_FLAG_APPEND
;
413 if (fcntl_flags
& O_NONBLOCK
)
415 if (fcntl_flags
& O_NDELAY
)
417 flags
|= G_IO_FLAG_NONBLOCK
;
419 switch (fcntl_flags
& (O_RDONLY
| O_WRONLY
| O_RDWR
))
422 channel
->is_readable
= TRUE
;
423 channel
->is_writeable
= FALSE
;
426 channel
->is_readable
= FALSE
;
427 channel
->is_writeable
= TRUE
;
430 channel
->is_readable
= TRUE
;
431 channel
->is_writeable
= TRUE
;
434 g_assert_not_reached ();
441 g_io_channel_new_file (const gchar
*filename
,
448 enum { /* Cheesy hack */
456 g_return_val_if_fail (filename
!= NULL
, NULL
);
457 g_return_val_if_fail (mode
!= NULL
, NULL
);
458 g_return_val_if_fail ((error
== NULL
) || (*error
== NULL
), NULL
);
472 g_warning ("Invalid GIOFileMode %s.\n", mode
);
483 mode_num
|= MODE_PLUS
;
488 g_warning ("Invalid GIOFileMode %s.\n", mode
);
498 flags
= O_WRONLY
| O_TRUNC
| O_CREAT
;
501 flags
= O_WRONLY
| O_APPEND
| O_CREAT
;
503 case MODE_R
| MODE_PLUS
:
506 case MODE_W
| MODE_PLUS
:
507 flags
= O_RDWR
| O_TRUNC
| O_CREAT
;
509 case MODE_A
| MODE_PLUS
:
510 flags
= O_RDWR
| O_APPEND
| O_CREAT
;
513 g_assert_not_reached ();
517 create_mode
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
518 fid
= open (filename
, flags
, create_mode
);
522 g_set_error_literal (error
, G_FILE_ERROR
,
523 g_file_error_from_errno (err
),
525 return (GIOChannel
*)NULL
;
528 if (fstat (fid
, &buffer
) == -1) /* In case someone opens a FIFO */
532 g_set_error_literal (error
, G_FILE_ERROR
,
533 g_file_error_from_errno (err
),
535 return (GIOChannel
*)NULL
;
538 channel
= (GIOChannel
*) g_new (GIOUnixChannel
, 1);
540 channel
->is_seekable
= S_ISREG (buffer
.st_mode
) || S_ISCHR (buffer
.st_mode
)
541 || S_ISBLK (buffer
.st_mode
);
546 channel
->is_readable
= TRUE
;
547 channel
->is_writeable
= FALSE
;
551 channel
->is_readable
= FALSE
;
552 channel
->is_writeable
= TRUE
;
554 case MODE_R
| MODE_PLUS
:
555 case MODE_W
| MODE_PLUS
:
556 case MODE_A
| MODE_PLUS
:
557 channel
->is_readable
= TRUE
;
558 channel
->is_writeable
= TRUE
;
561 g_assert_not_reached ();
564 g_io_channel_init (channel
);
565 channel
->close_on_unref
= TRUE
; /* must be after g_io_channel_init () */
566 channel
->funcs
= &unix_channel_funcs
;
568 ((GIOUnixChannel
*) channel
)->fd
= fid
;
573 * g_io_channel_unix_new:
574 * @fd: a file descriptor.
575 * @Returns: a new #GIOChannel.
577 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
578 * this works for plain files, pipes, and sockets.
580 * The returned #GIOChannel has a reference count of 1.
582 * The default encoding for #GIOChannel is UTF-8. If your application
583 * is reading output from a command using via pipe, you may need to set
584 * the encoding to the encoding of the current locale (see
585 * g_get_charset()) with the g_io_channel_set_encoding() function.
587 * If you want to read raw binary data without interpretation, then
588 * call the g_io_channel_set_encoding() function with %NULL for the
591 * This function is available in GLib on Windows, too, but you should
592 * avoid using it on Windows. The domain of file descriptors and
593 * sockets overlap. There is no way for GLib to know which one you mean
594 * in case the argument you pass to this function happens to be both a
595 * valid file descriptor and socket. If that happens a warning is
596 * issued, and GLib assumes that it is the file descriptor you mean.
599 g_io_channel_unix_new (gint fd
)
602 GIOUnixChannel
*unix_channel
= g_new (GIOUnixChannel
, 1);
603 GIOChannel
*channel
= (GIOChannel
*)unix_channel
;
605 g_io_channel_init (channel
);
606 channel
->funcs
= &unix_channel_funcs
;
608 unix_channel
->fd
= fd
;
610 /* I'm not sure if fstat on a non-file (e.g., socket) works
611 * it should be safe to say if it fails, the fd isn't seekable.
613 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
614 * succeed in most cases.
616 if (fstat (unix_channel
->fd
, &buffer
) == 0)
617 channel
->is_seekable
= S_ISREG (buffer
.st_mode
) || S_ISCHR (buffer
.st_mode
)
618 || S_ISBLK (buffer
.st_mode
);
619 else /* Assume not seekable */
620 channel
->is_seekable
= FALSE
;
622 g_io_unix_get_flags (channel
); /* Sets is_readable, is_writeable */
628 * g_io_channel_unix_get_fd:
629 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
630 * @Returns: the file descriptor of the #GIOChannel.
632 * Returns the file descriptor of the #GIOChannel.
634 * On Windows this function returns the file descriptor or socket of
638 g_io_channel_unix_get_fd (GIOChannel
*channel
)
640 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
641 return unix_channel
->fd
;
644 #define __G_IO_UNIX_C__
645 #include "galiasdef.c"