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>
47 #include "giochannel.h"
50 #include "gfileutils.h"
51 #include "gstrfuncs.h"
52 #include "gtestutils.h"
58 typedef struct _GIOUnixChannel GIOUnixChannel;
59 typedef struct _GIOUnixWatch GIOUnixWatch;
61 struct _GIOUnixChannel
72 GIOCondition condition;
76 static GIOStatus g_io_unix_read (GIOChannel *channel,
81 static GIOStatus g_io_unix_write (GIOChannel *channel,
86 static GIOStatus g_io_unix_seek (GIOChannel *channel,
90 static GIOStatus g_io_unix_close (GIOChannel *channel,
92 static void g_io_unix_free (GIOChannel *channel);
93 static GSource* g_io_unix_create_watch (GIOChannel *channel,
94 GIOCondition condition);
95 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
98 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
100 static gboolean g_io_unix_prepare (GSource *source,
102 static gboolean g_io_unix_check (GSource *source);
103 static gboolean g_io_unix_dispatch (GSource *source,
104 GSourceFunc callback,
106 static void g_io_unix_finalize (GSource *source);
108 GSourceFuncs g_io_watch_funcs = {
116 static GIOFuncs unix_channel_funcs = {
121 g_io_unix_create_watch,
128 g_io_unix_prepare (GSource *source,
131 GIOUnixWatch *watch = (GIOUnixWatch *)source;
132 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
136 /* Only return TRUE here if _all_ bits in watch->condition will be set
138 return ((watch->condition & buffer_condition) == watch->condition);
142 g_io_unix_check (GSource *source)
144 GIOUnixWatch *watch = (GIOUnixWatch *)source;
145 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
146 GIOCondition poll_condition = watch->pollfd.revents;
148 return ((poll_condition | buffer_condition) & watch->condition);
152 g_io_unix_dispatch (GSource *source,
153 GSourceFunc callback,
157 GIOFunc func = (GIOFunc)callback;
158 GIOUnixWatch *watch = (GIOUnixWatch *)source;
159 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
163 g_warning ("IO watch dispatched without callback. "
164 "You must call g_source_connect().");
168 return (*func) (watch->channel,
169 (watch->pollfd.revents | buffer_condition) & watch->condition,
174 g_io_unix_finalize (GSource *source)
176 GIOUnixWatch *watch = (GIOUnixWatch *)source;
178 g_io_channel_unref (watch->channel);
182 g_io_unix_read (GIOChannel *channel,
188 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
191 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
195 result = read (unix_channel->fd, buf, count);
210 return G_IO_STATUS_AGAIN;
213 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
214 g_io_channel_error_from_errno (errsv),
216 return G_IO_STATUS_ERROR;
220 *bytes_read = result;
222 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
226 g_io_unix_write (GIOChannel *channel,
229 gsize *bytes_written,
232 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
236 result = write (unix_channel->fd, buf, count);
251 return G_IO_STATUS_AGAIN;
254 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
255 g_io_channel_error_from_errno (errsv),
257 return G_IO_STATUS_ERROR;
261 *bytes_written = result;
263 return G_IO_STATUS_NORMAL;
267 g_io_unix_seek (GIOChannel *channel,
272 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
289 whence = -1; /* Shut the compiler up */
290 g_assert_not_reached ();
294 if (tmp_offset != offset)
296 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
297 g_io_channel_error_from_errno (EINVAL),
298 g_strerror (EINVAL));
299 return G_IO_STATUS_ERROR;
302 result = lseek (unix_channel->fd, tmp_offset, whence);
307 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
308 g_io_channel_error_from_errno (errsv),
310 return G_IO_STATUS_ERROR;
313 return G_IO_STATUS_NORMAL;
318 g_io_unix_close (GIOChannel *channel,
321 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
323 if (close (unix_channel->fd) < 0)
326 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
327 g_io_channel_error_from_errno (errsv),
329 return G_IO_STATUS_ERROR;
332 return G_IO_STATUS_NORMAL;
336 g_io_unix_free (GIOChannel *channel)
338 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
340 g_free (unix_channel);
344 g_io_unix_create_watch (GIOChannel *channel,
345 GIOCondition condition)
347 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
352 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
353 g_source_set_static_name (source, "GIOChannel (Unix)");
354 watch = (GIOUnixWatch *)source;
356 watch->channel = channel;
357 g_io_channel_ref (channel);
359 watch->condition = condition;
361 watch->pollfd.fd = unix_channel->fd;
362 watch->pollfd.events = condition;
364 g_source_add_poll (source, &watch->pollfd);
370 g_io_unix_set_flags (GIOChannel *channel,
375 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
379 if (flags & G_IO_FLAG_APPEND)
380 fcntl_flags |= O_APPEND;
381 if (flags & G_IO_FLAG_NONBLOCK)
383 fcntl_flags |= O_NONBLOCK;
385 fcntl_flags |= O_NDELAY;
388 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
391 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
392 g_io_channel_error_from_errno (errsv),
394 return G_IO_STATUS_ERROR;
397 return G_IO_STATUS_NORMAL;
401 g_io_unix_get_flags (GIOChannel *channel)
403 GIOFlags flags = G_IO_FLAG_NONE;
405 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
407 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
409 if (fcntl_flags == -1)
412 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)",
413 g_strerror (err), err);
417 if (fcntl_flags & O_APPEND)
418 flags |= G_IO_FLAG_APPEND;
420 if (fcntl_flags & O_NONBLOCK)
422 if (fcntl_flags & O_NDELAY)
424 flags |= G_IO_FLAG_NONBLOCK;
426 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
429 channel->is_readable = TRUE;
430 channel->is_writeable = FALSE;
433 channel->is_readable = FALSE;
434 channel->is_writeable = TRUE;
437 channel->is_readable = TRUE;
438 channel->is_writeable = TRUE;
441 g_assert_not_reached ();
448 g_io_channel_new_file (const gchar *filename,
455 enum { /* Cheesy hack */
460 MODE_R_PLUS = MODE_R | MODE_PLUS,
461 MODE_W_PLUS = MODE_W | MODE_PLUS,
462 MODE_A_PLUS = MODE_A | MODE_PLUS
466 g_return_val_if_fail (filename != NULL, NULL);
467 g_return_val_if_fail (mode != NULL, NULL);
468 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
482 g_warning ("Invalid GIOFileMode %s.", mode);
493 mode_num |= MODE_PLUS;
498 g_warning ("Invalid GIOFileMode %s.", mode);
508 flags = O_WRONLY | O_TRUNC | O_CREAT;
511 flags = O_WRONLY | O_APPEND | O_CREAT;
517 flags = O_RDWR | O_TRUNC | O_CREAT;
520 flags = O_RDWR | O_APPEND | O_CREAT;
524 g_assert_not_reached ();
528 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
530 fid = g_open (filename, flags, create_mode);
534 g_set_error_literal (error, G_FILE_ERROR,
535 g_file_error_from_errno (err),
537 return (GIOChannel *)NULL;
540 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
544 g_set_error_literal (error, G_FILE_ERROR,
545 g_file_error_from_errno (err),
547 return (GIOChannel *)NULL;
550 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
552 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
553 || S_ISBLK (buffer.st_mode);
558 channel->is_readable = TRUE;
559 channel->is_writeable = FALSE;
563 channel->is_readable = FALSE;
564 channel->is_writeable = TRUE;
569 channel->is_readable = TRUE;
570 channel->is_writeable = TRUE;
574 g_assert_not_reached ();
577 g_io_channel_init (channel);
578 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
579 channel->funcs = &unix_channel_funcs;
581 ((GIOUnixChannel *) channel)->fd = fid;
586 * g_io_channel_unix_new:
587 * @fd: a file descriptor.
589 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
590 * this works for plain files, pipes, and sockets.
592 * The returned #GIOChannel has a reference count of 1.
594 * The default encoding for #GIOChannel is UTF-8. If your application
595 * is reading output from a command using via pipe, you may need to set
596 * the encoding to the encoding of the current locale (see
597 * g_get_charset()) with the g_io_channel_set_encoding() function.
598 * By default, the fd passed will not be closed when the final reference
599 * to the #GIOChannel data structure is dropped.
601 * If you want to read raw binary data without interpretation, then
602 * call the g_io_channel_set_encoding() function with %NULL for the
605 * This function is available in GLib on Windows, too, but you should
606 * avoid using it on Windows. The domain of file descriptors and
607 * sockets overlap. There is no way for GLib to know which one you mean
608 * in case the argument you pass to this function happens to be both a
609 * valid file descriptor and socket. If that happens a warning is
610 * issued, and GLib assumes that it is the file descriptor you mean.
612 * Returns: a new #GIOChannel.
615 g_io_channel_unix_new (gint fd)
618 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
619 GIOChannel *channel = (GIOChannel *)unix_channel;
621 g_io_channel_init (channel);
622 channel->funcs = &unix_channel_funcs;
624 unix_channel->fd = fd;
626 /* I'm not sure if fstat on a non-file (e.g., socket) works
627 * it should be safe to say if it fails, the fd isn't seekable.
629 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
630 * succeed in most cases.
632 if (fstat (unix_channel->fd, &buffer) == 0)
633 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
634 || S_ISBLK (buffer.st_mode);
635 else /* Assume not seekable */
636 channel->is_seekable = FALSE;
638 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
644 * g_io_channel_unix_get_fd:
645 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
647 * Returns the file descriptor of the #GIOChannel.
649 * On Windows this function returns the file descriptor or socket of
652 * Returns: the file descriptor of the #GIOChannel.
655 g_io_channel_unix_get_fd (GIOChannel *channel)
657 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
658 return unix_channel->fd;