proc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Tiny process service: fake ttys for the browser's curl helpers. */
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <signal.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/prctl.h>
  9. #include <sys/types.h>
  10. #include <sys/wait.h>
  11. #include <unistd.h>
  12. #include "host.h"
  13. #include "fs.h"
  14. #include "proc.h"
  15. #define HOST_TTY_MAX 8
  16. struct host_tty {
  17. int used;
  18. int rfd;
  19. int wfd;
  20. };
  21. static struct host_tty ttys[HOST_TTY_MAX];
  22. int host_tty_create(void)
  23. {
  24. int i;
  25. int fds[2];
  26. for (i = 0; i < HOST_TTY_MAX; i++) {
  27. if (ttys[i].used)
  28. continue;
  29. if (pipe2(fds, O_NONBLOCK | O_CLOEXEC) != 0)
  30. return -1;
  31. ttys[i].used = 1;
  32. ttys[i].rfd = fds[0];
  33. ttys[i].wfd = fds[1];
  34. return i + 1;
  35. }
  36. return -1;
  37. }
  38. int host_tty_read(int id, char *buf, unsigned int max)
  39. {
  40. ssize_t n;
  41. if (id <= 0 || id > HOST_TTY_MAX)
  42. return -1;
  43. id--;
  44. if (!ttys[id].used)
  45. return -1;
  46. n = read(ttys[id].rfd, buf, max);
  47. if (n < 0)
  48. return (errno == EAGAIN || errno == EWOULDBLOCK) ? 0 : -1;
  49. return (int)n;
  50. }
  51. static const char *exec_path(const char *path, char *buf, size_t cap)
  52. {
  53. if (!path || !path[0])
  54. return 0;
  55. if (!strcmp(path, "/ramfs/bin/curl") && host_curl_bin[0])
  56. return host_curl_bin;
  57. if (path[0] == '/') {
  58. if (host_guest_to_host(path, buf, cap) != 0)
  59. return 0;
  60. if (access(buf, X_OK) == 0)
  61. return buf;
  62. }
  63. if (access(path, X_OK) == 0)
  64. return path;
  65. return 0;
  66. }
  67. int host_spawn(const char *path, const char *arg, int tty, const char *cwd)
  68. {
  69. char exe_buf[HOST_PATH_MAX];
  70. char cwd_buf[HOST_PATH_MAX];
  71. const char *exe;
  72. const char *cwd_host = 0;
  73. pid_t pid;
  74. exe = exec_path(path, exe_buf, sizeof(exe_buf));
  75. if (!exe) {
  76. fprintf(stderr, "browser-host: cannot find helper %s\n", path);
  77. return -1;
  78. }
  79. if (cwd && cwd[0] &&
  80. host_guest_to_host(cwd, cwd_buf, sizeof(cwd_buf)) == 0)
  81. cwd_host = cwd_buf;
  82. pid = fork();
  83. if (pid < 0)
  84. return -1;
  85. if (pid == 0) {
  86. prctl(PR_SET_PDEATHSIG, SIGKILL);
  87. if (tty > 0 && tty <= HOST_TTY_MAX &&
  88. ttys[tty - 1].used) {
  89. int wfd = ttys[tty - 1].wfd;
  90. dup2(wfd, STDOUT_FILENO);
  91. dup2(wfd, STDERR_FILENO);
  92. close(ttys[tty - 1].rfd);
  93. if (wfd > STDERR_FILENO)
  94. close(wfd);
  95. }
  96. if (cwd_host)
  97. chdir(cwd_host);
  98. if (arg && arg[0]) {
  99. char *const argv[] = { (char *)exe, (char *)arg, 0 };
  100. execv(exe, argv);
  101. } else {
  102. char *const argv[] = { (char *)exe, 0 };
  103. execv(exe, argv);
  104. }
  105. _exit(127);
  106. }
  107. return (int)pid;
  108. }
  109. int host_alive(unsigned int pid)
  110. {
  111. int status;
  112. if (pid == 0)
  113. return 0;
  114. for (;;) {
  115. pid_t r = waitpid((pid_t)pid, &status, WNOHANG);
  116. if (r == 0)
  117. return 1;
  118. if (r == (pid_t)pid)
  119. return 0;
  120. if (errno == EINTR)
  121. continue;
  122. return 0;
  123. }
  124. }
  125. int host_kill(unsigned int pid)
  126. {
  127. if (pid == 0)
  128. return -1;
  129. return kill((pid_t)pid, SIGKILL) == 0 ? 0 : -1;
  130. }