gunixfdmessage.c 6.88 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright © 2009 Codethink Limited
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2 of the licence or (at
 * your option) any later version.
 *
 * See the included COPYING file for more information.
 *
 * Authors: Ryan Lortie <desrt@desrt.ca>
 */

/**
 * SECTION: gunixfdmessage
 * @title: GUnixFDMessage
 * @short_description: a #GSocketControlMessage containing a list of
 * file descriptors
 * @see_also: #GUnixConnection
 *
 * This #GSocketControlMessage contains a list of file descriptors.
 * It may be sent using g_socket_send_message() and received using
 * g_socket_receive_message() over UNIX sockets (ie: sockets in the
 * %G_SOCKET_ADDRESS_UNIX family).
 *
 * For an easier way to send and receive file descriptors over
 * stream-oriented UNIX sockets, see g_unix_connection_send_fd() and
 * g_unix_connection_receive_fd().
 **/

#include "config.h"

#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#include "gunixfdmessage.h"
#include "gioerror.h"

#include "gioalias.h"


G_DEFINE_TYPE (GUnixFDMessage, g_unix_fd_message,
               G_TYPE_SOCKET_CONTROL_MESSAGE);

struct _GUnixFDMessagePrivate
{
  gint *fds;
  gint nfd;
};

static gsize
g_unix_fd_message_get_size (GSocketControlMessage *message)
{
  GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);

  return fd_message->priv->nfd * sizeof (gint);
}

static int
g_unix_fd_message_get_level (GSocketControlMessage *message)
{
  return SOL_SOCKET;
}

static int
g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
{
  return SCM_RIGHTS;
}

static GSocketControlMessage *
g_unix_fd_message_deserialize (int level,
			       int type,
			       gsize size,
			       gpointer data)
{
  GUnixFDMessage *message;

  if (level != SOL_SOCKET ||
      level != SCM_RIGHTS)
    return NULL;
  
  if (size % 4 > 0)
    {
      g_warning ("Kernel returned non-integral number of fds");
      return NULL;
    }

  message = g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
  message->priv->nfd = size / sizeof (gint);
  message->priv->fds = g_new (gint, message->priv->nfd + 1);
  memcpy (message->priv->fds, data, size);
  message->priv->fds[message->priv->nfd] = -1;

  return G_SOCKET_CONTROL_MESSAGE (message);
}

static void
g_unix_fd_message_serialize (GSocketControlMessage *message,
			     gpointer               data)
{
  GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
  memcpy (data, fd_message->priv->fds,
	  sizeof (gint) * fd_message->priv->nfd);
}
static void
g_unix_fd_message_init (GUnixFDMessage *message)
{
  message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
                                               G_TYPE_UNIX_FD_MESSAGE,
                                               GUnixFDMessagePrivate);
}

static void
g_unix_fd_message_finalize (GObject *object)
{
  GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
  gint i;

  for (i = 0; i < message->priv->nfd; i++)
    close (message->priv->fds[i]);
  g_free (message->priv->fds);

  G_OBJECT_CLASS (g_unix_fd_message_parent_class)
    ->finalize (object);
}

static void
g_unix_fd_message_class_init (GUnixFDMessageClass *class)
{
  GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
  GObjectClass *object_class = G_OBJECT_CLASS (class);

  g_type_class_add_private (class, sizeof (GUnixFDMessagePrivate));
  scm_class->get_size = g_unix_fd_message_get_size;
  scm_class->get_level = g_unix_fd_message_get_level;
  scm_class->get_type = g_unix_fd_message_get_msg_type;
  scm_class->serialize = g_unix_fd_message_serialize;
  scm_class->deserialize = g_unix_fd_message_deserialize;
  object_class->finalize = g_unix_fd_message_finalize;
}

/**
 * g_unix_fd_message_new:
 * @returns: a new #GUnixFDMessage
 *
 * Creates a new #GUnixFDMessage containing no file descriptors.
 **/
GSocketControlMessage *
g_unix_fd_message_new (void)
{
  return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
}

/**
 * g_unix_fd_message_steal_fds:
 * @message: a #GUnixFDMessage
 * @length: pointer to the length of the returned array, or %NULL
 * @returns: an array of file descriptors
 *
 * Returns the array of file descriptors that is contained in this
 * object.
 *
 * After this call, the descriptors are no longer contained in
 * @message.  Further calls will return an empty list (unless more
 * descriptors have been added).
 *
 * The return result of this function must be freed with g_free().
 * The caller is also responsible for closing all of the file
 * descriptors.
 *
 * If @length is non-%NULL then it is set to the number of file
 * descriptors in the returned array.  The returned array is also
 * terminated with -1.
 *
 * This function never returns NULL.  In case there are no file
 * descriptors contained in @message, an empty array is returned.
 **/
gint *
g_unix_fd_message_steal_fds (GUnixFDMessage *message,
                               gint             *length)
{
  gint *result;

  g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), NULL);

  /* will be true for fresh object or if we were just called */
  if (message->priv->fds == NULL)
    {
      message->priv->fds = g_new (gint, 1);
      message->priv->fds[0] = -1;
      message->priv->nfd = 0;
    }

  if (length)
    *length = message->priv->nfd;
  result = message->priv->fds;

  message->priv->fds = NULL;
  message->priv->nfd = 0;

  return result;
}

/**
 * g_unix_fd_message_append_fd:
 * @message: a #GUnixFDMessage
 * @fd: a valid open file descriptor
 * @error: a #GError pointer
 * @returns: %TRUE in case of success, else %FALSE (and @error is set)
 *
 * Adds a file descriptor to @message.
 *
 * The file descriptor is duplicated using dup().  You keep your copy
 * of the descriptor and the copy contained in @message will be closed
 * when @message is finalized.
 *
 * A possible cause of failure is exceeding the per-process or
 * system-wide file descriptor limit.
 **/
gboolean
g_unix_fd_message_append_fd (GUnixFDMessage  *message,
                               gint               fd,
                               GError           **error)
{
  gint new_fd;

  g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), FALSE);
  g_return_val_if_fail (fd >= 0, FALSE);

  do
    new_fd = dup (fd);
  while (new_fd < 0 && (errno == EINTR));

  if (fd < 0)
    {
      int saved_errno = errno;

      g_set_error (error, G_IO_ERROR,
                   g_io_error_from_errno (saved_errno),
                   "dup: %s", g_strerror (saved_errno));

      return FALSE;
    }

  message->priv->fds = g_realloc (message->priv->fds,
                                  sizeof (gint) *
                                   (message->priv->nfd + 2));
  message->priv->fds[message->priv->nfd++] = new_fd;
  message->priv->fds[message->priv->nfd] = -1;

  return TRUE;
}

#define __G_UNIX_FD_MESSAGE_C__
#include "gioaliasdef.c"