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/.
37 #include <sys/types.h>
49 typedef struct _GIOUnixChannel GIOUnixChannel;
50 typedef struct _GIOUnixWatch GIOUnixWatch;
52 struct _GIOUnixChannel
63 GIOCondition condition;
67 static GIOStatus g_io_unix_read (GIOChannel *channel,
72 static GIOStatus g_io_unix_write (GIOChannel *channel,
77 static GIOStatus g_io_unix_seek (GIOChannel *channel,
81 static GIOStatus g_io_unix_close (GIOChannel *channel,
83 static void g_io_unix_free (GIOChannel *channel);
84 static GSource* g_io_unix_create_watch (GIOChannel *channel,
85 GIOCondition condition);
86 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
89 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
91 static gboolean g_io_unix_prepare (GSource *source,
93 static gboolean g_io_unix_check (GSource *source);
94 static gboolean g_io_unix_dispatch (GSource *source,
97 static void g_io_unix_finalize (GSource *source);
99 GSourceFuncs g_io_watch_funcs = {
106 static GIOFuncs unix_channel_funcs = {
111 g_io_unix_create_watch,
118 g_io_unix_prepare (GSource *source,
121 GIOUnixWatch *watch = (GIOUnixWatch *)source;
122 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
126 /* Only return TRUE here if _all_ bits in watch->condition will be set
128 return ((watch->condition & buffer_condition) == watch->condition);
132 g_io_unix_check (GSource *source)
134 GIOUnixWatch *watch = (GIOUnixWatch *)source;
135 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
136 GIOCondition poll_condition = watch->pollfd.revents;
138 return ((poll_condition | buffer_condition) & watch->condition);
142 g_io_unix_dispatch (GSource *source,
143 GSourceFunc callback,
147 GIOFunc func = (GIOFunc)callback;
148 GIOUnixWatch *watch = (GIOUnixWatch *)source;
149 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
153 g_warning ("IO watch dispatched without callback\n"
154 "You must call g_source_connect().");
158 return (*func) (watch->channel,
159 (watch->pollfd.revents | buffer_condition) & watch->condition,
164 g_io_unix_finalize (GSource *source)
166 GIOUnixWatch *watch = (GIOUnixWatch *)source;
168 g_io_channel_unref (watch->channel);
172 g_io_unix_read (GIOChannel *channel,
178 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
181 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
185 result = read (unix_channel->fd, buf, count);
199 return G_IO_STATUS_AGAIN;
202 g_set_error (err, G_IO_CHANNEL_ERROR,
203 g_io_channel_error_from_errno (errno),
205 return G_IO_STATUS_ERROR;
209 *bytes_read = result;
211 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
215 g_io_unix_write (GIOChannel *channel,
218 gsize *bytes_written,
221 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
225 result = write (unix_channel->fd, buf, count);
239 return G_IO_STATUS_AGAIN;
242 g_set_error (err, G_IO_CHANNEL_ERROR,
243 g_io_channel_error_from_errno (errno),
245 return G_IO_STATUS_ERROR;
249 *bytes_written = result;
251 return G_IO_STATUS_NORMAL;
255 g_io_unix_seek (GIOChannel *channel,
260 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
277 whence = -1; /* Shut the compiler up */
278 g_assert_not_reached ();
282 if (tmp_offset != offset)
284 g_set_error (err, G_IO_CHANNEL_ERROR,
285 g_io_channel_error_from_errno (EINVAL),
286 g_strerror (EINVAL));
287 return G_IO_STATUS_ERROR;
290 result = lseek (unix_channel->fd, tmp_offset, whence);
294 g_set_error (err, G_IO_CHANNEL_ERROR,
295 g_io_channel_error_from_errno (errno),
297 return G_IO_STATUS_ERROR;
300 return G_IO_STATUS_NORMAL;
305 g_io_unix_close (GIOChannel *channel,
308 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
310 if (close (unix_channel->fd) < 0)
312 g_set_error (err, G_IO_CHANNEL_ERROR,
313 g_io_channel_error_from_errno (errno),
315 return G_IO_STATUS_ERROR;
318 return G_IO_STATUS_NORMAL;
322 g_io_unix_free (GIOChannel *channel)
324 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
326 g_free (unix_channel);
330 g_io_unix_create_watch (GIOChannel *channel,
331 GIOCondition condition)
333 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
338 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
339 watch = (GIOUnixWatch *)source;
341 watch->channel = channel;
342 g_io_channel_ref (channel);
344 watch->condition = condition;
346 watch->pollfd.fd = unix_channel->fd;
347 watch->pollfd.events = condition;
349 g_source_add_poll (source, &watch->pollfd);
355 g_io_unix_set_flags (GIOChannel *channel,
360 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
364 if (flags & G_IO_FLAG_APPEND)
365 fcntl_flags |= O_APPEND;
366 if (flags & G_IO_FLAG_NONBLOCK)
368 fcntl_flags |= O_NONBLOCK;
370 fcntl_flags |= O_NDELAY;
373 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
375 g_set_error (err, G_IO_CHANNEL_ERROR,
376 g_io_channel_error_from_errno (errno),
378 return G_IO_STATUS_ERROR;
381 return G_IO_STATUS_NORMAL;
385 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 if (fcntl_flags & O_APPEND)
401 flags |= G_IO_FLAG_APPEND;
403 if (fcntl_flags & O_NONBLOCK)
405 if (fcntl_flags & O_NDELAY)
407 flags |= G_IO_FLAG_NONBLOCK;
409 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
412 channel->is_readable = TRUE;
413 channel->is_writeable = FALSE;
416 channel->is_readable = FALSE;
417 channel->is_writeable = TRUE;
420 channel->is_readable = TRUE;
421 channel->is_writeable = TRUE;
424 g_assert_not_reached ();
431 g_io_channel_new_file (const gchar *filename,
438 enum { /* Cheesy hack */
446 g_return_val_if_fail (filename != NULL, NULL);
447 g_return_val_if_fail (mode != NULL, NULL);
448 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
462 g_warning ("Invalid GIOFileMode %s.\n", mode);
473 mode_num |= MODE_PLUS;
478 g_warning ("Invalid GIOFileMode %s.\n", mode);
488 flags = O_WRONLY | O_TRUNC | O_CREAT;
491 flags = O_WRONLY | O_APPEND | O_CREAT;
493 case MODE_R | MODE_PLUS:
496 case MODE_W | MODE_PLUS:
497 flags = O_RDWR | O_TRUNC | O_CREAT;
499 case MODE_A | MODE_PLUS:
500 flags = O_RDWR | O_APPEND | O_CREAT;
503 g_assert_not_reached ();
507 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
508 fid = open (filename, flags, create_mode);
511 g_set_error (error, G_FILE_ERROR,
512 g_file_error_from_errno (errno),
514 return (GIOChannel *)NULL;
517 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
520 g_set_error (error, G_FILE_ERROR,
521 g_file_error_from_errno (errno),
523 return (GIOChannel *)NULL;
526 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
528 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
529 || S_ISBLK (buffer.st_mode);
534 channel->is_readable = TRUE;
535 channel->is_writeable = FALSE;
539 channel->is_readable = FALSE;
540 channel->is_writeable = TRUE;
542 case MODE_R | MODE_PLUS:
543 case MODE_W | MODE_PLUS:
544 case MODE_A | MODE_PLUS:
545 channel->is_readable = TRUE;
546 channel->is_writeable = TRUE;
549 g_assert_not_reached ();
552 g_io_channel_init (channel);
553 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
554 channel->funcs = &unix_channel_funcs;
556 ((GIOUnixChannel *) channel)->fd = fid;
561 g_io_channel_unix_new (gint fd)
564 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
565 GIOChannel *channel = (GIOChannel *)unix_channel;
567 g_io_channel_init (channel);
568 channel->funcs = &unix_channel_funcs;
570 unix_channel->fd = fd;
572 /* I'm not sure if fstat on a non-file (e.g., socket) works
573 * it should be safe to say if it fails, the fd isn't seekable.
575 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
576 * succeed in most cases.
578 if (fstat (unix_channel->fd, &buffer) == 0)
579 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
580 || S_ISBLK (buffer.st_mode);
581 else /* Assume not seekable */
582 channel->is_seekable = FALSE;
584 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
590 g_io_channel_unix_get_fd (GIOChannel *channel)
592 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
593 return unix_channel->fd;