D-Bus: dbus-misc.c Source File

D-Bus 1.10.22

dbus-misc.c
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
3 *
4 * Copyright (C) 2006 Red Hat, Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24 #include <config.h>
25 #include "dbus-misc.h"
26 #include "dbus-internals.h"
27 #include "dbus-string.h"
28
72 char*
73 dbus_get_local_machine_id (void)
74 {
75 DBusString uuid;
76 char *s;
77
78 s = NULL;
79
80 if (!_dbus_string_init (&uuid))
81 return NULL;
82
83 /* The documentation says dbus_get_local_machine_id() only fails on OOM;
84 * this can actually also fail if the D-Bus installation is faulty
85 * (no UUID) *and* reading a new random UUID fails, but we have no way
86 * to report that */
87 if (!_dbus_get_local_machine_uuid_encoded (&uuid, NULL) ||
88 !_dbus_string_steal_data (&uuid, &s))
89 {
90 _dbus_string_free (&uuid);
91 return NULL;
92 }
93 else
94 {
95 _dbus_string_free (&uuid);
96 return s;
97 }
98
99 }
100
164 void
165 dbus_get_version (int *major_version_p,
166 int *minor_version_p,
167 int *micro_version_p)
168 {
169 if (major_version_p)
170 *major_version_p = DBUS_MAJOR_VERSION;
171 if (minor_version_p)
172 *minor_version_p = DBUS_MINOR_VERSION;
173 if (micro_version_p)
174 *micro_version_p = DBUS_MICRO_VERSION;
175 }
176
177 /* End of public API */
179
180 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
181
182 #ifndef DOXYGEN_SHOULD_SKIP_THIS
183
184 #include "dbus-test.h"
185 #include <stdlib.h>
186
187
188 dbus_bool_t
189 _dbus_misc_test (void)
190 {
191 int major, minor, micro;
192 DBusString str;
193
194 /* make sure we don't crash on NULL */
195 dbus_get_version (NULL, NULL, NULL);
196
197 /* Now verify that all the compile-time version stuff
198 * is right and matches the runtime. These tests
199 * are mostly intended to catch various kinds of
200 * typo (mixing up major and minor, that sort of thing).
201 */
202 dbus_get_version (&major, &minor, &micro);
203
204 _dbus_assert (major == DBUS_MAJOR_VERSION);
205 _dbus_assert (minor == DBUS_MINOR_VERSION);
206 _dbus_assert (micro == DBUS_MICRO_VERSION);
207
208 #define MAKE_VERSION(x, y, z) (((x) << 16) | ((y) << 8) | (z))
209
210 /* check that MAKE_VERSION works and produces the intended ordering */
211 _dbus_assert (MAKE_VERSION (1, 0, 0) > MAKE_VERSION (0, 0, 0));
212 _dbus_assert (MAKE_VERSION (1, 1, 0) > MAKE_VERSION (1, 0, 0));
213 _dbus_assert (MAKE_VERSION (1, 1, 1) > MAKE_VERSION (1, 1, 0));
214
215 _dbus_assert (MAKE_VERSION (2, 0, 0) > MAKE_VERSION (1, 1, 1));
216 _dbus_assert (MAKE_VERSION (2, 1, 0) > MAKE_VERSION (1, 1, 1));
217 _dbus_assert (MAKE_VERSION (2, 1, 1) > MAKE_VERSION (1, 1, 1));
218
219 /* check DBUS_VERSION */
220 _dbus_assert (MAKE_VERSION (major, minor, micro) == DBUS_VERSION);
221
222 /* check that ordering works with DBUS_VERSION */
223 _dbus_assert (MAKE_VERSION (major - 1, minor, micro) < DBUS_VERSION);
224 _dbus_assert (MAKE_VERSION (major, minor - 1, micro) < DBUS_VERSION);
225 _dbus_assert (MAKE_VERSION (major, minor, micro - 1) < DBUS_VERSION);
226
227 _dbus_assert (MAKE_VERSION (major + 1, minor, micro) > DBUS_VERSION);
228 _dbus_assert (MAKE_VERSION (major, minor + 1, micro) > DBUS_VERSION);
229 _dbus_assert (MAKE_VERSION (major, minor, micro + 1) > DBUS_VERSION);
230
231 /* Check DBUS_VERSION_STRING */
232
233 if (!_dbus_string_init (&str))
234 _dbus_assert_not_reached ("no memory");
235
236 if (!(_dbus_string_append_int (&str, major) &&
237 _dbus_string_append_byte (&str, '.') &&
238 _dbus_string_append_int (&str, minor) &&
239 _dbus_string_append_byte (&str, '.') &&
240 _dbus_string_append_int (&str, micro)))
241 _dbus_assert_not_reached ("no memory");
242
243 _dbus_assert (_dbus_string_equal_c_str (&str, DBUS_VERSION_STRING));
244
245 _dbus_string_free (&str);
246
247 return TRUE;
248 }
249
250 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
251
252 #endif
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
_dbus_string_append_int
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_string_append_int(DBusString *str, long value)
Appends an integer to a DBusString.
Definition: dbus-sysdeps.c:354
_dbus_get_local_machine_uuid_encoded
dbus_bool_t _dbus_get_local_machine_uuid_encoded(DBusString *uuid_str, DBusError *error)
Gets the hex-encoded UUID of the machine this function is executed on.
Definition: dbus-internals.c:884
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:148
dbus_get_version
void dbus_get_version(int *major_version_p, int *minor_version_p, int *micro_version_p)
Gets the DYNAMICALLY LINKED version of libdbus.
Definition: dbus-misc.c:165
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
_dbus_string_equal_c_str
dbus_bool_t _dbus_string_equal_c_str(const DBusString *a, const char *c_str)
Checks whether a string is equal to a C string.
Definition: dbus-string.c:2152
DBusString
Definition: dbus-string.h:42
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
_dbus_string_append_byte
dbus_bool_t _dbus_string_append_byte(DBusString *str, unsigned char byte)
Appends a single byte to the string, returning FALSE if not enough memory.
Definition: dbus-string.c:1157
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
TRUE
#define TRUE
Expands to "1".
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition: dbus-internals.h:159
dbus_get_local_machine_id
char * dbus_get_local_machine_id(void)
Obtains the machine UUID of the machine this process is running on.
Definition: dbus-misc.c:73
_dbus_string_steal_data
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:641