cprocs.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* ----- cprocs.c ----- */
  2. struct child_process_info {
  3. int pid;
  4. char state; // 'q'ueued, 'r'unning, 'd'ead
  5. int exit_status;
  6. int pty;
  7. int child_stdin;
  8. int child_stdout;
  9. int child_stderr;
  10. FILE* f_stdin;
  11. FILE* f_stdout;
  12. FILE* f_stderr;
  13. char* output_buffer;
  14. size_t buf_alloc;
  15. size_t buf_len;
  16. };
  17. struct job {
  18. int type; // c = child process, f = function
  19. volatile int state;
  20. union {
  21. int (*fn)(void*);
  22. char* cmd;
  23. };
  24. union {
  25. struct child_process_info* cpi;
  26. void* user_data;
  27. };
  28. };
  29. struct child_process_info* exec_cmdline_pipe(char* cmdline);
  30. struct child_process_info* exec_process_pipe(char* exec_path, char* args[]);
  31. void free_cpi(struct child_process_info* cpi) {
  32. if(cpi->output_buffer) free(cpi->output_buffer);
  33. if(cpi->f_stdin) fclose(cpi->f_stdin);
  34. if(cpi->f_stdout) fclose(cpi->f_stdout);
  35. if(cpi->f_stderr) fclose(cpi->f_stderr);
  36. if(cpi->child_stdin) close(cpi->child_stdin);
  37. if(cpi->child_stdout) close(cpi->child_stdout);
  38. if(cpi->child_stderr) close(cpi->child_stderr);
  39. if(cpi->pty) close(cpi->pty);
  40. free(cpi);
  41. }
  42. void read_cpi(struct child_process_info* cpi) {
  43. while(1) {
  44. if(cpi->buf_len > cpi->buf_alloc - 128) {
  45. cpi->buf_alloc *= 2;
  46. cpi->output_buffer = realloc(cpi->output_buffer, cpi->buf_alloc * sizeof(*cpi->output_buffer));
  47. }
  48. int ret = read(cpi->pty, cpi->output_buffer + cpi->buf_len, cpi->buf_alloc - cpi->buf_len - 1);
  49. if(ret > 0) cpi->buf_len += ret;
  50. else return;
  51. }
  52. return ;
  53. }
  54. char* printpct(float f) {
  55. static char buf[32];
  56. if(f != 100) sprintf(buf, "\e[1;33m %2.0f%%\e[0m", f);
  57. else sprintf(buf, "\e[1;32m100%%\e[0m");
  58. return buf;
  59. }
  60. int execute_mt(strlist* cmds, int threads, char* fmt, struct child_process_info*** cpis) {
  61. int ret = 0;
  62. int running = 0;
  63. struct child_process_info** procs = calloc(1, cmds->len * sizeof(*procs));
  64. if(cpis) *cpis = procs;
  65. if(fmt) printf(fmt, printpct(0)), fflush(stdout);
  66. int waiting = cmds->len;
  67. while(waiting > 0) {
  68. for(int i = 0; i < cmds->len; i++) {
  69. // keep the cores full
  70. if(!procs[i] && running < threads) {
  71. procs[i] = exec_cmdline_pipe(cmds->entries[i]);
  72. procs[i]->state = 'r';
  73. running++;
  74. }
  75. if(!procs[i] || procs[i]->state == 'd') continue;
  76. read_cpi(procs[i]);
  77. int status;
  78. // returns 0 if nothing happened, -1 on error, childpid if it exited
  79. int pid = waitpid(procs[i]->pid, &status, WNOHANG);
  80. if(pid != 0) {
  81. procs[i]->state = 'd';
  82. waiting--;
  83. running--;
  84. if(fmt) printf("\r"), printf(fmt, printpct((cmds->len - waiting) * 100.0f / (float)cmds->len)), fflush(stdout);
  85. read_cpi(procs[i]);
  86. procs[i]->output_buffer[procs[i]->buf_len] = 0;
  87. procs[i]->exit_status = WEXITSTATUS(status);
  88. if(!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  89. // printf("error on pid %d/%d = %d\n", pid, procs[i]->pid, WEXITSTATUS(status));
  90. // printf("%s\n", procs[i]->output_buffer);
  91. ret = 1;
  92. }
  93. }
  94. }
  95. usleep(100);
  96. }
  97. if(fmt) {
  98. printf("\r");
  99. if(ret) printf(fmt, "\e[1;31mFAIL\e[0m");
  100. else printf(fmt, "\e[32mDONE\e[0m");
  101. fflush(stdout);
  102. }
  103. printf("\n");
  104. return ret;
  105. }
  106. struct child_process_info* exec_cmdline_pipe(char* cmdline) {
  107. // kinda janky. it doesn't handle quotes
  108. char** args = strsplit(" ", cmdline);
  109. struct child_process_info* cpi = exec_process_pipe(args[0], args);
  110. // printf("executing '%s'\n", cmdline);
  111. // for(char** s = args; *s; s++) printf("%d - '%s'\n", (s-args), *s);
  112. free_strpp(args);
  113. return cpi;
  114. }
  115. // effectively a better, asynchronous version of system()
  116. // redirects and captures the child process i/o
  117. struct child_process_info* exec_process_pipe(char* exec_path, char* args[]) {
  118. int master, slave; // pty
  119. errno = 0;
  120. if(openpty(&master, &slave, NULL, NULL, NULL) < 0) {
  121. fprintf(stderr, "Error opening new pty for '%s' [errno=%d]\n", exec_path, errno);
  122. return NULL;
  123. }
  124. errno = 0;
  125. int childPID = fork();
  126. if(childPID == -1) {
  127. fprintf(stderr, "failed to fork trying to execute '%s'\n", exec_path);
  128. perror(strerror(errno));
  129. return NULL;
  130. }
  131. else if(childPID == 0) { // child process
  132. setsid();
  133. // redirect standard fd's to the pipe fd's
  134. if(dup2(slave, fileno(stdin)) == -1) {
  135. printf("failed 1\n");
  136. exit(errno);
  137. }
  138. if(dup2(slave, fileno(stdout)) == -1) {
  139. printf("failed 2\n");
  140. exit(errno);
  141. }
  142. if(dup2(slave, fileno(stderr)) == -1) {
  143. printf("failed 3\n");
  144. exit(errno);
  145. }
  146. if(ioctl(slave, TIOCSCTTY, NULL) < 0) {
  147. fprintf(stderr, "ioctl TIOCSCTTY failed: %s, %d\n", exec_path, errno);
  148. }
  149. // close original fd's
  150. close(master);
  151. close(slave);
  152. // die when the parent does (linux only)
  153. prctl(PR_SET_PDEATHSIG, SIGHUP);
  154. // swap for the desired program
  155. execvp(exec_path, args); // never returns if successful
  156. fprintf(stderr, "failed to execute '%s'\n", exec_path);
  157. exit(1); // kill the forked process
  158. }
  159. else { // parent process
  160. // close the child-end of the pipes
  161. struct child_process_info* cpi;
  162. cpi = calloc(1, sizeof(*cpi));
  163. cpi->pid = childPID;
  164. cpi->pty = master;
  165. cpi->state = 'q';
  166. cpi->buf_alloc = 4096;
  167. cpi->output_buffer = malloc(cpi->buf_alloc * sizeof(*cpi->output_buffer));
  168. // set to non-blocking
  169. fcntl(master, F_SETFL, fcntl(master, F_GETFL) | FNDELAY | O_NONBLOCK);
  170. close(slave);
  171. // tcsetattr(STDIN_FILENO, TCSANOW, &master);
  172. // fcntl(master, F_SETFL, FNDELAY);
  173. // int status;
  174. // returns 0 if nothing happened, -1 on error
  175. // pid = waitpid(childPID, &status, WNOHANG);
  176. return cpi;
  177. }
  178. return NULL; // shouldn't reach here
  179. }
  180. /* -END- cprocs.c ----- */