< prev index next >

src/os/linux/vm/os_linux.cpp

Print this page




5912         assert (status == 0, "invariant");
5913       } else {
5914         status = pthread_mutex_unlock(_mutex);
5915         assert (status == 0, "invariant");
5916         status = pthread_cond_signal (&_cond[_cur_index]);
5917         assert (status == 0, "invariant");
5918       }
5919     } else {
5920       pthread_mutex_unlock(_mutex);
5921       assert (status == 0, "invariant") ;
5922     }
5923   } else {
5924     pthread_mutex_unlock(_mutex);
5925     assert (status == 0, "invariant") ;
5926   }
5927 }
5928 
5929 
5930 extern char** environ;
5931 
5932 #ifndef __NR_fork
5933 #define __NR_fork IA32_ONLY(2) IA64_ONLY(not defined) AMD64_ONLY(57)
5934 #endif
5935 
5936 #ifndef __NR_execve
5937 #define __NR_execve IA32_ONLY(11) IA64_ONLY(1033) AMD64_ONLY(59)
5938 #endif
5939 
5940 // Run the specified command in a separate process. Return its exit value,
5941 // or -1 on failure (e.g. can't fork a new process).
5942 // Unlike system(), this function can be called from signal handler. It
5943 // doesn't block SIGINT et al.
5944 int os::fork_and_exec(char* cmd) {
5945   const char * argv[4] = {"sh", "-c", cmd, NULL};
5946 
5947   // fork() in LinuxThreads/NPTL is not async-safe. It needs to run
5948   // pthread_atfork handlers and reset pthread library. All we need is a
5949   // separate process to execve. Make a direct syscall to fork process.
5950   // On IA64 there's no fork syscall, we have to use fork() and hope for
5951   // the best...
5952   pid_t pid = NOT_IA64(syscall(__NR_fork);)
5953               IA64_ONLY(fork();)
5954 
5955   if (pid < 0) {
5956     // fork failed
5957     return -1;
5958 
5959   } else if (pid == 0) {
5960     // child process
5961 
5962     // execve() in LinuxThreads will call pthread_kill_other_threads_np()
5963     // first to kill every thread on the thread list. Because this list is
5964     // not reset by fork() (see notes above), execve() will instead kill
5965     // every thread in the parent process. We know this is the only thread
5966     // in the new process, so make a system call directly.
5967     // IA64 should use normal execve() from glibc to match the glibc fork()
5968     // above.
5969     NOT_IA64(syscall(__NR_execve, "/bin/sh", argv, environ);)
5970     IA64_ONLY(execve("/bin/sh", (char* const*)argv, environ);)
5971 
5972     // execve failed
5973     _exit(-1);
5974 
5975   } else  {
5976     // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
5977     // care about the actual exit code, for now.
5978 
5979     int status;
5980 
5981     // Wait for the child process to exit.  This returns immediately if
5982     // the child has already exited. */
5983     while (waitpid(pid, &status, 0) < 0) {
5984         switch (errno) {
5985         case ECHILD: return 0;
5986         case EINTR: break;
5987         default: return -1;
5988         }
5989     }
5990 




5912         assert (status == 0, "invariant");
5913       } else {
5914         status = pthread_mutex_unlock(_mutex);
5915         assert (status == 0, "invariant");
5916         status = pthread_cond_signal (&_cond[_cur_index]);
5917         assert (status == 0, "invariant");
5918       }
5919     } else {
5920       pthread_mutex_unlock(_mutex);
5921       assert (status == 0, "invariant") ;
5922     }
5923   } else {
5924     pthread_mutex_unlock(_mutex);
5925     assert (status == 0, "invariant") ;
5926   }
5927 }
5928 
5929 
5930 extern char** environ;
5931 








5932 // Run the specified command in a separate process. Return its exit value,
5933 // or -1 on failure (e.g. can't fork a new process).
5934 // Unlike system(), this function can be called from signal handler. It
5935 // doesn't block SIGINT et al.
5936 int os::fork_and_exec(char* cmd) {
5937   const char * argv[4] = {"sh", "-c", cmd, NULL};
5938 
5939   pid_t pid = fork();






5940 
5941   if (pid < 0) {
5942     // fork failed
5943     return -1;
5944 
5945   } else if (pid == 0) {
5946     // child process
5947 
5948     execve("/bin/sh", (char* const*)argv, environ);








5949 
5950     // execve failed
5951     _exit(-1);
5952 
5953   } else  {
5954     // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
5955     // care about the actual exit code, for now.
5956 
5957     int status;
5958 
5959     // Wait for the child process to exit.  This returns immediately if
5960     // the child has already exited. */
5961     while (waitpid(pid, &status, 0) < 0) {
5962         switch (errno) {
5963         case ECHILD: return 0;
5964         case EINTR: break;
5965         default: return -1;
5966         }
5967     }
5968 


< prev index next >