1
0
mirror of https://gitlab.gnome.org/GNOME/glib.git synced 2025-08-15 05:18:47 +02:00
Files
.gitlab-ci
.reuse
LICENSES
docs
fuzzing
gio
completion
gdbus-2.0
inotify
kqueue
tests
win32
xdgmime
data-to-c.py
dbus-daemon.xml
gaction.c
gaction.h
gactiongroup.c
gactiongroup.h
gactiongroupexporter.c
gactiongroupexporter.h
gactionmap.c
gactionmap.h
gappinfo.c
gappinfo.h
gappinfoprivate.h
gapplication-tool.c
gapplication.c
gapplication.h
gapplicationcommandline.c
gapplicationcommandline.h
gapplicationimpl-dbus.c
gapplicationimpl.h
gasynchelper.c
gasynchelper.h
gasyncinitable.c
gasyncinitable.h
gasyncresult.c
gasyncresult.h
gbufferedinputstream.c
gbufferedinputstream.h
gbufferedoutputstream.c
gbufferedoutputstream.h
gbytesicon.c
gbytesicon.h
gcancellable.c
gcancellable.h
gcharsetconverter.c
gcharsetconverter.h
gcocoanotificationbackend.m
gcontenttype-win32.c
gcontenttype.c
gcontenttype.h
gcontenttypeprivate.h
gcontextspecificgroup.c
gcontextspecificgroup.h
gconverter.c
gconverter.h
gconverterinputstream.c
gconverterinputstream.h
gconverteroutputstream.c
gconverteroutputstream.h
gcredentials.c
gcredentials.h
gcredentialsprivate.h
gdatagrambased.c
gdatagrambased.h
gdatainputstream.c
gdatainputstream.h
gdataoutputstream.c
gdataoutputstream.h
gdbus-tool.c
gdbusactiongroup-private.h
gdbusactiongroup.c
gdbusactiongroup.h
gdbusaddress.c
gdbusaddress.h
gdbusauth.c
gdbusauth.h
gdbusauthmechanism.c
gdbusauthmechanism.h
gdbusauthmechanismanon.c
gdbusauthmechanismanon.h
gdbusauthmechanismexternal.c
gdbusauthmechanismexternal.h
gdbusauthmechanismsha1.c
gdbusauthmechanismsha1.h
gdbusauthobserver.c
gdbusauthobserver.h
gdbusconnection.c
gdbusconnection.h
gdbusdaemon.c
gdbusdaemon.h
gdbuserror.c
gdbuserror.h
gdbusinterface.c
gdbusinterface.h
gdbusinterfaceskeleton.c
gdbusinterfaceskeleton.h
gdbusintrospection.c
gdbusintrospection.h
gdbusmenumodel.c
gdbusmenumodel.h
gdbusmessage.c
gdbusmessage.h
gdbusmethodinvocation.c
gdbusmethodinvocation.h
gdbusnameowning.c
gdbusnameowning.h
gdbusnamewatching.c
gdbusnamewatching.h
gdbusobject.c
gdbusobject.h
gdbusobjectmanager.c
gdbusobjectmanager.h
gdbusobjectmanagerclient.c
gdbusobjectmanagerclient.h
gdbusobjectmanagerserver.c
gdbusobjectmanagerserver.h
gdbusobjectproxy.c
gdbusobjectproxy.h
gdbusobjectskeleton.c
gdbusobjectskeleton.h
gdbusprivate.c
gdbusprivate.h
gdbusproxy.c
gdbusproxy.h
gdbusserver.c
gdbusserver.h
gdbusutils.c
gdbusutils.h
gdebugcontroller.c
gdebugcontroller.h
gdebugcontrollerdbus.c
gdebugcontrollerdbus.h
gdelayedsettingsbackend.c
gdelayedsettingsbackend.h
gdesktopappinfo.c
gdesktopappinfo.h
gdocumentportal.c
gdocumentportal.h
gdrive.c
gdrive.h
gdtlsclientconnection.c
gdtlsclientconnection.h
gdtlsconnection.c
gdtlsconnection.h
gdtlsserverconnection.c
gdtlsserverconnection.h
gdummyfile.c
gdummyfile.h
gdummyproxyresolver.c
gdummyproxyresolver.h
gdummytlsbackend.c
gdummytlsbackend.h
gemblem.c
gemblem.h
gemblemedicon.c
gemblemedicon.h
gfdonotificationbackend.c
gfile.c
gfile.h
gfileattribute-priv.h
gfileattribute.c
gfileattribute.h
gfiledescriptorbased.c
gfiledescriptorbased.h
gfileenumerator.c
gfileenumerator.h
gfileicon.c
gfileicon.h
gfileinfo-priv.h
gfileinfo.c
gfileinfo.h
gfileinputstream.c
gfileinputstream.h
gfileiostream.c
gfileiostream.h
gfilemonitor.c
gfilemonitor.h
gfilenamecompleter.c
gfilenamecompleter.h
gfileoutputstream.c
gfileoutputstream.h
gfilterinputstream.c
gfilterinputstream.h
gfilteroutputstream.c
gfilteroutputstream.h
ggtknotificationbackend.c
ghttpproxy.c
ghttpproxy.h
gicon.c
gicon.h
ginetaddress.c
ginetaddress.h
ginetaddressmask.c
ginetaddressmask.h
ginetsocketaddress.c
ginetsocketaddress.h
ginitable.c
ginitable.h
ginputstream.c
ginputstream.h
gio-autocleanups.h
gio-launch-desktop.c
gio-querymodules.c
gio-tool-cat.c
gio-tool-copy.c
gio-tool-info.c
gio-tool-launch.c
gio-tool-list.c
gio-tool-mime.c
gio-tool-mkdir.c
gio-tool-monitor.c
gio-tool-mount.c
gio-tool-move.c
gio-tool-open.c
gio-tool-remove.c
gio-tool-rename.c
gio-tool-save.c
gio-tool-set.c
gio-tool-trash.c
gio-tool-tree.c
gio-tool.c
gio-tool.h
gio.h
gio.rc.in
gio.stp.in
gio_probes.d
gio_trace.h
gioenums.h
gioenumtypes.c.template
gioenumtypes.h.template
gioerror.c
gioerror.h
giomodule-priv.c
giomodule-priv.h
giomodule.c
giomodule.h
gioprivate.h
gioscheduler.c
gioscheduler.h
giostream.c
giostream.h
giotypes.h
giounix-private.c
giounix-private.h
giowin32-afunix.h
giowin32-priv.h
giowin32-private.c
gkeyfilesettingsbackend.c
glib-compile-resources.c
glib-compile-schemas.c
glistmodel.c
glistmodel.h
gliststore.c
gliststore.h
gloadableicon.c
gloadableicon.h
glocalfile.c
glocalfile.h
glocalfileenumerator.c
glocalfileenumerator.h
glocalfileinfo.c
glocalfileinfo.h
glocalfileinputstream.c
glocalfileinputstream.h
glocalfileiostream.c
glocalfileiostream.h
glocalfilemonitor.c
glocalfilemonitor.h
glocalfileoutputstream.c
glocalfileoutputstream.h
glocalvfs.c
glocalvfs.h
gmarshal-internal.c
gmarshal-internal.h
gmarshal-internal.list
gmemoryinputstream.c
gmemoryinputstream.h
gmemorymonitor.c
gmemorymonitor.h
gmemorymonitordbus.c
gmemorymonitordbus.h
gmemorymonitorportal.c
gmemorymonitorportal.h
gmemorymonitorwin32.c
gmemoryoutputstream.c
gmemoryoutputstream.h
gmemorysettingsbackend.c
gmenu.c
gmenu.h
gmenuexporter.c
gmenuexporter.h
gmenumodel.c
gmenumodel.h
gmount.c
gmount.h
gmountoperation.c
gmountoperation.h
gmountprivate.h
gnativesocketaddress.c
gnativesocketaddress.h
gnativevolumemonitor.c
gnativevolumemonitor.h
gnetworkaddress.c
gnetworkaddress.h
gnetworking.c
gnetworking.h.in
gnetworkingprivate.h
gnetworkmonitor.c
gnetworkmonitor.h
gnetworkmonitorbase.c
gnetworkmonitorbase.h
gnetworkmonitornetlink.c
gnetworkmonitornetlink.h
gnetworkmonitornm.c
gnetworkmonitornm.h
gnetworkmonitorportal.c
gnetworkmonitorportal.h
gnetworkservice.c
gnetworkservice.h
gnextstepsettingsbackend.m
gnotification-private.h
gnotification.c
gnotification.h
gnotificationbackend.c
gnotificationbackend.h
gnullsettingsbackend.c
gopenuriportal.c
gopenuriportal.h
gosxappinfo.h
gosxappinfo.m
gosxcontenttype.m
goutputstream.c
goutputstream.h
gpermission.c
gpermission.h
gpollableinputstream.c
gpollableinputstream.h
gpollableoutputstream.c
gpollableoutputstream.h
gpollableutils.c
gpollableutils.h
gpollfilemonitor.c
gpollfilemonitor.h
gportalnotificationbackend.c
gportalsupport.c
gportalsupport.h
gpowerprofilemonitor.c
gpowerprofilemonitor.h
gpowerprofilemonitordbus.c
gpowerprofilemonitordbus.h
gpowerprofilemonitorportal.c
gpowerprofilemonitorportal.h
gpropertyaction.c
gpropertyaction.h
gproxy.c
gproxy.h
gproxyaddress.c
gproxyaddress.h
gproxyaddressenumerator.c
gproxyaddressenumerator.h
gproxyresolver.c
gproxyresolver.h
gproxyresolverportal.c
gproxyresolverportal.h
gregistrysettingsbackend.c
gregistrysettingsbackend.h
gremoteactiongroup.c
gremoteactiongroup.h
gresolver.c
gresolver.h
gresource-tool.c
gresource.c
gresource.h
gresourcefile.c
gresourcefile.h
gschema.dtd
gschema.its
gschema.loc
gseekable.c
gseekable.h
gsettings-mapping.c
gsettings-mapping.h
gsettings-tool.c
gsettings.c
gsettings.h
gsettingsbackend.c
gsettingsbackend.h
gsettingsbackendinternal.h
gsettingsschema-internal.h
gsettingsschema.c
gsettingsschema.h
gsimpleaction.c
gsimpleaction.h
gsimpleactiongroup.c
gsimpleactiongroup.h
gsimpleasyncresult.c
gsimpleasyncresult.h
gsimpleiostream.c
gsimpleiostream.h
gsimplepermission.c
gsimplepermission.h
gsimpleproxyresolver.c
gsimpleproxyresolver.h
gsocket.c
gsocket.h
gsocketaddress.c
gsocketaddress.h
gsocketaddressenumerator.c
gsocketaddressenumerator.h
gsocketclient.c
gsocketclient.h
gsocketconnectable.c
gsocketconnectable.h
gsocketconnection.c
gsocketconnection.h
gsocketcontrolmessage.c
gsocketcontrolmessage.h
gsocketinputstream.c
gsocketinputstream.h
gsocketlistener.c
gsocketlistener.h
gsocketoutputstream.c
gsocketoutputstream.h
gsocketservice.c
gsocketservice.h
gsocks4aproxy.c
gsocks4aproxy.h
gsocks4proxy.c
gsocks4proxy.h
gsocks5proxy.c
gsocks5proxy.h
gsrvtarget.c
gsrvtarget.h
gsubprocess.c
gsubprocess.h
gsubprocesslauncher-private.h
gsubprocesslauncher.c
gsubprocesslauncher.h
gtask.c
gtask.h
gtcpconnection.c
gtcpconnection.h
gtcpwrapperconnection.c
gtcpwrapperconnection.h
gtestdbus.c
gtestdbus.h
gthemedicon.c
gthemedicon.h
gthreadedresolver.c
gthreadedresolver.h
gthreadedsocketservice.c
gthreadedsocketservice.h
gtlsbackend.c
gtlsbackend.h
gtlscertificate.c
gtlscertificate.h
gtlsclientconnection.c
gtlsclientconnection.h
gtlsconnection.c
gtlsconnection.h
gtlsdatabase.c
gtlsdatabase.h
gtlsfiledatabase.c
gtlsfiledatabase.h
gtlsinteraction.c
gtlsinteraction.h
gtlspassword.c
gtlspassword.h
gtlsserverconnection.c
gtlsserverconnection.h
gtrashportal.c
gtrashportal.h
gunionvolumemonitor.c
gunionvolumemonitor.h
gunixconnection.c
gunixconnection.h
gunixcredentialsmessage.c
gunixcredentialsmessage.h
gunixfdlist.c
gunixfdlist.h
gunixfdmessage.c
gunixfdmessage.h
gunixinputstream.c
gunixinputstream.h
gunixmount.c
gunixmount.h
gunixmounts.c
gunixmounts.h
gunixoutputstream.c
gunixoutputstream.h
gunixsocketaddress.c
gunixsocketaddress.h
gunixvolume.c
gunixvolume.h
gunixvolumemonitor.c
gunixvolumemonitor.h
gvfs.c
gvfs.h
gvolume.c
gvolume.h
gvolumemonitor.c
gvolumemonitor.h
gwin32api-application-activation-manager.h
gwin32api-iterator.h
gwin32api-misc.h
gwin32api-package.h
gwin32api-storage.h
gwin32appinfo.c
gwin32appinfo.h
gwin32file-sync-stream.c
gwin32file-sync-stream.h
gwin32inputstream.c
gwin32inputstream.h
gwin32mount.c
gwin32mount.h
gwin32networkmonitor.c
gwin32networkmonitor.h
gwin32notificationbackend.c
gwin32outputstream.c
gwin32outputstream.h
gwin32packageparser.c
gwin32packageparser.h
gwin32registrykey.c
gwin32registrykey.h
gwin32sid.c
gwin32sid.h
gwin32volumemonitor.c
gwin32volumemonitor.h
gzlibcompressor.c
gzlibcompressor.h
gzlibdecompressor.c
gzlibdecompressor.h
meson.build
org.freedesktop.portal.Documents.xml
org.freedesktop.portal.OpenURI.xml
org.freedesktop.portal.ProxyResolver.xml
org.freedesktop.portal.Trash.xml
strinfo.c
thumbnail-verify.c
thumbnail-verify.h
glib
gmodule
gobject
gthread
m4macros
po
subprojects
tests
tools
.clang-format
.dir-locals.el
.editorconfig
.gitignore
.gitlab-ci.yml
.gitmodules
.lcovrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
HACKING
INSTALL.md
NEWS
README.md
README.win32.md
SECURITY.md
glib.doap
meson.build
meson_options.txt
msvc_recommended_pragmas.h
glib/gio/gunixfdmessage.c

334 lines
8.9 KiB
C
Raw Normal View History

/* GIO - GLib Input, Output and Streaming Library
*
* Copyright © 2009 Codethink Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library 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.1 of the License, or (at your option) any later version.
*
* See the included COPYING file for more information.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
/**
* SECTION:gunixfdmessage
* @title: GUnixFDMessage
* @short_description: A GSocketControlMessage containing a GUnixFDList
* @include: gio/gunixfdmessage.h
* @see_also: #GUnixConnection, #GUnixFDList, #GSocketControlMessage
*
* This #GSocketControlMessage contains a #GUnixFDList.
* It may be sent using g_socket_send_message() and received using
* g_socket_receive_message() over UNIX sockets (ie: sockets in the
* %G_SOCKET_FAMILY_UNIX family). The file descriptors are copied
* between processes by the kernel.
*
* 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().
*
* Note that `<gio/gunixfdmessage.h>` belongs to the UNIX-specific GIO
* interfaces, thus you have to use the `gio-unix-2.0.pc` pkg-config
* file when using it.
*/
2015-02-05 16:20:43 +01:00
/**
* GUnixFDMessage:
*
* #GUnixFDMessage is an opaque data structure and can only be accessed
* using the following functions.
**/
#include "config.h"
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include "gunixfdmessage.h"
#include "gunixfdlist.h"
#include "gnetworking.h"
#include "gioerror.h"
struct _GUnixFDMessagePrivate
{
GUnixFDList *list;
};
G_DEFINE_TYPE_WITH_PRIVATE (GUnixFDMessage, g_unix_fd_message, G_TYPE_SOCKET_CONTROL_MESSAGE)
static gsize
g_unix_fd_message_get_size (GSocketControlMessage *message)
{
GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
return g_unix_fd_list_get_length (fd_message->priv->list) * sizeof (gint);
}
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 *
g_unix_fd_message_deserialize (int level,
int type,
gsize size,
gpointer data)
{
GSocketControlMessage *message;
GUnixFDList *list;
gint n, s, i;
gint *fds;
if (level != SOL_SOCKET ||
type != SCM_RIGHTS)
return NULL;
if (size % 4 > 0)
{
g_warning ("Kernel returned non-integral number of fds");
return NULL;
}
fds = data;
n = size / sizeof (gint);
/* 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.
*/
for (i = 0; i < n; i++)
{
int errsv;
do
{
s = fcntl (fds[i], F_SETFD, FD_CLOEXEC);
errsv = errno;
}
while (s < 0 && errsv == EINTR);
if (s < 0)
{
g_warning ("Error setting close-on-exec flag on incoming fd: %s",
g_strerror (errsv));
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);
return message;
}
static void
g_unix_fd_message_serialize (GSocketControlMessage *message,
gpointer data)
{
GUnixFDMessage *fd_message = G_UNIX_FD_MESSAGE (message);
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 18:24:41 -03:00
* Returns: (transfer none): the #GUnixFDList from @message
*
* 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));
}
static void
g_unix_fd_message_init (GUnixFDMessage *message)
{
message->priv = g_unix_fd_message_get_instance_private (message);
}
static void
g_unix_fd_message_finalize (GObject *object)
{
GUnixFDMessage *message = G_UNIX_FD_MESSAGE (object);
g_object_unref (message->priv->list);
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;
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));
}
/**
* g_unix_fd_message_new:
*
* Creates a new #GUnixFDMessage containing an empty file descriptor
* list.
*
* Returns: a new #GUnixFDMessage
*
* Since: 2.22
**/
GSocketControlMessage *
g_unix_fd_message_new (void)
{
return g_object_new (G_TYPE_UNIX_FD_MESSAGE, NULL);
}
/**
* 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);
}
/**
* g_unix_fd_message_steal_fds:
* @message: a #GUnixFDMessage
* @length: (out) (optional): pointer to the length of the returned
* array, or %NULL
*
* Returns the array of file descriptors that is contained in this
* object.
*
* After this call, the descriptors are no longer contained in
* @message. Further calls will return an empty list (unless more
* 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
* descriptors in the returned array. The returned array is also
* terminated with -1.
*
* This function never returns %NULL. In case there are no file
* descriptors contained in @message, an empty array is returned.
*
* Returns: (array length=length) (transfer full): an array of file
* descriptors
*
* Since: 2.22
**/
gint *
g_unix_fd_message_steal_fds (GUnixFDMessage *message,
gint *length)
{
g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), NULL);
return g_unix_fd_list_steal_fds (message->priv->list, length);
}
/**
* 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.
*
* The file descriptor is duplicated using dup(). You keep your copy
* 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.
*
* Returns: %TRUE in case of success, else %FALSE (and @error is set)
*
* Since: 2.22
**/
gboolean
g_unix_fd_message_append_fd (GUnixFDMessage *message,
gint fd,
GError **error)
{
g_return_val_if_fail (G_UNIX_FD_MESSAGE (message), FALSE);
return g_unix_fd_list_append (message->priv->list, fd, error) >= 0;
}