1433d6423SLionel Sambuc /* This file handles the EXEC system call. It performs the work as follows:
2433d6423SLionel Sambuc * - see if the permissions allow the file to be executed
3433d6423SLionel Sambuc * - read the header and extract the sizes
4433d6423SLionel Sambuc * - fetch the initial args and environment from the user space
5433d6423SLionel Sambuc * - allocate the memory for the new process
6433d6423SLionel Sambuc * - copy the initial stack from PM to the process
7433d6423SLionel Sambuc * - read in the text and data segments and copy to the process
8433d6423SLionel Sambuc * - take care of setuid and setgid bits
9433d6423SLionel Sambuc * - fix up 'mproc' table
10433d6423SLionel Sambuc * - tell kernel about EXEC
11433d6423SLionel Sambuc * - save offset to initial argc (for ps)
12433d6423SLionel Sambuc *
13433d6423SLionel Sambuc * The entry points into this file are:
14433d6423SLionel Sambuc * pm_exec: perform the EXEC system call
15433d6423SLionel Sambuc */
16433d6423SLionel Sambuc
17433d6423SLionel Sambuc #include "fs.h"
18433d6423SLionel Sambuc #include <sys/stat.h>
19433d6423SLionel Sambuc #include <sys/mman.h>
20433d6423SLionel Sambuc #include <minix/callnr.h>
21433d6423SLionel Sambuc #include <minix/endpoint.h>
22433d6423SLionel Sambuc #include <minix/com.h>
23433d6423SLionel Sambuc #include <minix/u64.h>
24594df55eSDavid van Moolenbroek #include <lib.h>
25433d6423SLionel Sambuc #include <signal.h>
26433d6423SLionel Sambuc #include <stdlib.h>
27433d6423SLionel Sambuc #include <string.h>
28433d6423SLionel Sambuc #include <sys/dirent.h>
29433d6423SLionel Sambuc #include <sys/exec.h>
30433d6423SLionel Sambuc #include <sys/param.h>
31433d6423SLionel Sambuc #include "path.h"
32433d6423SLionel Sambuc #include "vnode.h"
33433d6423SLionel Sambuc #include "file.h"
34433d6423SLionel Sambuc #include <minix/vfsif.h>
35433d6423SLionel Sambuc #include <machine/vmparam.h>
36433d6423SLionel Sambuc #include <assert.h>
37433d6423SLionel Sambuc #include <fcntl.h>
38433d6423SLionel Sambuc
39433d6423SLionel Sambuc #define _KERNEL /* for ELF_AUX_ENTRIES */
40433d6423SLionel Sambuc #include <libexec.h>
41433d6423SLionel Sambuc
42433d6423SLionel Sambuc /* fields only used by elf and in VFS */
43433d6423SLionel Sambuc struct vfs_exec_info {
44433d6423SLionel Sambuc struct exec_info args; /* libexec exec args */
45433d6423SLionel Sambuc struct vnode *vp; /* Exec file's vnode */
46433d6423SLionel Sambuc struct vmnt *vmp; /* Exec file's vmnt */
47433d6423SLionel Sambuc struct stat sb; /* Exec file's stat structure */
48433d6423SLionel Sambuc int userflags; /* exec() flags from userland */
49433d6423SLionel Sambuc int is_dyn; /* Dynamically linked executable */
50433d6423SLionel Sambuc int elf_main_fd; /* Dyn: FD of main program execuatble */
51433d6423SLionel Sambuc char execname[PATH_MAX]; /* Full executable invocation */
52433d6423SLionel Sambuc int vmfd;
53433d6423SLionel Sambuc int vmfd_used;
54433d6423SLionel Sambuc };
55433d6423SLionel Sambuc
56433d6423SLionel Sambuc static int patch_stack(struct vnode *vp, char stack[ARG_MAX],
57433d6423SLionel Sambuc size_t *stk_bytes, char path[PATH_MAX], vir_bytes *vsp);
58433d6423SLionel Sambuc static int is_script(struct vfs_exec_info *execi);
59433d6423SLionel Sambuc static int insert_arg(char stack[ARG_MAX], size_t *stk_bytes, char *arg,
60433d6423SLionel Sambuc vir_bytes *vsp, char replace);
61433d6423SLionel Sambuc static void clo_exec(struct fproc *rfp);
62433d6423SLionel Sambuc static int stack_prepare_elf(struct vfs_exec_info *execi,
63433d6423SLionel Sambuc char *curstack, size_t *frame_len, vir_bytes *vsp);
64433d6423SLionel Sambuc static int map_header(struct vfs_exec_info *execi);
65433d6423SLionel Sambuc static int read_seg(struct exec_info *execi, off_t off, vir_bytes seg_addr, size_t seg_bytes);
66433d6423SLionel Sambuc
67433d6423SLionel Sambuc #define PTRSIZE sizeof(char *) /* Size of pointers in argv[] and envp[]. */
68433d6423SLionel Sambuc
69433d6423SLionel Sambuc /* Array of loaders for different object file formats */
70433d6423SLionel Sambuc typedef int (*exechook_t)(struct vfs_exec_info *execpackage);
71433d6423SLionel Sambuc typedef int (*stackhook_t)(struct vfs_exec_info *execi, char *curstack,
72433d6423SLionel Sambuc size_t *frame_len, vir_bytes *vsp);
73433d6423SLionel Sambuc struct exec_loaders {
74433d6423SLionel Sambuc libexec_exec_loadfunc_t load_object; /* load executable into memory */
75433d6423SLionel Sambuc stackhook_t setup_stack; /* prepare stack before argc and argv push */
76433d6423SLionel Sambuc };
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc static const struct exec_loaders exec_loaders[] = {
79433d6423SLionel Sambuc { libexec_load_elf, stack_prepare_elf },
80433d6423SLionel Sambuc { NULL, NULL }
81433d6423SLionel Sambuc };
82433d6423SLionel Sambuc
83433d6423SLionel Sambuc #define lock_exec() lock_proc(fproc_addr(VM_PROC_NR))
84433d6423SLionel Sambuc #define unlock_exec() unlock_proc(fproc_addr(VM_PROC_NR))
85433d6423SLionel Sambuc
86433d6423SLionel Sambuc /*===========================================================================*
87433d6423SLionel Sambuc * get_read_vp *
88433d6423SLionel Sambuc *===========================================================================*/
get_read_vp(struct vfs_exec_info * execi,char * fullpath,int copyprogname,int sugid,struct lookup * resolve,struct fproc * fp)89433d6423SLionel Sambuc static int get_read_vp(struct vfs_exec_info *execi,
90433d6423SLionel Sambuc char *fullpath, int copyprogname, int sugid, struct lookup *resolve, struct fproc *fp)
91433d6423SLionel Sambuc {
92433d6423SLionel Sambuc /* Make the executable that we want to exec() into the binary pointed
93433d6423SLionel Sambuc * to by 'fullpath.' This function fills in necessary details in the execi
94433d6423SLionel Sambuc * structure, such as opened vnode. It unlocks and releases the vnode if
95433d6423SLionel Sambuc * it was already there. This makes it easy to change the executable
96433d6423SLionel Sambuc * during the exec(), which is often necessary, by calling this function
97433d6423SLionel Sambuc * more than once. This is specifically necessary when we discover the
98433d6423SLionel Sambuc * executable is actually a script or a dynamically linked executable.
99433d6423SLionel Sambuc */
100433d6423SLionel Sambuc int r;
101433d6423SLionel Sambuc
102433d6423SLionel Sambuc /* Caller wants to switch vp to the file in 'fullpath.'
103433d6423SLionel Sambuc * unlock and put it first if there is any there.
104433d6423SLionel Sambuc */
105433d6423SLionel Sambuc if(execi->vp) {
106433d6423SLionel Sambuc unlock_vnode(execi->vp);
107433d6423SLionel Sambuc put_vnode(execi->vp);
108433d6423SLionel Sambuc execi->vp = NULL;
109433d6423SLionel Sambuc }
110433d6423SLionel Sambuc
111433d6423SLionel Sambuc /* Remember/overwrite the executable name if requested. */
112433d6423SLionel Sambuc if(copyprogname) {
113433d6423SLionel Sambuc char *cp = strrchr(fullpath, '/');
114433d6423SLionel Sambuc if(cp) cp++;
115433d6423SLionel Sambuc else cp = fullpath;
116433d6423SLionel Sambuc strlcpy(execi->args.progname, cp, sizeof(execi->args.progname));
117433d6423SLionel Sambuc execi->args.progname[sizeof(execi->args.progname)-1] = '\0';
118433d6423SLionel Sambuc }
119433d6423SLionel Sambuc
120433d6423SLionel Sambuc /* Open executable */
121433d6423SLionel Sambuc if ((execi->vp = eat_path(resolve, fp)) == NULL)
122433d6423SLionel Sambuc return err_code;
123433d6423SLionel Sambuc
124433d6423SLionel Sambuc unlock_vmnt(execi->vmp);
125433d6423SLionel Sambuc
126433d6423SLionel Sambuc if (!S_ISREG(execi->vp->v_mode))
127433d6423SLionel Sambuc return ENOEXEC;
128433d6423SLionel Sambuc else if ((r = forbidden(fp, execi->vp, X_BIT)) != OK)
129433d6423SLionel Sambuc return r;
130433d6423SLionel Sambuc else
131433d6423SLionel Sambuc r = req_stat(execi->vp->v_fs_e, execi->vp->v_inode_nr,
132433d6423SLionel Sambuc VFS_PROC_NR, (vir_bytes) &(execi->sb));
133433d6423SLionel Sambuc
134433d6423SLionel Sambuc if (r != OK) return r;
135433d6423SLionel Sambuc
136433d6423SLionel Sambuc /* If caller wants us to, honour suid/guid mode bits. */
137433d6423SLionel Sambuc if (sugid) {
138433d6423SLionel Sambuc /* Deal with setuid/setgid executables */
139433d6423SLionel Sambuc if (execi->vp->v_mode & I_SET_UID_BIT) {
140433d6423SLionel Sambuc execi->args.new_uid = execi->vp->v_uid;
141433d6423SLionel Sambuc execi->args.allow_setuid = 1;
142433d6423SLionel Sambuc }
143433d6423SLionel Sambuc if (execi->vp->v_mode & I_SET_GID_BIT) {
144433d6423SLionel Sambuc execi->args.new_gid = execi->vp->v_gid;
145433d6423SLionel Sambuc execi->args.allow_setuid = 1;
146433d6423SLionel Sambuc }
147433d6423SLionel Sambuc }
148433d6423SLionel Sambuc
149433d6423SLionel Sambuc /* Read in first chunk of file. */
150433d6423SLionel Sambuc if((r=map_header(execi)) != OK)
151433d6423SLionel Sambuc return r;
152433d6423SLionel Sambuc
153433d6423SLionel Sambuc return OK;
154433d6423SLionel Sambuc }
155433d6423SLionel Sambuc
156433d6423SLionel Sambuc #define FAILCHECK(expr) if((r=(expr)) != OK) { goto pm_execfinal; } while(0)
157433d6423SLionel Sambuc #define Get_read_vp(e,f,p,s,rs,fp) do { \
158433d6423SLionel Sambuc r=get_read_vp(&e,f,p,s,rs,fp); if(r != OK) { FAILCHECK(r); } \
159433d6423SLionel Sambuc } while(0)
160433d6423SLionel Sambuc
vfs_memmap(struct exec_info * execi,vir_bytes vaddr,vir_bytes len,vir_bytes foffset,u16_t clearend,int protflags)161433d6423SLionel Sambuc static int vfs_memmap(struct exec_info *execi,
162433d6423SLionel Sambuc vir_bytes vaddr, vir_bytes len, vir_bytes foffset, u16_t clearend,
163433d6423SLionel Sambuc int protflags)
164433d6423SLionel Sambuc {
165433d6423SLionel Sambuc struct vfs_exec_info *vi = (struct vfs_exec_info *) execi->opaque;
166433d6423SLionel Sambuc struct vnode *vp = ((struct vfs_exec_info *) execi->opaque)->vp;
167433d6423SLionel Sambuc int r;
168433d6423SLionel Sambuc u16_t flags = 0;
169433d6423SLionel Sambuc
170433d6423SLionel Sambuc if(protflags & PROT_WRITE)
171433d6423SLionel Sambuc flags |= MVM_WRITABLE;
172433d6423SLionel Sambuc
173433d6423SLionel Sambuc r = minix_vfs_mmap(execi->proc_e, foffset, len,
174433d6423SLionel Sambuc vp->v_dev, vp->v_inode_nr, vi->vmfd, vaddr, clearend, flags);
175433d6423SLionel Sambuc if(r == OK) {
176433d6423SLionel Sambuc vi->vmfd_used = 1;
177433d6423SLionel Sambuc }
178433d6423SLionel Sambuc
179433d6423SLionel Sambuc return r;
180433d6423SLionel Sambuc }
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc /*===========================================================================*
183433d6423SLionel Sambuc * pm_exec *
184433d6423SLionel Sambuc *===========================================================================*/
pm_exec(vir_bytes path,size_t path_len,vir_bytes frame,size_t frame_len,vir_bytes * pc,vir_bytes * newsp,vir_bytes * UNUSED (ps_str))185433d6423SLionel Sambuc int pm_exec(vir_bytes path, size_t path_len, vir_bytes frame, size_t frame_len,
186433d6423SLionel Sambuc vir_bytes *pc, vir_bytes *newsp, vir_bytes *UNUSED(ps_str))
187433d6423SLionel Sambuc {
188433d6423SLionel Sambuc /* Perform the execve(name, argv, envp) call. The user library builds a
189433d6423SLionel Sambuc * complete stack image, including pointers, args, environ, etc. The stack
190433d6423SLionel Sambuc * is copied to a buffer inside VFS, and then to the new core image.
191433d6423SLionel Sambuc *
192433d6423SLionel Sambuc * ps_str is not currently used, but may be if the ps_strings structure has to
193433d6423SLionel Sambuc * be moved to another location.
194433d6423SLionel Sambuc */
195433d6423SLionel Sambuc int r;
196433d6423SLionel Sambuc vir_bytes vsp;
197433d6423SLionel Sambuc static char mbuf[ARG_MAX]; /* buffer for stack and zeroes */
198433d6423SLionel Sambuc struct vfs_exec_info execi;
199433d6423SLionel Sambuc int i;
200433d6423SLionel Sambuc static char fullpath[PATH_MAX],
201433d6423SLionel Sambuc elf_interpreter[PATH_MAX],
202433d6423SLionel Sambuc firstexec[PATH_MAX],
203433d6423SLionel Sambuc finalexec[PATH_MAX];
204433d6423SLionel Sambuc struct lookup resolve;
205433d6423SLionel Sambuc struct fproc *vmfp = fproc_addr(VM_PROC_NR);
206433d6423SLionel Sambuc stackhook_t makestack = NULL;
207433d6423SLionel Sambuc struct filp *newfilp = NULL;
208433d6423SLionel Sambuc
209433d6423SLionel Sambuc lock_exec();
210433d6423SLionel Sambuc
211433d6423SLionel Sambuc /* unset execi values are 0. */
212433d6423SLionel Sambuc memset(&execi, 0, sizeof(execi));
213433d6423SLionel Sambuc execi.vmfd = -1;
214433d6423SLionel Sambuc
215433d6423SLionel Sambuc /* passed from exec() libc code */
216433d6423SLionel Sambuc execi.userflags = 0;
21720054ae9SDavid van Moolenbroek execi.args.stack_high = minix_get_user_sp();
218433d6423SLionel Sambuc execi.args.stack_size = DEFAULT_STACK_LIMIT;
219433d6423SLionel Sambuc
220433d6423SLionel Sambuc lookup_init(&resolve, fullpath, PATH_NOFLAGS, &execi.vmp, &execi.vp);
221433d6423SLionel Sambuc
222433d6423SLionel Sambuc resolve.l_vmnt_lock = VMNT_READ;
223433d6423SLionel Sambuc resolve.l_vnode_lock = VNODE_READ;
224433d6423SLionel Sambuc
225433d6423SLionel Sambuc /* Fetch the stack from the user before destroying the old core image. */
226433d6423SLionel Sambuc if (frame_len > ARG_MAX)
227433d6423SLionel Sambuc FAILCHECK(ENOMEM); /* stack too big */
228433d6423SLionel Sambuc
229433d6423SLionel Sambuc r = sys_datacopy_wrapper(fp->fp_endpoint, (vir_bytes) frame, SELF, (vir_bytes) mbuf,
230433d6423SLionel Sambuc (size_t) frame_len);
231433d6423SLionel Sambuc if (r != OK) { /* can't fetch stack (e.g. bad virtual addr) */
232433d6423SLionel Sambuc printf("VFS: pm_exec: sys_datacopy failed\n");
233433d6423SLionel Sambuc FAILCHECK(r);
234433d6423SLionel Sambuc }
235433d6423SLionel Sambuc
236433d6423SLionel Sambuc /* Compute the current virtual stack pointer, has to be done before calling
237433d6423SLionel Sambuc * patch_stack, which needs it, and will adapt as required. */
238433d6423SLionel Sambuc vsp = execi.args.stack_high - frame_len;
239433d6423SLionel Sambuc
240433d6423SLionel Sambuc /* The default is to keep the original user and group IDs */
241433d6423SLionel Sambuc execi.args.new_uid = fp->fp_effuid;
242433d6423SLionel Sambuc execi.args.new_gid = fp->fp_effgid;
243433d6423SLionel Sambuc
244433d6423SLionel Sambuc /* Get the exec file name. */
245433d6423SLionel Sambuc FAILCHECK(fetch_name(path, path_len, fullpath));
246433d6423SLionel Sambuc strlcpy(finalexec, fullpath, PATH_MAX);
247433d6423SLionel Sambuc strlcpy(firstexec, fullpath, PATH_MAX);
248433d6423SLionel Sambuc
249433d6423SLionel Sambuc /* Get_read_vp will return an opened vn in execi.
250433d6423SLionel Sambuc * if necessary it releases the existing vp so we can
251433d6423SLionel Sambuc * switch after we find out what's inside the file.
252433d6423SLionel Sambuc * It reads the start of the file.
253433d6423SLionel Sambuc */
254433d6423SLionel Sambuc Get_read_vp(execi, fullpath, 1, 1, &resolve, fp);
255433d6423SLionel Sambuc
256433d6423SLionel Sambuc /* If this is a script (i.e. has a #!/interpreter line),
257433d6423SLionel Sambuc * retrieve the name of the interpreter and open that
258433d6423SLionel Sambuc * executable instead.
259433d6423SLionel Sambuc */
260433d6423SLionel Sambuc if(is_script(&execi)) {
261433d6423SLionel Sambuc /* patch_stack will add interpreter name and
262433d6423SLionel Sambuc * args to stack and retrieve the new binary
263433d6423SLionel Sambuc * name into fullpath.
264433d6423SLionel Sambuc */
265433d6423SLionel Sambuc FAILCHECK(fetch_name(path, path_len, fullpath));
266433d6423SLionel Sambuc FAILCHECK(patch_stack(execi.vp, mbuf, &frame_len, fullpath, &vsp));
267433d6423SLionel Sambuc
268433d6423SLionel Sambuc strlcpy(finalexec, fullpath, PATH_MAX);
269433d6423SLionel Sambuc strlcpy(firstexec, fullpath, PATH_MAX);
270433d6423SLionel Sambuc Get_read_vp(execi, fullpath, 1, 0, &resolve, fp);
271433d6423SLionel Sambuc }
272433d6423SLionel Sambuc
273433d6423SLionel Sambuc /* If this is a dynamically linked executable, retrieve
274433d6423SLionel Sambuc * the name of that interpreter in elf_interpreter and open that
275433d6423SLionel Sambuc * executable instead. But open the current executable in an
276433d6423SLionel Sambuc * fd for the current process.
277433d6423SLionel Sambuc */
2780a6a1f1dSLionel Sambuc r = elf_has_interpreter(execi.args.hdr, execi.args.hdr_len,
2790a6a1f1dSLionel Sambuc elf_interpreter, sizeof(elf_interpreter));
2800a6a1f1dSLionel Sambuc if (0 > r)
2810a6a1f1dSLionel Sambuc FAILCHECK(r);
2820a6a1f1dSLionel Sambuc
2830a6a1f1dSLionel Sambuc if (0 < r) {
284433d6423SLionel Sambuc /* Switch the executable vnode to the interpreter */
285433d6423SLionel Sambuc execi.is_dyn = 1;
286433d6423SLionel Sambuc
287433d6423SLionel Sambuc /* The interpreter (loader) needs an fd to the main program,
288433d6423SLionel Sambuc * which is currently in finalexec
289433d6423SLionel Sambuc */
29056ac45c1SDavid van Moolenbroek if ((r = execi.elf_main_fd =
29156ac45c1SDavid van Moolenbroek common_open(finalexec, O_RDONLY, 0, TRUE /*for_exec*/)) < 0) {
292433d6423SLionel Sambuc printf("VFS: exec: dynamic: open main exec failed %s (%d)\n",
293433d6423SLionel Sambuc fullpath, r);
294433d6423SLionel Sambuc FAILCHECK(r);
295433d6423SLionel Sambuc }
296433d6423SLionel Sambuc
297433d6423SLionel Sambuc /* ld.so is linked at 0, but it can relocate itself; we
298433d6423SLionel Sambuc * want it higher to trap NULL pointer dereferences.
299433d6423SLionel Sambuc * Let's put it below the stack, and reserve 10MB for ld.so.
300433d6423SLionel Sambuc */
301433d6423SLionel Sambuc execi.args.load_offset =
302433d6423SLionel Sambuc execi.args.stack_high - execi.args.stack_size - 0xa00000;
303433d6423SLionel Sambuc
304433d6423SLionel Sambuc /* Remember it */
305433d6423SLionel Sambuc strlcpy(execi.execname, finalexec, PATH_MAX);
306433d6423SLionel Sambuc
307433d6423SLionel Sambuc /* The executable we need to execute first (loader)
308433d6423SLionel Sambuc * is in elf_interpreter, and has to be in fullpath to
309433d6423SLionel Sambuc * be looked up
310433d6423SLionel Sambuc */
311433d6423SLionel Sambuc strlcpy(fullpath, elf_interpreter, PATH_MAX);
312433d6423SLionel Sambuc strlcpy(firstexec, elf_interpreter, PATH_MAX);
313433d6423SLionel Sambuc Get_read_vp(execi, fullpath, 0, 0, &resolve, fp);
314433d6423SLionel Sambuc }
315433d6423SLionel Sambuc
316433d6423SLionel Sambuc /* We also want an FD for VM to mmap() the process in if possible. */
317433d6423SLionel Sambuc {
318433d6423SLionel Sambuc struct vnode *vp = execi.vp;
319433d6423SLionel Sambuc assert(vp);
320433d6423SLionel Sambuc if ((vp->v_vmnt->m_fs_flags & RES_HASPEEK) &&
321433d6423SLionel Sambuc major(vp->v_dev) != MEMORY_MAJOR) {
322433d6423SLionel Sambuc int newfd = -1;
323433d6423SLionel Sambuc if(get_fd(vmfp, 0, R_BIT, &newfd, &newfilp) == OK) {
324433d6423SLionel Sambuc assert(newfd >= 0 && newfd < OPEN_MAX);
325433d6423SLionel Sambuc assert(!vmfp->fp_filp[newfd]);
326433d6423SLionel Sambuc newfilp->filp_count = 1;
327433d6423SLionel Sambuc newfilp->filp_vno = vp;
328433d6423SLionel Sambuc newfilp->filp_flags = O_RDONLY;
329433d6423SLionel Sambuc vmfp->fp_filp[newfd] = newfilp;
330433d6423SLionel Sambuc /* dup_vnode(vp); */
331433d6423SLionel Sambuc execi.vmfd = newfd;
332433d6423SLionel Sambuc execi.args.memmap = vfs_memmap;
333433d6423SLionel Sambuc }
334433d6423SLionel Sambuc }
335433d6423SLionel Sambuc }
336433d6423SLionel Sambuc
337433d6423SLionel Sambuc /* callback functions and data */
338433d6423SLionel Sambuc execi.args.copymem = read_seg;
339433d6423SLionel Sambuc execi.args.clearproc = libexec_clearproc_vm_procctl;
340433d6423SLionel Sambuc execi.args.clearmem = libexec_clear_sys_memset;
341433d6423SLionel Sambuc execi.args.allocmem_prealloc_cleared = libexec_alloc_mmap_prealloc_cleared;
342433d6423SLionel Sambuc execi.args.allocmem_prealloc_junk = libexec_alloc_mmap_prealloc_junk;
343433d6423SLionel Sambuc execi.args.allocmem_ondemand = libexec_alloc_mmap_ondemand;
344433d6423SLionel Sambuc execi.args.opaque = &execi;
345433d6423SLionel Sambuc
346433d6423SLionel Sambuc execi.args.proc_e = fp->fp_endpoint;
347433d6423SLionel Sambuc execi.args.frame_len = frame_len;
348433d6423SLionel Sambuc execi.args.filesize = execi.vp->v_size;
349433d6423SLionel Sambuc
350433d6423SLionel Sambuc for (i = 0; exec_loaders[i].load_object != NULL; i++) {
351433d6423SLionel Sambuc r = (*exec_loaders[i].load_object)(&execi.args);
352433d6423SLionel Sambuc /* Loaded successfully, so no need to try other loaders */
353433d6423SLionel Sambuc if (r == OK) { makestack = exec_loaders[i].setup_stack; break; }
354433d6423SLionel Sambuc }
355433d6423SLionel Sambuc
356433d6423SLionel Sambuc FAILCHECK(r);
357433d6423SLionel Sambuc
358433d6423SLionel Sambuc /* Inform PM */
359433d6423SLionel Sambuc FAILCHECK(libexec_pm_newexec(fp->fp_endpoint, &execi.args));
360433d6423SLionel Sambuc
361433d6423SLionel Sambuc /* Save off PC */
362433d6423SLionel Sambuc *pc = execi.args.pc;
363433d6423SLionel Sambuc
364433d6423SLionel Sambuc /* call a stack-setup function if this executable type wants it */
365433d6423SLionel Sambuc if(makestack) FAILCHECK(makestack(&execi, mbuf, &frame_len, &vsp));
366433d6423SLionel Sambuc
367433d6423SLionel Sambuc /* Copy the stack from VFS to new core image. */
368433d6423SLionel Sambuc FAILCHECK(sys_datacopy_wrapper(SELF, (vir_bytes) mbuf, fp->fp_endpoint,
369433d6423SLionel Sambuc (vir_bytes) vsp, (phys_bytes)frame_len));
370433d6423SLionel Sambuc
371433d6423SLionel Sambuc /* Return new stack pointer to caller */
372433d6423SLionel Sambuc *newsp = vsp;
373433d6423SLionel Sambuc
374433d6423SLionel Sambuc clo_exec(fp);
375433d6423SLionel Sambuc
376433d6423SLionel Sambuc if (execi.args.allow_setuid) {
377433d6423SLionel Sambuc /* If after loading the image we're still allowed to run with
378433d6423SLionel Sambuc * setuid or setgid, change credentials now */
379433d6423SLionel Sambuc fp->fp_effuid = execi.args.new_uid;
380433d6423SLionel Sambuc fp->fp_effgid = execi.args.new_gid;
381433d6423SLionel Sambuc }
382433d6423SLionel Sambuc
383433d6423SLionel Sambuc /* Remember the new name of the process */
384433d6423SLionel Sambuc strlcpy(fp->fp_name, execi.args.progname, PROC_NAME_LEN);
385433d6423SLionel Sambuc
386433d6423SLionel Sambuc pm_execfinal:
387433d6423SLionel Sambuc if(newfilp) unlock_filp(newfilp);
388433d6423SLionel Sambuc else if (execi.vp != NULL) {
389433d6423SLionel Sambuc unlock_vnode(execi.vp);
390433d6423SLionel Sambuc put_vnode(execi.vp);
391433d6423SLionel Sambuc }
392433d6423SLionel Sambuc
393433d6423SLionel Sambuc if(execi.vmfd >= 0 && !execi.vmfd_used) {
394*491d647aSDavid van Moolenbroek if(OK != close_fd(vmfp, execi.vmfd, FALSE /*may_suspend*/)) {
395433d6423SLionel Sambuc printf("VFS: unexpected close fail of vm fd\n");
396433d6423SLionel Sambuc }
397433d6423SLionel Sambuc }
398433d6423SLionel Sambuc
399433d6423SLionel Sambuc unlock_exec();
400433d6423SLionel Sambuc
401433d6423SLionel Sambuc return(r);
402433d6423SLionel Sambuc }
403433d6423SLionel Sambuc
404433d6423SLionel Sambuc /* This is a copy-paste of the same macro in minix/lib/libc/sys/stack_utils.c.
405433d6423SLionel Sambuc * Keep it synchronized. */
406433d6423SLionel Sambuc #define STACK_MIN_SZ \
407433d6423SLionel Sambuc ( \
408433d6423SLionel Sambuc sizeof(int) + sizeof(void *) * 2 + \
409433d6423SLionel Sambuc sizeof(AuxInfo) * PMEF_AUXVECTORS + PMEF_EXECNAMELEN1 + \
410433d6423SLionel Sambuc sizeof(struct ps_strings) \
411433d6423SLionel Sambuc )
412433d6423SLionel Sambuc
stack_prepare_elf(struct vfs_exec_info * execi,char * frame,size_t * frame_size,vir_bytes * vsp)413433d6423SLionel Sambuc static int stack_prepare_elf(struct vfs_exec_info *execi, char *frame, size_t *frame_size,
414433d6423SLionel Sambuc vir_bytes *vsp)
415433d6423SLionel Sambuc {
416433d6423SLionel Sambuc AuxInfo *aux_vec, *aux_vec_end;
417433d6423SLionel Sambuc vir_bytes vap; /* Address in proc space of the first AuxVec. */
418433d6423SLionel Sambuc Elf_Ehdr const * const elf_header = (Elf_Ehdr *) execi->args.hdr;
419433d6423SLionel Sambuc struct ps_strings const * const psp = (struct ps_strings *)
420433d6423SLionel Sambuc (frame + (*frame_size - sizeof(struct ps_strings)));
421433d6423SLionel Sambuc
422433d6423SLionel Sambuc size_t const execname_len = strlen(execi->execname);
423433d6423SLionel Sambuc
424433d6423SLionel Sambuc if (!execi->is_dyn)
425433d6423SLionel Sambuc return OK;
426433d6423SLionel Sambuc
427433d6423SLionel Sambuc if (execi->args.hdr_len < sizeof(*elf_header)) {
428433d6423SLionel Sambuc printf("VFS: malformed ELF headers for exec\n");
429433d6423SLionel Sambuc return ENOEXEC;
430433d6423SLionel Sambuc }
431433d6423SLionel Sambuc
432433d6423SLionel Sambuc if (*frame_size < STACK_MIN_SZ) {
433433d6423SLionel Sambuc printf("VFS: malformed stack for exec(), smaller than minimum"
434433d6423SLionel Sambuc " possible size.\n");
435433d6423SLionel Sambuc return ENOEXEC;
436433d6423SLionel Sambuc }
437433d6423SLionel Sambuc
438433d6423SLionel Sambuc /* Find first Aux vector in the stack frame. */
439433d6423SLionel Sambuc vap = (vir_bytes)(psp->ps_envstr + (psp->ps_nenvstr + 1));
440433d6423SLionel Sambuc aux_vec = (AuxInfo *) (frame + (vap - *vsp));
441433d6423SLionel Sambuc aux_vec_end = aux_vec + PMEF_AUXVECTORS;
442433d6423SLionel Sambuc
443433d6423SLionel Sambuc if (((char *)aux_vec < frame) ||
444433d6423SLionel Sambuc ((char *)aux_vec > (frame + *frame_size))) {
445433d6423SLionel Sambuc printf("VFS: malformed stack for exec(), first AuxVector is"
446433d6423SLionel Sambuc " not on the stack.\n");
447433d6423SLionel Sambuc return ENOEXEC;
448433d6423SLionel Sambuc }
449433d6423SLionel Sambuc
450433d6423SLionel Sambuc if (((char *)aux_vec_end < frame) ||
451433d6423SLionel Sambuc ((char *)aux_vec_end > (frame + *frame_size))) {
452433d6423SLionel Sambuc printf("VFS: malformed stack for exec(), last AuxVector is"
453433d6423SLionel Sambuc " not on the stack.\n");
454433d6423SLionel Sambuc return ENOEXEC;
455433d6423SLionel Sambuc }
456433d6423SLionel Sambuc
457433d6423SLionel Sambuc /* Userland provides a fully filled stack frame, with argc, argv, envp
458433d6423SLionel Sambuc * and then all the argv and envp strings; consistent with ELF ABI,
459433d6423SLionel Sambuc * except for a list of Aux vectors that should be between envp points
460433d6423SLionel Sambuc * and the start of the strings.
461433d6423SLionel Sambuc *
462433d6423SLionel Sambuc * It would take some very unpleasant hackery to insert the aux vectors
463433d6423SLionel Sambuc * before the strings, and correct all the pointers, so the exec code
464433d6423SLionel Sambuc * in libc makes space for us.
465433d6423SLionel Sambuc */
466433d6423SLionel Sambuc
467433d6423SLionel Sambuc #define AUXINFO(a, type, value) \
468433d6423SLionel Sambuc do { \
469433d6423SLionel Sambuc if (a < aux_vec_end) { \
470433d6423SLionel Sambuc a->a_type = type; \
471433d6423SLionel Sambuc a->a_v = value; \
472433d6423SLionel Sambuc a++; \
473433d6423SLionel Sambuc } else { \
474433d6423SLionel Sambuc printf("VFS: No more room for ELF AuxVec type %d, skipping it for %s\n", type, execi->execname); \
475433d6423SLionel Sambuc (aux_vec_end - 1)->a_type = AT_NULL; \
476433d6423SLionel Sambuc (aux_vec_end - 1)->a_v = 0; \
477433d6423SLionel Sambuc } \
478433d6423SLionel Sambuc } while(0)
479433d6423SLionel Sambuc
480433d6423SLionel Sambuc AUXINFO(aux_vec, AT_BASE, execi->args.load_base);
481433d6423SLionel Sambuc AUXINFO(aux_vec, AT_ENTRY, execi->args.pc);
482433d6423SLionel Sambuc AUXINFO(aux_vec, AT_EXECFD, execi->elf_main_fd);
483433d6423SLionel Sambuc #if 0
484433d6423SLionel Sambuc AUXINFO(aux_vec, AT_PHDR, XXX ); /* should be &phdr[0] */
485433d6423SLionel Sambuc AUXINFO(aux_vec, AT_PHENT, elf_header->e_phentsize);
486433d6423SLionel Sambuc AUXINFO(aux_vec, AT_PHNUM, elf_header->e_phnum);
487433d6423SLionel Sambuc
488433d6423SLionel Sambuc AUXINFO(aux_vec, AT_RUID, XXX);
489433d6423SLionel Sambuc AUXINFO(aux_vec, AT_RGID, XXX);
490433d6423SLionel Sambuc #endif
491433d6423SLionel Sambuc AUXINFO(aux_vec, AT_EUID, execi->args.new_uid);
492433d6423SLionel Sambuc AUXINFO(aux_vec, AT_EGID, execi->args.new_gid);
493433d6423SLionel Sambuc AUXINFO(aux_vec, AT_PAGESZ, PAGE_SIZE);
494433d6423SLionel Sambuc
495433d6423SLionel Sambuc if(execname_len < PMEF_EXECNAMELEN1) {
496433d6423SLionel Sambuc char *spacestart;
497433d6423SLionel Sambuc vir_bytes userp;
498433d6423SLionel Sambuc
499433d6423SLionel Sambuc /* Empty space starts after aux_vec table; we can put the name
500433d6423SLionel Sambuc * here. */
501433d6423SLionel Sambuc spacestart = (char *) aux_vec + 2 * sizeof(AuxInfo);
502433d6423SLionel Sambuc strlcpy(spacestart, execi->execname, PMEF_EXECNAMELEN1);
503433d6423SLionel Sambuc memset(spacestart + execname_len, '\0',
504433d6423SLionel Sambuc PMEF_EXECNAMELEN1 - execname_len);
505433d6423SLionel Sambuc
506433d6423SLionel Sambuc /* What will the address of the string for the user be */
507433d6423SLionel Sambuc userp = *vsp + (spacestart - frame);
508433d6423SLionel Sambuc
509433d6423SLionel Sambuc /* Move back to where the AT_NULL is */
510433d6423SLionel Sambuc AUXINFO(aux_vec, AT_SUN_EXECNAME, userp);
511433d6423SLionel Sambuc }
512433d6423SLionel Sambuc
513433d6423SLionel Sambuc /* Always terminate with AT_NULL */
514433d6423SLionel Sambuc AUXINFO(aux_vec, AT_NULL, 0);
515433d6423SLionel Sambuc
516433d6423SLionel Sambuc return OK;
517433d6423SLionel Sambuc }
518433d6423SLionel Sambuc
519433d6423SLionel Sambuc /*===========================================================================*
520433d6423SLionel Sambuc * is_script *
521433d6423SLionel Sambuc *===========================================================================*/
is_script(struct vfs_exec_info * execi)522433d6423SLionel Sambuc static int is_script(struct vfs_exec_info *execi)
523433d6423SLionel Sambuc {
524433d6423SLionel Sambuc /* Is Interpreted script? */
525433d6423SLionel Sambuc assert(execi->args.hdr != NULL);
526433d6423SLionel Sambuc
527433d6423SLionel Sambuc return(execi->args.hdr[0] == '#' && execi->args.hdr[1] == '!'
528433d6423SLionel Sambuc && execi->args.hdr_len >= 2);
529433d6423SLionel Sambuc }
530433d6423SLionel Sambuc
531433d6423SLionel Sambuc /*===========================================================================*
532433d6423SLionel Sambuc * patch_stack *
533433d6423SLionel Sambuc *===========================================================================*/
patch_stack(vp,stack,stk_bytes,path,vsp)534433d6423SLionel Sambuc static int patch_stack(vp, stack, stk_bytes, path, vsp)
535433d6423SLionel Sambuc struct vnode *vp; /* pointer for open script file */
536433d6423SLionel Sambuc char stack[ARG_MAX]; /* pointer to stack image within VFS */
537433d6423SLionel Sambuc size_t *stk_bytes; /* size of initial stack */
538433d6423SLionel Sambuc char path[PATH_MAX]; /* path to script file */
539433d6423SLionel Sambuc vir_bytes *vsp;
540433d6423SLionel Sambuc {
541433d6423SLionel Sambuc /* Patch the argument vector to include the path name of the script to be
542433d6423SLionel Sambuc * interpreted, and all strings on the #! line. Returns the path name of
543433d6423SLionel Sambuc * the interpreter.
544433d6423SLionel Sambuc */
545433d6423SLionel Sambuc enum { INSERT=FALSE, REPLACE=TRUE };
546433d6423SLionel Sambuc int n, r;
547433d6423SLionel Sambuc off_t pos, new_pos;
548433d6423SLionel Sambuc char *sp, *interp = NULL;
5493c8950ccSBen Gras size_t cum_io;
550433d6423SLionel Sambuc char buf[PAGE_SIZE];
551433d6423SLionel Sambuc
552433d6423SLionel Sambuc /* Make 'path' the new argv[0]. */
553433d6423SLionel Sambuc if (!insert_arg(stack, stk_bytes, path, vsp, REPLACE)) return(ENOMEM);
554433d6423SLionel Sambuc
555433d6423SLionel Sambuc pos = 0; /* Read from the start of the file */
556433d6423SLionel Sambuc
557433d6423SLionel Sambuc /* Issue request */
558433d6423SLionel Sambuc r = req_readwrite(vp->v_fs_e, vp->v_inode_nr, pos, READING, VFS_PROC_NR,
559433d6423SLionel Sambuc (vir_bytes) buf, sizeof(buf), &new_pos, &cum_io);
560433d6423SLionel Sambuc
561433d6423SLionel Sambuc if (r != OK) return(r);
562433d6423SLionel Sambuc
563433d6423SLionel Sambuc n = vp->v_size;
564433d6423SLionel Sambuc if (n > sizeof(buf))
565433d6423SLionel Sambuc n = sizeof(buf);
566433d6423SLionel Sambuc if (n < 2) return ENOEXEC;
567433d6423SLionel Sambuc
568433d6423SLionel Sambuc sp = &(buf[2]); /* just behind the #! */
569433d6423SLionel Sambuc n -= 2;
570433d6423SLionel Sambuc if (n > PATH_MAX) n = PATH_MAX;
571433d6423SLionel Sambuc
572433d6423SLionel Sambuc /* Use the 'path' variable for temporary storage */
573433d6423SLionel Sambuc memcpy(path, sp, n);
574433d6423SLionel Sambuc
575433d6423SLionel Sambuc if ((sp = memchr(path, '\n', n)) == NULL) /* must be a proper line */
576433d6423SLionel Sambuc return(ENOEXEC);
577433d6423SLionel Sambuc
578433d6423SLionel Sambuc /* Move sp backwards through script[], prepending each string to stack. */
579433d6423SLionel Sambuc for (;;) {
580433d6423SLionel Sambuc /* skip spaces behind argument. */
581433d6423SLionel Sambuc while (sp > path && (*--sp == ' ' || *sp == '\t')) {}
582433d6423SLionel Sambuc if (sp == path) break;
583433d6423SLionel Sambuc
584433d6423SLionel Sambuc sp[1] = 0;
585433d6423SLionel Sambuc /* Move to the start of the argument. */
586433d6423SLionel Sambuc while (sp > path && sp[-1] != ' ' && sp[-1] != '\t') --sp;
587433d6423SLionel Sambuc
588433d6423SLionel Sambuc interp = sp;
589433d6423SLionel Sambuc if (!insert_arg(stack, stk_bytes, sp, vsp, INSERT)) {
590433d6423SLionel Sambuc printf("VFS: patch_stack: insert_arg failed\n");
591433d6423SLionel Sambuc return(ENOMEM);
592433d6423SLionel Sambuc }
593433d6423SLionel Sambuc }
594433d6423SLionel Sambuc
595433d6423SLionel Sambuc if(!interp)
596433d6423SLionel Sambuc return ENOEXEC;
597433d6423SLionel Sambuc
598433d6423SLionel Sambuc if (interp != path)
599433d6423SLionel Sambuc memmove(path, interp, strlen(interp)+1);
600433d6423SLionel Sambuc
601433d6423SLionel Sambuc return(OK);
602433d6423SLionel Sambuc }
603433d6423SLionel Sambuc
604433d6423SLionel Sambuc /*===========================================================================*
605433d6423SLionel Sambuc * insert_arg *
606433d6423SLionel Sambuc *===========================================================================*/
insert_arg(char stack[ARG_MAX],size_t * stk_bytes,char * arg,vir_bytes * vsp,char replace)607433d6423SLionel Sambuc static int insert_arg(char stack[ARG_MAX], size_t *stk_bytes, char *arg,
608433d6423SLionel Sambuc vir_bytes *vsp, char replace)
609433d6423SLionel Sambuc {
610433d6423SLionel Sambuc /* Patch the stack so that arg will become argv[0]. Be careful, the
611433d6423SLionel Sambuc * stack may be filled with garbage, although it normally looks like
612433d6423SLionel Sambuc * this:
613433d6423SLionel Sambuc * nargs argv[0] ... argv[nargs-1] NULL envp[0] ... NULL
614433d6423SLionel Sambuc * followed by the strings "pointed" to by the argv[i] and the envp[i].
615433d6423SLionel Sambuc * The * pointers are in the new process address space.
616433d6423SLionel Sambuc *
617433d6423SLionel Sambuc * Return true iff the operation succeeded.
618433d6423SLionel Sambuc */
619433d6423SLionel Sambuc struct ps_strings *psp;
620433d6423SLionel Sambuc int offset;
621433d6423SLionel Sambuc size_t old_bytes = *stk_bytes;
622433d6423SLionel Sambuc
623433d6423SLionel Sambuc int const arg_len = strlen(arg) + 1;
624433d6423SLionel Sambuc
625433d6423SLionel Sambuc /* Offset to argv[0][0] in the stack frame. */
626433d6423SLionel Sambuc int const a0 = (int)(((char **)stack)[1] - *vsp);
627433d6423SLionel Sambuc
628433d6423SLionel Sambuc /* Check that argv[0] points within the stack frame. */
629433d6423SLionel Sambuc if ((a0 < 0) || (a0 >= old_bytes)) {
630433d6423SLionel Sambuc printf("vfs:: argv[0][] not within stack range!! %i\n", a0);
631433d6423SLionel Sambuc return FALSE;
632433d6423SLionel Sambuc }
633433d6423SLionel Sambuc
634433d6423SLionel Sambuc if (!replace) {
635433d6423SLionel Sambuc /* Prepending arg adds one pointer, one string and a zero byte. */
636433d6423SLionel Sambuc offset = arg_len + PTRSIZE;
637433d6423SLionel Sambuc } else {
638433d6423SLionel Sambuc /* replacing argv[0] with arg adds the difference in length of
639433d6423SLionel Sambuc * the two strings. Make sure we don't go beyond the stack size
640433d6423SLionel Sambuc * when computing the length of the current argv[0]. */
641433d6423SLionel Sambuc offset = arg_len - strnlen(stack + a0, ARG_MAX - a0 - 1);
642433d6423SLionel Sambuc }
643433d6423SLionel Sambuc
644433d6423SLionel Sambuc /* As ps_strings follows the strings, ensure the offset is word aligned. */
645433d6423SLionel Sambuc offset = offset + (PTRSIZE - ((PTRSIZE + offset) % PTRSIZE));
646433d6423SLionel Sambuc
647433d6423SLionel Sambuc /* The stack will grow (or shrink) by offset bytes. */
648433d6423SLionel Sambuc if ((*stk_bytes += offset) > ARG_MAX) {
6493c8950ccSBen Gras printf("vfs:: offset too big!! %zu (max %d)\n", *stk_bytes,
650433d6423SLionel Sambuc ARG_MAX);
651433d6423SLionel Sambuc return FALSE;
652433d6423SLionel Sambuc }
653433d6423SLionel Sambuc
654433d6423SLionel Sambuc /* Reposition the strings by offset bytes */
655433d6423SLionel Sambuc memmove(stack + a0 + offset, stack + a0, old_bytes - a0);
656433d6423SLionel Sambuc
657433d6423SLionel Sambuc /* Put arg in the new space, leaving padding in front of it. */
658433d6423SLionel Sambuc strlcpy(stack + a0 + offset - arg_len, arg, arg_len);
659433d6423SLionel Sambuc
660433d6423SLionel Sambuc if (!replace) {
661433d6423SLionel Sambuc /* Make space for a new argv[0]. */
662433d6423SLionel Sambuc memmove(stack + 2 * PTRSIZE,
663433d6423SLionel Sambuc stack + 1 * PTRSIZE, a0 - 2 * PTRSIZE);
664433d6423SLionel Sambuc
665433d6423SLionel Sambuc ((char **) stack)[0]++; /* nargs++; */
666433d6423SLionel Sambuc }
667433d6423SLionel Sambuc
668433d6423SLionel Sambuc /* set argv[0] correctly */
669433d6423SLionel Sambuc ((char **) stack)[1] = (char *) a0 - arg_len + *vsp;
670433d6423SLionel Sambuc
671433d6423SLionel Sambuc /* Update stack pointer in the process address space. */
672433d6423SLionel Sambuc *vsp -= offset;
673433d6423SLionel Sambuc
674433d6423SLionel Sambuc /* Update argv and envp in ps_strings */
675433d6423SLionel Sambuc psp = (struct ps_strings *) (stack + *stk_bytes - sizeof(struct ps_strings));
676433d6423SLionel Sambuc psp->ps_argvstr -= (offset / PTRSIZE);
677433d6423SLionel Sambuc if (!replace) {
678433d6423SLionel Sambuc psp->ps_nargvstr++;
679433d6423SLionel Sambuc }
680433d6423SLionel Sambuc psp->ps_envstr = psp->ps_argvstr + psp->ps_nargvstr + 1;
681433d6423SLionel Sambuc
682433d6423SLionel Sambuc return TRUE;
683433d6423SLionel Sambuc }
684433d6423SLionel Sambuc
685433d6423SLionel Sambuc /*===========================================================================*
686433d6423SLionel Sambuc * read_seg *
687433d6423SLionel Sambuc *===========================================================================*/
read_seg(struct exec_info * execi,off_t off,vir_bytes seg_addr,size_t seg_bytes)688433d6423SLionel Sambuc static int read_seg(struct exec_info *execi, off_t off, vir_bytes seg_addr, size_t seg_bytes)
689433d6423SLionel Sambuc {
690433d6423SLionel Sambuc /*
691433d6423SLionel Sambuc * The byte count on read is usually smaller than the segment count, because
692433d6423SLionel Sambuc * a segment is padded out to a click multiple, and the data segment is only
693433d6423SLionel Sambuc * partially initialized.
694433d6423SLionel Sambuc */
695433d6423SLionel Sambuc int r;
696433d6423SLionel Sambuc off_t new_pos;
6973c8950ccSBen Gras size_t cum_io;
698433d6423SLionel Sambuc struct vnode *vp = ((struct vfs_exec_info *) execi->opaque)->vp;
699433d6423SLionel Sambuc
700433d6423SLionel Sambuc /* Make sure that the file is big enough */
701433d6423SLionel Sambuc if (off + seg_bytes > LONG_MAX) return(EIO);
702433d6423SLionel Sambuc if ((unsigned long) vp->v_size < off+seg_bytes) return(EIO);
703433d6423SLionel Sambuc
704433d6423SLionel Sambuc if ((r = req_readwrite(vp->v_fs_e, vp->v_inode_nr, off, READING,
705433d6423SLionel Sambuc execi->proc_e, (vir_bytes) seg_addr, seg_bytes,
706433d6423SLionel Sambuc &new_pos, &cum_io)) != OK) {
707433d6423SLionel Sambuc printf("VFS: read_seg: req_readwrite failed (data)\n");
708433d6423SLionel Sambuc return(r);
709433d6423SLionel Sambuc }
710433d6423SLionel Sambuc
711433d6423SLionel Sambuc if (r == OK && cum_io != seg_bytes)
712433d6423SLionel Sambuc printf("VFS: read_seg segment has not been read properly\n");
713433d6423SLionel Sambuc
714433d6423SLionel Sambuc return(r);
715433d6423SLionel Sambuc }
716433d6423SLionel Sambuc
717433d6423SLionel Sambuc
718433d6423SLionel Sambuc /*===========================================================================*
719433d6423SLionel Sambuc * clo_exec *
720433d6423SLionel Sambuc *===========================================================================*/
clo_exec(struct fproc * rfp)721433d6423SLionel Sambuc static void clo_exec(struct fproc *rfp)
722433d6423SLionel Sambuc {
723433d6423SLionel Sambuc /* Files can be marked with the FD_CLOEXEC bit (in fp->fp_cloexec).
724433d6423SLionel Sambuc */
725433d6423SLionel Sambuc int i;
726433d6423SLionel Sambuc
727433d6423SLionel Sambuc /* Check the file desriptors one by one for presence of FD_CLOEXEC. */
728433d6423SLionel Sambuc for (i = 0; i < OPEN_MAX; i++)
729433d6423SLionel Sambuc if ( FD_ISSET(i, &rfp->fp_cloexec_set))
730*491d647aSDavid van Moolenbroek (void) close_fd(rfp, i, FALSE /*may_suspend*/);
731433d6423SLionel Sambuc }
732433d6423SLionel Sambuc
733433d6423SLionel Sambuc /*===========================================================================*
734433d6423SLionel Sambuc * map_header *
735433d6423SLionel Sambuc *===========================================================================*/
map_header(struct vfs_exec_info * execi)736433d6423SLionel Sambuc static int map_header(struct vfs_exec_info *execi)
737433d6423SLionel Sambuc {
738433d6423SLionel Sambuc int r;
7393c8950ccSBen Gras size_t cum_io;
740433d6423SLionel Sambuc off_t pos, new_pos;
7415ba2e6e6SDavid van Moolenbroek /* Assume that header is not larger than a page. Align the buffer reasonably
7425ba2e6e6SDavid van Moolenbroek * well, because libexec casts it to a structure directly and therefore
7435ba2e6e6SDavid van Moolenbroek * expects it to be aligned appropriately. From here we can only guess the
7445ba2e6e6SDavid van Moolenbroek * proper alignment, but 64 bits should work for all versions of ELF..
7455ba2e6e6SDavid van Moolenbroek */
7460a6a1f1dSLionel Sambuc static char hdr[10*PAGE_SIZE] __aligned(8);
747433d6423SLionel Sambuc
748433d6423SLionel Sambuc pos = 0; /* Read from the start of the file */
749433d6423SLionel Sambuc
750433d6423SLionel Sambuc /* How much is sensible to read */
751433d6423SLionel Sambuc execi->args.hdr_len = MIN(execi->vp->v_size, sizeof(hdr));
752433d6423SLionel Sambuc execi->args.hdr = hdr;
753433d6423SLionel Sambuc
754433d6423SLionel Sambuc r = req_readwrite(execi->vp->v_fs_e, execi->vp->v_inode_nr,
755433d6423SLionel Sambuc pos, READING, VFS_PROC_NR, (vir_bytes) hdr,
756433d6423SLionel Sambuc execi->args.hdr_len, &new_pos, &cum_io);
757433d6423SLionel Sambuc if (r != OK) {
758433d6423SLionel Sambuc printf("VFS: exec: map_header: req_readwrite failed\n");
759433d6423SLionel Sambuc return(r);
760433d6423SLionel Sambuc }
761433d6423SLionel Sambuc
762433d6423SLionel Sambuc return(OK);
763433d6423SLionel Sambuc }
764