syscalls: implement basic execve syscall, no argc, argv, envp support yet
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
// callee.c
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char *argv[], char *envp[]) {
|
||||
printf("--- In callee.c ---\n");
|
||||
printf("This program has taken over the process.\n");
|
||||
|
||||
printf("argc: %d\n", argc);
|
||||
|
||||
printf("\nArguments (argv):\n");
|
||||
for (int i = 0; argv[i] != NULL; i++) {
|
||||
printf(" argv[%d]: %s\n", i, argv[i]);
|
||||
}
|
||||
|
||||
printf("\nEnvironment Variables (envp):\n");
|
||||
for (int i = 0; envp[i] != NULL; i++) {
|
||||
printf(" %s\n", envp[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
// caller.c
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main() {
|
||||
printf("--- In caller.c ---\n");
|
||||
printf("This message is from the original program.\n");
|
||||
printf("Calling execve to run './callee'...\n\n");
|
||||
|
||||
// Arguments for the new program
|
||||
// The first argument is conventionally the program name
|
||||
char *argv[] = {NULL}; //{ "./callee", "first_arg", "second_arg", NULL };
|
||||
|
||||
// Environment variables for the new program
|
||||
char *envp[] = {NULL}; //{ "CUSTOM_VAR=Hello from caller!", "ANOTHER_VAR=123", NULL };
|
||||
|
||||
// The execve system call
|
||||
// The first argument is the path to the executable
|
||||
// The second is the array of arguments
|
||||
// The third is the array of environment variables
|
||||
execve("callee.", argv, envp);
|
||||
|
||||
// This part of the code will only be reached if execve fails
|
||||
perror("execve failed");
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user