Clean up platform-specific includes
[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 #GUnixFDList
19  * @see_also: #GUnixConnection, #GUnixFDList, #GSocketControlMessage
20  *
21  * This #GSocketControlMessage contains a #GUnixFDList.  It may be sent
22  * using g_socket_send_message() and received using
23  * g_socket_receive_message() over UNIX sockets (ie: sockets in the
24  * %G_SOCKET_ADDRESS_UNIX family).  The file descriptors are copied
25  * between processes by the kernel.
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/types.h>
35 #include <sys/socket.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <fcntl.h>
39 #include <errno.h>
40
41 #include "gunixfdmessage.h"
42 #include "gunixfdlist.h"
43 #include "gioerror.h"
44
45 #include "gioalias.h"
46
47
48 G_DEFINE_TYPE (GUnixFDMessage, g_unix_fd_message,
49                G_TYPE_SOCKET_CONTROL_MESSAGE);
50
51 struct _GUnixFDMessagePrivate
52 {
53   GUnixFDList *list;
54 };
55
56 static gsize
57 g_unix_fd_message_get_size (GSocketControlMessage *message)
58 {
59   GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
60
61   return g_unix_fd_list_get_length (fd_message->priv->list) * sizeof (gint);
62 }
63
64 static int
65 g_unix_fd_message_get_level (GSocketControlMessage *message)
66 {
67   return SOL_SOCKET;
68 }
69
70 static int
71 g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
72 {
73   return SCM_RIGHTS;
74 }
75
76 static GSocketControlMessage *
77 g_unix_fd_message_deserialize (int      level,
78                                int      type,
79                                gsize    size,
80                                gpointer data)
81 {
82   GSocketControlMessage *message;
83   GUnixFDList *list;
84   gint n, s, i;
85   gint *fds;
86
87   if (level != SOL_SOCKET ||
88       type != SCM_RIGHTS)
89     return NULL;
90   
91   if (size % 4 > 0)
92     {
93       g_warning ("Kernel returned non-integral number of fds");
94       return NULL;
95     }
96
97   fds = data;
98   n = size / sizeof (gint);
99
100   for (i = 0; i < n; i++)
101     {
102       do
103         s = fcntl (fds[i], F_SETFD, FD_CLOEXEC);
104       while (s < 0 && errno == EINTR);
105
106       if (s < 0)
107         {
108           g_warning ("Error setting close-on-exec flag on incoming fd: %s",
109                      g_strerror (errno));
110           return NULL;
111         }
112     }
113
114   list = g_unix_fd_list_new_from_array (fds, n);
115   message = g_unix_fd_message_new_with_fd_list (list);
116   g_object_unref (list);
117
118   return message;
119 }
120
121 static void
122 g_unix_fd_message_serialize (GSocketControlMessage *message,
123                              gpointer               data)
124 {
125   GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
126   const gint *fds;
127   gint n_fds;
128
129   fds = g_unix_fd_list_peek_fds (fd_message->priv->list, &n_fds);
130   memcpy (data, fds, sizeof (gint) * n_fds);
131 }
132
133 static void
134 g_unix_fd_message_set_property (GObject *object, guint prop_id,
135                                 const GValue *value, GParamSpec *pspec)
136 {
137   GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
138
139   g_assert (message->priv->list == NULL);
140   g_assert_cmpint (prop_id, ==, 1);
141
142   message->priv->list = g_value_dup_object (value);
143
144   if (message->priv->list == NULL)
145     message->priv->list = g_unix_fd_list_new ();
146 }
147
148 /**
149  * g_unix_fd_message_get_fd_list:
150  * @message: a #GUnixFDMessage
151  *
152  * Gets the #GUnixFDList contained in @message.  This function does not
153  * return a reference to the caller, but the returned list is valid for
154  * the lifetime of @message.
155  *
156  * Returns: the #GUnixFDList from @message
157  *
158  * Since: 2.24
159  **/
160 GUnixFDList *
161 g_unix_fd_message_get_fd_list (GUnixFDMessage *message)
162 {
163   return message->priv->list;
164 }
165
166 static void
167 g_unix_fd_message_get_property (GObject *object, guint prop_id,
168                                 GValue *value, GParamSpec *pspec)
169 {
170   GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
171
172   g_assert_cmpint (prop_id, ==, 1);
173
174   g_value_set_object (value, g_unix_fd_message_get_fd_list (message));
175 }
176
177 static void
178 g_unix_fd_message_init (GUnixFDMessage *message)
179 {
180   message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
181                                                G_TYPE_UNIX_FD_MESSAGE,
182                                                GUnixFDMessagePrivate);
183 }
184
185 static void
186 g_unix_fd_message_finalize (GObject *object)
187 {
188   GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
189
190   g_object_unref (message->priv->list);
191
192   G_OBJECT_CLASS (g_unix_fd_message_parent_class)
193     ->finalize (object);
194 }
195
196 static void
197 g_unix_fd_message_class_init (GUnixFDMessageClass *class)
198 {
199   GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
200   GObjectClass *object_class = G_OBJECT_CLASS (class);
201
202   g_type_class_add_private (class, sizeof (GUnixFDMessagePrivate));
203   scm_class->get_size = g_unix_fd_message_get_size;
204   scm_class->get_level = g_unix_fd_message_get_level;
205   scm_class->get_type = g_unix_fd_message_get_msg_type;
206   scm_class->serialize = g_unix_fd_message_serialize;
207   scm_class->deserialize = g_unix_fd_message_deserialize;
208   object_class->finalize = g_unix_fd_message_finalize;
209   object_class->set_property = g_unix_fd_message_set_property;
210   object_class->get_property = g_unix_fd_message_get_property;
211
212   g_object_class_install_property (object_class, 1,
213     g_param_spec_object ("fd-list", "file descriptor list",
214                          "The GUnixFDList object to send with the message",
215                          G_TYPE_UNIX_FD_LIST, G_PARAM_STATIC_STRINGS |
216                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
217 }
218
219 /**
220  * g_unix_fd_message_new:
221  *
222  * Creates a new #GUnixFDMessage containing an empty file descriptor
223  * list.
224  *
225  * Returns: a new #GUnixFDMessage
226  *
227  * Since: 2.22
228  **/
229 GSocketControlMessage *
230 g_unix_fd_message_new (void)
231 {
232   return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
233 }
234
235 /**
236  * g_unix_fd_message_new_with_fd_list:
237  * @fd_list: a #GUnixFDList
238  *
239  * Creates a new #GUnixFDMessage containing @list.
240  *
241  * Returns: a new #GUnixFDMessage
242  *
243  * Since: 2.24
244  **/
245 GSocketControlMessage *
246 g_unix_fd_message_new_with_fd_list (GUnixFDList *fd_list)
247 {
248   return g_object_new (G_TYPE_UNIX_FD_MESSAGE,
249                        "fd-list", fd_list,
250                        NULL);
251 }
252
253 /**
254  * g_unix_fd_message_steal_fds:
255  * @message: a #GUnixFDMessage
256  * @length: pointer to the length of the returned array, or %NULL
257  *
258  * Returns the array of file descriptors that is contained in this
259  * object.
260  *
261  * After this call, the descriptors are no longer contained in
262  * @message. Further calls will return an empty list (unless more
263  * descriptors have been added).
264  *
265  * The return result of this function must be freed with g_free().
266  * The caller is also responsible for closing all of the file
267  * descriptors.
268  *
269  * If @length is non-%NULL then it is set to the number of file
270  * descriptors in the returned array. The returned array is also
271  * terminated with -1.
272  *
273  * This function never returns %NULL. In case there are no file
274  * descriptors contained in @message, an empty array is returned.
275  *
276  * Returns: an array of file descriptors
277  *
278  * Since: 2.22
279  **/
280 gint *
281 g_unix_fd_message_steal_fds (GUnixFDMessage *message,
282                              gint           *length)
283 {
284   g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);
285
286   return g_unix_fd_list_steal_fds (message->priv->list, length);
287 }
288
289 /**
290  * g_unix_fd_message_append_fd:
291  * @message: a #GUnixFDMessage
292  * @fd: a valid open file descriptor
293  * @error: a #GError pointer
294  *
295  * Adds a file descriptor to @message.
296  *
297  * The file descriptor is duplicated using dup(). You keep your copy
298  * of the descriptor and the copy contained in @message will be closed
299  * when @message is finalized.
300  *
301  * A possible cause of failure is exceeding the per-process or
302  * system-wide file descriptor limit.
303  *
304  * Returns: %TRUE in case of success, else %FALSE (and @error is set)
305  *
306  * Since: 2.22
307  **/
308 gboolean
309 g_unix_fd_message_append_fd (GUnixFDMessage  *message,
310                              gint             fd,
311                              GError         **error)
312 {
313   g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);
314
315   return g_unix_fd_list_append (message->priv->list, fd, error) > 0;
316 }
317
318 #define __G_UNIX_FD_MESSAGE_C__
319 #include "gioaliasdef.c"