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>
16 * SECTION: gunixfdmessage
17 * @title: GUnixFDMessage
18 * @short_description: a #GSocketControlMessage containing a list of
20 * @see_also: #GUnixConnection
22 * This #GSocketControlMessage contains a list of file descriptors.
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).
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().
34 #include <sys/socket.h>
39 #include "gunixfdmessage.h"
45 G_DEFINE_TYPE (GUnixFDMessage, g_unix_fd_message,
46 G_TYPE_SOCKET_CONTROL_MESSAGE);
48 struct _GUnixFDMessagePrivate
55 g_unix_fd_message_get_size (GSocketControlMessage *message)
57 GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
59 return fd_message->priv->nfd * sizeof (gint);
63 g_unix_fd_message_get_level (GSocketControlMessage *message)
69 g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
74 static GSocketControlMessage *
75 g_unix_fd_message_deserialize (int level,
80 GUnixFDMessage *message;
82 if (level != SOL_SOCKET ||
88 g_warning ("Kernel returned non-integral number of fds");
92 message = g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
93 message->priv->nfd = size / sizeof (gint);
94 message->priv->fds = g_new (gint, message->priv->nfd + 1);
95 memcpy (message->priv->fds, data, size);
96 message->priv->fds[message->priv->nfd] = -1;
98 return G_SOCKET_CONTROL_MESSAGE (message);
102 g_unix_fd_message_serialize (GSocketControlMessage *message,
105 GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
106 memcpy (data, fd_message->priv->fds,
107 sizeof (gint) * fd_message->priv->nfd);
110 g_unix_fd_message_init (GUnixFDMessage *message)
112 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
113 G_TYPE_UNIX_FD_MESSAGE,
114 GUnixFDMessagePrivate);
118 g_unix_fd_message_finalize (GObject *object)
120 GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
123 for (i = 0; i < message->priv->nfd; i++)
124 close (message->priv->fds[i]);
125 g_free (message->priv->fds);
127 G_OBJECT_CLASS (g_unix_fd_message_parent_class)
132 g_unix_fd_message_class_init (GUnixFDMessageClass *class)
134 GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
135 GObjectClass *object_class = G_OBJECT_CLASS (class);
137 g_type_class_add_private (class, sizeof (GUnixFDMessagePrivate));
138 scm_class->get_size = g_unix_fd_message_get_size;
139 scm_class->get_level = g_unix_fd_message_get_level;
140 scm_class->get_type = g_unix_fd_message_get_msg_type;
141 scm_class->serialize = g_unix_fd_message_serialize;
142 scm_class->deserialize = g_unix_fd_message_deserialize;
143 object_class->finalize = g_unix_fd_message_finalize;
147 * g_unix_fd_message_new:
149 * Creates a new #GUnixFDMessage containing no file descriptors.
151 * Returns: a new #GUnixFDMessage
155 GSocketControlMessage *
156 g_unix_fd_message_new (void)
158 return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
162 * g_unix_fd_message_steal_fds:
163 * @message: a #GUnixFDMessage
164 * @length: pointer to the length of the returned array, or %NULL
166 * Returns the array of file descriptors that is contained in this
169 * After this call, the descriptors are no longer contained in
170 * @message. Further calls will return an empty list (unless more
171 * descriptors have been added).
173 * The return result of this function must be freed with g_free().
174 * The caller is also responsible for closing all of the file
177 * If @length is non-%NULL then it is set to the number of file
178 * descriptors in the returned array. The returned array is also
179 * terminated with -1.
181 * This function never returns %NULL. In case there are no file
182 * descriptors contained in @message, an empty array is returned.
184 * Returns: an array of file descriptors
189 g_unix_fd_message_steal_fds (GUnixFDMessage *message,
194 g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), NULL);
196 /* will be true for fresh object or if we were just called */
197 if (message->priv->fds == NULL)
199 message->priv->fds = g_new (gint, 1);
200 message->priv->fds[0] = -1;
201 message->priv->nfd = 0;
205 *length = message->priv->nfd;
206 result = message->priv->fds;
208 message->priv->fds = NULL;
209 message->priv->nfd = 0;
215 * g_unix_fd_message_append_fd:
216 * @message: a #GUnixFDMessage
217 * @fd: a valid open file descriptor
218 * @error: a #GError pointer
220 * Adds a file descriptor to @message.
222 * The file descriptor is duplicated using dup(). You keep your copy
223 * of the descriptor and the copy contained in @message will be closed
224 * when @message is finalized.
226 * A possible cause of failure is exceeding the per-process or
227 * system-wide file descriptor limit.
229 * Returns: %TRUE in case of success, else %FALSE (and @error is set)
234 g_unix_fd_message_append_fd (GUnixFDMessage *message,
240 g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), FALSE);
241 g_return_val_if_fail (fd >= 0, FALSE);
245 while (new_fd < 0 && (errno == EINTR));
249 int saved_errno = errno;
251 g_set_error (error, G_IO_ERROR,
252 g_io_error_from_errno (saved_errno),
253 "dup: %s", g_strerror (saved_errno));
258 message->priv->fds = g_realloc (message->priv->fds,
260 (message->priv->nfd + 2));
261 message->priv->fds[message->priv->nfd++] = new_fd;
262 message->priv->fds[message->priv->nfd] = -1;
267 #define __G_UNIX_FD_MESSAGE_C__
268 #include "gioaliasdef.c"