?? fork.c
字號:
/* Copyright (C) 1994,1995,1996,1997,1999,2001,2002,2004,2005,2006 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 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */#include <errno.h>#include <unistd.h>#include <hurd.h>#include <hurd/signal.h>#include <setjmp.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>#undef __fork/* Things that want to be locked while forking. */symbol_set_declare (_hurd_fork_locks)/* 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. */DEFINE_HOOK (_hurd_fork_prepare_hook, (void));/* Things that want to be called when we are forking, with the above all locked. They are passed the task port of the child. The child process is all set up except for doing proc_child, and has no threads yet. */DEFINE_HOOK (_hurd_fork_setup_hook, (void));/* Things to be run in the child fork. */DEFINE_HOOK (_hurd_fork_child_hook, (void));/* Things to be run in the parent fork. */DEFINE_HOOK (_hurd_fork_parent_hook, (void));/* Clone the calling process, creating an exact copy. Return -1 for errors, 0 to the new process, and the process ID of the new process to the old process. */pid_t__fork (void){ jmp_buf env; pid_t pid; size_t i; error_t err; struct hurd_sigstate *volatile ss; ss = _hurd_self_sigstate (); __spin_lock (&ss->critical_section_lock);#undef LOSE#define LOSE do { assert_perror (err); goto lose; } while (0) /* XXX */ if (! setjmp (env)) { process_t newproc; task_t newtask; thread_t thread, sigthread; mach_port_urefs_t thread_refs, sigthread_refs; struct machine_thread_state state; mach_msg_type_number_t statecount; mach_port_t *portnames = NULL; mach_msg_type_number_t nportnames = 0; mach_port_type_t *porttypes = NULL; mach_msg_type_number_t nporttypes = 0; thread_t *threads = NULL; mach_msg_type_number_t nthreads = 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. */ { 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; /* Lock all the port cells for the standard ports while we copy the address space. We want to insert all the send rights into the child with the same names. */ for (i = 0; i < _hurd_nports; ++i) __spin_lock (&_hurd_ports[i].lock); ports_locked = 1; /* 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. */ __mutex_unlock (&_hurd_siglock); if (err) LOSE; /* Fetch the names of all ports used in this task. */ if (err = __mach_port_names (__mach_task_self (), &portnames, &nportnames, &porttypes, &nporttypes)) LOSE; if (nportnames != nporttypes) { err = EGRATUITOUS; LOSE; } /* Get send rights for all the threads in this task. We want to avoid giving these rights to the child. */ if (err = __task_threads (__mach_task_self (), &threads, &nthreads)) LOSE; /* Get the child process's proc server port. We will insert it into the child with the same name as we use for our own proc server port; and we will need it to set the child's message port. */ if (err = __proc_task2proc (_hurd_ports[INIT_PORT_PROC].port, newtask, &newproc)) LOSE; /* Insert all our port rights into the child task. */ thread_refs = sigthread_refs = 0; for (i = 0; i < nportnames; ++i) { if (porttypes[i] & MACH_PORT_TYPE_RECEIVE) { /* This is a receive right. We want to give the child task its own new receive right under the same name. */ err = __mach_port_allocate_name (newtask, MACH_PORT_RIGHT_RECEIVE, portnames[i]); if (err == KERN_NAME_EXISTS) { /* It already has a right under this name (?!). Well, there is this bizarre old Mach IPC feature (in #ifdef MACH_IPC_COMPAT in the ukernel) which results in new tasks getting a new receive right for task special port number 2. What else might be going on I'm not sure. So let's check. */#if !MACH_IPC_COMPAT#define TASK_NOTIFY_PORT 2#endif assert (({ mach_port_t thisport, notify_port; mach_msg_type_name_t poly; (__task_get_special_port (newtask, TASK_NOTIFY_PORT, ¬ify_port) == 0 && __mach_port_extract_right (newtask, portnames[i], MACH_MSG_TYPE_MAKE_SEND, &thisport, &poly) == 0 && (thisport == notify_port) && __mach_port_deallocate (__mach_task_self (), thisport) == 0 && __mach_port_deallocate (__mach_task_self (), notify_port) == 0); })); } else if (err) LOSE; if (porttypes[i] & MACH_PORT_TYPE_SEND) { /* Give the child as many send rights for its receive right as we have for ours. */ mach_port_urefs_t refs; mach_port_t port; mach_msg_type_name_t poly; if (err = __mach_port_get_refs (__mach_task_self (), portnames[i], MACH_PORT_RIGHT_SEND, &refs)) LOSE; if (err = __mach_port_extract_right (newtask, portnames[i], MACH_MSG_TYPE_MAKE_SEND, &port, &poly)) LOSE; if (portnames[i] == _hurd_msgport) { /* We just created a receive right for the child's message port and are about to insert send rights for it. Now, while we happen to have a send right for it, give it to the proc server. */ mach_port_t old; if (err = __proc_setmsgport (newproc, port, &old)) LOSE; 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], port, MACH_MSG_TYPE_MOVE_SEND)) LOSE; if (refs > 1 && (err = __mach_port_mod_refs (newtask, portnames[i], MACH_PORT_RIGHT_SEND, refs - 1))) LOSE; } if (porttypes[i] & MACH_PORT_TYPE_SEND_ONCE) { /* Give the child a send-once right for its receive right, since we have one for ours. */ mach_port_t port; mach_msg_type_name_t poly; if (err = __mach_port_extract_right (newtask, portnames[i], MACH_MSG_TYPE_MAKE_SEND_ONCE, &port, &poly)) LOSE; if (err = __mach_port_insert_right (newtask, portnames[i], port, MACH_MSG_TYPE_MOVE_SEND_ONCE)) LOSE; } } else if (porttypes[i] & (MACH_PORT_TYPE_SEND|MACH_PORT_TYPE_DEAD_NAME)) { /* 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_t insert; 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, insert the child's task port instead. */ insert = newtask; else if (portnames[i] == _hurd_ports[INIT_PORT_PROC].port) { /* Use the proc server port for the new task. */ insert = newproc; insert_type = MACH_MSG_TYPE_COPY_SEND; } 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 after we create it. */ 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). */ if (err = __mach_port_allocate_name (newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i])) LOSE; } else if (portnames[i] == _hurd_msgport_thread) /* For the name we use for our signal thread's thread port, we will insert the thread port for the child's signal thread after we create it. */ { insert = MACH_PORT_NULL; record_refs = &sigthread_refs;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -