27 lines
848 B
C
27 lines
848 B
C
// 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[] = { "./callee", "first_arg", "second_arg", NULL };
|
|
|
|
// Environment variables for the new program
|
|
char *envp[] = { "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;
|
|
}
|