1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * See the included COPYING file for more information.
12 * Authors: Ryan Lortie <desrt@desrt.ca>
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
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.
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().
32 * Note that `<gio/gunixfdmessage.h>` belongs to the UNIX-specific GIO
33 * interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
40 * #GUnixFDMessage is an opaque data structure and can only be accessed
41 * using the following functions.
51 #include "gunixfdmessage.h"
52 #include "gunixfdlist.h"
53 #include "gnetworking.h"
56 struct _GUnixFDMessagePrivate
61 G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDMessage
, g_unix_fd_message
, G_TYPE_SOCKET_CONTROL_MESSAGE
)
64 g_unix_fd_message_get_size (GSocketControlMessage
*message
)
66 GUnixFDMessage
*fd_message
= G_UNIX_FD_MESSAGE (message
);
68 return g_unix_fd_list_get_length (fd_message
->priv
->list
) * sizeof (gint
);
72 g_unix_fd_message_get_level (GSocketControlMessage
*message
)
78 g_unix_fd_message_get_msg_type (GSocketControlMessage
*message
)
83 static GSocketControlMessage
*
84 g_unix_fd_message_deserialize (int level
,
89 GSocketControlMessage
*message
;
94 if (level
!= SOL_SOCKET
||
100 g_warning ("Kernel returned non-integral number of fds");
105 n
= size
/ sizeof (gint
);
107 /* Note we probably handled this in gsocket.c already if we're on
108 * Linux and have MSG_CMSG_CLOEXEC, but this code remains as a fallback
109 * in case the kernel is too old for MSG_CMSG_CLOEXEC.
111 for (i
= 0; i
< n
; i
++)
117 s
= fcntl (fds
[i
], F_SETFD
, FD_CLOEXEC
);
120 while (s
< 0 && errsv
== EINTR
);
124 g_warning ("Error setting close-on-exec flag on incoming fd: %s",
130 list
= g_unix_fd_list_new_from_array (fds
, n
);
131 message
= g_unix_fd_message_new_with_fd_list (list
);
132 g_object_unref (list
);
138 g_unix_fd_message_serialize (GSocketControlMessage
*message
,
141 GUnixFDMessage
*fd_message
= G_UNIX_FD_MESSAGE (message
);
145 fds
= g_unix_fd_list_peek_fds (fd_message
->priv
->list
, &n_fds
);
146 memcpy (data
, fds
, sizeof (gint
) * n_fds
);
150 g_unix_fd_message_set_property (GObject
*object
, guint prop_id
,
151 const GValue
*value
, GParamSpec
*pspec
)
153 GUnixFDMessage
*message
= G_UNIX_FD_MESSAGE (object
);
155 g_assert (message
->priv
->list
== NULL
);
156 g_assert_cmpint (prop_id
, ==, 1);
158 message
->priv
->list
= g_value_dup_object (value
);
160 if (message
->priv
->list
== NULL
)
161 message
->priv
->list
= g_unix_fd_list_new ();
165 * g_unix_fd_message_get_fd_list:
166 * @message: a #GUnixFDMessage
168 * Gets the #GUnixFDList contained in @message. This function does not
169 * return a reference to the caller, but the returned list is valid for
170 * the lifetime of @message.
172 * Returns: (transfer none): the #GUnixFDList from @message
177 g_unix_fd_message_get_fd_list (GUnixFDMessage
*message
)
179 return message
->priv
->list
;
183 g_unix_fd_message_get_property (GObject
*object
, guint prop_id
,
184 GValue
*value
, GParamSpec
*pspec
)
186 GUnixFDMessage
*message
= G_UNIX_FD_MESSAGE (object
);
188 g_assert_cmpint (prop_id
, ==, 1);
190 g_value_set_object (value
, g_unix_fd_message_get_fd_list (message
));
194 g_unix_fd_message_init (GUnixFDMessage
*message
)
196 message
->priv
= g_unix_fd_message_get_instance_private (message
);
200 g_unix_fd_message_finalize (GObject
*object
)
202 GUnixFDMessage
*message
= G_UNIX_FD_MESSAGE (object
);
204 g_object_unref (message
->priv
->list
);
206 G_OBJECT_CLASS (g_unix_fd_message_parent_class
)
211 g_unix_fd_message_class_init (GUnixFDMessageClass
*class)
213 GSocketControlMessageClass
*scm_class
= G_SOCKET_CONTROL_MESSAGE_CLASS (class);
214 GObjectClass
*object_class
= G_OBJECT_CLASS (class);
216 scm_class
->get_size
= g_unix_fd_message_get_size
;
217 scm_class
->get_level
= g_unix_fd_message_get_level
;
218 scm_class
->get_type
= g_unix_fd_message_get_msg_type
;
219 scm_class
->serialize
= g_unix_fd_message_serialize
;
220 scm_class
->deserialize
= g_unix_fd_message_deserialize
;
221 object_class
->finalize
= g_unix_fd_message_finalize
;
222 object_class
->set_property
= g_unix_fd_message_set_property
;
223 object_class
->get_property
= g_unix_fd_message_get_property
;
225 g_object_class_install_property (object_class
, 1,
226 g_param_spec_object ("fd-list", "file descriptor list",
227 "The GUnixFDList object to send with the message",
228 G_TYPE_UNIX_FD_LIST
, G_PARAM_STATIC_STRINGS
|
229 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT_ONLY
));
233 * g_unix_fd_message_new:
235 * Creates a new #GUnixFDMessage containing an empty file descriptor
238 * Returns: a new #GUnixFDMessage
242 GSocketControlMessage
*
243 g_unix_fd_message_new (void)
245 return g_object_new (G_TYPE_UNIX_FD_MESSAGE
, NULL
);
249 * g_unix_fd_message_new_with_fd_list:
250 * @fd_list: a #GUnixFDList
252 * Creates a new #GUnixFDMessage containing @list.
254 * Returns: a new #GUnixFDMessage
258 GSocketControlMessage
*
259 g_unix_fd_message_new_with_fd_list (GUnixFDList
*fd_list
)
261 return g_object_new (G_TYPE_UNIX_FD_MESSAGE
,
267 * g_unix_fd_message_steal_fds:
268 * @message: a #GUnixFDMessage
269 * @length: (out) (optional): pointer to the length of the returned
272 * Returns the array of file descriptors that is contained in this
275 * After this call, the descriptors are no longer contained in
276 * @message. Further calls will return an empty list (unless more
277 * descriptors have been added).
279 * The return result of this function must be freed with g_free().
280 * The caller is also responsible for closing all of the file
283 * If @length is non-%NULL then it is set to the number of file
284 * descriptors in the returned array. The returned array is also
285 * terminated with -1.
287 * This function never returns %NULL. In case there are no file
288 * descriptors contained in @message, an empty array is returned.
290 * Returns: (array length=length) (transfer full): an array of file
296 g_unix_fd_message_steal_fds (GUnixFDMessage
*message
,
299 g_return_val_if_fail (G_UNIX_FD_MESSAGE (message
), NULL
);
301 return g_unix_fd_list_steal_fds (message
->priv
->list
, length
);
305 * g_unix_fd_message_append_fd:
306 * @message: a #GUnixFDMessage
307 * @fd: a valid open file descriptor
308 * @error: a #GError pointer
310 * Adds a file descriptor to @message.
312 * The file descriptor is duplicated using dup(). You keep your copy
313 * of the descriptor and the copy contained in @message will be closed
314 * when @message is finalized.
316 * A possible cause of failure is exceeding the per-process or
317 * system-wide file descriptor limit.
319 * Returns: %TRUE in case of success, else %FALSE (and @error is set)
324 g_unix_fd_message_append_fd (GUnixFDMessage
*message
,
328 g_return_val_if_fail (G_UNIX_FD_MESSAGE (message
), FALSE
);
330 return g_unix_fd_list_append (message
->priv
->list
, fd
, error
) >= 0;