sh.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #include <errno.h>
  2. #include <pwd.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <fcntl.h>
  9. #include <termios.h>
  10. #include <sys/mman.h>
  11. #include <sys/stat.h>
  12. #include <sys/utsname.h>
  13. #include <AK/FileSystemPath.h>
  14. struct GlobalState {
  15. String cwd;
  16. String username;
  17. char ttyname[32];
  18. char hostname[32];
  19. pid_t sid;
  20. uid_t uid;
  21. struct termios termios;
  22. bool was_interrupted { false };
  23. };
  24. static GlobalState* g;
  25. static void prompt()
  26. {
  27. if (g->uid == 0)
  28. printf("# ");
  29. else {
  30. printf("\033]0;%s@%s:%s\007", g->username.characters(), g->hostname, g->cwd.characters());
  31. printf("\033[31;1m%s\033[0m@\033[37;1m%s\033[0m:\033[32;1m%s\033[0m$> ", g->username.characters(), g->hostname, g->cwd.characters());
  32. }
  33. fflush(stdout);
  34. }
  35. static int sh_pwd(int, char**)
  36. {
  37. printf("%s\n", g->cwd.characters());
  38. return 0;
  39. }
  40. static volatile bool g_got_signal = false;
  41. void did_receive_signal(int signum)
  42. {
  43. printf("\nMy word, I've received a signal with number %d\n", signum);
  44. g_got_signal = true;
  45. }
  46. void handle_sigint(int)
  47. {
  48. g->was_interrupted = true;
  49. }
  50. static int sh_exit(int, char**)
  51. {
  52. printf("Good-bye!\n");
  53. exit(0);
  54. return 0;
  55. }
  56. static int sh_cd(int argc, char** argv)
  57. {
  58. if (argc == 1) {
  59. printf("usage: cd <path>\n");
  60. return 0;
  61. }
  62. char pathbuf[128];
  63. if (argv[1][0] == '/')
  64. memcpy(pathbuf, argv[1], strlen(argv[1]) + 1);
  65. else
  66. sprintf(pathbuf, "%s/%s", g->cwd.characters(), argv[1]);
  67. FileSystemPath canonical_path(pathbuf);
  68. if (!canonical_path.is_valid()) {
  69. printf("FileSystemPath failed to canonicalize '%s'\n", pathbuf);
  70. return 1;
  71. }
  72. const char* path = canonical_path.string().characters();
  73. struct stat st;
  74. int rc = stat(path, &st);
  75. if (rc < 0) {
  76. printf("lstat(%s) failed: %s\n", path, strerror(errno));
  77. return 1;
  78. }
  79. if (!S_ISDIR(st.st_mode)) {
  80. printf("Not a directory: %s\n", path);
  81. return 1;
  82. }
  83. rc = chdir(path);
  84. if (rc < 0) {
  85. printf("chdir(%s) failed: %s\n", path, strerror(errno));
  86. return 1;
  87. }
  88. g->cwd = canonical_path.string();
  89. return 0;
  90. }
  91. static bool handle_builtin(int argc, char** argv, int& retval)
  92. {
  93. if (argc == 0)
  94. return false;
  95. if (!strcmp(argv[0], "cd")) {
  96. retval = sh_cd(argc, argv);
  97. return true;
  98. }
  99. if (!strcmp(argv[0], "pwd")) {
  100. retval = sh_pwd(argc, argv);
  101. return true;
  102. }
  103. if (!strcmp(argv[0], "exit")) {
  104. retval = sh_exit(argc, argv);
  105. return true;
  106. }
  107. return false;
  108. }
  109. static int try_exec(const char* path, char** argv)
  110. {
  111. int ret = execve(path, argv, environ);
  112. assert(ret < 0);
  113. {
  114. const char* search_path = "/bin";
  115. char pathbuf[128];
  116. sprintf(pathbuf, "%s/%s", search_path, argv[0]);
  117. ret = execve(pathbuf, argv, environ);
  118. assert(ret < 0);
  119. }
  120. {
  121. const char* search_path = "/usr/bin";
  122. char pathbuf[128];
  123. sprintf(pathbuf, "%s/%s", search_path, argv[0]);
  124. ret = execve(pathbuf, argv, environ);
  125. }
  126. if (ret == -1)
  127. return -1;
  128. return ret;
  129. }
  130. static int runcmd(char* cmd)
  131. {
  132. if (cmd[0] == 0)
  133. return 0;
  134. char buf[128];
  135. memcpy(buf, cmd, 128);
  136. char* argv[32];
  137. size_t argc = 1;
  138. argv[0] = &buf[0];
  139. size_t buflen = strlen(buf);
  140. for (size_t i = 0; i < buflen; ++i) {
  141. if (buf[i] == ' ') {
  142. buf[i] = '\0';
  143. argv[argc++] = &buf[i + 1];
  144. }
  145. }
  146. argv[argc] = nullptr;
  147. int retval = 0;
  148. if (handle_builtin(argc, argv, retval)) {
  149. return 0;
  150. }
  151. struct termios trm;
  152. tcgetattr(0, &trm);
  153. pid_t child = fork();
  154. if (!child) {
  155. setpgid(0, 0);
  156. tcsetpgrp(0, getpid());
  157. int ret = try_exec(argv[0], argv);
  158. if (ret < 0) {
  159. printf("exec failed: %s (%s)\n", cmd, strerror(errno));
  160. exit(1);
  161. }
  162. // We should never get here!
  163. assert(false);
  164. }
  165. int wstatus = 0;
  166. int rc;
  167. do {
  168. rc = waitpid(child, &wstatus, 0);
  169. if (rc < 0 && errno != EINTR) {
  170. perror("waitpid");
  171. break;
  172. }
  173. } while(errno == EINTR);
  174. // FIXME: Should I really have to tcsetpgrp() after my child has exited?
  175. // Is the terminal controlling pgrp really still the PGID of the dead process?
  176. tcsetpgrp(0, getpid());
  177. tcsetattr(0, TCSANOW, &trm);
  178. if (WIFEXITED(wstatus)) {
  179. if (WEXITSTATUS(wstatus) != 0)
  180. printf("Exited with status %d\n", WEXITSTATUS(wstatus));
  181. } else {
  182. if (WIFSIGNALED(wstatus)) {
  183. switch (WTERMSIG(wstatus)) {
  184. case SIGINT:
  185. printf("Interrupted\n");
  186. break;
  187. default:
  188. printf("Terminated by signal %d\n", WTERMSIG(wstatus));
  189. break;
  190. }
  191. } else {
  192. printf("Exited abnormally\n");
  193. }
  194. }
  195. return retval;
  196. }
  197. int main(int argc, char** argv)
  198. {
  199. g = new GlobalState;
  200. g->uid = getuid();
  201. g->sid = setsid();
  202. tcsetpgrp(0, getpgrp());
  203. tcgetattr(0, &g->termios);
  204. {
  205. struct sigaction sa;
  206. sa.sa_handler = handle_sigint;
  207. sa.sa_flags = 0;
  208. sa.sa_mask = 0;
  209. sa.sa_restorer = nullptr;
  210. int rc = sigaction(SIGINT, &sa, nullptr);
  211. assert(rc == 0);
  212. }
  213. int rc = gethostname(g->hostname, sizeof(g->hostname));
  214. if (rc < 0)
  215. perror("gethostname");
  216. rc = ttyname_r(0, g->ttyname, sizeof(g->ttyname));
  217. if (rc < 0)
  218. perror("ttyname_r");
  219. {
  220. auto* pw = getpwuid(getuid());
  221. if (pw)
  222. g->username = pw->pw_name;
  223. endpwent();
  224. }
  225. if (argc > 1 && !strcmp(argv[1], "-c")) {
  226. fprintf(stderr, "FIXME: Implement /bin/sh -c\n");
  227. return 1;
  228. }
  229. char linebuf[128];
  230. int linedx = 0;
  231. linebuf[0] = '\0';
  232. {
  233. char cwdbuf[1024];
  234. getcwd(cwdbuf, sizeof(cwdbuf));
  235. g->cwd = cwdbuf;
  236. }
  237. prompt();
  238. for (;;) {
  239. char keybuf[16];
  240. ssize_t nread = read(0, keybuf, sizeof(keybuf));
  241. if (nread == 0)
  242. return 0;
  243. if (nread < 0) {
  244. if (errno == EINTR) {
  245. if (g->was_interrupted) {
  246. if (linedx != 0)
  247. printf("^C");
  248. }
  249. g->was_interrupted = false;
  250. linebuf[0] = '\0';
  251. linedx = 0;
  252. putchar('\n');
  253. prompt();
  254. continue;
  255. } else {
  256. perror("read failed");
  257. return 2;
  258. }
  259. }
  260. for (ssize_t i = 0; i < nread; ++i) {
  261. char ch = keybuf[i];
  262. if (ch == 0)
  263. continue;
  264. if (ch == 8 || ch == g->termios.c_cc[VERASE]) {
  265. if (linedx == 0)
  266. continue;
  267. linebuf[--linedx] = '\0';
  268. putchar(8);
  269. fflush(stdout);
  270. continue;
  271. }
  272. if (ch == g->termios.c_cc[VKILL]) {
  273. if (linedx == 0)
  274. continue;
  275. for (; linedx; --linedx)
  276. putchar(0x8);
  277. linebuf[0] = '\0';
  278. fflush(stdout);
  279. continue;
  280. }
  281. putchar(ch);
  282. fflush(stdout);
  283. if (ch != '\n') {
  284. linebuf[linedx++] = ch;
  285. linebuf[linedx] = '\0';
  286. } else {
  287. runcmd(linebuf);
  288. linebuf[0] = '\0';
  289. linedx = 0;
  290. prompt();
  291. }
  292. }
  293. }
  294. return 0;
  295. }