Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/wait.h>
  6. #include <time.h>
  7.  
  8. #define maxCommandLength 80
  9. #define maxCommands 100
  10. #define maxArgs 10
  11. char command[maxCommands][maxCommandLength];
  12. int commandPID[maxCommands];
  13. int commandTimeStamp[maxCommands];
  14. char * args[maxArgs];
  15.  
  16. void execCommand(char * input){
  17. char * inputCopy = input;
  18. char * commandToExec = strtok(inputCopy, " ");
  19. int totalArgs = 0;
  20.  
  21. //Parse Arguments into local list for exec
  22. for(int argCount = 0; argCount < maxArgs; argCount++){
  23. char * currentArg = strtok(inputCopy, " ");
  24. if(currentArg != NULL){
  25. args[argCount] = currentArg;
  26. }
  27. else{
  28. args[argCount] = "\0";
  29. totalArgs = argCount;
  30. break;
  31. }
  32. }
  33.  
  34. execvp(commandToExec, args);
  35. printf("Execv: %s\n", commandToExec);
  36. for(int i = 0; i < totalArgs; i++){
  37. printf("Arguments: %s\n", args[i]);
  38. }
  39. }
  40.  
  41.  
  42. int main(int argc, char **argv) {
  43. if(argc == 1){
  44. printf("No commands provided");
  45. return(-1);
  46. }
  47. //Initialization
  48. int commandCurrentCounter = 0;
  49. int commandTotal = 0;
  50.  
  51. //loop to initialize command array
  52. for(int i = 0; i < maxCommands; i++){
  53. command[i][0] = '\0';
  54. }
  55. //Loop to parse argvs into command array
  56. for(int i = 0; i < argc; i++){
  57. char *cur_command = command[commandCurrentCounter];
  58. if(argv[i][0] != '.'){
  59. strcat(cur_command, argv[i]);
  60. strcat(cur_command, " ");
  61. }
  62. else{ //is a dot, terminating the command
  63. if(strlen(cur_command) > 0){
  64. commandCurrentCounter++;
  65. }
  66. }
  67. }
  68. if(strlen(command[commandCurrentCounter]) == 0){
  69. commandTotal = commandCurrentCounter;
  70. }
  71. else{
  72. commandTotal = commandCurrentCounter + 1;
  73. }
  74.  
  75. //Print out Command Array Debug
  76. for(int i = 0; i < commandTotal; i++){
  77. printf("command[%d] = \"%s\"\n", i, command[i]);
  78. }
  79.  
  80. //Issue Commands
  81. for(int i = 0; i < commandTotal; i++){
  82. pid_t pid;
  83. printf("About to exec: %s\n", command[i]);
  84. pid = fork();
  85. if(pid == 0){
  86. //Exec function in here
  87. execCommand(command[i]);
  88. }
  89. else{
  90. commandPID[i] = pid;
  91. clock_t begin = clock();
  92. commandTimeStamp[i] = begin;
  93. }
  94. }
  95.  
  96. //Wait and Restart Loop
  97. int activeChildren = commandTotal;
  98. printf("active children: %d\n", activeChildren);
  99. while(activeChildren > 0){
  100. siginfo_t childInformation;
  101. waitid(P_ALL, 1, &childInformation, WEXITED || WSTOPPED);
  102. if(childInformation.si_code == CLD_EXITED){ //If child successfully completed
  103. activeChildren--;
  104. for(int i = 0; i < maxCommands; i++){
  105. if(commandPID[i] == childInformation.si_pid){
  106. commandPID[i] = -1;
  107. break;
  108. }
  109. }
  110. }
  111. else{ //Error code, check for timestamp on whether to restart
  112. int failedCommand = 0;
  113. clock_t beginChildTime;
  114. for(int i = 0; i < maxCommands; i++){
  115. if(commandPID[i] == childInformation.si_pid){
  116. beginChildTime = commandTimeStamp[i];
  117. failedCommand = i;
  118. break;
  119. }
  120. }
  121. //Check to see if 2 seconds have passed
  122. clock_t difference = clock() - beginChildTime;
  123. long seconds = difference / CLOCKS_PER_SEC;
  124. if(seconds > 2){
  125. //Fork and exec the command that failed and reset timer
  126. pid_t pid = fork();
  127. if(pid == 0){
  128. execCommand(command[failedCommand]);
  129. }
  130. else{
  131. commandPID[failedCommand] = pid;
  132. clock_t begin = clock();
  133. commandTimeStamp[failedCommand] = begin;
  134. }
  135. }
  136. else{
  137. printf("Spawning too fast!\n");
  138. activeChildren--;
  139. for(int i = 0; i < maxCommands; i++){
  140. if(commandPID[i] == childInformation.si_pid){
  141. commandPID[i] = -1;
  142. break;
  143. }
  144. }
  145. }
  146. }
  147. }
  148.  
  149.  
  150. return 0;
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement