LCOV - code coverage report
Current view: top level - glib/gio - gunixsocketaddress.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 126 149 84.6 %
Date: 2024-04-16 05:15:53 Functions: 19 21 90.5 %
Branches: 41 57 71.9 %

           Branch data     Line data    Source code
       1                 :            : /* GIO - GLib Input, Output and Streaming Library
       2                 :            :  *
       3                 :            :  * Copyright (C) 2008 Christian Kellner, Samuel Cormier-Iijima
       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                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General
      18                 :            :  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  *
      20                 :            :  * Authors: Christian Kellner <gicmo@gnome.org>
      21                 :            :  *          Samuel Cormier-Iijima <sciyoshi@gmail.com>
      22                 :            :  */
      23                 :            : 
      24                 :            : #include <config.h>
      25                 :            : #include <glib.h>
      26                 :            : #include <string.h>
      27                 :            : 
      28                 :            : #include "gunixsocketaddress.h"
      29                 :            : #include "gsocketconnectable.h"
      30                 :            : #include "glibintl.h"
      31                 :            : #include "gnetworking.h"
      32                 :            : 
      33                 :            : #ifdef G_OS_WIN32
      34                 :            : #include "giowin32-afunix.h"
      35                 :            : #endif
      36                 :            : 
      37                 :            : /**
      38                 :            :  * GUnixSocketAddress:
      39                 :            :  *
      40                 :            :  * Support for UNIX-domain (also known as local) sockets, corresponding to
      41                 :            :  * `struct sockaddr_un`.
      42                 :            :  *
      43                 :            :  * UNIX domain sockets are generally visible in the filesystem.
      44                 :            :  * However, some systems support abstract socket names which are not
      45                 :            :  * visible in the filesystem and not affected by the filesystem
      46                 :            :  * permissions, visibility, etc. Currently this is only supported
      47                 :            :  * under Linux. If you attempt to use abstract sockets on other
      48                 :            :  * systems, function calls may return `G_IO_ERROR_NOT_SUPPORTED`
      49                 :            :  * errors. You can use [func@Gio.UnixSocketAddress.abstract_names_supported]
      50                 :            :  * to see if abstract names are supported.
      51                 :            :  *
      52                 :            :  * Since GLib 2.72, `GUnixSocketAddress` is available on all platforms. It
      53                 :            :  * requires underlying system support (such as Windows 10 with `AF_UNIX`) at
      54                 :            :  * run time.
      55                 :            :  *
      56                 :            :  * Before GLib 2.72, `<gio/gunixsocketaddress.h>` belonged to the UNIX-specific
      57                 :            :  * GIO interfaces, thus you had to use the `gio-unix-2.0.pc` pkg-config file
      58                 :            :  * when using it. This is no longer necessary since GLib 2.72.
      59                 :            :  */
      60                 :            : 
      61                 :            : enum
      62                 :            : {
      63                 :            :   PROP_0,
      64                 :            :   PROP_PATH,
      65                 :            :   PROP_PATH_AS_ARRAY,
      66                 :            :   PROP_ABSTRACT,
      67                 :            :   PROP_ADDRESS_TYPE
      68                 :            : };
      69                 :            : 
      70                 :            : #ifndef UNIX_PATH_MAX
      71                 :            : #define UNIX_PATH_MAX G_SIZEOF_MEMBER (struct sockaddr_un, sun_path)
      72                 :            : #endif
      73                 :            : 
      74                 :            : struct _GUnixSocketAddressPrivate
      75                 :            : {
      76                 :            :   char path[UNIX_PATH_MAX]; /* Not including the initial zero in abstract case, so
      77                 :            :                                we can guarantee zero termination of abstract
      78                 :            :                                pathnames in the get_path() API */
      79                 :            :   gsize path_len; /* Not including any terminating zeros */
      80                 :            :   GUnixSocketAddressType address_type;
      81                 :            : };
      82                 :            : 
      83                 :            : static void   g_unix_socket_address_connectable_iface_init (GSocketConnectableIface *iface);
      84                 :            : static gchar *g_unix_socket_address_connectable_to_string  (GSocketConnectable      *connectable);
      85                 :            : 
      86   [ +  +  +  -  :      12495 : G_DEFINE_TYPE_WITH_CODE (GUnixSocketAddress, g_unix_socket_address, G_TYPE_SOCKET_ADDRESS,
                   +  + ]
      87                 :            :                          G_ADD_PRIVATE (GUnixSocketAddress)
      88                 :            :                          G_IMPLEMENT_INTERFACE (G_TYPE_SOCKET_CONNECTABLE,
      89                 :            :                                                 g_unix_socket_address_connectable_iface_init))
      90                 :            : 
      91                 :            : static void
      92                 :       5436 : g_unix_socket_address_set_property (GObject      *object,
      93                 :            :                                     guint         prop_id,
      94                 :            :                                     const GValue *value,
      95                 :            :                                     GParamSpec   *pspec)
      96                 :            : {
      97                 :       5436 :   GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
      98                 :            :   const char *str;
      99                 :            :   GByteArray *array;
     100                 :            :   gsize len;
     101                 :            : 
     102   [ +  +  +  +  :       5436 :   switch (prop_id)
                      - ]
     103                 :            :     {
     104                 :       1359 :     case PROP_PATH:
     105                 :       1359 :       str = g_value_get_string (value);
     106         [ +  + ]:       1359 :       if (str)
     107                 :            :         {
     108                 :       1345 :           g_strlcpy (address->priv->path, str,
     109                 :            :                      sizeof (address->priv->path));
     110                 :       1345 :           address->priv->path_len = strlen (address->priv->path);
     111                 :            :         }
     112                 :       1359 :       break;
     113                 :            : 
     114                 :       1359 :     case PROP_PATH_AS_ARRAY:
     115                 :       1359 :       array = g_value_get_boxed (value);
     116                 :            : 
     117         [ +  + ]:       1359 :       if (array)
     118                 :            :         {
     119                 :            :           /* Clip to fit in UNIX_PATH_MAX with zero termination or first byte */
     120                 :          5 :           len = MIN (array->len, UNIX_PATH_MAX-1);
     121                 :            : 
     122         [ +  + ]:          5 :           if (len != 0)
     123                 :          3 :             memcpy (address->priv->path, array->data, len);
     124                 :            : 
     125                 :          5 :           address->priv->path[len] = 0; /* Ensure null-terminated */
     126                 :          5 :           address->priv->path_len = len;
     127                 :            :         }
     128                 :       1359 :       break;
     129                 :            : 
     130                 :       1359 :     case PROP_ABSTRACT:
     131                 :            :       /* Only set it if it's not the default... */
     132         [ +  + ]:       1359 :       if (g_value_get_boolean (value))
     133                 :          1 :        address->priv->address_type = G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED;
     134                 :       1359 :       break;
     135                 :            : 
     136                 :       1359 :     case PROP_ADDRESS_TYPE:
     137                 :            :       /* Only set it if it's not the default... */
     138         [ +  + ]:       1359 :       if (g_value_get_enum (value) != G_UNIX_SOCKET_ADDRESS_PATH)
     139                 :          7 :         address->priv->address_type = g_value_get_enum (value);
     140                 :       1359 :       break;
     141                 :            : 
     142                 :          0 :     default:
     143                 :          0 :       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     144                 :            :     }
     145                 :       5436 : }
     146                 :            : 
     147                 :            : static void
     148                 :          4 : g_unix_socket_address_get_property (GObject    *object,
     149                 :            :                                     guint       prop_id,
     150                 :            :                                     GValue     *value,
     151                 :            :                                     GParamSpec *pspec)
     152                 :            : {
     153                 :          4 :   GUnixSocketAddress *address = G_UNIX_SOCKET_ADDRESS (object);
     154                 :            :   GByteArray *array;
     155                 :            : 
     156   [ +  +  +  +  :          4 :   switch (prop_id)
                      - ]
     157                 :            :     {
     158                 :          1 :       case PROP_PATH:
     159                 :          1 :         g_value_set_string (value, address->priv->path);
     160                 :          1 :         break;
     161                 :            : 
     162                 :          1 :       case PROP_PATH_AS_ARRAY:
     163                 :          1 :         array = g_byte_array_sized_new (address->priv->path_len);
     164                 :          1 :         g_byte_array_append (array, (guint8 *)address->priv->path, address->priv->path_len);
     165                 :          1 :         g_value_take_boxed (value, array);
     166                 :          1 :         break;
     167                 :            : 
     168                 :          1 :       case PROP_ABSTRACT:
     169         [ +  - ]:          2 :         g_value_set_boolean (value, (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
     170         [ -  + ]:          1 :                                      address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED));
     171                 :            : 
     172                 :          1 :         break;
     173                 :            : 
     174                 :          1 :       case PROP_ADDRESS_TYPE:
     175                 :          1 :         g_value_set_enum (value, address->priv->address_type);
     176                 :          1 :         break;
     177                 :            : 
     178                 :          0 :       default:
     179                 :          0 :         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
     180                 :            :     }
     181                 :          4 : }
     182                 :            : 
     183                 :            : static GSocketFamily
     184                 :       1349 : g_unix_socket_address_get_family (GSocketAddress *address)
     185                 :            : {
     186                 :            :   g_assert (PF_UNIX == G_SOCKET_FAMILY_UNIX);
     187                 :            : 
     188                 :       1349 :   return G_SOCKET_FAMILY_UNIX;
     189                 :            : }
     190                 :            : 
     191                 :            : static gssize
     192                 :       2700 : g_unix_socket_address_get_native_size (GSocketAddress *address)
     193                 :            : {
     194                 :       2700 :   GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
     195                 :            : 
     196      [ -  -  + ]:       2700 :   switch (addr->priv->address_type)
     197                 :            :     {
     198                 :          0 :     case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
     199                 :          0 :       return G_STRUCT_OFFSET(struct sockaddr_un, sun_path);
     200                 :          0 :     case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
     201                 :          0 :       return G_STRUCT_OFFSET(struct sockaddr_un, sun_path) + addr->priv->path_len + 1;
     202                 :       2700 :     default:
     203                 :       2700 :       return sizeof (struct sockaddr_un);
     204                 :            :     }
     205                 :            : }
     206                 :            : 
     207                 :            : static gboolean
     208                 :       1350 : g_unix_socket_address_to_native (GSocketAddress *address,
     209                 :            :                                  gpointer        dest,
     210                 :            :                                  gsize           destlen,
     211                 :            :                                  GError        **error)
     212                 :            : {
     213                 :       1350 :   GUnixSocketAddress *addr = G_UNIX_SOCKET_ADDRESS (address);
     214                 :            :   struct sockaddr_un *sock;
     215                 :            :   gssize socklen;
     216                 :            : 
     217                 :       1350 :   socklen = g_unix_socket_address_get_native_size (address);
     218                 :       1350 :   g_assert (socklen >= 0);
     219         [ -  + ]:       1350 :   if (destlen < (gsize) socklen)
     220                 :            :     {
     221                 :          0 :       g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
     222                 :            :                            _("Not enough space for socket address"));
     223                 :          0 :       return FALSE;
     224                 :            :     }
     225                 :            : 
     226                 :       1350 :   sock = (struct sockaddr_un *) dest;
     227                 :       1350 :   memset (sock, 0, socklen);
     228                 :       1350 :   sock->sun_family = AF_UNIX;
     229                 :            : 
     230   [ -  +  -  - ]:       1350 :   switch (addr->priv->address_type)
     231                 :            :     {
     232                 :          0 :     case G_UNIX_SOCKET_ADDRESS_INVALID:
     233                 :            :     case G_UNIX_SOCKET_ADDRESS_ANONYMOUS:
     234                 :          0 :       break;
     235                 :            : 
     236                 :       1350 :     case G_UNIX_SOCKET_ADDRESS_PATH:
     237                 :       1350 :       strcpy (sock->sun_path, addr->priv->path);
     238                 :       1350 :       break;
     239                 :            : 
     240                 :          0 :     case G_UNIX_SOCKET_ADDRESS_ABSTRACT:
     241                 :            :     case G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED:
     242         [ #  # ]:          0 :       if (!g_unix_socket_address_abstract_names_supported ())
     243                 :            :         {
     244                 :          0 :           g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
     245                 :            :                                _("Abstract UNIX domain socket addresses not supported on this system"));
     246                 :          0 :           return FALSE;
     247                 :            :         }
     248                 :            : 
     249                 :          0 :       sock->sun_path[0] = 0;
     250                 :          0 :       memcpy (sock->sun_path+1, addr->priv->path, addr->priv->path_len);
     251                 :          0 :       break;
     252                 :            :     }
     253                 :            : 
     254                 :       1350 :   return TRUE;
     255                 :            : }
     256                 :            : 
     257                 :            : static void
     258                 :        119 : g_unix_socket_address_class_init (GUnixSocketAddressClass *klass)
     259                 :            : {
     260                 :        119 :   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
     261                 :        119 :   GSocketAddressClass *gsocketaddress_class = G_SOCKET_ADDRESS_CLASS (klass);
     262                 :            : 
     263                 :        119 :   gobject_class->set_property = g_unix_socket_address_set_property;
     264                 :        119 :   gobject_class->get_property = g_unix_socket_address_get_property;
     265                 :            : 
     266                 :        119 :   gsocketaddress_class->get_family = g_unix_socket_address_get_family;
     267                 :        119 :   gsocketaddress_class->to_native = g_unix_socket_address_to_native;
     268                 :        119 :   gsocketaddress_class->get_native_size = g_unix_socket_address_get_native_size;
     269                 :            : 
     270                 :            :   /**
     271                 :            :    * GUnixSocketAddress:path:
     272                 :            :    *
     273                 :            :    * Unix socket path.
     274                 :            :    *
     275                 :            :    * Since: 2.22
     276                 :            :    */
     277                 :        119 :   g_object_class_install_property (gobject_class,
     278                 :            :                                    PROP_PATH,
     279                 :            :                                    g_param_spec_string ("path", NULL, NULL,
     280                 :            :                                                         NULL,
     281                 :            :                                                         G_PARAM_READWRITE |
     282                 :            :                                                         G_PARAM_CONSTRUCT_ONLY |
     283                 :            :                                                         G_PARAM_STATIC_STRINGS));
     284                 :            : 
     285                 :            :   /**
     286                 :            :    * GUnixSocketAddress:path-as-array:
     287                 :            :    *
     288                 :            :    * Unix socket path, as a byte array.
     289                 :            :    *
     290                 :            :    * Since: 2.22
     291                 :            :    */
     292                 :        119 :   g_object_class_install_property (gobject_class, PROP_PATH_AS_ARRAY,
     293                 :            :                                    g_param_spec_boxed ("path-as-array", NULL, NULL,
     294                 :            :                                                        G_TYPE_BYTE_ARRAY,
     295                 :            :                                                        G_PARAM_READWRITE |
     296                 :            :                                                        G_PARAM_CONSTRUCT_ONLY |
     297                 :            :                                                        G_PARAM_STATIC_STRINGS));
     298                 :            : 
     299                 :            :   /**
     300                 :            :    * GUnixSocketAddress:abstract:
     301                 :            :    *
     302                 :            :    * Whether or not this is an abstract address
     303                 :            :    *
     304                 :            :    * Deprecated: Use #GUnixSocketAddress:address-type, which
     305                 :            :    * distinguishes between zero-padded and non-zero-padded
     306                 :            :    * abstract addresses.
     307                 :            :    */
     308                 :        119 :   g_object_class_install_property (gobject_class, PROP_ABSTRACT,
     309                 :            :                                    g_param_spec_boolean ("abstract", NULL, NULL,
     310                 :            :                                                          FALSE,
     311                 :            :                                                          G_PARAM_READWRITE |
     312                 :            :                                                          G_PARAM_CONSTRUCT_ONLY |
     313                 :            :                                                          G_PARAM_STATIC_STRINGS));
     314                 :            : 
     315                 :            :   /**
     316                 :            :    * GUnixSocketAddress:address-type:
     317                 :            :    *
     318                 :            :    * The type of Unix socket address.
     319                 :            :    *
     320                 :            :    * Since: 2.22
     321                 :            :    */
     322                 :        119 :   g_object_class_install_property (gobject_class, PROP_ADDRESS_TYPE,
     323                 :            :                                    g_param_spec_enum ("address-type", NULL, NULL,
     324                 :            :                                                       G_TYPE_UNIX_SOCKET_ADDRESS_TYPE,
     325                 :            :                                                       G_UNIX_SOCKET_ADDRESS_PATH,
     326                 :            :                                                       G_PARAM_READWRITE |
     327                 :            :                                                       G_PARAM_CONSTRUCT_ONLY |
     328                 :            :                                                       G_PARAM_STATIC_STRINGS));
     329                 :        119 : }
     330                 :            : 
     331                 :            : static void
     332                 :        119 : g_unix_socket_address_connectable_iface_init (GSocketConnectableIface *iface)
     333                 :            : {
     334                 :        119 :   GSocketConnectableIface *parent_iface = g_type_interface_peek_parent (iface);
     335                 :            : 
     336                 :        119 :   iface->enumerate = parent_iface->enumerate;
     337                 :        119 :   iface->proxy_enumerate = parent_iface->proxy_enumerate;
     338                 :        119 :   iface->to_string = g_unix_socket_address_connectable_to_string;
     339                 :        119 : }
     340                 :            : 
     341                 :            : static gchar *
     342                 :          4 : g_unix_socket_address_connectable_to_string (GSocketConnectable *connectable)
     343                 :            : {
     344                 :            :   GUnixSocketAddress *ua;
     345                 :            :   GString *out;
     346                 :            :   const gchar *path;
     347                 :            :   gsize path_len, i;
     348                 :            : 
     349                 :          4 :   ua = G_UNIX_SOCKET_ADDRESS (connectable);
     350                 :            : 
     351                 :            :   /* Anonymous sockets have no path. */
     352         [ +  + ]:          4 :   if (ua->priv->address_type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
     353                 :          1 :     return g_strdup ("anonymous");
     354                 :            : 
     355                 :          3 :   path = g_unix_socket_address_get_path (ua);
     356                 :          3 :   path_len = g_unix_socket_address_get_path_len (ua);
     357                 :          3 :   out = g_string_sized_new (path_len);
     358                 :            : 
     359                 :            :   /* Return the #GUnixSocketAddress:path, but with all non-printable characters
     360                 :            :    * (including nul bytes) escaped to hex. */
     361         [ +  + ]:         47 :   for (i = 0; i < path_len; i++)
     362                 :            :     {
     363                 :         44 :       guint8 c = path[i];
     364                 :            : 
     365         [ +  + ]:         44 :       if (g_ascii_isprint (path[i]))
     366         [ +  - ]:         36 :         g_string_append_c (out, c);
     367                 :            :       else
     368                 :          8 :         g_string_append_printf (out, "\\x%02x", (guint) c);
     369                 :            :     }
     370                 :            : 
     371                 :          3 :   return g_string_free (out, FALSE);
     372                 :            : }
     373                 :            : 
     374                 :            : static void
     375                 :       1359 : g_unix_socket_address_init (GUnixSocketAddress *address)
     376                 :            : {
     377                 :       1359 :   address->priv = g_unix_socket_address_get_instance_private (address);
     378                 :            : 
     379                 :       1359 :   memset (address->priv->path, 0, sizeof (address->priv->path));
     380                 :       1359 :   address->priv->path_len = -1;
     381                 :       1359 :   address->priv->address_type = G_UNIX_SOCKET_ADDRESS_PATH;
     382                 :       1359 : }
     383                 :            : 
     384                 :            : /**
     385                 :            :  * g_unix_socket_address_new:
     386                 :            :  * @path: the socket path
     387                 :            :  *
     388                 :            :  * Creates a new #GUnixSocketAddress for @path.
     389                 :            :  *
     390                 :            :  * To create abstract socket addresses, on systems that support that,
     391                 :            :  * use g_unix_socket_address_new_abstract().
     392                 :            :  *
     393                 :            :  * Returns: a new #GUnixSocketAddress
     394                 :            :  *
     395                 :            :  * Since: 2.22
     396                 :            :  */
     397                 :            : GSocketAddress *
     398                 :       1345 : g_unix_socket_address_new (const gchar *path)
     399                 :            : {
     400                 :       1345 :   return g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
     401                 :            :                        "path", path,
     402                 :            :                        "abstract", FALSE,
     403                 :            :                        NULL);
     404                 :            : }
     405                 :            : 
     406                 :            : /**
     407                 :            :  * g_unix_socket_address_new_abstract:
     408                 :            :  * @path: (array length=path_len) (element-type gchar): the abstract name
     409                 :            :  * @path_len: the length of @path, or -1
     410                 :            :  *
     411                 :            :  * Creates a new %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED
     412                 :            :  * #GUnixSocketAddress for @path.
     413                 :            :  *
     414                 :            :  * Returns: a new #GUnixSocketAddress
     415                 :            :  *
     416                 :            :  * Deprecated: Use g_unix_socket_address_new_with_type().
     417                 :            :  */
     418                 :            : GSocketAddress *
     419                 :          0 : g_unix_socket_address_new_abstract (const gchar *path,
     420                 :            :                                     gint         path_len)
     421                 :            : {
     422                 :          0 :   return g_unix_socket_address_new_with_type (path, path_len,
     423                 :            :                                               G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
     424                 :            : }
     425                 :            : 
     426                 :            : /**
     427                 :            :  * g_unix_socket_address_new_with_type:
     428                 :            :  * @path: (array length=path_len) (element-type gchar): the name
     429                 :            :  * @path_len: the length of @path, or -1
     430                 :            :  * @type: a #GUnixSocketAddressType
     431                 :            :  *
     432                 :            :  * Creates a new #GUnixSocketAddress of type @type with name @path.
     433                 :            :  *
     434                 :            :  * If @type is %G_UNIX_SOCKET_ADDRESS_PATH, this is equivalent to
     435                 :            :  * calling g_unix_socket_address_new().
     436                 :            :  *
     437                 :            :  * If @type is %G_UNIX_SOCKET_ADDRESS_ANONYMOUS, @path and @path_len will be
     438                 :            :  * ignored.
     439                 :            :  *
     440                 :            :  * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT, then @path_len
     441                 :            :  * bytes of @path will be copied to the socket's path, and only those
     442                 :            :  * bytes will be considered part of the name. (If @path_len is -1,
     443                 :            :  * then @path is assumed to be NUL-terminated.) For example, if @path
     444                 :            :  * was "test", then calling g_socket_address_get_native_size() on the
     445                 :            :  * returned socket would return 7 (2 bytes of overhead, 1 byte for the
     446                 :            :  * abstract-socket indicator byte, and 4 bytes for the name "test").
     447                 :            :  *
     448                 :            :  * If @path_type is %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED, then
     449                 :            :  * @path_len bytes of @path will be copied to the socket's path, the
     450                 :            :  * rest of the path will be padded with 0 bytes, and the entire
     451                 :            :  * zero-padded buffer will be considered the name. (As above, if
     452                 :            :  * @path_len is -1, then @path is assumed to be NUL-terminated.) In
     453                 :            :  * this case, g_socket_address_get_native_size() will always return
     454                 :            :  * the full size of a `struct sockaddr_un`, although
     455                 :            :  * g_unix_socket_address_get_path_len() will still return just the
     456                 :            :  * length of @path.
     457                 :            :  *
     458                 :            :  * %G_UNIX_SOCKET_ADDRESS_ABSTRACT is preferred over
     459                 :            :  * %G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED for new programs. Of course,
     460                 :            :  * when connecting to a server created by another process, you must
     461                 :            :  * use the appropriate type corresponding to how that process created
     462                 :            :  * its listening socket.
     463                 :            :  *
     464                 :            :  * Returns: a new #GUnixSocketAddress
     465                 :            :  *
     466                 :            :  * Since: 2.26
     467                 :            :  */
     468                 :            : GSocketAddress *
     469                 :          5 : g_unix_socket_address_new_with_type (const gchar            *path,
     470                 :            :                                      gint                    path_len,
     471                 :            :                                      GUnixSocketAddressType  type)
     472                 :            : {
     473                 :            :   GSocketAddress *address;
     474                 :            :   GByteArray *array;
     475                 :            : 
     476         [ +  + ]:          5 :   if (type == G_UNIX_SOCKET_ADDRESS_ANONYMOUS)
     477                 :          2 :     path_len = 0;
     478         [ +  + ]:          3 :   else if (path_len == -1)
     479                 :          1 :     path_len = strlen (path);
     480                 :            : 
     481                 :          5 :   array = g_byte_array_sized_new (path_len);
     482                 :            : 
     483                 :          5 :   g_byte_array_append (array, (guint8 *)path, path_len);
     484                 :            : 
     485                 :          5 :   address = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
     486                 :            :                           "path-as-array", array,
     487                 :            :                           "address-type", type,
     488                 :            :                           NULL);
     489                 :            : 
     490                 :          5 :   g_byte_array_unref (array);
     491                 :            : 
     492                 :          5 :   return address;
     493                 :            : }
     494                 :            : 
     495                 :            : /**
     496                 :            :  * g_unix_socket_address_get_path:
     497                 :            :  * @address: a #GInetSocketAddress
     498                 :            :  *
     499                 :            :  * Gets @address's path, or for abstract sockets the "name".
     500                 :            :  *
     501                 :            :  * Guaranteed to be zero-terminated, but an abstract socket
     502                 :            :  * may contain embedded zeros, and thus you should use
     503                 :            :  * g_unix_socket_address_get_path_len() to get the true length
     504                 :            :  * of this string.
     505                 :            :  *
     506                 :            :  * Returns: the path for @address
     507                 :            :  *
     508                 :            :  * Since: 2.22
     509                 :            :  */
     510                 :            : const char *
     511                 :         23 : g_unix_socket_address_get_path (GUnixSocketAddress *address)
     512                 :            : {
     513                 :         23 :   return address->priv->path;
     514                 :            : }
     515                 :            : 
     516                 :            : /**
     517                 :            :  * g_unix_socket_address_get_path_len:
     518                 :            :  * @address: a #GInetSocketAddress
     519                 :            :  *
     520                 :            :  * Gets the length of @address's path.
     521                 :            :  *
     522                 :            :  * For details, see g_unix_socket_address_get_path().
     523                 :            :  *
     524                 :            :  * Returns: the length of the path
     525                 :            :  *
     526                 :            :  * Since: 2.22
     527                 :            :  */
     528                 :            : gsize
     529                 :          4 : g_unix_socket_address_get_path_len (GUnixSocketAddress *address)
     530                 :            : {
     531                 :          4 :   return address->priv->path_len;
     532                 :            : }
     533                 :            : 
     534                 :            : /**
     535                 :            :  * g_unix_socket_address_get_address_type:
     536                 :            :  * @address: a #GInetSocketAddress
     537                 :            :  *
     538                 :            :  * Gets @address's type.
     539                 :            :  *
     540                 :            :  * Returns: a #GUnixSocketAddressType
     541                 :            :  *
     542                 :            :  * Since: 2.26
     543                 :            :  */
     544                 :            : GUnixSocketAddressType
     545                 :         30 : g_unix_socket_address_get_address_type (GUnixSocketAddress *address)
     546                 :            : {
     547                 :         30 :   return address->priv->address_type;
     548                 :            : }
     549                 :            : 
     550                 :            : /**
     551                 :            :  * g_unix_socket_address_get_is_abstract:
     552                 :            :  * @address: a #GInetSocketAddress
     553                 :            :  *
     554                 :            :  * Tests if @address is abstract.
     555                 :            :  *
     556                 :            :  * Returns: %TRUE if the address is abstract, %FALSE otherwise
     557                 :            :  *
     558                 :            :  * Since: 2.22
     559                 :            :  *
     560                 :            :  * Deprecated: Use g_unix_socket_address_get_address_type()
     561                 :            :  */
     562                 :            : gboolean
     563                 :          1 : g_unix_socket_address_get_is_abstract (GUnixSocketAddress *address)
     564                 :            : {
     565         [ +  - ]:          2 :   return (address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT ||
     566         [ -  + ]:          1 :           address->priv->address_type == G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
     567                 :            : }
     568                 :            : 
     569                 :            : /**
     570                 :            :  * g_unix_socket_address_abstract_names_supported:
     571                 :            :  *
     572                 :            :  * Checks if abstract UNIX domain socket names are supported.
     573                 :            :  *
     574                 :            :  * Returns: %TRUE if supported, %FALSE otherwise
     575                 :            :  *
     576                 :            :  * Since: 2.22
     577                 :            :  */
     578                 :            : gboolean
     579                 :          0 : g_unix_socket_address_abstract_names_supported (void)
     580                 :            : {
     581                 :            : #ifdef __linux__
     582                 :          0 :   return TRUE;
     583                 :            : #else
     584                 :            :   return FALSE;
     585                 :            : #endif
     586                 :            : }

Generated by: LCOV version 1.14