Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <time.h>
- #define maxCommandLength 80
- #define maxCommands 100
- #define maxArgs 10
- char command[maxCommands][maxCommandLength];
- int commandPID[maxCommands];
- int commandTimeStamp[maxCommands];
- char * args[maxArgs];
- void execCommand(char * input){
- char * inputCopy = input;
- char * commandToExec = strtok(inputCopy, " ");
- //Parse Arguments into local list for exec
- for(int argCount = 0; argCount < maxArgs; argCount++){
- char * currentArg = strtok(inputCopy, " ");
- if(currentArg != NULL){
- args[argCount] = currentArg;
- }
- else{
- args[argCount] = "\0";
- break;
- }
- }
- execv(commandToExec, args);
- //printf("Execv: %s \n", commandToExec);
- }
- int main(int argc, char **argv) {
- if(argc == 1){
- printf("No commands provided");
- return(-1);
- }
- //Initialization
- int commandCurrentCounter = 0;
- int commandTotal = 0;
- //loop to initialize command array
- for(int i = 0; i < maxCommands; i++){
- command[i][0] = '\0';
- }
- //Loop to parse argvs into command array
- for(int i = 0; i < argc; i++){
- char *cur_command = command[commandCurrentCounter];
- if(argv[i][0] != '.'){
- strcat(cur_command, argv[i]);
- strcat(cur_command, " ");
- }
- else{ //is a dot, terminating the command
- if(strlen(cur_command) > 0){
- commandCurrentCounter++;
- }
- }
- }
- if(strlen(command[commandCurrentCounter]) == 0){
- commandTotal = commandCurrentCounter;
- }
- else{
- commandTotal = commandCurrentCounter + 1;
- }
- //Print out Command Array Debug
- for(int i = 0; i < commandTotal; i++){
- printf("command[%d] = \"%s\"\n", i, command[i]);
- }
- //Issue Commands
- for(int i = 0; i < commandTotal; i++){
- pid_t pid;
- printf("About to exec: %s\n", command[i]);
- pid = fork();
- if(pid == 0){
- //Exec function in here
- execCommand(command[i]);
- }
- else{
- commandPID[i] = pid;
- clock_t begin = clock();
- commandTimeStamp[i] = begin;
- }
- }
- //Wait and Restart Loop
- int activeChildren = commandTotal;
- printf("active children: %d\n", activeChildren);
- while(activeChildren > 0){
- siginfo_t childInformation;
- waitid(P_ALL, 1, &childInformation, WEXITED || WSTOPPED);
- if(childInformation.si_code == CLD_EXITED){ //If child successfully completed
- activeChildren--;
- for(int i = 0; i < maxCommands; i++){
- if(commandPID[i] == childInformation.si_pid){
- commandPID[i] = -1;
- break;
- }
- }
- }
- else{ //Error code, check for timestamp on whether to restart
- int failedCommand = 0;
- clock_t beginChildTime;
- for(int i = 0; i < maxCommands; i++){
- if(commandPID[i] == childInformation.si_pid){
- beginChildTime = commandTimeStamp[i];
- failedCommand = i;
- break;
- }
- }
- //Check to see if 2 seconds have passed
- clock_t difference = clock() - beginChildTime;
- long seconds = difference / CLOCKS_PER_SEC;
- if(seconds > 2){
- //Fork and exec the command that failed and reset timer
- pid_t pid = fork();
- if(pid == 0){
- execCommand(command[failedCommand]);
- }
- else{
- commandPID[failedCommand] = pid;
- clock_t begin = clock();
- commandTimeStamp[failedCommand] = begin;
- }
- }
- else{
- printf("Spawning too fast!\n");
- activeChildren--;
- for(int i = 0; i < maxCommands; i++){
- if(commandPID[i] == childInformation.si_pid){
- commandPID[i] = -1;
- break;
- }
- }
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement