1 /* This file handles the EXEC system call. It performs the work as follows:
2 * - see if the permissions allow the file to be executed
3 * - read the header and extract the sizes
4 * - fetch the initial args and environment from the user space
5 * - allocate the memory for the new process
6 * - copy the initial stack from PM to the process
7 * - read in the text and data segments and copy to the process
8 * - take care of setuid and setgid bits
9 * - fix up 'mproc' table
10 * - tell kernel about EXEC
11 * - save offset to initial argc (for ps)
13 * The entry points into this file are:
14 * do_exec: perform the EXEC system call
15 * do_exec_newmem: allocate new memory map for a process that tries to exec
16 * do_execrestart: finish the special exec call for RS
17 * exec_restart: finish a regular exec call
18 * find_share: find a process whose text segment can be shared
23 #include <minix/callnr.h>
24 #include <minix/endpoint.h>
25 #include <minix/com.h>
30 #include <sys/ptrace.h>
34 #define ESCRIPT (-2000) /* Returned by read_header for a #! script. */
35 #define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
37 /*===========================================================================*
39 *===========================================================================*/
44 /* Forward call to FS */
46 m
.PM_PROC
= mp
->mp_endpoint
;
47 m
.PM_PATH
= m_in
.exec_name
;
48 m
.PM_PATH_LEN
= m_in
.exec_len
;
49 m
.PM_FRAME
= m_in
.stack_ptr
;
50 m
.PM_FRAME_LEN
= m_in
.stack_bytes
;
59 /*===========================================================================*
61 *===========================================================================*/
62 PUBLIC
int do_exec_newmem()
64 int proc_e
, proc_n
, allow_setuid
;
67 struct exec_newmem args
;
71 if (who_e
!= FS_PROC_NR
&& who_e
!= RS_PROC_NR
)
74 proc_e
= m_in
.EXC_NM_PROC
;
75 if (pm_isokendpt(proc_e
, &proc_n
) != OK
) {
76 panic("do_exec_newmem: got bad endpoint: %d", proc_e
);
80 r
= sys_datacopy(who_e
, (vir_bytes
)ptr
,
81 SELF
, (vir_bytes
)&args
, sizeof(args
));
83 panic("do_exec_newmem: sys_datacopy failed: %d", r
);
85 if((r
=vm_exec_newmem(proc_e
, &args
, sizeof(args
), &stack_top
, &flags
)) == OK
) {
86 allow_setuid
= 0; /* Do not allow setuid execution */
88 if (rmp
->mp_tracer
== NO_TRACER
) {
89 /* Okay, setuid execution is allowed */
91 rmp
->mp_effuid
= args
.new_uid
;
92 rmp
->mp_effgid
= args
.new_gid
;
95 /* System will save command line for debugging, ps(1) output, etc. */
96 strncpy(rmp
->mp_name
, args
.progname
, PROC_NAME_LEN
-1);
97 rmp
->mp_name
[PROC_NAME_LEN
-1] = '\0';
99 /* Save offset to initial argc (for ps) */
100 rmp
->mp_procargs
= (vir_bytes
) stack_top
- args
.args_bytes
;
102 /* Kill process if something goes wrong after this point. */
103 rmp
->mp_flags
|= PARTIAL_EXEC
;
105 mp
->mp_reply
.reply_res2
= (vir_bytes
) stack_top
;
106 mp
->mp_reply
.reply_res3
= flags
;
108 mp
->mp_reply
.reply_res3
|= EXC_NM_RF_ALLOW_SETUID
;
110 printf("PM: newmem failed for %s\n", args
.progname
);
115 /*===========================================================================*
117 *===========================================================================*/
118 PUBLIC
int do_execrestart()
120 int proc_e
, proc_n
, result
;
123 if (who_e
!= RS_PROC_NR
)
126 proc_e
= m_in
.EXC_RS_PROC
;
127 if (pm_isokendpt(proc_e
, &proc_n
) != OK
) {
128 panic("do_execrestart: got bad endpoint: %d", proc_e
);
131 result
= m_in
.EXC_RS_RESULT
;
133 exec_restart(rmp
, result
);
139 /*===========================================================================*
141 *===========================================================================*/
142 PUBLIC
void exec_restart(rmp
, result
)
152 if (rmp
->mp_flags
& PARTIAL_EXEC
)
154 /* Use SIGKILL to signal that something went wrong */
155 sys_kill(rmp
->mp_endpoint
, SIGKILL
);
158 setreply(rmp
-mproc
, result
);
162 rmp
->mp_flags
&= ~PARTIAL_EXEC
;
164 /* Fix 'mproc' fields, tell kernel that exec is done, reset caught
167 for (sn
= 1; sn
< _NSIG
; sn
++) {
168 if (sigismember(&rmp
->mp_catch
, sn
)) {
169 sigdelset(&rmp
->mp_catch
, sn
);
170 rmp
->mp_sigact
[sn
].sa_handler
= SIG_DFL
;
171 sigemptyset(&rmp
->mp_sigact
[sn
].sa_mask
);
175 /* Cause a signal if this process is traced.
176 * Do this before making the process runnable again!
178 if (rmp
->mp_tracer
!= NO_TRACER
&& !(rmp
->mp_trace_flags
& TO_NOEXEC
))
180 sn
= (rmp
->mp_trace_flags
& TO_ALTEXEC
) ? SIGSTOP
: SIGTRAP
;
182 check_sig(rmp
->mp_pid
, sn
, FALSE
/* ksig */);
185 new_sp
= (char *)rmp
->mp_procargs
;
187 r
= sys_exec(rmp
->mp_endpoint
, new_sp
, rmp
->mp_name
, pc
);
188 if (r
!= OK
) panic("sys_exec failed: %d", r
);