Ejemplo n.º 1
0
static dbus_bool_t 
check_signals (DBusConnection *monitor,
               int iteration,
               DBusConnection *conn[NUM_CONN])
{
  DBusConnection *lost_conn = NULL;
  DBusConnection *acquired_conn = NULL;
  const char *lost_name;
  const char *acquired_name;
  
  if (iteration == 0)
    {
      int i;
      i = test_data[iteration].expected_queue[0];

      if (i >= 0)
        acquired_conn = conn[i];
    }
  else
    {
      int i;
      i = test_data[iteration - 1].expected_queue[0];

      if (i >= 0)
        lost_conn = conn[i];

      i = test_data[iteration].expected_queue[0];

      if (i >= 0)
        acquired_conn = conn[i];

      if (acquired_conn == lost_conn)
        acquired_conn = lost_conn = NULL;
    }

    lost_name = lost_conn == NULL? NULL : 
                         dbus_bus_get_unique_name (lost_conn);

    acquired_name = acquired_conn == NULL? NULL :
                         dbus_bus_get_unique_name (acquired_conn);

    if (lost_name != NULL)
      if (!match_acquired_or_lost_signal (lost_conn,
                                         "NameLost",
                                         TEST_NAME))
        return FALSE;

    if (acquired_name != NULL)
      if (!match_acquired_or_lost_signal (acquired_conn,
                                         "NameAcquired",
                                         TEST_NAME))
        return FALSE;

    if (acquired_name != NULL || lost_name != NULL)
      if (!match_name_owner_changed_signal (monitor,
                                            TEST_NAME,
                                            lost_name,
                                            acquired_name))
        return FALSE;
    
    return TRUE;
}
Ejemplo n.º 2
0
int
main (int argc, char *argv[])
{
  DBusConnection *conn[NUM_CONN];
  DBusConnection *monitor;
  DBusError error;
  int i;
  int test_data_len;

  test_data_len = sizeof (test_data) / sizeof (CommandAndResult);
  
  dbus_error_init (&error);

  conn[0] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 0 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }
  
  if (!match_acquired_or_lost_signal (conn[0],
                                "NameAcquired",
                                dbus_bus_get_unique_name (conn[0])))
    return 1;
  
  conn[1] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 1 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (conn[1],
                                "NameAcquired",
                                dbus_bus_get_unique_name (conn[1])))
    return 1;


  conn[2] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 2 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (conn[2],
                                "NameAcquired",
                                dbus_bus_get_unique_name (conn[2])))
    return 1;


  conn[3] = dbus_bus_get_private (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open connection 3 to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (conn[3],
                                "NameAcquired",
                                dbus_bus_get_unique_name (conn[3])))
    return 1;


  monitor = dbus_bus_get (DBUS_BUS_SESSION, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to open monitoring connection to session bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  if (!match_acquired_or_lost_signal (monitor,
                                "NameAcquired",
                                dbus_bus_get_unique_name (monitor)))
    return 1;

  dbus_bus_add_match (monitor, "", &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "*** Failed to set filter on monitoring connection: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }


  for (i = 0; i < NUM_CONN; i++) 
    dbus_connection_set_exit_on_disconnect (conn[i], FALSE);

  for (i = 0; i < test_data_len; i++)
    {
      dbus_uint32_t result;
      result = 0;

      if (test_data[i].command == ADD_CONNECTION)
        {
          result = dbus_bus_request_name (conn[test_data[i].connection_number], 
                                          TEST_NAME, 
                                          test_data[i].flags,
                                          &error);

          if (dbus_error_is_set (&error))
            {
              fprintf (stderr, "Error on addition in iteration %i: %s\n", i, error.message);
              dbus_error_free (&error);
              return 1;
            }
        } 
      else if (test_data[i].command == REMOVE_CONNECTION)
        {
          result = dbus_bus_release_name (conn[test_data[i].connection_number], 
                                          TEST_NAME, 
                                          &error);  
          if (dbus_error_is_set (&error))
            {
              fprintf (stderr, "*** Failed to remove connection %i in iteration %i: %s\n",
                       test_data[i].connection_number,
                       i,
                       error.message);
              dbus_error_free (&error);
              return 1;
            }
        }
      else
        {
          fprintf (stderr, "Command #%i not a valid command!\n", test_data[i].command);
          return 1;
        }


      if (result != test_data[i].expected_result)
        {
          fprintf (stderr, "Results recived (%i) are not the expected results (%i) in iteration %i\n",
                   result,
                   test_data[i].expected_result,
                   i);
          return 1;
        }

      if (!check_connection (monitor, i, conn))
        {
          fprintf (stderr, "Failed at iteration %i\n", i);
          return 1;
        }

      if (!check_signals (monitor, i, conn))
        {
          fprintf (stderr, "Failed at iteration %i\n", i);
          return 1;
        }
    }

    return 0;
}
Ejemplo n.º 3
0
static DBusHandlerResult filter_handler(
	DBusConnection *c,
	DBusMessage *m,
	void *userdata) {

	rd_device *d;
	DBusError error;
	char *name_owner = NULL;

	dbus_error_init(&error);

	d = userdata;
	assert(d->ref >= 1);

	if (dbus_message_is_signal(m, "org.freedesktop.DBus", "NameLost")) {
		const char *name;

		if (!dbus_message_get_args(
			    m,
			    &error,
			    DBUS_TYPE_STRING, &name,
			    DBUS_TYPE_INVALID))
			goto invalid;

		if (strcmp(name, d->service_name) == 0 && d->owning) {
			/* Verify the actual owner of the name to avoid leaked NameLost
			 * signals from previous reservations. The D-Bus daemon will send
			 * all messages asynchronously in the correct order, but we could
			 * potentially process them too late due to the pseudo-blocking
			 * call mechanism used during both acquisition and release. This
			 * can happen if we release the device and immediately after
			 * reacquire it before NameLost is processed. */
			if (!d->gave_up) {
				const char *un;

				if ((un = dbus_bus_get_unique_name(c)) && rd_dbus_get_name_owner(c, d->service_name, &name_owner, &error) == 0)
					if (strcmp(name_owner, un) == 0)
						goto invalid; /* Name still owned by us */
			}

			d->owning = 0;

			if (!d->gave_up)  {
				d->ref++;

				if (d->request_cb)
					d->request_cb(d, 1);
				d->gave_up = 1;

				rd_release(d);
			}

		}
	}

invalid:
	free(name_owner);
	dbus_error_free(&error);

	return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}