1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation; either version 2 of the licence or (at
8 * your option) any later version.
10 * See the included COPYING file for more information.
12 * Authors: Ryan Lortie <desrt@desrt.ca>
16 * SECTION: gunixfdlist
18 * @short_description: An object containing a set of UNIX file descriptors
19 * @include: gio/gunixfdlist.h
20 * @see_also: #GUnixFDMessage
22 * A #GUnixFDList contains a list of file descriptors. It owns the file
23 * descriptors that it contains, closing them when finalized.
25 * It may be wrapped in a #GUnixFDMessage and sent over a #GSocket in
26 * the %G_SOCKET_ADDRESS_UNIX family by using g_socket_send_message()
27 * and received using g_socket_receive_message().
29 * Note that <filename><gio/gunixfdlist.h></filename> belongs to
30 * the UNIX-specific GIO interfaces, thus you have to use the
31 * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
34 #define _GNU_SOURCE /* for F_DUPFD_CLOEXEC */
38 #include <sys/types.h>
39 #include <sys/socket.h>
45 #include "gunixfdlist.h"
50 G_DEFINE_TYPE (GUnixFDList, g_unix_fd_list, G_TYPE_OBJECT)
52 struct _GUnixFDListPrivate
59 g_unix_fd_list_init (GUnixFDList *list)
61 list->priv = G_TYPE_INSTANCE_GET_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 close (list->priv->fds[i]);
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 g_type_class_add_private (class, sizeof (GUnixFDListPrivate));
86 object_class->finalize = g_unix_fd_list_finalize;
90 dup_close_on_exec_fd (gint fd,
96 #ifdef F_DUPFD_CLOEXEC
98 new_fd = fcntl (fd, F_DUPFD_CLOEXEC, 0l);
99 while (new_fd < 0 && (errno == EINTR));
104 /* if that didn't work (new libc/old kernel?), try it the other way. */
109 while (new_fd < 0 && (errno == EINTR));
113 int saved_errno = errno;
115 g_set_error (error, G_IO_ERROR,
116 g_io_error_from_errno (saved_errno),
117 "dup: %s", g_strerror (saved_errno));
125 s = fcntl (new_fd, F_GETFD);
128 s = fcntl (new_fd, F_SETFD, (long) (s | FD_CLOEXEC));
130 while (s < 0 && (errno == EINTR));
134 int saved_errno = errno;
136 g_set_error (error, G_IO_ERROR,
137 g_io_error_from_errno (saved_errno),
138 "fcntl: %s", g_strerror (saved_errno));
148 * g_unix_fd_list_new:
150 * Creates a new #GUnixFDList containing no file descriptors.
152 * Returns: a new #GUnixFDList
157 g_unix_fd_list_new (void)
159 return g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
163 * g_unix_fd_list_new_from_array:
164 * @fds: (array length=n_fds): the initial list of file descriptors
165 * @n_fds: the length of #fds, or -1
167 * Creates a new #GUnixFDList containing the file descriptors given in
168 * @fds. The file descriptors become the property of the new list and
169 * may no longer be used by the caller. The array itself is owned by
172 * Each file descriptor in the array should be set to close-on-exec.
174 * If @n_fds is -1 then @fds must be terminated with -1.
176 * Returns: a new #GUnixFDList
181 g_unix_fd_list_new_from_array (const gint *fds,
186 g_return_val_if_fail (fds != NULL || n_fds == 0, NULL);
189 for (n_fds = 0; fds[n_fds] != -1; n_fds++);
191 list = g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
192 list->priv->fds = g_new (gint, n_fds + 1);
193 list->priv->nfd = n_fds;
195 memcpy (list->priv->fds, fds, sizeof (gint) * n_fds);
196 list->priv->fds[n_fds] = -1;
202 * g_unix_fd_list_steal_fds:
203 * @list: a #GUnixFDList
204 * @length: (out) (allow-none): pointer to the length of the returned
207 * Returns the array of file descriptors that is contained in this
210 * After this call, the descriptors are no longer contained in
211 * @list. Further calls will return an empty list (unless more
212 * descriptors have been added).
214 * The return result of this function must be freed with g_free().
215 * The caller is also responsible for closing all of the file
216 * descriptors. The file descriptors in the array are set to
219 * If @length is non-%NULL then it is set to the number of file
220 * descriptors in the returned array. The returned array is also
221 * terminated with -1.
223 * This function never returns %NULL. In case there are no file
224 * descriptors contained in @list, an empty array is returned.
226 * Returns: (array length=length) (transfer full): an array of file
232 g_unix_fd_list_steal_fds (GUnixFDList *list,
237 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
239 /* will be true for fresh object or if we were just called */
240 if (list->priv->fds == NULL)
242 list->priv->fds = g_new (gint, 1);
243 list->priv->fds[0] = -1;
248 *length = list->priv->nfd;
249 result = list->priv->fds;
251 list->priv->fds = NULL;
258 * g_unix_fd_list_peek_fds:
259 * @list: a #GUnixFDList
260 * @length: (out) (allow-none): pointer to the length of the returned
263 * Returns the array of file descriptors that is contained in this
266 * After this call, the descriptors remain the property of @list. The
267 * caller must not close them and must not free the array. The array is
268 * valid only until @list is changed in any way.
270 * If @length is non-%NULL then it is set to the number of file
271 * descriptors in the returned array. The returned array is also
272 * terminated with -1.
274 * This function never returns %NULL. In case there are no file
275 * descriptors contained in @list, an empty array is returned.
277 * Returns: (array length=length) (transfer none): an array of file
283 g_unix_fd_list_peek_fds (GUnixFDList *list,
286 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
288 /* will be true for fresh object or if steal() was just called */
289 if (list->priv->fds == NULL)
291 list->priv->fds = g_new (gint, 1);
292 list->priv->fds[0] = -1;
297 *length = list->priv->nfd;
299 return list->priv->fds;
303 * g_unix_fd_list_append:
304 * @list: a #GUnixFDList
305 * @fd: a valid open file descriptor
306 * @error: a #GError pointer
308 * Adds a file descriptor to @list.
310 * The file descriptor is duplicated using dup(). You keep your copy
311 * of the descriptor and the copy contained in @list will be closed
312 * when @list is finalized.
314 * A possible cause of failure is exceeding the per-process or
315 * system-wide file descriptor limit.
317 * The index of the file descriptor in the list is returned. If you use
318 * this index with g_unix_fd_list_get() then you will receive back a
319 * duplicated copy of the same file descriptor.
321 * Returns: the index of the appended fd in case of success, else -1
322 * (and @error is set)
327 g_unix_fd_list_append (GUnixFDList *list,
333 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
334 g_return_val_if_fail (fd >= 0, -1);
335 g_return_val_if_fail (error == NULL || *error == NULL, -1);
337 if ((new_fd = dup_close_on_exec_fd (fd, error)) < 0)
340 list->priv->fds = g_realloc (list->priv->fds,
342 (list->priv->nfd + 2));
343 list->priv->fds[list->priv->nfd++] = new_fd;
344 list->priv->fds[list->priv->nfd] = -1;
346 return list->priv->nfd - 1;
350 * g_unix_fd_list_get:
351 * @list: a #GUnixFDList
352 * @index_: the index into the list
353 * @error: a #GError pointer
355 * Gets a file descriptor out of @list.
357 * @index_ specifies the index of the file descriptor to get. It is a
358 * programmer error for @index_ to be out of range; see
359 * g_unix_fd_list_get_length().
361 * The file descriptor is duplicated using dup() and set as
362 * close-on-exec before being returned. You must call close() on it
365 * A possible cause of failure is exceeding the per-process or
366 * system-wide file descriptor limit.
368 * Returns: the file descriptor, or -1 in case of error
373 g_unix_fd_list_get (GUnixFDList *list,
377 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
378 g_return_val_if_fail (index_ < list->priv->nfd, -1);
379 g_return_val_if_fail (error == NULL || *error == NULL, -1);
381 return dup_close_on_exec_fd (list->priv->fds[index_], error);
385 * g_unix_fd_list_get_length:
386 * @list: a #GUnixFDList
388 * Gets the length of @list (ie: the number of file descriptors
391 * Returns: the length of @list
396 g_unix_fd_list_get_length (GUnixFDList *list)
398 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), 0);
400 return list->priv->nfd;