Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / gunixfdlist.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright © 2009 Codethink Limited
4  *
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.
9  *
10  * See the included COPYING file for more information.
11  *
12  * Authors: Ryan Lortie <desrt@desrt.ca>
13  */
14
15 /**
16  * SECTION: gunixfdlist
17  * @title: GUnixFDList
18  * @short_description: An object containing a set of UNIX file descriptors
19  * @include: gio/gunixfdlist.h
20  * @see_also: #GUnixFDMessage
21  *
22  * A #GUnixFDList contains a list of file descriptors.  It owns the file
23  * descriptors that it contains, closing them when finalized.
24  *
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().
28  *
29  * Note that <filename>&lt;gio/gunixfdlist.h&gt;</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.
32  */
33
34 #define _GNU_SOURCE /* for F_DUPFD_CLOEXEC */
35
36 #include "config.h"
37
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <string.h>
43 #include <errno.h>
44
45 #include "gunixfdlist.h"
46 #include "gioerror.h"
47
48 #include "gioalias.h"
49
50
51 G_DEFINE_TYPE (GUnixFDList, g_unix_fd_list, G_TYPE_OBJECT)
52
53 struct _GUnixFDListPrivate
54 {
55   gint *fds;
56   gint nfd;
57 };
58
59 static void
60 g_unix_fd_list_init (GUnixFDList *list)
61 {
62   list->priv = G_TYPE_INSTANCE_GET_PRIVATE (list,
63                                                G_TYPE_UNIX_FD_LIST,
64                                                GUnixFDListPrivate);
65 }
66
67 static void
68 g_unix_fd_list_finalize (GObject *object)
69 {
70   GUnixFDList *list = G_UNIX_FD_LIST (object);
71   gint i;
72
73   for (i = 0; i < list->priv->nfd; i++)
74     close (list->priv->fds[i]);
75   g_free (list->priv->fds);
76
77   G_OBJECT_CLASS (g_unix_fd_list_parent_class)
78     ->finalize (object);
79 }
80
81 static void
82 g_unix_fd_list_class_init (GUnixFDListClass *class)
83 {
84   GObjectClass *object_class = G_OBJECT_CLASS (class);
85
86   g_type_class_add_private (class, sizeof (GUnixFDListPrivate));
87   object_class->finalize = g_unix_fd_list_finalize;
88 }
89
90 static int
91 dup_close_on_exec_fd (gint     fd,
92                       GError **error)
93 {
94   gint new_fd;
95   gint s;
96
97 #ifdef F_DUPFD_CLOEXEC
98   do
99     new_fd = fcntl (fd, F_DUPFD_CLOEXEC, 0l);
100   while (new_fd < 0 && (errno == EINTR));
101
102   if (new_fd >= 0)
103     return new_fd;
104
105   /* if that didn't work (new libc/old kernel?), try it the other way. */
106 #endif
107
108   do
109     new_fd = dup (fd);
110   while (new_fd < 0 && (errno == EINTR));
111
112   if (new_fd < 0)
113     {
114       int saved_errno = errno;
115
116       g_set_error (error, G_IO_ERROR,
117                    g_io_error_from_errno (saved_errno),
118                    "dup: %s", g_strerror (saved_errno));
119       close (new_fd);
120
121       return -1;
122     }
123
124   do
125     {
126       s = fcntl (new_fd, F_GETFD);
127
128       if (s >= 0)
129         s = fcntl (new_fd, F_SETFD, (long) (s | FD_CLOEXEC));
130     }
131   while (s < 0 && (errno == EINTR));
132
133   if (s < 0)
134     {
135       int saved_errno = errno;
136
137       g_set_error (error, G_IO_ERROR,
138                    g_io_error_from_errno (saved_errno),
139                    "fcntl: %s", g_strerror (saved_errno));
140       close (new_fd);
141
142       return -1;
143     }
144
145   return new_fd;
146 }
147
148 /**
149  * g_unix_fd_list_new:
150  *
151  * Creates a new #GUnixFDList containing no file descriptors.
152  *
153  * Returns: a new #GUnixFDList
154  *
155  * Since: 2.24
156  **/
157 GUnixFDList *
158 g_unix_fd_list_new (void)
159 {
160   return g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
161 }
162
163 /**
164  * g_unix_fd_list_new_from_array:
165  * @fds: the initial list of file descriptors
166  * @n_fds: the length of #fds, or -1
167  *
168  * Creates a new #GUnixFDList containing the file descriptors given in
169  * @fds.  The file descriptors become the property of the new list and
170  * may no longer be used by the caller.  The array itself is owned by
171  * 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 #GUnixFDList
178  *
179  * Since: 2.24
180  **/
181 GUnixFDList *
182 g_unix_fd_list_new_from_array (const gint *fds,
183                                gint        n_fds)
184 {
185   GUnixFDList *list;
186
187   g_return_val_if_fail (fds != NULL || n_fds == 0, NULL);
188
189   if (n_fds == -1)
190     for (n_fds = 0; fds[n_fds] != -1; n_fds++);
191
192   list = g_object_new (G_TYPE_UNIX_FD_LIST, NULL);
193   list->priv->fds = g_new (gint, n_fds + 1);
194   list->priv->nfd = n_fds;
195
196   memcpy (list->priv->fds, fds, sizeof (gint) * n_fds);
197   list->priv->fds[n_fds] = -1;
198
199   return list;
200 }
201
202 /**
203  * g_unix_fd_list_steal_fds:
204  * @list: a #GUnixFDList
205  * @length: pointer to the length of the returned array, or %NULL
206  *
207  * Returns the array of file descriptors that is contained in this
208  * object.
209  *
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).
213  *
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
217  * close-on-exec.
218  *
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.
222  *
223  * This function never returns %NULL. In case there are no file
224  * descriptors contained in @list, an empty array is returned.
225  *
226  * Returns: an array of file descriptors
227  *
228  * Since: 2.24
229  */
230 gint *
231 g_unix_fd_list_steal_fds (GUnixFDList *list,
232                           gint        *length)
233 {
234   gint *result;
235
236   g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
237
238   /* will be true for fresh object or if we were just called */
239   if (list->priv->fds == NULL)
240     {
241       list->priv->fds = g_new (gint, 1);
242       list->priv->fds[0] = -1;
243       list->priv->nfd = 0;
244     }
245
246   if (length)
247     *length = list->priv->nfd;
248   result = list->priv->fds;
249
250   list->priv->fds = NULL;
251   list->priv->nfd = 0;
252
253   return result;
254 }
255
256 /**
257  * g_unix_fd_list_peek_fds:
258  * @list: a #GUnixFDList
259  * @length: pointer to the length of the returned array, or %NULL
260  *
261  * Returns the array of file descriptors that is contained in this
262  * object.
263  *
264  * After this call, the descriptors remain the property of @list.  The
265  * caller must not close them and must not free the array.  The array is
266  * valid only until @list is changed in any way.
267  *
268  * If @length is non-%NULL then it is set to the number of file
269  * descriptors in the returned array. The returned array is also
270  * terminated with -1.
271  *
272  * This function never returns %NULL. In case there are no file
273  * descriptors contained in @list, an empty array is returned.
274  *
275  * Returns: an array of file descriptors
276  *
277  * Since: 2.24
278  */
279 const gint *
280 g_unix_fd_list_peek_fds (GUnixFDList *list,
281                          gint        *length)
282 {
283   g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), NULL);
284
285   /* will be true for fresh object or if steal() was just called */
286   if (list->priv->fds == NULL)
287     {
288       list->priv->fds = g_new (gint, 1);
289       list->priv->fds[0] = -1;
290       list->priv->nfd = 0;
291     }
292
293   if (length)
294     *length = list->priv->nfd;
295
296   return list->priv->fds;
297 }
298
299 /**
300  * g_unix_fd_list_append:
301  * @list: a #GUnixFDList
302  * @fd: a valid open file descriptor
303  * @error: a #GError pointer
304  *
305  * Adds a file descriptor to @list.
306  *
307  * The file descriptor is duplicated using dup(). You keep your copy
308  * of the descriptor and the copy contained in @list will be closed
309  * when @list is finalized.
310  *
311  * A possible cause of failure is exceeding the per-process or
312  * system-wide file descriptor limit.
313  *
314  * The index of the file descriptor in the list is returned.  If you use
315  * this index with g_unix_fd_list_get() then you will receive back a
316  * duplicated copy of the same file descriptor.
317  *
318  * Returns: the index of the appended fd in case of success, else -1
319  *          (and @error is set)
320  *
321  * Since: 2.24
322  */
323 gint
324 g_unix_fd_list_append (GUnixFDList  *list,
325                        gint          fd,
326                        GError      **error)
327 {
328   gint new_fd;
329
330   g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
331   g_return_val_if_fail (fd >= 0, -1);
332   g_return_val_if_fail (error == NULL || *error == NULL, -1);
333
334   if ((new_fd = dup_close_on_exec_fd (fd, error)) < 0)
335     return -1;
336
337   list->priv->fds = g_realloc (list->priv->fds,
338                                   sizeof (gint) *
339                                    (list->priv->nfd + 2));
340   list->priv->fds[list->priv->nfd++] = new_fd;
341   list->priv->fds[list->priv->nfd] = -1;
342
343   return list->priv->nfd - 1;
344 }
345
346 /**
347  * g_unix_fd_list_get:
348  * @list: a #GUnixFDList
349  * @index_: the index into the list
350  * @error: a #GError pointer
351  *
352  * Gets a file descriptor out of @list.
353  *
354  * @index_ specifies the index of the file descriptor to get.  It is a
355  * programmer error for @index_ to be out of range; see
356  * g_unix_fd_list_get_length().
357  *
358  * The file descriptor is duplicated using dup() and set as
359  * close-on-exec before being returned.  You must call close() on it
360  * when you are done.
361  *
362  * A possible cause of failure is exceeding the per-process or
363  * system-wide file descriptor limit.
364  *
365  * Returns: the file descriptor, or -1 in case of error
366  *
367  * Since: 2.24
368  **/
369 gint
370 g_unix_fd_list_get (GUnixFDList  *list,
371                     gint          index_,
372                     GError      **error)
373 {
374   g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), -1);
375   g_return_val_if_fail (index_ < list->priv->nfd, -1);
376   g_return_val_if_fail (error == NULL || *error == NULL, -1);
377
378   return dup_close_on_exec_fd (list->priv->fds[index_], error);
379 }
380
381 /**
382  * g_unix_fd_list_get_length:
383  * @list: a #GUnixFDList
384  *
385  * Gets the length of @list (ie: the number of file descriptors
386  * contained within).
387  *
388  * Returns: the length of @list
389  *
390  * Since: 2.24
391  **/
392 gint
393 g_unix_fd_list_get_length (GUnixFDList *list)
394 {
395   g_return_val_if_fail (G_IS_UNIX_FD_LIST (list), 0);
396
397   return list->priv->nfd;
398 }
399
400 #define __G_UNIX_FD_LIST_C__
401 #include "gioaliasdef.c"