Coverage Report

Created: 2026-11-12 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glib/gio/gunixfdlist.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright © 2009 Codethink Limited
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * See the included COPYING file for more information.
13
 *
14
 * Authors: Ryan Lortie <desrt@desrt.ca>
15
 */
16
17
/**
18
 * GUnixFDList:
19
 *
20
 * A `GUnixFDList` contains a list of file descriptors.  It owns the file
21
 * descriptors that it contains, closing them when finalized.
22
 *
23
 * It may be wrapped in a
24
 * [`GUnixFDMessage`](../gio-unix/class.UnixFDMessage.html) and sent over a
25
 * [class@Gio.Socket] in the `G_SOCKET_FAMILY_UNIX` family by using
26
 * [method@Gio.Socket.send_message] and received using
27
 * [method@Gio.Socket.receive_message].
28
 *
29
 * Before 2.74, `<gio/gunixfdlist.h>` belonged to the UNIX-specific GIO
30
 * interfaces, thus you had to use the `gio-unix-2.0.pc` pkg-config file when
31
 * using it.
32
 *
33
 * Since 2.74, the API is available for Windows.
34
 */
35
36
#include "config.h"
37
38
#include <fcntl.h>
39
#include <string.h>
40
#include <errno.h>
41
42
#include "gunixfdlist.h"
43
#include "gnetworking.h"
44
#include "gioerror.h"
45
#include "glib/glib-private.h"
46
#include "glib/gstdio.h"
47
48
#ifdef G_OS_WIN32
49
#include <io.h>
50
#endif
51
52
struct _GUnixFDListPrivate
53
{
54
  int *fds;
55
  size_t nfd;
56
};
57
58
0
G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDList, g_unix_fd_list, G_TYPE_OBJECT)
59
0
60
0
static void
61
0
g_unix_fd_list_init (GUnixFDList *list)
62
0
{
63
0
  list->priv = g_unix_fd_list_get_instance_private (list);
64
0
}
65
66
static void
67
g_unix_fd_list_finalize (GObject *object)
68
0
{
69
0
  GUnixFDList *list = G_UNIX_FD_LIST (object);
70
71
0
  for (size_t i = 0; i < list->priv->nfd; i++)
72
0
    g_close (list->priv->fds[i], NULL);
73
0
  g_free (list->priv->fds);
74
75
0
  G_OBJECT_CLASS (g_unix_fd_list_parent_class)->finalize (object);
76
0
}
77
78
static void
79
g_unix_fd_list_class_init (GUnixFDListClass *class)
80
0
{
81
0
  GObjectClass *object_class = G_OBJECT_CLASS (class);
82
83
0
  object_class->finalize = g_unix_fd_list_finalize;
84
0
}
85
86
static int
87
dup_close_on_exec_fd (int      fd,
88
                      GError **error)
89
0
{
90
0
  int new_fd;
91
0
#ifndef G_OS_WIN32
92
0
  int s;
93
0
#endif
94
95
0
#ifdef F_DUPFD_CLOEXEC
96
0
  do
97
0
    new_fd = fcntl (fd, F_DUPFD_CLOEXEC, 0l);
98
0
  while (new_fd < 0 && (errno == EINTR));
99
100
0
  if (new_fd >= 0)
101
0
    return new_fd;
102
103
  /* if that didn't work (new libc/old kernel?), try it the other way. */
104
0
#endif
105
106
0
  do
107
0
    new_fd = dup (fd);
108
0
  while (new_fd < 0 && (errno == EINTR));
109
110
0
  if (new_fd < 0)
111
0
    {
112
0
      int saved_errno = errno;
113
114
0
      g_set_error (error, G_IO_ERROR,
115
0
                   g_io_error_from_errno (saved_errno),
116
0
                   "dup: %s", g_strerror (saved_errno));
117
118
0
      return -1;
119
0
    }
120
121
#ifdef G_OS_WIN32
122
  new_fd = GLIB_PRIVATE_CALL (g_win32_reopen_noninherited) (new_fd, 0, error);
123
#else
124
0
  do
125
0
    {
126
0
      s = fcntl (new_fd, F_GETFD);
127
128
0
      if (s >= 0)
129
0
        s = fcntl (new_fd, F_SETFD, (long) (s | FD_CLOEXEC));
130
0
    }
131
0
  while (s < 0 && (errno == EINTR));
132
133
0
  if (s < 0)
134
0
    {
135
0
      int saved_errno = errno;
136
137
0
      g_set_error (error, G_IO_ERROR,
138
0
                   g_io_error_from_errno (saved_errno),
139
0
                   "fcntl: %s", g_strerror (saved_errno));
140
0
      g_close (new_fd, NULL);
141
142
0
      return -1;
143
0
    }
144
0
#endif
145
146
0
  return new_fd;
147
0
}
148
149
/**
150
 * g_unix_fd_list_new:
151
 *
152
 * Creates a new [class@Gio.UnixFDList] containing no file descriptors.
153
 *
154
 * Returns: a new [class@Gio.UnixFDList]
155
 *
156
 * Since: 2.24
157
 **/
158
GUnixFDList *
159
g_unix_fd_list_new (void)
160
0
{
161
0
  return g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
162
0
}
163
164
/**
165
 * g_unix_fd_list_new_from_array:
166
 * @fds: (array length=n_fds): the initial list of file descriptors
167
 * @n_fds: the length of @fds, or `-1`
168
 *
169
 * Creates a new [class@Gio.UnixFDList] containing the file descriptors given
170
 * in @fds. The file descriptors become the property of the new list and may no
171
 * longer be used by the caller. The array itself is owned by the caller.
172
 *
173
 * Each file descriptor in the array should be set to close-on-exec.
174
 *
175
 * If @n_fds is -1 then @fds must be terminated with -1.
176
 *
177
 * Returns: a new [class@Gio.UnixFDList]
178
 *
179
 * Since: 2.24
180
 **/
181
GUnixFDList *
182
g_unix_fd_list_new_from_array (const int *fds,
183
                               int        n_fds)
184
0
{
185
0
  GUnixFDList *list;
186
0
  size_t n_fds_unsigned;
187
188
0
  g_return_val_if_fail (fds != NULL || n_fds == 0, NULL);
189
0
  g_return_val_if_fail (n_fds >= -1, NULL);
190
191
0
  if (n_fds >= 0)
192
0
    n_fds_unsigned = n_fds;
193
0
  else
194
0
    for (n_fds_unsigned = 0; fds[n_fds_unsigned] != -1; n_fds_unsigned++);
195
196
0
  g_assert (n_fds_unsigned < G_MAXSIZE);
197
198
0
  list = g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
199
0
  list->priv->fds = g_new (int, n_fds_unsigned + 1);
200
0
  list->priv->nfd = n_fds_unsigned;
201
202
0
  if (n_fds_unsigned > 0)
203
0
    memcpy (list->priv->fds, fds, sizeof (int) * n_fds_unsigned);
204
0
  list->priv->fds[n_fds_unsigned] = -1;
205
206
0
  return list;
207
0
}
208
209
/**
210
 * g_unix_fd_list_steal_fds:
211
 * @list: a [class@Gio.UnixFDList]
212
 * @length: (out) (optional): pointer to the length of the returned
213
 *     array, or `NULL`
214
 *
215
 * Returns the array of file descriptors that is contained in this
216
 * object.
217
 *
218
 * After this call, the descriptors are no longer contained in
219
 * @list. Further calls will return an empty list (unless more
220
 * descriptors have been added).
221
 *
222
 * The return result of this function must be freed with `g_free()`.
223
 * The caller is also responsible for closing all of the file
224
 * descriptors.  The file descriptors in the array are set to
225
 * close-on-exec.
226
 *
227
 * If @length is non-`NULL` then it is set to the number of file
228
 * descriptors in the returned array. The returned array is also
229
 * terminated with `-1`.
230
 *
231
 * This function never returns `NULL`. In case there are no file
232
 * descriptors contained in @list, an empty array is returned.
233
 *
234
 * Returns: (array length=length) (transfer full): an array of file
235
 *     descriptors
236
 *
237
 * Since: 2.24
238
 */
239
int *
240
g_unix_fd_list_steal_fds (GUnixFDList *list,
241
                          int         *length)
242
0
{
243
0
  int *result;
244
245
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
246
0
  g_return_val_if_fail (list->priv->nfd <= G_MAXINT, NULL);
247
248
  /* will be true for fresh object or if we were just called */
249
0
  if (list->priv->fds == NULL)
250
0
    {
251
0
      list->priv->fds = g_new (int, 1);
252
0
      list->priv->fds[0] = -1;
253
0
      list->priv->nfd = 0;
254
0
    }
255
256
0
  if (length)
257
0
    *length = list->priv->nfd;
258
0
  result = list->priv->fds;
259
260
0
  list->priv->fds = NULL;
261
0
  list->priv->nfd = 0;
262
263
0
  return result;
264
0
}
265
266
/**
267
 * g_unix_fd_list_peek_fds:
268
 * @list: a [class@Gio.UnixFDList]
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 remain the property of @list.  The
276
 * caller must not close them and must not free the array.  The array is
277
 * valid only until @list is changed in any way.
278
 *
279
 * If @length is non-`NULL` then it is set to the number of file
280
 * descriptors in the returned array. The returned array is also
281
 * terminated with `-1`.
282
 *
283
 * This function never returns `NULL`. In case there are no file
284
 * descriptors contained in @list, an empty array is returned.
285
 *
286
 * Returns: (array length=length) (transfer none): an array of file
287
 *     descriptors
288
 *
289
 * Since: 2.24
290
 */
291
const int *
292
g_unix_fd_list_peek_fds (GUnixFDList *list,
293
                         int         *length)
294
0
{
295
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
296
0
  g_return_val_if_fail (list->priv->nfd <= G_MAXINT, NULL);
297
298
  /* will be true for fresh object or if steal() was just called */
299
0
  if (list->priv->fds == NULL)
300
0
    {
301
0
      list->priv->fds = g_new (int, 1);
302
0
      list->priv->fds[0] = -1;
303
0
      list->priv->nfd = 0;
304
0
    }
305
306
0
  if (length)
307
0
    *length = list->priv->nfd;
308
309
0
  return list->priv->fds;
310
0
}
311
312
/**
313
 * g_unix_fd_list_append:
314
 * @list: a [class@Gio.UnixFDList]
315
 * @fd: a valid open file descriptor
316
 * @error: a [type@GLib.Error]
317
 *
318
 * Adds a file descriptor to @list.
319
 *
320
 * The file descriptor is duplicated using `dup()`. You keep your copy
321
 * of the descriptor and the copy contained in @list will be closed
322
 * when @list is finalized.
323
 *
324
 * A possible cause of failure is exceeding the per-process or
325
 * system-wide file descriptor limit.
326
 *
327
 * The index of the file descriptor in the list is returned.  If you use
328
 * this index with [method@Gio.UnixFDList.get] then you will receive back a
329
 * duplicated copy of the same file descriptor.
330
 *
331
 * Returns: the index of the appended fd in case of success, else `-1`
332
 *          (and @error is set)
333
 *
334
 * Since: 2.24
335
 */
336
int
337
g_unix_fd_list_append (GUnixFDList  *list,
338
                       int           fd,
339
                       GError      **error)
340
0
{
341
0
  int new_fd;
342
0
  size_t index_;
343
344
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
345
0
  g_return_val_if_fail (fd >= 0, -1);
346
0
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
347
348
0
  index_ = list->priv->nfd;
349
0
  if (index_ > G_MAXINT)
350
0
    {
351
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
352
0
                   "Too many file descriptors");
353
0
      return -1;
354
0
    }
355
356
0
  if ((new_fd = dup_close_on_exec_fd (fd, error)) < 0)
357
0
    return -1;
358
359
  /* we allocate nfd + 2 elements (fd itself and -1 terminator) */
360
0
  g_assert (list->priv->nfd <= G_MAXSIZE - 2);
361
362
0
  list->priv->nfd++;
363
0
  list->priv->fds = g_realloc_n (list->priv->fds,
364
0
                                 list->priv->nfd + 1,
365
0
                                 sizeof (int));
366
0
  list->priv->fds[index_] = new_fd;
367
0
  list->priv->fds[index_ + 1] = -1;
368
369
0
  return index_;
370
0
}
371
372
/**
373
 * g_unix_fd_list_append_take:
374
 * @list: a [class@Gio.UnixFDList]
375
 * @fd: a valid open file descriptor
376
 *
377
 * Adds a file descriptor to @list.
378
 *
379
 * After this call, @fd belongs to the @list and may no longer be closed by the
380
 * caller.
381
 *
382
 * The file descriptor @fd should be set to close-on-exec.
383
 *
384
 * The index of the file descriptor in the list is returned. If you use this
385
 * index with [method@Gio.UnixFDList.get] then you will receive back a
386
 * duplicated copy of the same file descriptor.
387
 *
388
 * Returns: the index of the appended @fd
389
 *
390
 * Since: 2.90
391
 */
392
size_t
393
g_unix_fd_list_append_take (GUnixFDList *list,
394
                            int          fd)
395
0
{
396
0
  size_t index_;
397
398
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), 0);
399
0
  g_return_val_if_fail (fd >= 0, 0);
400
401
0
  index_ = list->priv->nfd;
402
403
  /* we allocate nfd + 2 elements (fd itself and -1 terminator) */
404
0
  g_assert (list->priv->nfd <= G_MAXSIZE - 2);
405
406
0
  list->priv->nfd++;
407
0
  list->priv->fds = g_realloc_n (list->priv->fds,
408
0
                                 list->priv->nfd + 1,
409
0
                                 sizeof (int));
410
0
  list->priv->fds[index_] = g_steal_fd (&fd);
411
0
  list->priv->fds[index_ + 1] = -1;
412
413
0
  return index_;
414
0
}
415
416
/**
417
 * g_unix_fd_list_get:
418
 * @list: a [method@Gio.UnixFDList.get]
419
 * @index_: the index into the list
420
 * @error: a [type@GLib.Error]
421
 *
422
 * Gets a file descriptor out of @list.
423
 *
424
 * @index_ specifies the index of the file descriptor to get.  It is a
425
 * programmer error for @index_ to be out of range. Either use
426
 * [method@Gio.UnixFDList.lookup] to do a checked lookup, or check the index
427
 * against the list length using [method@Gio.UnixFDList.get_length].
428
 *
429
 * The file descriptor is duplicated using `dup()` and set as
430
 * close-on-exec before being returned.  You must call `close()` on it
431
 * when you are done.
432
 *
433
 * A possible cause of failure is exceeding the per-process or
434
 * system-wide file descriptor limit.
435
 *
436
 * Returns: the file descriptor, or `-1` in case of error
437
 *
438
 * Since: 2.24
439
 **/
440
int
441
g_unix_fd_list_get (GUnixFDList  *list,
442
                    int           index_,
443
                    GError      **error)
444
0
{
445
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
446
0
  g_return_val_if_fail (index_ >= 0 && (size_t) index_ < list->priv->nfd, -1);
447
0
  g_return_val_if_fail (error == NULL || *error == NULL, -1);
448
449
0
  return dup_close_on_exec_fd (list->priv->fds[index_], error);
450
0
}
451
452
/**
453
 * g_unix_fd_list_peek:
454
 * @list: a [class@Gio.UnixFDList]
455
 * @index_: the index into the list
456
 *
457
 * Gets a file descriptor out of @list.
458
 *
459
 * @index_ specifies the index of the file descriptor to get. It is a programmer
460
 * error for @index_ to be out of range; see [method@Gio.UnixFDList.get_length].
461
 *
462
 * This will always return a valid (non-negative) file descriptor.
463
 *
464
 * After this call, the descriptor remains the property of @list. The caller
465
 * must not close it. The descriptor is valid only until @list is changed in any
466
 * way.
467
 *
468
 * Returns: the file descriptor
469
 *
470
 * Since: 2.90
471
 **/
472
int
473
g_unix_fd_list_peek (GUnixFDList *list,
474
                     size_t       index_)
475
0
{
476
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
477
0
  g_return_val_if_fail (index_ < list->priv->nfd, -1);
478
479
0
  return list->priv->fds[index_];
480
0
}
481
482
/**
483
 * g_unix_fd_list_lookup:
484
 * @list: a [class@Gio.UnixFDList]
485
 * @index_: the file descriptor index
486
 *
487
 * Looks up a file descriptor in @list at position @index_.
488
 *
489
 * @index_ specifies the index of the file descriptor to get. If no file
490
 * descriptor exists at this index, `-1` is returned.
491
 *
492
 * After this call, the descriptor remains the property of @list. The caller
493
 * must not close it. The descriptor is valid only until @list is changed in any
494
 * way.
495
 *
496
 * Returns: the file descriptor, or `-1` if not found
497
 *
498
 * Since: 2.90
499
 **/
500
int
501
g_unix_fd_list_lookup (GUnixFDList *list,
502
                       size_t       index_)
503
0
{
504
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
505
506
0
  if (index_ >= list->priv->nfd)
507
0
    return -1;
508
509
0
  return list->priv->fds[index_];
510
0
}
511
512
/**
513
 * g_unix_fd_list_get_length:
514
 * @list: a [class@Gio.UnixFDList]
515
 *
516
 * Gets the length of @list (ie: the number of file descriptors
517
 * contained within).
518
 *
519
 * Returns: the length of @list
520
 *
521
 * Since: 2.24
522
 **/
523
int
524
g_unix_fd_list_get_length (GUnixFDList *list)
525
0
{
526
0
  g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), 0);
527
0
  g_return_val_if_fail (list->priv->nfd <= G_MAXINT, 0);
528
529
0
  return list->priv->nfd;
530
0
}