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 file descriptors
19 * @see_also: #GUnixFDMessage
21 * A #GUnixFDList contains a list of file descriptors. It owns the file
22 * descriptors that it contains, closing them when finalized.
24 * It may be wrapped in a #GUnixFDMessage and sent over a #GSocket in
25 * the %G_SOCKET_ADDRESS_UNIX family by using g_socket_send_message()
26 * and received using g_socket_receive_message().
29 #define _GNU_SOURCE /* for F_DUPFD_CLOEXEC */
33 #include <sys/types.h>
34 #include <sys/socket.h>
40 #include "gunixfdlist.h"
46 G_DEFINE_TYPE (GUnixFDList, g_unix_fd_list, G_TYPE_OBJECT)
48 struct _GUnixFDListPrivate
55 g_unix_fd_list_init (GUnixFDList *list)
57 list->priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
63 g_unix_fd_list_finalize (GObject *object)
65 GUnixFDList *list = G_UNIX_FD_LIST (object);
68 for (i = 0; i < list->priv->nfd; i++)
69 close (list->priv->fds[i]);
70 g_free (list->priv->fds);
72 G_OBJECT_CLASS (g_unix_fd_list_parent_class)
77 g_unix_fd_list_class_init (GUnixFDListClass *class)
79 GObjectClass *object_class = G_OBJECT_CLASS (class);
81 g_type_class_add_private (class, sizeof (GUnixFDListPrivate));
82 object_class->finalize = g_unix_fd_list_finalize;
86 dup_close_on_exec_fd (gint fd,
92 #ifdef F_DUPFD_CLOEXEC
94 new_fd = fcntl (fd, F_DUPFD_CLOEXEC, 0l);
95 while (new_fd < 0 && (errno == EINTR));
100 /* if that didn't work (new libc/old kernel?), try it the other way. */
105 while (new_fd < 0 && (errno == EINTR));
109 int saved_errno = errno;
111 g_set_error (error, G_IO_ERROR,
112 g_io_error_from_errno (saved_errno),
113 "dup: %s", g_strerror (saved_errno));
120 s = fcntl (new_fd, F_SETFD, FD_CLOEXEC);
121 while (s < 0 && (errno == EINTR));
125 int saved_errno = errno;
127 g_set_error (error, G_IO_ERROR,
128 g_io_error_from_errno (saved_errno),
129 "fcntl: %s", g_strerror (saved_errno));
139 * g_unix_fd_list_new:
141 * Creates a new #GUnixFDList containing no file descriptors.
143 * Returns: a new #GUnixFDList
148 g_unix_fd_list_new (void)
150 return g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
154 * g_unix_fd_list_new_from_array:
155 * @fds: the initial list of file descriptors
156 * @n_fds: the length of #fds, or -1
158 * Creates a new #GUnixFDList containing the file descriptors given in
159 * @fds. The file descriptors become the property of the new list and
160 * may no longer be used by the caller. The array itself is owned by
163 * Each file descriptor in the array should be set to close-on-exec.
165 * If @n_fds is -1 then @fds must be terminated with -1.
167 * Returns: a new #GUnixFDList
172 g_unix_fd_list_new_from_array (const gint *fds,
177 g_return_val_if_fail (fds != NULL || n_fds == 0, NULL);
180 for (n_fds = 0; fds[n_fds] != -1; n_fds++);
182 list = g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
183 list->priv->fds = g_new (gint, n_fds + 1);
184 list->priv->nfd = n_fds;
186 memcpy (list->priv->fds, fds, sizeof (gint) * n_fds);
187 list->priv->fds[n_fds] = -1;
193 * g_unix_fd_list_steal_fds:
194 * @list: a #GUnixFDList
195 * @length: pointer to the length of the returned array, or %NULL
197 * Returns the array of file descriptors that is contained in this
200 * After this call, the descriptors are no longer contained in
201 * @list. Further calls will return an empty list (unless more
202 * descriptors have been added).
204 * The return result of this function must be freed with g_free().
205 * The caller is also responsible for closing all of the file
206 * descriptors. The file descriptors in the array are set to
209 * If @length is non-%NULL then it is set to the number of file
210 * descriptors in the returned array. The returned array is also
211 * terminated with -1.
213 * This function never returns %NULL. In case there are no file
214 * descriptors contained in @list, an empty array is returned.
216 * Returns: an array of file descriptors
221 g_unix_fd_list_steal_fds (GUnixFDList *list,
226 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
228 /* will be true for fresh object or if we were just called */
229 if (list->priv->fds == NULL)
231 list->priv->fds = g_new (gint, 1);
232 list->priv->fds[0] = -1;
237 *length = list->priv->nfd;
238 result = list->priv->fds;
240 list->priv->fds = NULL;
247 * g_unix_fd_list_peek_fds:
248 * @list: a #GUnixFDList
249 * @length: pointer to the length of the returned array, or %NULL
251 * Returns the array of file descriptors that is contained in this
254 * After this call, the descriptors remain the property of @list. The
255 * caller must not close them and must not free the array. The array is
256 * valid only until @list is changed in any way.
258 * If @length is non-%NULL then it is set to the number of file
259 * descriptors in the returned array. The returned array is also
260 * terminated with -1.
262 * This function never returns %NULL. In case there are no file
263 * descriptors contained in @list, an empty array is returned.
265 * Returns: an array of file descriptors
270 g_unix_fd_list_peek_fds (GUnixFDList *list,
273 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
275 /* will be true for fresh object or if steal() was just called */
276 if (list->priv->fds == NULL)
278 list->priv->fds = g_new (gint, 1);
279 list->priv->fds[0] = -1;
284 *length = list->priv->nfd;
286 return list->priv->fds;
290 * g_unix_fd_list_append:
291 * @list: a #GUnixFDList
292 * @fd: a valid open file descriptor
293 * @error: a #GError pointer
295 * Adds a file descriptor to @list.
297 * The file descriptor is duplicated using dup(). You keep your copy
298 * of the descriptor and the copy contained in @list will be closed
299 * when @list is finalized.
301 * A possible cause of failure is exceeding the per-process or
302 * system-wide file descriptor limit.
304 * The index of the file descriptor in the list is returned. If you use
305 * this index with g_unix_fd_list_get() then you will receive back a
306 * duplicated copy of the same file descriptor.
308 * Returns: the index of the appended fd in case of success, else -1
309 * (and @error is set)
314 g_unix_fd_list_append (GUnixFDList *list,
320 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
321 g_return_val_if_fail (fd >= 0, -1);
322 g_return_val_if_fail (error == NULL || *error == NULL, -1);
324 if ((new_fd = dup_close_on_exec_fd (fd, error)) < 0)
327 list->priv->fds = g_realloc (list->priv->fds,
329 (list->priv->nfd + 2));
330 list->priv->fds[list->priv->nfd++] = new_fd;
331 list->priv->fds[list->priv->nfd] = -1;
333 return list->priv->nfd - 1;
337 * g_unix_fd_list_get:
338 * @list: a #GUnixFDList
339 * @index_: the index into the list
340 * @error: a #GError pointer
342 * Gets a file descriptor out of @list.
344 * @index_ specifies the index of the file descriptor to get. It is a
345 * programmer error for @index_ to be out of range; see
346 * g_unix_fd_list_get_length().
348 * The file descriptor is duplicated using dup() and set as
349 * close-on-exec before being returned. You must call close() on it
352 * A possible cause of failure is exceeding the per-process or
353 * system-wide file descriptor limit.
355 * Returns: the file descriptor, or -1 in case of error
360 g_unix_fd_list_get (GUnixFDList *list,
364 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
365 g_return_val_if_fail (index_ < list->priv->nfd, -1);
366 g_return_val_if_fail (error == NULL || *error == NULL, -1);
368 return dup_close_on_exec_fd (list->priv->fds[index_], error);
372 * g_unix_fd_list_get_length:
373 * @list: a #GUnixFDList
375 * Gets the length of @list (ie: the number of file descriptors
378 * Returns: the length of @list
383 g_unix_fd_list_get_length (GUnixFDList *list)
385 g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), 0);
387 return list->priv->nfd;
390 #define __G_UNIX_FD_LIST_C__
391 #include "gioaliasdef.c"