Пример #1
0
int elf_main(int argc, char *argv[])
{
  struct binary_s bin;
  int ret;
  int i;

  /* Initialize the memory monitor */

  mm_initmonitor();

  /* Initialize the ELF binary loader */

  message("Initializing the ELF binary loader\n");
  ret = elf_initialize();
  if (ret < 0)
    {
      err("ERROR: Initialization of the ELF loader failed: %d\n", ret);
      exit(1);
    }

  mm_update(&g_mmstep, "after elf_initialize");

  /* Create a ROM disk for the ROMFS filesystem */

  message("Registering romdisk at /dev/ram%d\n", CONFIG_EXAMPLES_ELF_DEVMINOR);
  ret = romdisk_register(CONFIG_EXAMPLES_ELF_DEVMINOR, (FAR uint8_t *)romfs_img,
                         NSECTORS(romfs_img_len), SECTORSIZE);
  if (ret < 0)
    {
      err("ERROR: romdisk_register failed: %d\n", ret);
      elf_uninitialize();
      exit(1);
    }

  mm_update(&g_mmstep, "after romdisk_register");

  /* Mount the file system */

  message("Mounting ROMFS filesystem at target=%s with source=%s\n",
         MOUNTPT, CONFIG_EXAMPLES_ELF_DEVPATH);

  ret = mount(CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, "romfs", MS_RDONLY, NULL);
  if (ret < 0)
    {
      err("ERROR: mount(%s,%s,romfs) failed: %s\n",
              CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, errno);
      elf_uninitialize();
    }

  mm_update(&g_mmstep, "after mount");

  /* Now excercise every program in the ROMFS file system */

  for (i = 0; dirlist[i]; i++)
    {
      testheader(dirlist[i]);

      memset(&bin, 0, sizeof(struct binary_s));
      snprintf(path, 128, "%s/%s", MOUNTPT, dirlist[i]);

      bin.filename = path;
      bin.exports  = exports;
      bin.nexports = nexports;

      ret = load_module(&bin);
      if (ret < 0)
        {
          err("ERROR: Failed to load program '%s'\n", dirlist[i]);
          exit(1);
        }

      mm_update(&g_mmstep, "after load_module");

      ret = exec_module(&bin, 50);

      mm_update(&g_mmstep, "after exec_module");

      if (ret < 0)
        {
          err("ERROR: Failed to execute program '%s'\n", dirlist[i]);
        }
      else
        {
          message("Wait a bit for test completion\n");
          sleep(4);
        }

      unload_module(&bin);
      mm_update(&g_mmstep, "after unload_module");
    }

  mm_update(&g_mmstep, "End-of-Test");
  return 0;
}
int elf_main(int argc, char *argv[])
#endif
{
  struct binary_s bin;
  int ret;
  int i;

  /* Initialize the memory monitor */

  mm_initmonitor();

  /* Initialize the ELF binary loader */

  message("Initializing the ELF binary loader\n");
  ret = elf_initialize();
  if (ret < 0)
    {
      err("ERROR: Initialization of the ELF loader failed: %d\n", ret);
      exit(1);
    }

  mm_update(&g_mmstep, "after elf_initialize");

  /* Create a ROM disk for the ROMFS filesystem */

  message("Registering romdisk at /dev/ram%d\n", CONFIG_EXAMPLES_ELF_DEVMINOR);
  ret = romdisk_register(CONFIG_EXAMPLES_ELF_DEVMINOR, (FAR uint8_t *)romfs_img,
                         NSECTORS(romfs_img_len), SECTORSIZE);
  if (ret < 0)
    {
      err("ERROR: romdisk_register failed: %d\n", ret);
      elf_uninitialize();
      exit(1);
    }

  mm_update(&g_mmstep, "after romdisk_register");

  /* Mount the file system */

  message("Mounting ROMFS filesystem at target=%s with source=%s\n",
         MOUNTPT, CONFIG_EXAMPLES_ELF_DEVPATH);

  ret = mount(CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, "romfs", MS_RDONLY, NULL);
  if (ret < 0)
    {
      err("ERROR: mount(%s,%s,romfs) failed: %s\n",
              CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, errno);
      elf_uninitialize();
    }

  mm_update(&g_mmstep, "after mount");

  /* Does the system support the PATH variable?  Has the PATH variable
   * already been set?  If YES and NO, then set the PATH variable to
   * the ROMFS mountpoint.
   */

#if defined(CONFIG_BINFMT_EXEPATH) && !defined(CONFIG_PATH_INITIAL)
  (void)setenv("PATH", MOUNTPT, 1);
#endif

  /* Now excercise every program in the ROMFS file system */

  for (i = 0; dirlist[i]; i++)
    {
      /* Output a seperated so that we can clearly discrinmate the output of
       * this program from the others.
       */

      testheader(dirlist[i]);

      /* Initialize the binary_s structure */

      memset(&bin, 0, sizeof(struct binary_s));

      /* If the binary loader does not support the PATH variable, then
       * create the full path to the executable program.  Otherwise,
       * use the relative path so that the binary loader will have to
       * search the PATH variable to find the executable.
       */

#ifdef CONFIG_BINFMT_EXEPATH
      bin.filename = dirlist[i];
#else
      snprintf(fullpath, 128, "%s/%s", MOUNTPT, dirlist[i]);
      bin.filename = fullpath;
#endif
      bin.exports  = exports;
      bin.nexports = nexports;

      /* Load the ELF module */

      ret = load_module(&bin);
      if (ret < 0)
        {
          err("ERROR: Failed to load program '%s'\n", dirlist[i]);
          exit(1);
        }

      mm_update(&g_mmstep, "after load_module");

      /* Execute the ELF module */

      ret = exec_module(&bin);

      mm_update(&g_mmstep, "after exec_module");

      if (ret < 0)
        {
          err("ERROR: Failed to execute program '%s'\n", dirlist[i]);
        }
      else
        {
          message("Wait a bit for test completion\n");
          sleep(4);
        }

      unload_module(&bin);
      mm_update(&g_mmstep, "after unload_module");
    }

  mm_update(&g_mmstep, "End-of-Test");
  return 0;
}
Пример #3
0
int spawn_main(int argc, char *argv[])
{
  posix_spawn_file_actions_t file_actions;
  posix_spawnattr_t attr;
  FAR const char *filepath;
  pid_t pid;
  int ret;

  /* Initialize the memory monitor */

  mm_initmonitor();

  /* Initialize the ELF binary loader */

  message("Initializing the ELF binary loader\n");
  ret = elf_initialize();
  if (ret < 0)
    {
      err("ERROR: Initialization of the ELF loader failed: %d\n", ret);
      exit(1);
    }

  mm_update(&g_mmstep, "after elf_initialize");

  /* Create a ROM disk for the ROMFS filesystem */

  message("Registering romdisk at /dev/ram%d\n", CONFIG_EXAMPLES_ELF_DEVMINOR);
  ret = romdisk_register(CONFIG_EXAMPLES_ELF_DEVMINOR, (FAR uint8_t *)romfs_img,
                         NSECTORS(romfs_img_len), SECTORSIZE);
  if (ret < 0)
    {
      err("ERROR: romdisk_register failed: %d\n", ret);
      elf_uninitialize();
      exit(1);
    }

  mm_update(&g_mmstep, "after romdisk_register");

  /* Mount the file system */

  message("Mounting ROMFS filesystem at target=%s with source=%s\n",
         MOUNTPT, CONFIG_EXAMPLES_ELF_DEVPATH);

  ret = mount(CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, "romfs", MS_RDONLY, NULL);
  if (ret < 0)
    {
      err("ERROR: mount(%s,%s,romfs) failed: %s\n",
          CONFIG_EXAMPLES_ELF_DEVPATH, MOUNTPT, errno);
      elf_uninitialize();
    }

  mm_update(&g_mmstep, "after mount");

  /* Does the system support the PATH variable?  Has the PATH variable
   * already been set?  If YES and NO, then set the PATH variable to
   * the ROMFS mountpoint.
   */

#if defined(CONFIG_BINFMT_EXEPATH) && !defined(CONFIG_PATH_INITIAL)
  (void)setenv("PATH", MOUNTPT, 1);
#endif

  /* Make sure that we are using our symbol take */

  exec_setsymtab(exports, nexports);

  /*************************************************************************
   * Case 1: Simple program with arguments
   *************************************************************************/

  /* Output a seperated so that we can clearly discriminate the output of
   * this program from the others.
   */

  testheader(g_hello);

  /* Initialize the attributes file actions structure */

  ret = posix_spawn_file_actions_init(&file_actions);
  if (ret != 0)
    {
      err("ERROR: posix_spawn_file_actions_init failed: %d\n", ret);
    }
  posix_spawn_file_actions_dump(&file_actions);

  ret = posix_spawnattr_init(&attr);
  if (ret != 0)
    {
      err("ERROR: posix_spawnattr_init failed: %d\n", ret);
    }
  posix_spawnattr_dump(&attr);

  mm_update(&g_mmstep, "after file_action/attr init");

  /* If the binary loader does not support the PATH variable, then
   * create the full path to the executable program.  Otherwise,
   * use the relative path so that the binary loader will have to
   * search the PATH variable to find the executable.
   */

#ifdef CONFIG_BINFMT_EXEPATH
  filepath = g_hello;
#else
  snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_hello);
  filepath = fullpath;
#endif

  /* Execute the program */

  mm_update(&g_mmstep, "before posix_spawn");

  ret = posix_spawn(&pid, filepath, &file_actions, &attr, NULL, (FAR char * const*)&g_argv);
  if (ret != 0)
    {
      err("ERROR: posix_spawn failed: %d\n", ret);
    }

  sleep(4);
  mm_update(&g_mmstep, "after posix_spawn");

  /* Free attibutes and file actions */

  ret = posix_spawn_file_actions_destroy(&file_actions);
  if (ret != 0)
    {
      err("ERROR: posix_spawn_file_actions_destroy failed: %d\n", ret);
    }
  posix_spawn_file_actions_dump(&file_actions);

  ret = posix_spawnattr_destroy(&attr);
  if (ret != 0)
    {
      err("ERROR: posix_spawnattr_destroy failed: %d\n", ret);
    }
  posix_spawnattr_dump(&attr);

  mm_update(&g_mmstep, "after file_action/attr destruction");

  /*************************************************************************
   * Case 2: Simple program with redirection of stdin to a file input
   *************************************************************************/

  /* Output a seperated so that we can clearly discriminate the output of
   * this program from the others.
   */

  testheader(g_redirect);

  /* Initialize the attributes file actions structure */

  ret = posix_spawn_file_actions_init(&file_actions);
  if (ret != 0)
    {
      err("ERROR: posix_spawn_file_actions_init failed: %d\n", ret);
    }
  posix_spawn_file_actions_dump(&file_actions);

  ret = posix_spawnattr_init(&attr);
  if (ret != 0)
    {
      err("ERROR: posix_spawnattr_init failed: %d\n", ret);
    }
  posix_spawnattr_dump(&attr);

  mm_update(&g_mmstep, "after file_action/attr init");

  /* Set up to close stdin (0) and open testdata.txt as the program input */

  ret = posix_spawn_file_actions_addclose(&file_actions, 0);
  if (ret != 0)
    {
      err("ERROR: posix_spawn_file_actions_addclose failed: %d\n", ret);
    }
  posix_spawn_file_actions_dump(&file_actions);

  snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_data);
  ret = posix_spawn_file_actions_addopen(&file_actions, 0, fullpath, O_RDONLY, 0644);
  if (ret != 0)
    {
      err("ERROR: posix_spawn_file_actions_addopen failed: %d\n", ret);
    }
  posix_spawn_file_actions_dump(&file_actions);
  
  mm_update(&g_mmstep, "after adding file_actions");

  /* If the binary loader does not support the PATH variable, then
   * create the full path to the executable program.  Otherwise,
   * use the relative path so that the binary loader will have to
   * search the PATH variable to find the executable.
   */

#ifdef CONFIG_BINFMT_EXEPATH
  filepath = g_redirect;
#else
  snprintf(fullpath, 128, "%s/%s", MOUNTPT, g_redirect);
  filepath = fullpath;
#endif

  /* Execute the program */

  mm_update(&g_mmstep, "before posix_spawn");

  ret = posix_spawn(&pid, filepath, &file_actions, &attr, NULL, NULL);
  if (ret != 0)
    {
      err("ERROR: posix_spawn failed: %d\n", ret);
    }

  sleep(2);
  mm_update(&g_mmstep, "after posix_spawn");

  /* Free attibutes and file actions */

  ret = posix_spawn_file_actions_destroy(&file_actions);
  if (ret != 0)
    {
      err("ERROR: posix_spawn_file_actions_destroy failed: %d\n", ret);
    }
  posix_spawn_file_actions_dump(&file_actions);

  ret = posix_spawnattr_destroy(&attr);
  if (ret != 0)
    {
      err("ERROR: posix_spawnattr_destroy failed: %d\n", ret);
    }
  posix_spawnattr_dump(&attr);

  mm_update(&g_mmstep, "after file_action/attr destruction");

  /* Clean-up */

  elf_uninitialize();

  mm_update(&g_mmstep, "End-of-Test");
  return 0;
}