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/types.h>
35 #include <sys/socket.h>
40 #include "gunixfdmessage.h"
46 G_DEFINE_TYPE (GUnixFDMessage, g_unix_fd_message,
47 G_TYPE_SOCKET_CONTROL_MESSAGE);
49 struct _GUnixFDMessagePrivate
56 g_unix_fd_message_get_size (GSocketControlMessage *message)
58 GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
60 return fd_message->priv->nfd * sizeof (gint);
64 g_unix_fd_message_get_level (GSocketControlMessage *message)
70 g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
75 static GSocketControlMessage *
76 g_unix_fd_message_deserialize (int level,
81 GUnixFDMessage *message;
83 if (level != SOL_SOCKET ||
89 g_warning ("Kernel returned non-integral number of fds");
93 message = g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
94 message->priv->nfd = size / sizeof (gint);
95 message->priv->fds = g_new (gint, message->priv->nfd + 1);
96 memcpy (message->priv->fds, data, size);
97 message->priv->fds[message->priv->nfd] = -1;
99 return G_SOCKET_CONTROL_MESSAGE (message);
103 g_unix_fd_message_serialize (GSocketControlMessage *message,
106 GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
107 memcpy (data, fd_message->priv->fds,
108 sizeof (gint) * fd_message->priv->nfd);
111 g_unix_fd_message_init (GUnixFDMessage *message)
113 message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
114 G_TYPE_UNIX_FD_MESSAGE,
115 GUnixFDMessagePrivate);
119 g_unix_fd_message_finalize (GObject *object)
121 GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
124 for (i = 0; i < message->priv->nfd; i++)
125 close (message->priv->fds[i]);
126 g_free (message->priv->fds);
128 G_OBJECT_CLASS (g_unix_fd_message_parent_class)
133 g_unix_fd_message_class_init (GUnixFDMessageClass *class)
135 GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
136 GObjectClass *object_class = G_OBJECT_CLASS (class);
138 g_type_class_add_private (class, sizeof (GUnixFDMessagePrivate));
139 scm_class->get_size = g_unix_fd_message_get_size;
140 scm_class->get_level = g_unix_fd_message_get_level;
141 scm_class->get_type = g_unix_fd_message_get_msg_type;
142 scm_class->serialize = g_unix_fd_message_serialize;
143 scm_class->deserialize = g_unix_fd_message_deserialize;
144 object_class->finalize = g_unix_fd_message_finalize;
148 * g_unix_fd_message_new:
150 * Creates a new #GUnixFDMessage containing no file descriptors.
152 * Returns: a new #GUnixFDMessage
156 GSocketControlMessage *
157 g_unix_fd_message_new (void)
159 return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
163 * g_unix_fd_message_steal_fds:
164 * @message: a #GUnixFDMessage
165 * @length: pointer to the length of the returned array, or %NULL
167 * Returns the array of file descriptors that is contained in this
170 * After this call, the descriptors are no longer contained in
171 * @message. Further calls will return an empty list (unless more
172 * descriptors have been added).
174 * The return result of this function must be freed with g_free().
175 * The caller is also responsible for closing all of the file
178 * If @length is non-%NULL then it is set to the number of file
179 * descriptors in the returned array. The returned array is also
180 * terminated with -1.
182 * This function never returns %NULL. In case there are no file
183 * descriptors contained in @message, an empty array is returned.
185 * Returns: an array of file descriptors
190 g_unix_fd_message_steal_fds (GUnixFDMessage *message,
195 g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), NULL);
197 /* will be true for fresh object or if we were just called */
198 if (message->priv->fds == NULL)
200 message->priv->fds = g_new (gint, 1);
201 message->priv->fds[0] = -1;
202 message->priv->nfd = 0;
206 *length = message->priv->nfd;
207 result = message->priv->fds;
209 message->priv->fds = NULL;
210 message->priv->nfd = 0;
216 * g_unix_fd_message_append_fd:
217 * @message: a #GUnixFDMessage
218 * @fd: a valid open file descriptor
219 * @error: a #GError pointer
221 * Adds a file descriptor to @message.
223 * The file descriptor is duplicated using dup(). You keep your copy
224 * of the descriptor and the copy contained in @message will be closed
225 * when @message is finalized.
227 * A possible cause of failure is exceeding the per-process or
228 * system-wide file descriptor limit.
230 * Returns: %TRUE in case of success, else %FALSE (and @error is set)
235 g_unix_fd_message_append_fd (GUnixFDMessage *message,
241 g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), FALSE);
242 g_return_val_if_fail (fd >= 0, FALSE);
246 while (new_fd < 0 && (errno == EINTR));
250 int saved_errno = errno;
252 g_set_error (error, G_IO_ERROR,
253 g_io_error_from_errno (saved_errno),
254 "dup: %s", g_strerror (saved_errno));
259 message->priv->fds = g_realloc (message->priv->fds,
261 (message->priv->nfd + 2));
262 message->priv->fds[message->priv->nfd++] = new_fd;
263 message->priv->fds[message->priv->nfd] = -1;
268 #define __G_UNIX_FD_MESSAGE_C__
269 #include "gioaliasdef.c"