gunixfdmessage.c 8.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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>
 */

/**
16
 * SECTION:gunixfdmessage
17
 * @title: GUnixFDMessage
Matthias Clasen's avatar
Matthias Clasen committed
18 19
 * @short_description: A GSocketControlMessage containing a GUnixFDList
 * @include: gio/gunixfdmessage.h
20
 * @see_also: #GUnixConnection, #GUnixFDList, #GSocketControlMessage
21
 *
Matthias Clasen's avatar
Matthias Clasen committed
22 23
 * This #GSocketControlMessage contains a #GUnixFDList.
 * It may be sent using g_socket_send_message() and received using
24
 * g_socket_receive_message() over UNIX sockets (ie: sockets in the
Matthias Clasen's avatar
Matthias Clasen committed
25
 * %G_SOCKET_ADDRESS_UNIX family). The file descriptors are copied
26
 * between processes by the kernel.
27 28 29 30
 *
 * 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().
Matthias Clasen's avatar
Matthias Clasen committed
31
 *
32
 * Note that `<gio/gunixfdmessage.h>` belongs to the UNIX-specific GIO
33 34 35
 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
 * file when using it.
 */
36

37 38 39 40 41 42 43
/**
 * GUnixFDMessage:
 *
 * #GUnixFDMessage is an opaque data structure and can only be accessed
 * using the following functions.
 **/

44 45 46 47
#include "config.h"

#include <unistd.h>
#include <string.h>
48
#include <fcntl.h>
49 50 51
#include <errno.h>

#include "gunixfdmessage.h"
52
#include "gunixfdlist.h"
Dan Winship's avatar
Dan Winship committed
53
#include "gnetworking.h"
54 55 56 57
#include "gioerror.h"

struct _GUnixFDMessagePrivate
{
58
  GUnixFDList *list;
59 60
};

61 62
G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDMessage, g_unix_fd_message, G_TYPE_SOCKET_CONTROL_MESSAGE)

63 64 65 66 67
static gsize
g_unix_fd_message_get_size (GSocketControlMessage *message)
{
  GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);

68
  return g_unix_fd_list_get_length (fd_message->priv->list) * sizeof (gint);
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
}

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 *
84 85 86
g_unix_fd_message_deserialize (int      level,
			       int      type,
			       gsize    size,
87 88
			       gpointer data)
{
89 90 91 92
  GSocketControlMessage *message;
  GUnixFDList *list;
  gint n, s, i;
  gint *fds;
93 94

  if (level != SOL_SOCKET ||
95
      type != SCM_RIGHTS)
96 97 98 99 100 101 102 103
    return NULL;
  
  if (size % 4 > 0)
    {
      g_warning ("Kernel returned non-integral number of fds");
      return NULL;
    }

104 105 106
  fds = data;
  n = size / sizeof (gint);

107 108 109 110
  /* Note we probably handled this in gsocket.c already if we're on
   * Linux and have MSG_CMSG_CLOEXEC, but this code remains as a fallback
   * in case the kernel is too old for MSG_CMSG_CLOEXEC.
   */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  for (i = 0; i < n; i++)
    {
      do
        s = fcntl (fds[i], F_SETFD, FD_CLOEXEC);
      while (s < 0 && errno == EINTR);

      if (s < 0)
        {
          g_warning ("Error setting close-on-exec flag on incoming fd: %s",
                     g_strerror (errno));
          return NULL;
        }
    }

  list = g_unix_fd_list_new_from_array (fds, n);
  message = g_unix_fd_message_new_with_fd_list (list);
  g_object_unref (list);
128

129
  return message;
130 131 132 133 134 135 136
}

static void
g_unix_fd_message_serialize (GSocketControlMessage *message,
			     gpointer               data)
{
  GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
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
  const gint *fds;
  gint n_fds;

  fds = g_unix_fd_list_peek_fds (fd_message->priv->list, &n_fds);
  memcpy (data, fds, sizeof (gint) * n_fds);
}

static void
g_unix_fd_message_set_property (GObject *object, guint prop_id,
                                const GValue *value, GParamSpec *pspec)
{
  GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);

  g_assert (message->priv->list == NULL);
  g_assert_cmpint (prop_id, ==, 1);

  message->priv->list = g_value_dup_object (value);

  if (message->priv->list == NULL)
    message->priv->list = g_unix_fd_list_new ();
}

/**
 * g_unix_fd_message_get_fd_list:
 * @message: a #GUnixFDMessage
 *
 * Gets the #GUnixFDList contained in @message.  This function does not
 * return a reference to the caller, but the returned list is valid for
 * the lifetime of @message.
 *
167
 * Returns: (transfer none): the #GUnixFDList from @message
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
 *
 * Since: 2.24
 **/
GUnixFDList *
g_unix_fd_message_get_fd_list (GUnixFDMessage *message)
{
  return message->priv->list;
}

static void
g_unix_fd_message_get_property (GObject *object, guint prop_id,
                                GValue *value, GParamSpec *pspec)
{
  GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);

  g_assert_cmpint (prop_id, ==, 1);

  g_value_set_object (value, g_unix_fd_message_get_fd_list (message));
186
}
187

188 189 190
static void
g_unix_fd_message_init (GUnixFDMessage *message)
{
191
  message->priv = g_unix_fd_message_get_instance_private (message);
192 193 194 195 196 197 198
}

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

199
  g_object_unref (message->priv->list);
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216

  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);

  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;
217 218 219 220 221 222 223 224
  object_class->set_property = g_unix_fd_message_set_property;
  object_class->get_property = g_unix_fd_message_get_property;

  g_object_class_install_property (object_class, 1,
    g_param_spec_object ("fd-list", "file descriptor list",
                         "The GUnixFDList object to send with the message",
                         G_TYPE_UNIX_FD_LIST, G_PARAM_STATIC_STRINGS |
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
225 226 227 228 229
}

/**
 * g_unix_fd_message_new:
 *
230 231
 * Creates a new #GUnixFDMessage containing an empty file descriptor
 * list.
232 233 234 235
 *
 * Returns: a new #GUnixFDMessage
 *
 * Since: 2.22
236
 **/
237 238 239 240 241 242
GSocketControlMessage *
g_unix_fd_message_new (void)
{
  return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
}

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
/**
 * g_unix_fd_message_new_with_fd_list:
 * @fd_list: a #GUnixFDList
 *
 * Creates a new #GUnixFDMessage containing @list.
 *
 * Returns: a new #GUnixFDMessage
 *
 * Since: 2.24
 **/
GSocketControlMessage *
g_unix_fd_message_new_with_fd_list (GUnixFDList *fd_list)
{
  return g_object_new (G_TYPE_UNIX_FD_MESSAGE,
                       "fd-list", fd_list,
                       NULL);
}

261 262 263
/**
 * g_unix_fd_message_steal_fds:
 * @message: a #GUnixFDMessage
264 265
 * @length: (out) (allow-none): pointer to the length of the returned
 *     array, or %NULL
266 267 268 269 270
 *
 * Returns the array of file descriptors that is contained in this
 * object.
 *
 * After this call, the descriptors are no longer contained in
271
 * @message. Further calls will return an empty list (unless more
272 273 274 275 276 277 278
 * 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
279
 * descriptors in the returned array. The returned array is also
280 281
 * terminated with -1.
 *
282
 * This function never returns %NULL. In case there are no file
283
 * descriptors contained in @message, an empty array is returned.
284
 *
285 286
 * Returns: (array length=length) (transfer full): an array of file
 *     descriptors
287 288
 *
 * Since: 2.22
289
 **/
290 291
gint *
g_unix_fd_message_steal_fds (GUnixFDMessage *message,
292
                             gint           *length)
293
{
Matthias Clasen's avatar
Matthias Clasen committed
294
  g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), NULL);
295

296
  return g_unix_fd_list_steal_fds (message->priv->list, length);
297 298 299 300 301 302 303 304 305 306
}

/**
 * g_unix_fd_message_append_fd:
 * @message: a #GUnixFDMessage
 * @fd: a valid open file descriptor
 * @error: a #GError pointer
 *
 * Adds a file descriptor to @message.
 *
307
 * The file descriptor is duplicated using dup(). You keep your copy
308 309 310 311 312
 * 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.
313 314 315 316
 *
 * Returns: %TRUE in case of success, else %FALSE (and @error is set)
 *
 * Since: 2.22
317
 **/
318 319
gboolean
g_unix_fd_message_append_fd (GUnixFDMessage  *message,
320 321
                             gint             fd,
                             GError         **error)
322
{
323
  g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);
324

325
  return g_unix_fd_list_append (message->priv->list, fd, error) >= 0;
326
}