Coverage Report

Created: 2025-07-01 07:09

/src/glib/gio/gunixfdmessage.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright © 2009 Codethink Limited
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2.1 of the License, or (at your option) any later version.
9
 *
10
 * See the included COPYING file for more information.
11
 *
12
 * Authors: Ryan Lortie <desrt@desrt.ca>
13
 */
14
15
/**
16
 * SECTION:gunixfdmessage
17
 * @title: GUnixFDMessage
18
 * @short_description: A GSocketControlMessage containing a GUnixFDList
19
 * @include: gio/gunixfdmessage.h
20
 * @see_also: #GUnixConnection, #GUnixFDList, #GSocketControlMessage
21
 *
22
 * This #GSocketControlMessage contains a #GUnixFDList.
23
 * It may be sent using g_socket_send_message() and received using
24
 * g_socket_receive_message() over UNIX sockets (ie: sockets in the
25
 * %G_SOCKET_FAMILY_UNIX family). The file descriptors are copied
26
 * between processes by the kernel.
27
 *
28
 * For an easier way to send and receive file descriptors over
29
 * stream-oriented UNIX sockets, see g_unix_connection_send_fd() and
30
 * g_unix_connection_receive_fd().
31
 *
32
 * Note that `<gio/gunixfdmessage.h>` belongs to the UNIX-specific GIO
33
 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
34
 * file when using it.
35
 */
36
37
/**
38
 * GUnixFDMessage:
39
 *
40
 * #GUnixFDMessage is an opaque data structure and can only be accessed
41
 * using the following functions.
42
 **/
43
44
#include "config.h"
45
46
#include <unistd.h>
47
#include <string.h>
48
#include <fcntl.h>
49
#include <errno.h>
50
51
#include "gunixfdmessage.h"
52
#include "gunixfdlist.h"
53
#include "gnetworking.h"
54
#include "gioerror.h"
55
56
struct _GUnixFDMessagePrivate
57
{
58
  GUnixFDList *list;
59
};
60
61
G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDMessage, g_unix_fd_message, G_TYPE_SOCKET_CONTROL_MESSAGE)
62
63
static gsize
64
g_unix_fd_message_get_size (GSocketControlMessage *message)
65
0
{
66
0
  GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
67
68
0
  return g_unix_fd_list_get_length (fd_message->priv->list) * sizeof (gint);
69
0
}
70
71
static int
72
g_unix_fd_message_get_level (GSocketControlMessage *message)
73
0
{
74
0
  return SOL_SOCKET;
75
0
}
76
77
static int
78
g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
79
0
{
80
0
  return SCM_RIGHTS;
81
0
}
82
83
static GSocketControlMessage *
84
g_unix_fd_message_deserialize (int      level,
85
             int      type,
86
             gsize    size,
87
             gpointer data)
88
0
{
89
0
  GSocketControlMessage *message;
90
0
  GUnixFDList *list;
91
0
  gint n, s, i;
92
0
  gint *fds;
93
94
0
  if (level != SOL_SOCKET ||
95
0
      type != SCM_RIGHTS)
96
0
    return NULL;
97
  
98
0
  if (size % 4 > 0)
99
0
    {
100
0
      g_warning ("Kernel returned non-integral number of fds");
101
0
      return NULL;
102
0
    }
103
104
0
  fds = data;
105
0
  n = size / sizeof (gint);
106
107
  /* Note we probably handled this in gsocket.c already if we're on
108
   * Linux and have MSG_CMSG_CLOEXEC, but this code remains as a fallback
109
   * in case the kernel is too old for MSG_CMSG_CLOEXEC.
110
   */
111
0
  for (i = 0; i < n; i++)
112
0
    {
113
0
      int errsv;
114
115
0
      do
116
0
        {
117
0
          s = fcntl (fds[i], F_SETFD, FD_CLOEXEC);
118
0
          errsv = errno;
119
0
        }
120
0
      while (s < 0 && errsv == EINTR);
121
122
0
      if (s < 0)
123
0
        {
124
0
          g_warning ("Error setting close-on-exec flag on incoming fd: %s",
125
0
                     g_strerror (errsv));
126
0
          return NULL;
127
0
        }
128
0
    }
129
130
0
  list = g_unix_fd_list_new_from_array (fds, n);
131
0
  message = g_unix_fd_message_new_with_fd_list (list);
132
0
  g_object_unref (list);
133
134
0
  return message;
135
0
}
136
137
static void
138
g_unix_fd_message_serialize (GSocketControlMessage *message,
139
           gpointer               data)
140
0
{
141
0
  GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
142
0
  const gint *fds;
143
0
  gint n_fds;
144
145
0
  fds = g_unix_fd_list_peek_fds (fd_message->priv->list, &n_fds);
146
0
  memcpy (data, fds, sizeof (gint) * n_fds);
147
0
}
148
149
static void
150
g_unix_fd_message_set_property (GObject *object, guint prop_id,
151
                                const GValue *value, GParamSpec *pspec)
152
0
{
153
0
  GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
154
155
0
  g_assert (message->priv->list == NULL);
156
0
  g_assert_cmpint (prop_id, ==, 1);
157
158
0
  message->priv->list = g_value_dup_object (value);
159
160
0
  if (message->priv->list == NULL)
161
0
    message->priv->list = g_unix_fd_list_new ();
162
0
}
163
164
/**
165
 * g_unix_fd_message_get_fd_list:
166
 * @message: a #GUnixFDMessage
167
 *
168
 * Gets the #GUnixFDList contained in @message.  This function does not
169
 * return a reference to the caller, but the returned list is valid for
170
 * the lifetime of @message.
171
 *
172
 * Returns: (transfer none): the #GUnixFDList from @message
173
 *
174
 * Since: 2.24
175
 **/
176
GUnixFDList *
177
g_unix_fd_message_get_fd_list (GUnixFDMessage *message)
178
0
{
179
0
  return message->priv->list;
180
0
}
181
182
static void
183
g_unix_fd_message_get_property (GObject *object, guint prop_id,
184
                                GValue *value, GParamSpec *pspec)
185
0
{
186
0
  GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
187
188
0
  g_assert_cmpint (prop_id, ==, 1);
189
190
0
  g_value_set_object (value, g_unix_fd_message_get_fd_list (message));
191
0
}
192
193
static void
194
g_unix_fd_message_init (GUnixFDMessage *message)
195
0
{
196
0
  message->priv = g_unix_fd_message_get_instance_private (message);
197
0
}
198
199
static void
200
g_unix_fd_message_finalize (GObject *object)
201
0
{
202
0
  GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
203
204
0
  g_object_unref (message->priv->list);
205
206
0
  G_OBJECT_CLASS (g_unix_fd_message_parent_class)
207
0
    ->finalize (object);
208
0
}
209
210
static void
211
g_unix_fd_message_class_init (GUnixFDMessageClass *class)
212
0
{
213
0
  GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
214
0
  GObjectClass *object_class = G_OBJECT_CLASS (class);
215
216
0
  scm_class->get_size = g_unix_fd_message_get_size;
217
0
  scm_class->get_level = g_unix_fd_message_get_level;
218
0
  scm_class->get_type = g_unix_fd_message_get_msg_type;
219
0
  scm_class->serialize = g_unix_fd_message_serialize;
220
0
  scm_class->deserialize = g_unix_fd_message_deserialize;
221
0
  object_class->finalize = g_unix_fd_message_finalize;
222
0
  object_class->set_property = g_unix_fd_message_set_property;
223
0
  object_class->get_property = g_unix_fd_message_get_property;
224
225
0
  g_object_class_install_property (object_class, 1,
226
0
    g_param_spec_object ("fd-list", "file descriptor list",
227
0
                         "The GUnixFDList object to send with the message",
228
0
                         G_TYPE_UNIX_FD_LIST, G_PARAM_STATIC_STRINGS |
229
0
                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
230
0
}
231
232
/**
233
 * g_unix_fd_message_new:
234
 *
235
 * Creates a new #GUnixFDMessage containing an empty file descriptor
236
 * list.
237
 *
238
 * Returns: a new #GUnixFDMessage
239
 *
240
 * Since: 2.22
241
 **/
242
GSocketControlMessage *
243
g_unix_fd_message_new (void)
244
0
{
245
0
  return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
246
0
}
247
248
/**
249
 * g_unix_fd_message_new_with_fd_list:
250
 * @fd_list: a #GUnixFDList
251
 *
252
 * Creates a new #GUnixFDMessage containing @list.
253
 *
254
 * Returns: a new #GUnixFDMessage
255
 *
256
 * Since: 2.24
257
 **/
258
GSocketControlMessage *
259
g_unix_fd_message_new_with_fd_list (GUnixFDList *fd_list)
260
0
{
261
0
  return g_object_new (G_TYPE_UNIX_FD_MESSAGE,
262
0
                       "fd-list", fd_list,
263
0
                       NULL);
264
0
}
265
266
/**
267
 * g_unix_fd_message_steal_fds:
268
 * @message: a #GUnixFDMessage
269
 * @length: (out) (optional): pointer to the length of the returned
270
 *     array, or %NULL
271
 *
272
 * Returns the array of file descriptors that is contained in this
273
 * object.
274
 *
275
 * After this call, the descriptors are no longer contained in
276
 * @message. Further calls will return an empty list (unless more
277
 * descriptors have been added).
278
 *
279
 * The return result of this function must be freed with g_free().
280
 * The caller is also responsible for closing all of the file
281
 * descriptors.
282
 *
283
 * If @length is non-%NULL then it is set to the number of file
284
 * descriptors in the returned array. The returned array is also
285
 * terminated with -1.
286
 *
287
 * This function never returns %NULL. In case there are no file
288
 * descriptors contained in @message, an empty array is returned.
289
 *
290
 * Returns: (array length=length) (transfer full): an array of file
291
 *     descriptors
292
 *
293
 * Since: 2.22
294
 **/
295
gint *
296
g_unix_fd_message_steal_fds (GUnixFDMessage *message,
297
                             gint           *length)
298
0
{
299
0
  g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), NULL);
300
301
0
  return g_unix_fd_list_steal_fds (message->priv->list, length);
302
0
}
303
304
/**
305
 * g_unix_fd_message_append_fd:
306
 * @message: a #GUnixFDMessage
307
 * @fd: a valid open file descriptor
308
 * @error: a #GError pointer
309
 *
310
 * Adds a file descriptor to @message.
311
 *
312
 * The file descriptor is duplicated using dup(). You keep your copy
313
 * of the descriptor and the copy contained in @message will be closed
314
 * when @message is finalized.
315
 *
316
 * A possible cause of failure is exceeding the per-process or
317
 * system-wide file descriptor limit.
318
 *
319
 * Returns: %TRUE in case of success, else %FALSE (and @error is set)
320
 *
321
 * Since: 2.22
322
 **/
323
gboolean
324
g_unix_fd_message_append_fd (GUnixFDMessage  *message,
325
                             gint             fd,
326
                             GError         **error)
327
0
{
328
0
  g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);
329
330
0
  return g_unix_fd_list_append (message->priv->list, fd, error) >= 0;
331
0
}