Add support for abstract unix socket addresses
[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 list of
19  * file descriptors
20  * @see_also: #GUnixConnection
21  *
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).
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/socket.h>
35 #include <unistd.h>
36 #include <string.h>
37 #include <errno.h>
38
39 #include "gunixfdmessage.h"
40 #include "gioerror.h"
41
42 #include "gioalias.h"
43
44
45 G_DEFINE_TYPE (GUnixFDMessage, g_unix_fd_message,
46                G_TYPE_SOCKET_CONTROL_MESSAGE);
47
48 struct _GUnixFDMessagePrivate
49 {
50   gint *fds;
51   gint nfd;
52 };
53
54 static gsize
55 g_unix_fd_message_get_size (GSocketControlMessage *message)
56 {
57   GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
58
59   return fd_message->priv->nfd * sizeof (gint);
60 }
61
62 static int
63 g_unix_fd_message_get_level (GSocketControlMessage *message)
64 {
65   return SOL_SOCKET;
66 }
67
68 static int
69 g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
70 {
71   return SCM_RIGHTS;
72 }
73
74 static GSocketControlMessage *
75 g_unix_fd_message_deserialize (int level,
76                                int type,
77                                gsize size,
78                                gpointer data)
79 {
80   GUnixFDMessage *message;
81
82   if (level != SOL_SOCKET ||
83       level != SCM_RIGHTS)
84     return NULL;
85   
86   if (size % 4 > 0)
87     {
88       g_warning ("Kernel returned non-integral number of fds");
89       return NULL;
90     }
91
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;
97
98   return G_SOCKET_CONTROL_MESSAGE (message);
99 }
100
101 static void
102 g_unix_fd_message_serialize (GSocketControlMessage *message,
103                              gpointer               data)
104 {
105   GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
106   memcpy (data, fd_message->priv->fds,
107           sizeof (gint) * fd_message->priv->nfd);
108 }
109 static void
110 g_unix_fd_message_init (GUnixFDMessage *message)
111 {
112   message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
113                                                G_TYPE_UNIX_FD_MESSAGE,
114                                                GUnixFDMessagePrivate);
115 }
116
117 static void
118 g_unix_fd_message_finalize (GObject *object)
119 {
120   GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
121   gint i;
122
123   for (i = 0; i < message->priv->nfd; i++)
124     close (message->priv->fds[i]);
125   g_free (message->priv->fds);
126
127   G_OBJECT_CLASS (g_unix_fd_message_parent_class)
128     ->finalize (object);
129 }
130
131 static void
132 g_unix_fd_message_class_init (GUnixFDMessageClass *class)
133 {
134   GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
135   GObjectClass *object_class = G_OBJECT_CLASS (class);
136
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;
144 }
145
146 /**
147  * g_unix_fd_message_new:
148  * @returns: a new #GUnixFDMessage
149  *
150  * Creates a new #GUnixFDMessage containing no file descriptors.
151  **/
152 GSocketControlMessage *
153 g_unix_fd_message_new (void)
154 {
155   return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
156 }
157
158 /**
159  * g_unix_fd_message_steal_fds:
160  * @message: a #GUnixFDMessage
161  * @length: pointer to the length of the returned array, or %NULL
162  * @returns: an array of file descriptors
163  *
164  * Returns the array of file descriptors that is contained in this
165  * object.
166  *
167  * After this call, the descriptors are no longer contained in
168  * @message.  Further calls will return an empty list (unless more
169  * descriptors have been added).
170  *
171  * The return result of this function must be freed with g_free().
172  * The caller is also responsible for closing all of the file
173  * descriptors.
174  *
175  * If @length is non-%NULL then it is set to the number of file
176  * descriptors in the returned array.  The returned array is also
177  * terminated with -1.
178  *
179  * This function never returns NULL.  In case there are no file
180  * descriptors contained in @message, an empty array is returned.
181  **/
182 gint *
183 g_unix_fd_message_steal_fds (GUnixFDMessage *message,
184                                gint             *length)
185 {
186   gint *result;
187
188   g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), NULL);
189
190   /* will be true for fresh object or if we were just called */
191   if (message->priv->fds == NULL)
192     {
193       message->priv->fds = g_new (gint, 1);
194       message->priv->fds[0] = -1;
195       message->priv->nfd = 0;
196     }
197
198   if (length)
199     *length = message->priv->nfd;
200   result = message->priv->fds;
201
202   message->priv->fds = NULL;
203   message->priv->nfd = 0;
204
205   return result;
206 }
207
208 /**
209  * g_unix_fd_message_append_fd:
210  * @message: a #GUnixFDMessage
211  * @fd: a valid open file descriptor
212  * @error: a #GError pointer
213  * @returns: %TRUE in case of success, else %FALSE (and @error is set)
214  *
215  * Adds a file descriptor to @message.
216  *
217  * The file descriptor is duplicated using dup().  You keep your copy
218  * of the descriptor and the copy contained in @message will be closed
219  * when @message is finalized.
220  *
221  * A possible cause of failure is exceeding the per-process or
222  * system-wide file descriptor limit.
223  **/
224 gboolean
225 g_unix_fd_message_append_fd (GUnixFDMessage  *message,
226                                gint               fd,
227                                GError           **error)
228 {
229   gint new_fd;
230
231   g_return_val_if_fail (G_IS_UNIX_FD_MESSAGE (message), FALSE);
232   g_return_val_if_fail (fd >= 0, FALSE);
233
234   do
235     new_fd = dup (fd);
236   while (new_fd < 0 && (errno == EINTR));
237
238   if (fd < 0)
239     {
240       int saved_errno = errno;
241
242       g_set_error (error, G_IO_ERROR,
243                    g_io_error_from_errno (saved_errno),
244                    "dup: %s", g_strerror (saved_errno));
245
246       return FALSE;
247     }
248
249   message->priv->fds = g_realloc (message->priv->fds,
250                                   sizeof (gint) *
251                                    (message->priv->nfd + 2));
252   message->priv->fds[message->priv->nfd++] = new_fd;
253   message->priv->fds[message->priv->nfd] = -1;
254
255   return TRUE;
256 }
257
258 #define __G_UNIX_FD_MESSAGE_C__
259 #include "gioaliasdef.c"