Fix make check
[platform/upstream/glib.git] / gio / gunixfdmessage.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: gunixfdmessage
17  * @title: GUnixFDMessage
18  * @short_description: A GSocketControlMessage containing a list of
19  * file descriptors
20  * @see_also: #GUnixConnection
21  *
22  * This #GSocketControlMessage contains a list of file descriptors.
23  * It may be sent using g_socket_send_message() and received using
24  * g_socket_receive_message() over UNIX sockets (ie: sockets in the
25  * %G_SOCKET_ADDRESS_UNIX family).
26  *
27  * For an easier way to send and receive file descriptors over
28  * stream-oriented UNIX sockets, see g_unix_connection_send_fd() and
29  * g_unix_connection_receive_fd().
30  */
31
32 #include "config.h"
33
34 #include <sys/socket.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #include "gunixfdmessage.h"
40 #include "gioerror.h"
41
42 #include "gioalias.h"
43
44
45 G_DEFINE_TYPE (GUnixFDMessage, g_unix_fd_message,
46                G_TYPE_SOCKET_CONTROL_MESSAGE);
47
48 struct _GUnixFDMessagePrivate
49 {
50   gint *fds;
51   gint nfd;
52 };
53
54 static gsize
55 g_unix_fd_message_get_size (GSocketControlMessage *message)
56 {
57   GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
58
59   return fd_message->priv->nfd * sizeof (gint);
60 }
61
62 static int
63 g_unix_fd_message_get_level (GSocketControlMessage *message)
64 {
65   return SOL_SOCKET;
66 }
67
68 static int
69 g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
70 {
71   return SCM_RIGHTS;
72 }
73
74 static GSocketControlMessage *
75 g_unix_fd_message_deserialize (int      level,
76                                int      type,
77                                gsize    size,
78                                gpointer data)
79 {
80   GUnixFDMessage *message;
81
82   if (level != SOL_SOCKET ||
83       level != SCM_RIGHTS)
84     return NULL;
85   
86   if (size % 4 > 0)
87     {
88       g_warning ("Kernel returned non-integral number of fds");
89       return NULL;
90     }
91
92   message = g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
93   message->priv->nfd = size / sizeof (gint);
94   message->priv->fds = g_new (gint, message->priv->nfd + 1);
95   memcpy (message->priv->fds, data, size);
96   message->priv->fds[message->priv->nfd] = -1;
97
98   return G_SOCKET_CONTROL_MESSAGE (message);
99 }
100
101 static void
102 g_unix_fd_message_serialize (GSocketControlMessage *message,
103                              gpointer               data)
104 {
105   GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
106   memcpy (data, fd_message->priv->fds,
107           sizeof (gint) * fd_message->priv->nfd);
108 }
109 static void
110 g_unix_fd_message_init (GUnixFDMessage *message)
111 {
112   message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
113                                                G_TYPE_UNIX_FD_MESSAGE,
114                                                GUnixFDMessagePrivate);
115 }
116
117 static void
118 g_unix_fd_message_finalize (GObject *object)
119 {
120   GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
121   gint i;
122
123   for (i = 0; i < message->priv->nfd; i++)
124     close (message->priv->fds[i]);
125   g_free (message->priv->fds);
126
127   G_OBJECT_CLASS (g_unix_fd_message_parent_class)
128     ->finalize (object);
129 }
130
131 static void
132 g_unix_fd_message_class_init (GUnixFDMessageClass *class)
133 {
134   GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
135   GObjectClass *object_class = G_OBJECT_CLASS (class);
136
137   g_type_class_add_private (class, sizeof (GUnixFDMessagePrivate));
138   scm_class->get_size = g_unix_fd_message_get_size;
139   scm_class->get_level = g_unix_fd_message_get_level;
140   scm_class->get_type = g_unix_fd_message_get_msg_type;
141   scm_class->serialize = g_unix_fd_message_serialize;
142   scm_class->deserialize = g_unix_fd_message_deserialize;
143   object_class->finalize = g_unix_fd_message_finalize;
144 }
145
146 /**
147  * g_unix_fd_message_new:
148  *
149  * Creates a new #GUnixFDMessage containing no file descriptors.
150  *
151  * Returns: a new #GUnixFDMessage
152  *
153  * Since: 2.22
154  */
155 GSocketControlMessage *
156 g_unix_fd_message_new (void)
157 {
158   return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
159 }
160
161 /**
162  * g_unix_fd_message_steal_fds:
163  * @message: a #GUnixFDMessage
164  * @length: pointer to the length of the returned array, or %NULL
165  *
166  * Returns the array of file descriptors that is contained in this
167  * object.
168  *
169  * After this call, the descriptors are no longer contained in
170  * @message. Further calls will return an empty list (unless more
171  * descriptors have been added).
172  *
173  * The return result of this function must be freed with g_free().
174  * The caller is also responsible for closing all of the file
175  * descriptors.
176  *
177  * If @length is non-%NULL then it is set to the number of file
178  * descriptors in the returned array. The returned array is also
179  * terminated with -1.
180  *
181  * This function never returns %NULL. In case there are no file
182  * descriptors contained in @message, an empty array is returned.
183  *
184  * Returns: an array of file descriptors
185  *
186  * Since: 2.22
187  */
188 gint *
189 g_unix_fd_message_steal_fds (GUnixFDMessage *message,
190                              gint           *length)
191 {
192   gint *result;
193
194   g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), NULL);
195
196   /* will be true for fresh object or if we were just called */
197   if (message->priv->fds == NULL)
198     {
199       message->priv->fds = g_new (gint, 1);
200       message->priv->fds[0] = -1;
201       message->priv->nfd = 0;
202     }
203
204   if (length)
205     *length = message->priv->nfd;
206   result = message->priv->fds;
207
208   message->priv->fds = NULL;
209   message->priv->nfd = 0;
210
211   return result;
212 }
213
214 /**
215  * g_unix_fd_message_append_fd:
216  * @message: a #GUnixFDMessage
217  * @fd: a valid open file descriptor
218  * @error: a #GError pointer
219  *
220  * Adds a file descriptor to @message.
221  *
222  * The file descriptor is duplicated using dup(). You keep your copy
223  * of the descriptor and the copy contained in @message will be closed
224  * when @message is finalized.
225  *
226  * A possible cause of failure is exceeding the per-process or
227  * system-wide file descriptor limit.
228  *
229  * Returns: %TRUE in case of success, else %FALSE (and @error is set)
230  *
231  * Since: 2.22
232  */
233 gboolean
234 g_unix_fd_message_append_fd (GUnixFDMessage  *message,
235                              gint             fd,
236                              GError         **error)
237 {
238   gint new_fd;
239
240   g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), FALSE);
241   g_return_val_if_fail (fd >= 0, FALSE);
242
243   do
244     new_fd = dup (fd);
245   while (new_fd < 0 && (errno == EINTR));
246
247   if (fd < 0)
248     {
249       int saved_errno = errno;
250
251       g_set_error (error, G_IO_ERROR,
252                    g_io_error_from_errno (saved_errno),
253                    "dup: %s", g_strerror (saved_errno));
254
255       return FALSE;
256     }
257
258   message->priv->fds = g_realloc (message->priv->fds,
259                                   sizeof (gint) *
260                                    (message->priv->nfd + 2));
261   message->priv->fds[message->priv->nfd++] = new_fd;
262   message->priv->fds[message->priv->nfd] = -1;
263
264   return TRUE;
265 }
266
267 #define __G_UNIX_FD_MESSAGE_C__
268 #include "gioaliasdef.c"