Last active
October 24, 2024 03:48
-
-
Save peterbean410/18e264f8e994bb9d55818a4da4adef2a to your computer and use it in GitHub Desktop.
Fork and Execute
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
gcc -o fork_execve_example fork_execve_example.c | |
chmod +x ./fork_execve_example | |
chmod +x ./test_script.sh |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <string.h> | |
#include <sys/wait.h> | |
#include <time.h> | |
int main() { | |
// Seed the random number generator with the current time | |
srand(time(0)); | |
// Generate a random value between 0 and RAND_MAX | |
int randomValue = rand(); | |
int randomValue2 = rand(); | |
char *local_var = "IamLocal"; | |
char randomValueStr[40]; | |
snprintf(randomValueStr, sizeof(randomValueStr), "%d-%d", randomValue, randomValue2); | |
// Print the random value as a string | |
printf("Random value as string: %s\n", randomValueStr); | |
// Step 1: Read the environment variable "MY_VAR" | |
char *env_var = getenv("MY_VAR"); | |
if (env_var != NULL) { | |
printf("Original MY_VAR: %s\n", env_var); | |
} else { | |
printf("MY_VAR is not set\n"); | |
} | |
// Step 2: Modify the environment variable value | |
char new_value[256]; | |
snprintf(new_value, sizeof(new_value), "[DEBUG][MY_VAR][VALUE] Modified_%s", env_var ? env_var : "default"); | |
// Step 3: Set the modified environment variable | |
setenv("MY_VAR", new_value, 1); // 1 means overwrite the existing value | |
printf("New MY_VAR: %s\n", getenv("MY_VAR")); | |
// Step 4: Fork the process | |
pid_t pid = fork(); | |
if (pid == -1) { | |
perror("fork failed"); | |
exit(EXIT_FAILURE); | |
} else if (pid == 0) { | |
// Child process | |
printf("Child process is executing ahihi...\n"); | |
printf("....... Check point 00\n"); | |
char *test = "How are you"; | |
printf("....... Check point 01: %s \n", test); | |
char *child_var_1 = getenv("MY_VAR"); | |
printf("....... Check point 02 \n"); | |
printf("....... Check point 03: %s \n", child_var_1); | |
// Step 5: Execute an external shell script using execve | |
char *argv[] = {"./test_script.sh", NULL}; // Arguments for the shell script | |
printf("....... Check point 04 \n"); | |
char *envp[] = {NULL}; // You can pass the modified environment or use the current environment | |
printf(".......... Check point 05 \n"); | |
printf(".......... Check point 06: %s - %s \n", local_var, randomValueStr); | |
char *child_var = getenv("MY_VAR"); | |
if (child_var == NULL) { child_var = "EMPTY"; } | |
printf(".......... MY_VAR retrieved by the forked process: %s \n", child_var); | |
sleep(3); | |
if (execve("./test_script.sh", argv, envp) == -1) { | |
perror("execve failed"); | |
exit(EXIT_FAILURE); | |
} | |
} else { | |
// Parent process | |
int status; | |
waitpid(pid, &status, 0); // Wait for the child process to complete | |
printf("Parent process: Child process finished with status %d\n", status); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
echo "Running test_script.sh" | |
echo "MY_VAR is: $MY_VAR" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment