Example #1
0
static dbus_bool_t
return_uuid (DBusGUID   *uuid,
             char      **uuid_p,
             DBusError  *error)
{
  if (uuid_p)
    {
      DBusString encoded;

      if (!_dbus_string_init (&encoded))
        {
          _DBUS_SET_OOM (error);
          return FALSE;
        }

      if (!_dbus_uuid_encode (uuid, &encoded) ||
          !_dbus_string_steal_data (&encoded, uuid_p))
        {
          _DBUS_SET_OOM (error);
          _dbus_string_free (&encoded);
          return FALSE;
        }
      _dbus_string_free (&encoded);
    }
  return TRUE;
}
Example #2
0
/**
 * Gets the hex-encoded UUID of the machine this function is
 * executed on. This UUID is guaranteed to be the same for a given
 * machine at least until it next reboots, though it also
 * makes some effort to be the same forever, it may change if the
 * machine is reconfigured or its hardware is modified.
 * 
 * @param uuid_str string to append hex-encoded machine uuid to
 * @returns #FALSE if no memory
 */
dbus_bool_t
_dbus_get_local_machine_uuid_encoded (DBusString *uuid_str)
{
  dbus_bool_t ok;
  
  _DBUS_LOCK (machine_uuid);
  if (machine_uuid_initialized_generation != _dbus_current_generation)
    {
      DBusError error = DBUS_ERROR_INIT;

      if (!_dbus_read_local_machine_uuid (&machine_uuid, FALSE,
                                          &error))
        {          
#ifndef DBUS_BUILD_TESTS
          /* For the test suite, we may not be installed so just continue silently
           * here. But in a production build, we want to be nice and loud about
           * this.
           */
          _dbus_warn_check_failed ("D-Bus library appears to be incorrectly set up; failed to read machine uuid: %s\n"
                                   "See the manual page for dbus-uuidgen to correct this issue.\n",
                                   error.message);
#endif
          
          dbus_error_free (&error);
          
          _dbus_generate_uuid (&machine_uuid);
        }
    }

  ok = _dbus_uuid_encode (&machine_uuid, uuid_str);

  _DBUS_UNLOCK (machine_uuid);

  return ok;
}
Example #3
0
static dbus_bool_t
_dbus_create_uuid_file_exclusively (const DBusString *filename,
                                    DBusGUID         *uuid,
                                    DBusError        *error)
{
  DBusString encoded;

  if (!_dbus_string_init (&encoded))
    {
      _DBUS_SET_OOM (error);
      return FALSE;
    }

  _dbus_generate_uuid (uuid);
  
  if (!_dbus_uuid_encode (uuid, &encoded))
    {
      _DBUS_SET_OOM (error);
      goto error;
    }
  
  /* FIXME this is racy; we need a save_file_exclusively
   * function. But in practice this should be fine for now.
   *
   * - first be sure we can create the file and it
   *   doesn't exist by creating it empty with O_EXCL
   * - then create it by creating a temporary file and
   *   overwriting atomically with rename()
   */
  if (!_dbus_create_file_exclusively (filename, error))
    goto error;

  if (!_dbus_string_append_byte (&encoded, '\n'))
    {
      _DBUS_SET_OOM (error);
      goto error;
    }
  
  if (!_dbus_string_save_to_file (&encoded, filename, error))
    goto error;

  if (!_dbus_make_file_world_readable (filename, error))
    goto error;

  _dbus_string_free (&encoded);

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  return TRUE;
  
 error:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  _dbus_string_free (&encoded);
  return FALSE;        
}
/**
 * Write the give UUID to a file.
 *
 * @param filename the file to write
 * @param uuid the UUID to save
 * @param error used to raise an error
 * @returns #FALSE on error
 */
dbus_bool_t
_dbus_write_uuid_file (const DBusString *filename,
                       const DBusGUID   *uuid,
                       DBusError        *error)
{
  DBusString encoded;

  if (!_dbus_string_init (&encoded))
    {
      _DBUS_SET_OOM (error);
      return FALSE;
    }
  
  if (!_dbus_uuid_encode (uuid, &encoded))
    {
      _DBUS_SET_OOM (error);
      goto error;
    }
  
  if (!_dbus_string_append_byte (&encoded, '\n'))
    {
      _DBUS_SET_OOM (error);
      goto error;
    }
  
  if (!_dbus_string_save_to_file (&encoded, filename, TRUE, error))
    goto error;

  _dbus_string_free (&encoded);

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  return TRUE;
  
 error:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  _dbus_string_free (&encoded);
  return FALSE;        
}
Example #5
0
/**
 * Initializes the members of the DBusServer base class.
 * Chained up to by subclass constructors.
 *
 * @param server the server.
 * @param vtable the vtable for the subclass.
 * @param address the server's address
 * @returns #TRUE on success.
 */
dbus_bool_t
_dbus_server_init_base (DBusServer             *server,
                        const DBusServerVTable *vtable,
                        const DBusString       *address)
{
    server->vtable = vtable;

#ifdef DBUS_DISABLE_ASSERT
    _dbus_atomic_inc (&server->refcount);
#else
    {
        dbus_int32_t old_refcount = _dbus_atomic_inc (&server->refcount);

        _dbus_assert (old_refcount == 0);
    }
#endif

    server->address = NULL;
    server->watches = NULL;
    server->timeouts = NULL;
    server->published_address = FALSE;

    if (!_dbus_string_init (&server->guid_hex))
        return FALSE;

    _dbus_generate_uuid (&server->guid);

    if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
        goto failed;

    server->address = copy_address_with_guid_appended (address,
                      &server->guid_hex);
    if (server->address == NULL)
        goto failed;

    _dbus_rmutex_new_at_location (&server->mutex);
    if (server->mutex == NULL)
        goto failed;

    server->watches = _dbus_watch_list_new ();
    if (server->watches == NULL)
        goto failed;

    server->timeouts = _dbus_timeout_list_new ();
    if (server->timeouts == NULL)
        goto failed;

    _dbus_data_slot_list_init (&server->slot_list);

    _dbus_verbose ("Initialized server on address %s\n", server->address);

    return TRUE;

failed:
    _dbus_rmutex_free_at_location (&server->mutex);
    server->mutex = NULL;
    if (server->watches)
    {
        _dbus_watch_list_free (server->watches);
        server->watches = NULL;
    }
    if (server->timeouts)
    {
        _dbus_timeout_list_free (server->timeouts);
        server->timeouts = NULL;
    }
    if (server->address)
    {
        dbus_free (server->address);
        server->address = NULL;
    }
    _dbus_string_free (&server->guid_hex);

    return FALSE;
}
Example #6
0
/**
 * Initializes the members of the DBusServer base class.
 * Chained up to by subclass constructors.
 *
 * @param server the server.
 * @param vtable the vtable for the subclass.
 * @param address the server's address
 * @returns #TRUE on success.
 */
dbus_bool_t
_dbus_server_init_base (DBusServer             *server,
                        const DBusServerVTable *vtable,
                        const DBusString       *address)
{
  server->vtable = vtable;
  server->refcount.value = 1;

  server->address = NULL;
  server->watches = NULL;
  server->timeouts = NULL;

  if (!_dbus_string_init (&server->guid_hex))
    return FALSE;

  _dbus_generate_uuid (&server->guid);

  if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
    goto failed;
  
  server->address = copy_address_with_guid_appended (address,
                                                     &server->guid_hex);
  if (server->address == NULL)
    goto failed;
  
  _dbus_mutex_new_at_location (&server->mutex);
  if (server->mutex == NULL)
    goto failed;
  
  server->watches = _dbus_watch_list_new ();
  if (server->watches == NULL)
    goto failed;

  server->timeouts = _dbus_timeout_list_new ();
  if (server->timeouts == NULL)
    goto failed;

  _dbus_data_slot_list_init (&server->slot_list);

  _dbus_verbose ("Initialized server on address %s\n", server->address);
  
  return TRUE;

 failed:
  _dbus_mutex_free_at_location (&server->mutex);
  server->mutex = NULL;
  if (server->watches)
    {
      _dbus_watch_list_free (server->watches);
      server->watches = NULL;
    }
  if (server->timeouts)
    {
      _dbus_timeout_list_free (server->timeouts);
      server->timeouts = NULL;
    }
  if (server->address)
    {
      dbus_free (server->address);
      server->address = NULL;
    }
  _dbus_string_free (&server->guid_hex);
  
  return FALSE;
}