unicode: Simplify width table generation
[glib.git] / gio / gunixfdmessage.c
blob12e62c53f5032c9824cfcebb898079d6c087a104
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright © 2009 Codethink Limited
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.
10 * See the included COPYING file for more information.
12 * Authors: Ryan Lortie <desrt@desrt.ca>
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
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
34 * file when using it.
37 #include "config.h"
39 #include <unistd.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <errno.h>
44 #include "gunixfdmessage.h"
45 #include "gunixfdlist.h"
46 #include "gnetworking.h"
47 #include "gioerror.h"
49 struct _GUnixFDMessagePrivate
51 GUnixFDList *list;
54 G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDMessage, g_unix_fd_message, G_TYPE_SOCKET_CONTROL_MESSAGE)
56 static gsize
57 g_unix_fd_message_get_size (GSocketControlMessage *message)
59 GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
61 return g_unix_fd_list_get_length (fd_message->priv->list) * sizeof (gint);
64 static int
65 g_unix_fd_message_get_level (GSocketControlMessage *message)
67 return SOL_SOCKET;
70 static int
71 g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
73 return SCM_RIGHTS;
76 static GSocketControlMessage *
77 g_unix_fd_message_deserialize (int level,
78 int type,
79 gsize size,
80 gpointer data)
82 GSocketControlMessage *message;
83 GUnixFDList *list;
84 gint n, s, i;
85 gint *fds;
87 if (level != SOL_SOCKET ||
88 type != SCM_RIGHTS)
89 return NULL;
91 if (size % 4 > 0)
93 g_warning ("Kernel returned non-integral number of fds");
94 return NULL;
97 fds = data;
98 n = size / sizeof (gint);
100 /* Note we probably handled this in gsocket.c already if we're on
101 * Linux and have MSG_CMSG_CLOEXEC, but this code remains as a fallback
102 * in case the kernel is too old for MSG_CMSG_CLOEXEC.
104 for (i = 0; i < n; i++)
107 s = fcntl (fds[i], F_SETFD, FD_CLOEXEC);
108 while (s < 0 && errno == EINTR);
110 if (s < 0)
112 g_warning ("Error setting close-on-exec flag on incoming fd: %s",
113 g_strerror (errno));
114 return NULL;
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);
122 return message;
125 static void
126 g_unix_fd_message_serialize (GSocketControlMessage *message,
127 gpointer data)
129 GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
130 const gint *fds;
131 gint n_fds;
133 fds = g_unix_fd_list_peek_fds (fd_message->priv->list, &n_fds);
134 memcpy (data, fds, sizeof (gint) * n_fds);
137 static void
138 g_unix_fd_message_set_property (GObject *object, guint prop_id,
139 const GValue *value, GParamSpec *pspec)
141 GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
143 g_assert (message->priv->list == NULL);
144 g_assert_cmpint (prop_id, ==, 1);
146 message->priv->list = g_value_dup_object (value);
148 if (message->priv->list == NULL)
149 message->priv->list = g_unix_fd_list_new ();
153 * g_unix_fd_message_get_fd_list:
154 * @message: a #GUnixFDMessage
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.
160 * Returns: (transfer none): the #GUnixFDList from @message
162 * Since: 2.24
164 GUnixFDList *
165 g_unix_fd_message_get_fd_list (GUnixFDMessage *message)
167 return message->priv->list;
170 static void
171 g_unix_fd_message_get_property (GObject *object, guint prop_id,
172 GValue *value, GParamSpec *pspec)
174 GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
176 g_assert_cmpint (prop_id, ==, 1);
178 g_value_set_object (value, g_unix_fd_message_get_fd_list (message));
181 static void
182 g_unix_fd_message_init (GUnixFDMessage *message)
184 message->priv = g_unix_fd_message_get_instance_private (message);
187 static void
188 g_unix_fd_message_finalize (GObject *object)
190 GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
192 g_object_unref (message->priv->list);
194 G_OBJECT_CLASS (g_unix_fd_message_parent_class)
195 ->finalize (object);
198 static void
199 g_unix_fd_message_class_init (GUnixFDMessageClass *class)
201 GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
202 GObjectClass *object_class = G_OBJECT_CLASS (class);
204 scm_class->get_size = g_unix_fd_message_get_size;
205 scm_class->get_level = g_unix_fd_message_get_level;
206 scm_class->get_type = g_unix_fd_message_get_msg_type;
207 scm_class->serialize = g_unix_fd_message_serialize;
208 scm_class->deserialize = g_unix_fd_message_deserialize;
209 object_class->finalize = g_unix_fd_message_finalize;
210 object_class->set_property = g_unix_fd_message_set_property;
211 object_class->get_property = g_unix_fd_message_get_property;
213 g_object_class_install_property (object_class, 1,
214 g_param_spec_object ("fd-list", "file descriptor list",
215 "The GUnixFDList object to send with the message",
216 G_TYPE_UNIX_FD_LIST, G_PARAM_STATIC_STRINGS |
217 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
221 * g_unix_fd_message_new:
223 * Creates a new #GUnixFDMessage containing an empty file descriptor
224 * list.
226 * Returns: a new #GUnixFDMessage
228 * Since: 2.22
230 GSocketControlMessage *
231 g_unix_fd_message_new (void)
233 return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
237 * g_unix_fd_message_new_with_fd_list:
238 * @fd_list: a #GUnixFDList
240 * Creates a new #GUnixFDMessage containing @list.
242 * Returns: a new #GUnixFDMessage
244 * Since: 2.24
246 GSocketControlMessage *
247 g_unix_fd_message_new_with_fd_list (GUnixFDList *fd_list)
249 return g_object_new (G_TYPE_UNIX_FD_MESSAGE,
250 "fd-list", fd_list,
251 NULL);
255 * g_unix_fd_message_steal_fds:
256 * @message: a #GUnixFDMessage
257 * @length: (out) (allow-none): pointer to the length of the returned
258 * array, or %NULL
260 * Returns the array of file descriptors that is contained in this
261 * object.
263 * After this call, the descriptors are no longer contained in
264 * @message. Further calls will return an empty list (unless more
265 * descriptors have been added).
267 * The return result of this function must be freed with g_free().
268 * The caller is also responsible for closing all of the file
269 * descriptors.
271 * If @length is non-%NULL then it is set to the number of file
272 * descriptors in the returned array. The returned array is also
273 * terminated with -1.
275 * This function never returns %NULL. In case there are no file
276 * descriptors contained in @message, an empty array is returned.
278 * Returns: (array length=length) (transfer full): an array of file
279 * descriptors
281 * Since: 2.22
283 gint *
284 g_unix_fd_message_steal_fds (GUnixFDMessage *message,
285 gint *length)
287 g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), NULL);
289 return g_unix_fd_list_steal_fds (message->priv->list, length);
293 * g_unix_fd_message_append_fd:
294 * @message: a #GUnixFDMessage
295 * @fd: a valid open file descriptor
296 * @error: a #GError pointer
298 * Adds a file descriptor to @message.
300 * The file descriptor is duplicated using dup(). You keep your copy
301 * of the descriptor and the copy contained in @message will be closed
302 * when @message is finalized.
304 * A possible cause of failure is exceeding the per-process or
305 * system-wide file descriptor limit.
307 * Returns: %TRUE in case of success, else %FALSE (and @error is set)
309 * Since: 2.22
311 gboolean
312 g_unix_fd_message_append_fd (GUnixFDMessage *message,
313 gint fd,
314 GError **error)
316 g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);
318 return g_unix_fd_list_append (message->priv->list, fd, error) >= 0;