1 /* GDBus - GLib D-Bus Library
3 * Copyright (C) 2008-2009 Red Hat, Inc.
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.
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.
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.
20 * Author: David Zeuthen <davidz@redhat.com>
28 #include <glib/gi18n.h>
30 #include "gdbusutils.h"
34 * @title: D-Bus Utilities
35 * @short_description: Various utilities related to D-Bus.
38 * Various utility routines related to D-Bus.
42 is_valid_bus_name_character (gint c,
43 gboolean allow_hyphen)
46 (c >= '0' && c <= '9') ||
47 (c >= 'A' && c <= 'Z') ||
48 (c >= 'a' && c <= 'z') ||
50 (allow_hyphen && c == '-');
54 is_valid_initial_bus_name_character (gint c,
55 gboolean allow_initial_digit,
56 gboolean allow_hyphen)
58 if (allow_initial_digit)
59 return is_valid_bus_name_character (c, allow_hyphen);
62 (c >= 'A' && c <= 'Z') ||
63 (c >= 'a' && c <= 'z') ||
65 (allow_hyphen && c == '-');
69 is_valid_name (const gchar *start,
71 gboolean allow_initial_digit,
72 gboolean allow_hyphen)
92 if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, allow_initial_digit, allow_hyphen)))
96 else if (G_UNLIKELY (!is_valid_bus_name_character (*s, allow_hyphen)))
103 if (G_UNLIKELY (!has_dot))
114 * @string: The string to check.
116 * Checks if @string is a valid D-Bus bus name (either unique or well-known).
118 * Returns: %TRUE if valid, %FALSE otherwise.
121 g_dbus_is_name (const gchar *string)
128 g_return_val_if_fail (string != NULL, FALSE);
132 len = strlen (string);
133 if (G_UNLIKELY (len == 0 || len > 255))
140 /* handle unique name */
141 if (!is_valid_name (s + 1, len - 1, TRUE, TRUE))
146 else if (G_UNLIKELY (*s == '.'))
148 /* can't start with a . */
151 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, TRUE)))
154 ret = is_valid_name (s + 1, len - 1, FALSE, TRUE);
161 * g_dbus_is_unique_name:
162 * @string: The string to check.
164 * Checks if @string is a valid D-Bus unique bus name.
166 * Returns: %TRUE if valid, %FALSE otherwise.
169 g_dbus_is_unique_name (const gchar *string)
174 g_return_val_if_fail (string != NULL, FALSE);
178 len = strlen (string);
179 if (G_UNLIKELY (len == 0 || len > 255))
182 if (G_UNLIKELY (*string != ':'))
185 if (G_UNLIKELY (!is_valid_name (string + 1, len - 1, TRUE, TRUE)))
195 * g_dbus_is_member_name:
196 * @string: The string to check.
198 * Checks if @string is a valid D-Bus member (e.g. signal or method) name.
200 * Returns: %TRUE if valid, %FALSE otherwise.
203 g_dbus_is_member_name (const gchar *string)
209 if (G_UNLIKELY (string == NULL))
212 if (G_UNLIKELY (!is_valid_initial_bus_name_character (string[0], FALSE, FALSE)))
215 for (n = 1; string[n] != '\0'; n++)
217 if (G_UNLIKELY (!is_valid_bus_name_character (string[n], FALSE)))
230 * g_dbus_is_interface_name:
231 * @string: The string to check.
233 * Checks if @string is a valid D-Bus interface name.
235 * Returns: %TRUE if valid, %FALSE otherwise.
238 g_dbus_is_interface_name (const gchar *string)
245 g_return_val_if_fail (string != NULL, FALSE);
249 len = strlen (string);
250 if (G_UNLIKELY (len == 0 || len > 255))
255 if (G_UNLIKELY (*s == '.'))
257 /* can't start with a . */
260 else if (G_UNLIKELY (!is_valid_initial_bus_name_character (*s, FALSE, FALSE)))
263 ret = is_valid_name (s + 1, len - 1, FALSE, FALSE);
269 /* ---------------------------------------------------------------------------------------------------- */
271 /* TODO: maybe move to glib? if so, it should conform to http://en.wikipedia.org/wiki/Guid and/or
272 * http://tools.ietf.org/html/rfc4122 - specifically it should have hyphens then.
276 * g_dbus_generate_guid:
278 * Generate a D-Bus GUID that can be used with
279 * e.g. g_dbus_connection_new().
281 * See the D-Bus specification regarding what strings are valid D-Bus
282 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
284 * Returns: A valid D-Bus GUID. Free with g_free().
287 g_dbus_generate_guid (void)
295 s = g_string_new (NULL);
297 r1 = g_random_int ();
298 r2 = g_random_int ();
299 r3 = g_random_int ();
300 g_get_current_time (&now);
302 g_string_append_printf (s, "%08x", r1);
303 g_string_append_printf (s, "%08x", r2);
304 g_string_append_printf (s, "%08x", r3);
305 g_string_append_printf (s, "%08x", (guint32) now.tv_sec);
307 return g_string_free (s, FALSE);
312 * @string: The string to check.
314 * Checks if @string is a D-Bus GUID.
316 * See the D-Bus specification regarding what strings are valid D-Bus
317 * GUID (for example, D-Bus GUIDs are not RFC-4122 compliant).
319 * Returns: %TRUE if @string is a guid, %FALSE otherwise.
322 g_dbus_is_guid (const gchar *string)
327 g_return_val_if_fail (string != NULL, FALSE);
331 for (n = 0; n < 32; n++)
333 if (!g_ascii_isxdigit (string[n]))
336 if (string[32] != '\0')
345 /* ---------------------------------------------------------------------------------------------------- */
349 * g_dbus_is_activated:
351 * Determine if the process has been activated by a message bus.
353 * Returns: %TRUE if this process has been started by a message bus, %FALSE otherwise.
356 g_dbus_is_activated (void)
358 /* TODO: technically this will make child processes forked by us
361 return g_getenv ("DBUS_STARTER_BUS_TYPE") != NULL;
364 /* ---------------------------------------------------------------------------------------------------- */