Blame gio/gunixfdlist.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright © 2009 Codethink Limited
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
 * See the included COPYING file for more information.
Packit ae235b
 *
Packit ae235b
 * Authors: Ryan Lortie <desrt@desrt.ca>
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gunixfdlist
Packit ae235b
 * @title: GUnixFDList
Packit ae235b
 * @short_description: An object containing a set of UNIX file descriptors
Packit ae235b
 * @include: gio/gunixfdlist.h
Packit ae235b
 * @see_also: #GUnixFDMessage
Packit ae235b
 *
Packit ae235b
 * A #GUnixFDList contains a list of file descriptors.  It owns the file
Packit ae235b
 * descriptors that it contains, closing them when finalized.
Packit ae235b
 *
Packit ae235b
 * It may be wrapped in a #GUnixFDMessage and sent over a #GSocket in
Packit ae235b
 * the %G_SOCKET_ADDRESS_UNIX family by using g_socket_send_message()
Packit ae235b
 * and received using g_socket_receive_message().
Packit ae235b
 *
Packit ae235b
 * Note that `<gio/gunixfdlist.h>` belongs to the UNIX-specific GIO
Packit ae235b
 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
Packit ae235b
 * file when using it.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GUnixFDList:
Packit ae235b
 *
Packit ae235b
 * #GUnixFDList is an opaque data structure and can only be accessed
Packit ae235b
 * using the following functions.
Packit ae235b
 **/
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <errno.h>
Packit ae235b
Packit ae235b
#include "gunixfdlist.h"
Packit ae235b
#include "gnetworking.h"
Packit ae235b
#include "gioerror.h"
Packit ae235b
Packit ae235b
struct _GUnixFDListPrivate
Packit ae235b
{
Packit ae235b
  gint *fds;
Packit ae235b
  gint nfd;
Packit ae235b
};
Packit ae235b
Packit ae235b
G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDList, g_unix_fd_list, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_fd_list_init (GUnixFDList *list)
Packit ae235b
{
Packit ae235b
  list->priv = g_unix_fd_list_get_instance_private (list);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_fd_list_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GUnixFDList *list = G_UNIX_FD_LIST (object);
Packit ae235b
  gint i;
Packit ae235b
Packit ae235b
  for (i = 0; i < list->priv->nfd; i++)
Packit ae235b
    close (list->priv->fds[i]);
Packit ae235b
  g_free (list->priv->fds);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_unix_fd_list_parent_class)
Packit ae235b
    ->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_unix_fd_list_class_init (GUnixFDListClass *class)
Packit ae235b
{
Packit ae235b
  GObjectClass *object_class = G_OBJECT_CLASS (class);
Packit ae235b
Packit ae235b
  object_class->finalize = g_unix_fd_list_finalize;
Packit ae235b
}
Packit ae235b
Packit ae235b
static int
Packit ae235b
dup_close_on_exec_fd (gint     fd,
Packit ae235b
                      GError **error)
Packit ae235b
{
Packit ae235b
  gint new_fd;
Packit ae235b
  gint s;
Packit ae235b
Packit ae235b
#ifdef F_DUPFD_CLOEXEC
Packit ae235b
  do
Packit ae235b
    new_fd = fcntl (fd, F_DUPFD_CLOEXEC, 0l);
Packit ae235b
  while (new_fd < 0 && (errno == EINTR));
Packit ae235b
Packit ae235b
  if (new_fd >= 0)
Packit ae235b
    return new_fd;
Packit ae235b
Packit ae235b
  /* if that didn't work (new libc/old kernel?), try it the other way. */
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  do
Packit ae235b
    new_fd = dup (fd);
Packit ae235b
  while (new_fd < 0 && (errno == EINTR));
Packit ae235b
Packit ae235b
  if (new_fd < 0)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
                   g_io_error_from_errno (saved_errno),
Packit ae235b
                   "dup: %s", g_strerror (saved_errno));
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  do
Packit ae235b
    {
Packit ae235b
      s = fcntl (new_fd, F_GETFD);
Packit ae235b
Packit ae235b
      if (s >= 0)
Packit ae235b
        s = fcntl (new_fd, F_SETFD, (long) (s | FD_CLOEXEC));
Packit ae235b
    }
Packit ae235b
  while (s < 0 && (errno == EINTR));
Packit ae235b
Packit ae235b
  if (s < 0)
Packit ae235b
    {
Packit ae235b
      int saved_errno = errno;
Packit ae235b
Packit ae235b
      g_set_error (error, G_IO_ERROR,
Packit ae235b
                   g_io_error_from_errno (saved_errno),
Packit ae235b
                   "fcntl: %s", g_strerror (saved_errno));
Packit ae235b
      close (new_fd);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return new_fd;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_list_new:
Packit ae235b
 *
Packit ae235b
 * Creates a new #GUnixFDList containing no file descriptors.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GUnixFDList
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
GUnixFDList *
Packit ae235b
g_unix_fd_list_new (void)
Packit ae235b
{
Packit ae235b
  return g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_list_new_from_array:
Packit ae235b
 * @fds: (array length=n_fds): the initial list of file descriptors
Packit ae235b
 * @n_fds: the length of #fds, or -1
Packit ae235b
 *
Packit ae235b
 * Creates a new #GUnixFDList containing the file descriptors given in
Packit ae235b
 * @fds.  The file descriptors become the property of the new list and
Packit ae235b
 * may no longer be used by the caller.  The array itself is owned by
Packit ae235b
 * the caller.
Packit ae235b
 *
Packit ae235b
 * Each file descriptor in the array should be set to close-on-exec.
Packit ae235b
 *
Packit ae235b
 * If @n_fds is -1 then @fds must be terminated with -1.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GUnixFDList
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
GUnixFDList *
Packit ae235b
g_unix_fd_list_new_from_array (const gint *fds,
Packit ae235b
                               gint        n_fds)
Packit ae235b
{
Packit ae235b
  GUnixFDList *list;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (fds != NULL || n_fds == 0, NULL);
Packit ae235b
Packit ae235b
  if (n_fds == -1)
Packit ae235b
    for (n_fds = 0; fds[n_fds] != -1; n_fds++);
Packit ae235b
Packit ae235b
  list = g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
Packit ae235b
  list->priv->fds = g_new (gint, n_fds + 1);
Packit ae235b
  list->priv->nfd = n_fds;
Packit ae235b
Packit ae235b
  if (n_fds > 0)
Packit ae235b
    memcpy (list->priv->fds, fds, sizeof (gint) * n_fds);
Packit ae235b
  list->priv->fds[n_fds] = -1;
Packit ae235b
Packit ae235b
  return list;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_list_steal_fds:
Packit ae235b
 * @list: a #GUnixFDList
Packit ae235b
 * @length: (out) (optional): pointer to the length of the returned
Packit ae235b
 *     array, or %NULL
Packit ae235b
 *
Packit ae235b
 * Returns the array of file descriptors that is contained in this
Packit ae235b
 * object.
Packit ae235b
 *
Packit ae235b
 * After this call, the descriptors are no longer contained in
Packit ae235b
 * @list. Further calls will return an empty list (unless more
Packit ae235b
 * descriptors have been added).
Packit ae235b
 *
Packit ae235b
 * The return result of this function must be freed with g_free().
Packit ae235b
 * The caller is also responsible for closing all of the file
Packit ae235b
 * descriptors.  The file descriptors in the array are set to
Packit ae235b
 * close-on-exec.
Packit ae235b
 *
Packit ae235b
 * If @length is non-%NULL then it is set to the number of file
Packit ae235b
 * descriptors in the returned array. The returned array is also
Packit ae235b
 * terminated with -1.
Packit ae235b
 *
Packit ae235b
 * This function never returns %NULL. In case there are no file
Packit ae235b
 * descriptors contained in @list, an empty array is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=length) (transfer full): an array of file
Packit ae235b
 *     descriptors
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 */
Packit ae235b
gint *
Packit ae235b
g_unix_fd_list_steal_fds (GUnixFDList *list,
Packit ae235b
                          gint        *length)
Packit ae235b
{
Packit ae235b
  gint *result;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
Packit ae235b
Packit ae235b
  /* will be true for fresh object or if we were just called */
Packit ae235b
  if (list->priv->fds == NULL)
Packit ae235b
    {
Packit ae235b
      list->priv->fds = g_new (gint, 1);
Packit ae235b
      list->priv->fds[0] = -1;
Packit ae235b
      list->priv->nfd = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (length)
Packit ae235b
    *length = list->priv->nfd;
Packit ae235b
  result = list->priv->fds;
Packit ae235b
Packit ae235b
  list->priv->fds = NULL;
Packit ae235b
  list->priv->nfd = 0;
Packit ae235b
Packit ae235b
  return result;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_list_peek_fds:
Packit ae235b
 * @list: a #GUnixFDList
Packit ae235b
 * @length: (out) (optional): pointer to the length of the returned
Packit ae235b
 *     array, or %NULL
Packit ae235b
 *
Packit ae235b
 * Returns the array of file descriptors that is contained in this
Packit ae235b
 * object.
Packit ae235b
 *
Packit ae235b
 * After this call, the descriptors remain the property of @list.  The
Packit ae235b
 * caller must not close them and must not free the array.  The array is
Packit ae235b
 * valid only until @list is changed in any way.
Packit ae235b
 *
Packit ae235b
 * If @length is non-%NULL then it is set to the number of file
Packit ae235b
 * descriptors in the returned array. The returned array is also
Packit ae235b
 * terminated with -1.
Packit ae235b
 *
Packit ae235b
 * This function never returns %NULL. In case there are no file
Packit ae235b
 * descriptors contained in @list, an empty array is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: (array length=length) (transfer none): an array of file
Packit ae235b
 *     descriptors
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 */
Packit ae235b
const gint *
Packit ae235b
g_unix_fd_list_peek_fds (GUnixFDList *list,
Packit ae235b
                         gint        *length)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
Packit ae235b
Packit ae235b
  /* will be true for fresh object or if steal() was just called */
Packit ae235b
  if (list->priv->fds == NULL)
Packit ae235b
    {
Packit ae235b
      list->priv->fds = g_new (gint, 1);
Packit ae235b
      list->priv->fds[0] = -1;
Packit ae235b
      list->priv->nfd = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (length)
Packit ae235b
    *length = list->priv->nfd;
Packit ae235b
Packit ae235b
  return list->priv->fds;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_list_append:
Packit ae235b
 * @list: a #GUnixFDList
Packit ae235b
 * @fd: a valid open file descriptor
Packit ae235b
 * @error: a #GError pointer
Packit ae235b
 *
Packit ae235b
 * Adds a file descriptor to @list.
Packit ae235b
 *
Packit ae235b
 * The file descriptor is duplicated using dup(). You keep your copy
Packit ae235b
 * of the descriptor and the copy contained in @list will be closed
Packit ae235b
 * when @list is finalized.
Packit ae235b
 *
Packit ae235b
 * A possible cause of failure is exceeding the per-process or
Packit ae235b
 * system-wide file descriptor limit.
Packit ae235b
 *
Packit ae235b
 * The index of the file descriptor in the list is returned.  If you use
Packit ae235b
 * this index with g_unix_fd_list_get() then you will receive back a
Packit ae235b
 * duplicated copy of the same file descriptor.
Packit ae235b
 *
Packit ae235b
 * Returns: the index of the appended fd in case of success, else -1
Packit ae235b
 *          (and @error is set)
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 */
Packit ae235b
gint
Packit ae235b
g_unix_fd_list_append (GUnixFDList  *list,
Packit ae235b
                       gint          fd,
Packit ae235b
                       GError      **error)
Packit ae235b
{
Packit ae235b
  gint new_fd;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
Packit ae235b
  g_return_val_if_fail (fd >= 0, -1);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
Packit ae235b
Packit ae235b
  if ((new_fd = dup_close_on_exec_fd (fd, error)) < 0)
Packit ae235b
    return -1;
Packit ae235b
Packit ae235b
  list->priv->fds = g_realloc (list->priv->fds,
Packit ae235b
                                  sizeof (gint) *
Packit ae235b
                                   (list->priv->nfd + 2));
Packit ae235b
  list->priv->fds[list->priv->nfd++] = new_fd;
Packit ae235b
  list->priv->fds[list->priv->nfd] = -1;
Packit ae235b
Packit ae235b
  return list->priv->nfd - 1;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_list_get:
Packit ae235b
 * @list: a #GUnixFDList
Packit ae235b
 * @index_: the index into the list
Packit ae235b
 * @error: a #GError pointer
Packit ae235b
 *
Packit ae235b
 * Gets a file descriptor out of @list.
Packit ae235b
 *
Packit ae235b
 * @index_ specifies the index of the file descriptor to get.  It is a
Packit ae235b
 * programmer error for @index_ to be out of range; see
Packit ae235b
 * g_unix_fd_list_get_length().
Packit ae235b
 *
Packit ae235b
 * The file descriptor is duplicated using dup() and set as
Packit ae235b
 * close-on-exec before being returned.  You must call close() on it
Packit ae235b
 * when you are done.
Packit ae235b
 *
Packit ae235b
 * A possible cause of failure is exceeding the per-process or
Packit ae235b
 * system-wide file descriptor limit.
Packit ae235b
 *
Packit ae235b
 * Returns: the file descriptor, or -1 in case of error
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_unix_fd_list_get (GUnixFDList  *list,
Packit ae235b
                    gint          index_,
Packit ae235b
                    GError      **error)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
Packit ae235b
  g_return_val_if_fail (index_ < list->priv->nfd, -1);
Packit ae235b
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
Packit ae235b
Packit ae235b
  return dup_close_on_exec_fd (list->priv->fds[index_], error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_unix_fd_list_get_length:
Packit ae235b
 * @list: a #GUnixFDList
Packit ae235b
 *
Packit ae235b
 * Gets the length of @list (ie: the number of file descriptors
Packit ae235b
 * contained within).
Packit ae235b
 *
Packit ae235b
 * Returns: the length of @list
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_unix_fd_list_get_length (GUnixFDList *list)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), 0);
Packit ae235b
Packit ae235b
  return list->priv->nfd;
Packit ae235b
}