?? fork.c
字號:
/* Allocate a dead name right as a placeholder. */ if (err = __mach_port_allocate_name (newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i])) LOSE; } else { /* Skip the name we use for any of our own thread ports. */ mach_msg_type_number_t j; for (j = 0; j < nthreads; ++j) if (portnames[i] == threads[j]) break; if (j < nthreads) continue; /* Copy our own send right. */ insert = portnames[i]; } /* Find out how many user references we have for the send right with this name. */ if (err = __mach_port_get_refs (__mach_task_self (), portnames[i], MACH_PORT_RIGHT_SEND, record_refs ?: &refs)) LOSE; if (insert == MACH_PORT_NULL) continue; if (insert == portnames[i] && (porttypes[i] & MACH_PORT_TYPE_DEAD_NAME)) /* This is a dead name; allocate another dead name with the same name in the child. */ allocate_dead_name: err = __mach_port_allocate_name (newtask, MACH_PORT_RIGHT_DEAD_NAME, portnames[i]); else /* Insert the chosen send right into the child. */ err = __mach_port_insert_right (newtask, portnames[i], insert, insert_type); switch (err) { case KERN_NAME_EXISTS: { /* It already has a send right under this name (?!). Well, it starts out with a send right for its task port, and inherits the bootstrap and exception ports from us. */ mach_port_t childport; mach_msg_type_name_t poly; assert (__mach_port_extract_right (newtask, portnames[i], MACH_MSG_TYPE_COPY_SEND, &childport, &poly) == 0 && childport == insert && __mach_port_deallocate (__mach_task_self (), childport) == 0); break; } case KERN_INVALID_CAPABILITY: /* The port just died. It was a send right, and now it's a dead name. */ goto allocate_dead_name; default: LOSE; break; case KERN_SUCCESS: /* Give the child as many user references as we have. */ if (refs > 1 && (err = __mach_port_mod_refs (newtask, portnames[i], MACH_PORT_RIGHT_SEND, refs - 1))) LOSE; } } } /* Unlock the standard port cells. The child must unlock its own copies too. */ for (i = 0; i < _hurd_nports; ++i) __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, 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 function, accounted for by mach_port_names (and which will thus be 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, ss->thread, MACH_PORT_RIGHT_SEND, thread_refs))) LOSE; if ((_hurd_msgport_thread != MACH_PORT_NULL) /* Let user have none. */ && ((err = __mach_port_deallocate (newtask, _hurd_msgport_thread)) || (err = __mach_port_insert_right (newtask, _hurd_msgport_thread, sigthread, MACH_MSG_TYPE_COPY_SEND)))) LOSE; if (sigthread_refs > 1 && (err = __mach_port_mod_refs (newtask, _hurd_msgport_thread, MACH_PORT_RIGHT_SEND, sigthread_refs - 1))) LOSE; /* This seems like a convenient juncture to copy the proc server's idea of what addresses our argv and envp are found at from the parent into the child. Since we happen to know that the child shares our memory image, it is we who should do this copying. */ { vm_address_t argv, envp; err = (__USEPORT (PROC, __proc_get_arg_locations (port, &argv, &envp)) ?: __proc_set_arg_locations (newproc, argv, envp)); 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 registers not affected by longjmp (such as i386 segment registers) are in their normal default state. */ statecount = MACHINE_THREAD_STATE_COUNT; if (err = __thread_get_state (_hurd_msgport_thread, MACHINE_THREAD_STATE_FLAVOR, (natural_t *) &state, &statecount)) LOSE;#if STACK_GROWTH_UP#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 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, (natural_t *) &state, statecount)) LOSE; /* We do not thread_resume SIGTHREAD here because the child fork needs to do more setup before it can take signals. */ /* 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; /* Get the PID of the child from the proc server. We must do this before calling proc_child below, because at that point any authorized POSIX.1 process may kill the child task with SIGKILL. */ if (err = __USEPORT (PROC, __proc_task2pid (port, newtask, &pid))) LOSE; /* Register the child with the proc server. It is important that this be that last thing we do before starting the child thread running. Once proc_child has been done for the task, it appears as a POSIX.1 process. Any errors we get must be detected before this point, and the child must have a message port so it responds to POSIX.1 signals. */ if (err = __USEPORT (PROC, __proc_child (port, newtask))) LOSE; /* This must be the absolutely last thing we do; we can't assume that the child will remain alive for even a moment once we do this. We ignore errors because we have committed to the fork and are not allowed to return them after the process becomes visible to POSIX.1 (which happened right above when we called proc_child). */ (void) __thread_resume (thread); lose: if (ports_locked) for (i = 0; i < _hurd_nports; ++i) __spin_unlock (&_hurd_ports[i].lock); resume_threads (); if (newtask != MACH_PORT_NULL) { if (err) __task_terminate (newtask); } if (thread != MACH_PORT_NULL) __mach_port_deallocate (__mach_task_self (), thread); if (sigthread != MACH_PORT_NULL) __mach_port_deallocate (__mach_task_self (), sigthread); if (newproc != MACH_PORT_NULL) __mach_port_deallocate (__mach_task_self (), newproc); if (portnames) __vm_deallocate (__mach_task_self (), (vm_address_t) portnames, nportnames * sizeof (*portnames)); if (porttypes) __vm_deallocate (__mach_task_self (), (vm_address_t) porttypes, nporttypes * sizeof (*porttypes)); if (threads) { for (i = 0; i < nthreads; ++i) __mach_port_deallocate (__mach_task_self (), threads[i]); __vm_deallocate (__mach_task_self (), (vm_address_t) threads, nthreads * sizeof (*threads)); } /* Run things that want to run in the parent to restore it to normality. Usually prepare hooks and parent hooks are symmetrical: the prepare hook arrests state in some way for the fork, and the parent hook restores the state for the parent to continue executing normally. */ RUN_HOOK (_hurd_fork_parent_hook, ()); } else { 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. */ for (i = 0; i < _hurd_nports; ++i) __spin_unlock (&_hurd_ports[i].lock); /* 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; else { while (_hurd_sigstates->next != ss) _hurd_sigstates = _hurd_sigstates->next; _hurd_sigstates->next = ss->next; } ss->next = NULL; _hurd_sigstates = ss; __mutex_unlock (&_hurd_siglock); /* Fetch our new process IDs from the proc server. No need to refetch our pgrp; it is always inherited from the parent (so _hurd_pgrp is already correct), and the proc server will send us a proc_newids notification when it changes. */ 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, ()); /* Set up proc server-assisted fault recovery for the signal thread. */ _hurdsig_fault_init (); /* Start the signal thread listening on the message port. */ if (!err) err = __thread_resume (_hurd_msgport_thread); /* Reclaim the signal thread's sigstate structure and free the other old sigstate structures. */ while (oldstates != NULL) { struct hurd_sigstate *next = oldstates->next; 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; } /* Unlock things we locked before creating the child task. They are locked in both the parent and child tasks. */ { 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); return err ? __hurd_fail (err) : pid;}libc_hidden_def (__fork)weak_alias (__fork, fork)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -