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 *===========================================================================*/
45 /* Forward call to FS */
47 m
.PM_PROC
= mp
->mp_endpoint
;
48 m
.PM_PATH
= m_in
.exec_name
;
49 m
.PM_PATH_LEN
= m_in
.exec_len
;
50 m
.PM_FRAME
= m_in
.stack_ptr
;
51 m
.PM_FRAME_LEN
= m_in
.stack_bytes
;
60 /*===========================================================================*
62 *===========================================================================*/
63 PUBLIC
int do_exec_newmem()
65 int proc_e
, proc_n
, allow_setuid
;
68 struct exec_newmem args
;
72 if (who_e
!= FS_PROC_NR
&& who_e
!= RS_PROC_NR
)
75 proc_e
= m_in
.EXC_NM_PROC
;
76 if (pm_isokendpt(proc_e
, &proc_n
) != OK
) {
77 panic("do_exec_newmem: got bad endpoint: %d", proc_e
);
81 r
= sys_datacopy(who_e
, (vir_bytes
)ptr
,
82 SELF
, (vir_bytes
)&args
, sizeof(args
));
84 panic("do_exec_newmem: sys_datacopy failed: %d", r
);
86 if((r
=vm_exec_newmem(proc_e
, &args
, sizeof(args
), &stack_top
, &flags
)) == OK
) {
87 allow_setuid
= 0; /* Do not allow setuid execution */
89 if (rmp
->mp_tracer
== NO_TRACER
) {
90 /* Okay, setuid execution is allowed */
92 rmp
->mp_effuid
= args
.new_uid
;
93 rmp
->mp_effgid
= args
.new_gid
;
96 /* System will save command line for debugging, ps(1) output, etc. */
97 strncpy(rmp
->mp_name
, args
.progname
, PROC_NAME_LEN
-1);
98 rmp
->mp_name
[PROC_NAME_LEN
-1] = '\0';
100 /* Save offset to initial argc (for ps) */
101 rmp
->mp_procargs
= (vir_bytes
) stack_top
- args
.args_bytes
;
103 /* Kill process if something goes wrong after this point. */
104 rmp
->mp_flags
|= PARTIAL_EXEC
;
106 mp
->mp_reply
.reply_res2
= (vir_bytes
) stack_top
;
107 mp
->mp_reply
.reply_res3
= flags
;
109 mp
->mp_reply
.reply_res3
|= EXC_NM_RF_ALLOW_SETUID
;
111 printf("PM: newmem failed for %s\n", args
.progname
);
116 /*===========================================================================*
118 *===========================================================================*/
119 PUBLIC
int do_execrestart()
121 int proc_e
, proc_n
, result
;
124 if (who_e
!= RS_PROC_NR
)
127 proc_e
= m_in
.EXC_RS_PROC
;
128 if (pm_isokendpt(proc_e
, &proc_n
) != OK
) {
129 panic("do_execrestart: got bad endpoint: %d", proc_e
);
132 result
= m_in
.EXC_RS_RESULT
;
134 exec_restart(rmp
, result
);
140 /*===========================================================================*
142 *===========================================================================*/
143 PUBLIC
void exec_restart(rmp
, result
)
153 if (rmp
->mp_flags
& PARTIAL_EXEC
)
155 /* Use SIGILL signal that something went wrong */
156 rmp
->mp_sigstatus
= SIGILL
;
157 exit_proc(rmp
, 0, FALSE
/*dump_core*/);
160 setreply(rmp
-mproc
, result
);
164 rmp
->mp_flags
&= ~PARTIAL_EXEC
;
166 /* Fix 'mproc' fields, tell kernel that exec is done, reset caught
169 for (sn
= 1; sn
< _NSIG
; sn
++) {
170 if (sigismember(&rmp
->mp_catch
, sn
)) {
171 sigdelset(&rmp
->mp_catch
, sn
);
172 rmp
->mp_sigact
[sn
].sa_handler
= SIG_DFL
;
173 sigemptyset(&rmp
->mp_sigact
[sn
].sa_mask
);
177 /* Cause a signal if this process is traced.
178 * Do this before making the process runnable again!
180 if (rmp
->mp_tracer
!= NO_TRACER
&& !(rmp
->mp_trace_flags
& TO_NOEXEC
))
182 sn
= (rmp
->mp_trace_flags
& TO_ALTEXEC
) ? SIGSTOP
: SIGTRAP
;
184 check_sig(rmp
->mp_pid
, sn
, FALSE
/* ksig */);
187 new_sp
= (char *)rmp
->mp_procargs
;
189 r
= sys_exec(rmp
->mp_endpoint
, new_sp
, rmp
->mp_name
, pc
);
190 if (r
!= OK
) panic("sys_exec failed: %d", r
);