1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-misc.c A few assorted public functions that don't fit elsewhere
4 * Copyright (C) 2006 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include "dbus-misc.h"
26 #include "dbus-internals.h"
27 #include "dbus-string.h"
30 * @defgroup DBusMisc Miscellaneous
32 * @brief Miscellaneous API that doesn't cleanly fit anywhere else
38 * Obtains the machine UUID of the machine this process is running on.
40 * The returned string must be freed with dbus_free().
42 * This UUID is guaranteed to remain the same until the next reboot
43 * (unless the sysadmin foolishly changes it and screws themselves).
44 * It will usually remain the same across reboots also, but hardware
45 * configuration changes or rebuilding the machine could break that.
47 * The idea is that two processes with the same machine ID should be
48 * able to use shared memory, UNIX domain sockets, process IDs, and other
49 * features of the OS that require both processes to be running
50 * on the same OS kernel instance.
52 * The machine ID can also be used to create unique per-machine
53 * instances. For example, you could use it in bus names or
56 * The machine ID is preferred over the machine hostname, because
57 * the hostname is frequently set to "localhost.localdomain" and
58 * may also change at runtime.
60 * You can get the machine ID of a remote application by invoking the
61 * method GetMachineId from interface org.freedesktop.DBus.Peer.
63 * If the remote application has the same machine ID as the one
64 * returned by this function, then the remote application is on the
65 * same machine as your application.
67 * The UUID is not a UUID in the sense of RFC4122; the details
68 * are explained in the D-Bus specification.
70 * @returns a 32-byte-long hex-encoded UUID string, or #NULL on failure
73 dbus_try_get_local_machine_id (DBusError *error)
80 if (!_dbus_string_init (&uuid))
82 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
86 if (!_dbus_get_local_machine_uuid_encoded (&uuid, error))
88 _dbus_string_free (&uuid);
92 if (!_dbus_string_steal_data (&uuid, &s))
94 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
95 _dbus_string_free (&uuid);
100 _dbus_string_free (&uuid);
107 * Obtains the machine UUID of the machine this process is running on.
109 * The returned string must be freed with dbus_free().
111 * This function returns #NULL if there was not enough memory to read
112 * the UUID, or if the UUID could not be read because the D-Bus
113 * library was installed incorrectly. In the latter case, a warning
116 * Other than its deficient error reporting, this function is the same as
117 * dbus_try_get_local_machine_id().
119 * @returns a 32-byte-long hex-encoded UUID string, or #NULL on failure
122 dbus_get_local_machine_id (void)
124 DBusError error = DBUS_ERROR_INIT;
127 s = dbus_try_get_local_machine_id (&error);
129 /* The documentation says dbus_get_local_machine_id() only fails on OOM;
130 * this can actually also fail if the D-Bus installation is faulty
131 * (no UUID), but we have no good way to report that. Historically,
132 * _dbus_get_local_machine_uuid_encoded was responsible for issuing the
133 * warning; now we do that here. */
136 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
137 _dbus_warn_check_failed ("%s", error.message);
139 dbus_error_free (&error);
147 * @def DBUS_MAJOR_VERSION
149 * The COMPILE TIME major version of libdbus, that is, the "X" in "X.Y.Z",
150 * as an integer literal. Consider carefully whether to use this or the
151 * runtime version from dbus_get_version().
155 * @def DBUS_MINOR_VERSION
157 * The COMPILE TIME minor version of libdbus, that is, the "Y" in "X.Y.Z",
158 * as an integer literal. Consider carefully whether to use this or the
159 * runtime version from dbus_get_version().
163 * @def DBUS_MICRO_VERSION
165 * The COMPILE TIME micro version of libdbus, that is, the "Z" in "X.Y.Z",
166 * as an integer literal. Consider carefully whether to use this or the
167 * runtime version from dbus_get_version().
173 * The COMPILE TIME version of libdbus, as a single integer that has 0 in the most
174 * significant byte, the major version in the next most significant byte,
175 * the minor version in the third most significant, and the micro version in the
176 * least significant byte. This means two DBUS_VERSION can be compared to see
179 * Consider carefully whether to use this or the runtime version from
180 * dbus_get_version().
184 * @def DBUS_VERSION_STRING
186 * The COMPILE TIME version of libdbus, as a string "X.Y.Z".
188 * Consider carefully whether to use this or the runtime version from
189 * dbus_get_version().
193 * Gets the DYNAMICALLY LINKED version of libdbus. Alternatively, there
194 * are macros #DBUS_MAJOR_VERSION, #DBUS_MINOR_VERSION, #DBUS_MICRO_VERSION,
195 * and #DBUS_VERSION which allow you to test the VERSION YOU ARE COMPILED AGAINST.
196 * In other words, you can get either the runtime or the compile-time version.
197 * Think carefully about which of these you want in a given case.
199 * The libdbus full version number is "MAJOR.MINOR.MICRO" where the
200 * MINOR changes if API is added, and the MICRO changes with each
201 * release of a MAJOR.MINOR series. The MINOR is an odd number for
202 * development releases and an even number for stable releases.
204 * @param major_version_p pointer to return the major version, or #NULL
205 * @param minor_version_p pointer to return the minor version, or #NULL
206 * @param micro_version_p pointer to return the micro version, or #NULL
210 dbus_get_version (int *major_version_p,
211 int *minor_version_p,
212 int *micro_version_p)
215 *major_version_p = DBUS_MAJOR_VERSION;
217 *minor_version_p = DBUS_MINOR_VERSION;
219 *micro_version_p = DBUS_MICRO_VERSION;
223 /** @} */ /* End of public API */
225 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
227 #ifndef DOXYGEN_SHOULD_SKIP_THIS
229 #include "dbus-test.h"
234 _dbus_misc_test (void)
236 int major, minor, micro;
239 /* make sure we don't crash on NULL */
240 dbus_get_version (NULL, NULL, NULL);
242 /* Now verify that all the compile-time version stuff
243 * is right and matches the runtime. These tests
244 * are mostly intended to catch various kinds of
245 * typo (mixing up major and minor, that sort of thing).
247 dbus_get_version (&major, &minor, µ);
249 _dbus_assert (major == DBUS_MAJOR_VERSION);
250 _dbus_assert (minor == DBUS_MINOR_VERSION);
251 _dbus_assert (micro == DBUS_MICRO_VERSION);
253 #define MAKE_VERSION(x, y, z) (((x) << 16) | ((y) << 8) | (z))
255 /* check that MAKE_VERSION works and produces the intended ordering */
256 _dbus_assert (MAKE_VERSION (1, 0, 0) > MAKE_VERSION (0, 0, 0));
257 _dbus_assert (MAKE_VERSION (1, 1, 0) > MAKE_VERSION (1, 0, 0));
258 _dbus_assert (MAKE_VERSION (1, 1, 1) > MAKE_VERSION (1, 1, 0));
260 _dbus_assert (MAKE_VERSION (2, 0, 0) > MAKE_VERSION (1, 1, 1));
261 _dbus_assert (MAKE_VERSION (2, 1, 0) > MAKE_VERSION (1, 1, 1));
262 _dbus_assert (MAKE_VERSION (2, 1, 1) > MAKE_VERSION (1, 1, 1));
264 /* check DBUS_VERSION */
265 _dbus_assert (MAKE_VERSION (major, minor, micro) == DBUS_VERSION);
267 /* check that ordering works with DBUS_VERSION */
268 _dbus_assert (MAKE_VERSION (major - 1, minor, micro) < DBUS_VERSION);
269 _dbus_assert (MAKE_VERSION (major, minor - 1, micro) < DBUS_VERSION);
270 _dbus_assert (MAKE_VERSION (major, minor, micro - 1) < DBUS_VERSION);
272 _dbus_assert (MAKE_VERSION (major + 1, minor, micro) > DBUS_VERSION);
273 _dbus_assert (MAKE_VERSION (major, minor + 1, micro) > DBUS_VERSION);
274 _dbus_assert (MAKE_VERSION (major, minor, micro + 1) > DBUS_VERSION);
276 /* Check DBUS_VERSION_STRING */
278 if (!_dbus_string_init (&str))
279 _dbus_assert_not_reached ("no memory");
281 if (!(_dbus_string_append_int (&str, major) &&
282 _dbus_string_append_byte (&str, '.') &&
283 _dbus_string_append_int (&str, minor) &&
284 _dbus_string_append_byte (&str, '.') &&
285 _dbus_string_append_int (&str, micro)))
286 _dbus_assert_not_reached ("no memory");
288 _dbus_assert (_dbus_string_equal_c_str (&str, DBUS_VERSION_STRING));
290 _dbus_string_free (&str);
295 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */