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 * SPDX-License-Identifier: LGPL-2.1-or-later
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
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>
45 #include <glib/gstdio.h>
51 #include "giochannel.h"
54 #include "gfileutils.h"
55 #include "gstrfuncs.h"
56 #include "gtestutils.h"
62 typedef struct _GIOUnixChannel GIOUnixChannel;
63 typedef struct _GIOUnixWatch GIOUnixWatch;
65 struct _GIOUnixChannel
76 GIOCondition condition;
80 static GIOStatus g_io_unix_read (GIOChannel *channel,
85 static GIOStatus g_io_unix_write (GIOChannel *channel,
90 static GIOStatus g_io_unix_seek (GIOChannel *channel,
94 static GIOStatus g_io_unix_close (GIOChannel *channel,
96 static void g_io_unix_free (GIOChannel *channel);
97 static GSource* g_io_unix_create_watch (GIOChannel *channel,
98 GIOCondition condition);
99 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
102 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
104 static gboolean g_io_unix_prepare (GSource *source,
106 static gboolean g_io_unix_check (GSource *source);
107 static gboolean g_io_unix_dispatch (GSource *source,
108 GSourceFunc callback,
110 static void g_io_unix_finalize (GSource *source);
112 GSourceFuncs g_io_watch_funcs = {
120 static GIOFuncs unix_channel_funcs = {
125 g_io_unix_create_watch,
132 g_io_unix_prepare (GSource *source,
135 GIOUnixWatch *watch = (GIOUnixWatch *)source;
136 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
138 /* Only return TRUE here if _all_ bits in watch->condition will be set
140 return ((watch->condition & buffer_condition) == watch->condition);
144 g_io_unix_check (GSource *source)
146 GIOUnixWatch *watch = (GIOUnixWatch *)source;
147 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
148 GIOCondition poll_condition = watch->pollfd.revents;
150 return ((poll_condition | buffer_condition) & watch->condition);
154 g_io_unix_dispatch (GSource *source,
155 GSourceFunc callback,
159 GIOFunc func = (GIOFunc)callback;
160 GIOUnixWatch *watch = (GIOUnixWatch *)source;
161 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
165 g_warning ("IO watch dispatched without callback. "
166 "You must call g_source_connect().");
170 return (*func) (watch->channel,
171 (watch->pollfd.revents | buffer_condition) & watch->condition,
176 g_io_unix_finalize (GSource *source)
178 GIOUnixWatch *watch = (GIOUnixWatch *)source;
180 g_io_channel_unref (watch->channel);
184 g_io_unix_read (GIOChannel *channel,
190 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
193 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
197 result = read (unix_channel->fd, buf, count);
212 return G_IO_STATUS_AGAIN;
215 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
216 g_io_channel_error_from_errno (errsv),
218 return G_IO_STATUS_ERROR;
222 *bytes_read = result;
224 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
228 g_io_unix_write (GIOChannel *channel,
231 gsize *bytes_written,
234 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
238 result = write (unix_channel->fd, buf, count);
253 return G_IO_STATUS_AGAIN;
256 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
257 g_io_channel_error_from_errno (errsv),
259 return G_IO_STATUS_ERROR;
263 *bytes_written = result;
265 return G_IO_STATUS_NORMAL;
269 g_io_unix_seek (GIOChannel *channel,
274 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
291 whence = -1; /* Shut the compiler up */
292 g_assert_not_reached ();
296 if (tmp_offset != offset)
298 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
299 g_io_channel_error_from_errno (EINVAL),
300 g_strerror (EINVAL));
301 return G_IO_STATUS_ERROR;
304 result = lseek (unix_channel->fd, tmp_offset, whence);
309 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
310 g_io_channel_error_from_errno (errsv),
312 return G_IO_STATUS_ERROR;
315 return G_IO_STATUS_NORMAL;
320 g_io_unix_close (GIOChannel *channel,
323 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
325 if (close (unix_channel->fd) < 0)
328 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
329 g_io_channel_error_from_errno (errsv),
331 return G_IO_STATUS_ERROR;
334 return G_IO_STATUS_NORMAL;
338 g_io_unix_free (GIOChannel *channel)
340 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
342 g_free (unix_channel);
346 g_io_unix_create_watch (GIOChannel *channel,
347 GIOCondition condition)
349 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
354 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
355 g_source_set_static_name (source, "GIOChannel (Unix)");
356 watch = (GIOUnixWatch *)source;
358 watch->channel = channel;
359 g_io_channel_ref (channel);
361 watch->condition = condition;
363 watch->pollfd.fd = unix_channel->fd;
364 watch->pollfd.events = condition;
366 g_source_add_poll (source, &watch->pollfd);
372 g_io_unix_set_flags (GIOChannel *channel,
377 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
381 if (flags & G_IO_FLAG_APPEND)
382 fcntl_flags |= O_APPEND;
383 if (flags & G_IO_FLAG_NONBLOCK)
385 fcntl_flags |= O_NONBLOCK;
387 fcntl_flags |= O_NDELAY;
390 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
393 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
394 g_io_channel_error_from_errno (errsv),
396 return G_IO_STATUS_ERROR;
399 return G_IO_STATUS_NORMAL;
403 g_io_unix_get_flags (GIOChannel *channel)
405 GIOFlags flags = G_IO_FLAG_NONE;
407 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
409 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
411 if (fcntl_flags == -1)
414 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)",
415 g_strerror (err), err);
419 if (fcntl_flags & O_APPEND)
420 flags |= G_IO_FLAG_APPEND;
422 if (fcntl_flags & O_NONBLOCK)
424 if (fcntl_flags & O_NDELAY)
426 flags |= G_IO_FLAG_NONBLOCK;
428 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
431 channel->is_readable = TRUE;
432 channel->is_writeable = FALSE;
435 channel->is_readable = FALSE;
436 channel->is_writeable = TRUE;
439 channel->is_readable = TRUE;
440 channel->is_writeable = TRUE;
443 g_assert_not_reached ();
450 g_io_channel_new_file (const gchar *filename,
457 enum { /* Cheesy hack */
462 MODE_R_PLUS = MODE_R | MODE_PLUS,
463 MODE_W_PLUS = MODE_W | MODE_PLUS,
464 MODE_A_PLUS = MODE_A | MODE_PLUS
468 g_return_val_if_fail (filename != NULL, NULL);
469 g_return_val_if_fail (mode != NULL, NULL);
470 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
484 g_warning ("Invalid GIOFileMode %s.", mode);
495 mode_num |= MODE_PLUS;
500 g_warning ("Invalid GIOFileMode %s.", mode);
510 flags = O_WRONLY | O_TRUNC | O_CREAT;
513 flags = O_WRONLY | O_APPEND | O_CREAT;
519 flags = O_RDWR | O_TRUNC | O_CREAT;
522 flags = O_RDWR | O_APPEND | O_CREAT;
526 g_assert_not_reached ();
530 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
532 fid = g_open (filename, flags | O_CLOEXEC, create_mode);
536 g_set_error_literal (error, G_FILE_ERROR,
537 g_file_error_from_errno (err),
539 return (GIOChannel *)NULL;
542 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
546 g_set_error_literal (error, G_FILE_ERROR,
547 g_file_error_from_errno (err),
549 return (GIOChannel *)NULL;
552 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
554 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
555 || S_ISBLK (buffer.st_mode);
560 channel->is_readable = TRUE;
561 channel->is_writeable = FALSE;
565 channel->is_readable = FALSE;
566 channel->is_writeable = TRUE;
571 channel->is_readable = TRUE;
572 channel->is_writeable = TRUE;
576 g_assert_not_reached ();
579 g_io_channel_init (channel);
580 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
581 channel->funcs = &unix_channel_funcs;
583 ((GIOUnixChannel *) channel)->fd = fid;
588 * g_io_channel_unix_new:
589 * @fd: a file descriptor.
591 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
592 * this works for plain files, pipes, and sockets.
594 * The returned #GIOChannel has a reference count of 1.
596 * The default encoding for #GIOChannel is UTF-8. If your application
597 * is reading output from a command using via pipe, you may need to set
598 * the encoding to the encoding of the current locale (see
599 * g_get_charset()) with the g_io_channel_set_encoding() function.
600 * By default, the fd passed will not be closed when the final reference
601 * to the #GIOChannel data structure is dropped.
603 * If you want to read raw binary data without interpretation, then
604 * call the g_io_channel_set_encoding() function with %NULL for the
607 * This function is available in GLib on Windows, too, but you should
608 * avoid using it on Windows. The domain of file descriptors and
609 * sockets overlap. There is no way for GLib to know which one you mean
610 * in case the argument you pass to this function happens to be both a
611 * valid file descriptor and socket. If that happens a warning is
612 * issued, and GLib assumes that it is the file descriptor you mean.
614 * Returns: a new #GIOChannel.
617 g_io_channel_unix_new (gint fd)
620 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
621 GIOChannel *channel = (GIOChannel *)unix_channel;
623 g_io_channel_init (channel);
624 channel->funcs = &unix_channel_funcs;
626 unix_channel->fd = fd;
628 /* I'm not sure if fstat on a non-file (e.g., socket) works
629 * it should be safe to say if it fails, the fd isn't seekable.
631 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
632 * succeed in most cases.
634 if (fstat (unix_channel->fd, &buffer) == 0)
635 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
636 || S_ISBLK (buffer.st_mode);
637 else /* Assume not seekable */
638 channel->is_seekable = FALSE;
640 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
646 * g_io_channel_unix_get_fd:
647 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
649 * Returns the file descriptor of the #GIOChannel.
651 * On Windows this function returns the file descriptor or socket of
654 * Returns: the file descriptor of the #GIOChannel.
657 g_io_channel_unix_get_fd (GIOChannel *channel)
659 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
660 return unix_channel->fd;