Exemple #1
0
int
main (int   arg,
      char *argv[])
{
  GModule *module_self, *module_a, *module_b;
  gchar *string;
  gchar *plugin_a, *plugin_b;
  SimpleFunc f_a, f_b, f_self;
  GModuleFunc gmod_f;

  string = g_get_current_dir ();
  g_print ("testgmodule (%s):\n", string);

#if (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
  plugin_a = g_strconcat (string, "\\libgplugin_a.dll", NULL);
  plugin_b = g_strconcat (string, "\\libgplugin_b.dll", NULL);
#elif (G_MODULE_IMPL == G_MODULE_IMPL_DLD)
  plugin_a = g_strconcat (string, "/.libs/", "libgplugin_a.sl", NULL);
  plugin_b = g_strconcat (string, "/.libs/", "libgplugin_b.sl", NULL);
#else /* neither DLD nor WIN32 */
  plugin_a = g_strconcat (string, "/.libs/", "libgplugin_a.so", NULL);
  plugin_b = g_strconcat (string, "/.libs/", "libgplugin_b.so", NULL);
#endif
  g_free (string);

  /* module handles
   */
  g_print ("get main module handle\n");
  module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
  if (!module_self)
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("check that not yet bound symbols in shared libraries of main module are retrievable:\n");
  string = "g_module_close";
  g_print ("retrive symbol `%s' from \"%s\":\n", string, g_basename (g_module_name (module_self)));
  if (!g_module_symbol (module_self, string, (gpointer) &f_self))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("retrived symbol `%s' as %p\n", string, f_self);
  g_print ("load plugin from \"%s\"\n", plugin_a);
  module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
  if (!module_a)
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("load plugin from \"%s\"\n", plugin_b);
  module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
  if (!module_b)
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }

  /* get plugin specific symbols and call them
   */
  string = "gplugin_a_func";
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
  if (!g_module_symbol (module_a, string, (gpointer) &f_a))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  string = "gplugin_b_func";
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
  if (!g_module_symbol (module_b, string, (gpointer) &f_b))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("call plugin function(%p) A: ", f_a);
  f_a ();
  g_print ("call plugin function(%p) B: ", f_b);
  f_b ();

  /* get and call globally clashing functions
   */
  string = "g_clash_func";
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_self)));
  if (!g_module_symbol (module_self, string, (gpointer) &f_self))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
  if (!g_module_symbol (module_a, string, (gpointer) &f_a))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
  if (!g_module_symbol (module_b, string, (gpointer) &f_b))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("call plugin function(%p) self: ", f_self);
  f_self ();
  g_print ("call plugin function(%p) A: ", f_a);
  f_a ();
  g_print ("call plugin function(%p) B: ", f_b);
  f_b ();

  /* get and call clashing plugin functions
   */
  string = "gplugin_clash_func";
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_self)));
  if (!g_module_symbol (module_self, string, (gpointer) &f_self))
    f_self = NULL;
  g_print ("retrived function `%s' from self: %p\n", string, f_self);
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
  if (!g_module_symbol (module_a, string, (gpointer) &f_a))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_b)));
  if (!g_module_symbol (module_b, string, (gpointer) &f_b))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("call plugin function(%p) A: ", f_a);
  plugin_clash_func = f_a;
  plugin_clash_func ();
  g_print ("call plugin function(%p) B: ", f_b);
  plugin_clash_func = f_b;
  plugin_clash_func ();

  /* call gmodule function form A
   */
  string = "gplugin_a_module_func";
  g_print ("retrive symbol `%s' from \"%s\"\n", string, g_basename (g_module_name (module_a)));
  if (!g_module_symbol (module_a, string, (gpointer) &gmod_f))
    {
      g_print ("error: %s\n", g_module_error ());
      return 1;
    }
  g_print ("call plugin A's module function(%p):\n{\n", gmod_f);
  gmod_f (module_b);
  g_print ("}\n");

  
  /* unload plugins
   */
  g_print ("unload plugin A:\n");
  if (!g_module_close (module_a))
    g_print ("error: %s\n", g_module_error ());
  g_print ("unload plugin B:\n");
  if (!g_module_close (module_b))
    g_print ("error: %s\n", g_module_error ());

#if 0
  g_log_set_fatal_mask ("GModule", G_LOG_FATAL_MASK|G_LOG_LEVEL_WARNING);
  g_module_symbol (0, 0, 0);
  g_warning("jahooo");
  g_on_error_query (".libs/testgmodule");
#endif
  
  return 0;
}
int
main (int   arg,
      char *argv[])
{
  GModule *module_self, *module_a, *module_b;
  gchar *dir;
  gchar *plugin_a, *plugin_b;
  SimpleFunc f_a, f_b, f_self;
  GModuleFunc gmod_f;

  if (!g_module_supported ())
    return 0;

  dir = g_get_current_dir ();

  plugin_a = g_strconcat (dir, G_DIR_SEPARATOR_S "libmoduletestplugin_a", 
			  NULL);
  plugin_b = g_strconcat (dir, G_DIR_SEPARATOR_S "libmoduletestplugin_b", 
			  NULL);

  g_free (dir);

  /* module handles */
  
  module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
  if (!module_self)
    g_error ("error: %s", g_module_error ());

  if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
    g_error ("error: %s", g_module_error ());

  module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
  if (!module_a)
    g_error ("error: %s", g_module_error ());

  module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
  if (!module_b)
    g_error ("error: %s", g_module_error ());

  /* get plugin state vars */

  if (!g_module_symbol (module_a, "gplugin_a_state", 
			(gpointer *) &gplugin_a_state))
    g_error ("error: %s", g_module_error ());
  
  if (!g_module_symbol (module_b, "gplugin_b_state", 
			(gpointer *) &gplugin_b_state))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, "check-init");
  
  /* get plugin specific symbols and call them
   */
  if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  f_a ();
  test_states (NULL, "Hello world", NULL);
  
  f_b ();
  test_states (NULL, NULL, "Hello world");
  
  /* get and call globally clashing functions
   */
 
  if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  f_self ();
  test_states ("global clash", NULL, NULL);
  
  f_a ();
  test_states (NULL, "global clash", NULL);

  f_b ();
  test_states (NULL, NULL, "global clash");

  /* get and call clashing plugin functions  */

  if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  plugin_clash_func = f_a;
  plugin_clash_func ();
  test_states (NULL, "plugin clash", NULL);

  plugin_clash_func = f_b;
  plugin_clash_func ();
  test_states (NULL, NULL, "plugin clash");

  /* call gmodule function from A  */

  if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  gmod_f (module_b);
  test_states (NULL, NULL, "BOOH");
 
  gmod_f (module_a);
  test_states (NULL, "BOOH", NULL);

  /* unload plugins  */

  if (!g_module_close (module_a))
    g_error ("error: %s", g_module_error ());

  if (!g_module_close (module_b))
    g_error ("error: %s", g_module_error ());
 
  return 0;
}
Exemple #3
0
int
main (int    argc,
      char **argv)
{
  GModule *module_self, *module_a, *module_b;
  gchar *plugin_a, *plugin_b;
  SimpleFunc f_a, f_b, f_self;
  GModuleFunc gmod_f;

  g_test_init (&argc, &argv, NULL);

  if (!g_module_supported ())
    g_error ("dynamic modules not supported");

  plugin_a = g_test_build_filename (G_TEST_BUILT, "libmoduletestplugin_a", NULL);
  plugin_b = g_test_build_filename (G_TEST_BUILT, "libmoduletestplugin_b", NULL);

  /* module handles */
  
  module_self = g_module_open (NULL, G_MODULE_BIND_LAZY);
  if (!module_self)
    g_error ("error: %s", g_module_error ());

  if (!g_module_symbol (module_self, "g_module_close", (gpointer *) &f_self))
    g_error ("error: %s", g_module_error ());

  module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
  if (!module_a)
    g_error ("error: %s", g_module_error ());

  module_b = g_module_open (plugin_b, G_MODULE_BIND_LAZY);
  if (!module_b)
    g_error ("error: %s", g_module_error ());

  /* get plugin state vars */

  if (!g_module_symbol (module_a, "gplugin_a_state", 
			(gpointer *) &gplugin_a_state))
    g_error ("error: %s", g_module_error ());
  
  if (!g_module_symbol (module_b, "gplugin_b_state", 
			(gpointer *) &gplugin_b_state))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, "check-init");
  
  /* get plugin specific symbols and call them
   */
  if (!g_module_symbol (module_a, "gplugin_a_func", (gpointer *) &f_a))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  if (!g_module_symbol (module_b, "gplugin_b_func", (gpointer *) &f_b))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  f_a ();
  test_states (NULL, "Hello world", NULL);
  
  f_b ();
  test_states (NULL, NULL, "Hello world");
  
  /* get and call globally clashing functions
   */
 
  if (!g_module_symbol (module_self, "g_clash_func", (gpointer *) &f_self))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  if (!g_module_symbol (module_a, "g_clash_func", (gpointer *) &f_a))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  if (!g_module_symbol (module_b, "g_clash_func", (gpointer *) &f_b))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);
 
  f_self ();
  test_states ("global clash", NULL, NULL);
  
  f_a ();
  test_states (NULL, "global clash", NULL);

  f_b ();
  test_states (NULL, NULL, "global clash");

  /* get and call clashing plugin functions  */

  if (!g_module_symbol (module_a, "gplugin_clash_func", (gpointer *) &f_a))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  if (!g_module_symbol (module_b, "gplugin_clash_func", (gpointer *) &f_b))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  plugin_clash_func = f_a;
  plugin_clash_func ();
  test_states (NULL, "plugin clash", NULL);

  plugin_clash_func = f_b;
  plugin_clash_func ();
  test_states (NULL, NULL, "plugin clash");

  /* call gmodule function from A  */

  if (!g_module_symbol (module_a, "gplugin_a_module_func", (gpointer *) &gmod_f))
    g_error ("error: %s", g_module_error ());
  test_states (NULL, NULL, NULL);

  gmod_f (module_b);
  test_states (NULL, NULL, "BOOH");
 
  gmod_f (module_a);
  test_states (NULL, "BOOH", NULL);

  /* unload plugins  */

  if (!g_module_close (module_a))
    g_error ("error: %s", g_module_error ());

  if (!g_module_close (module_b))
    g_error ("error: %s", g_module_error ());

  g_free (plugin_a);
  g_free (plugin_b);
  g_module_close (module_self);
  return 0;
}