gtestutils: Fix a typo in a documentation comment
[glib.git] / gio / gunixcredentialsmessage.c
blob77612f132848dac54793638a84643485153d07f3
1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (C) 2009 Codethink Limited
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.
11 * See the included COPYING file for more information.
13 * Authors: David Zeuthen <davidz@redhat.com>
16 /**
17 * SECTION:gunixcredentialsmessage
18 * @title: GUnixCredentialsMessage
19 * @short_description: A GSocketControlMessage containing credentials
20 * @include: gio/gunixcredentialsmessage.h
21 * @see_also: #GUnixConnection, #GSocketControlMessage
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_FAMILY_UNIX family).
28 * For an easier way to send and receive credentials over
29 * stream-oriented UNIX sockets, see
30 * g_unix_connection_send_credentials() and
31 * g_unix_connection_receive_credentials(). To receive credentials of
32 * a foreign process connected to a socket, use
33 * g_socket_get_credentials().
36 #include "config.h"
38 /* ---------------------------------------------------------------------------------------------------- */
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <unistd.h>
45 #include "gunixcredentialsmessage.h"
46 #include "gcredentials.h"
47 #include "gcredentialsprivate.h"
48 #include "gnetworking.h"
50 #include "glibintl.h"
52 struct _GUnixCredentialsMessagePrivate
54 GCredentials *credentials;
57 enum
59 PROP_0,
60 PROP_CREDENTIALS
63 G_DEFINE_TYPE_WITH_PRIVATE (GUnixCredentialsMessage, g_unix_credentials_message, G_TYPE_SOCKET_CONTROL_MESSAGE)
65 static gsize
66 g_unix_credentials_message_get_size (GSocketControlMessage *message)
68 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
69 return G_CREDENTIALS_NATIVE_SIZE;
70 #else
71 return 0;
72 #endif
75 static int
76 g_unix_credentials_message_get_level (GSocketControlMessage *message)
78 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
79 return SOL_SOCKET;
80 #else
81 return 0;
82 #endif
85 static int
86 g_unix_credentials_message_get_msg_type (GSocketControlMessage *message)
88 #if G_CREDENTIALS_USE_LINUX_UCRED
89 return SCM_CREDENTIALS;
90 #elif G_CREDENTIALS_USE_FREEBSD_CMSGCRED
91 return SCM_CREDS;
92 #elif G_CREDENTIALS_USE_NETBSD_UNPCBID
93 return SCM_CREDS;
94 #elif G_CREDENTIALS_USE_SOLARIS_UCRED
95 return SCM_UCRED;
96 #elif G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
97 #error "G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED is set but there is no msg_type defined for this platform"
98 #else
99 return 0;
100 #endif
103 static GSocketControlMessage *
104 g_unix_credentials_message_deserialize (gint level,
105 gint type,
106 gsize size,
107 gpointer data)
109 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
110 GSocketControlMessage *message;
111 GCredentials *credentials;
113 if (level != SOL_SOCKET || type != g_unix_credentials_message_get_msg_type (NULL))
114 return NULL;
116 if (size != G_CREDENTIALS_NATIVE_SIZE)
118 g_warning ("Expected a credentials struct of %" G_GSIZE_FORMAT " bytes but "
119 "got %" G_GSIZE_FORMAT " bytes of data",
120 G_CREDENTIALS_NATIVE_SIZE, size);
121 return NULL;
124 credentials = g_credentials_new ();
125 g_credentials_set_native (credentials, G_CREDENTIALS_NATIVE_TYPE, data);
127 if (g_credentials_get_unix_user (credentials, NULL) == (uid_t) -1)
129 /* This happens on Linux if the remote side didn't pass the credentials */
130 g_object_unref (credentials);
131 return NULL;
134 message = g_unix_credentials_message_new_with_credentials (credentials);
135 g_object_unref (credentials);
137 return message;
139 #else /* !G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED */
141 return NULL;
142 #endif
145 static void
146 g_unix_credentials_message_serialize (GSocketControlMessage *_message,
147 gpointer data)
149 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
150 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (_message);
152 memcpy (data,
153 g_credentials_get_native (message->priv->credentials,
154 G_CREDENTIALS_NATIVE_TYPE),
155 G_CREDENTIALS_NATIVE_SIZE);
156 #endif
159 static void
160 g_unix_credentials_message_finalize (GObject *object)
162 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
164 if (message->priv->credentials != NULL)
165 g_object_unref (message->priv->credentials);
167 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->finalize (object);
170 static void
171 g_unix_credentials_message_init (GUnixCredentialsMessage *message)
173 message->priv = g_unix_credentials_message_get_instance_private (message);
176 static void
177 g_unix_credentials_message_get_property (GObject *object,
178 guint prop_id,
179 GValue *value,
180 GParamSpec *pspec)
182 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
184 switch (prop_id)
186 case PROP_CREDENTIALS:
187 g_value_set_object (value, message->priv->credentials);
188 break;
190 default:
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 break;
196 static void
197 g_unix_credentials_message_set_property (GObject *object,
198 guint prop_id,
199 const GValue *value,
200 GParamSpec *pspec)
202 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
204 switch (prop_id)
206 case PROP_CREDENTIALS:
207 message->priv->credentials = g_value_dup_object (value);
208 break;
210 default:
211 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
212 break;
216 static void
217 g_unix_credentials_message_constructed (GObject *object)
219 GUnixCredentialsMessage *message = G_UNIX_CREDENTIALS_MESSAGE (object);
221 if (message->priv->credentials == NULL)
222 message->priv->credentials = g_credentials_new ();
224 if (G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed != NULL)
225 G_OBJECT_CLASS (g_unix_credentials_message_parent_class)->constructed (object);
228 static void
229 g_unix_credentials_message_class_init (GUnixCredentialsMessageClass *class)
231 GSocketControlMessageClass *scm_class;
232 GObjectClass *gobject_class;
234 gobject_class = G_OBJECT_CLASS (class);
235 gobject_class->get_property = g_unix_credentials_message_get_property;
236 gobject_class->set_property = g_unix_credentials_message_set_property;
237 gobject_class->finalize = g_unix_credentials_message_finalize;
238 gobject_class->constructed = g_unix_credentials_message_constructed;
240 scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
241 scm_class->get_size = g_unix_credentials_message_get_size;
242 scm_class->get_level = g_unix_credentials_message_get_level;
243 scm_class->get_type = g_unix_credentials_message_get_msg_type;
244 scm_class->serialize = g_unix_credentials_message_serialize;
245 scm_class->deserialize = g_unix_credentials_message_deserialize;
248 * GUnixCredentialsMessage:credentials:
250 * The credentials stored in the message.
252 * Since: 2.26
254 g_object_class_install_property (gobject_class,
255 PROP_CREDENTIALS,
256 g_param_spec_object ("credentials",
257 P_("Credentials"),
258 P_("The credentials stored in the message"),
259 G_TYPE_CREDENTIALS,
260 G_PARAM_READABLE |
261 G_PARAM_WRITABLE |
262 G_PARAM_CONSTRUCT_ONLY |
263 G_PARAM_STATIC_NAME |
264 G_PARAM_STATIC_BLURB |
265 G_PARAM_STATIC_NICK));
269 /* ---------------------------------------------------------------------------------------------------- */
272 * g_unix_credentials_message_is_supported:
274 * Checks if passing #GCredentials on a #GSocket is supported on this platform.
276 * Returns: %TRUE if supported, %FALSE otherwise
278 * Since: 2.26
280 gboolean
281 g_unix_credentials_message_is_supported (void)
283 #if G_CREDENTIALS_UNIX_CREDENTIALS_MESSAGE_SUPPORTED
284 return TRUE;
285 #else
286 return FALSE;
287 #endif
290 /* ---------------------------------------------------------------------------------------------------- */
293 * g_unix_credentials_message_new:
295 * Creates a new #GUnixCredentialsMessage with credentials matching the current processes.
297 * Returns: a new #GUnixCredentialsMessage
299 * Since: 2.26
301 GSocketControlMessage *
302 g_unix_credentials_message_new (void)
304 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
305 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
306 NULL);
310 * g_unix_credentials_message_new_with_credentials:
311 * @credentials: A #GCredentials object.
313 * Creates a new #GUnixCredentialsMessage holding @credentials.
315 * Returns: a new #GUnixCredentialsMessage
317 * Since: 2.26
319 GSocketControlMessage *
320 g_unix_credentials_message_new_with_credentials (GCredentials *credentials)
322 g_return_val_if_fail (G_IS_CREDENTIALS (credentials), NULL);
323 g_return_val_if_fail (g_unix_credentials_message_is_supported (), NULL);
324 return g_object_new (G_TYPE_UNIX_CREDENTIALS_MESSAGE,
325 "credentials", credentials,
326 NULL);
330 * g_unix_credentials_message_get_credentials:
331 * @message: A #GUnixCredentialsMessage.
333 * Gets the credentials stored in @message.
335 * Returns: (transfer none): A #GCredentials instance. Do not free, it is owned by @message.
337 * Since: 2.26
339 GCredentials *
340 g_unix_credentials_message_get_credentials (GUnixCredentialsMessage *message)
342 g_return_val_if_fail (G_IS_UNIX_CREDENTIALS_MESSAGE (message), NULL);
343 return message->priv->credentials;