main.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Client.h"
  7. #include <AK/HashMap.h>
  8. #include <AK/String.h>
  9. #include <AK/Types.h>
  10. #include <LibCore/ArgsParser.h>
  11. #include <LibCore/EventLoop.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/TCPServer.h>
  14. #include <LibMain/Main.h>
  15. #include <fcntl.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <sys/ioctl.h>
  19. #include <unistd.h>
  20. static void run_command(int ptm_fd, String command)
  21. {
  22. pid_t pid = fork();
  23. if (pid == 0) {
  24. char const* tty_name = ptsname(ptm_fd);
  25. if (!tty_name) {
  26. perror("ptsname");
  27. exit(1);
  28. }
  29. close(ptm_fd);
  30. int pts_fd = open(tty_name, O_RDWR);
  31. if (pts_fd < 0) {
  32. perror("open");
  33. exit(1);
  34. }
  35. // NOTE: It's okay if this fails.
  36. [[maybe_unused]] auto rc = ioctl(0, TIOCNOTTY);
  37. close(0);
  38. close(1);
  39. close(2);
  40. rc = dup2(pts_fd, 0);
  41. if (rc < 0) {
  42. perror("dup2");
  43. exit(1);
  44. }
  45. rc = dup2(pts_fd, 1);
  46. if (rc < 0) {
  47. perror("dup2");
  48. exit(1);
  49. }
  50. rc = dup2(pts_fd, 2);
  51. if (rc < 0) {
  52. perror("dup2");
  53. exit(1);
  54. }
  55. rc = close(pts_fd);
  56. if (rc < 0) {
  57. perror("close");
  58. exit(1);
  59. }
  60. rc = ioctl(0, TIOCSCTTY);
  61. if (rc < 0) {
  62. perror("ioctl(TIOCSCTTY)");
  63. exit(1);
  64. }
  65. char const* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr };
  66. if (!command.is_empty()) {
  67. args[1] = "-c";
  68. args[2] = command.characters();
  69. }
  70. char const* envs[] = { "TERM=xterm", "PATH=" DEFAULT_PATH, nullptr };
  71. rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs));
  72. if (rc < 0) {
  73. perror("execve");
  74. exit(1);
  75. }
  76. VERIFY_NOT_REACHED();
  77. }
  78. }
  79. ErrorOr<int> serenity_main(Main::Arguments arguments)
  80. {
  81. int port = 23;
  82. char const* command = "";
  83. Core::ArgsParser args_parser;
  84. args_parser.add_option(port, "Port to listen on", nullptr, 'p', "port");
  85. args_parser.add_option(command, "Program to run on connection", nullptr, 'c', "command");
  86. args_parser.parse(arguments);
  87. if ((u16)port != port) {
  88. warnln("Invalid port number: {}", port);
  89. exit(1);
  90. }
  91. Core::EventLoop event_loop;
  92. auto server = TRY(Core::TCPServer::try_create());
  93. TRY(server->listen({}, port));
  94. HashMap<int, NonnullRefPtr<Client>> clients;
  95. int next_id = 0;
  96. server->on_ready_to_accept = [&next_id, &clients, &server, command] {
  97. int id = next_id++;
  98. auto maybe_client_socket = server->accept();
  99. if (maybe_client_socket.is_error()) {
  100. warnln("accept: {}", maybe_client_socket.error());
  101. return;
  102. }
  103. auto client_socket = maybe_client_socket.release_value();
  104. int ptm_fd = posix_openpt(O_RDWR);
  105. if (ptm_fd < 0) {
  106. perror("posix_openpt");
  107. client_socket->close();
  108. return;
  109. }
  110. if (grantpt(ptm_fd) < 0) {
  111. perror("grantpt");
  112. client_socket->close();
  113. return;
  114. }
  115. if (unlockpt(ptm_fd) < 0) {
  116. perror("unlockpt");
  117. client_socket->close();
  118. return;
  119. }
  120. run_command(ptm_fd, command);
  121. auto maybe_client = Client::create(id, move(client_socket), ptm_fd);
  122. if (maybe_client.is_error()) {
  123. warnln("Failed to create the client: {}", maybe_client.error());
  124. return;
  125. }
  126. auto client = maybe_client.release_value();
  127. client->on_exit = [&clients, id] {
  128. Core::deferred_invoke([&clients, id] { clients.remove(id); });
  129. };
  130. clients.set(id, client);
  131. };
  132. return event_loop.exec();
  133. }