static void test_setenv (const ACE_TCHAR *argv0) { ACE_Process_Options options; // options.setenv ("ACE_PROCESS_TEST", "here's a really large number: %u", 0 - 1); options.setenv (ACE_TEXT ("ACE_PROCESS_TEST= here's a large number %u"), 0 - 1); options.setenv (ACE_TEXT ("ACE_PROCESS_TEST2"), ACE_TEXT ("ophilli")); #if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR) options.command_line ("%s -g", argv0); #else options.command_line ("%ls -g", argv0); #endif ACE_Process process; if (process.spawn (options) == -1) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p.\n"), ACE_TEXT ("test_setenv"))); return; } ACE_exitcode status; process.wait (&status); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Process exit with status %d\n"), status)); }
void run_parent (bool inherit_files) { ACE_TCHAR t[] = ACE_TEXT ("ace_testXXXXXX"); // Create tempfile. This will be tested for inheritance. ACE_TCHAR tempfile[MAXPATHLEN + 1]; if (ACE::get_temp_dir (tempfile, MAXPATHLEN - sizeof (t)) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("Could not get temp dir\n"))); ACE_OS::strcat (tempfile, t); ACE_HANDLE file_handle = ACE_OS::mkstemp (tempfile); if (file_handle == ACE_INVALID_HANDLE) ACE_ERROR ((LM_ERROR, ACE_TEXT ("Could not get temp filename\n"))); // Build child options ACE_TString exe_sub_dir; const char *subdir_env = ACE_OS::getenv ("ACE_EXE_SUB_DIR"); if (subdir_env) { exe_sub_dir = ACE_TEXT_CHAR_TO_TCHAR (subdir_env); exe_sub_dir += ACE_DIRECTORY_SEPARATOR_STR; } ACE_Process_Options options; options.command_line (ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR ACE_TEXT ("%sProcess_Test") ACE_PLATFORM_EXE_SUFFIX ACE_TEXT (" -c -h %d -f %s"), exe_sub_dir.c_str(), (int)inherit_files, tempfile); options.handle_inheritance (inherit_files); /* ! */ // Spawn child ACE_Process child; pid_t result = child.spawn (options); if (result == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("Parent could NOT spawn child process\n"))); else ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Parent spawned child process with pid = %d.\n"), child.getpid ())); ACE_exitcode child_status; result = child.wait (&child_status); if (result == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("Could NOT wait on child process\n"))); else if (child_status == 0) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Child %d finished ok\n"), child.getpid ())); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("Child %d finished with status %d\n"), child.getpid (), child_status)); }
static double prof_ace_process (size_t iteration) { if (iteration != 0) { ACE_Process_Options popt; ACE_Process aProcess; popt.command_line (SUBPROGRAM); iteration *= MULTIPLY_FACTOR; if (do_exec_after_fork == 0) popt.creation_flags (ACE_Process_Options::NO_EXEC); ACE_Profile_Timer ptimer; ACE_Profile_Timer::ACE_Elapsed_Time et; double time = 0; pid_t result; for (size_t c = 0; c < iteration; c++) { ACE_STOP_SIGN; ptimer.start (); result = aProcess.spawn (popt); ptimer.stop (); if (result == -1) ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "process.spawn"), -1); else if (do_exec_after_fork == 0 && result == 0) ACE_OS::exit (0) ; else { ptimer.elapsed_time (et); time += et.real_time; } } return time / iteration; } else return -1.0; }
int Handle_Events::serve (char *buf) { ACE_ARGV arguments (buf); if (ACE_OS::strcmp (arguments[0], TESTER) == 0) { ACE_Process_Options po; ACE_Process p; po.set_handles (ACE_INVALID_HANDLE, OUTPUT_FILE, OUTPUT_FILE); po.command_line (arguments.argv ()); p.spawn (po); return 0; } else return -1; }
int main(int argc, char *argv[]) { ACE_Process_Options options; FILE *fp = 0; char *n_env = 0; int n; if (argc == 1) { n_env = ACE_OS::getenv("FACTORIAL"); n = n_env == 0 ? 10 : atoi(n_env); options.command_line("%s %d", argv[0], n - 1); const char *working_dir = ACE_OS::getenv("WORKING_DIR"); if (working_dir) options.working_directory(working_dir); fp = fopen("factorial.log", "a"); cout << "before setenv" << endl; options.setenv("PROGRAM=%s", ACE::basename(argv[0])); cout << "after setenv" << endl; } else { fp = fopen("factorial.log", "a"); if (atoi(argv[1]) == 1) { fprintf(fp, "[%s|%d]: base case\n", ACE_OS::getenv("PROGRAM"), ACE_OS::getpid()); fclose(fp); return 1; } else { n = atoi(argv[1]); options.command_line("%s %d", argv[0], n - 1); } } ACE_Process child; child.spawn(options); child.wait(); int factorial = n * child.return_value(); fprintf(fp, "[%s|%d]: %d! == %d\n", ACE_OS::getenv("PROGRAM"), ACE_OS::getpid(), n, factorial); fclose(fp); return factorial; }
int main(int argc, char *argv[]) { ACE_Process_Options options; char *n_env = 0; int n; if (argc == 1) { n_env = ACE_OS::getenv("FACTORIAL"); n = n_env == 0 ? 10 : atoi(n_env); options.command_line("%s %d", argv[0], n - 1); } else if (atoi(argv[1]) == 1) { return 1; } else { n = atoi(argv[1]); options.command_line("%s %d", argv[0], n - 1); } ACE_Process child; child.spawn(options); child.wait(); return n * child.return_value(); }
int run_main (int, ACE_TCHAR *[]) { ACE_START_TEST (ACE_TEXT ("Time_Service_Test")); // Make sure that the backing store is not there. We need to make // sure because this test kills the Time Clerk and on some platforms // the Clerk is not allowed to do a graceful shutdown. By cleaning // the backing store here, we are sure that we get a fresh start and // no garbage data from a possible aborted run ACE_TCHAR backing_store[MAXPATHLEN + 1]; #if defined (ACE_DEFAULT_BACKING_STORE) // Create a temporary file. ACE_OS::strcpy (backing_store, ACE_DEFAULT_BACKING_STORE); #else /* ACE_DEFAULT_BACKING_STORE */ if (ACE::get_temp_dir (backing_store, MAXPATHLEN - 17) == -1) // -17 for ace-malloc-XXXXXX { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Temporary path too long, ") ACE_TEXT ("defaulting to current directory\n"))); backing_store[0] = 0; } // Add the filename to the end ACE_OS::strcat (backing_store, ACE_TEXT ("ace-malloc-XXXXXX")); #endif /* ACE_DEFAULT_BACKING_STORE */ ACE_OS::unlink (backing_store); const ACE_TCHAR *server_cl = APPLICATION ACE_TEXT ("server.conf"); ACE_Process_Options server_options; server_options.command_line (server_cl); ACE_Process server; if (server.spawn (server_options) == -1) ACE_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("%n; %p (%s).\n"), ACE_TEXT ("Server fork failed"), server_cl), -1); else ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Server forked with pid = %d.\n"), server.getpid ())); ACE_OS::sleep (3); const ACE_TCHAR *clerk_cl = APPLICATION ACE_TEXT ("clerk.conf"); ACE_Process_Options clerk_options; clerk_options.command_line (clerk_cl); ACE_Process clerk; if (clerk.spawn (clerk_options) == -1) ACE_ERROR_RETURN ((LM_DEBUG, ACE_TEXT ("%n; %p: (%s).\n"), ACE_TEXT ("Clerk fork failed"), clerk_cl), -1); else ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Clerk forked with pid = %d.\n"), clerk.getpid ())); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Sleeping...\n"))); ACE_OS::sleep (10); if (clerk.terminate () == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Terminate failed for clerk.\n")), -1); if (server.terminate () == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Terminate failed for server.\n")), -1); // Because we kill the clerk process, on Win32 it may not do a // graceful shutdown and the backing store file is left behind. if (clerk.wait () != 0) ACE_OS::unlink (backing_store); ACE_END_TEST; return 0; }
int ACE_TMAIN (int argc, ACE_TCHAR *argv[]) { if (ACE_LOG_MSG->open (argv[0]) == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("cannot open logger!!!\n"))); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("starting...\n"))); if (::parse_args (argc, argv) == -1) return -1; if (run_all) { #if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR) const ACE_TCHAR *cmdline = ACE_TEXT ("%s -d -l -s -w"); #else const ACE_TCHAR *cmdline = ACE_TEXT ("%ls -d -l -s -w"); #endif ACE_Process_Options options; options.command_line (cmdline, argv[0]); ACE_Process process; if (process.spawn (options) == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p.\n"), ACE_TEXT ("main")), -1); ACE_exitcode status; process.wait (&status); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Process exit with status %d\n"), status)); } if (run_date) ::test_date (); if (run_setenv) ::test_setenv (argv[0]); if (get_env) { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("checking ACE_PROCESS_TEST\n"))); char *value = ACE_OS::getenv ("ACE_PROCESS_TEST"); char *value2 = ACE_OS::getenv ("ACE_PROCESS_TEST2"); ACE_DEBUG ((LM_DEBUG, "ACE_PROCESS_TEST = %C.\n" "ACE_PROCESS_TEST2 = %C.\n", value == 0 ? "no value" : value, value2 == 0 ? "no value" : value2)); } if (run_ls) ::test_ls (); if (run_wait) ::test_wait (); #if defined (ACE_WIN32) ACE_UNUSED_ARG (&win32_test_ls); if (environment_string != 0) win32_spawn_environment_process (); #endif /* ACE_WIN32 */ if (print_file != 0) test_more (); ACE_TCHAR buf1[30]; ACE_TCHAR buf2[30]; ACE_OS::strcpy(buf1, ACE_TEXT (" -f hi honey -g \"I\'m home\"")); ACE_OS::strcpy(buf2, ACE_TEXT ("\"token 1\"\'token 2\'\"token 3\" ")); if (run_tokenizer) { tokenize (buf1); tokenize (buf2); } return 0; }
int run_main (int argc, ACE_TCHAR *argv[]) { #if defined (ACE_WIN32) get_base_addrs(); #endif if (argc == 1) { ACE_START_TEST (ACE_TEXT ("Malloc_Test")); ACE_INIT_LOG (ACE_TEXT ("Malloc_Test-child")); init_test (PARENT_BASE_ADDR); ACE_Control_Block::print_alignment_info (); # if (ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1) ACE_PI_Control_Block::print_alignment_info (); # endif /* ACE_HAS_POSITION_INDEPENDENT_POINTERS == 1 */ // No arguments means we're the parent process. ACE_Process_Options options (1); #if !defined (ACE_WIN32) && defined (ACE_USES_WCHAR) static const ACE_TCHAR* format = ACE_TEXT ("%ls%ls%ls"); #else static const ACE_TCHAR* format = ACE_TEXT ("%s%s%s"); #endif /* !ACE_WIN32 && ACE_USES_WCHAR */ options.command_line (format, EXE_LOCATION, argc > 0 ? argv[0] : ACE_TEXT ("Malloc_Test"), ACE_TEXT (" run_as_test")); MALLOC *myalloc = myallocator (PARENT_BASE_ADDR); Test_Data *data = initialize (myalloc); ACE_TEST_ASSERT (data != 0); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P) PARENT allocator at = %@, ") ACE_TEXT ("data allocated at %@\n"), myalloc, data)); myalloc->dump (); int result = myalloc->bind ("foo", data); ACE_TEST_ASSERT (result != -1); ACE_Process p; pid_t pid = p.spawn (options); if (pid == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn")), 1); parent (data); // Synchronize on the exit of the child. result = p.wait (); if (result == -1) ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("wait")), 1); ACE_TEST_ASSERT (myalloc->ref_counter () == 1); myalloc->remove (); ACE_END_TEST; return 0; } else { // In this case we're the child process. ACE_APPEND_LOG (ACE_TEXT ("Malloc_Test-child")); void *data = 0; MALLOC *myalloc = myallocator (CHILD_BASE_ADDR); int result = myalloc->find ("foo", data); ACE_TEST_ASSERT (result != -1); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P) CHILD allocator at = %@, ") ACE_TEXT ("data allocated at %@\n"), myalloc, data)); myalloc->dump (); child (); myalloc->release (); ACE_END_LOG; return 0; } }
int // This has been unconditionally turned on for the time being since I can't // figure out an easy way to enable it and still keep ACE_TMAIN in a seperate // cpp. #if 1 || defined (ACE_HAS_NONSTATIC_OBJECT_MANAGER) || defined (ACE_LACKS_FORK) // ACE_HAS_NONSTATIC_OBJECT_MANAGER only allows main to have two // arguments. And on platforms that lack fork (), we can't use spawn. run_main (int argc, ACE_TCHAR* []) { ACE_UNUSED_ARG (argc); // Only Win32 can set wide-char environment strings. So, for all // others, use char string literals regardless of ACE_USES_WCHAR. # if defined (ACE_WIN32) ACE_OS::putenv (ACE_TEXT ("TEST_VALUE_POSITIVE=10.2")); ACE_OS::putenv (ACE_TEXT ("TEST_VALUE_NEGATIVE=-10.2")); # else ACE_OS::putenv ("TEST_VALUE_POSITIVE=10.2"); ACE_OS::putenv ("TEST_VALUE_NEGATIVE=-10.2"); # endif /* ACE_WIN32 */ #else /* ! ACE_HAS_NONSTATIC_OBJECT_MANAGER && ! ACE_LACKS_FORK */ run_main (int argc, ACE_TCHAR * [], ACE_TCHAR *envp[]) { if (argc == 1) { int status; // No arguments means we're the initial test. ACE_Process_Options options (1); status = options.setenv (envp); if (status != 0) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("setenv(envp)"))); options.command_line (ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR ACE_TEXT ("Env_Value_Test run_as_test")); status = options.setenv (ACE_TEXT ("TEST_VALUE_POSITIVE"), ACE_TEXT ("%s"), ACE_TEXT ("10.2")); if (status != 0) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("setenv(TEST_VALUE_POSITIVE)"))); status = options.setenv (ACE_TEXT ("TEST_VALUE_NEGATIVE"), ACE_TEXT ("%s"), ACE_TEXT ("-10.2")); if (status != 0) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("setenv(TEST_VALUE_NEGATIVE)"))); ACE_Process p; pid_t result = p.spawn (options); if (result == -1) ACE_ERROR ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn"))); else p.wait (); } else #endif /* ! ACE_HAS_NONSTATIC_OBJECT_MANAGER && ! ACE_LACKS_FORK */ { // In this case we're the child ACE_START_TEST (ACE_TEXT ("Env_Value_Test")); TEST_THIS (int, ACE_TEXT ("TEST_VALUE_POSITIVE"), 4, 10); TEST_THIS (double, ACE_TEXT ("TEST_VALUE_POSITIVE"), -1.0, 10.2); TEST_THIS (long, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (unsigned long, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (short, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (unsigned short, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (int, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 4, -10); TEST_THIS (double, ACE_TEXT ("TEST_VALUE_NEGATIVE"), -1.0, -10.2); TEST_THIS (long, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, -10L); TEST_THIS (unsigned long, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, (unsigned long) -10); TEST_THIS (short, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, -10); TEST_THIS (unsigned short, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, (unsigned short) -10); const ACE_TCHAR *defstr = ACE_TEXT ("Sarah Cleeland is Two!"); ACE_Env_Value<const ACE_TCHAR *> sval (ACE_TEXT ("This_Shouldnt_Be_Set_Hopefully"), defstr); if (ACE_OS::strcmp (sval, defstr) != 0) ACE_ERROR ((LM_ERROR, ACE_TEXT ("Mismatch: %s should be %s\n"), (const ACE_TCHAR *)sval, defstr)); ACE_END_TEST; } return 0; }
// This function runs the parent process in a separate worker thread. static ACE_THR_FUNC_RETURN worker_parent (void *arg) { long handle_signals_synchronously = reinterpret_cast <long> (arg); ACE_Process_Options options; ACE_TCHAR *l_argv[3]; ACE_TCHAR pid_str[100]; // Store the parent's process id so we can pass it to the child // portably. Also, pass the test number, as well. ACE_OS::sprintf (pid_str, ACE_TEXT ("-p %ld -t %d"), static_cast <long> (parent_pid), test_number); // We're going to create a new process that runs this program again, // so we need to indicate that it's the child. const ACE_TCHAR *t = ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR ACE_TEXT ("%sSignal_Test") ACE_PLATFORM_EXE_SUFFIX ACE_TEXT (" -c"); l_argv[0] = const_cast <ACE_TCHAR *> (t); l_argv[1] = pid_str; l_argv[2] = 0; ACE_ARGV argv (l_argv); // Generate a command-line! ACE_TString exe_sub_dir; const char *subdir_env = ACE_OS::getenv ("ACE_EXE_SUB_DIR"); if (subdir_env) { exe_sub_dir = ACE_TEXT_CHAR_TO_TCHAR (subdir_env); exe_sub_dir += ACE_DIRECTORY_SEPARATOR_STR; } options.command_line (argv.buf (), exe_sub_dir.c_str ()); ACE_Process pm; child_pid = pm.spawn (options); if (child_pid == ACE_INVALID_PID) ACE_ERROR_RETURN ((LM_ERROR, "(%P|%t) spawning child process failed\n"), 0); else ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) spawning child process %d\n"), child_pid)); // Perform a <wait> until our child process has exited. if (handle_signals_synchronously) { int status; // Wait for the child process to exit. pm.wait (&status); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) reaped child with status %d\n"), status)); } else while (shut_down == 0) { // Wait for a signal to arrive. if (ACE_OS::sigsuspend (0) == -1 && errno != EINTR) ACE_ERROR ((LM_ERROR, ACE_TEXT ("(%P|%t) %p\n"), ACE_TEXT ("sigsuspend"))); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) got signal!\n"))); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("(%P|%t) parent worker done\n"))); return 0; }
void Obj_Module::add_source(const char *p, int imports_only) { ACE_Process nmproc; ACE_Process_Options nm_opts; ACE_CString path (p); ACE_CString::size_type pathsep = path.rfind('/'); ACE_CString src_name; ACE_CString workpath; if (pathsep == ACE_CString::npos) { src_name = path; workpath = "."; } else { src_name = path.substr(pathsep+1); workpath= path.substr(0,pathsep); } ACE_HANDLE pipe[2]; ACE_Pipe io(pipe); nm_opts.working_directory (workpath.c_str()); nm_opts.set_handles (ACE_STDIN,pipe[1]); // Options for the command line shown here are for the GNU nm 2.9.5 int result = nm_opts.command_line ("nm -C %s",src_name.c_str()); // Prevent compiler warning about "unused variable" if ACE_ASSERT is // an empty macro. ACE_UNUSED_ARG (result); ACE_ASSERT (result == 0); nmproc.spawn (nm_opts); if (ACE_OS::close(pipe[1]) == -1) ACE_DEBUG ((LM_DEBUG, "%p\n", "close")); nm_opts.release_handles(); int import_lines = 0; int export_lines = 0; ACE_Message_Block im_buffer (102400); ACE_Message_Block ex_buffer (102400); ACE_Message_Block *im_buf_cur = &im_buffer; ACE_Message_Block *ex_buf_cur = &ex_buffer; char dummy; int eoln = 1; // ACE_Time_Value timeout (1,0); int is_import = 1; int is_export = 1; while (eoln == 1) { for (int i = 0; i < 10; i++) { if (ACE_OS::read(pipe[0],&dummy,1) != 1) { eoln = 2; break; } } if (eoln == 2) break; is_import = dummy == 'U'; is_export = !imports_only && (ACE_OS::strchr("BCDRTVW",dummy) != 0); // if (ACE::recv(pipe[0],&dummy,1,&timeout) != 1) if (ACE_OS::read(pipe[0],&dummy,1) != 1) break; eoln = this->read_line (pipe[0], is_import ? &im_buf_cur : (is_export ? &ex_buf_cur : 0)); import_lines += is_import; export_lines += is_export; } // ACE_DEBUG ((LM_DEBUG, "read %d import lines and %d export lines\n", // import_lines, export_lines)); nmproc.wait (); ACE_OS::close (pipe[0]); this->populate_sig_list (imports_,import_lines,&im_buffer); if (!imports_only) this->populate_sig_list (exports_,export_lines,&ex_buffer); }
int run_main (int argc, ACE_TCHAR *argv[]) { parse_args (argc, argv); if (child_process) { ACE_APPEND_LOG (ACE_TEXT("Pipe_Test-children")); ACE_Pipe a, b, c, d, e; open_pipe (a, "a"); open_pipe (b, "b"); open_pipe (c, "c"); open_pipe (d, "d"); open_pipe (e, "e"); ACE_END_LOG; } else { ACE_START_TEST (ACE_TEXT("Pipe_Test")); ACE_INIT_LOG (ACE_TEXT("Pipe_Test-children")); # if defined (ACE_WIN32) || !defined (ACE_USES_WCHAR) const ACE_TCHAR *cmdline_fmt = ACE_TEXT ("%s -c%s"); # else const ACE_TCHAR *cmdline_fmt = ACE_TEXT ("%ls -c%ls"); # endif /* ACE_WIN32 || !ACE_USES_WCHAR */ ACE_Process_Options options; options.command_line (cmdline_fmt, argc > 0 ? argv[0] : ACE_TEXT ("Pipe_Test"), close_pipe == 0 ? ACE_TEXT (" -d") : ACE_TEXT ("")); ACE_exitcode status = 0; for (int i = 0; i < ::iterations; i++) { ACE_Process server; if (server.spawn (options) == -1) { ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("%p\n"), ACE_TEXT ("spawn failed")), -1); } else { ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Server forked with pid = %d.\n"), server.getpid ())); } // Wait for the process we just created to exit. server.wait (&status); // Check if child exited without error. if (WIFEXITED (status) != 0 && WEXITSTATUS (status) != 0) { ACE_ERROR ((LM_ERROR, ACE_TEXT ("Child of server %d finished with error ") ACE_TEXT ("exit status %d\n"), server.getpid (), WEXITSTATUS (status))); ACE_END_TEST; ACE_OS::exit (WEXITSTATUS (status)); } ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Server %d finished\n"), server.getpid ())); } ACE_END_TEST; } return 0; }
void SO_Group::add_executable (const char * path) { ACE_Process proc; ACE_Process_Options opts; ACE_HANDLE pipe[2]; ACE_Pipe io(pipe); opts.set_handles (ACE_STDIN,pipe[1]); int result = opts.command_line ("ldd %s",path); // Prevent compiler warning about "unused variable" if ACE_ASSERT is // an empty macro. ACE_UNUSED_ARG (result); ACE_ASSERT (result == 0); proc.spawn (opts); if (ACE_OS::close(pipe[1]) == -1) ACE_DEBUG ((LM_DEBUG, "%p\n", "close")); opts.release_handles(); const int max_line_length = 1000; char line[max_line_length]; while (1) { ACE_OS::memset (line,0,max_line_length); int len = 0; int nread = 0; int bogus = 0; // skip initial whitespace while ((nread = ACE_OS::read(pipe[0],line,1)) == 1 && (*line == ' ' || *line == '\t')); if (nread != 1) break; // read the library name len = 1; while ((nread = ACE_OS::read(pipe[0],line + len,1)) == 1 && (line[len] != ' ')) if (! bogus && ++len == max_line_length) { bogus = 1; break; } if (nread != 1 || bogus) break; line[len] = 0; char * dot = ACE_OS::strchr (line,'.'); if (dot) *dot = 0; char * libname = line + 3; // skip over "lib" // check to see if this is a new library int found = 0; for (int i = 0; !found && i < num_libs_; i++) found = (libs_[i]->name() == libname); if (!found) { Library *nlib = new Library(libname); ACE_OS::memset (line,0,max_line_length); // skip over '=> ' if (ACE_OS::read(pipe[0],line,3) != 3) break; // get library path len = 0; while ((nread = ACE_OS::read(pipe[0],line + len,1)) == 1 && (line[len] != ' ')) if (! bogus && ++len == max_line_length) { bogus = 1; break; } if (nread != 1 || bogus) break; line[len] = 0; nlib->set_path (line); libs_[num_libs_++] = nlib; ACE_ASSERT (num_libs_ < max_libs_); // grow max libs? } // skip the rest of the line while ((nread = ACE_OS::read(pipe[0],line,1)) == 1 && *line != '\n'); if (nread != 1) break; } proc.wait (); ACE_OS::close (pipe[0]); undef_wrapper_.add_source(path,1); // now do the ldd, iterate over the results to add new libs, etc. }
// Spawns the process, and waits for it to finish booting. // Then uses bind_to_naming_service, get_stream_endpoint, and get_vdev // to get the object references to the various objects created in the // child int TAO_AV_Endpoint_Process_Strategy::activate (void) { ACE_Process process; // Create a new process to contain this endpoint this->pid_ = process.spawn (*this->process_options_); // Process creation failed if (this->pid_ == -1) ORBSVCS_ERROR_RETURN ((LM_ERROR, "(%P|%t) ACE_Process:: spawn failed: %p\n", "spawn"), -1); // Create a unique semaphore name, using my hostname, and pid. ACE_TCHAR sem_str [BUFSIZ]; // create a unique semaphore name ACE_OS::sprintf (sem_str, ACE_TEXT("%s:%s:%ld"), "TAO_AV_Process_Semaphore", this->host_, static_cast<long int> (this->pid_)); ORBSVCS_DEBUG ((LM_DEBUG, "(%P|%t) semaphore is %s\n", sem_str)); // Create the semaphore ACE_Process_Semaphore semaphore (0, // 0 means that the // semaphore is locked initially sem_str); // wait until the child finishes booting while (1) { if (semaphore.acquire () == -1) { // See if my child process is still alive -- if not, return an error if (ACE_OS::kill (this->pid_, 0) == -1) ORBSVCS_ERROR_RETURN ((LM_ERROR, "(%P|%t) Process_Strategy: Process being waited on died unexpectedly.\n"), -1); // if we were not interrupted due to a EINTR, break if (errno != EINTR) break; } else break; } // The job of the semaphore is done, remove it. if (semaphore.remove () == -1) ORBSVCS_ERROR_RETURN ((LM_ERROR, "(%P|%t) semaphore remove failed: %p\n", "remove"), -1); try { // Get ourselves a Naming service this->bind_to_naming_service (); // Get the stream endpoint created by the child from the naming service this->get_stream_endpoint (); // Get the Vdev created by the child from the naming service this->get_vdev (); } catch (const CORBA::Exception& ex) { ex._tao_print_exception ( "TAO_AV_Endpoint_Process_Strategy::activate"); return -1; } return 0; }
int run_main (int argc, ACE_TCHAR *argv[]) { #if defined (ACE_LACKS_FORK) ACE_UNUSED_ARG (argc); ACE_UNUSED_ARG (argv); ACE_START_TEST (ACE_TEXT ("Process_Semaphore_Test")); ACE_ERROR ((LM_INFO, ACE_TEXT ("fork is not supported on this platform\n"))); ACE_END_TEST; #else parse_args (argc, argv); // Child process code. if (child_process) { ACE_START_TEST (ACE_TEXT ("Process_Semaphore_Test-child")); acquire_release (); ACE_END_LOG; } else { ACE_START_TEST (ACE_TEXT ("Process_Semaphore_Test")); ACE_TString exe_sub_dir; const char *subdir_env = ACE_OS::getenv ("ACE_EXE_SUB_DIR"); if (subdir_env) { exe_sub_dir = ACE_TEXT_CHAR_TO_TCHAR (subdir_env); exe_sub_dir += ACE_DIRECTORY_SEPARATOR_STR; } ACE_Process_Options options; options.command_line (ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR ACE_TEXT ("%sProcess_Semaphore_Test") ACE_PLATFORM_EXE_SUFFIX ACE_TEXT (" -c -i %d"), exe_sub_dir.c_str(), iterations); // Spawn a child process that will contend for the // lock. ACE_Process child; // Spawn the child process. int result = child.spawn (options); ACE_TEST_ASSERT (result != -1); ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Parent spawned child process with pid = %d.\n"), child.getpid ())); // start test acquire_release (); ACE_exitcode child_status; // Wait for the child processes we created to exit. ACE_TEST_ASSERT (child.wait (&child_status) != -1); if (child_status == 0) ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("Child %d finished ok\n"), child.getpid ())); else ACE_ERROR ((LM_ERROR, ACE_TEXT ("Child %d finished with status %d\n"), child.getpid (), child_status)); ACE_END_TEST; } #endif /* ! ACE_LACKS_FORK */ return 0; }
int #if defined (ACE_HAS_NONSTATIC_OBJECT_MANAGER) || defined (ACE_LACKS_FORK) // ACE_HAS_NONSTATIC_OBJECT_MANAGER only allows main to have two // arguments. And on platforms that lack fork (), we can't use spawn. main (int argc, ACE_TCHAR* []) { ACE_UNUSED_ARG (argc); ACE_OS::putenv (ACE_TEXT ("TEST_VALUE_POSITIVE=10.2")); ACE_OS::putenv (ACE_TEXT ("TEST_VALUE_NEGATIVE=-10.2")); #else /* ! ACE_HAS_NONSTATIC_OBJECT_MANAGER && ! ACE_LACKS_FORK */ main (int argc, ACE_TCHAR * [], ACE_TCHAR *envp[]) { if (argc == 1) { int status; // No arguments means we're the initial test. ACE_Process_Options options (1); status = options.setenv (envp); ACE_ASSERT (status == 0); options.command_line (ACE_TEXT (".") ACE_DIRECTORY_SEPARATOR_STR ACE_TEXT ("Env_Value_Test run_as_test")); status = options.setenv (ACE_TEXT ("TEST_VALUE_POSITIVE"), ACE_TEXT ("%s"), ACE_TEXT ("10.2")); ACE_ASSERT (status == 0); status = options.setenv (ACE_TEXT ("TEST_VALUE_NEGATIVE"), ACE_TEXT ("%s"), ACE_TEXT ("-10.2")); ACE_ASSERT (status == 0); ACE_Process p; pid_t result = p.spawn (options); ACE_ASSERT (result != -1); p.wait (); } else #endif /* ! ACE_HAS_NONSTATIC_OBJECT_MANAGER && ! ACE_LACKS_FORK */ { // In this case we're the child ACE_START_TEST (ACE_TEXT ("Env_Value_Test")); TEST_THIS (int, ACE_TEXT ("TEST_VALUE_POSITIVE"), 4, 10); #if !defined (ACE_LACKS_FLOATING_POINT) TEST_THIS (double, ACE_TEXT ("TEST_VALUE_POSITIVE"), -1.0, 10.2); #endif /* ! ACE_LACKS_FLOATING_POINT */ TEST_THIS (long, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (unsigned long, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (short, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (unsigned short, ACE_TEXT ("TEST_VALUE_POSITIVE"), 0, 10); TEST_THIS (int, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 4, -10); #if !defined (ACE_LACKS_FLOATING_POINT) TEST_THIS (double, ACE_TEXT ("TEST_VALUE_NEGATIVE"), -1.0, -10.2); #endif /* ! ACE_LACKS_FLOATING_POINT */ TEST_THIS (long, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, -10L); TEST_THIS (unsigned long, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, (unsigned long) -10); TEST_THIS (short, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, -10); TEST_THIS (unsigned short, ACE_TEXT ("TEST_VALUE_NEGATIVE"), 0, (unsigned short) -10); const ACE_TCHAR *defstr = ACE_TEXT ("Sarah Cleeland is Two!"); ACE_Env_Value<const ACE_TCHAR *> sval (ACE_TEXT ("This_Shouldnt_Be_Set_Hopefully"), defstr); ACE_ASSERT (ACE_OS::strcmp (sval, defstr) == 0); ACE_END_TEST; } return 0; }