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