Merge branch 'win32-fd' into 'main'

gio: various unix/fd-related enablement on win32

See merge request GNOME/glib!2656
This commit is contained in:
Philip Withnall 2022-05-18 14:01:43 +00:00
commit caf6d99fbe
10 changed files with 224 additions and 109 deletions

View File

@ -39,11 +39,9 @@
#include "gdatainputstream.h"
#include "gdataoutputstream.h"
#ifdef G_OS_UNIX
#include "gnetworking.h"
#include "gunixconnection.h"
#include "gunixcredentialsmessage.h"
#endif
#include "glibintl.h"
@ -974,7 +972,6 @@ _g_dbus_auth_run_server (GDBusAuth *auth,
g_data_input_stream_set_newline_type (dis, G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
/* read the NUL-byte, possibly with credentials attached */
#ifdef G_OS_UNIX
#ifndef G_CREDENTIALS_PREFER_MESSAGE_PASSING
if (G_IS_SOCKET_CONNECTION (auth->priv->stream))
{
@ -1020,15 +1017,7 @@ _g_dbus_auth_run_server (GDBusAuth *auth,
goto out;
}
}
#else
local_error = NULL;
(void)g_data_input_stream_read_byte (dis, cancellable, &local_error);
if (local_error != NULL)
{
g_propagate_error (error, local_error);
goto out;
}
#endif
if (credentials != NULL)
{
if (G_UNLIKELY (_g_dbus_debug_authentication ()))

View File

@ -53,9 +53,7 @@
#include <io.h>
#endif
#ifdef G_OS_UNIX
#include "gunixsocketaddress.h"
#endif
#include "glibintl.h"
@ -675,8 +673,6 @@ g_dbus_server_stop (GDBusServer *server)
/* ---------------------------------------------------------------------------------------------------- */
#ifdef G_OS_UNIX
static gint
random_ascii (void)
{
@ -825,7 +821,6 @@ try_unix (GDBusServer *server,
}
return ret;
}
#endif
/* ---------------------------------------------------------------------------------------------------- */
@ -1144,10 +1139,8 @@ initable_init (GInitable *initable,
if (FALSE)
{
}
#ifdef G_OS_UNIX
else if (g_strcmp0 (transport_name, "unix") == 0)
ret = try_unix (server, address_entry, key_value_pairs, &this_error);
#endif
else if (g_strcmp0 (transport_name, "tcp") == 0)
ret = try_tcp (server, address_entry, key_value_pairs, FALSE, &this_error);
else if (g_strcmp0 (transport_name, "nonce-tcp") == 0)

View File

@ -171,6 +171,7 @@
#include <gio/gtlsserverconnection.h>
#include <gio/gunixconnection.h>
#include <gio/gunixcredentialsmessage.h>
#include <gio/gunixfdlist.h>
#include <gio/gunixsocketaddress.h>
#include <gio/gvfs.h>
#include <gio/gvolume.h>

View File

@ -28,9 +28,11 @@
* the %G_SOCKET_FAMILY_UNIX family by using g_socket_send_message()
* and received using g_socket_receive_message().
*
* Note that `<gio/gunixfdlist.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.
* Before 2.74, `<gio/gunixfdlist.h>` belonged to the UNIX-specific GIO
* interfaces, thus you had to use the `gio-unix-2.0.pc` pkg-config file when
* using it.
*
* Since 2.74, the API is available for Windows.
*/
/**
@ -42,7 +44,6 @@
#include "config.h"
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
@ -50,6 +51,12 @@
#include "gunixfdlist.h"
#include "gnetworking.h"
#include "gioerror.h"
#include "glib/glib-private.h"
#include "glib/gstdio.h"
#ifdef G_OS_WIN32
#include <io.h>
#endif
struct _GUnixFDListPrivate
{
@ -72,7 +79,7 @@ g_unix_fd_list_finalize (GObject *object)
gint i;
for (i = 0; i < list->priv->nfd; i++)
close (list->priv->fds[i]);
g_close (list->priv->fds[i], NULL);
g_free (list->priv->fds);
G_OBJECT_CLASS (g_unix_fd_list_parent_class)
@ -92,7 +99,9 @@ dup_close_on_exec_fd (gint fd,
GError **error)
{
gint new_fd;
#ifndef G_OS_WIN32
gint s;
#endif
#ifdef F_DUPFD_CLOEXEC
do
@ -120,6 +129,9 @@ dup_close_on_exec_fd (gint fd,
return -1;
}
#ifdef G_OS_WIN32
new_fd = GLIB_PRIVATE_CALL (g_win32_reopen_noninherited) (new_fd, 0, error);
#else
do
{
s = fcntl (new_fd, F_GETFD);
@ -136,10 +148,11 @@ dup_close_on_exec_fd (gint fd,
g_set_error (error, G_IO_ERROR,
g_io_error_from_errno (saved_errno),
"fcntl: %s", g_strerror (saved_errno));
close (new_fd);
g_close (new_fd, NULL);
return -1;
}
#endif
return new_fd;
}

View File

@ -354,7 +354,6 @@ if host_system != 'windows'
unix_sources = files(
'gfiledescriptorbased.c',
'giounix-private.c',
'gunixfdlist.c',
'gunixfdmessage.c',
'gunixmount.c',
'gunixmounts.c',
@ -574,6 +573,7 @@ gio_sources = files(
'gdtlsserverconnection.c',
'gunionvolumemonitor.c',
'gunixconnection.c',
'gunixfdlist.c',
'gunixcredentialsmessage.c',
'gunixsocketaddress.c',
'gvfs.c',

View File

@ -23,7 +23,6 @@
#include "config.h"
#include <gio/gio.h>
#include <unistd.h>
#include <string.h>
/* for open(2) */
@ -45,6 +44,10 @@
#include <errno.h>
#endif
#ifdef G_OS_WIN32
#include <gio/giowin32-afunix.h>
#endif
#include "gdbus-tests.h"
#include "gdbus-object-manager-example/objectmanager-gen.h"
@ -298,10 +301,27 @@ on_proxy_signal_received_with_name_set (GDBusProxy *proxy,
/* ---------------------------------------------------------------------------------------------------- */
static gboolean
af_unix_works (void)
{
int fd;
g_networking_init ();
fd = socket (AF_UNIX, SOCK_STREAM, 0);
#ifdef G_OS_WIN32
closesocket (fd);
return fd != (int) INVALID_SOCKET;
#else
g_close (fd, NULL);
return fd >= 0;
#endif
}
static void
setup_test_address (void)
{
if (is_unix)
if (is_unix || af_unix_works ())
{
g_test_message ("Testing with unix:dir address");
tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
@ -390,6 +410,13 @@ on_new_connection (GDBusServer *server,
credentials = g_dbus_connection_get_peer_credentials (connection);
g_assert (credentials != NULL);
#ifdef G_OS_WIN32
{
DWORD *pid;
pid = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_WIN32_PID);
g_assert_cmpuint (*pid, ==, GetCurrentProcessId ());
}
#else
g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
getuid ());
#if G_CREDENTIALS_HAS_PID
@ -400,9 +427,10 @@ on_new_connection (GDBusServer *server,
g_assert_cmpint (g_credentials_get_unix_pid (credentials, &error), ==, -1);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
g_clear_error (&error);
#endif
#endif /* G_CREDENTIALS_HAS_PID */
#endif /* G_OS_WIN32 */
}
#endif
#endif /* G_CREDENTIALS_SUPPORTED */
/* export object on the newly established connection */
reg_id = g_dbus_connection_register_object (connection,
@ -1000,6 +1028,13 @@ do_test_peer (void)
g_assert_no_error (error);
g_assert (G_IS_CREDENTIALS (credentials));
#ifdef G_OS_WIN32
{
DWORD *pid;
pid = g_credentials_get_native (credentials, G_CREDENTIALS_TYPE_WIN32_PID);
g_assert_cmpuint (*pid, ==, GetCurrentProcessId ());
}
#else
g_assert_cmpuint (g_credentials_get_unix_user (credentials, NULL), ==,
getuid ());
#if G_CREDENTIALS_HAS_PID
@ -1010,12 +1045,13 @@ do_test_peer (void)
g_assert_cmpint (g_credentials_get_unix_pid (credentials, &error), ==, -1);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
g_clear_error (&error);
#endif
#endif /* G_CREDENTIALS_HAS_PID */
g_object_unref (credentials);
#endif /* G_OS_WIN32 */
#else
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
g_assert (credentials == NULL);
#endif
#endif /* G_CREDENTIALS_SOCKET_GET_CREDENTIALS_SUPPORTED */
}
@ -1474,7 +1510,7 @@ dmp_on_new_connection (GDBusServer *server,
* G_DBUS_CONNECTION_FLAGS_DELAY_MESSAGE_PROCESSING really works
* (GDBusServer uses this feature).
*/
usleep (100 * 1000);
g_usleep (100 * 1000);
/* export an object */
error = NULL;
@ -1726,7 +1762,7 @@ test_nonce_tcp (void)
s = strstr (address, "noncefile=");
g_assert (s != NULL);
s += sizeof "noncefile=" - 1;
nonce_file = g_strdup (s);
nonce_file = g_uri_unescape_string (s, NULL); /* URI-unescaping should be good enough */
/* First try invalid data in the nonce file - this will actually
* make the client send this and the server will reject it. The way
@ -1809,17 +1845,26 @@ test_credentials (void)
GCredentials *c1, *c2;
GError *error;
gchar *desc;
gboolean same;
c1 = g_credentials_new ();
c2 = g_credentials_new ();
error = NULL;
#ifdef G_OS_UNIX
if (g_credentials_set_unix_user (c2, getuid (), &error))
g_assert_no_error (error);
#endif
g_clear_error (&error);
g_assert (g_credentials_is_same_user (c1, c2, &error));
same = g_credentials_is_same_user (c1, c2, &error);
#ifdef G_OS_UNIX
g_assert (same);
g_assert_no_error (error);
#else
g_assert (!same);
g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
g_clear_error (&error);
#endif
desc = g_credentials_to_string (c1);
g_assert (desc != NULL);

View File

@ -81,6 +81,12 @@ gio_tests = {
'g-icon' : {},
'gdbus-addresses' : {},
'gdbus-message' : {},
'gdbus-peer' : {
'dependencies' : [libgdbus_example_objectmanager_dep],
'install_rpath' : installed_tests_execdir,
# FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392
'should_fail' : host_system == 'darwin',
},
'inet-address' : {},
'io-stream' : {},
'memory-input-stream' : {},
@ -115,6 +121,7 @@ gio_tests = {
'tls-interaction' : {'extra_sources' : ['gtesttlsbackend.c']},
'tls-database' : {'extra_sources' : ['gtesttlsbackend.c']},
'tls-bindings' : {'extra_sources' : ['gtesttlsbackend.c']},
'unix-fd' : {},
'gdbus-address-get-session' : {
# FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392
'should_fail' : host_system == 'darwin',
@ -183,18 +190,11 @@ endif
if host_machine.system() != 'windows'
gio_tests += {
'file' : {},
'gdbus-peer' : {
'dependencies' : [libgdbus_example_objectmanager_dep],
'install_rpath' : installed_tests_execdir,
# FIXME: https://gitlab.gnome.org/GNOME/glib/-/issues/1392
'should_fail' : host_system == 'darwin',
},
'gdbus-peer-object-manager' : {},
'live-g-file' : {},
'resolver-parsing' : {'dependencies' : [network_libs]},
'socket-address' : {},
'stream-rw_all' : {},
'unix-fd' : {},
'unix-mounts' : {},
'unix-streams' : {},
'g-file-info-filesystem-readonly' : {},

View File

@ -2,8 +2,16 @@
#include <gio/gnetworking.h>
#include <gio/gunixfdmessage.h>
#include <gio/gunixsocketaddress.h>
#include <string.h>
#ifdef G_OS_UNIX
#include <glib-unix.h>
#include <unistd.h>
#endif
#include <glib/gstdio.h>
#include <fcntl.h>
#include <string.h>
#ifdef G_OS_WIN32
#include <io.h>
#endif
/* ensures that no FDs are left open at the end */
static void
@ -20,7 +28,7 @@ check_fd_list (const gint *fd_list)
}
for (i = 0; i < 40; i++)
close (fd_list[i]);
g_close (fd_list[i], NULL);
}
static void
@ -35,12 +43,68 @@ create_fd_list (gint *fd_list)
}
for (i = 0; i < 40; i++)
close (fd_list[i]);
g_close (fd_list[i], NULL);
}
static void
test_fd_list (void)
{
GError *err = NULL;
GUnixFDList *list;
const gint *peek;
gint *stolen;
gint fd_list[40];
gint sv[3];
gint s;
create_fd_list (fd_list);
sv[2] = -1;
#ifdef G_OS_WIN32
s = _pipe (sv, 4096, _O_NOINHERIT | _O_BINARY);
g_assert_cmpint (s, ==, 0);
#else
g_unix_open_pipe (sv, FD_CLOEXEC, &err);
g_assert_no_error (err);
#endif
list = g_unix_fd_list_new_from_array (sv, -1);
peek = g_unix_fd_list_peek_fds (list, &s);
g_assert_cmpint (s, ==, 2);
g_assert_cmpint (peek[0], ==, sv[0]);
g_assert_cmpint (peek[1], ==, sv[1]);
s = g_unix_fd_list_get (list, 0, &err);
g_assert_no_error (err);
g_close (s, &err);
g_assert_no_error (err);
s = g_unix_fd_list_get (list, 1, &err);
g_assert_no_error (err);
g_close (s, &err);
g_assert_no_error (err);
s = g_unix_fd_list_append (list, sv[0], &err);
g_assert_no_error (err);
g_assert_cmpint (s, >=, 0);
stolen = g_unix_fd_list_steal_fds (list, &s);
g_assert_cmpint (s, ==, 3);
g_assert_cmpint (stolen[0], ==, sv[0]);
g_assert_cmpint (stolen[1], ==, sv[1]);
g_assert_cmpint (stolen[2], >=, 0);
g_close (stolen[0], &err);
g_assert_no_error (err);
g_close (stolen[1], &err);
g_assert_no_error (err);
g_close (stolen[2], &err);
g_assert_no_error (err);
g_free (stolen);
g_object_unref (list);
check_fd_list (fd_list);
}
static void
test_scm (void)
{
#ifndef G_OS_WIN32
GError *err = NULL;
GUnixFDMessage *message;
GUnixFDMessage **mv;
@ -56,7 +120,7 @@ test_scm (void)
gint sv[3];
gint flags;
gint nm;
gint s;
gint s, i;
gchar *path;
GByteArray *array;
gboolean abstract;
@ -90,31 +154,24 @@ test_scm (void)
g_unix_fd_message_append_fd (message, sv[0], &err);
g_assert_no_error (err);
s = close (sv[0]);
g_assert_cmpint (s, ==, 0);
g_close (sv[0], &err);
g_assert_no_error (err);
g_unix_fd_message_append_fd (message, sv[1], &err);
g_assert_no_error (err);
s = close (sv[1]);
g_assert_cmpint (s, ==, 0);
g_close (sv[1], &err);
g_assert_no_error (err);
s = close (g_unix_fd_list_get (list, 0, &err));
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
s = close (g_unix_fd_list_get (list, 1, &err));
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
s = close (g_unix_fd_list_get (list, 0, &err));
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
s = close (g_unix_fd_list_get (list, 1, &err));
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
s = close (g_unix_fd_list_get (list, 0, &err));
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
s = close (g_unix_fd_list_get (list, 1, &err));
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
for (i = 0; i < 3; i++)
{
s = g_unix_fd_list_get (list, 0, &err);
g_assert_no_error (err);
g_close (s, &err);
g_assert_no_error (err);
s = g_unix_fd_list_get (list, 1, &err);
g_assert_no_error (err);
g_close (s, &err);
g_assert_no_error (err);
}
g_object_unref (message);
g_object_unref (list);
@ -131,16 +188,18 @@ test_scm (void)
g_assert_no_error (err);
g_assert_cmpint (s, >=, 0);
s = close (sv[0]);
g_assert_cmpint (s, ==, 0);
s = close (sv[1]);
g_assert_cmpint (s, ==, 0);
s = close (g_unix_fd_list_get (list, 0, &err));
g_close (sv[0], &err);
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
s = close (g_unix_fd_list_get (list, 1, &err));
g_close (sv[1], &err);
g_assert_no_error (err);
s = g_unix_fd_list_get (list, 0, &err);
g_assert_no_error (err);
g_close (s, &err);
g_assert_no_error (err);
s = g_unix_fd_list_get (list, 1, &err);
g_assert_no_error (err);
g_close (s, &err);
g_assert_no_error (err);
g_assert_cmpint (s, ==, 0);
s = socketpair (PF_UNIX, SOCK_STREAM, 0, sv);
g_assert_cmpint (s, ==, 0);
@ -217,7 +276,7 @@ G_GNUC_END_IGNORE_DEPRECATIONS
s = write (sv[0], buffer, strlen (buffer) + 1);
g_assert_cmpint (s, ==, strlen (buffer) + 1);
close (sv[0]);
g_close (sv[0], NULL);
memset (buffer, 0xff, sizeof buffer);
s = read (peek[0], buffer, sizeof buffer);
@ -228,6 +287,9 @@ G_GNUC_END_IGNORE_DEPRECATIONS
g_object_unref (list);
check_fd_list (fd_list);
#else
g_test_skip ("FD SCM support doesnt exist on Windows");
#endif
}
int
@ -235,8 +297,8 @@ main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/unix-fd/fd-list", test_fd_list);
g_test_add_func ("/unix-fd/scm", test_scm);
return g_test_run();
}

View File

@ -419,7 +419,8 @@ test_search_path_heap_allocation (void)
if (skip_win32 ())
return;
memset (placeholder, '_', sizeof (placeholder));
memset (placeholder, '_', sizeof (placeholder) - 1);
placeholder[sizeof (placeholder) - 1] = '\0';
/* Force search_path_buffer to be heap-allocated */
long_dir = g_test_build_filename (G_TEST_BUILT, "path-test-subdir", placeholder, NULL);
long_path = g_strjoin (G_SEARCHPATH_SEPARATOR_S, subdir, long_dir, NULL);

View File

@ -705,36 +705,22 @@ g_signal_group_new (GType target_type)
NULL);
}
/**
* g_signal_group_connect_closure:
* @self: a #GSignalGroup
* @detailed_signal: a string of the form `signal-name` with optional `::signal-detail`
* @closure: (not nullable): the closure to connect.
* @after: whether the handler should be called before or after the
* default handler of the signal.
*
* Connects @closure to the signal @detailed_signal on #GSignalGroup:target.
*
* You cannot connect a signal handler after #GSignalGroup:target has been set.
*
* Since: 2.74
*/
void
g_signal_group_connect_closure (GSignalGroup *self,
const gchar *detailed_signal,
GClosure *closure,
gboolean after)
static gboolean
g_signal_group_connect_closure_ (GSignalGroup *self,
const gchar *detailed_signal,
GClosure *closure,
gboolean after)
{
GObject *target;
SignalHandler *handler;
guint signal_id;
GQuark signal_detail;
g_return_if_fail (G_IS_SIGNAL_GROUP (self));
g_return_if_fail (detailed_signal != NULL);
g_return_if_fail (g_signal_parse_name (detailed_signal, self->target_type,
&signal_id, &signal_detail, TRUE) != 0);
g_return_if_fail (closure != NULL);
g_return_val_if_fail (G_IS_SIGNAL_GROUP (self), FALSE);
g_return_val_if_fail (detailed_signal != NULL, FALSE);
g_return_val_if_fail (g_signal_parse_name (detailed_signal, self->target_type,
&signal_id, &signal_detail, TRUE) != 0, FALSE);
g_return_val_if_fail (closure != NULL, FALSE);
g_rec_mutex_lock (&self->mutex);
@ -742,7 +728,7 @@ g_signal_group_connect_closure (GSignalGroup *self,
{
g_critical ("Cannot add signals after setting target");
g_rec_mutex_unlock (&self->mutex);
return;
return FALSE;
}
handler = g_slice_new0 (SignalHandler);
@ -768,6 +754,30 @@ g_signal_group_connect_closure (GSignalGroup *self,
g_signal_group_gc_handlers (self);
g_rec_mutex_unlock (&self->mutex);
return TRUE;
}
/**
* g_signal_group_connect_closure:
* @self: a #GSignalGroup
* @detailed_signal: a string of the form `signal-name` with optional `::signal-detail`
* @closure: (not nullable): the closure to connect.
* @after: whether the handler should be called before or after the
* default handler of the signal.
*
* Connects @closure to the signal @detailed_signal on #GSignalGroup:target.
*
* You cannot connect a signal handler after #GSignalGroup:target has been set.
*
* Since: 2.74
*/
void
g_signal_group_connect_closure (GSignalGroup *self,
const gchar *detailed_signal,
GClosure *closure,
gboolean after)
{
g_signal_group_connect_closure_ (self, detailed_signal, closure, after);
}
static void
@ -798,10 +808,11 @@ g_signal_group_connect_full (GSignalGroup *self,
g_object_watch_closure (data, closure);
}
g_signal_group_connect_closure (self,
detailed_signal,
closure,
(flags & G_CONNECT_AFTER) != 0);
if (!g_signal_group_connect_closure_ (self,
detailed_signal,
closure,
(flags & G_CONNECT_AFTER) != 0))
g_closure_unref (closure);
}
/**