|
|||
Prex Home / Browse Source - Prex Version: 0.9.0 |
|||
root/usr/lib/posix/process/fork.c/* [<][>][^][v][top][bottom][index][help] */DEFINITIONSThis source file includes following definitions.1 /* 2 * Copyright (c) 2005-2006, Kohsuke Ohtani 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the author nor the names of any co-contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/prex.h> 31 #include <sys/posix.h> 32 #include <sys/signal.h> 33 #include <ipc/proc.h> 34 #include <ipc/fs.h> 35 #include <ipc/ipc.h> 36 37 #include <stddef.h> 38 #include <setjmp.h> 39 #include <errno.h> 40 #include <unistd.h> 41 42 static void __child_entry(void); 43 44 static jmp_buf __fork_env; 45 46 /* 47 * fork() - fork for MMU system. 48 * 49 * RETURN VALUE: 50 * 51 * fork() returns 0 to the child process and return the process 52 * ID of the child process to the parent process. Or, -1 will be 53 * returned to the parent process if error. 54 * 55 * ERRORS: 56 * 57 * EAGAIN 58 * ENOMEM 59 * 60 * NOTE: 61 * 62 * Since no thread is created by task_create(), thread_create() 63 * must be called follwing task_crate(). But, when new thread is 64 * created by thread_create(), the stack pointer of new thread is 65 * used at thread_create() although the stack image is copied at 66 * task_create(). So, the stack pointer must be reset to same 67 * address of thread_create() before calling task_create(); 68 * This is a little tricky... 69 * 70 * The new process is an exact copy of the calling process 71 * except as detailed below: 72 * - Process IDs are different. 73 * - tms_* is set to 0. 74 * - Alarm clock is set to 0. 75 * - Opend semaphore is inherited. 76 * - Pending signals are cleared. 77 * 78 * - File lock is not inherited. 79 * - File descriptor is shared. 80 * - Directory stream is shared. 81 */ 82 static pid_t 83 fork(void) 84 { 85 struct msg m; 86 task_t tsk; 87 thread_t t; 88 int error; 89 pid_t pid; 90 91 /* Save current stack pointer */ 92 if (setjmp(__fork_env) == 0) { 93 /* 94 * Create new task 95 */ 96 error = task_create(task_self(), VM_COPY, &tsk); 97 if (error) { 98 errno = error; 99 return -1; 100 } 101 if ((error = thread_create(tsk, &t)) != 0) { 102 task_terminate(tsk); 103 errno = error; 104 return -1; 105 } 106 /* 107 * Notify to process server 108 */ 109 m.hdr.code = PS_FORK; 110 m.data[0] = tsk; /* child task */ 111 m.data[1] = 0; /* fork type */ 112 if (__posix_call(__proc_obj, &m, sizeof(m), 1) != 0) 113 return -1; 114 pid = m.data[0]; 115 116 /* 117 * Notify to file system server 118 */ 119 m.hdr.code = FS_FORK; 120 m.data[0] = tsk; /* child task */ 121 if (__posix_call(__fs_obj, &m, sizeof(m), 1) != 0) 122 return -1; 123 124 /* 125 * Start child task 126 */ 127 thread_load(t, __child_entry, NULL); 128 thread_resume(t); 129 } else { 130 /* 131 * Child task 132 */ 133 #ifdef _REENTRANT 134 error = mutex_init(&__sig_lock); 135 #endif 136 __sig_pending = 0; 137 return 0; 138 } 139 return pid; 140 } 141 142 static void 143 __child_entry(void) 144 { 145 146 longjmp(__fork_env, 1); 147 /* NOTREACHED */ 148 } 149 150 pid_t 151 vfork(void) 152 { 153 154 return fork(); 155 } 156 /* [<][>][^][v][top][bottom][index][help] */ | |||
Copyright© 2005-2009 Kohsuke Ohtani |