summaryrefslogtreecommitdiff
path: root/gio/gunixfdmessage.c
blob: 889a0c9214bc5d34e372134247e9c6976d34dfb0 (plain)
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright © 2009 Codethink Limited
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library 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.1 of the License, 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 GUnixFDList
 * @include: gio/gunixfdmessage.h
 * @see_also: #GUnixConnection, #GUnixFDList, #GSocketControlMessage
 *
 * This #GSocketControlMessage contains a #GUnixFDList.
 * 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_FAMILY_UNIX family). The file descriptors are copied
 * between processes by the kernel.
 *
 * 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().
 *
 * Note that `<gio/gunixfdmessage.h>` belongs to the UNIX-specific GIO
 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
 * file when using it.
 */

/**
 * GUnixFDMessage:
 *
 * #GUnixFDMessage is an opaque data structure and can only be accessed
 * using the following functions.
 **/

#include "config.h"

#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

#include "gunixfdmessage.h"
#include "gunixfdlist.h"
#include "gnetworking.h"
#include "gioerror.h"

struct _GUnixFDMessagePrivate
{
  GUnixFDList *list;
};

G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDMessage, g_unix_fd_message, G_TYPE_SOCKET_CONTROL_MESSAGE)

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

  return g_unix_fd_list_get_length (fd_message->priv->list) * 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)
{
  GSocketControlMessage *message;
  GUnixFDList *list;
  gint n, s, i;
  gint *fds;

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

  fds = data;
  n = size / sizeof (gint);

  /* 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.
   */
  for (i = 0; i < n; i++)
    {
      int errsv;

      do
        {
          s = fcntl (fds[i], F_SETFD, FD_CLOEXEC);
          errsv = errno;
        }
      while (s < 0 && errsv == EINTR);

      if (s < 0)
        {
          g_warning ("Error setting close-on-exec flag on incoming fd: %s",
                     g_strerror (errsv));
          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);

  return message;
}

static void
g_unix_fd_message_serialize (GSocketControlMessage *message,
			     gpointer               data)
{
  GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
  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.
 *
 * Returns: (transfer none): the #GUnixFDList from @message
 *
 * 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));
}

static void
g_unix_fd_message_init (GUnixFDMessage *message)
{
  message->priv = g_unix_fd_message_get_instance_private (message);
}

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

  g_object_unref (message->priv->list);

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

/**
 * g_unix_fd_message_new:
 *
 * Creates a new #GUnixFDMessage containing an empty file descriptor
 * list.
 *
 * Returns: a new #GUnixFDMessage
 *
 * Since: 2.22
 **/
GSocketControlMessage *
g_unix_fd_message_new (void)
{
  return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
}

/**
 * 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);
}

/**
 * g_unix_fd_message_steal_fds:
 * @message: a #GUnixFDMessage
 * @length: (out) (optional): pointer to the length of the returned
 *     array, or %NULL
 *
 * 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.
 *
 * Returns: (array length=length) (transfer full): an array of file
 *     descriptors
 *
 * Since: 2.22
 **/
gint *
g_unix_fd_message_steal_fds (GUnixFDMessage *message,
                             gint           *length)
{
  g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), NULL);

  return g_unix_fd_list_steal_fds (message->priv->list, length);
}

/**
 * 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.
 *
 * 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.
 *
 * Returns: %TRUE in case of success, else %FALSE (and @error is set)
 *
 * Since: 2.22
 **/
gboolean
g_unix_fd_message_append_fd (GUnixFDMessage  *message,
                             gint             fd,
                             GError         **error)
{
  g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);

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