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