1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
5 * SPDX-License-Identifier: LGPL-2.1-or-later
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.
12 * See the included COPYING file for more information.
14 * Authors: Ryan Lortie <desrt@desrt.ca>
20 * A `GUnixFDList` contains a list of file descriptors. It owns the file
21 * descriptors that it contains, closing them when finalized.
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].
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
33 * Since 2.74, the API is available for Windows.
42 #include "gunixfdlist.h"
43 #include "gnetworking.h"
45 #include "glib/glib-private.h"
46 #include "glib/gstdio.h"
52 struct _GUnixFDListPrivate
58 G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDList, g_unix_fd_list, G_TYPE_OBJECT)
61 g_unix_fd_list_init (GUnixFDList *list)
63 list->priv = g_unix_fd_list_get_instance_private (list);
67 g_unix_fd_list_finalize (GObject *object)
69 GUnixFDList *list = G_UNIX_FD_LIST (object);
72 for (i = 0; i < list->priv->nfd; i++)
73 g_close (list->priv->fds[i], NULL);
74 g_free (list->priv->fds);
76 G_OBJECT_CLASS (g_unix_fd_list_parent_class)
81 g_unix_fd_list_class_init (GUnixFDListClass *class)
83 GObjectClass *object_class = G_OBJECT_CLASS (class);
85 object_class->finalize = g_unix_fd_list_finalize;
89 dup_close_on_exec_fd (gint fd,
97 #ifdef F_DUPFD_CLOEXEC
99 new_fd = fcntl (fd, F_DUPFD_CLOEXEC, 0l);
100 while (new_fd < 0 && (errno == EINTR));
105 /* if that didn't work (new libc/old kernel?), try it the other way. */
110 while (new_fd < 0 && (errno == EINTR));
114 int saved_errno = errno;
116 g_set_error (error, G_IO_ERROR,
117 g_io_error_from_errno (saved_errno),
118 "dup: %s", g_strerror (saved_errno));
124 new_fd = GLIB_PRIVATE_CALL (g_win32_reopen_noninherited) (new_fd, 0, error);
128 s = fcntl (new_fd, F_GETFD);
131 s = fcntl (new_fd, F_SETFD, (long) (s | FD_CLOEXEC));
133 while (s < 0 && (errno == EINTR));
137 int saved_errno = errno;
139 g_set_error (error, G_IO_ERROR,
140 g_io_error_from_errno (saved_errno),
141 "fcntl: %s", g_strerror (saved_errno));
142 g_close (new_fd, NULL);
152 * g_unix_fd_list_new:
154 * Creates a new #GUnixFDList containing no file descriptors.
156 * Returns: a new #GUnixFDList
161 g_unix_fd_list_new (void)
163 return g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
167 * g_unix_fd_list_new_from_array:
168 * @fds: (array length=n_fds): the initial list of file descriptors
169 * @n_fds: the length of #fds, or -1
171 * Creates a new #GUnixFDList containing the file descriptors given in
172 * @fds. The file descriptors become the property of the new list and
173 * may no longer be used by the caller. The array itself is owned by
176 * Each file descriptor in the array should be set to close-on-exec.
178 * If @n_fds is -1 then @fds must be terminated with -1.
180 * Returns: a new #GUnixFDList
185 g_unix_fd_list_new_from_array (const gint *fds,
190 g_return_val_if_fail (fds != NULL || n_fds == 0, NULL);
193 for (n_fds = 0; fds[n_fds] != -1; n_fds++);
195 list = g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
196 list->priv->fds = g_new (gint, n_fds + 1);
197 list->priv->nfd = n_fds;
200 memcpy (list->priv->fds, fds, sizeof (gint) * n_fds);
201 list->priv->fds[n_fds] = -1;
207 * g_unix_fd_list_steal_fds:
208 * @list: a #GUnixFDList
209 * @length: (out) (optional): pointer to the length of the returned
212 * Returns the array of file descriptors that is contained in this
215 * After this call, the descriptors are no longer contained in
216 * @list. Further calls will return an empty list (unless more
217 * descriptors have been added).
219 * The return result of this function must be freed with g_free().
220 * The caller is also responsible for closing all of the file
221 * descriptors. The file descriptors in the array are set to
224 * If @length is non-%NULL then it is set to the number of file
225 * descriptors in the returned array. The returned array is also
226 * terminated with -1.
228 * This function never returns %NULL. In case there are no file
229 * descriptors contained in @list, an empty array is returned.
231 * Returns: (array length=length) (transfer full): an array of file
237 g_unix_fd_list_steal_fds (GUnixFDList *list,
242 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
244 /* will be true for fresh object or if we were just called */
245 if (list->priv->fds == NULL)
247 list->priv->fds = g_new (gint, 1);
248 list->priv->fds[0] = -1;
253 *length = list->priv->nfd;
254 result = list->priv->fds;
256 list->priv->fds = NULL;
263 * g_unix_fd_list_peek_fds:
264 * @list: a #GUnixFDList
265 * @length: (out) (optional): pointer to the length of the returned
268 * Returns the array of file descriptors that is contained in this
271 * After this call, the descriptors remain the property of @list. The
272 * caller must not close them and must not free the array. The array is
273 * valid only until @list is changed in any way.
275 * If @length is non-%NULL then it is set to the number of file
276 * descriptors in the returned array. The returned array is also
277 * terminated with -1.
279 * This function never returns %NULL. In case there are no file
280 * descriptors contained in @list, an empty array is returned.
282 * Returns: (array length=length) (transfer none): an array of file
288 g_unix_fd_list_peek_fds (GUnixFDList *list,
291 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
293 /* will be true for fresh object or if steal() was just called */
294 if (list->priv->fds == NULL)
296 list->priv->fds = g_new (gint, 1);
297 list->priv->fds[0] = -1;
302 *length = list->priv->nfd;
304 return list->priv->fds;
308 * g_unix_fd_list_append:
309 * @list: a #GUnixFDList
310 * @fd: a valid open file descriptor
311 * @error: a #GError pointer
313 * Adds a file descriptor to @list.
315 * The file descriptor is duplicated using dup(). You keep your copy
316 * of the descriptor and the copy contained in @list will be closed
317 * when @list is finalized.
319 * A possible cause of failure is exceeding the per-process or
320 * system-wide file descriptor limit.
322 * The index of the file descriptor in the list is returned. If you use
323 * this index with g_unix_fd_list_get() then you will receive back a
324 * duplicated copy of the same file descriptor.
326 * Returns: the index of the appended fd in case of success, else -1
327 * (and @error is set)
332 g_unix_fd_list_append (GUnixFDList *list,
338 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
339 g_return_val_if_fail (fd >= 0, -1);
340 g_return_val_if_fail (error == NULL || *error == NULL, -1);
342 if ((new_fd = dup_close_on_exec_fd (fd, error)) < 0)
345 list->priv->fds = g_realloc (list->priv->fds,
347 (list->priv->nfd + 2));
348 list->priv->fds[list->priv->nfd++] = new_fd;
349 list->priv->fds[list->priv->nfd] = -1;
351 return list->priv->nfd - 1;
355 * g_unix_fd_list_get:
356 * @list: a #GUnixFDList
357 * @index_: the index into the list
358 * @error: a #GError pointer
360 * Gets a file descriptor out of @list.
362 * @index_ specifies the index of the file descriptor to get. It is a
363 * programmer error for @index_ to be out of range; see
364 * g_unix_fd_list_get_length().
366 * The file descriptor is duplicated using dup() and set as
367 * close-on-exec before being returned. You must call close() on it
370 * A possible cause of failure is exceeding the per-process or
371 * system-wide file descriptor limit.
373 * Returns: the file descriptor, or -1 in case of error
378 g_unix_fd_list_get (GUnixFDList *list,
382 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
383 g_return_val_if_fail (index_ < list->priv->nfd, -1);
384 g_return_val_if_fail (error == NULL || *error == NULL, -1);
386 return dup_close_on_exec_fd (list->priv->fds[index_], error);
390 * g_unix_fd_list_get_length:
391 * @list: a #GUnixFDList
393 * Gets the length of @list (ie: the number of file descriptors
396 * Returns: the length of @list
401 g_unix_fd_list_get_length (GUnixFDList *list)
403 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), 0);
405 return list->priv->nfd;