Example #1
0
int
main (int argc, char *argv[])
{
  DBusServer *server;
  DBusError error;
  DBusLoop *loop;
  DBusConnection *session;
  TestServiceData *testdata;

  dbus_error_init (&error);

  loop = _dbus_loop_new ();

  testdata = dbus_new (TestServiceData, 1);
  testdata->loop = loop;

  session = dbus_bus_get (DBUS_BUS_SESSION, &error);
  if (!session)
    die ("couldn't access session bus");

  test_connection_setup (loop, session);

  dbus_bus_request_name (session, "org.freedesktop.DBus.TestSuite.PrivServer", 0, &error);
  if (dbus_error_is_set (&error))
    die ("couldn't request name: %s", error.message);

  if (!dbus_connection_add_filter (session, filter_session_message, testdata, NULL))
    die ("couldn't add filter");

#ifdef DBUS_CMAKE
  server = dbus_server_listen (TEST_LISTEN, &error);
#else
  server = dbus_server_listen ("unix:tmpdir=/tmp", &error);
#endif
  if (!server)
    die ("%s", error.message);
  testdata->private_addr = dbus_server_get_address (server);
  fprintf (stderr, "test server listening on %s\n", testdata->private_addr);

  dbus_server_set_new_connection_function (server, new_connection_callback,
                                           testdata, NULL);

  test_server_setup (loop, server);

  fprintf (stderr, "server running mainloop\n");
  _dbus_loop_run (loop);
  fprintf (stderr, "server mainloop quit\n");

  test_server_shutdown (loop, server);

  test_connection_shutdown (loop, session);

  dbus_connection_unref (session);

  _dbus_loop_unref (loop);

  dbus_free (testdata);

  return 0;
}
Example #2
0
static void
open_destroy_shared_session_bus_connection (void)
{
  DBusError error;
  DBusConnection *connection;
  char *session_addr_no_guid;
  char *comma;
  
  dbus_error_init (&error);

  session_addr_no_guid = strdup (getenv ("DBUS_SESSION_BUS_ADDRESS"));
  comma = strchr (session_addr_no_guid, ',');
  if (comma == NULL)
    die ("Couldn't find GUID in session bus address");
  *comma = '\0';
    
  connection = dbus_connection_open (session_addr_no_guid, &error);
  free (session_addr_no_guid);
  if (connection == NULL)
    die ("Failed to open connection to temp session bus\n");

  loop = _dbus_loop_new ();
  if (loop == NULL)
    die ("No memory\n");
  
  if (!test_connection_setup (loop, connection))
    die ("No memory\n");

  test_connection_shutdown (loop, connection);
 
  _dbus_loop_unref (loop);

  dbus_connection_unref (connection); 
}
Example #3
0
static void
setup_connection (Fixture *f,
    gconstpointer data)
{
  char *address;

  setup (f, data);

  dbus_server_set_new_connection_function (f->server,
      new_conn_cb, f, NULL);

  if (!test_server_setup (f->loop, f->server))
    g_error ("failed to set up server");

  address = dbus_server_get_address (f->server);
  g_assert (address != NULL);
  f->connection = dbus_connection_open_private (address, &f->e);
  assert_no_error (&f->e);
  g_assert (f->connection != NULL);
  dbus_free (address);

  if (!test_connection_setup (f->loop, f->connection))
    g_error ("failed to set up connection");

  while (f->server_connection == NULL)
    _dbus_loop_iterate (f->loop, TRUE);

  test_connection_shutdown (f->loop, f->connection);
  test_server_shutdown (f->loop, f->server);
}
Example #4
0
int dbusrecv_uninit(dbusrecv_hdl_st *p_dbusrecv_hdl)
{
    if (NULL == p_dbusrecv_hdl)
    {
        printf("dbusrecv_uninit: invalid parameter, p_dbusrecv_hdl!");
        return -1;
    }
    if (p_dbusrecv_hdl->dbusloop)
    {
        _dbus_loop_quit (p_dbusrecv_hdl->dbusloop);
        sleep(2);
    }

    if (p_dbusrecv_hdl->connection)
    {
        test_connection_shutdown (p_dbusrecv_hdl->dbusloop, p_dbusrecv_hdl->connection);
        if (p_dbusrecv_hdl->filter_func)
            dbus_connection_remove_filter (p_dbusrecv_hdl->connection, p_dbusrecv_hdl->filter_func, NULL);
        dbus_connection_unref (p_dbusrecv_hdl->connection);
        p_dbusrecv_hdl->connection = NULL;
    }
    if (p_dbusrecv_hdl->dbusloop)
    {
        _dbus_loop_unref (p_dbusrecv_hdl->dbusloop);
        p_dbusrecv_hdl->dbusloop = NULL;
    }
    dbus_shutdown();

    if (p_dbusrecv_hdl->dbusrecv_cfg.myname)
    {
        free(p_dbusrecv_hdl->dbusrecv_cfg.myname);
        p_dbusrecv_hdl->dbusrecv_cfg.myname = NULL;
    }
    free(p_dbusrecv_hdl);
    p_dbusrecv_hdl = NULL;

    _dbus_verbose("%s: service exiting!\n", __FILE__);

    return 0;
}
static void
open_shutdown_private_connection (dbus_bool_t use_guid)
{
  DBusError error;
  DBusLoop *loop;
  DBusConnection *session;
  DBusMessage *msg;
  DBusMessage *reply;
  DBusConnection *privconn;
  char *addr;
  dbus_bool_t service_died;
  dbus_bool_t private_conn_lost;

  dbus_error_init (&error);
  service_died = FALSE;
  private_conn_lost = FALSE;

  loop = _dbus_loop_new ();

  session = dbus_bus_get (DBUS_BUS_SESSION, &error);
  if (!session)
    die ("couldn't access session bus\n");
  dbus_connection_set_exit_on_disconnect (session, FALSE);
  test_connection_setup (loop, session);

  dbus_bus_add_match (session, PRIVSERVER_DIED_RULE, &error);
  if (dbus_error_is_set (&error))
    die ("couldn't add match rule \"%s\": %s: %s", PRIVSERVER_DIED_RULE,
         error.name, error.message);

  if (!dbus_connection_add_filter (session, filter_session_message,
                                   &service_died, NULL))
    die ("couldn't add filter to session bus\n");

  msg = dbus_message_new_method_call (PRIVSERVER_SERVICE, "/",
                                      PRIVSERVER_INTERFACE, "GetPrivateAddress");
  if (!(reply = dbus_connection_send_with_reply_and_block (session, msg, -1, &error)))
    die ("couldn't send message: %s\n", error.message);
  dbus_message_unref (msg);
  if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &addr, DBUS_TYPE_INVALID))
    die ("couldn't parse message replym\n");
  printf ("# got private temp address %s\n", addr);
  addr = strdup (addr);
  if (!use_guid)
    {
      char *comma = strrchr (addr, ',');
      if (comma)
        *comma = '\0';
    }
  privconn = dbus_connection_open (addr, &error);
  free (addr);
  if (!privconn)
    die ("couldn't connect to server direct connection: %s\n", error.message);
  dbus_message_unref (reply);

  dbus_connection_set_exit_on_disconnect (privconn, FALSE);
  if (!dbus_connection_add_filter (privconn, filter_private_message,
                                   &private_conn_lost, NULL))
    die ("couldn't add filter to private connection\n");
  test_connection_setup (loop, privconn);

  msg = dbus_message_new_method_call (PRIVSERVER_SERVICE, "/",
                                      PRIVSERVER_INTERFACE, "Quit");
  if (!dbus_connection_send (session, msg, NULL))
    die ("couldn't send Quit message\n");
  dbus_message_unref (msg);

  while (!service_died || !private_conn_lost)
    _dbus_loop_iterate (loop, TRUE);

  dbus_connection_remove_filter (session, filter_session_message,
                                 &service_died);
  dbus_bus_remove_match (session, PRIVSERVER_DIED_RULE, NULL);
  test_connection_shutdown (loop, session);
  dbus_connection_unref (session);

  test_connection_shutdown (loop, privconn);
  dbus_connection_remove_filter (privconn, filter_private_message,
                                 &private_conn_lost);
  dbus_connection_unref (privconn);

  _dbus_loop_unref (loop);
}
Example #6
0
dbusrecv_hdl_st *dbusrecv_init(char *myname, callbk_filter_func filter_func)
{
    dbusrecv_hdl_st *p_dbusrecv_hdl = NULL;
    int result = 0;
    int retval = 0;

    if (NULL == myname)
    {
        printf("dbusrev_init: invalid parameters, myname couldn't be NULL!\n");
        return NULL;
    }
    if (NULL == filter_func)
    {
        printf("dbusrev_init: invalid parameters, filter_func couldn't be NULL!\n");
        return NULL;
    }

    p_dbusrecv_hdl = DBUS_MALLOC(sizeof(dbusrecv_hdl_st));
    if (NULL == p_dbusrecv_hdl)
    {
        printf("dbusrev_init: malloc failed, p_dbusrecv_hdl!\n");
        return NULL;
    }

    p_dbusrecv_hdl->dbusrecv_cfg.myname = (char *)malloc(strlen(myname) + 1);
    memset(p_dbusrecv_hdl->dbusrecv_cfg.myname, 0x0, strlen(myname) + 1);
    memcpy(p_dbusrecv_hdl->dbusrecv_cfg.myname, myname, strlen(myname));
    p_dbusrecv_hdl->filter_func = filter_func;

    dbus_error_init (&p_dbusrecv_hdl->error);
    p_dbusrecv_hdl->connection = dbus_bus_get (DBUS_BUS_SYSTEM, &p_dbusrecv_hdl->error);
    if (NULL == p_dbusrecv_hdl->connection)
    {
        printf("%s: Failed to open connection to activating message bus: %s\n", __FILE__, p_dbusrecv_hdl->error.message);
        retval = -1;
        goto ERROR;
    }
    p_dbusrecv_hdl->dbusloop = _dbus_loop_new ();
    if (NULL == p_dbusrecv_hdl->dbusloop)
    {
        printf("%s: No memory, p_dbusrecv_hdl->dbusloop\n", __FILE__);
        retval = -1;
        goto ERROR;
    }
    if (!test_connection_setup (p_dbusrecv_hdl->dbusloop, p_dbusrecv_hdl->connection))
    {
        printf("%s: No memory, test_connection_setup\n", __FILE__);
        retval = -1;
        goto ERROR;
    }

    /*
        match options:
                type
                interface;
                member;
                sender;
                destination;
                path;
    */
#if 0
    memset(rules, 0x0, sizeof(rules));
    sprintf(rules, "type='signal', interface='%s'", p_dbusrecv_hdl->dbusrecv_cfg.interface);
    dbus_bus_add_match (p_dbusrecv_hdl->connection, rules, &p_dbusrecv_hdl->error);
    if (dbus_error_is_set (&p_dbusrecv_hdl->error))
    {
        printf("%s: add match(%s) failed\n", __FILE__, rules);
        retval = -1;
        goto ERROR;
    }
    memset(rules, 0x0, sizeof(rules));
    sprintf(rules, "type='signal', interface='org.actiontec.Test'");
    dbus_bus_add_match (p_dbusrecv_hdl->connection, rules, &p_dbusrecv_hdl->error);
    if (dbus_error_is_set (&p_dbusrecv_hdl->error))
    {
        printf("%s: add match(%s) failed\n", __FILE__, rules);
        retval = -1;
        goto ERROR;
    }
#endif
    if (!dbus_connection_add_filter(p_dbusrecv_hdl->connection, p_dbusrecv_hdl->filter_func, (void *)p_dbusrecv_hdl, NULL))
    {
        printf("%s: No memory, dbus_connection_add_filter\n", __FILE__);
        retval = -1;
        goto ERROR;
    }
    p_dbusrecv_hdl->is_addfilter = 1;

    if (!dbus_connection_register_object_path (p_dbusrecv_hdl->connection,
            DBUS_DEFAULT_PATH,
            &dbusrecv_vtable,
            (void *)p_dbusrecv_hdl))
        //(void*) 0xdeadbeef))
    {
        printf("%s: No memory, dbus_connection_register_object_path\n", __FILE__);
        retval = -1;
        goto ERROR;
    }

    {
        void *d;
        if (!dbus_connection_get_object_path_data (p_dbusrecv_hdl->connection, DBUS_DEFAULT_PATH, &d))
        {
            printf("%s: No memory, dbus_connection_get_object_path_data\n", __FILE__);
            retval = -1;
            goto ERROR;
        }
        //if (d != (void*) 0xdeadbeef)
        if (d != (void*) p_dbusrecv_hdl)
        {
            printf("%s: dbus_connection_get_object_path_data() doesn't seem to work right\n", __FILE__);
            retval = -1;
            goto ERROR;
        }
    }

    result = dbus_bus_request_name(p_dbusrecv_hdl->connection, myname, 0, &p_dbusrecv_hdl->error);
    if (dbus_error_is_set (&p_dbusrecv_hdl->error))
    {
        printf("%s: Failed to acquire service: %s\n", __FILE__, p_dbusrecv_hdl->error.message);
        retval = -1;
        goto ERROR;
    }
    //_dbus_verbose("%s: service(%s) entering main loop!\n", __FILE__, myname);
    //_dbus_loop_run(p_dbusrecv_hdl->dbusloop);
    retval = 0;

ERROR:
    if (-1 == retval)
    {
        dbus_error_free (&p_dbusrecv_hdl->error);
        if (p_dbusrecv_hdl->connection)
        {
            test_connection_shutdown (p_dbusrecv_hdl->dbusloop, p_dbusrecv_hdl->connection);
            if (p_dbusrecv_hdl->is_addfilter)
                dbus_connection_remove_filter (p_dbusrecv_hdl->connection, p_dbusrecv_hdl->filter_func, NULL);//
            dbus_connection_unref (p_dbusrecv_hdl->connection);
            p_dbusrecv_hdl->connection = NULL;
        }
        if (p_dbusrecv_hdl->dbusloop)
        {
            _dbus_loop_unref (p_dbusrecv_hdl->dbusloop);
            p_dbusrecv_hdl->dbusloop = NULL;
        }
        dbus_shutdown();
        _dbus_verbose("%s: service(%s) exiting!\n", __FILE__, myname);
        return NULL;
    }

    return p_dbusrecv_hdl;
}
Example #7
0
int
main (int    argc,
      char **argv)
{
  DBusError error;
  int result;
  DBusConnection *connection;
  
  dbus_error_init (&error);
  connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
  if (connection == NULL)
    {
      fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  loop = _dbus_loop_new ();
  if (loop == NULL)
    die ("No memory\n");
  
  if (!test_connection_setup (loop, connection))
    die ("No memory\n");

  if (!dbus_connection_add_filter (connection,
                                   filter_func, NULL, NULL))
    die ("No memory");

  if (!dbus_connection_register_object_path (connection,
                                             echo_path,
                                             &echo_vtable,
                                             (void*) 0xdeadbeef))
    die ("No memory");

  {
    void *d;
    if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
      die ("No memory");
    if (d != (void*) 0xdeadbeef)
      die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
  }
  
  result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
                                  0, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "Error %s\n", error.message);
      _dbus_verbose ("*** Failed to acquire service: %s\n",
                     error.message);
      dbus_error_free (&error);
      exit (1);
    }
  
  _dbus_verbose ("*** Test service entering main loop\n");
  _dbus_loop_run (loop);
  
  test_connection_shutdown (loop, connection);

  dbus_connection_remove_filter (connection, filter_func, NULL);
  
  dbus_connection_unref (connection);

  _dbus_loop_unref (loop);
  loop = NULL;
  
  dbus_shutdown ();

  _dbus_verbose ("*** Test service exiting\n");
  
  return 0;
}
int
main (int    argc,
      char **argv)
{
  DBusError error;
  int result;
  DBusConnection *connection;
  const char *name;
  dbus_bool_t do_fork;

  if (argc != 3)
    {
      name = "org.freedesktop.DBus.TestSuiteEchoService";
      do_fork = FALSE;
    }
  else
    {
      name = argv[1];
#ifndef DBUS_WIN
      do_fork = strcmp (argv[2], "fork") == 0;
#else
      do_fork = FALSE;
#endif
    }

  /* The bare minimum for simulating a program "daemonizing"; the intent
   * is to test services which move from being legacy init scripts to
   * activated services.
   * https://bugzilla.redhat.com/show_bug.cgi?id=545267
   */
#ifndef DBUS_WIN
   if (do_fork)
    {
      pid_t pid = fork ();
      if (pid != 0)
        exit (0);
      sleep (1);
    }
#endif

  dbus_error_init (&error);
  connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
  if (connection == NULL)
    {
      fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
               error.message);
      dbus_error_free (&error);
      return 1;
    }

  loop = _dbus_loop_new ();
  if (loop == NULL)
    die ("No memory\n");
  
  if (!test_connection_setup (loop, connection))
    die ("No memory\n");

  if (!dbus_connection_add_filter (connection,
                                   filter_func, NULL, NULL))
    die ("No memory");

  if (!dbus_connection_register_object_path (connection,
                                             echo_path,
                                             &echo_vtable,
                                             (void*) 0xdeadbeef))
    die ("No memory");

  {
    void *d;
    if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
      die ("No memory");
    if (d != (void*) 0xdeadbeef)
      die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
  }

  result = dbus_bus_request_name (connection, name,
                                  0, &error);
  if (dbus_error_is_set (&error))
    {
      fprintf (stderr, "Error %s\n", error.message);
      _dbus_verbose ("*** Failed to acquire service: %s\n",
                     error.message);
      dbus_error_free (&error);
      exit (1);
    }
  
  _dbus_verbose ("*** Test service entering main loop\n");
  _dbus_loop_run (loop);
  
  test_connection_shutdown (loop, connection);

  dbus_connection_remove_filter (connection, filter_func, NULL);
  
  dbus_connection_unref (connection);

  _dbus_loop_unref (loop);
  loop = NULL;
  
  dbus_shutdown ();

  _dbus_verbose ("*** Test service exiting\n");
  
  return 0;
}