< prev index next >

src/os/bsd/vm/os_bsd.cpp

Print this page


   1 /*
   2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


4699      }
4700   } else {
4701     pthread_mutex_unlock(_mutex);
4702     assert (status == 0, "invariant") ;
4703   }
4704 }
4705 
4706 
4707 /* Darwin has no "environ" in a dynamic library. */
4708 #ifdef __APPLE__
4709 #include <crt_externs.h>
4710 #define environ (*_NSGetEnviron())
4711 #else
4712 extern char** environ;
4713 #endif
4714 
4715 // Run the specified command in a separate process. Return its exit value,
4716 // or -1 on failure (e.g. can't fork a new process).
4717 // Unlike system(), this function can be called from signal handler. It
4718 // doesn't block SIGINT et al.
4719 int os::fork_and_exec(char* cmd) {
4720   const char * argv[4] = {"sh", "-c", cmd, NULL};
4721 
4722   // fork() in BsdThreads/NPTL is not async-safe. It needs to run
4723   // pthread_atfork handlers and reset pthread library. All we need is a
4724   // separate process to execve. Make a direct syscall to fork process.
4725   // On IA64 there's no fork syscall, we have to use fork() and hope for
4726   // the best...
4727   pid_t pid = fork();
4728 
4729   if (pid < 0) {
4730     // fork failed
4731     return -1;
4732 
4733   } else if (pid == 0) {
4734     // child process
4735 
4736     // execve() in BsdThreads will call pthread_kill_other_threads_np()
4737     // first to kill every thread on the thread list. Because this list is
4738     // not reset by fork() (see notes above), execve() will instead kill
4739     // every thread in the parent process. We know this is the only thread


   1 /*
   2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *


4699      }
4700   } else {
4701     pthread_mutex_unlock(_mutex);
4702     assert (status == 0, "invariant") ;
4703   }
4704 }
4705 
4706 
4707 /* Darwin has no "environ" in a dynamic library. */
4708 #ifdef __APPLE__
4709 #include <crt_externs.h>
4710 #define environ (*_NSGetEnviron())
4711 #else
4712 extern char** environ;
4713 #endif
4714 
4715 // Run the specified command in a separate process. Return its exit value,
4716 // or -1 on failure (e.g. can't fork a new process).
4717 // Unlike system(), this function can be called from signal handler. It
4718 // doesn't block SIGINT et al.
4719 int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
4720   const char * argv[4] = {"sh", "-c", cmd, NULL};
4721 
4722   // fork() in BsdThreads/NPTL is not async-safe. It needs to run
4723   // pthread_atfork handlers and reset pthread library. All we need is a
4724   // separate process to execve. Make a direct syscall to fork process.
4725   // On IA64 there's no fork syscall, we have to use fork() and hope for
4726   // the best...
4727   pid_t pid = fork();
4728 
4729   if (pid < 0) {
4730     // fork failed
4731     return -1;
4732 
4733   } else if (pid == 0) {
4734     // child process
4735 
4736     // execve() in BsdThreads will call pthread_kill_other_threads_np()
4737     // first to kill every thread on the thread list. Because this list is
4738     // not reset by fork() (see notes above), execve() will instead kill
4739     // every thread in the parent process. We know this is the only thread


< prev index next >