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