D-Bus 1.12.4

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_try_get_local_machine_id (DBusError *error)
74{
75 DBusString uuid;
76 char *s;
77
78 s = NULL;
79
80 if (!_dbus_string_init (&uuid))
81 {
82 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
83 return NULL;
84 }
85
86 if (!_dbus_get_local_machine_uuid_encoded (&uuid, error))
87 {
88 _dbus_string_free (&uuid);
89 return NULL;
90 }
91
92 if (!_dbus_string_steal_data (&uuid, &s))
93 {
94 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
95 _dbus_string_free (&uuid);
96 return NULL;
97 }
98 else
99 {
100 _dbus_string_free (&uuid);
101 return s;
102 }
103
104}
105
121 char *
122 dbus_get_local_machine_id (void)
123{
124 DBusError error = DBUS_ERROR_INIT;
125 char *s;
126
127 s = dbus_try_get_local_machine_id (&error);
128
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. */
134 if (s == NULL)
135 {
136 if (!dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
137 _dbus_warn_check_failed ("%s", error.message);
138
139 dbus_error_free (&error);
140 return NULL;
141 }
142
143 return s;
144}
145
209 void
210 dbus_get_version (int *major_version_p,
211 int *minor_version_p,
212 int *micro_version_p)
213{
214 if (major_version_p)
215 *major_version_p = DBUS_MAJOR_VERSION;
216 if (minor_version_p)
217 *minor_version_p = DBUS_MINOR_VERSION;
218 if (micro_version_p)
219 *micro_version_p = DBUS_MICRO_VERSION;
220}
221
222 /* End of public API */
224
225#ifdef DBUS_ENABLE_EMBEDDED_TESTS
226
227#ifndef DOXYGEN_SHOULD_SKIP_THIS
228
229#include "dbus-test.h"
230#include <stdlib.h>
231
232
233 dbus_bool_t
234_dbus_misc_test (void)
235{
236 int major, minor, micro;
237 DBusString str;
238
239 /* make sure we don't crash on NULL */
240 dbus_get_version (NULL, NULL, NULL);
241
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).
246 */
247 dbus_get_version (&major, &minor, &micro);
248
249 _dbus_assert (major == DBUS_MAJOR_VERSION);
250 _dbus_assert (minor == DBUS_MINOR_VERSION);
251 _dbus_assert (micro == DBUS_MICRO_VERSION);
252
253#define MAKE_VERSION(x, y, z) (((x) << 16) | ((y) << 8) | (z))
254
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));
259
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));
263
264 /* check DBUS_VERSION */
265 _dbus_assert (MAKE_VERSION (major, minor, micro) == DBUS_VERSION);
266
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);
271
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);
275
276 /* Check DBUS_VERSION_STRING */
277
278 if (!_dbus_string_init (&str))
279 _dbus_assert_not_reached ("no memory");
280
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");
287
288 _dbus_assert (_dbus_string_equal_c_str (&str, DBUS_VERSION_STRING));
289
290 _dbus_string_free (&str);
291
292 return TRUE;
293}
294
295#endif /* !DOXYGEN_SHOULD_SKIP_THIS */
296
297#endif
DBUS_ERROR_INIT
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
dbus_error_has_name
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:302
dbus_set_error
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
dbus_error_free
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
_dbus_assert_not_reached
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
Definition: dbus-internals.h:163
_dbus_assert
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
Definition: dbus-internals.h:152
_dbus_warn_check_failed
void _dbus_warn_check_failed(const char *format,...)
Prints a "critical" warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
Definition: dbus-internals.c:262
_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:898
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
TRUE
#define TRUE
Expands to "1".
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:122
DBUS_MICRO_VERSION
#define DBUS_MICRO_VERSION
The COMPILE TIME micro version of libdbus, that is, the "Z" in "X.Y.Z", as an integer literal.
DBUS_VERSION_STRING
#define DBUS_VERSION_STRING
The COMPILE TIME version of libdbus, as a string "X.Y.Z".
dbus_try_get_local_machine_id
char * dbus_try_get_local_machine_id(DBusError *error)
Obtains the machine UUID of the machine this process is running on.
Definition: dbus-misc.c:73
DBUS_MINOR_VERSION
#define DBUS_MINOR_VERSION
The COMPILE TIME minor version of libdbus, that is, the "Y" in "X.Y.Z", as an integer literal.
DBUS_VERSION
#define DBUS_VERSION
The COMPILE TIME version of libdbus, as a single integer that has 0 in the most significant byte,...
DBUS_MAJOR_VERSION
#define DBUS_MAJOR_VERSION
The COMPILE TIME major version of libdbus, that is, the "X" in "X.Y.Z", as an integer literal.
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:210
DBUS_ERROR_NO_MEMORY
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
Definition: dbus-protocol.h:357
_dbus_string_init
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
_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:356
_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
_dbus_string_free
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:259
_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
_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_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
DBusError
Object representing an exception.
Definition: dbus-errors.h:49
DBusError::message
const char * message
public error message field
Definition: dbus-errors.h:51
DBusString
Definition: dbus-string.h:43