1*433d6423SLionel Sambuc /* execve() - basic program execution call */
2*433d6423SLionel Sambuc
3*433d6423SLionel Sambuc #include <sys/cdefs.h>
4*433d6423SLionel Sambuc #include "namespace.h"
5*433d6423SLionel Sambuc #include <lib.h>
6*433d6423SLionel Sambuc
7*433d6423SLionel Sambuc #include <unistd.h>
8*433d6423SLionel Sambuc #include <string.h>
9*433d6423SLionel Sambuc #include <stddef.h>
10*433d6423SLionel Sambuc #include <minix/param.h>
11*433d6423SLionel Sambuc #include <sys/exec_elf.h>
12*433d6423SLionel Sambuc #include <sys/exec.h>
13*433d6423SLionel Sambuc
execve(const char * path,char * const * argv,char * const * envp)14*433d6423SLionel Sambuc int execve(const char *path, char * const *argv, char * const *envp)
15*433d6423SLionel Sambuc {
16*433d6423SLionel Sambuc message m;
17*433d6423SLionel Sambuc size_t frame_size = 0; /* Size of the new initial stack. */
18*433d6423SLionel Sambuc int argc = 0; /* Argument count. */
19*433d6423SLionel Sambuc int envc = 0; /* Environment count */
20*433d6423SLionel Sambuc char overflow = 0; /* No overflow yet. */
21*433d6423SLionel Sambuc char *frame;
22*433d6423SLionel Sambuc struct ps_strings *psp;
23*433d6423SLionel Sambuc int vsp = 0; /* (virtual) Stack pointer in new address space. */
24*433d6423SLionel Sambuc
25*433d6423SLionel Sambuc minix_stack_params(path, argv, envp, &frame_size, &overflow,
26*433d6423SLionel Sambuc &argc, &envc);
27*433d6423SLionel Sambuc
28*433d6423SLionel Sambuc /* The party is off if there is an overflow. */
29*433d6423SLionel Sambuc if (overflow) {
30*433d6423SLionel Sambuc errno = E2BIG;
31*433d6423SLionel Sambuc return -1;
32*433d6423SLionel Sambuc }
33*433d6423SLionel Sambuc
34*433d6423SLionel Sambuc /* Allocate space for the stack frame. */
35*433d6423SLionel Sambuc if ((frame = (char *) sbrk(frame_size)) == (char *) -1) {
36*433d6423SLionel Sambuc errno = E2BIG;
37*433d6423SLionel Sambuc return -1;
38*433d6423SLionel Sambuc }
39*433d6423SLionel Sambuc
40*433d6423SLionel Sambuc minix_stack_fill(path, argc, argv, envc, envp, frame_size, frame,
41*433d6423SLionel Sambuc &vsp, &psp);
42*433d6423SLionel Sambuc
43*433d6423SLionel Sambuc /* Clear unused message fields */
44*433d6423SLionel Sambuc memset(&m, 0, sizeof(m));
45*433d6423SLionel Sambuc
46*433d6423SLionel Sambuc /* We can finally make the system call. */
47*433d6423SLionel Sambuc m.m_lc_pm_exec.name = (vir_bytes)path;
48*433d6423SLionel Sambuc m.m_lc_pm_exec.namelen = strlen(path) + 1;
49*433d6423SLionel Sambuc m.m_lc_pm_exec.frame = (vir_bytes)frame;
50*433d6423SLionel Sambuc m.m_lc_pm_exec.framelen = frame_size;
51*433d6423SLionel Sambuc m.m_lc_pm_exec.ps_str = (vir_bytes)(vsp + ((char *)psp - frame));
52*433d6423SLionel Sambuc
53*433d6423SLionel Sambuc (void) _syscall(PM_PROC_NR, PM_EXEC, &m);
54*433d6423SLionel Sambuc
55*433d6423SLionel Sambuc /* Failure, return the memory used for the frame and exit. */
56*433d6423SLionel Sambuc (void) sbrk(-frame_size);
57*433d6423SLionel Sambuc
58*433d6423SLionel Sambuc return -1;
59*433d6423SLionel Sambuc }
60