]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - sysdeps/mach/hurd/fork.c
Update copyright notices with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / mach / hurd / fork.c
index 8e625b8e5e8043acf88e04cda79e75326b08c9a9..60c34c762065723bddfd28ba627d995e418c4bdb 100644 (file)
@@ -1,43 +1,43 @@
-/* Copyright (C) 1994, 1995 Free Software Foundation, Inc.
-This file is part of the GNU C Library.
+/* Copyright (C) 1994-2014 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
 
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
 
-The GNU C Library is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-Library General Public License for more details.
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
 
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB.  If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA.  */
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <errno.h>
 #include <unistd.h>
 #include <hurd.h>
 #include <hurd/signal.h>
 #include <setjmp.h>
-#include "thread_state.h"
+#include <thread_state.h>
 #include <sysdep.h>            /* For stack growth direction.  */
 #include "set-hooks.h"
 #include <assert.h>
 #include "hurdmalloc.h"                /* XXX */
+#include <tls.h>
 
-extern void _hurd_longjmp_thread_state (struct machine_thread_state *,
-                                       jmp_buf env, int value);
+#undef __fork
 
 
 /* Things that want to be locked while forking.  */
-struct
-  {
-    size_t n;
-    struct mutex *locks[0];
-  } _hurd_fork_locks;
+symbol_set_declare (_hurd_fork_locks)
+
 
+/* Application callbacks registered through pthread_atfork.  */
+DEFINE_HOOK (_hurd_atfork_prepare_hook, (void));
+DEFINE_HOOK (_hurd_atfork_child_hook, (void));
+DEFINE_HOOK (_hurd_atfork_parent_hook, (void));
 
 /* Things that want to be called before we fork, to prepare the parent for
    task_create, when the new child task will inherit our address space.  */
@@ -65,16 +65,15 @@ __fork (void)
   pid_t pid;
   size_t i;
   error_t err;
-  thread_t thread_self = __mach_thread_self ();
   struct hurd_sigstate *volatile ss;
 
+  RUN_HOOK (_hurd_atfork_prepare_hook, ());
+
   ss = _hurd_self_sigstate ();
-  __spin_lock (&ss->lock);
-  ss->critical_section = 1;
-  __spin_unlock (&ss->lock);
+  __spin_lock (&ss->critical_section_lock);
 
 #undef LOSE
-#define LOSE assert_perror (err) /* XXX */
+#define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */
 
   if (! setjmp (env))
     {
@@ -90,16 +89,34 @@ __fork (void)
       mach_msg_type_number_t nporttypes = 0;
       thread_t *threads = NULL;
       mach_msg_type_number_t nthreads = 0;
-      int ports_locked = 0;
+      int ports_locked = 0, stopped = 0;
+
+      void resume_threads (void)
+       {
+         if (! stopped)
+           return;
+
+         assert (threads);
+
+         for (i = 0; i < nthreads; ++i)
+           if (threads[i] != ss->thread)
+             __thread_resume (threads[i]);
+         stopped = 0;
+       }
 
       /* Run things that prepare for forking before we create the task.  */
       RUN_HOOK (_hurd_fork_prepare_hook, ());
 
       /* Lock things that want to be locked before we fork.  */
-      for (i = 0; i < _hurd_fork_locks.n; ++i)
-       __mutex_lock (_hurd_fork_locks.locks[i]);
+      {
+       void *const *p;
+       for (p = symbol_set_first_element (_hurd_fork_locks);
+            ! symbol_set_end_p (_hurd_fork_locks, p);
+            ++p)
+         __mutex_lock (*p);
+      }
       __mutex_lock (&_hurd_siglock);
-      
+
       newtask = MACH_PORT_NULL;
       thread = sigthread = MACH_PORT_NULL;
       newproc = MACH_PORT_NULL;
@@ -111,8 +128,40 @@ __fork (void)
        __spin_lock (&_hurd_ports[i].lock);
       ports_locked = 1;
 
-      /* Create the child task.  It will inherit a copy of our memory.  */
-      err = __task_create (__mach_task_self (), 1, &newtask);
+
+      /* Stop all other threads while copying the address space,
+        so nothing changes.  */
+      err = __proc_dostop (_hurd_ports[INIT_PORT_PROC].port, ss->thread);
+      if (!err)
+       {
+         stopped = 1;
+
+#define XXX_KERNEL_PAGE_FAULT_BUG /* XXX work around page fault bug in mk */
+
+#ifdef XXX_KERNEL_PAGE_FAULT_BUG
+         /* Gag me with a pitchfork.
+            The bug scenario is this:
+
+            - The page containing __mach_task_self_ is paged out.
+            - The signal thread was faulting on that page when we
+              suspended it via proc_dostop.  It holds some lock, or set
+              some busy bit, or somesuch.
+            - Now this thread faults on that same page.
+            - GRATUIOUS DEADLOCK
+
+            We can break the deadlock by aborting the thread that faulted
+            first, which if the bug happened was the signal thread because
+            it is the only other thread and we just suspended it.
+            */
+         __thread_abort (_hurd_msgport_thread);
+#endif
+         /* Create the child task.  It will inherit a copy of our memory.  */
+         err = __task_create (__mach_task_self (),
+#ifdef KERN_INVALID_LEDGER
+                              NULL, 0, /* OSF Mach */
+#endif
+                              1, &newtask);
+       }
 
       /* Unlock the global signal state lock, so we do not
         block the signal thread any longer than necessary.  */
@@ -171,7 +220,7 @@ __fork (void)
                             (__task_get_special_port (newtask,
                                                       TASK_NOTIFY_PORT,
                                                       &notify_port) == 0 &&
-                             __mach_port_extract_right 
+                             __mach_port_extract_right
                              (newtask,
                               portnames[i],
                               MACH_MSG_TYPE_MAKE_SEND,
@@ -214,6 +263,24 @@ __fork (void)
                      if (old != MACH_PORT_NULL)
                        /* XXX what to do here? */
                        __mach_port_deallocate (__mach_task_self (), old);
+                     /* The new task will receive its own exceptions
+                        on its message port.  */
+                     if (err =
+#ifdef TASK_EXCEPTION_PORT
+                         __task_set_special_port (newtask,
+                                                  TASK_EXCEPTION_PORT,
+                                                  port)
+#elif defined (EXC_MASK_ALL)
+                         __task_set_exception_ports
+                         (newtask, EXC_MASK_ALL & ~(EXC_MASK_SYSCALL
+                                                    | EXC_MASK_MACH_SYSCALL
+                                                    | EXC_MASK_RPC_ALERT),
+                          port, EXCEPTION_DEFAULT, MACHINE_THREAD_STATE)
+#else
+# error task_set_exception_port?
+#endif
+                         )
+                       LOSE;
                    }
                  if (err = __mach_port_insert_right (newtask,
                                                      portnames[i],
@@ -251,10 +318,11 @@ __fork (void)
            {
              /* This is a send right or a dead name.
                 Give the child as many references for it as we have.  */
-             mach_port_urefs_t refs, *record_refs = NULL;
+             mach_port_urefs_t refs = 0, *record_refs = NULL;
              mach_port_t insert;
-             if (portnames[i] == newtask)
-               /* Skip the name we use for the child's task port.  */
+             mach_msg_type_name_t insert_type = MACH_MSG_TYPE_COPY_SEND;
+             if (portnames[i] == newtask || portnames[i] == newproc)
+               /* Skip the name we use for the child's task or proc ports.  */
                continue;
              if (portnames[i] == __mach_task_self ())
                /* For the name we use for our own task port,
@@ -262,11 +330,11 @@ __fork (void)
                insert = newtask;
              else if (portnames[i] == _hurd_ports[INIT_PORT_PROC].port)
                {
-                 /* Get the proc server port for the new task.  */
-                 if (err = __proc_task2proc (portnames[i], newtask, &insert))
-                   LOSE;
+                 /* Use the proc server port for the new task.  */
+                 insert = newproc;
+                 insert_type = MACH_MSG_TYPE_COPY_SEND;
                }
-             else if (portnames[i] == thread_self)
+             else if (portnames[i] == ss->thread)
                {
                  /* For the name we use for our own thread port, we will
                     insert the thread port for the child main user thread
@@ -274,9 +342,9 @@ __fork (void)
                  insert = MACH_PORT_NULL;
                  record_refs = &thread_refs;
                  /* Allocate a dead name right for this name as a
-                     placeholder, so the kernel will not chose this name
-                     for any other new port (it might use it for one of the
-                     rights created when a thread is created).  */
+                    placeholder, so the kernel will not chose this name
+                    for any other new port (it might use it for one of the
+                    rights created when a thread is created).  */
                  if (err = __mach_port_allocate_name
                      (newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i]))
                    LOSE;
@@ -303,6 +371,7 @@ __fork (void)
                  if (j < nthreads)
                    continue;
 
+                 /* Copy our own send right.  */
                  insert = portnames[i];
                }
              /* Find out how many user references we have for
@@ -326,8 +395,7 @@ __fork (void)
                /* Insert the chosen send right into the child.  */
                err = __mach_port_insert_right (newtask,
                                                portnames[i],
-                                               insert,
-                                               MACH_MSG_TYPE_COPY_SEND);
+                                               insert, insert_type);
              switch (err)
                {
                case KERN_NAME_EXISTS:
@@ -375,17 +443,21 @@ __fork (void)
        __spin_unlock (&_hurd_ports[i].lock);
       ports_locked = 0;
 
+      /* All state has now been copied from the parent.  It is safe to
+        resume other parent threads.  */
+      resume_threads ();
+
       /* Create the child main user thread and signal thread.  */
       if ((err = __thread_create (newtask, &thread)) ||
          (err = __thread_create (newtask, &sigthread)))
        LOSE;
 
       /* Insert send rights for those threads.  We previously allocated
-         dead name rights with the names we want to give the thread ports
-         in the child as placeholders.  Now deallocate them so we can use
-         the names.  */
-      if ((err = __mach_port_deallocate (newtask, thread_self)) ||
-         (err = __mach_port_insert_right (newtask, thread_self,
+        dead name rights with the names we want to give the thread ports
+        in the child as placeholders.  Now deallocate them so we can use
+        the names.  */
+      if ((err = __mach_port_deallocate (newtask, ss->thread)) ||
+         (err = __mach_port_insert_right (newtask, ss->thread,
                                           thread, MACH_MSG_TYPE_COPY_SEND)))
        LOSE;
       /* We have one extra user reference created at the beginning of this
@@ -393,9 +465,9 @@ __fork (void)
         accounted for in the child below).  This extra right gets consumed
         in the child by the store into _hurd_sigthread in the child fork.  */
       if (thread_refs > 1 &&
-         (err = __mach_port_mod_refs (newtask, thread_self,
+         (err = __mach_port_mod_refs (newtask, ss->thread,
                                       MACH_PORT_RIGHT_SEND,
-                                      thread_refs - 1)))
+                                      thread_refs)))
        LOSE;
       if ((_hurd_msgport_thread != MACH_PORT_NULL) /* Let user have none.  */
          && ((err = __mach_port_deallocate (newtask, _hurd_msgport_thread)) ||
@@ -420,7 +492,7 @@ __fork (void)
        if (err)
          LOSE;
       }
-           
+
       /* Set the child signal thread up to run the msgport server function
         using the same signal thread stack copied from our address space.
         We fetch the state before longjmp'ing it so that miscellaneous
@@ -432,10 +504,28 @@ __fork (void)
                                    (natural_t *) &state, &statecount))
        LOSE;
 #if STACK_GROWTH_UP
-      state.SP = __hurd_sigthread_stack_base;
+#define THREADVAR_SPACE (__hurd_threadvar_max \
+                        * sizeof *__hurd_sightread_variables)
+      if (__hurd_sigthread_stack_base == 0)
+       {
+         state.SP &= __hurd_threadvar_stack_mask;
+         state.SP += __hurd_threadvar_stack_offset + THREADVAR_SPACE;
+       }
+      else
+       state.SP = __hurd_sigthread_stack_base;
 #else
-      state.SP = __hurd_sigthread_stack_end;
-#endif      
+      if (__hurd_sigthread_stack_end == 0)
+       {
+         /* The signal thread has a normal stack assigned by cthreads.
+            The threadvar_stack variables conveniently tell us how
+            to get to the highest address in the stack, just below
+            the per-thread variables.  */
+         state.SP &= __hurd_threadvar_stack_mask;
+         state.SP += __hurd_threadvar_stack_offset;
+       }
+      else
+       state.SP = __hurd_sigthread_stack_end;
+#endif
       MACHINE_THREAD_STATE_SET_PC (&state,
                                   (unsigned long int) _hurd_msgport_receive);
       if (err = __thread_set_state (sigthread, MACHINE_THREAD_STATE_FLAVOR,
@@ -446,6 +536,11 @@ __fork (void)
 
       /* Set the child user thread up to return 1 from the setjmp above.  */
       _hurd_longjmp_thread_state (&state, env, 1);
+
+      /* Do special thread setup for TLS if needed.  */
+      if (err = _hurd_tls_fork (thread, &state))
+       LOSE;
+
       if (err = __thread_set_state (thread, MACHINE_THREAD_STATE_FLAVOR,
                                    (natural_t *) &state, statecount))
        LOSE;
@@ -477,6 +572,8 @@ __fork (void)
        for (i = 0; i < _hurd_nports; ++i)
          __spin_unlock (&_hurd_ports[i].lock);
 
+      resume_threads ();
+
       if (newtask != MACH_PORT_NULL)
        {
          if (err)
@@ -489,8 +586,6 @@ __fork (void)
        __mach_port_deallocate (__mach_task_self (), sigthread);
       if (newproc != MACH_PORT_NULL)
        __mach_port_deallocate (__mach_task_self (), newproc);
-      if (thread_self != MACH_PORT_NULL)
-       __mach_port_deallocate (__mach_task_self (), thread_self);
 
       if (portnames)
        __vm_deallocate (__mach_task_self (),
@@ -521,19 +616,22 @@ __fork (void)
       struct hurd_sigstate *oldstates;
 
       /* We are the child task.  Unlock the standard port cells, which were
-         locked in the parent when we copied its memory.  The parent has
-         inserted send rights with the names that were in the cells then.  */
+        locked in the parent when we copied its memory.  The parent has
+        inserted send rights with the names that were in the cells then.  */
       for (i = 0; i < _hurd_nports; ++i)
        __spin_unlock (&_hurd_ports[i].lock);
 
-      /* We are the only thread in this new task, so we will
-        take the task-global signals.  */
-      _hurd_sigthread = thread_self;
-
-      /* Unchain the sigstate structures for threads that existed in the
-        parent task but don't exist in this task (the child process).
-        Delay freeing them until later because some of the further setup
-        and unlocking might be required for free to work.  */
+      /* We are one of the (exactly) two threads in this new task, we
+        will take the task-global signals.  */
+      _hurd_sigthread = ss->thread;
+
+      /* Claim our sigstate structure and unchain the rest: the
+        threads existed in the parent task but don't exist in this
+        task (the child process).  Delay freeing them until later
+        because some of the further setup and unlocking might be
+        required for free to work.  Before we finish cleaning up,
+        we will reclaim the signal thread's sigstate structure (if
+        it had one).  */
       oldstates = _hurd_sigstates;
       if (oldstates == ss)
        oldstates = ss->next;
@@ -554,6 +652,9 @@ __fork (void)
       err = __USEPORT (PROC, __proc_getpids (port, &_hurd_pid, &_hurd_ppid,
                                             &_hurd_orphaned));
 
+      /* Forking clears the trace flag.  */
+      __sigemptyset (&_hurdsig_traced);
+
       /* Run things that want to run in the child task to set up.  */
       RUN_HOOK (_hurd_fork_child_hook, ());
 
@@ -564,13 +665,26 @@ __fork (void)
       if (!err)
        err = __thread_resume (_hurd_msgport_thread);
 
-      /* Free the old sigstate structures.  */
+      /* Reclaim the signal thread's sigstate structure and free the
+        other old sigstate structures.  */
       while (oldstates != NULL)
        {
          struct hurd_sigstate *next = oldstates->next;
-         free (oldstates);
+
+         if (oldstates->thread == _hurd_msgport_thread)
+           {
+             /* If we have a second signal state structure then we
+                must have been through here before--not good.  */
+             assert (_hurd_sigstates->next == 0);
+             _hurd_sigstates->next = oldstates;
+             oldstates->next = 0;
+           }
+         else
+           free (oldstates);
+
          oldstates = next;
        }
+
       /* XXX what to do if we have any errors here? */
 
       pid = 0;
@@ -578,12 +692,26 @@ __fork (void)
 
   /* Unlock things we locked before creating the child task.
      They are locked in both the parent and child tasks.  */
-  for (i = 0; i < _hurd_fork_locks.n; ++i)
-    __mutex_unlock (_hurd_fork_locks.locks[i]);
+  {
+    void *const *p;
+    for (p = symbol_set_first_element (_hurd_fork_locks);
+        ! symbol_set_end_p (_hurd_fork_locks, p);
+        ++p)
+      __mutex_unlock (*p);
+  }
 
   _hurd_critical_section_unlock (ss);
 
+  if (!err)
+    {
+      if (pid != 0)
+       RUN_HOOK (_hurd_atfork_parent_hook, ());
+      else
+       RUN_HOOK (_hurd_atfork_child_hook, ());
+    }
+
   return err ? __hurd_fail (err) : pid;
 }
+libc_hidden_def (__fork)
 
 weak_alias (__fork, fork)