tty: don't use custom kputc; this fixes tty printf()s.
[minix.git] / servers / pm / exec.c
blob350d0e6f7105c98bc341a9895c1d959bc51b5707
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;
44 /* Forward call to FS */
45 m.m_type = PM_EXEC;
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;
52 tell_fs(mp, &m);
54 /* Do not reply */
55 return SUSPEND;
59 /*===========================================================================*
60 * do_exec_newmem *
61 *===========================================================================*/
62 PUBLIC int do_exec_newmem()
64 int proc_e, proc_n, allow_setuid;
65 char *ptr;
66 struct mproc *rmp;
67 struct exec_newmem args;
68 int r, flags;
69 char *stack_top;
71 if (who_e != FS_PROC_NR && who_e != RS_PROC_NR)
72 return EPERM;
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);
78 rmp= &mproc[proc_n];
79 ptr= m_in.EXC_NM_PTR;
80 r= sys_datacopy(who_e, (vir_bytes)ptr,
81 SELF, (vir_bytes)&args, sizeof(args));
82 if (r != OK)
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 */
90 allow_setuid= 1;
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;
107 if (allow_setuid)
108 mp->mp_reply.reply_res3 |= EXC_NM_RF_ALLOW_SETUID;
109 } else {
110 printf("PM: newmem failed for %s\n", args.progname);
112 return r;
115 /*===========================================================================*
116 * do_execrestart *
117 *===========================================================================*/
118 PUBLIC int do_execrestart()
120 int proc_e, proc_n, result;
121 struct mproc *rmp;
123 if (who_e != RS_PROC_NR)
124 return EPERM;
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);
130 rmp= &mproc[proc_n];
131 result= m_in.EXC_RS_RESULT;
133 exec_restart(rmp, result);
135 return OK;
139 /*===========================================================================*
140 * exec_restart *
141 *===========================================================================*/
142 PUBLIC void exec_restart(rmp, result)
143 struct mproc *rmp;
144 int result;
146 int r, sn;
147 vir_bytes pc;
148 char *new_sp;
150 if (result != OK)
152 if (rmp->mp_flags & PARTIAL_EXEC)
154 /* Use SIGKILL to signal that something went wrong */
155 sys_kill(rmp->mp_endpoint, SIGKILL);
156 return;
158 setreply(rmp-mproc, result);
159 return;
162 rmp->mp_flags &= ~PARTIAL_EXEC;
164 /* Fix 'mproc' fields, tell kernel that exec is done, reset caught
165 * sigs.
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;
186 pc= 0; /* for now */
187 r= sys_exec(rmp->mp_endpoint, new_sp, rmp->mp_name, pc);
188 if (r != OK) panic("sys_exec failed: %d", r);