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 #include <sys/types.h>
50 typedef struct _GIOUnixChannel GIOUnixChannel;
51 typedef struct _GIOUnixWatch GIOUnixWatch;
53 struct _GIOUnixChannel
64 GIOCondition condition;
68 static GIOStatus g_io_unix_read (GIOChannel *channel,
73 static GIOStatus g_io_unix_write (GIOChannel *channel,
78 static GIOStatus g_io_unix_seek (GIOChannel *channel,
82 static GIOStatus g_io_unix_close (GIOChannel *channel,
84 static void g_io_unix_free (GIOChannel *channel);
85 static GSource* g_io_unix_create_watch (GIOChannel *channel,
86 GIOCondition condition);
87 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
90 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
92 static gboolean g_io_unix_prepare (GSource *source,
94 static gboolean g_io_unix_check (GSource *source);
95 static gboolean g_io_unix_dispatch (GSource *source,
98 static void g_io_unix_finalize (GSource *source);
100 GSourceFuncs g_io_watch_funcs = {
107 static GIOFuncs unix_channel_funcs = {
112 g_io_unix_create_watch,
119 g_io_unix_prepare (GSource *source,
122 GIOUnixWatch *watch = (GIOUnixWatch *)source;
123 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
127 /* Only return TRUE here if _all_ bits in watch->condition will be set
129 return ((watch->condition & buffer_condition) == watch->condition);
133 g_io_unix_check (GSource *source)
135 GIOUnixWatch *watch = (GIOUnixWatch *)source;
136 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
137 GIOCondition poll_condition = watch->pollfd.revents;
139 return ((poll_condition | buffer_condition) & watch->condition);
143 g_io_unix_dispatch (GSource *source,
144 GSourceFunc callback,
148 GIOFunc func = (GIOFunc)callback;
149 GIOUnixWatch *watch = (GIOUnixWatch *)source;
150 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
154 g_warning ("IO watch dispatched without callback\n"
155 "You must call g_source_connect().");
159 return (*func) (watch->channel,
160 (watch->pollfd.revents | buffer_condition) & watch->condition,
165 g_io_unix_finalize (GSource *source)
167 GIOUnixWatch *watch = (GIOUnixWatch *)source;
169 g_io_channel_unref (watch->channel);
173 g_io_unix_read (GIOChannel *channel,
179 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
182 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
186 result = read (unix_channel->fd, buf, count);
200 return G_IO_STATUS_AGAIN;
203 g_set_error (err, G_IO_CHANNEL_ERROR,
204 g_io_channel_error_from_errno (errno),
206 return G_IO_STATUS_ERROR;
210 *bytes_read = result;
212 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
216 g_io_unix_write (GIOChannel *channel,
219 gsize *bytes_written,
222 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
226 result = write (unix_channel->fd, buf, count);
240 return G_IO_STATUS_AGAIN;
243 g_set_error (err, G_IO_CHANNEL_ERROR,
244 g_io_channel_error_from_errno (errno),
246 return G_IO_STATUS_ERROR;
250 *bytes_written = result;
252 return G_IO_STATUS_NORMAL;
256 g_io_unix_seek (GIOChannel *channel,
261 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
278 whence = -1; /* Shut the compiler up */
279 g_assert_not_reached ();
283 if (tmp_offset != offset)
285 g_set_error (err, G_IO_CHANNEL_ERROR,
286 g_io_channel_error_from_errno (EINVAL),
287 g_strerror (EINVAL));
288 return G_IO_STATUS_ERROR;
291 result = lseek (unix_channel->fd, tmp_offset, whence);
295 g_set_error (err, G_IO_CHANNEL_ERROR,
296 g_io_channel_error_from_errno (errno),
298 return G_IO_STATUS_ERROR;
301 return G_IO_STATUS_NORMAL;
306 g_io_unix_close (GIOChannel *channel,
309 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
311 if (close (unix_channel->fd) < 0)
313 g_set_error (err, G_IO_CHANNEL_ERROR,
314 g_io_channel_error_from_errno (errno),
316 return G_IO_STATUS_ERROR;
319 return G_IO_STATUS_NORMAL;
323 g_io_unix_free (GIOChannel *channel)
325 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
327 g_free (unix_channel);
331 g_io_unix_create_watch (GIOChannel *channel,
332 GIOCondition condition)
334 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
339 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
340 watch = (GIOUnixWatch *)source;
342 watch->channel = channel;
343 g_io_channel_ref (channel);
345 watch->condition = condition;
347 watch->pollfd.fd = unix_channel->fd;
348 watch->pollfd.events = condition;
350 g_source_add_poll (source, &watch->pollfd);
356 g_io_unix_set_flags (GIOChannel *channel,
361 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
365 if (flags & G_IO_FLAG_APPEND)
366 fcntl_flags |= O_APPEND;
367 if (flags & G_IO_FLAG_NONBLOCK)
369 fcntl_flags |= O_NONBLOCK;
371 fcntl_flags |= O_NDELAY;
374 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
376 g_set_error (err, G_IO_CHANNEL_ERROR,
377 g_io_channel_error_from_errno (errno),
379 return G_IO_STATUS_ERROR;
382 return G_IO_STATUS_NORMAL;
386 g_io_unix_get_flags (GIOChannel *channel)
390 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
392 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
394 if (fcntl_flags == -1)
396 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
397 g_strerror (errno), errno);
401 if (fcntl_flags & O_APPEND)
402 flags |= G_IO_FLAG_APPEND;
404 if (fcntl_flags & O_NONBLOCK)
406 if (fcntl_flags & O_NDELAY)
408 flags |= G_IO_FLAG_NONBLOCK;
410 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
413 channel->is_readable = TRUE;
414 channel->is_writeable = FALSE;
417 channel->is_readable = FALSE;
418 channel->is_writeable = TRUE;
421 channel->is_readable = TRUE;
422 channel->is_writeable = TRUE;
425 g_assert_not_reached ();
432 g_io_channel_new_file (const gchar *filename,
439 enum { /* Cheesy hack */
447 g_return_val_if_fail (filename != NULL, NULL);
448 g_return_val_if_fail (mode != NULL, NULL);
449 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
463 g_warning ("Invalid GIOFileMode %s.\n", mode);
474 mode_num |= MODE_PLUS;
479 g_warning ("Invalid GIOFileMode %s.\n", mode);
489 flags = O_WRONLY | O_TRUNC | O_CREAT;
492 flags = O_WRONLY | O_APPEND | O_CREAT;
494 case MODE_R | MODE_PLUS:
497 case MODE_W | MODE_PLUS:
498 flags = O_RDWR | O_TRUNC | O_CREAT;
500 case MODE_A | MODE_PLUS:
501 flags = O_RDWR | O_APPEND | O_CREAT;
504 g_assert_not_reached ();
508 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
509 fid = open (filename, flags, create_mode);
512 g_set_error (error, G_FILE_ERROR,
513 g_file_error_from_errno (errno),
515 return (GIOChannel *)NULL;
518 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
521 g_set_error (error, G_FILE_ERROR,
522 g_file_error_from_errno (errno),
524 return (GIOChannel *)NULL;
527 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
529 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
530 || S_ISBLK (buffer.st_mode);
535 channel->is_readable = TRUE;
536 channel->is_writeable = FALSE;
540 channel->is_readable = FALSE;
541 channel->is_writeable = TRUE;
543 case MODE_R | MODE_PLUS:
544 case MODE_W | MODE_PLUS:
545 case MODE_A | MODE_PLUS:
546 channel->is_readable = TRUE;
547 channel->is_writeable = TRUE;
550 g_assert_not_reached ();
553 g_io_channel_init (channel);
554 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
555 channel->funcs = &unix_channel_funcs;
557 ((GIOUnixChannel *) channel)->fd = fid;
562 g_io_channel_unix_new (gint fd)
565 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
566 GIOChannel *channel = (GIOChannel *)unix_channel;
568 g_io_channel_init (channel);
569 channel->funcs = &unix_channel_funcs;
571 unix_channel->fd = fd;
573 /* I'm not sure if fstat on a non-file (e.g., socket) works
574 * it should be safe to say if it fails, the fd isn't seekable.
576 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
577 * succeed in most cases.
579 if (fstat (unix_channel->fd, &buffer) == 0)
580 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
581 || S_ISBLK (buffer.st_mode);
582 else /* Assume not seekable */
583 channel->is_seekable = FALSE;
585 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
591 g_io_channel_unix_get_fd (GIOChannel *channel)
593 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
594 return unix_channel->fd;