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