/** * @fn : tc_libc_spawn_posix_spawn_file_actions_dump * @brief : Dump the file action whose address is passed * @scenario : Dump the file action whose address is passed * @API's covered : posix_spawn_file_actions_init, posix_spawn_file_actions_addopen, posix_spawn_file_actions_dump, posix_spawn_file_actions_destroy * @Preconditions : posix_spawn_file_actions_init, posix_spawn_file_actions_addopen * @Postconditions : posix_spawn_file_actions_destroy * @Return : void */ static void tc_libc_spawn_posix_spawn_file_actions_dump(void) { const char szfilepath[] = "./testdata.txt"; posix_spawn_file_actions_t st_fileactions; int ret_chk = ERROR; ret_chk = posix_spawn_file_actions_init(&st_fileactions); TC_ASSERT_EQ("posix_spawn_file_actions_init", ret_chk, OK); ret_chk = posix_spawn_file_actions_addopen(&st_fileactions, 1, szfilepath, O_WRONLY, 0644); TC_ASSERT_EQ("posix_spawn_file_actions_addopen", ret_chk, OK); /* posix_spawn_file_actions_dump returns (void) */ posix_spawn_file_actions_dump(&st_fileactions); ret_chk = posix_spawn_file_actions_destroy(&st_fileactions); TC_ASSERT_EQ("posix_spawn_file_actions_destroy", ret_chk, OK); TC_SUCCESS_RESULT(); }
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; }