TerminalWrapper.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #include "TerminalWrapper.h"
  2. #include "ProcessStateWidget.h"
  3. #include <AK/String.h>
  4. #include <LibCore/CConfigFile.h>
  5. #include <LibGUI/GBoxLayout.h>
  6. #include <LibGUI/GMessageBox.h>
  7. #include <LibVT/TerminalWidget.h>
  8. #include <fcntl.h>
  9. #include <signal.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <sys/ioctl.h>
  13. #include <sys/types.h>
  14. #include <sys/wait.h>
  15. #include <unistd.h>
  16. void TerminalWrapper::run_command(const String& command)
  17. {
  18. if (m_pid != -1) {
  19. GMessageBox::show(
  20. "A command is already running in this TerminalWrapper",
  21. "Can't run command",
  22. GMessageBox::Type::Error,
  23. GMessageBox::InputType::OK,
  24. window());
  25. return;
  26. }
  27. int ptm_fd = open("/dev/ptmx", O_RDWR | O_CLOEXEC);
  28. if (ptm_fd < 0) {
  29. perror("open(ptmx)");
  30. ASSERT_NOT_REACHED();
  31. }
  32. m_terminal_widget->set_pty_master_fd(ptm_fd);
  33. m_terminal_widget->on_command_exit = [this] {
  34. int wstatus;
  35. int rc = waitpid(m_pid, &wstatus, 0);
  36. if (rc < 0) {
  37. perror("waitpid");
  38. ASSERT_NOT_REACHED();
  39. }
  40. if (WIFEXITED(wstatus)) {
  41. m_terminal_widget->inject_string(String::format("\033[%d;1m(Command exited with code %d)\033[0m\n", wstatus == 0 ? 32 : 31, WEXITSTATUS(wstatus)));
  42. } else if (WIFSTOPPED(wstatus)) {
  43. m_terminal_widget->inject_string(String::format("\033[34;1m(Command stopped!)\033[0m\n"));
  44. } else if (WIFSIGNALED(wstatus)) {
  45. m_terminal_widget->inject_string(String::format("\033[34;1m(Command signaled with %s!)\033[0m\n", strsignal(WTERMSIG(wstatus))));
  46. }
  47. m_process_state_widget->set_tty_fd(-1);
  48. m_pid = -1;
  49. if (on_command_exit)
  50. on_command_exit();
  51. };
  52. m_pid = fork();
  53. if (m_pid == 0) {
  54. // Create a new process group.
  55. setsid();
  56. const char* tty_name = ptsname(ptm_fd);
  57. if (!tty_name) {
  58. perror("ptsname");
  59. exit(1);
  60. }
  61. close(ptm_fd);
  62. int pts_fd = open(tty_name, O_RDWR);
  63. if (pts_fd < 0) {
  64. perror("open");
  65. exit(1);
  66. }
  67. // NOTE: It's okay if this fails.
  68. (void)ioctl(0, TIOCNOTTY);
  69. close(0);
  70. close(1);
  71. close(2);
  72. int rc = dup2(pts_fd, 0);
  73. if (rc < 0) {
  74. perror("dup2");
  75. exit(1);
  76. }
  77. rc = dup2(pts_fd, 1);
  78. if (rc < 0) {
  79. perror("dup2");
  80. exit(1);
  81. }
  82. rc = dup2(pts_fd, 2);
  83. if (rc < 0) {
  84. perror("dup2");
  85. exit(1);
  86. }
  87. rc = close(pts_fd);
  88. if (rc < 0) {
  89. perror("close");
  90. exit(1);
  91. }
  92. rc = ioctl(0, TIOCSCTTY);
  93. if (rc < 0) {
  94. perror("ioctl(TIOCSCTTY)");
  95. exit(1);
  96. }
  97. setenv("TERM", "xterm", true);
  98. setenv("PATH", "/bin:/usr/bin:/usr/local/bin", true);
  99. auto parts = command.split(' ');
  100. ASSERT(!parts.is_empty());
  101. const char** args = (const char**) calloc(parts.size() + 1, sizeof(const char*));
  102. for (int i = 0; i < parts.size(); i++) {
  103. args[i] = parts[i].characters();
  104. }
  105. rc = execvp(args[0], const_cast<char**>(args));
  106. if (rc < 0) {
  107. perror("execve");
  108. exit(1);
  109. }
  110. ASSERT_NOT_REACHED();
  111. }
  112. // Parent process, cont'd.
  113. m_process_state_widget->set_tty_fd(ptm_fd);
  114. }
  115. void TerminalWrapper::kill_running_command()
  116. {
  117. ASSERT(m_pid != -1);
  118. // Kill our child process and its whole process group.
  119. (void)killpg(m_pid, SIGTERM);
  120. }
  121. TerminalWrapper::TerminalWrapper(GWidget* parent)
  122. : GWidget(parent)
  123. {
  124. set_layout(make<GBoxLayout>(Orientation::Vertical));
  125. RefPtr<CConfigFile> config = CConfigFile::get_for_app("Terminal");
  126. m_terminal_widget = TerminalWidget::construct(-1, false, config);
  127. add_child(*m_terminal_widget);
  128. m_process_state_widget = ProcessStateWidget::construct(this);
  129. }
  130. TerminalWrapper::~TerminalWrapper()
  131. {
  132. }