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