#include "glibintl.h"
/**
- * SECTION: gunixconnection
+ * SECTION:gunixconnection
* @title: GUnixConnection
* @short_description: A UNIX domain GSocketConnection
* @include: gio/gunixconnection.h
* @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore.
* @error: (allow-none): #GError for error reporting, or %NULL to ignore.
*
- * Passes a file descriptor to the recieving side of the
- * connection. The recieving end has to call g_unix_connection_receive_fd()
+ * Passes a file descriptor to the receiving side of the
+ * connection. The receiving end has to call g_unix_connection_receive_fd()
* to accept the file descriptor.
*
* As well as sending the fd this also writes a single byte to the
/**
* g_unix_connection_send_credentials:
* @connection: A #GUnixConnection.
- * @cancellable: A #GCancellable or %NULL.
+ * @cancellable: (allow-none): A #GCancellable or %NULL.
* @error: Return location for error or %NULL.
*
* Passes the credentials of the current user the receiving side
- * of the connection. The recieving end has to call
+ * of the connection. The receiving end has to call
* g_unix_connection_receive_credentials() (or similar) to accept the
* credentials.
*
* Other ways to exchange credentials with a foreign peer includes the
* #GUnixCredentialsMessage type and g_socket_get_credentials() function.
*
- * Note that this function only works on Linux, currently.
- *
* Returns: %TRUE on success, %FALSE if @error is set.
*
* Since: 2.26
gboolean ret;
GOutputVector vector;
guchar nul_byte[1] = {'\0'};
+ gint num_messages;
g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
vector.buffer = &nul_byte;
vector.size = 1;
- scm = g_unix_credentials_message_new_with_credentials (credentials);
+
+ if (g_unix_credentials_message_is_supported ())
+ {
+ scm = g_unix_credentials_message_new_with_credentials (credentials);
+ num_messages = 1;
+ }
+ else
+ {
+ scm = NULL;
+ num_messages = 0;
+ }
+
g_object_get (connection, "socket", &socket, NULL);
if (g_socket_send_message (socket,
NULL, /* address */
&vector,
1,
&scm,
- 1,
+ num_messages,
G_SOCKET_MSG_NONE,
cancellable,
error) != 1)
out:
g_object_unref (socket);
- g_object_unref (scm);
+ if (scm != NULL)
+ g_object_unref (scm);
g_object_unref (credentials);
return ret;
}
/**
* g_unix_connection_receive_credentials:
* @connection: A #GUnixConnection.
- * @cancellable: A #GCancellable or %NULL.
+ * @cancellable: (allow-none): A #GCancellable or %NULL.
* @error: Return location for error or %NULL.
*
* Receives credentials from the sending end of the connection. The
* Other ways to exchange credentials with a foreign peer includes the
* #GUnixCredentialsMessage type and g_socket_get_credentials() function.
*
- * Returns: Received credentials on success (free with
+ * Returns: (transfer full): Received credentials on success (free with
* g_object_unref()), %NULL if @error is set.
*
* Since: 2.26
/* ensure the type of GUnixCredentialsMessage has been registered with the type system */
credentials_message_gtype = G_TYPE_UNIX_CREDENTIALS_MESSAGE;
+ (credentials_message_gtype); /* To avoid -Wunused-but-set-variable */
num_bytes_read = g_socket_receive_message (socket,
NULL, /* GSocketAddress **address */
NULL,
goto out;
}
- if (nscm != 1)
+ if (g_unix_credentials_message_is_supported ())
{
- g_set_error (error,
- G_IO_ERROR,
- G_IO_ERROR_FAILED,
- _("Expecting 1 control message, got %d"),
- nscm);
- goto out;
- }
+ if (nscm != 1)
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Expecting 1 control message, got %d"),
+ nscm);
+ goto out;
+ }
- if (!G_IS_UNIX_CREDENTIALS_MESSAGE (scms[0]))
+ if (!G_IS_UNIX_CREDENTIALS_MESSAGE (scms[0]))
+ {
+ g_set_error_literal (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Unexpected type of ancillary data"));
+ goto out;
+ }
+
+ ret = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (scms[0]));
+ g_object_ref (ret);
+ }
+ else
{
- g_set_error_literal (error,
- G_IO_ERROR,
- G_IO_ERROR_FAILED,
- _("Unexpected type of ancillary data"));
- goto out;
+ if (nscm != 0)
+ {
+ g_set_error (error,
+ G_IO_ERROR,
+ G_IO_ERROR_FAILED,
+ _("Not expecting control message, but got %d"),
+ nscm);
+ goto out;
+ }
+ else
+ {
+ ret = g_socket_get_credentials (socket, error);
+ }
}
- ret = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (scms[0]));
- g_object_ref (ret);
-
out:
#ifdef __linux__