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