Blame glib/glib/glib-unix.c

Packit db3073
/* GLIB - Library of useful routines for C programming
Packit db3073
 * Copyright (C) 2011 Red Hat, Inc.
Packit db3073
 *
Packit db3073
 * glib-unix.c: UNIX specific API wrappers and convenience functions
Packit db3073
 *
Packit db3073
 * This library is free software; you can redistribute it and/or
Packit db3073
 * modify it under the terms of the GNU Lesser General Public
Packit db3073
 * License as published by the Free Software Foundation; either
Packit db3073
 * version 2 of the License, or (at your option) any later version.
Packit db3073
 *
Packit db3073
 * This library is distributed in the hope that it will be useful,
Packit db3073
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit db3073
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit db3073
 * Lesser General Public License for more details.
Packit db3073
 *
Packit db3073
 * You should have received a copy of the GNU Lesser General Public
Packit db3073
 * License along with this library; if not, write to the
Packit db3073
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit db3073
 * Boston, MA 02111-1307, USA.
Packit db3073
 *
Packit db3073
 * Authors: Colin Walters <walters@verbum.org>
Packit db3073
 */
Packit db3073
Packit db3073
#include "config.h"
Packit db3073
Packit db3073
#include "glib-unix.h"
Packit db3073
#include "gmain-internal.h"
Packit db3073
Packit db3073
#include <string.h>
Packit db3073
Packit db3073
/**
Packit db3073
 * SECTION:gunix
Packit db3073
 * @title: UNIX-specific utilities and integration
Packit db3073
 * @short_description: pipes, signal handling
Packit db3073
 * @include: glib-unix.h
Packit db3073
 *
Packit db3073
 * Most of GLib is intended to be portable; in contrast, this set of
Packit db3073
 * functions is designed for programs which explicitly target UNIX,
Packit db3073
 * or are using it to build higher level abstractions which would be
Packit db3073
 * conditionally compiled if the platform matches G_OS_UNIX.
Packit db3073
 *
Packit db3073
 * To use these functions, you must explicitly include the
Packit db3073
 * "glib-unix.h" header.
Packit db3073
 */
Packit db3073
Packit db3073
GQuark
Packit db3073
g_unix_error_quark (void)
Packit db3073
{
Packit db3073
  return g_quark_from_static_string ("g-unix-error-quark");
Packit db3073
}
Packit db3073
Packit db3073
static gboolean
Packit db3073
g_unix_set_error_from_errno (GError **error,
Packit db3073
                             gint     saved_errno)
Packit db3073
{
Packit db3073
  g_set_error_literal (error,
Packit db3073
                       G_UNIX_ERROR,
Packit db3073
                       0,
Packit db3073
                       g_strerror (saved_errno));
Packit db3073
  errno = saved_errno;
Packit db3073
  return FALSE;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_unix_open_pipe:
Packit db3073
 * @fds: Array of two integers
Packit db3073
 * @flags: Bitfield of file descriptor flags, see "man 2 fcntl"
Packit db3073
 * @error: a #GError
Packit db3073
 *
Packit db3073
 * Similar to the UNIX pipe() call, but on modern systems like Linux
Packit db3073
 * uses the pipe2() system call, which atomically creates a pipe with
Packit db3073
 * the configured flags.  The only supported flag currently is
Packit db3073
 * <literal>FD_CLOEXEC</literal>.  If for example you want to configure
Packit db3073
 * <literal>O_NONBLOCK</literal>, that must still be done separately with
Packit db3073
 * fcntl().
Packit db3073
 *
Packit db3073
 * <note>This function does *not* take <literal>O_CLOEXEC</literal>, it takes
Packit db3073
 * <literal>FD_CLOEXEC</literal> as if for fcntl(); these are
Packit db3073
 * different on Linux/glibc.</note>
Packit db3073
 *
Packit db3073
 * Returns: %TRUE on success, %FALSE if not (and errno will be set).
Packit db3073
 *
Packit db3073
 * Since: 2.30
Packit db3073
 */
Packit db3073
gboolean
Packit db3073
g_unix_open_pipe (int     *fds,
Packit db3073
                  int      flags,
Packit db3073
                  GError **error)
Packit db3073
{
Packit db3073
  int ecode;
Packit db3073
Packit db3073
  /* We only support FD_CLOEXEC */
Packit db3073
  g_return_val_if_fail ((flags & (FD_CLOEXEC)) == flags, FALSE);
Packit db3073
Packit db3073
#ifdef HAVE_PIPE2
Packit db3073
  {
Packit db3073
    int pipe2_flags = 0;
Packit db3073
    if (flags & FD_CLOEXEC)
Packit db3073
      pipe2_flags |= O_CLOEXEC;
Packit db3073
    /* Atomic */
Packit db3073
    ecode = pipe2 (fds, pipe2_flags);
Packit db3073
    if (ecode == -1 && errno != ENOSYS)
Packit db3073
      return g_unix_set_error_from_errno (error, errno);
Packit db3073
    else if (ecode == 0)
Packit db3073
      return TRUE;
Packit db3073
    /* Fall through on -ENOSYS, we must be running on an old kernel */
Packit db3073
  }
Packit db3073
#endif
Packit db3073
  ecode = pipe (fds);
Packit db3073
  if (ecode == -1)
Packit db3073
    return g_unix_set_error_from_errno (error, errno);
Packit db3073
  ecode = fcntl (fds[0], flags);
Packit db3073
  if (ecode == -1)
Packit db3073
    {
Packit db3073
      int saved_errno = errno;
Packit db3073
      close (fds[0]);
Packit db3073
      close (fds[1]);
Packit db3073
      return g_unix_set_error_from_errno (error, saved_errno);
Packit db3073
    }
Packit db3073
  ecode = fcntl (fds[1], flags);
Packit db3073
  if (ecode == -1)
Packit db3073
    {
Packit db3073
      int saved_errno = errno;
Packit db3073
      close (fds[0]);
Packit db3073
      close (fds[1]);
Packit db3073
      return g_unix_set_error_from_errno (error, saved_errno);
Packit db3073
    }
Packit db3073
  return TRUE;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_unix_set_fd_nonblocking:
Packit db3073
 * @fd: A file descriptor
Packit db3073
 * @nonblock: If %TRUE, set the descriptor to be non-blocking
Packit db3073
 * @error: a #GError
Packit db3073
 *
Packit db3073
 * Control the non-blocking state of the given file descriptor,
Packit db3073
 * according to @nonblock.  On most systems this uses <literal>O_NONBLOCK</literal>, but
Packit db3073
 * on some older ones may use <literal>O_NDELAY</literal>.
Packit db3073
 *
Packit db3073
 * Returns: %TRUE if successful
Packit db3073
 *
Packit db3073
 * Since: 2.30
Packit db3073
 */
Packit db3073
gboolean
Packit db3073
g_unix_set_fd_nonblocking (gint       fd,
Packit db3073
                           gboolean   nonblock,
Packit db3073
                           GError   **error)
Packit db3073
{
Packit db3073
#ifdef F_GETFL
Packit db3073
  glong fcntl_flags;
Packit db3073
  fcntl_flags = fcntl (fd, F_GETFL);
Packit db3073
Packit db3073
  if (fcntl_flags == -1)
Packit db3073
    return g_unix_set_error_from_errno (error, errno);
Packit db3073
Packit db3073
  if (nonblock)
Packit db3073
    {
Packit db3073
#ifdef O_NONBLOCK
Packit db3073
      fcntl_flags |= O_NONBLOCK;
Packit db3073
#else
Packit db3073
      fcntl_flags |= O_NDELAY;
Packit db3073
#endif
Packit db3073
    }
Packit db3073
  else
Packit db3073
    {
Packit db3073
#ifdef O_NONBLOCK
Packit db3073
      fcntl_flags &= ~O_NONBLOCK;
Packit db3073
#else
Packit db3073
      fcntl_flags &= ~O_NDELAY;
Packit db3073
#endif
Packit db3073
    }
Packit db3073
Packit db3073
  if (fcntl (fd, F_SETFL, fcntl_flags) == -1)
Packit db3073
    return g_unix_set_error_from_errno (error, errno);
Packit db3073
  return TRUE;
Packit db3073
#else
Packit db3073
  return g_unix_set_error_from_errno (error, EINVAL);
Packit db3073
#endif
Packit db3073
}
Packit db3073
Packit db3073
Packit db3073
/**
Packit db3073
 * g_unix_signal_source_new:
Packit db3073
 * @signum: A signal number
Packit db3073
 *
Packit db3073
 * Create a #GSource that will be dispatched upon delivery of the UNIX
Packit db3073
 * signal @signum.  Currently only <literal>SIGHUP</literal>,
Packit db3073
 * <literal>SIGINT</literal>, and <literal>SIGTERM</literal> can
Packit db3073
 * be monitored.  Note that unlike the UNIX default, all sources which
Packit db3073
 * have created a watch will be dispatched, regardless of which
Packit db3073
 * underlying thread invoked g_unix_signal_source_new().
Packit db3073
 *
Packit db3073
 * For example, an effective use of this function is to handle <literal>SIGTERM</literal>
Packit db3073
 * cleanly; flushing any outstanding files, and then calling
Packit db3073
 * g_main_loop_quit ().  It is not safe to do any of this a regular
Packit db3073
 * UNIX signal handler; your handler may be invoked while malloc() or
Packit db3073
 * another library function is running, causing reentrancy if you
Packit db3073
 * attempt to use it from the handler.  None of the GLib/GObject API
Packit db3073
 * is safe against this kind of reentrancy.
Packit db3073
 *
Packit db3073
 * The interaction of this source when combined with native UNIX
Packit db3073
 * functions like sigprocmask() is not defined.
Packit db3073
 *
Packit db3073
 * The source will not initially be associated with any #GMainContext
Packit db3073
 * and must be added to one with g_source_attach() before it will be
Packit db3073
 * executed.
Packit db3073
 *
Packit db3073
 * Returns: A newly created #GSource
Packit db3073
 *
Packit db3073
 * Since: 2.30
Packit db3073
 */
Packit db3073
GSource *
Packit db3073
g_unix_signal_source_new (int signum)
Packit db3073
{
Packit db3073
  g_return_val_if_fail (signum == SIGHUP || signum == SIGINT || signum == SIGTERM, NULL);
Packit db3073
Packit db3073
  return _g_main_create_unix_signal_watch (signum);
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_unix_signal_add_full:
Packit db3073
 * @priority: the priority of the signal source. Typically this will be in
Packit db3073
 *            the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
Packit db3073
 * @signum: Signal number
Packit db3073
 * @handler: Callback
Packit db3073
 * @user_data: Data for @handler
Packit db3073
 * @notify: #GDestroyNotify for @handler
Packit db3073
 *
Packit db3073
 * A convenience function for g_unix_signal_source_new(), which
Packit db3073
 * attaches to the default #GMainContext.  You can remove the watch
Packit db3073
 * using g_source_remove().
Packit db3073
 *
Packit db3073
 * Returns: An ID (greater than 0) for the event source
Packit db3073
 *
Packit db3073
 * Since: 2.30
Packit db3073
 */
Packit db3073
guint
Packit db3073
g_unix_signal_add_full (int            priority,
Packit db3073
                        int            signum,
Packit db3073
                        GSourceFunc    handler,
Packit db3073
                        gpointer       user_data,
Packit db3073
                        GDestroyNotify notify)
Packit db3073
{
Packit db3073
  guint id;
Packit db3073
  GSource *source;
Packit db3073
Packit db3073
  source = g_unix_signal_source_new (signum);
Packit db3073
Packit db3073
  if (priority != G_PRIORITY_DEFAULT)
Packit db3073
    g_source_set_priority (source, priority);
Packit db3073
Packit db3073
  g_source_set_callback (source, handler, user_data, notify);
Packit db3073
  id = g_source_attach (source, NULL);
Packit db3073
  g_source_unref (source);
Packit db3073
Packit db3073
  return id;
Packit db3073
}
Packit db3073
Packit db3073
/**
Packit db3073
 * g_unix_signal_add:
Packit db3073
 * @signum: Signal number
Packit db3073
 * @handler: Callback
Packit db3073
 * @user_data: Data for @handler
Packit db3073
 *
Packit db3073
 * A convenience function for g_unix_signal_source_new(), which
Packit db3073
 * attaches to the default #GMainContext.  You can remove the watch
Packit db3073
 * using g_source_remove().
Packit db3073
 *
Packit db3073
 * Returns: An ID (greater than 0) for the event source
Packit db3073
 *
Packit db3073
 * Since: 2.30
Packit db3073
 */
Packit db3073
guint
Packit db3073
g_unix_signal_add (int         signum,
Packit db3073
                   GSourceFunc handler,
Packit db3073
                   gpointer    user_data)
Packit db3073
{
Packit db3073
  return g_unix_signal_add_full (G_PRIORITY_DEFAULT, signum, handler, user_data, NULL);
Packit db3073
}