vfs: pm_dumpcore: always clean up process
[minix.git] / lib / libexec / exec_general.c
blob20d3ca9eed364f6b5666e7fac2a7b86582ff50ad
1 #define _SYSTEM 1
3 #include <minix/type.h>
4 #include <minix/const.h>
5 #include <sys/param.h>
6 #include <assert.h>
7 #include <unistd.h>
8 #include <errno.h>
9 #include <libexec.h>
10 #include <string.h>
11 #include <assert.h>
12 #include <minix/ipc.h>
13 #include <minix/com.h>
14 #include <minix/callnr.h>
15 #include <minix/vm.h>
16 #include <minix/syslib.h>
17 #include <sys/mman.h>
18 #include <machine/elf.h>
20 int libexec_alloc_mmap_prealloc(struct exec_info *execi, off_t vaddr, size_t len)
22 if(minix_mmap_for(execi->proc_e, (void *) vaddr, len,
23 PROT_READ|PROT_WRITE|PROT_EXEC,
24 MAP_ANON|MAP_PREALLOC|MAP_UNINITIALIZED|MAP_FIXED, -1, 0) == MAP_FAILED) {
25 return ENOMEM;
28 return OK;
31 int libexec_alloc_mmap_ondemand(struct exec_info *execi, off_t vaddr, size_t len)
33 if(minix_mmap_for(execi->proc_e, (void *) vaddr, len,
34 PROT_READ|PROT_WRITE|PROT_EXEC,
35 MAP_ANON|MAP_FIXED, -1, 0) == MAP_FAILED) {
36 return ENOMEM;
39 return OK;
42 int libexec_clearproc_vm_procctl(struct exec_info *execi)
44 return vm_procctl(execi->proc_e, VMPPARAM_CLEAR);
47 int libexec_clear_sys_memset(struct exec_info *execi, off_t vaddr, size_t len)
49 return sys_memset(execi->proc_e, 0, vaddr, len);
52 int libexec_copy_memcpy(struct exec_info *execi,
53 off_t off, off_t vaddr, size_t len)
55 assert(off + len <= execi->hdr_len);
56 memcpy((char *) vaddr, (char *) execi->hdr + off, len);
57 return OK;
60 int libexec_clear_memset(struct exec_info *execi, off_t vaddr, size_t len)
62 memset((char *) vaddr, 0, len);
63 return OK;
66 void libexec_patch_ptr(char stack[ARG_MAX], vir_bytes base)
68 /* When doing an exec(name, argv, envp) call, the user builds up a stack
69 * image with arg and env pointers relative to the start of the stack. Now
70 * these pointers must be relocated, since the stack is not positioned at
71 * address 0 in the user's address space.
74 char **ap, flag;
75 vir_bytes v;
77 flag = 0; /* counts number of 0-pointers seen */
78 ap = (char **) stack; /* points initially to 'nargs' */
79 ap++; /* now points to argv[0] */
80 while (flag < 2) {
81 if (ap >= (char **) &stack[ARG_MAX]) return; /* too bad */
82 if (*ap != NULL) {
83 v = (vir_bytes) *ap; /* v is relative pointer */
84 v += base; /* relocate it */
85 *ap = (char *) v; /* put it back */
86 } else {
87 flag++;
89 ap++;
93 int libexec_pm_newexec(endpoint_t proc_e, struct exec_info *e)
95 int r;
96 message m;
98 m.m_type = PM_NEWEXEC;
99 m.EXC_NM_PROC = proc_e;
100 m.EXC_NM_PTR = (char *)e;
101 if ((r = sendrec(PM_PROC_NR, &m)) != OK) return(r);
103 e->allow_setuid = !!(m.m1_i2 & EXC_NM_RF_ALLOW_SETUID);
105 return(m.m_type);