Coverage Report

Created: 2025-11-11 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rauc/subprojects/glib-2.76.5/gio/gunixconnection.c
Line
Count
Source
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright © 2009 Codethink Limited
4
 *
5
 * SPDX-License-Identifier: LGPL-2.1-or-later
6
 *
7
 * This library is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * See the included COPYING file for more information.
13
 *
14
 * Authors: Ryan Lortie <desrt@desrt.ca>
15
 */
16
17
#include "config.h"
18
19
#include "gunixconnection.h"
20
#include "gnetworking.h"
21
#include "gsocket.h"
22
#include "gsocketcontrolmessage.h"
23
#include "gunixcredentialsmessage.h"
24
#include "gunixfdmessage.h"
25
#include "glibintl.h"
26
27
#include <errno.h>
28
#include <string.h>
29
#ifdef HAVE_UNISTD_H
30
#include <unistd.h>
31
#endif
32
33
/**
34
 * SECTION:gunixconnection
35
 * @title: GUnixConnection
36
 * @short_description: A UNIX domain GSocketConnection
37
 * @include: gio/gunixconnection.h
38
 * @see_also: #GSocketConnection.
39
 *
40
 * This is the subclass of #GSocketConnection that is created
41
 * for UNIX domain sockets.
42
 *
43
 * It contains functions to do some of the UNIX socket specific
44
 * functionality like passing file descriptors.
45
 *
46
 * Since GLib 2.72, #GUnixConnection is available on all platforms. It requires
47
 * underlying system support (such as Windows 10 with `AF_UNIX`) at run time.
48
 *
49
 * Before GLib 2.72, `<gio/gunixconnection.h>` belonged to the UNIX-specific GIO
50
 * interfaces, thus you had to use the `gio-unix-2.0.pc` pkg-config file when
51
 * using it. This is no longer necessary since GLib 2.72.
52
 *
53
 * Since: 2.22
54
 */
55
56
/**
57
 * GUnixConnection:
58
 *
59
 * #GUnixConnection is an opaque data structure and can only be accessed
60
 * using the following functions.
61
 **/
62
63
0
G_DEFINE_TYPE_WITH_CODE (GUnixConnection, g_unix_connection,
64
0
       G_TYPE_SOCKET_CONNECTION,
65
0
  g_socket_connection_factory_register_type (g_define_type_id,
66
0
               G_SOCKET_FAMILY_UNIX,
67
0
               G_SOCKET_TYPE_STREAM,
68
0
               G_SOCKET_PROTOCOL_DEFAULT);
69
0
       );
70
0
71
0
/**
72
0
 * g_unix_connection_send_fd:
73
0
 * @connection: a #GUnixConnection
74
0
 * @fd: a file descriptor
75
0
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
76
0
 * @error: (nullable): #GError for error reporting, or %NULL to ignore.
77
0
 *
78
0
 * Passes a file descriptor to the receiving side of the
79
0
 * connection. The receiving end has to call g_unix_connection_receive_fd()
80
0
 * to accept the file descriptor.
81
0
 *
82
0
 * As well as sending the fd this also writes a single byte to the
83
0
 * stream, as this is required for fd passing to work on some
84
0
 * implementations.
85
0
 *
86
0
 * Returns: a %TRUE on success, %NULL on error.
87
0
 *
88
0
 * Since: 2.22
89
0
 */
90
0
gboolean
91
0
g_unix_connection_send_fd (GUnixConnection  *connection,
92
0
                           gint              fd,
93
0
                           GCancellable     *cancellable,
94
0
                           GError          **error)
95
0
{
96
0
#ifdef G_OS_UNIX
97
0
  GSocketControlMessage *scm;
98
0
  GSocket *socket;
99
100
0
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
101
0
  g_return_val_if_fail (fd >= 0, FALSE);
102
103
0
  scm = g_unix_fd_message_new ();
104
105
0
  if (!g_unix_fd_message_append_fd (G_UNIX_FD_MESSAGE (scm), fd, error))
106
0
    {
107
0
      g_object_unref (scm);
108
0
      return FALSE;
109
0
    }
110
111
0
  g_object_get (connection, "socket", &socket, NULL);
112
0
  if (g_socket_send_message (socket, NULL, NULL, 0, &scm, 1, 0, cancellable, error) != 1)
113
    /* XXX could it 'fail' with zero? */
114
0
    {
115
0
      g_object_unref (socket);
116
0
      g_object_unref (scm);
117
118
0
      return FALSE;
119
0
    }
120
121
0
  g_object_unref (socket);
122
0
  g_object_unref (scm);
123
124
0
  return TRUE;
125
#else
126
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
127
                       _("Sending FD is not supported"));
128
  return FALSE;
129
#endif
130
0
}
131
132
/**
133
 * g_unix_connection_receive_fd:
134
 * @connection: a #GUnixConnection
135
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore
136
 * @error: (nullable): #GError for error reporting, or %NULL to ignore
137
 *
138
 * Receives a file descriptor from the sending end of the connection.
139
 * The sending end has to call g_unix_connection_send_fd() for this
140
 * to work.
141
 *
142
 * As well as reading the fd this also reads a single byte from the
143
 * stream, as this is required for fd passing to work on some
144
 * implementations.
145
 *
146
 * Returns: a file descriptor on success, -1 on error.
147
 *
148
 * Since: 2.22
149
 **/
150
gint
151
g_unix_connection_receive_fd (GUnixConnection  *connection,
152
                              GCancellable     *cancellable,
153
                              GError          **error)
154
0
{
155
0
#ifdef G_OS_UNIX
156
0
  GSocketControlMessage **scms;
157
0
  gint *fds, nfd, fd, nscm;
158
0
  GUnixFDMessage *fdmsg;
159
0
  GSocket *socket;
160
161
0
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), -1);
162
163
0
  g_object_get (connection, "socket", &socket, NULL);
164
0
  if (g_socket_receive_message (socket, NULL, NULL, 0,
165
0
                                &scms, &nscm, NULL, cancellable, error) != 1)
166
    /* XXX it _could_ 'fail' with zero. */
167
0
    {
168
0
      g_object_unref (socket);
169
170
0
      return -1;
171
0
    }
172
173
0
  g_object_unref (socket);
174
175
0
  if (nscm != 1)
176
0
    {
177
0
      gint i;
178
179
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
180
0
                   g_dngettext (NULL,
181
0
                                "Expecting 1 control message, got %d",
182
0
                                "Expecting 1 control message, got %d",
183
0
                                nscm),
184
0
                   nscm);
185
186
0
      for (i = 0; i < nscm; i++)
187
0
        g_object_unref (scms[i]);
188
189
0
      g_free (scms);
190
191
0
      return -1;
192
0
    }
193
194
0
  if (!G_IS_UNIX_FD_MESSAGE (scms[0]))
195
0
    {
196
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
197
0
         _("Unexpected type of ancillary data"));
198
0
      g_object_unref (scms[0]);
199
0
      g_free (scms);
200
201
0
      return -1;
202
0
    }
203
204
0
  fdmsg = G_UNIX_FD_MESSAGE (scms[0]);
205
0
  g_free (scms);
206
207
0
  fds = g_unix_fd_message_steal_fds (fdmsg, &nfd);
208
0
  g_object_unref (fdmsg);
209
210
0
  if (nfd != 1)
211
0
    {
212
0
      gint i;
213
214
0
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
215
0
                   g_dngettext (NULL,
216
0
                                "Expecting one fd, but got %d\n",
217
0
                                "Expecting one fd, but got %d\n",
218
0
                                nfd),
219
0
                   nfd);
220
221
0
      for (i = 0; i < nfd; i++)
222
0
        close (fds[i]);
223
224
0
      g_free (fds);
225
226
0
      return -1;
227
0
    }
228
229
0
  fd = *fds;
230
0
  g_free (fds);
231
232
0
  if (fd < 0)
233
0
    {
234
0
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
235
0
                           _("Received invalid fd"));
236
0
      fd = -1;
237
0
    }
238
239
0
  return fd;
240
#else
241
  g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
242
                       _("Receiving FD is not supported"));
243
  return -1;
244
#endif
245
0
}
246
247
static void
248
g_unix_connection_init (GUnixConnection *connection)
249
0
{
250
0
}
251
252
static void
253
g_unix_connection_class_init (GUnixConnectionClass *class)
254
0
{
255
0
}
256
257
/* TODO: Other stuff we might want to add are:
258
void                    g_unix_connection_send_fd_async                 (GUnixConnection      *connection,
259
                                                                         gint                  fd,
260
                                                                         gboolean              close,
261
                                                                         gint                  io_priority,
262
                                                                         GAsyncReadyCallback   callback,
263
                                                                         gpointer              user_data);
264
gboolean                g_unix_connection_send_fd_finish                (GUnixConnection      *connection,
265
                                                                         GError              **error);
266
267
gboolean                g_unix_connection_send_fds                      (GUnixConnection      *connection,
268
                                                                         gint                 *fds,
269
                                                                         gint                  nfds,
270
                                                                         GError              **error);
271
void                    g_unix_connection_send_fds_async                (GUnixConnection      *connection,
272
                                                                         gint                 *fds,
273
                                                                         gint                  nfds,
274
                                                                         gint                  io_priority,
275
                                                                         GAsyncReadyCallback   callback,
276
                                                                         gpointer              user_data);
277
gboolean                g_unix_connection_send_fds_finish               (GUnixConnection      *connection,
278
                                                                         GError              **error);
279
280
void                    g_unix_connection_receive_fd_async              (GUnixConnection      *connection,
281
                                                                         gint                  io_priority,
282
                                                                         GAsyncReadyCallback   callback,
283
                                                                         gpointer              user_data);
284
gint                    g_unix_connection_receive_fd_finish             (GUnixConnection      *connection,
285
                                                                         GError              **error);
286
287
288
gboolean                g_unix_connection_send_fake_credentials         (GUnixConnection      *connection,
289
                                                                         guint64               pid,
290
                                                                         guint64               uid,
291
                                                                         guint64               gid,
292
                                                                         GError              **error);
293
void                    g_unix_connection_send_fake_credentials_async   (GUnixConnection      *connection,
294
                                                                         guint64               pid,
295
                                                                         guint64               uid,
296
                                                                         guint64               gid,
297
                                                                         gint                  io_priority,
298
                                                                         GAsyncReadyCallback   callback,
299
                                                                         gpointer              user_data);
300
gboolean                g_unix_connection_send_fake_credentials_finish  (GUnixConnection      *connection,
301
                                                                         GError              **error);
302
303
gboolean                g_unix_connection_create_pair                   (GUnixConnection     **one,
304
                                                                         GUnixConnection     **two,
305
                                                                         GError              **error);
306
*/
307
308
309
/**
310
 * g_unix_connection_send_credentials:
311
 * @connection: A #GUnixConnection.
312
 * @cancellable: (nullable): A #GCancellable or %NULL.
313
 * @error: Return location for error or %NULL.
314
 *
315
 * Passes the credentials of the current user the receiving side
316
 * of the connection. The receiving end has to call
317
 * g_unix_connection_receive_credentials() (or similar) to accept the
318
 * credentials.
319
 *
320
 * As well as sending the credentials this also writes a single NUL
321
 * byte to the stream, as this is required for credentials passing to
322
 * work on some implementations.
323
 *
324
 * This method can be expected to be available on the following platforms:
325
 *
326
 * - Linux since GLib 2.26
327
 * - FreeBSD since GLib 2.26
328
 * - GNU/kFreeBSD since GLib 2.36
329
 * - Solaris, Illumos and OpenSolaris since GLib 2.40
330
 * - GNU/Hurd since GLib 2.40
331
 *
332
 * Other ways to exchange credentials with a foreign peer includes the
333
 * #GUnixCredentialsMessage type and g_socket_get_credentials() function.
334
 *
335
 * Returns: %TRUE on success, %FALSE if @error is set.
336
 *
337
 * Since: 2.26
338
 */
339
gboolean
340
g_unix_connection_send_credentials (GUnixConnection      *connection,
341
                                    GCancellable         *cancellable,
342
                                    GError              **error)
343
0
{
344
0
  GCredentials *credentials;
345
0
  GSocketControlMessage *scm;
346
0
  GSocket *socket;
347
0
  gboolean ret;
348
0
  GOutputVector vector;
349
0
  guchar nul_byte[1] = {'\0'};
350
0
  gint num_messages;
351
352
0
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), FALSE);
353
0
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
354
355
0
  ret = FALSE;
356
357
0
  credentials = g_credentials_new ();
358
359
0
  vector.buffer = &nul_byte;
360
0
  vector.size = 1;
361
362
0
  if (g_unix_credentials_message_is_supported ())
363
0
    {
364
0
      scm = g_unix_credentials_message_new_with_credentials (credentials);
365
0
      num_messages = 1;
366
0
    }
367
0
  else
368
0
    {
369
0
      scm = NULL;
370
0
      num_messages = 0;
371
0
    }
372
373
0
  g_object_get (connection, "socket", &socket, NULL);
374
0
  if (g_socket_send_message (socket,
375
0
                             NULL, /* address */
376
0
                             &vector,
377
0
                             1,
378
0
                             &scm,
379
0
                             num_messages,
380
0
                             G_SOCKET_MSG_NONE,
381
0
                             cancellable,
382
0
                             error) != 1)
383
0
    {
384
0
      g_prefix_error (error, _("Error sending credentials: "));
385
0
      goto out;
386
0
    }
387
388
0
  ret = TRUE;
389
390
0
 out:
391
0
  g_object_unref (socket);
392
0
  if (scm != NULL)
393
0
    g_object_unref (scm);
394
0
  g_object_unref (credentials);
395
0
  return ret;
396
0
}
397
398
static void
399
send_credentials_async_thread (GTask         *task,
400
             gpointer       source_object,
401
             gpointer       task_data,
402
             GCancellable  *cancellable)
403
0
{
404
0
  GError *error = NULL;
405
406
0
  if (g_unix_connection_send_credentials (G_UNIX_CONNECTION (source_object),
407
0
            cancellable,
408
0
            &error))
409
0
    g_task_return_boolean (task, TRUE);
410
0
  else
411
0
    g_task_return_error (task, error);
412
0
  g_object_unref (task);
413
0
}
414
415
/**
416
 * g_unix_connection_send_credentials_async:
417
 * @connection: A #GUnixConnection.
418
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
419
 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
420
 * @user_data: (closure): the data to pass to callback function
421
 *
422
 * Asynchronously send credentials.
423
 *
424
 * For more details, see g_unix_connection_send_credentials() which is
425
 * the synchronous version of this call.
426
 *
427
 * When the operation is finished, @callback will be called. You can then call
428
 * g_unix_connection_send_credentials_finish() to get the result of the operation.
429
 *
430
 * Since: 2.32
431
 **/
432
void
433
g_unix_connection_send_credentials_async (GUnixConnection      *connection,
434
                                          GCancellable         *cancellable,
435
                                          GAsyncReadyCallback   callback,
436
                                          gpointer              user_data)
437
0
{
438
0
  GTask *task;
439
440
0
  task = g_task_new (connection, cancellable, callback, user_data);
441
0
  g_task_set_source_tag (task, g_unix_connection_send_credentials_async);
442
0
  g_task_run_in_thread (task, send_credentials_async_thread);
443
0
}
444
445
/**
446
 * g_unix_connection_send_credentials_finish:
447
 * @connection: A #GUnixConnection.
448
 * @result: a #GAsyncResult.
449
 * @error: a #GError, or %NULL
450
 *
451
 * Finishes an asynchronous send credentials operation started with
452
 * g_unix_connection_send_credentials_async().
453
 *
454
 * Returns: %TRUE if the operation was successful, otherwise %FALSE.
455
 *
456
 * Since: 2.32
457
 **/
458
gboolean
459
g_unix_connection_send_credentials_finish (GUnixConnection *connection,
460
                                           GAsyncResult    *result,
461
                                           GError         **error)
462
0
{
463
0
  g_return_val_if_fail (g_task_is_valid (result, connection), FALSE);
464
465
0
  return g_task_propagate_boolean (G_TASK (result), error);
466
0
}
467
468
/**
469
 * g_unix_connection_receive_credentials:
470
 * @connection: A #GUnixConnection.
471
 * @cancellable: (nullable): A #GCancellable or %NULL.
472
 * @error: Return location for error or %NULL.
473
 *
474
 * Receives credentials from the sending end of the connection.  The
475
 * sending end has to call g_unix_connection_send_credentials() (or
476
 * similar) for this to work.
477
 *
478
 * As well as reading the credentials this also reads (and discards) a
479
 * single byte from the stream, as this is required for credentials
480
 * passing to work on some implementations.
481
 *
482
 * This method can be expected to be available on the following platforms:
483
 *
484
 * - Linux since GLib 2.26
485
 * - FreeBSD since GLib 2.26
486
 * - GNU/kFreeBSD since GLib 2.36
487
 * - Solaris, Illumos and OpenSolaris since GLib 2.40
488
 * - GNU/Hurd since GLib 2.40
489
 *
490
 * Other ways to exchange credentials with a foreign peer includes the
491
 * #GUnixCredentialsMessage type and g_socket_get_credentials() function.
492
 *
493
 * Returns: (transfer full): Received credentials on success (free with
494
 * g_object_unref()), %NULL if @error is set.
495
 *
496
 * Since: 2.26
497
 */
498
GCredentials *
499
g_unix_connection_receive_credentials (GUnixConnection      *connection,
500
                                       GCancellable         *cancellable,
501
                                       GError              **error)
502
0
{
503
0
  GCredentials *ret;
504
0
  GSocketControlMessage **scms;
505
0
  gint nscm;
506
0
  GSocket *socket;
507
0
  gint n;
508
0
  gssize num_bytes_read;
509
0
#ifdef __linux__
510
0
  gboolean turn_off_so_passcreds;
511
0
#endif
512
513
0
  g_return_val_if_fail (G_IS_UNIX_CONNECTION (connection), NULL);
514
0
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
515
516
0
  ret = NULL;
517
0
  scms = NULL;
518
519
0
  g_object_get (connection, "socket", &socket, NULL);
520
521
  /* On Linux, we need to turn on SO_PASSCRED if it isn't enabled
522
   * already. We also need to turn it off when we're done.  See
523
   * #617483 for more discussion.
524
   */
525
0
#ifdef __linux__
526
0
  {
527
0
    gint opt_val;
528
529
0
    turn_off_so_passcreds = FALSE;
530
0
    opt_val = 0;
531
0
    if (!g_socket_get_option (socket,
532
0
            SOL_SOCKET,
533
0
            SO_PASSCRED,
534
0
            &opt_val,
535
0
            NULL))
536
0
      {
537
0
        int errsv = errno;
538
0
        g_set_error (error,
539
0
                     G_IO_ERROR,
540
0
                     g_io_error_from_errno (errsv),
541
0
                     _("Error checking if SO_PASSCRED is enabled for socket: %s"),
542
0
                     g_strerror (errsv));
543
0
        goto out;
544
0
      }
545
0
    if (opt_val == 0)
546
0
      {
547
0
        if (!g_socket_set_option (socket,
548
0
          SOL_SOCKET,
549
0
          SO_PASSCRED,
550
0
          TRUE,
551
0
          NULL))
552
0
          {
553
0
            int errsv = errno;
554
0
            g_set_error (error,
555
0
                         G_IO_ERROR,
556
0
                         g_io_error_from_errno (errsv),
557
0
                         _("Error enabling SO_PASSCRED: %s"),
558
0
                         g_strerror (errsv));
559
0
            goto out;
560
0
          }
561
0
        turn_off_so_passcreds = TRUE;
562
0
      }
563
0
  }
564
0
#endif
565
566
0
  g_type_ensure (G_TYPE_UNIX_CREDENTIALS_MESSAGE);
567
0
  num_bytes_read = g_socket_receive_message (socket,
568
0
                                             NULL, /* GSocketAddress **address */
569
0
                                             NULL,
570
0
                                             0,
571
0
                                             &scms,
572
0
                                             &nscm,
573
0
                                             NULL,
574
0
                                             cancellable,
575
0
                                             error);
576
0
  if (num_bytes_read != 1)
577
0
    {
578
      /* Handle situation where g_socket_receive_message() returns
579
       * 0 bytes and not setting @error
580
       */
581
0
      if (num_bytes_read == 0 && error != NULL && *error == NULL)
582
0
        {
583
0
          g_set_error_literal (error,
584
0
                               G_IO_ERROR,
585
0
                               G_IO_ERROR_FAILED,
586
0
                               _("Expecting to read a single byte for receiving credentials but read zero bytes"));
587
0
        }
588
0
      goto out;
589
0
    }
590
591
0
  if (g_unix_credentials_message_is_supported () &&
592
      /* Fall back on get_credentials if the other side didn't send the credentials */
593
0
      nscm > 0)
594
0
    {
595
0
      if (nscm != 1)
596
0
        {
597
0
          g_set_error (error,
598
0
                       G_IO_ERROR,
599
0
                       G_IO_ERROR_FAILED,
600
0
                       g_dngettext (NULL,
601
0
                                    "Expecting 1 control message, got %d",
602
0
                                    "Expecting 1 control message, got %d",
603
0
                                    nscm),
604
0
                       nscm);
605
0
          goto out;
606
0
        }
607
608
0
      if (!G_IS_UNIX_CREDENTIALS_MESSAGE (scms[0]))
609
0
        {
610
0
          g_set_error_literal (error,
611
0
                               G_IO_ERROR,
612
0
                               G_IO_ERROR_FAILED,
613
0
                               _("Unexpected type of ancillary data"));
614
0
          goto out;
615
0
        }
616
617
0
      ret = g_unix_credentials_message_get_credentials (G_UNIX_CREDENTIALS_MESSAGE (scms[0]));
618
0
      g_object_ref (ret);
619
0
    }
620
0
  else
621
0
    {
622
0
      if (nscm != 0)
623
0
        {
624
0
          g_set_error (error,
625
0
                       G_IO_ERROR,
626
0
                       G_IO_ERROR_FAILED,
627
0
                       _("Not expecting control message, but got %d"),
628
0
                       nscm);
629
0
          goto out;
630
0
        }
631
0
      else
632
0
        {
633
0
          ret = g_socket_get_credentials (socket, error);
634
0
        }
635
0
    }
636
637
0
 out:
638
639
0
#ifdef __linux__
640
0
  if (turn_off_so_passcreds)
641
0
    {
642
0
      if (!g_socket_set_option (socket,
643
0
        SOL_SOCKET,
644
0
        SO_PASSCRED,
645
0
        FALSE,
646
0
        NULL))
647
0
        {
648
0
          int errsv = errno;
649
0
          g_set_error (error,
650
0
                       G_IO_ERROR,
651
0
                       g_io_error_from_errno (errsv),
652
0
                       _("Error while disabling SO_PASSCRED: %s"),
653
0
                       g_strerror (errsv));
654
0
          goto out;
655
0
        }
656
0
    }
657
0
#endif
658
659
0
  if (scms != NULL)
660
0
    {
661
0
      for (n = 0; n < nscm; n++)
662
0
        g_object_unref (scms[n]);
663
0
      g_free (scms);
664
0
    }
665
0
  g_object_unref (socket);
666
0
  return ret;
667
0
}
668
669
static void
670
receive_credentials_async_thread (GTask         *task,
671
          gpointer       source_object,
672
          gpointer       task_data,
673
          GCancellable  *cancellable)
674
0
{
675
0
  GCredentials *creds;
676
0
  GError *error = NULL;
677
678
0
  creds = g_unix_connection_receive_credentials (G_UNIX_CONNECTION (source_object),
679
0
                                                 cancellable,
680
0
                                                 &error);
681
0
  if (creds)
682
0
    g_task_return_pointer (task, creds, g_object_unref);
683
0
  else
684
0
    g_task_return_error (task, error);
685
0
  g_object_unref (task);
686
0
}
687
688
/**
689
 * g_unix_connection_receive_credentials_async:
690
 * @connection: A #GUnixConnection.
691
 * @cancellable: (nullable): optional #GCancellable object, %NULL to ignore.
692
 * @callback: (scope async): a #GAsyncReadyCallback to call when the request is satisfied
693
 * @user_data: (closure): the data to pass to callback function
694
 *
695
 * Asynchronously receive credentials.
696
 *
697
 * For more details, see g_unix_connection_receive_credentials() which is
698
 * the synchronous version of this call.
699
 *
700
 * When the operation is finished, @callback will be called. You can then call
701
 * g_unix_connection_receive_credentials_finish() to get the result of the operation.
702
 *
703
 * Since: 2.32
704
 **/
705
void
706
g_unix_connection_receive_credentials_async (GUnixConnection      *connection,
707
                                              GCancellable         *cancellable,
708
                                              GAsyncReadyCallback   callback,
709
                                              gpointer              user_data)
710
0
{
711
0
  GTask *task;
712
713
0
  task = g_task_new (connection, cancellable, callback, user_data);
714
0
  g_task_set_source_tag (task, g_unix_connection_receive_credentials_async);
715
0
  g_task_run_in_thread (task, receive_credentials_async_thread);
716
0
}
717
718
/**
719
 * g_unix_connection_receive_credentials_finish:
720
 * @connection: A #GUnixConnection.
721
 * @result: a #GAsyncResult.
722
 * @error: a #GError, or %NULL
723
 *
724
 * Finishes an asynchronous receive credentials operation started with
725
 * g_unix_connection_receive_credentials_async().
726
 *
727
 * Returns: (transfer full): a #GCredentials, or %NULL on error.
728
 *     Free the returned object with g_object_unref().
729
 *
730
 * Since: 2.32
731
 **/
732
GCredentials *
733
g_unix_connection_receive_credentials_finish (GUnixConnection *connection,
734
                                              GAsyncResult    *result,
735
                                              GError         **error)
736
0
{
737
0
  g_return_val_if_fail (g_task_is_valid (result, connection), NULL);
738
739
0
  return g_task_propagate_pointer (G_TASK (result), error);
740
0
}