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 */
458 MODE_R_PLUS = MODE_R | MODE_PLUS,
459 MODE_W_PLUS = MODE_W | MODE_PLUS,
460 MODE_A_PLUS = MODE_A | MODE_PLUS
464 g_return_val_if_fail (filename != NULL, NULL);
465 g_return_val_if_fail (mode != NULL, NULL);
466 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
480 g_warning ("Invalid GIOFileMode %s.\n", mode);
491 mode_num |= MODE_PLUS;
496 g_warning ("Invalid GIOFileMode %s.\n", mode);
506 flags = O_WRONLY | O_TRUNC | O_CREAT;
509 flags = O_WRONLY | O_APPEND | O_CREAT;
515 flags = O_RDWR | O_TRUNC | O_CREAT;
518 flags = O_RDWR | O_APPEND | O_CREAT;
522 g_assert_not_reached ();
526 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
527 fid = 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.
585 * @Returns: a new #GIOChannel.
587 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
588 * this works for plain files, pipes, and sockets.
590 * The returned #GIOChannel has a reference count of 1.
592 * The default encoding for #GIOChannel is UTF-8. If your application
593 * is reading output from a command using via pipe, you may need to set
594 * the encoding to the encoding of the current locale (see
595 * g_get_charset()) with the g_io_channel_set_encoding() function.
597 * If you want to read raw binary data without interpretation, then
598 * call the g_io_channel_set_encoding() function with %NULL for the
601 * This function is available in GLib on Windows, too, but you should
602 * avoid using it on Windows. The domain of file descriptors and
603 * sockets overlap. There is no way for GLib to know which one you mean
604 * in case the argument you pass to this function happens to be both a
605 * valid file descriptor and socket. If that happens a warning is
606 * issued, and GLib assumes that it is the file descriptor you mean.
609 g_io_channel_unix_new (gint fd)
612 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
613 GIOChannel *channel = (GIOChannel *)unix_channel;
615 g_io_channel_init (channel);
616 channel->funcs = &unix_channel_funcs;
618 unix_channel->fd = fd;
620 /* I'm not sure if fstat on a non-file (e.g., socket) works
621 * it should be safe to say if it fails, the fd isn't seekable.
623 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
624 * succeed in most cases.
626 if (fstat (unix_channel->fd, &buffer) == 0)
627 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
628 || S_ISBLK (buffer.st_mode);
629 else /* Assume not seekable */
630 channel->is_seekable = FALSE;
632 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
638 * g_io_channel_unix_get_fd:
639 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
640 * @Returns: the file descriptor of the #GIOChannel.
642 * Returns the file descriptor of the #GIOChannel.
644 * On Windows this function returns the file descriptor or socket of
648 g_io_channel_unix_get_fd (GIOChannel *channel)
650 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
651 return unix_channel->fd;