2009-05-14 15:26:37 +02:00
|
|
|
/* GIO - GLib Input, Output and Streaming Library
|
|
|
|
*
|
|
|
|
* Copyright © 2009 Codethink Limited
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published
|
|
|
|
* by the Free Software Foundation; either version 2 of the licence or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
|
|
|
* See the included COPYING file for more information.
|
|
|
|
*
|
|
|
|
* Authors: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2011-02-01 19:17:23 +01:00
|
|
|
* SECTION:gunixfdmessage
|
2009-05-14 15:26:37 +02:00
|
|
|
* @title: GUnixFDMessage
|
2010-05-09 05:28:17 +02:00
|
|
|
* @short_description: A GSocketControlMessage containing a GUnixFDList
|
|
|
|
* @include: gio/gunixfdmessage.h
|
2009-11-12 04:40:28 +01:00
|
|
|
* @see_also: #GUnixConnection, #GUnixFDList, #GSocketControlMessage
|
2009-05-14 15:26:37 +02:00
|
|
|
*
|
2010-05-09 05:28:17 +02:00
|
|
|
* This #GSocketControlMessage contains a #GUnixFDList.
|
|
|
|
* It may be sent using g_socket_send_message() and received using
|
2009-05-14 15:26:37 +02:00
|
|
|
* g_socket_receive_message() over UNIX sockets (ie: sockets in the
|
2010-05-09 05:28:17 +02:00
|
|
|
* %G_SOCKET_ADDRESS_UNIX family). The file descriptors are copied
|
2009-11-12 04:40:28 +01:00
|
|
|
* between processes by the kernel.
|
2009-05-14 15:26:37 +02:00
|
|
|
*
|
|
|
|
* For an easier way to send and receive file descriptors over
|
|
|
|
* stream-oriented UNIX sockets, see g_unix_connection_send_fd() and
|
|
|
|
* g_unix_connection_receive_fd().
|
2010-05-09 05:28:17 +02:00
|
|
|
*
|
2014-02-09 08:07:26 +01:00
|
|
|
* Note that `<gio/gunixfdmessage.h>` belongs to the UNIX-specific GIO
|
2014-02-06 02:17:46 +01:00
|
|
|
* interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
|
|
|
|
* file when using it.
|
|
|
|
*/
|
2009-05-14 15:26:37 +02:00
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2009-11-12 04:40:28 +01:00
|
|
|
#include <fcntl.h>
|
2009-05-14 15:26:37 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "gunixfdmessage.h"
|
2010-05-14 02:29:04 +02:00
|
|
|
#include "gunixfdlist.h"
|
2010-11-11 15:57:25 +01:00
|
|
|
#include "gnetworking.h"
|
2009-05-14 15:26:37 +02:00
|
|
|
#include "gioerror.h"
|
|
|
|
|
|
|
|
struct _GUnixFDMessagePrivate
|
|
|
|
{
|
2009-11-12 04:40:28 +01:00
|
|
|
GUnixFDList *list;
|
2009-05-14 15:26:37 +02:00
|
|
|
};
|
|
|
|
|
2013-06-11 01:29:58 +02:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDMessage, g_unix_fd_message, G_TYPE_SOCKET_CONTROL_MESSAGE)
|
|
|
|
|
2009-05-14 15:26:37 +02:00
|
|
|
static gsize
|
|
|
|
g_unix_fd_message_get_size (GSocketControlMessage *message)
|
|
|
|
{
|
|
|
|
GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
|
|
|
|
|
2009-11-12 04:40:28 +01:00
|
|
|
return g_unix_fd_list_get_length (fd_message->priv->list) * sizeof (gint);
|
2009-05-14 15:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_unix_fd_message_get_level (GSocketControlMessage *message)
|
|
|
|
{
|
|
|
|
return SOL_SOCKET;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
g_unix_fd_message_get_msg_type (GSocketControlMessage *message)
|
|
|
|
{
|
|
|
|
return SCM_RIGHTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSocketControlMessage *
|
2009-05-28 00:20:08 +02:00
|
|
|
g_unix_fd_message_deserialize (int level,
|
|
|
|
int type,
|
|
|
|
gsize size,
|
2009-05-14 15:26:37 +02:00
|
|
|
gpointer data)
|
|
|
|
{
|
2009-11-12 04:40:28 +01:00
|
|
|
GSocketControlMessage *message;
|
|
|
|
GUnixFDList *list;
|
|
|
|
gint n, s, i;
|
|
|
|
gint *fds;
|
2009-05-14 15:26:37 +02:00
|
|
|
|
|
|
|
if (level != SOL_SOCKET ||
|
2010-01-06 00:49:23 +01:00
|
|
|
type != SCM_RIGHTS)
|
2009-05-14 15:26:37 +02:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (size % 4 > 0)
|
|
|
|
{
|
|
|
|
g_warning ("Kernel returned non-integral number of fds");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-11-12 04:40:28 +01:00
|
|
|
fds = data;
|
|
|
|
n = size / sizeof (gint);
|
|
|
|
|
2011-05-05 19:16:54 +02:00
|
|
|
/* Note we probably handled this in gsocket.c already if we're on
|
|
|
|
* Linux and have MSG_CMSG_CLOEXEC, but this code remains as a fallback
|
|
|
|
* in case the kernel is too old for MSG_CMSG_CLOEXEC.
|
|
|
|
*/
|
2009-11-12 04:40:28 +01:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
s = fcntl (fds[i], F_SETFD, FD_CLOEXEC);
|
|
|
|
while (s < 0 && errno == EINTR);
|
|
|
|
|
|
|
|
if (s < 0)
|
|
|
|
{
|
|
|
|
g_warning ("Error setting close-on-exec flag on incoming fd: %s",
|
|
|
|
g_strerror (errno));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
list = g_unix_fd_list_new_from_array (fds, n);
|
|
|
|
message = g_unix_fd_message_new_with_fd_list (list);
|
|
|
|
g_object_unref (list);
|
2009-05-14 15:26:37 +02:00
|
|
|
|
2009-11-12 04:40:28 +01:00
|
|
|
return message;
|
2009-05-14 15:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_unix_fd_message_serialize (GSocketControlMessage *message,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
|
2009-11-12 04:40:28 +01:00
|
|
|
const gint *fds;
|
|
|
|
gint n_fds;
|
|
|
|
|
|
|
|
fds = g_unix_fd_list_peek_fds (fd_message->priv->list, &n_fds);
|
|
|
|
memcpy (data, fds, sizeof (gint) * n_fds);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_unix_fd_message_set_property (GObject *object, guint prop_id,
|
|
|
|
const GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
|
|
|
|
|
|
|
|
g_assert (message->priv->list == NULL);
|
|
|
|
g_assert_cmpint (prop_id, ==, 1);
|
|
|
|
|
|
|
|
message->priv->list = g_value_dup_object (value);
|
|
|
|
|
|
|
|
if (message->priv->list == NULL)
|
|
|
|
message->priv->list = g_unix_fd_list_new ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_fd_message_get_fd_list:
|
|
|
|
* @message: a #GUnixFDMessage
|
|
|
|
*
|
|
|
|
* Gets the #GUnixFDList contained in @message. This function does not
|
|
|
|
* return a reference to the caller, but the returned list is valid for
|
|
|
|
* the lifetime of @message.
|
|
|
|
*
|
2010-09-24 23:24:41 +02:00
|
|
|
* Returns: (transfer none): the #GUnixFDList from @message
|
2009-11-12 04:40:28 +01:00
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
**/
|
|
|
|
GUnixFDList *
|
|
|
|
g_unix_fd_message_get_fd_list (GUnixFDMessage *message)
|
|
|
|
{
|
|
|
|
return message->priv->list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_unix_fd_message_get_property (GObject *object, guint prop_id,
|
|
|
|
GValue *value, GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
|
|
|
|
|
|
|
|
g_assert_cmpint (prop_id, ==, 1);
|
|
|
|
|
|
|
|
g_value_set_object (value, g_unix_fd_message_get_fd_list (message));
|
2009-05-14 15:26:37 +02:00
|
|
|
}
|
2009-11-12 04:40:28 +01:00
|
|
|
|
2009-05-14 15:26:37 +02:00
|
|
|
static void
|
|
|
|
g_unix_fd_message_init (GUnixFDMessage *message)
|
|
|
|
{
|
2013-06-24 16:43:04 +02:00
|
|
|
message->priv = g_unix_fd_message_get_instance_private (message);
|
2009-05-14 15:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_unix_fd_message_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
|
|
|
|
|
2009-11-12 04:40:28 +01:00
|
|
|
g_object_unref (message->priv->list);
|
2009-05-14 15:26:37 +02:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (g_unix_fd_message_parent_class)
|
|
|
|
->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_unix_fd_message_class_init (GUnixFDMessageClass *class)
|
|
|
|
{
|
|
|
|
GSocketControlMessageClass *scm_class = G_SOCKET_CONTROL_MESSAGE_CLASS (class);
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (class);
|
|
|
|
|
|
|
|
scm_class->get_size = g_unix_fd_message_get_size;
|
|
|
|
scm_class->get_level = g_unix_fd_message_get_level;
|
|
|
|
scm_class->get_type = g_unix_fd_message_get_msg_type;
|
|
|
|
scm_class->serialize = g_unix_fd_message_serialize;
|
|
|
|
scm_class->deserialize = g_unix_fd_message_deserialize;
|
|
|
|
object_class->finalize = g_unix_fd_message_finalize;
|
2009-11-12 04:40:28 +01:00
|
|
|
object_class->set_property = g_unix_fd_message_set_property;
|
|
|
|
object_class->get_property = g_unix_fd_message_get_property;
|
|
|
|
|
|
|
|
g_object_class_install_property (object_class, 1,
|
|
|
|
g_param_spec_object ("fd-list", "file descriptor list",
|
|
|
|
"The GUnixFDList object to send with the message",
|
|
|
|
G_TYPE_UNIX_FD_LIST, G_PARAM_STATIC_STRINGS |
|
|
|
|
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
|
2009-05-14 15:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_fd_message_new:
|
|
|
|
*
|
2009-11-12 04:40:28 +01:00
|
|
|
* Creates a new #GUnixFDMessage containing an empty file descriptor
|
|
|
|
* list.
|
2009-05-28 00:20:08 +02:00
|
|
|
*
|
|
|
|
* Returns: a new #GUnixFDMessage
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-11-12 04:40:28 +01:00
|
|
|
**/
|
2009-05-14 15:26:37 +02:00
|
|
|
GSocketControlMessage *
|
|
|
|
g_unix_fd_message_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
|
|
|
|
}
|
|
|
|
|
2009-11-12 04:40:28 +01:00
|
|
|
/**
|
|
|
|
* g_unix_fd_message_new_with_fd_list:
|
|
|
|
* @fd_list: a #GUnixFDList
|
|
|
|
*
|
|
|
|
* Creates a new #GUnixFDMessage containing @list.
|
|
|
|
*
|
|
|
|
* Returns: a new #GUnixFDMessage
|
|
|
|
*
|
|
|
|
* Since: 2.24
|
|
|
|
**/
|
|
|
|
GSocketControlMessage *
|
|
|
|
g_unix_fd_message_new_with_fd_list (GUnixFDList *fd_list)
|
|
|
|
{
|
|
|
|
return g_object_new (G_TYPE_UNIX_FD_MESSAGE,
|
|
|
|
"fd-list", fd_list,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
2009-05-14 15:26:37 +02:00
|
|
|
/**
|
|
|
|
* g_unix_fd_message_steal_fds:
|
|
|
|
* @message: a #GUnixFDMessage
|
2010-12-29 13:51:44 +01:00
|
|
|
* @length: (out) (allow-none): pointer to the length of the returned
|
|
|
|
* array, or %NULL
|
2009-05-14 15:26:37 +02:00
|
|
|
*
|
|
|
|
* Returns the array of file descriptors that is contained in this
|
|
|
|
* object.
|
|
|
|
*
|
|
|
|
* After this call, the descriptors are no longer contained in
|
2009-05-28 00:20:08 +02:00
|
|
|
* @message. Further calls will return an empty list (unless more
|
2009-05-14 15:26:37 +02:00
|
|
|
* descriptors have been added).
|
|
|
|
*
|
|
|
|
* The return result of this function must be freed with g_free().
|
|
|
|
* The caller is also responsible for closing all of the file
|
|
|
|
* descriptors.
|
|
|
|
*
|
|
|
|
* If @length is non-%NULL then it is set to the number of file
|
2009-05-28 00:20:08 +02:00
|
|
|
* descriptors in the returned array. The returned array is also
|
2009-05-14 15:26:37 +02:00
|
|
|
* terminated with -1.
|
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
* This function never returns %NULL. In case there are no file
|
2009-05-14 15:26:37 +02:00
|
|
|
* descriptors contained in @message, an empty array is returned.
|
2009-05-28 00:20:08 +02:00
|
|
|
*
|
2010-12-29 13:51:44 +01:00
|
|
|
* Returns: (array length=length) (transfer full): an array of file
|
|
|
|
* descriptors
|
2009-05-28 00:20:08 +02:00
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-11-12 04:40:28 +01:00
|
|
|
**/
|
2009-05-14 15:26:37 +02:00
|
|
|
gint *
|
|
|
|
g_unix_fd_message_steal_fds (GUnixFDMessage *message,
|
2009-05-28 00:20:08 +02:00
|
|
|
gint *length)
|
2009-05-14 15:26:37 +02:00
|
|
|
{
|
2010-07-10 23:21:32 +02:00
|
|
|
g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), NULL);
|
2009-05-14 15:26:37 +02:00
|
|
|
|
2009-11-12 04:40:28 +01:00
|
|
|
return g_unix_fd_list_steal_fds (message->priv->list, length);
|
2009-05-14 15:26:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* g_unix_fd_message_append_fd:
|
|
|
|
* @message: a #GUnixFDMessage
|
|
|
|
* @fd: a valid open file descriptor
|
|
|
|
* @error: a #GError pointer
|
|
|
|
*
|
|
|
|
* Adds a file descriptor to @message.
|
|
|
|
*
|
2009-05-28 00:20:08 +02:00
|
|
|
* The file descriptor is duplicated using dup(). You keep your copy
|
2009-05-14 15:26:37 +02:00
|
|
|
* of the descriptor and the copy contained in @message will be closed
|
|
|
|
* when @message is finalized.
|
|
|
|
*
|
|
|
|
* A possible cause of failure is exceeding the per-process or
|
|
|
|
* system-wide file descriptor limit.
|
2009-05-28 00:20:08 +02:00
|
|
|
*
|
|
|
|
* Returns: %TRUE in case of success, else %FALSE (and @error is set)
|
|
|
|
*
|
|
|
|
* Since: 2.22
|
2009-11-12 04:40:28 +01:00
|
|
|
**/
|
2009-05-14 15:26:37 +02:00
|
|
|
gboolean
|
|
|
|
g_unix_fd_message_append_fd (GUnixFDMessage *message,
|
2009-05-28 00:20:08 +02:00
|
|
|
gint fd,
|
|
|
|
GError **error)
|
2009-05-14 15:26:37 +02:00
|
|
|
{
|
2009-11-12 04:40:28 +01:00
|
|
|
g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);
|
2009-05-14 15:26:37 +02:00
|
|
|
|
2011-01-11 03:33:21 +01:00
|
|
|
return g_unix_fd_list_append (message->priv->list, fd, error) >= 0;
|
2009-05-14 15:26:37 +02:00
|
|
|
}
|