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>
52 typedef struct _GIOUnixChannel GIOUnixChannel;
53 typedef struct _GIOUnixWatch GIOUnixWatch;
55 struct _GIOUnixChannel
66 GIOCondition condition;
70 static GIOStatus g_io_unix_read (GIOChannel *channel,
75 static GIOStatus g_io_unix_write (GIOChannel *channel,
80 static GIOStatus g_io_unix_seek (GIOChannel *channel,
84 static GIOStatus g_io_unix_close (GIOChannel *channel,
86 static void g_io_unix_free (GIOChannel *channel);
87 static GSource* g_io_unix_create_watch (GIOChannel *channel,
88 GIOCondition condition);
89 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
92 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
94 static gboolean g_io_unix_prepare (GSource *source,
96 static gboolean g_io_unix_check (GSource *source);
97 static gboolean g_io_unix_dispatch (GSource *source,
100 static void g_io_unix_finalize (GSource *source);
102 GSourceFuncs g_io_watch_funcs = {
109 static GIOFuncs unix_channel_funcs = {
114 g_io_unix_create_watch,
121 g_io_unix_prepare (GSource *source,
124 GIOUnixWatch *watch = (GIOUnixWatch *)source;
125 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
129 /* Only return TRUE here if _all_ bits in watch->condition will be set
131 return ((watch->condition & buffer_condition) == watch->condition);
135 g_io_unix_check (GSource *source)
137 GIOUnixWatch *watch = (GIOUnixWatch *)source;
138 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
139 GIOCondition poll_condition = watch->pollfd.revents;
141 return ((poll_condition | buffer_condition) & watch->condition);
145 g_io_unix_dispatch (GSource *source,
146 GSourceFunc callback,
150 GIOFunc func = (GIOFunc)callback;
151 GIOUnixWatch *watch = (GIOUnixWatch *)source;
152 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
156 g_warning ("IO watch dispatched without callback\n"
157 "You must call g_source_connect().");
161 return (*func) (watch->channel,
162 (watch->pollfd.revents | buffer_condition) & watch->condition,
167 g_io_unix_finalize (GSource *source)
169 GIOUnixWatch *watch = (GIOUnixWatch *)source;
171 g_io_channel_unref (watch->channel);
175 g_io_unix_read (GIOChannel *channel,
181 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
184 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
188 result = read (unix_channel->fd, buf, count);
202 return G_IO_STATUS_AGAIN;
205 g_set_error (err, G_IO_CHANNEL_ERROR,
206 g_io_channel_error_from_errno (errno),
208 return G_IO_STATUS_ERROR;
212 *bytes_read = result;
214 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
218 g_io_unix_write (GIOChannel *channel,
221 gsize *bytes_written,
224 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
228 result = write (unix_channel->fd, buf, count);
242 return G_IO_STATUS_AGAIN;
245 g_set_error (err, G_IO_CHANNEL_ERROR,
246 g_io_channel_error_from_errno (errno),
248 return G_IO_STATUS_ERROR;
252 *bytes_written = result;
254 return G_IO_STATUS_NORMAL;
258 g_io_unix_seek (GIOChannel *channel,
263 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
280 whence = -1; /* Shut the compiler up */
281 g_assert_not_reached ();
285 if (tmp_offset != offset)
287 g_set_error (err, G_IO_CHANNEL_ERROR,
288 g_io_channel_error_from_errno (EINVAL),
289 g_strerror (EINVAL));
290 return G_IO_STATUS_ERROR;
293 result = lseek (unix_channel->fd, tmp_offset, whence);
297 g_set_error (err, G_IO_CHANNEL_ERROR,
298 g_io_channel_error_from_errno (errno),
300 return G_IO_STATUS_ERROR;
303 return G_IO_STATUS_NORMAL;
308 g_io_unix_close (GIOChannel *channel,
311 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
313 if (close (unix_channel->fd) < 0)
315 g_set_error (err, G_IO_CHANNEL_ERROR,
316 g_io_channel_error_from_errno (errno),
318 return G_IO_STATUS_ERROR;
321 return G_IO_STATUS_NORMAL;
325 g_io_unix_free (GIOChannel *channel)
327 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
329 g_free (unix_channel);
333 g_io_unix_create_watch (GIOChannel *channel,
334 GIOCondition condition)
336 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
341 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
342 watch = (GIOUnixWatch *)source;
344 watch->channel = channel;
345 g_io_channel_ref (channel);
347 watch->condition = condition;
349 watch->pollfd.fd = unix_channel->fd;
350 watch->pollfd.events = condition;
352 g_source_add_poll (source, &watch->pollfd);
358 g_io_unix_set_flags (GIOChannel *channel,
363 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
367 if (flags & G_IO_FLAG_APPEND)
368 fcntl_flags |= O_APPEND;
369 if (flags & G_IO_FLAG_NONBLOCK)
371 fcntl_flags |= O_NONBLOCK;
373 fcntl_flags |= O_NDELAY;
376 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
378 g_set_error (err, G_IO_CHANNEL_ERROR,
379 g_io_channel_error_from_errno (errno),
381 return G_IO_STATUS_ERROR;
384 return G_IO_STATUS_NORMAL;
388 g_io_unix_get_flags (GIOChannel *channel)
392 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
394 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
396 if (fcntl_flags == -1)
398 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
399 g_strerror (errno), errno);
403 if (fcntl_flags & O_APPEND)
404 flags |= G_IO_FLAG_APPEND;
406 if (fcntl_flags & O_NONBLOCK)
408 if (fcntl_flags & O_NDELAY)
410 flags |= G_IO_FLAG_NONBLOCK;
412 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
415 channel->is_readable = TRUE;
416 channel->is_writeable = FALSE;
419 channel->is_readable = FALSE;
420 channel->is_writeable = TRUE;
423 channel->is_readable = TRUE;
424 channel->is_writeable = TRUE;
427 g_assert_not_reached ();
434 g_io_channel_new_file (const gchar *filename,
441 enum { /* Cheesy hack */
449 g_return_val_if_fail (filename != NULL, NULL);
450 g_return_val_if_fail (mode != NULL, NULL);
451 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
465 g_warning ("Invalid GIOFileMode %s.\n", mode);
476 mode_num |= MODE_PLUS;
481 g_warning ("Invalid GIOFileMode %s.\n", mode);
491 flags = O_WRONLY | O_TRUNC | O_CREAT;
494 flags = O_WRONLY | O_APPEND | O_CREAT;
496 case MODE_R | MODE_PLUS:
499 case MODE_W | MODE_PLUS:
500 flags = O_RDWR | O_TRUNC | O_CREAT;
502 case MODE_A | MODE_PLUS:
503 flags = O_RDWR | O_APPEND | O_CREAT;
506 g_assert_not_reached ();
510 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
511 fid = open (filename, flags, create_mode);
514 g_set_error (error, G_FILE_ERROR,
515 g_file_error_from_errno (errno),
517 return (GIOChannel *)NULL;
520 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
523 g_set_error (error, G_FILE_ERROR,
524 g_file_error_from_errno (errno),
526 return (GIOChannel *)NULL;
529 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
531 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
532 || S_ISBLK (buffer.st_mode);
537 channel->is_readable = TRUE;
538 channel->is_writeable = FALSE;
542 channel->is_readable = FALSE;
543 channel->is_writeable = TRUE;
545 case MODE_R | MODE_PLUS:
546 case MODE_W | MODE_PLUS:
547 case MODE_A | MODE_PLUS:
548 channel->is_readable = TRUE;
549 channel->is_writeable = TRUE;
552 g_assert_not_reached ();
555 g_io_channel_init (channel);
556 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
557 channel->funcs = &unix_channel_funcs;
559 ((GIOUnixChannel *) channel)->fd = fid;
564 g_io_channel_unix_new (gint fd)
567 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
568 GIOChannel *channel = (GIOChannel *)unix_channel;
570 g_io_channel_init (channel);
571 channel->funcs = &unix_channel_funcs;
573 unix_channel->fd = fd;
575 /* I'm not sure if fstat on a non-file (e.g., socket) works
576 * it should be safe to say if it fails, the fd isn't seekable.
578 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
579 * succeed in most cases.
581 if (fstat (unix_channel->fd, &buffer) == 0)
582 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
583 || S_ISBLK (buffer.st_mode);
584 else /* Assume not seekable */
585 channel->is_seekable = FALSE;
587 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
593 g_io_channel_unix_get_fd (GIOChannel *channel)
595 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
596 return unix_channel->fd;