1 | /* The kernel call implemented in this file:
|
---|
2 | * m_type: SYS_FORK
|
---|
3 | *
|
---|
4 | * The parameters for this kernel call are:
|
---|
5 | * m1_i1: PR_SLOT (child's process table slot)
|
---|
6 | * m1_i2: PR_ENDPT (parent, process that forked)
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include "../system.h"
|
---|
10 | #include <signal.h>
|
---|
11 | #if (CHIP == INTEL)
|
---|
12 | #include "../protect.h"
|
---|
13 | #endif
|
---|
14 |
|
---|
15 | #include <minix/endpoint.h>
|
---|
16 |
|
---|
17 | #if USE_FORK
|
---|
18 |
|
---|
19 | /*===========================================================================*
|
---|
20 | * do_fork *
|
---|
21 | *===========================================================================*/
|
---|
22 | PUBLIC int do_fork(m_ptr)
|
---|
23 | register message *m_ptr; /* pointer to request message */
|
---|
24 | {
|
---|
25 | /* Handle sys_fork(). PR_ENDPT has forked. The child is PR_SLOT. */
|
---|
26 | #if (CHIP == INTEL)
|
---|
27 | reg_t old_ldt_sel;
|
---|
28 | #endif
|
---|
29 | register struct proc *rpc; /* child process pointer */
|
---|
30 | struct proc *rpp; /* parent process pointer */
|
---|
31 | int i, gen;
|
---|
32 | int p_proc;
|
---|
33 |
|
---|
34 | if(!isokendpt(m_ptr->PR_ENDPT, &p_proc))
|
---|
35 | return EINVAL;
|
---|
36 | rpp = proc_addr(p_proc);
|
---|
37 | rpc = proc_addr(m_ptr->PR_SLOT);
|
---|
38 | if (isemptyp(rpp) || ! isemptyp(rpc)) return(EINVAL);
|
---|
39 |
|
---|
40 | /* Copy parent 'proc' struct to child. And reinitialize some fields. */
|
---|
41 | gen = _ENDPOINT_G(rpc->p_endpoint);
|
---|
42 | #if (CHIP == INTEL)
|
---|
43 | old_ldt_sel = rpc->p_ldt_sel; /* backup local descriptors */
|
---|
44 | *rpc = *rpp; /* copy 'proc' struct */
|
---|
45 | rpc->p_ldt_sel = old_ldt_sel; /* restore descriptors */
|
---|
46 | #else
|
---|
47 | *rpc = *rpp; /* copy 'proc' struct */
|
---|
48 | #endif
|
---|
49 | if(++gen >= _ENDPOINT_MAX_GENERATION) /* increase generation */
|
---|
50 | gen = 1; /* generation number wraparound */
|
---|
51 | rpc->p_nr = m_ptr->PR_SLOT; /* this was obliterated by copy */
|
---|
52 | rpc->p_endpoint = _ENDPOINT(gen, rpc->p_nr); /* new endpoint of slot */
|
---|
53 |
|
---|
54 | /* Only one in group should have SIGNALED, child doesn't inherit tracing. */
|
---|
55 | rpc->p_rts_flags |= NO_MAP; /* inhibit process from running */
|
---|
56 | rpc->p_rts_flags &= ~(SIGNALED | SIG_PENDING | P_STOP);
|
---|
57 | sigemptyset(&rpc->p_pending);
|
---|
58 |
|
---|
59 | rpc->p_reg.retreg = 0; /* child sees pid = 0 to know it is child */
|
---|
60 | rpc->p_user_time = 0; /* set all the accounting times to 0 */
|
---|
61 | rpc->p_sys_time = 0;
|
---|
62 |
|
---|
63 | /* Parent and child have to share the quantum that the forked process had,
|
---|
64 | * so that queued processes do not have to wait longer because of the fork.
|
---|
65 | * If the time left is odd, the child gets an extra tick.
|
---|
66 | */
|
---|
67 | rpc->p_ticks_left = (rpc->p_ticks_left + 1) / 2;
|
---|
68 | rpp->p_ticks_left = rpp->p_ticks_left / 2;
|
---|
69 |
|
---|
70 | /* If the parent is a privileged process, take away the privileges from the
|
---|
71 | * child process and inhibit it from running by setting the NO_PRIV flag.
|
---|
72 | * The caller should explicitely set the new privileges before executing.
|
---|
73 | */
|
---|
74 | if (priv(rpp)->s_flags & SYS_PROC) {
|
---|
75 | rpc->p_priv = priv_addr(USER_PRIV_ID);
|
---|
76 | rpc->p_rts_flags |= NO_PRIV;
|
---|
77 | }
|
---|
78 |
|
---|
79 | /* Calculate endpoint identifier, so caller knows what it is. */
|
---|
80 | m_ptr->PR_ENDPT = rpc->p_endpoint;
|
---|
81 |
|
---|
82 | return(OK);
|
---|
83 | }
|
---|
84 |
|
---|
85 | #endif /* USE_FORK */
|
---|
86 |
|
---|