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