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 g_io_watch_funcs = {
105 static 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 (&g_io_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);
344 g_io_unix_set_flags (GIOChannel *channel,
349 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
353 if (flags & G_IO_FLAG_APPEND)
354 fcntl_flags |= O_APPEND;
355 if (flags & G_IO_FLAG_NONBLOCK)
357 fcntl_flags |= O_NONBLOCK;
359 fcntl_flags |= O_NDELAY;
362 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
364 g_set_error (err, G_IO_CHANNEL_ERROR,
365 g_io_channel_error_from_errno (errno),
367 return G_IO_STATUS_ERROR;
370 return G_IO_STATUS_NORMAL;
374 g_io_unix_get_flags (GIOChannel *channel)
378 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
380 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
382 if (fcntl_flags == -1)
384 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
385 g_strerror (errno), errno);
389 if (fcntl_flags & O_APPEND)
390 flags |= G_IO_FLAG_APPEND;
392 if (fcntl_flags & O_NONBLOCK)
394 if (fcntl_flags & O_NDELAY)
396 flags |= G_IO_FLAG_NONBLOCK;
398 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
401 channel->is_readable = TRUE;
402 channel->is_writeable = FALSE;
405 channel->is_readable = FALSE;
406 channel->is_writeable = TRUE;
409 channel->is_readable = TRUE;
410 channel->is_writeable = TRUE;
413 g_assert_not_reached ();
420 g_io_channel_new_file (const gchar *filename,
427 enum { /* Cheesy hack */
435 g_return_val_if_fail (filename != NULL, NULL);
436 g_return_val_if_fail (mode != NULL, NULL);
437 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
451 g_warning ("Invalid GIOFileMode %s.\n", mode);
462 mode_num |= MODE_PLUS;
467 g_warning ("Invalid GIOFileMode %s.\n", mode);
477 flags = O_WRONLY | O_TRUNC | O_CREAT;
480 flags = O_WRONLY | O_APPEND | O_CREAT;
482 case MODE_R | MODE_PLUS:
485 case MODE_W | MODE_PLUS:
486 flags = O_RDWR | O_TRUNC | O_CREAT;
488 case MODE_A | MODE_PLUS:
489 flags = O_RDWR | O_APPEND | O_CREAT;
492 g_assert_not_reached ();
496 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
497 fid = open (filename, flags, create_mode);
500 g_set_error (error, G_FILE_ERROR,
501 g_file_error_from_errno (errno),
503 return (GIOChannel *)NULL;
506 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
509 g_set_error (error, G_FILE_ERROR,
510 g_file_error_from_errno (errno),
512 return (GIOChannel *)NULL;
515 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
517 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
518 || S_ISBLK (buffer.st_mode);
523 channel->is_readable = TRUE;
524 channel->is_writeable = FALSE;
528 channel->is_readable = FALSE;
529 channel->is_writeable = TRUE;
531 case MODE_R | MODE_PLUS:
532 case MODE_W | MODE_PLUS:
533 case MODE_A | MODE_PLUS:
534 channel->is_readable = TRUE;
535 channel->is_writeable = TRUE;
538 g_assert_not_reached ();
541 g_io_channel_init (channel);
542 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
543 channel->funcs = &unix_channel_funcs;
545 ((GIOUnixChannel *) channel)->fd = fid;
550 g_io_channel_unix_new (gint fd)
553 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
554 GIOChannel *channel = (GIOChannel *)unix_channel;
556 g_io_channel_init (channel);
557 channel->funcs = &unix_channel_funcs;
559 unix_channel->fd = fd;
561 /* I'm not sure if fstat on a non-file (e.g., socket) works
562 * it should be safe to say if it fails, the fd isn't seekable.
564 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
565 * succeed in most cases.
567 if (fstat (unix_channel->fd, &buffer) == 0)
568 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
569 || S_ISBLK (buffer.st_mode);
570 else /* Assume not seekable */
571 channel->is_seekable = FALSE;
573 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
579 g_io_channel_unix_get_fd (GIOChannel *channel)
581 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
582 return unix_channel->fd;