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>
53 typedef struct _GIOUnixChannel GIOUnixChannel;
54 typedef struct _GIOUnixWatch GIOUnixWatch;
56 struct _GIOUnixChannel
67 GIOCondition condition;
71 static GIOStatus g_io_unix_read (GIOChannel *channel,
76 static GIOStatus g_io_unix_write (GIOChannel *channel,
81 static GIOStatus g_io_unix_seek (GIOChannel *channel,
85 static GIOStatus g_io_unix_close (GIOChannel *channel,
87 static void g_io_unix_free (GIOChannel *channel);
88 static GSource* g_io_unix_create_watch (GIOChannel *channel,
89 GIOCondition condition);
90 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
93 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
95 static gboolean g_io_unix_prepare (GSource *source,
97 static gboolean g_io_unix_check (GSource *source);
98 static gboolean g_io_unix_dispatch (GSource *source,
101 static void g_io_unix_finalize (GSource *source);
103 GSourceFuncs g_io_watch_funcs = {
110 static GIOFuncs unix_channel_funcs = {
115 g_io_unix_create_watch,
122 g_io_unix_prepare (GSource *source,
125 GIOUnixWatch *watch = (GIOUnixWatch *)source;
126 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
130 /* Only return TRUE here if _all_ bits in watch->condition will be set
132 return ((watch->condition & buffer_condition) == watch->condition);
136 g_io_unix_check (GSource *source)
138 GIOUnixWatch *watch = (GIOUnixWatch *)source;
139 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
140 GIOCondition poll_condition = watch->pollfd.revents;
142 return ((poll_condition | buffer_condition) & watch->condition);
146 g_io_unix_dispatch (GSource *source,
147 GSourceFunc callback,
151 GIOFunc func = (GIOFunc)callback;
152 GIOUnixWatch *watch = (GIOUnixWatch *)source;
153 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
157 g_warning ("IO watch dispatched without callback\n"
158 "You must call g_source_connect().");
162 return (*func) (watch->channel,
163 (watch->pollfd.revents | buffer_condition) & watch->condition,
168 g_io_unix_finalize (GSource *source)
170 GIOUnixWatch *watch = (GIOUnixWatch *)source;
172 g_io_channel_unref (watch->channel);
176 g_io_unix_read (GIOChannel *channel,
182 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
185 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
189 result = read (unix_channel->fd, buf, count);
203 return G_IO_STATUS_AGAIN;
206 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
207 g_io_channel_error_from_errno (errno),
209 return G_IO_STATUS_ERROR;
213 *bytes_read = result;
215 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
219 g_io_unix_write (GIOChannel *channel,
222 gsize *bytes_written,
225 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
229 result = write (unix_channel->fd, buf, count);
243 return G_IO_STATUS_AGAIN;
246 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
247 g_io_channel_error_from_errno (errno),
249 return G_IO_STATUS_ERROR;
253 *bytes_written = result;
255 return G_IO_STATUS_NORMAL;
259 g_io_unix_seek (GIOChannel *channel,
264 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
281 whence = -1; /* Shut the compiler up */
282 g_assert_not_reached ();
286 if (tmp_offset != offset)
288 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
289 g_io_channel_error_from_errno (EINVAL),
290 g_strerror (EINVAL));
291 return G_IO_STATUS_ERROR;
294 result = lseek (unix_channel->fd, tmp_offset, whence);
298 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
299 g_io_channel_error_from_errno (errno),
301 return G_IO_STATUS_ERROR;
304 return G_IO_STATUS_NORMAL;
309 g_io_unix_close (GIOChannel *channel,
312 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
314 if (close (unix_channel->fd) < 0)
316 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
317 g_io_channel_error_from_errno (errno),
319 return G_IO_STATUS_ERROR;
322 return G_IO_STATUS_NORMAL;
326 g_io_unix_free (GIOChannel *channel)
328 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
330 g_free (unix_channel);
334 g_io_unix_create_watch (GIOChannel *channel,
335 GIOCondition condition)
337 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
342 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
343 watch = (GIOUnixWatch *)source;
345 watch->channel = channel;
346 g_io_channel_ref (channel);
348 watch->condition = condition;
350 watch->pollfd.fd = unix_channel->fd;
351 watch->pollfd.events = condition;
353 g_source_add_poll (source, &watch->pollfd);
359 g_io_unix_set_flags (GIOChannel *channel,
364 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
368 if (flags & G_IO_FLAG_APPEND)
369 fcntl_flags |= O_APPEND;
370 if (flags & G_IO_FLAG_NONBLOCK)
372 fcntl_flags |= O_NONBLOCK;
374 fcntl_flags |= O_NDELAY;
377 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
379 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
380 g_io_channel_error_from_errno (errno),
382 return G_IO_STATUS_ERROR;
385 return G_IO_STATUS_NORMAL;
389 g_io_unix_get_flags (GIOChannel *channel)
393 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
395 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
397 if (fcntl_flags == -1)
399 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
400 g_strerror (errno), errno);
404 if (fcntl_flags & O_APPEND)
405 flags |= G_IO_FLAG_APPEND;
407 if (fcntl_flags & O_NONBLOCK)
409 if (fcntl_flags & O_NDELAY)
411 flags |= G_IO_FLAG_NONBLOCK;
413 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
416 channel->is_readable = TRUE;
417 channel->is_writeable = FALSE;
420 channel->is_readable = FALSE;
421 channel->is_writeable = TRUE;
424 channel->is_readable = TRUE;
425 channel->is_writeable = TRUE;
428 g_assert_not_reached ();
435 g_io_channel_new_file (const gchar *filename,
442 enum { /* Cheesy hack */
450 g_return_val_if_fail (filename != NULL, NULL);
451 g_return_val_if_fail (mode != NULL, NULL);
452 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
466 g_warning ("Invalid GIOFileMode %s.\n", mode);
477 mode_num |= MODE_PLUS;
482 g_warning ("Invalid GIOFileMode %s.\n", mode);
492 flags = O_WRONLY | O_TRUNC | O_CREAT;
495 flags = O_WRONLY | O_APPEND | O_CREAT;
497 case MODE_R | MODE_PLUS:
500 case MODE_W | MODE_PLUS:
501 flags = O_RDWR | O_TRUNC | O_CREAT;
503 case MODE_A | MODE_PLUS:
504 flags = O_RDWR | O_APPEND | O_CREAT;
507 g_assert_not_reached ();
511 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
512 fid = open (filename, flags, create_mode);
515 g_set_error_literal (error, G_FILE_ERROR,
516 g_file_error_from_errno (errno),
518 return (GIOChannel *)NULL;
521 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
524 g_set_error_literal (error, G_FILE_ERROR,
525 g_file_error_from_errno (errno),
527 return (GIOChannel *)NULL;
530 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
532 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
533 || S_ISBLK (buffer.st_mode);
538 channel->is_readable = TRUE;
539 channel->is_writeable = FALSE;
543 channel->is_readable = FALSE;
544 channel->is_writeable = TRUE;
546 case MODE_R | MODE_PLUS:
547 case MODE_W | MODE_PLUS:
548 case MODE_A | MODE_PLUS:
549 channel->is_readable = TRUE;
550 channel->is_writeable = TRUE;
553 g_assert_not_reached ();
556 g_io_channel_init (channel);
557 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
558 channel->funcs = &unix_channel_funcs;
560 ((GIOUnixChannel *) channel)->fd = fid;
565 g_io_channel_unix_new (gint fd)
568 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
569 GIOChannel *channel = (GIOChannel *)unix_channel;
571 g_io_channel_init (channel);
572 channel->funcs = &unix_channel_funcs;
574 unix_channel->fd = fd;
576 /* I'm not sure if fstat on a non-file (e.g., socket) works
577 * it should be safe to say if it fails, the fd isn't seekable.
579 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
580 * succeed in most cases.
582 if (fstat (unix_channel->fd, &buffer) == 0)
583 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
584 || S_ISBLK (buffer.st_mode);
585 else /* Assume not seekable */
586 channel->is_seekable = FALSE;
588 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
594 g_io_channel_unix_get_fd (GIOChannel *channel)
596 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
597 return unix_channel->fd;
600 #define __G_IO_UNIX_C__
601 #include "galiasdef.c"