Home | History | Annotate | Download | only in dbus
      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 
     29 /**
     30  * @defgroup DBusMisc Miscellaneous
     31  * @ingroup  DBus
     32  * @brief Miscellaneous API that doesn't cleanly fit anywhere else
     33  *
     34  * @{
     35  */
     36 
     37 /**
     38  * Obtains the machine UUID of the machine this process is running on.
     39  *
     40  * The returned string must be freed with dbus_free().
     41  *
     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.
     46  *
     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.
     51  *
     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
     54  * X selection names.
     55  *
     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.
     59  *
     60  * You can get the machine ID of a remote application by invoking the
     61  * method GetMachineId from interface org.freedesktop.DBus.Peer.
     62  *
     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.
     66  *
     67  * The UUID is not a UUID in the sense of RFC4122; the details
     68  * are explained in the D-Bus specification.
     69  *
     70  * @returns a 32-byte-long hex-encoded UUID string, or #NULL if insufficient memory
     71  */
     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   if (!_dbus_get_local_machine_uuid_encoded (&uuid) ||
     84       !_dbus_string_steal_data (&uuid, &s))
     85     {
     86       _dbus_string_free (&uuid);
     87       return NULL;
     88     }
     89   else
     90     {
     91       _dbus_string_free (&uuid);
     92       return s;
     93     }
     94 
     95 }
     96 
     97 /**
     98  * @def DBUS_MAJOR_VERSION
     99  *
    100  * The COMPILE TIME major version of libdbus, that is, the "X" in "X.Y.Z",
    101  * as an integer literal. Consider carefully whether to use this or the
    102  * runtime version from dbus_get_version().
    103  */
    104 
    105 /**
    106  * @def DBUS_MINOR_VERSION
    107  *
    108  * The COMPILE TIME minor version of libdbus, that is, the "Y" in "X.Y.Z",
    109  * as an integer literal. Consider carefully whether to use this or the
    110  * runtime version from dbus_get_version().
    111  */
    112 
    113 /**
    114  * @def DBUS_MICRO_VERSION
    115  *
    116  * The COMPILE TIME micro version of libdbus, that is, the "Z" in "X.Y.Z",
    117  * as an integer literal. Consider carefully whether to use this or the
    118  * runtime version from dbus_get_version().
    119  */
    120 
    121 /**
    122  * @def DBUS_VERSION
    123  *
    124  * The COMPILE TIME version of libdbus, as a single integer that has 0 in the most
    125  * significant byte, the major version in the next most significant byte,
    126  * the minor version in the third most significant, and the micro version in the
    127  * least significant byte. This means two DBUS_VERSION can be compared to see
    128  * which is higher.
    129  *
    130  * Consider carefully whether to use this or the runtime version from
    131  * dbus_get_version().
    132  */
    133 
    134 /**
    135  * @def DBUS_VERSION_STRING
    136  *
    137  * The COMPILE TIME version of libdbus, as a string "X.Y.Z".
    138  *
    139  * Consider carefully whether to use this or the runtime version from
    140  * dbus_get_version().
    141  */
    142 
    143 /**
    144  * Gets the DYNAMICALLY LINKED version of libdbus. Alternatively, there
    145  * are macros #DBUS_MAJOR_VERSION, #DBUS_MINOR_VERSION, #DBUS_MICRO_VERSION,
    146  * and #DBUS_VERSION which allow you to test the VERSION YOU ARE COMPILED AGAINST.
    147  * In other words, you can get either the runtime or the compile-time version.
    148  * Think carefully about which of these you want in a given case.
    149  *
    150  * The libdbus full version number is "MAJOR.MINOR.MICRO" where the
    151  * MINOR changes if API is added, and the MICRO changes with each
    152  * release of a MAJOR.MINOR series.  The MINOR is an odd number for
    153  * development releases and an even number for stable releases.
    154  *
    155  * @param major_version_p pointer to return the major version, or #NULL
    156  * @param minor_version_p pointer to return the minor version, or #NULL
    157  * @param micro_version_p pointer to return the micro version, or #NULL
    158  *
    159  */
    160 void
    161 dbus_get_version (int *major_version_p,
    162                   int *minor_version_p,
    163                   int *micro_version_p)
    164 {
    165   if (major_version_p)
    166     *major_version_p = DBUS_MAJOR_VERSION;
    167   if (minor_version_p)
    168     *minor_version_p = DBUS_MINOR_VERSION;
    169   if (micro_version_p)
    170     *micro_version_p = DBUS_MICRO_VERSION;
    171 }
    172 
    173 
    174 /** @} */ /* End of public API */
    175 
    176 #ifdef DBUS_BUILD_TESTS
    177 
    178 #ifndef DOXYGEN_SHOULD_SKIP_THIS
    179 
    180 #include "dbus-test.h"
    181 #include <stdlib.h>
    182 
    183 
    184 dbus_bool_t
    185 _dbus_misc_test (void)
    186 {
    187   int major, minor, micro;
    188   DBusString str;
    189 
    190   /* make sure we don't crash on NULL */
    191   dbus_get_version (NULL, NULL, NULL);
    192 
    193   /* Now verify that all the compile-time version stuff
    194    * is right and matches the runtime. These tests
    195    * are mostly intended to catch various kinds of
    196    * typo (mixing up major and minor, that sort of thing).
    197    */
    198   dbus_get_version (&major, &minor, &micro);
    199 
    200   _dbus_assert (major == DBUS_MAJOR_VERSION);
    201   _dbus_assert (minor == DBUS_MINOR_VERSION);
    202   _dbus_assert (micro == DBUS_MICRO_VERSION);
    203 
    204 #define MAKE_VERSION(x, y, z) (((x) << 16) | ((y) << 8) | (z))
    205 
    206   /* check that MAKE_VERSION works and produces the intended ordering */
    207   _dbus_assert (MAKE_VERSION (1, 0, 0) > MAKE_VERSION (0, 0, 0));
    208   _dbus_assert (MAKE_VERSION (1, 1, 0) > MAKE_VERSION (1, 0, 0));
    209   _dbus_assert (MAKE_VERSION (1, 1, 1) > MAKE_VERSION (1, 1, 0));
    210 
    211   _dbus_assert (MAKE_VERSION (2, 0, 0) > MAKE_VERSION (1, 1, 1));
    212   _dbus_assert (MAKE_VERSION (2, 1, 0) > MAKE_VERSION (1, 1, 1));
    213   _dbus_assert (MAKE_VERSION (2, 1, 1) > MAKE_VERSION (1, 1, 1));
    214 
    215   /* check DBUS_VERSION */
    216   _dbus_assert (MAKE_VERSION (major, minor, micro) == DBUS_VERSION);
    217 
    218   /* check that ordering works with DBUS_VERSION */
    219   _dbus_assert (MAKE_VERSION (major - 1, minor, micro) < DBUS_VERSION);
    220   _dbus_assert (MAKE_VERSION (major, minor - 1, micro) < DBUS_VERSION);
    221   _dbus_assert (MAKE_VERSION (major, minor, micro - 1) < DBUS_VERSION);
    222 
    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   /* Check DBUS_VERSION_STRING */
    228 
    229   if (!_dbus_string_init (&str))
    230     _dbus_assert_not_reached ("no memory");
    231 
    232   if (!(_dbus_string_append_int (&str, major) &&
    233         _dbus_string_append_byte (&str, '.') &&
    234         _dbus_string_append_int (&str, minor) &&
    235         _dbus_string_append_byte (&str, '.') &&
    236         _dbus_string_append_int (&str, micro)))
    237     _dbus_assert_not_reached ("no memory");
    238 
    239   _dbus_assert (_dbus_string_equal_c_str (&str, DBUS_VERSION_STRING));
    240 
    241   _dbus_string_free (&str);
    242 
    243   return TRUE;
    244 }
    245 
    246 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
    247 
    248 #endif
    249