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/.
35 #include <sys/types.h>
47 typedef struct _GIOUnixChannel GIOUnixChannel;
48 typedef struct _GIOUnixWatch GIOUnixWatch;
50 struct _GIOUnixChannel
61 GIOCondition condition;
66 static GIOStatus g_io_unix_read (GIOChannel *channel,
71 static GIOStatus g_io_unix_write (GIOChannel *channel,
76 static GIOStatus g_io_unix_seek (GIOChannel *channel,
80 static GIOStatus g_io_unix_close (GIOChannel *channel,
82 static void g_io_unix_free (GIOChannel *channel);
83 static GSource* g_io_unix_create_watch (GIOChannel *channel,
84 GIOCondition condition);
85 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
88 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
90 static gboolean g_io_unix_prepare (GSource *source,
92 static gboolean g_io_unix_check (GSource *source);
93 static gboolean g_io_unix_dispatch (GSource *source,
96 static void g_io_unix_finalize (GSource *source);
98 GSourceFuncs unix_watch_funcs = {
105 GIOFuncs unix_channel_funcs = {
110 g_io_unix_create_watch,
117 g_io_unix_prepare (GSource *source,
120 GIOUnixWatch *watch = (GIOUnixWatch *)source;
121 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
125 /* Only return TRUE here if _all_ bits in watch->condition will be set
127 return ((watch->condition & buffer_condition) == watch->condition);
131 g_io_unix_check (GSource *source)
133 GIOUnixWatch *watch = (GIOUnixWatch *)source;
134 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
135 GIOCondition poll_condition = watch->pollfd.revents;
137 return ((poll_condition | buffer_condition) & watch->condition);
141 g_io_unix_dispatch (GSource *source,
142 GSourceFunc callback,
146 GIOFunc func = (GIOFunc)callback;
147 GIOUnixWatch *watch = (GIOUnixWatch *)source;
148 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
152 g_warning ("IO watch dispatched without callback\n"
153 "You must call g_source_connect().");
157 return (*func) (watch->channel,
158 (watch->pollfd.revents | buffer_condition) & watch->condition,
163 g_io_unix_finalize (GSource *source)
165 GIOUnixWatch *watch = (GIOUnixWatch *)source;
167 g_io_channel_unref (watch->channel);
171 g_io_unix_read (GIOChannel *channel,
177 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
180 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
184 result = read (unix_channel->fd, buf, count);
198 return G_IO_STATUS_AGAIN;
201 g_set_error (err, G_IO_CHANNEL_ERROR,
202 g_io_channel_error_from_errno (errno),
204 return G_IO_STATUS_ERROR;
208 *bytes_read = result;
210 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
214 g_io_unix_write (GIOChannel *channel,
217 gsize *bytes_written,
220 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
224 result = write (unix_channel->fd, buf, count);
238 return G_IO_STATUS_AGAIN;
241 g_set_error (err, G_IO_CHANNEL_ERROR,
242 g_io_channel_error_from_errno (errno),
244 return G_IO_STATUS_ERROR;
248 *bytes_written = result;
250 return G_IO_STATUS_NORMAL;
254 g_io_unix_seek (GIOChannel *channel,
259 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
275 whence = -1; /* Shut the compiler up */
276 g_assert_not_reached ();
279 result = lseek (unix_channel->fd, offset, whence);
283 g_set_error (err, G_IO_CHANNEL_ERROR,
284 g_io_channel_error_from_errno (errno),
286 return G_IO_STATUS_ERROR;
289 return G_IO_STATUS_NORMAL;
294 g_io_unix_close (GIOChannel *channel,
297 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
299 if (close (unix_channel->fd) < 0)
301 g_set_error (err, G_IO_CHANNEL_ERROR,
302 g_io_channel_error_from_errno (errno),
304 return G_IO_STATUS_ERROR;
307 return G_IO_STATUS_NORMAL;
311 g_io_unix_free (GIOChannel *channel)
313 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
315 g_free (unix_channel);
319 g_io_unix_create_watch (GIOChannel *channel,
320 GIOCondition condition)
322 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
327 source = g_source_new (&unix_watch_funcs, sizeof (GIOUnixWatch));
328 watch = (GIOUnixWatch *)source;
330 watch->channel = channel;
331 g_io_channel_ref (channel);
333 watch->condition = condition;
335 watch->pollfd.fd = unix_channel->fd;
336 watch->pollfd.events = condition;
338 g_source_add_poll (source, &watch->pollfd);
343 static const GIOFlags g_io_unix_fcntl_flags[] = {
347 static const glong g_io_unix_fcntl_posix_flags[] = {
355 #define G_IO_UNIX_NUM_FCNTL_FLAGS G_N_ELEMENTS (g_io_unix_fcntl_flags)
358 g_io_unix_set_flags (GIOChannel *channel,
364 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
368 for (loop = 0; loop < G_IO_UNIX_NUM_FCNTL_FLAGS; loop++)
369 if (flags & g_io_unix_fcntl_flags[loop])
370 fcntl_flags |= g_io_unix_fcntl_posix_flags[loop];
372 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
374 g_set_error (err, G_IO_CHANNEL_ERROR,
375 g_io_channel_error_from_errno (errno),
377 return G_IO_STATUS_ERROR;
380 return G_IO_STATUS_NORMAL;
384 g_io_unix_get_flags (GIOChannel *channel)
389 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
391 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
393 if (fcntl_flags == -1)
395 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
396 g_strerror (errno), errno);
400 for (loop = 0; loop < G_IO_UNIX_NUM_FCNTL_FLAGS; loop++)
401 if (fcntl_flags & g_io_unix_fcntl_posix_flags[loop])
402 flags |= g_io_unix_fcntl_flags[loop];
408 g_io_channel_new_file (const gchar *filename,
414 enum { /* Cheesy hack */
421 g_return_val_if_fail (filename != NULL, NULL);
422 g_return_val_if_fail (mode != NULL, NULL);
423 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
437 g_warning ("Invalid GIOFileMode %s.\n", mode);
448 mode_num |= MODE_PLUS;
453 g_warning ("Invalid GIOFileMode %s.\n", mode);
463 flags = O_WRONLY | O_TRUNC | O_CREAT;
466 flags = O_WRONLY | O_APPEND | O_CREAT;
468 case MODE_R | MODE_PLUS:
471 case MODE_W | MODE_PLUS:
472 flags = O_RDWR | O_TRUNC | O_CREAT;
474 case MODE_A | MODE_PLUS:
475 flags = O_RDWR | O_APPEND | O_CREAT;
478 g_assert_not_reached ();
482 fid = open (filename, flags);
485 g_set_error (error, G_FILE_ERROR,
486 g_file_error_from_errno (errno),
488 return (GIOChannel *)NULL;
491 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
493 channel->close_on_unref = TRUE;
494 channel->is_seekable = TRUE;
499 channel->is_readable = TRUE;
500 channel->is_writeable = FALSE;
504 channel->is_readable = FALSE;
505 channel->is_writeable = TRUE;
507 case MODE_R | MODE_PLUS:
508 case MODE_W | MODE_PLUS:
509 case MODE_A | MODE_PLUS:
510 channel->is_readable = TRUE;
511 channel->is_writeable = TRUE;
514 g_assert_not_reached ();
517 g_io_channel_init (channel);
518 channel->funcs = &unix_channel_funcs;
520 ((GIOUnixChannel *) channel)->fd = fid;
525 g_io_channel_unix_new (gint fd)
528 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
529 GIOChannel *channel = (GIOChannel *)unix_channel;
532 g_io_channel_init (channel);
533 channel->funcs = &unix_channel_funcs;
535 /* I'm not sure if fstat on a non-file (e.g., socket) works
536 * it should be safe to say if it fails, the fd isn't seekable.
538 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
539 * succeed in most cases.
541 if (fstat (unix_channel->fd, &buffer) == 0)
542 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
543 || S_ISBLK (buffer.st_mode);
544 else /* Assume not seekable */
545 channel->is_seekable = FALSE;
547 flags = fcntl (fd, F_GETFL);
551 /* Don't know if fcntl flags overlap, be careful */
553 if (flags & O_WRONLY)
555 channel->is_readable = FALSE;
556 channel->is_writeable = TRUE;
558 else if (flags & O_RDWR)
559 channel->is_readable = channel->is_writeable = TRUE;
561 else /* O_RDONLY defined as zero on linux (elsewhere?) */
563 channel->is_readable = TRUE;
564 channel->is_writeable = FALSE;
566 #else /* O_RDONLY == 0 */
567 else if (flags & O_RDONLY)
569 channel->is_readable = TRUE;
570 channel->is_writeable = FALSE;
573 channel->is_readable = channel->is_writeable = FALSE;
574 #endif /* O_RDONLY == 0 */
578 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
579 g_strerror (errno), errno);
584 unix_channel->fd = fd;
589 g_io_channel_unix_get_fd (GIOChannel *channel)
591 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
592 return unix_channel->fd;