Esempio n. 1
0
/**
 * Reads (and optionally writes) a uuid to a file. Initializes the uuid
 * unless an error is returned.
 *
 * @param filename the name of the file
 * @param uuid uuid to be initialized with the loaded uuid
 * @param create_if_not_found #TRUE to create a new uuid and save it if the file doesn't exist
 * @param error the error return
 * @returns #FALSE if the error is set
 */
dbus_bool_t
_dbus_read_uuid_file (const DBusString *filename,
                      DBusGUID         *uuid,
                      dbus_bool_t       create_if_not_found,
                      DBusError        *error)
{
  DBusError read_error = DBUS_ERROR_INIT;

  if (_dbus_read_uuid_file_without_creating (filename, uuid, &read_error))
    return TRUE;

  if (!create_if_not_found)
    {
      dbus_move_error (&read_error, error);
      return FALSE;
    }

  /* If the file exists and contains junk, we want to keep that error
   * message instead of overwriting it with a "file exists" error
   * message when we try to write
   */
  if (dbus_error_has_name (&read_error, DBUS_ERROR_INVALID_FILE_CONTENT))
    {
      dbus_move_error (&read_error, error);
      return FALSE;
    }
  else
    {
      dbus_error_free (&read_error);
      return _dbus_create_uuid_file_exclusively (filename, uuid, error);
    }
}
int main()
{
	DBusError src;
	DBusError dest;
	char err[] = "Sample Error0";	
			
	dbus_error_init(&src);
	dbus_error_init(&dest);
	
	dbus_set_error(&src, err, NULL);
	
	dbus_move_error(&src, &dest);
	
	if(!CheckMovedError(&src, &dest, err))
	{
		std_log(LOG_FILENAME_LINE, "Test Fail");
		dbus_error_free(&src);
		dbus_error_free(&dest);
		create_xml(1);
		return 1;
	}
	else
	{
		std_log(LOG_FILENAME_LINE, "Test Successful");
		dbus_error_free(&dest);
		create_xml(0);
		return 0;
	}
}
Error::Error( DBusError* error )
: Dbg::Error( NULL )
{
	dbus_move_error(error, &_error);

#ifdef _DEBUG
	if( is_set() ) cbus_dbg("error %s: %s",name(), message() );
#endif
}
int main()
{
	DBusError src;
	DBusError dest;
	char str[] = "Sample Error No.";
	int no = 1;
	char err[] = "Sample Error0";
	
			
	dbus_error_init(&src);
	dbus_error_init(&dest);
	
	dbus_set_error(&src, err, "This is %s :: %d", str, no);
	
	dbus_move_error(&src, &dest);
	
	if(!CheckMovedError(&src, &dest, err))
	{
		std_log(LOG_FILENAME_LINE, "Test Fail");
		dbus_error_free(&src);
		dbus_error_free(&dest);
		create_xml(1);
		return 1;
	}
	else
	{
		if(strcmp("This is Sample Error No. :: 1", dest.message))
		{
			std_log(LOG_FILENAME_LINE, "Destination Message is not set Properly.");
			dbus_error_free(&src);
			dbus_error_free(&dest);
			create_xml(1);
			return 1;	
		}
		
		if(src.message)
		{
			std_log(LOG_FILENAME_LINE, "Source Message is not NULL.");
			dbus_error_free(&src);
			dbus_error_free(&dest);
			create_xml(1);
			return 1;
		}
		
		dbus_error_free(&dest);
		std_log(LOG_FILENAME_LINE, "Test Successful");
		create_xml(0);
		return 0;
	}
}
Esempio n. 5
0
DBusConnection *dsme_dbus_get_connection(DBusError *error)
{
    DBusError       err = DBUS_ERROR_INIT;
    DBusConnection *con = dsme_dbus_try_to_connect(&err);

    if( !con ) {
	if( error )
	    dbus_move_error(&err, error);
	else
	    dsme_log(LOG_DEBUG, "dbus_bus_get(): %s\n", err.message);
    }
    dbus_error_free(&err);

    // NOTE: returns null or new reference
    return con;
}
Esempio n. 6
0
static void
cb_method_call(void *data, DBusMessage *msg, DBusError *err)
{
  E_DBus_Callback *cb = data;
  void *method_return = NULL;
  DBusError new_err;
  if (!cb) return;

  dbus_error_init(&new_err);
  if (!dbus_error_is_set(err))
    method_return = e_dbus_callback_unmarshal(cb, msg, &new_err);
  else
    dbus_move_error(err, &new_err);

  e_dbus_callback_call(cb, method_return, &new_err);
  e_dbus_callback_return_free(cb, method_return);

  if (dbus_error_is_set(&new_err))
    dbus_error_free(&new_err);

  e_dbus_callback_free(cb);
}
Esempio n. 7
0
static BusDesktopFile *
desktop_file_for_name (BusConfigParser *parser,
                       const char *name,
                       DBusError  *error)
{
  BusDesktopFile *desktop_file;
  DBusList **service_dirs;
  DBusList *link;
  DBusError tmp_error;
  DBusString full_path;
  DBusString filename;
  const char *dir;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);

  desktop_file = NULL;

  if (!_dbus_string_init (&filename))
    {
      BUS_SET_OOM (error);
      goto out_all;
    }

  if (!_dbus_string_init (&full_path))
    {
      BUS_SET_OOM (error);
      goto out_filename;
    }

  if (!_dbus_string_append (&filename, name) ||
      !_dbus_string_append (&filename, ".service"))
    {
      BUS_SET_OOM (error);
      goto out;
    }

  service_dirs = bus_config_parser_get_service_dirs (parser);
  for (link = _dbus_list_get_first_link (service_dirs);
       link != NULL;
       link = _dbus_list_get_next_link (service_dirs, link))
    {
      dir = link->data;
      _dbus_verbose ("Looking at '%s'\n", dir);

      dbus_error_init (&tmp_error);

      /* clear the path from last time */
      _dbus_string_set_length (&full_path, 0);

      /* build the full path */
      if (!_dbus_string_append (&full_path, dir) ||
          !_dbus_concat_dir_and_file (&full_path, &filename))
        {
          BUS_SET_OOM (error);
          goto out;
        }

      _dbus_verbose ("Trying to load file '%s'\n", _dbus_string_get_data (&full_path));
      desktop_file = bus_desktop_file_load (&full_path, &tmp_error);
      if (desktop_file == NULL)
        {
          _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
          _dbus_verbose ("Could not load %s: %s: %s\n",
                         _dbus_string_get_const_data (&full_path),
                         tmp_error.name, tmp_error.message);

          /* we may have failed if the file is not found; this is not fatal */
          if (dbus_error_has_name (&tmp_error, DBUS_ERROR_NO_MEMORY))
            {
              dbus_move_error (&tmp_error, error);
              /* we only bail out on OOM */
              goto out;
            }
          dbus_error_free (&tmp_error);
        }

      /* did we find the desktop file we want? */
      if (desktop_file != NULL)
        break;
    }

  /* Didn't find desktop file; set error */
  if (desktop_file == NULL)
    {
      dbus_set_error (error, DBUS_ERROR_SPAWN_SERVICE_NOT_FOUND,
                      "The name %s was not provided by any .service files",
                      name);
    }

out:
  _dbus_string_free (&full_path);
out_filename:
  _dbus_string_free (&filename);
out_all:
  return desktop_file;
}
Esempio n. 8
0
/**
 * Try to open a new transport for the given address entry.  (This
 * opens a client-side-of-the-connection transport.)
 * 
 * @param entry the address entry
 * @param error location to store reason for failure.
 * @returns new transport of #NULL on failure.
 */
DBusTransport*
_dbus_transport_open (DBusAddressEntry *entry,
                      DBusError        *error)
{
  DBusTransport *transport;
  const char *expected_guid_orig;
  char *expected_guid;
  int i;
  DBusError tmp_error = DBUS_ERROR_INIT;

  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
  transport = NULL;
  expected_guid_orig = dbus_address_entry_get_value (entry, "guid");
  expected_guid = _dbus_strdup (expected_guid_orig);

  if (expected_guid_orig != NULL && expected_guid == NULL)
    {
      _DBUS_SET_OOM (error);
      return NULL;
    }

  for (i = 0; i < (int) _DBUS_N_ELEMENTS (open_funcs); ++i)
    {
      DBusTransportOpenResult result;

      _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
      result = (* open_funcs[i].func) (entry, &transport, &tmp_error);

      switch (result)
        {
        case DBUS_TRANSPORT_OPEN_OK:
          _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
          goto out;
          break;
        case DBUS_TRANSPORT_OPEN_NOT_HANDLED:
          _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
          /* keep going through the loop of open funcs */
          break;
        case DBUS_TRANSPORT_OPEN_BAD_ADDRESS:
          _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
          goto out;
          break;
        case DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT:
          _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
          goto out;
          break;
        }
    }

 out:
  
  if (transport == NULL)
    {
      if (!dbus_error_is_set (&tmp_error))
        _dbus_set_bad_address (&tmp_error,
                               NULL, NULL,
                               "Unknown address type (examples of valid types are \"tcp\" and on UNIX \"unix\")");
      
      _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
      dbus_move_error(&tmp_error, error);
      dbus_free (expected_guid);
    }
  else
    {
      _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);

      /* In the case of autostart the initial guid is NULL
       * and the autostart transport recursively calls
       * _dbus_open_transport wich returns a transport
       * with a guid.  That guid is the definitive one.
       *
       * FIXME: if more transports are added they may have
       * an effect on the expected_guid semantics (i.e. 
       * expected_guid and transport->expected_guid may
       * both have values).  This is very unlikely though
       * we should either throw asserts here for those 
       * corner cases or refactor the code so it is 
       * clearer on what is expected and what is not
       */
      if(expected_guid)
        transport->expected_guid = expected_guid;
    }

  return transport;
}
Esempio n. 9
0
/**
 * Listens for new connections on the given address.  If there are
 * multiple semicolon-separated address entries in the address, tries
 * each one and listens on the first one that works.
 *
 * Returns #NULL and sets error if listening fails for any reason.
 * Otherwise returns a new #DBusServer.
 * dbus_server_set_new_connection_function(),
 * dbus_server_set_watch_functions(), and
 * dbus_server_set_timeout_functions() should be called immediately to
 * render the server fully functional.
 *
 * To free the server, applications must call first
 * dbus_server_disconnect() and then dbus_server_unref().
 *
 * @param address the address of this server.
 * @param error location to store reason for failure.
 * @returns a new #DBusServer, or #NULL on failure.
 *
 */
DBusServer*
dbus_server_listen (const char     *address,
                    DBusError      *error)
{
    DBusServer *server;
    DBusAddressEntry **entries;
    int len, i;
    DBusError first_connect_error = DBUS_ERROR_INIT;
    dbus_bool_t handled_once;

    _dbus_return_val_if_fail (address != NULL, NULL);
    _dbus_return_val_if_error_is_set (error, NULL);

    if (!dbus_parse_address (address, &entries, &len, error))
        return NULL;

    server = NULL;
    handled_once = FALSE;

    for (i = 0; i < len; i++)
    {
        int j;

        for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
        {
            DBusServerListenResult result;
            DBusError tmp_error = DBUS_ERROR_INIT;

            result = (* listen_funcs[j].func) (entries[i],
                                               &server,
                                               &tmp_error);

            if (result == DBUS_SERVER_LISTEN_OK)
            {
                _dbus_assert (server != NULL);
                _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
                handled_once = TRUE;
                goto out;
            }
            else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
            {
                _dbus_assert (server == NULL);
                dbus_set_error (error,
                                DBUS_ERROR_ADDRESS_IN_USE,
                                "Address '%s' already used",
                                dbus_address_entry_get_method (entries[0]));
                handled_once = TRUE;
                goto out;
            }
            else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
            {
                _dbus_assert (server == NULL);
                _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
                dbus_move_error (&tmp_error, error);
                handled_once = TRUE;
                goto out;
            }
            else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
            {
                _dbus_assert (server == NULL);
                _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);

                /* keep trying addresses */
            }
            else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
            {
                _dbus_assert (server == NULL);
                _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
                if (!dbus_error_is_set (&first_connect_error))
                    dbus_move_error (&tmp_error, &first_connect_error);
                else
                    dbus_error_free (&tmp_error);

                handled_once = TRUE;

                /* keep trying addresses */
            }
        }

        _dbus_assert (server == NULL);
        _DBUS_ASSERT_ERROR_IS_CLEAR (error);
    }

out:

    if (!handled_once)
    {
        _DBUS_ASSERT_ERROR_IS_CLEAR (error);
        if (len > 0)
            dbus_set_error (error,
                            DBUS_ERROR_BAD_ADDRESS,
                            "Unknown address type '%s'",
                            dbus_address_entry_get_method (entries[0]));
        else
            dbus_set_error (error,
                            DBUS_ERROR_BAD_ADDRESS,
                            "Empty address '%s'",
                            address);
    }

    dbus_address_entries_free (entries);

    if (server == NULL)
    {
        _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
                      dbus_error_is_set (error));

        if (error && dbus_error_is_set (error))
        {
            /* already set the error */
        }
        else
        {
            /* didn't set the error but either error should be
             * NULL or first_connect_error should be set.
             */
            _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
            dbus_move_error (&first_connect_error, error);
        }

        _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
        _DBUS_ASSERT_ERROR_IS_SET (error);

        return NULL;
    }
    else
    {
        _DBUS_ASSERT_ERROR_IS_CLEAR (error);
        return server;
    }
}
BusConfigParser*
bus_config_load (const DBusString      *file,
                 dbus_bool_t            is_toplevel,
                 const BusConfigParser *parent,
                 DBusError             *error)

{
  xmlTextReader *reader;
  BusConfigParser *parser;
  DBusString dirname, data;
  DBusError tmp_error;
  int ret;
  
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  
  parser = NULL;
  reader = NULL;

  if (!_dbus_string_init (&dirname))
    {
      _DBUS_SET_OOM (error);
      return NULL;
    }

  if (!_dbus_string_init (&data))
    {
      _DBUS_SET_OOM (error);
      _dbus_string_free (&dirname);
      return NULL;
    }

  if (is_toplevel)
    {
      /* xmlMemSetup only fails if one of the functions is NULL */
    /*
      xmlMemSetup (dbus_free,
                   dbus_malloc,
                   dbus_realloc,
                   _dbus_strdup);
     */              
      xmlInitParser ();
      xmlSetGenericErrorFunc (NULL, xml_shut_up);
    }

  if (!_dbus_string_get_dirname (file, &dirname))
    {
      _DBUS_SET_OOM (error);
      goto failed;
    }
  
  parser = bus_config_parser_new (&dirname, is_toplevel, parent);
  if (parser == NULL)
    {
      _DBUS_SET_OOM (error);
      goto failed;
    }
  
  if (!_dbus_file_get_contents (&data, file, error))
    goto failed;

  reader = xmlReaderForMemory (_dbus_string_get_const_data (&data), 
                               _dbus_string_get_length (&data),
			       NULL, NULL, 0);
  if (reader == NULL)
    {
      _DBUS_SET_OOM (error);
      goto failed;
    }

  xmlTextReaderSetParserProp (reader, XML_PARSER_SUBST_ENTITIES, 1);

  dbus_error_init (&tmp_error);
  xmlTextReaderSetStructuredErrorHandler (reader, xml_text_reader_error, &tmp_error);

  while ((ret = xmlTextReaderRead (reader)) == 1)
    {
      int type;
      
      if (dbus_error_is_set (&tmp_error))
        goto reader_out;

      type = xmlTextReaderNodeType (reader);
      if (type == -1)
        {
          _DBUS_MAYBE_SET_OOM (&tmp_error);
          goto reader_out;
        }

      switch ((xmlReaderTypes) type) {
      case XML_READER_TYPE_ELEMENT:
	xml_text_start_element (parser, reader, &tmp_error);
	break;

      case XML_READER_TYPE_TEXT:
      case XML_READER_TYPE_CDATA:
	{
	  DBusString content;
	  const char *value;
#ifdef __SYMBIAN32__
	  value = (const char *) xmlTextReaderConstValue (reader);
#else
	  value = xmlTextReaderConstValue (reader);
#endif
	  if (value != NULL)
	    {
	      _dbus_string_init_const (&content, value);
	      bus_config_parser_content (parser, &content, &tmp_error);
	    }
          else
            _DBUS_MAYBE_SET_OOM (&tmp_error);
	  break;
	}

      case XML_READER_TYPE_DOCUMENT_TYPE:
	{
	  const char *name;
#ifdef __SYMBIAN32__
	  name = (const char *) xmlTextReaderConstName (reader);
#else
	  name = xmlTextReaderConstName (reader);
#endif
	  if (name != NULL)
	    bus_config_parser_check_doctype (parser, name, &tmp_error);
          else
            _DBUS_MAYBE_SET_OOM (&tmp_error);
	  break;
	}

      case XML_READER_TYPE_END_ELEMENT:
	{
	  const char *name;
#ifdef __SYMBIAN32__
	  name = (const char *) xmlTextReaderConstName (reader);
#else
	  name = xmlTextReaderConstName (reader);
#endif
	  if (name != NULL)
	    bus_config_parser_end_element (parser, name, &tmp_error);
          else
            _DBUS_MAYBE_SET_OOM (&tmp_error);
	  break;
	}

      case XML_READER_TYPE_DOCUMENT:
      case XML_READER_TYPE_DOCUMENT_FRAGMENT:
      case XML_READER_TYPE_PROCESSING_INSTRUCTION:
      case XML_READER_TYPE_COMMENT:
      case XML_READER_TYPE_ENTITY:
      case XML_READER_TYPE_NOTATION:
      case XML_READER_TYPE_WHITESPACE:
      case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
      case XML_READER_TYPE_END_ENTITY:
      case XML_READER_TYPE_XML_DECLARATION:
	/* nothing to do, just read on */
	break;

      case XML_READER_TYPE_NONE:
      case XML_READER_TYPE_ATTRIBUTE:
      case XML_READER_TYPE_ENTITY_REFERENCE:
	_dbus_assert_not_reached ("unexpected nodes in XML");
      }

      if (dbus_error_is_set (&tmp_error))
        goto reader_out;
    }

  if (ret == -1)
    _DBUS_MAYBE_SET_OOM (&tmp_error);

 reader_out:
  xmlFreeTextReader (reader);
  reader = NULL;
  if (dbus_error_is_set (&tmp_error))
    {
      dbus_move_error (&tmp_error, error);
      goto failed;
    }
  
  if (!bus_config_parser_finished (parser, error))
    goto failed;
  _dbus_string_free (&dirname);
  _dbus_string_free (&data);
  if (is_toplevel)
    xmlCleanupParser();
  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
  return parser;
  
 failed:
  _DBUS_ASSERT_ERROR_IS_SET (error);
  _dbus_string_free (&dirname);
  _dbus_string_free (&data);
  if (is_toplevel)
    xmlCleanupParser();
  if (parser)
    bus_config_parser_unref (parser);
  _dbus_assert (reader == NULL); /* must go to reader_out first */
  return NULL;
}