Annotation of prex-old/usr/lib/posix/process/fork.c, Revision 1.1.1.1
1.1 nbrk 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 <prex/prex.h>
31: #include <prex/posix.h>
32: #include <prex/signal.h>
33: #include <server/proc.h>
34: #include <server/fs.h>
35: #include <server/stdmsg.h>
36:
37: #include <stddef.h>
38: #include <setjmp.h>
39: #include <errno.h>
40:
41: static void __child_entry(void);
42:
43: static jmp_buf __fork_env;
44:
45: /*
46: * fork() - fork for MMU system.
47: *
48: * RETURN VALUE:
49: *
50: * fork() returns 0 to the child process and return the process
51: * ID of the child process to the parent process. Or, -1 will be
52: * returned to the parent process if error.
53: *
54: * ERRORS:
55: *
56: * EAGAIN
57: * ENOMEM
58: *
59: * NOTE:
60: *
61: * Since no thread is created by task_create(), thread_create()
62: * must be called follwing task_crate(). But, when new thread is
63: * created by thread_create(), the stack pointer of new thread is
64: * used at thread_create() although the stack image is copied at
65: * task_create(). So, the stack pointer must be reset to same
66: * address of thread_create() before calling task_create();
67: * This is a little tricky...
68: *
69: * The new process is an exact copy of the calling process
70: * except as detailed below:
71: * - Process IDs are different.
72: * - tms_* is set to 0.
73: * - Alarm clock is set to 0.
74: * - Opend semaphore is inherited.
75: * - Pending signals are cleared.
76: *
77: * - File lock is not inherited.
78: * - File descriptor is same
79: * - Directory stream is same
80: */
81: static pid_t
82: fork(void)
83: {
84: struct msg m;
85: task_t tsk;
86: thread_t th;
87: int err;
88: pid_t pid;
89:
90: /* Save current stack pointer */
91: if (setjmp(__fork_env) == 0) {
92: /*
93: * Create new task
94: */
95: err = task_create(task_self(), VM_COPY, &tsk);
96: if (err) {
97: errno = err;
98: return -1;
99: }
100: if ((err = thread_create(tsk, &th)) != 0) {
101: task_terminate(tsk);
102: errno = err;
103: return -1;
104: }
105: /*
106: * Notify to process server
107: */
108: m.hdr.code = PS_FORK;
109: m.data[0] = tsk; /* child task */
110: m.data[1] = 0; /* fork type */
111: if (__posix_call(__proc_obj, &m, sizeof(m), 1) != 0)
112: return -1;
113: pid = m.data[0];
114:
115: /*
116: * Notify to file system server
117: */
118: m.hdr.code = FS_FORK;
119: m.data[0] = tsk; /* child task */
120: if (__posix_call(__fs_obj, &m, sizeof(m), 1) != 0)
121: return -1;
122:
123: /*
124: * Start child task
125: */
126: thread_load(th, __child_entry, NULL);
127: thread_resume(th);
128: } else {
129: /*
130: * Child task
131: */
132: #ifdef _REENTRANT
133: err = mutex_init(&__sig_lock);
134: #endif
135: __sig_pending = 0;
136: return 0;
137: }
138: return pid;
139: }
140:
141: static void
142: __child_entry(void)
143: {
144:
145: longjmp(__fork_env, 1);
146: /* NOTREACHED */
147: }
148:
149: pid_t
150: vfork(void)
151: {
152:
153: return fork();
154: }
155:
CVSweb