Make a deep copy of envp before starting a new process.
This commit is contained in:
parent
c10e10b1fb
commit
dc1e5eaf0e
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@
|
||||||
#
|
#
|
||||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
ESP_SIZE_MIB = 50
|
ESP_SIZE_MIB = 350
|
||||||
QEMU = qemu-system-x86_64
|
QEMU = qemu-system-x86_64
|
||||||
QEMU_KVM = -enable-kvm
|
QEMU_KVM = -enable-kvm
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,7 @@ struct process {
|
||||||
int forked;
|
int forked;
|
||||||
int fd_map[__FILEDES_MAX];
|
int fd_map[__FILEDES_MAX];
|
||||||
int argc;
|
int argc;
|
||||||
|
int envc;
|
||||||
char **argv;
|
char **argv;
|
||||||
char **envp;
|
char **envp;
|
||||||
};
|
};
|
||||||
|
@ -94,8 +95,9 @@ void* entry_point(char* raw_elf)
|
||||||
|
|
||||||
void jump(void* start_address, int argc, char** argv, char** envp)
|
void jump(void* start_address, int argc, char** argv, char** envp)
|
||||||
{
|
{
|
||||||
|
/* Make a copy of argv */
|
||||||
current_process->argc = argc;
|
current_process->argc = argc;
|
||||||
current_process->argv = calloc(argc + 1, sizeof(char*));
|
current_process->argv = calloc(argc + 1, sizeof(char *));
|
||||||
int i;
|
int i;
|
||||||
size_t length;
|
size_t length;
|
||||||
for (i = 0; i < argc; i += 1) {
|
for (i = 0; i < argc; i += 1) {
|
||||||
|
@ -105,10 +107,24 @@ void jump(void* start_address, int argc, char** argv, char** envp)
|
||||||
}
|
}
|
||||||
current_process->argv[argc] = 0;
|
current_process->argv[argc] = 0;
|
||||||
|
|
||||||
|
/* Make a copy of envp */
|
||||||
|
current_process->envc = 0;
|
||||||
|
char** e = envp;
|
||||||
|
for (; *e != 0; e += sizeof(char *)) {
|
||||||
|
current_process->envc += 1;
|
||||||
|
}
|
||||||
|
current_process->envp = calloc(current_process->envc + 1, sizeof(char *));
|
||||||
|
for (i = 0; i < current_process->envc; i += 1) {
|
||||||
|
length = strlen(envp[i]) + 1;
|
||||||
|
current_process->envp[i] = malloc(length);
|
||||||
|
memcpy(current_process->envp[i], envp[i], length);
|
||||||
|
}
|
||||||
|
current_process->envp[current_process->envc] = 0;
|
||||||
|
|
||||||
|
/* Prepare stack of the new executable */
|
||||||
current_process->stack = get_stack();
|
current_process->stack = get_stack();
|
||||||
asm("push !0");
|
for (i = current_process->envc; i >= 0; i -= 1) {
|
||||||
for (; *envp != 0; envp += sizeof(char *)) {
|
current_process->envp[i];
|
||||||
*envp;
|
|
||||||
asm("push_rax");
|
asm("push_rax");
|
||||||
}
|
}
|
||||||
for (i = argc; i >= 0; i -= 1) {
|
for (i = argc; i >= 0; i -= 1) {
|
||||||
|
@ -259,6 +275,10 @@ void sys_exit(unsigned value, void, void, void, void, void)
|
||||||
free(current_process->argv[i]);
|
free(current_process->argv[i]);
|
||||||
}
|
}
|
||||||
free(current_process->argv);
|
free(current_process->argv);
|
||||||
|
for (i = 0; i < current_process->envc; i += 1) {
|
||||||
|
free(current_process->envp[i]);
|
||||||
|
}
|
||||||
|
free(current_process->envp);
|
||||||
|
|
||||||
if (current_process->parent == NULL) {
|
if (current_process->parent == NULL) {
|
||||||
exit(value);
|
exit(value);
|
||||||
|
|
Loading…
Reference in New Issue