Advertisement
Guest User

Untitled

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