panic() cleanup.
[minix.git] / servers / pm / exec.c
blobd464ad21cdbcf886f543a80acb0f7868a60b913b
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
21 #include "pm.h"
22 #include <sys/stat.h>
23 #include <minix/callnr.h>
24 #include <minix/endpoint.h>
25 #include <minix/com.h>
26 #include <minix/vm.h>
27 #include <a.out.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <sys/ptrace.h>
31 #include "mproc.h"
32 #include "param.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 /*===========================================================================*
38 * do_exec *
39 *===========================================================================*/
40 PUBLIC int do_exec()
42 message m;
43 int r;
45 /* Forward call to FS */
46 m.m_type = PM_EXEC;
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;
53 tell_fs(mp, &m);
55 /* Do not reply */
56 return SUSPEND;
60 /*===========================================================================*
61 * do_exec_newmem *
62 *===========================================================================*/
63 PUBLIC int do_exec_newmem()
65 int proc_e, proc_n, allow_setuid;
66 char *ptr;
67 struct mproc *rmp;
68 struct exec_newmem args;
69 int r, flags;
70 char *stack_top;
72 if (who_e != FS_PROC_NR && who_e != RS_PROC_NR)
73 return EPERM;
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);
79 rmp= &mproc[proc_n];
80 ptr= m_in.EXC_NM_PTR;
81 r= sys_datacopy(who_e, (vir_bytes)ptr,
82 SELF, (vir_bytes)&args, sizeof(args));
83 if (r != OK)
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 */
91 allow_setuid= 1;
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;
108 if (allow_setuid)
109 mp->mp_reply.reply_res3 |= EXC_NM_RF_ALLOW_SETUID;
110 } else {
111 printf("PM: newmem failed for %s\n", args.progname);
113 return r;
116 /*===========================================================================*
117 * do_execrestart *
118 *===========================================================================*/
119 PUBLIC int do_execrestart()
121 int proc_e, proc_n, result;
122 struct mproc *rmp;
124 if (who_e != RS_PROC_NR)
125 return EPERM;
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);
131 rmp= &mproc[proc_n];
132 result= m_in.EXC_RS_RESULT;
134 exec_restart(rmp, result);
136 return OK;
140 /*===========================================================================*
141 * exec_restart *
142 *===========================================================================*/
143 PUBLIC void exec_restart(rmp, result)
144 struct mproc *rmp;
145 int result;
147 int r, sn;
148 vir_bytes pc;
149 char *new_sp;
151 if (result != OK)
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*/);
158 return;
160 setreply(rmp-mproc, result);
161 return;
164 rmp->mp_flags &= ~PARTIAL_EXEC;
166 /* Fix 'mproc' fields, tell kernel that exec is done, reset caught
167 * sigs.
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;
188 pc= 0; /* for now */
189 r= sys_exec(rmp->mp_endpoint, new_sp, rmp->mp_name, pc);
190 if (r != OK) panic("sys_exec failed: %d", r);