GDBusWorker: move flush async op into continue_writing()
[platform/upstream/glib.git] / gio / gunixconnection.c
index 1d48ff5..c21353e 100644 (file)
 
 #include "config.h"
 #include "gunixconnection.h"
+#include "gunixcredentialsmessage.h"
 #include "glibintl.h"
 
 /**
- * SECTION: gunixconnection
+ * SECTION:gunixconnection
  * @title: GUnixConnection
- * @short_description: a Unix domain #GSocketConnection
+ * @short_description: A UNIX domain GSocketConnection
+ * @include: gio/gunixconnection.h
  * @see_also: #GSocketConnection.
  *
  * This is the subclass of #GSocketConnection that is created
  * for UNIX domain sockets.
  *
- * It contains functions to do some of the unix socket specific
- * functionallity like passing file descriptors.
+ * It contains functions to do some of the UNIX socket specific
+ * functionality like passing file descriptors.
+ *
+ * Note that <filename>&lt;gio/gunixconnection.h&gt;</filename> belongs to
+ * the UNIX-specific GIO interfaces, thus you have to use the
+ * <filename>gio-unix-2.0.pc</filename> pkg-config file when using it.
  *
  * Since: 2.22
  */
@@ -44,7 +50,6 @@
 #include <string.h>
 #endif
 
-#include "gioalias.h"
 
 G_DEFINE_TYPE_WITH_CODE (GUnixConnection, g_unix_connection,
                         G_TYPE_SOCKET_CONNECTION,
@@ -58,11 +63,11 @@ G_DEFINE_TYPE_WITH_CODE (GUnixConnection, g_unix_connection,
  * g_unix_connection_send_fd:
  * @connection: a #GUnixConnection
  * @fd: a file descriptor
- * @cancellable: optional #GCancellable object, %NULL to ignore.
- * @error: #GError for error reporting, or %NULL to ignore.
+ * @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
@@ -112,8 +117,8 @@ g_unix_connection_send_fd (GUnixConnection  *connection,
 /**
  * g_unix_connection_receive_fd:
  * @connection: a #GUnixConnection
- * @cancellable: optional #GCancellable object, %NULL to ignore
- * @error: #GError for error reporting, or %NULL to ignore
+ * @cancellable: (allow-none): optional #GCancellable object, %NULL to ignore
+ * @error: (allow-none): #GError for error reporting, or %NULL to ignore
  *
  * Receives a file descriptor from the sending end of the connection.
  * The sending end has to call g_unix_connection_send_fd() for this
@@ -126,7 +131,7 @@ g_unix_connection_send_fd (GUnixConnection  *connection,
  * Returns: a file descriptor on success, -1 on error.
  *
  * Since: 2.22
- */
+ **/
 gint
 g_unix_connection_receive_fd (GUnixConnection  *connection,
                               GCancellable     *cancellable,
@@ -299,11 +304,11 @@ gboolean                g_unix_connection_create_pair                   (GUnixCo
 /**
  * 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.
  *
@@ -311,7 +316,8 @@ gboolean                g_unix_connection_create_pair                   (GUnixCo
  * byte to the stream, as this is required for credentials passing to
  * work on some implementations.
  *
- * Note that this function only works on Linux, currently.
+ * Other ways to exchange credentials with a foreign peer includes the
+ * #GUnixCredentialsMessage type and g_socket_get_credentials() function.
  *
  * Returns: %TRUE on success, %FALSE if @error is set.
  *
@@ -328,6 +334,7 @@ g_unix_connection_send_credentials (GUnixConnection      *connection,
   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);
@@ -338,14 +345,25 @@ g_unix_connection_send_credentials (GUnixConnection      *connection,
 
   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)
@@ -358,7 +376,8 @@ g_unix_connection_send_credentials (GUnixConnection      *connection,
 
  out:
   g_object_unref (socket);
-  g_object_unref (scm);
+  if (scm != NULL)
+    g_object_unref (scm);
   g_object_unref (credentials);
   return ret;
 }
@@ -366,7 +385,7 @@ g_unix_connection_send_credentials (GUnixConnection      *connection,
 /**
  * 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
@@ -377,7 +396,10 @@ g_unix_connection_send_credentials (GUnixConnection      *connection,
  * single byte from the stream, as this is required for credentials
  * passing to work on some implementations.
  *
- * Returns: Received credentials on success (free with
+ * Other ways to exchange credentials with a foreign peer includes the
+ * #GUnixCredentialsMessage type and g_socket_get_credentials() function.
+ *
+ * Returns: (transfer full): Received credentials on success (free with
  * g_object_unref()), %NULL if @error is set.
  *
  * Since: 2.26
@@ -464,6 +486,7 @@ g_unix_connection_receive_credentials (GUnixConnection      *connection,
 
   /* 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,
@@ -488,28 +511,47 @@ g_unix_connection_receive_credentials (GUnixConnection      *connection,
       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__
@@ -542,6 +584,3 @@ g_unix_connection_receive_credentials (GUnixConnection      *connection,
   g_object_unref (socket);
   return ret;
 }
-
-#define __G_UNIX_CONNECTION_C__
-#include "gioaliasdef.c"