Blame glib/glib-unix.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 2011 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * glib-unix.c: UNIX specific API wrappers and convenience functions
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Authors: Colin Walters <walters@verbum.org>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
/* To make bionic export pipe2() */
Packit ae235b
#ifndef _GNU_SOURCE
Packit ae235b
#define _GNU_SOURCE 1
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glib-unix.h"
Packit ae235b
#include "gmain-internal.h"
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gunix
Packit ae235b
 * @title: UNIX-specific utilities and integration
Packit ae235b
 * @short_description: pipes, signal handling
Packit ae235b
 * @include: glib-unix.h
Packit ae235b
 *
Packit ae235b
 * Most of GLib is intended to be portable; in contrast, this set of
Packit ae235b
 * functions is designed for programs which explicitly target UNIX,
Packit ae235b
 * or are using it to build higher level abstractions which would be
Packit ae235b
 * conditionally compiled if the platform matches G_OS_UNIX.
Packit ae235b
 *
Packit ae235b
 * To use these functions, you must explicitly include the
Packit ae235b
 * "glib-unix.h" header.
Packit ae235b
 */
Packit ae235b
Packit ae235b
G_DEFINE_QUARK (g-unix-error-quark, g_unix_error)
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_set_error_from_errno (GError **error,
Packit ae235b
                             gint     saved_errno)
Packit ae235b
{
Packit ae235b
  g_set_error_literal (error,
Packit ae235b
                       G_UNIX_ERROR,
Packit ae235b
                       0,
Packit ae235b
                       g_strerror (saved_errno));
Packit ae235b
  errno = saved_errno;
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_open_pipe:
Packit ae235b
 * @fds: Array of two integers
Packit ae235b
 * @flags: Bitfield of file descriptor flags, as for fcntl()
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Similar to the UNIX pipe() call, but on modern systems like Linux
Packit ae235b
 * uses the pipe2() system call, which atomically creates a pipe with
Packit ae235b
 * the configured flags. The only supported flag currently is
Packit ae235b
 * %FD_CLOEXEC. If for example you want to configure %O_NONBLOCK, that
Packit ae235b
 * must still be done separately with fcntl().
Packit ae235b
 *
Packit ae235b
 * This function does not take %O_CLOEXEC, it takes %FD_CLOEXEC as if
Packit ae235b
 * for fcntl(); these are different on Linux/glibc.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE on success, %FALSE if not (and errno will be set).
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_unix_open_pipe (int     *fds,
Packit ae235b
                  int      flags,
Packit ae235b
                  GError **error)
Packit ae235b
{
Packit ae235b
  int ecode;
Packit ae235b
Packit ae235b
  /* We only support FD_CLOEXEC */
Packit ae235b
  g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE);
Packit ae235b
Packit ae235b
#ifdef HAVE_PIPE2
Packit ae235b
  {
Packit ae235b
    int pipe2_flags = 0;
Packit ae235b
    if (flags & FD_CLOEXEC)
Packit ae235b
      pipe2_flags |= O_CLOEXEC;
Packit ae235b
    /* Atomic */
Packit ae235b
    ecode = pipe2 (fds, pipe2_flags);
Packit ae235b
    if (ecode == -1 && errno != ENOSYS)
Packit ae235b
      return g_unix_set_error_from_errno (error, errno);
Packit ae235b
    else if (ecode == 0)
Packit ae235b
      return TRUE;
Packit ae235b
    /* Fall through on -ENOSYS, we must be running on an old kernel */
Packit ae235b
  }
Packit ae235b
#endif
Packit ae235b
  ecode = pipe (fds);
Packit ae235b
  if (ecode == -1)
Packit ae235b
    return g_unix_set_error_from_errno (error, errno);
Packit ae235b
Packit ae235b
  if (flags == 0)
Packit ae235b
    return TRUE;
Packit ae235b
Packit ae235b
  ecode = fcntl (fds[0], F_SETFD, flags);
Packit ae235b
  if (ecode == -1)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
      close (fds[0]);
Packit ae235b
      close (fds[1]);
Packit ae235b
      return g_unix_set_error_from_errno (error, saved_errno);
Packit ae235b
    }
Packit ae235b
  ecode = fcntl (fds[1], F_SETFD, flags);
Packit ae235b
  if (ecode == -1)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
      close (fds[0]);
Packit ae235b
      close (fds[1]);
Packit ae235b
      return g_unix_set_error_from_errno (error, saved_errno);
Packit ae235b
    }
Packit ae235b
  return TRUE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_set_fd_nonblocking:
Packit ae235b
 * @fd: A file descriptor
Packit ae235b
 * @nonblock: If %TRUE, set the descriptor to be non-blocking
Packit ae235b
 * @error: a #GError
Packit ae235b
 *
Packit ae235b
 * Control the non-blocking state of the given file descriptor,
Packit ae235b
 * according to @nonblock. On most systems this uses %O_NONBLOCK, but
Packit ae235b
 * on some older ones may use %O_NDELAY.
Packit ae235b
 *
Packit ae235b
 * Returns: %TRUE if successful
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
gboolean
Packit ae235b
g_unix_set_fd_nonblocking (gint       fd,
Packit ae235b
                           gboolean   nonblock,
Packit ae235b
                           GError   **error)
Packit ae235b
{
Packit ae235b
#ifdef F_GETFL
Packit ae235b
  glong fcntl_flags;
Packit ae235b
  fcntl_flags = fcntl (fd, F_GETFL);
Packit ae235b
Packit ae235b
  if (fcntl_flags == -1)
Packit ae235b
    return g_unix_set_error_from_errno (error, errno);
Packit ae235b
Packit ae235b
  if (nonblock)
Packit ae235b
    {
Packit ae235b
#ifdef O_NONBLOCK
Packit ae235b
      fcntl_flags |= O_NONBLOCK;
Packit ae235b
#else
Packit ae235b
      fcntl_flags |= O_NDELAY;
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
#ifdef O_NONBLOCK
Packit ae235b
      fcntl_flags &= ~O_NONBLOCK;
Packit ae235b
#else
Packit ae235b
      fcntl_flags &= ~O_NDELAY;
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
Packit ae235b
    return g_unix_set_error_from_errno (error, errno);
Packit ae235b
  return TRUE;
Packit ae235b
#else
Packit ae235b
  return g_unix_set_error_from_errno (error, EINVAL);
Packit ae235b
#endif
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_signal_source_new:
Packit ae235b
 * @signum: A signal number
Packit ae235b
 *
Packit ae235b
 * Create a #GSource that will be dispatched upon delivery of the UNIX
Packit ae235b
 * signal @signum.  In GLib versions before 2.36, only `SIGHUP`, `SIGINT`,
Packit ae235b
 * `SIGTERM` can be monitored.  In GLib 2.36, `SIGUSR1` and `SIGUSR2`
Packit ae235b
 * were added. In GLib 2.54, `SIGWINCH` was added.
Packit ae235b
 *
Packit ae235b
 * Note that unlike the UNIX default, all sources which have created a
Packit ae235b
 * watch will be dispatched, regardless of which underlying thread
Packit ae235b
 * invoked g_unix_signal_source_new().
Packit ae235b
 *
Packit ae235b
 * For example, an effective use of this function is to handle `SIGTERM`
Packit ae235b
 * cleanly; flushing any outstanding files, and then calling
Packit ae235b
 * g_main_loop_quit ().  It is not safe to do any of this a regular
Packit ae235b
 * UNIX signal handler; your handler may be invoked while malloc() or
Packit ae235b
 * another library function is running, causing reentrancy if you
Packit ae235b
 * attempt to use it from the handler.  None of the GLib/GObject API
Packit ae235b
 * is safe against this kind of reentrancy.
Packit ae235b
 *
Packit ae235b
 * The interaction of this source when combined with native UNIX
Packit ae235b
 * functions like sigprocmask() is not defined.
Packit ae235b
 *
Packit ae235b
 * The source will not initially be associated with any #GMainContext
Packit ae235b
 * and must be added to one with g_source_attach() before it will be
Packit ae235b
 * executed.
Packit ae235b
 *
Packit ae235b
 * Returns: A newly created #GSource
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
GSource *
Packit ae235b
g_unix_signal_source_new (int signum)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM ||
Packit ae235b
                        signum == SIGUSR1 || signum == SIGUSR2 || signum == SIGWINCH,
Packit ae235b
                        NULL);
Packit ae235b
Packit ae235b
  return _g_main_create_unix_signal_watch (signum);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_signal_add_full: (rename-to g_unix_signal_add)
Packit ae235b
 * @priority: the priority of the signal source. Typically this will be in
Packit ae235b
 *            the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
Packit ae235b
 * @signum: Signal number
Packit ae235b
 * @handler: Callback
Packit ae235b
 * @user_data: Data for @handler
Packit ae235b
 * @notify: #GDestroyNotify for @handler
Packit ae235b
 *
Packit ae235b
 * A convenience function for g_unix_signal_source_new(), which
Packit ae235b
 * attaches to the default #GMainContext.  You can remove the watch
Packit ae235b
 * using g_source_remove().
Packit ae235b
 *
Packit ae235b
 * Returns: An ID (greater than 0) for the event source
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_unix_signal_add_full (int            priority,
Packit ae235b
                        int            signum,
Packit ae235b
                        GSourceFunc    handler,
Packit ae235b
                        gpointer       user_data,
Packit ae235b
                        GDestroyNotify notify)
Packit ae235b
{
Packit ae235b
  guint id;
Packit ae235b
  GSource *source;
Packit ae235b
Packit ae235b
  source = g_unix_signal_source_new (signum);
Packit ae235b
Packit ae235b
  if (priority != G_PRIORITY_DEFAULT)
Packit ae235b
    g_source_set_priority (source, priority);
Packit ae235b
Packit ae235b
  g_source_set_callback (source, handler, user_data, notify);
Packit ae235b
  id = g_source_attach (source, NULL);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  return id;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_signal_add:
Packit ae235b
 * @signum: Signal number
Packit ae235b
 * @handler: Callback
Packit ae235b
 * @user_data: Data for @handler
Packit ae235b
 *
Packit ae235b
 * A convenience function for g_unix_signal_source_new(), which
Packit ae235b
 * attaches to the default #GMainContext.  You can remove the watch
Packit ae235b
 * using g_source_remove().
Packit ae235b
 *
Packit ae235b
 * Returns: An ID (greater than 0) for the event source
Packit ae235b
 *
Packit ae235b
 * Since: 2.30
Packit ae235b
 */
Packit ae235b
guint
Packit ae235b
g_unix_signal_add (int         signum,
Packit ae235b
                   GSourceFunc handler,
Packit ae235b
                   gpointer    user_data)
Packit ae235b
{
Packit ae235b
  return g_unix_signal_add_full (G_PRIORITY_DEFAULT, signum, handler, user_data, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  GSource source;
Packit ae235b
Packit ae235b
  gint     fd;
Packit ae235b
  gpointer tag;
Packit ae235b
} GUnixFDSource;
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_unix_fd_source_dispatch (GSource     *source,
Packit ae235b
                           GSourceFunc  callback,
Packit ae235b
                           gpointer     user_data)
Packit ae235b
{
Packit ae235b
  GUnixFDSource *fd_source = (GUnixFDSource *) source;
Packit ae235b
  GUnixFDSourceFunc func = (GUnixFDSourceFunc) callback;
Packit ae235b
Packit ae235b
  if (!callback)
Packit ae235b
    {
Packit ae235b
      g_warning ("GUnixFDSource dispatched without callback\n"
Packit ae235b
                 "You must call g_source_set_callback().");
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return (* func) (fd_source->fd, g_source_query_unix_fd (source, fd_source->tag), user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
GSourceFuncs g_unix_fd_source_funcs = {
Packit ae235b
  NULL, NULL, g_unix_fd_source_dispatch, NULL
Packit ae235b
};
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_source_new:
Packit ae235b
 * @fd: a file descriptor
Packit ae235b
 * @condition: IO conditions to watch for on @fd
Packit ae235b
 *
Packit ae235b
 * Creates a #GSource to watch for a particular IO condition on a file
Packit ae235b
 * descriptor.
Packit ae235b
 *
Packit ae235b
 * The source will never close the fd -- you must do it yourself.
Packit ae235b
 *
Packit ae235b
 * Returns: the newly created #GSource
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 **/
Packit ae235b
GSource *
Packit ae235b
g_unix_fd_source_new (gint         fd,
Packit ae235b
                      GIOCondition condition)
Packit ae235b
{
Packit ae235b
  GUnixFDSource *fd_source;
Packit ae235b
  GSource *source;
Packit ae235b
Packit ae235b
  source = g_source_new (&g_unix_fd_source_funcs, sizeof (GUnixFDSource));
Packit ae235b
  fd_source = (GUnixFDSource *) source;
Packit ae235b
Packit ae235b
  fd_source->fd = fd;
Packit ae235b
  fd_source->tag = g_source_add_unix_fd (source, fd, condition);
Packit ae235b
Packit ae235b
  return source;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_add_full:
Packit ae235b
 * @priority: the priority of the source
Packit ae235b
 * @fd: a file descriptor
Packit ae235b
 * @condition: IO conditions to watch for on @fd
Packit ae235b
 * @function: a #GUnixFDSourceFunc
Packit ae235b
 * @user_data: data to pass to @function
Packit ae235b
 * @notify: function to call when the idle is removed, or %NULL
Packit ae235b
 *
Packit ae235b
 * Sets a function to be called when the IO condition, as specified by
Packit ae235b
 * @condition becomes true for @fd.
Packit ae235b
 *
Packit ae235b
 * This is the same as g_unix_fd_add(), except that it allows you to
Packit ae235b
 * specify a non-default priority and a provide a #GDestroyNotify for
Packit ae235b
 * @user_data.
Packit ae235b
 *
Packit ae235b
 * Returns: the ID (greater than 0) of the event source
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 **/
Packit ae235b
guint
Packit ae235b
g_unix_fd_add_full (gint              priority,
Packit ae235b
                    gint              fd,
Packit ae235b
                    GIOCondition      condition,
Packit ae235b
                    GUnixFDSourceFunc function,
Packit ae235b
                    gpointer          user_data,
Packit ae235b
                    GDestroyNotify    notify)
Packit ae235b
{
Packit ae235b
  GSource *source;
Packit ae235b
  guint id;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (function != NULL, 0);
Packit ae235b
Packit ae235b
  source = g_unix_fd_source_new (fd, condition);
Packit ae235b
Packit ae235b
  if (priority != G_PRIORITY_DEFAULT)
Packit ae235b
    g_source_set_priority (source, priority);
Packit ae235b
Packit ae235b
  g_source_set_callback (source, (GSourceFunc) function, user_data, notify);
Packit ae235b
  id = g_source_attach (source, NULL);
Packit ae235b
  g_source_unref (source);
Packit ae235b
Packit ae235b
  return id;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_add:
Packit ae235b
 * @fd: a file descriptor
Packit ae235b
 * @condition: IO conditions to watch for on @fd
Packit ae235b
 * @function: a #GUnixFDSourceFunc
Packit ae235b
 * @user_data: data to pass to @function
Packit ae235b
 *
Packit ae235b
 * Sets a function to be called when the IO condition, as specified by
Packit ae235b
 * @condition becomes true for @fd.
Packit ae235b
 *
Packit ae235b
 * @function will be called when the specified IO condition becomes
Packit ae235b
 * %TRUE.  The function is expected to clear whatever event caused the
Packit ae235b
 * IO condition to become true and return %TRUE in order to be notified
Packit ae235b
 * when it happens again.  If @function returns %FALSE then the watch
Packit ae235b
 * will be cancelled.
Packit ae235b
 *
Packit ae235b
 * The return value of this function can be passed to g_source_remove()
Packit ae235b
 * to cancel the watch at any time that it exists.
Packit ae235b
 *
Packit ae235b
 * The source will never close the fd -- you must do it yourself.
Packit ae235b
 *
Packit ae235b
 * Returns: the ID (greater than 0) of the event source
Packit ae235b
 *
Packit ae235b
 * Since: 2.36
Packit ae235b
 **/
Packit ae235b
guint
Packit ae235b
g_unix_fd_add (gint              fd,
Packit ae235b
               GIOCondition      condition,
Packit ae235b
               GUnixFDSourceFunc function,
Packit ae235b
               gpointer          user_data)
Packit ae235b
{
Packit ae235b
  return g_unix_fd_add_full (G_PRIORITY_DEFAULT, fd, condition, function, user_data, NULL);
Packit ae235b
}