Improve the g_file_make_symbolic_link docs
[platform/upstream/glib.git] / gio / gunixcredentialsmessage.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2010 Red Hat, Inc.
4  * Copyright (C) 2009 Codethink Limited
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published
8  * by the Free Software Foundation; either version 2 of the licence or (at
9  * your option) any later version.
10  *
11  * See the included COPYING file for more information.
12  *
13  * Authors: David Zeuthen <davidz@redhat.com>
14  */
15
16 /**
17  * SECTION: gunixcredentialsmessage
18  * @title: GUnixCredentialsMessage
19  * @short_description: A GSocketControlMessage containing credentials
20  * @include: gio/gunixcredentialsmessage.h
21  * @see_also: #GUnixConnection, #GSocketControlMessage
22  *
23  * This #GSocketControlMessage contains a #GCredentials instance.  It
24  * may be sent using g_socket_send_message() and received using
25  * g_socket_receive_message() over UNIX sockets (ie: sockets in the
26  * %G_SOCKET_ADDRESS_UNIX family).
27  *
28  * For an easier way to send and receive credentials over
29  * stream-oriented UNIX sockets, see g_unix_connection_send_credentials() and
30  * g_unix_connection_receive_credentials().
31  **/
32
33 #include "config.h"
34
35 /* ---------------------------------------------------------------------------------------------------- */
36 #ifdef __linux__
37
38 #define _GNU_SOURCE
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <sys/un.h>
42 #include <unistd.h>
43 #include <fcntl.h>
44 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 1
45
46 #else
47 /* TODO: please add support for your UNIX flavor */
48 #define G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED 0
49 #endif
50
51 /* ---------------------------------------------------------------------------------------------------- */
52
53 #include <string.h>
54 #include <errno.h>
55
56 #include "gunixcredentialsmessage.h"
57 #include "gcredentials.h"
58
59 #include "glibintl.h"
60 #include "gioalias.h"
61
62 struct _GUnixCredentialsMessagePrivate
63 {
64   GCredentials *credentials;
65 };
66
67 enum
68 {
69   PROP_0,
70   PROP_CREDENTIALS
71 };
72
73 G_DEFINE_TYPE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE);
74
75 static gsize
76 g_unix_credentials_message_get_size (GSocketControlMessage *message)
77 {
78 #ifdef __linux__
79   return sizeof (struct ucred);
80 #else
81   return 0;
82 #endif
83 }
84
85 static int
86 g_unix_credentials_message_get_level (GSocketControlMessage *message)
87 {
88 #ifdef __linux__
89   return SOL_SOCKET;
90 #else
91   return 0;
92 #endif
93 }
94
95 static int
96 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
97 {
98 #ifdef __linux__
99   return SCM_CREDENTIALS;
100 #else
101   return 0;
102 #endif
103 }
104
105 static GSocketControlMessage *
106 g_unix_credentials_message_deserialize (gint     level,
107                                         gint     type,
108                                         gsize    size,
109                                         gpointer data)
110 {
111   GSocketControlMessage *message;
112
113   message = NULL;
114
115 #ifdef __linux__
116   {
117     GCredentials *credentials;
118     struct ucred *ucred;
119
120     if (level != SOL_SOCKET || type != SCM_CREDENTIALS)
121       goto out;
122
123     if (size != sizeof (struct ucred))
124       {
125         g_warning ("Expected a struct ucred (%" G_GSIZE_FORMAT " bytes) but "
126                    "got %" G_GSIZE_FORMAT " bytes of data",
127                    sizeof (struct ucred),
128                    size);
129         goto out;
130       }
131
132     ucred = data;
133
134     credentials = g_credentials_new ();
135     g_credentials_set_native (credentials, ucred);
136     message = g_unix_credentials_message_new_with_credentials (credentials);
137     g_object_unref (credentials);
138  out:
139     ;
140   }
141 #endif
142
143   return message;
144 }
145
146 static void
147 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
148                                       gpointer               data)
149 {
150   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
151 #ifdef __linux__
152   memcpy (data, g_credentials_get_native (message->priv->credentials), sizeof (struct ucred));
153 #endif
154 }
155
156 static void
157 g_unix_credentials_message_finalize (GObject *object)
158 {
159   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
160
161   if (message->priv->credentials != NULL)
162     g_object_unref (message->priv->credentials);
163
164   G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
165 }
166
167 static void
168 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
169 {
170   message->priv = G_TYPE_INSTANCE_GET_PRIVATE (message,
171                                                G_TYPE_UNIX_CREDENTIALS_MESSAGE,
172                                                GUnixCredentialsMessagePrivate);
173 }
174
175 static void
176 g_unix_credentials_message_get_property (GObject    *object,
177                                          guint       prop_id,
178                                          GValue     *value,
179                                          GParamSpec *pspec)
180 {
181   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
182
183   switch (prop_id)
184     {
185     case PROP_CREDENTIALS:
186       g_value_set_object (value, message->priv->credentials);
187       break;
188
189     default:
190       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
191       break;
192     }
193 }
194
195 static void
196 g_unix_credentials_message_set_property (GObject      *object,
197                                          guint         prop_id,
198                                          const GValue *value,
199                                          GParamSpec   *pspec)
200 {
201   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
202
203   switch (prop_id)
204     {
205     case PROP_CREDENTIALS:
206       message->priv->credentials = g_value_dup_object (value);
207       break;
208
209     default:
210       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
211       break;
212     }
213 }
214
215 static void
216 g_unix_credentials_message_constructed (GObject *object)
217 {
218   GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
219
220   if (message->priv->credentials == NULL)
221     message->priv->credentials = g_credentials_new ();
222
223   if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
224     G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
225 }
226
227 static void
228 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
229 {
230   GSocketControlMessageClass *scm_class;
231   GObjectClass *gobject_class;
232
233   g_type_class_add_private (class, sizeof (GUnixCredentialsMessagePrivate));
234
235   gobject_class = G_OBJECT_CLASS (class);
236   gobject_class->get_property = g_unix_credentials_message_get_property;
237   gobject_class->set_property = g_unix_credentials_message_set_property;
238   gobject_class->finalize = g_unix_credentials_message_finalize;
239   gobject_class->constructed = g_unix_credentials_message_constructed;
240
241   scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
242   scm_class->get_size = g_unix_credentials_message_get_size;
243   scm_class->get_level = g_unix_credentials_message_get_level;
244   scm_class->get_type = g_unix_credentials_message_get_msg_type;
245   scm_class->serialize = g_unix_credentials_message_serialize;
246   scm_class->deserialize = g_unix_credentials_message_deserialize;
247
248   /**
249    * GUnixCredentialsMessage:credentials:
250    *
251    * The credentials stored in the message.
252    *
253    * Since: 2.26
254    */
255   g_object_class_install_property (gobject_class,
256                                    PROP_CREDENTIALS,
257                                    g_param_spec_object ("credentials",
258                                                         P_("Credentials"),
259                                                         P_("The credentials stored in the message"),
260                                                         G_TYPE_CREDENTIALS,
261                                                         G_PARAM_READABLE |
262                                                         G_PARAM_WRITABLE |
263                                                         G_PARAM_CONSTRUCT_ONLY |
264                                                         G_PARAM_STATIC_NAME |
265                                                         G_PARAM_STATIC_BLURB |
266                                                         G_PARAM_STATIC_NICK));
267
268 }
269
270 /* ---------------------------------------------------------------------------------------------------- */
271
272 /**
273  * g_unix_credentials_message_is_supported:
274  *
275  * Checks if passing a #GCredential on a #GSocket is supported on this platform.
276  *
277  * Returns: %TRUE if supported, %FALSE otherwise
278  *
279  * Since: 2.26
280  */
281 gboolean
282 g_unix_credentials_message_is_supported (void)
283 {
284   return G_UNIX_CREDENTIALS_MESSAGE_SUPPORTED;
285 }
286
287 /* ---------------------------------------------------------------------------------------------------- */
288
289 /**
290  * g_unix_credentials_message_new:
291  *
292  * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
293  *
294  * Returns: a new #GUnixCredentialsMessage
295  *
296  * Since: 2.26
297  */
298 GSocketControlMessage *
299 g_unix_credentials_message_new (void)
300 {
301   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
302   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
303                        NULL);
304 }
305
306 /**
307  * g_unix_credentials_message_new_with_credentials:
308  * @credentials: A #GCredentials object.
309  *
310  * Creates a new #GUnixCredentialsMessage holding @credentials.
311  *
312  * Returns: a new #GUnixCredentialsMessage
313  *
314  * Since: 2.26
315  */
316 GSocketControlMessage *
317 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
318 {
319   g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
320   g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
321   return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
322                        "credentials", credentials,
323                        NULL);
324 }
325
326 /**
327  * g_unix_credentials_message_get_credentials:
328  * @message: A #GUnixCredentialsMessage.
329  *
330  * Gets the credentials stored in @message.
331  *
332  * Returns: A #GCredentials instance. Do not free, it is owned by @message.
333  *
334  * Since: 2.26
335  */
336 GCredentials *
337 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
338 {
339   g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
340   return message->priv->credentials;
341 }
342
343
344 #define __G_UNIX_CREDENTIALS_MESSAGE_C__
345 #include "gioaliasdef.c"