Serenity Operating System
at portability 177 lines 5.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "Client.h" 28#include <AK/BufferStream.h> 29#include <AK/ByteBuffer.h> 30#include <AK/HashMap.h> 31#include <AK/IPv4Address.h> 32#include <AK/String.h> 33#include <AK/StringBuilder.h> 34#include <AK/Types.h> 35#include <LibCore/EventLoop.h> 36#include <LibCore/TCPServer.h> 37#include <LibCore/TCPSocket.h> 38#include <fcntl.h> 39#include <getopt.h> 40#include <stdio.h> 41#include <stdlib.h> 42#include <sys/ioctl.h> 43 44static void run_command(int ptm_fd, String command) 45{ 46 pid_t pid = fork(); 47 if (pid == 0) { 48 const char* tty_name = ptsname(ptm_fd); 49 if (!tty_name) { 50 perror("ptsname"); 51 exit(1); 52 } 53 close(ptm_fd); 54 int pts_fd = open(tty_name, O_RDWR); 55 if (pts_fd < 0) { 56 perror("open"); 57 exit(1); 58 } 59 60 // NOTE: It's okay if this fails. 61 (void)ioctl(0, TIOCNOTTY); 62 63 close(0); 64 close(1); 65 close(2); 66 67 int rc = dup2(pts_fd, 0); 68 if (rc < 0) { 69 perror("dup2"); 70 exit(1); 71 } 72 rc = dup2(pts_fd, 1); 73 if (rc < 0) { 74 perror("dup2"); 75 exit(1); 76 } 77 rc = dup2(pts_fd, 2); 78 if (rc < 0) { 79 perror("dup2"); 80 exit(1); 81 } 82 rc = close(pts_fd); 83 if (rc < 0) { 84 perror("close"); 85 exit(1); 86 } 87 rc = ioctl(0, TIOCSCTTY); 88 if (rc < 0) { 89 perror("ioctl(TIOCSCTTY)"); 90 exit(1); 91 } 92 const char* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr }; 93 if (!command.is_empty()) { 94 args[1] = "-c"; 95 args[2] = command.characters(); 96 } 97 const char* envs[] = { "TERM=xterm", "PATH=/bin:/usr/bin:/usr/local/bin", nullptr }; 98 rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs)); 99 if (rc < 0) { 100 perror("execve"); 101 exit(1); 102 } 103 ASSERT_NOT_REACHED(); 104 } 105} 106 107int main(int argc, char** argv) 108{ 109 Core::EventLoop event_loop; 110 auto server = Core::TCPServer::construct(); 111 112 int opt; 113 u16 port = 23; 114 const char* command = ""; 115 while ((opt = getopt(argc, argv, "p:c:")) != -1) { 116 switch (opt) { 117 case 'p': 118 port = atoi(optarg); 119 break; 120 case 'c': 121 command = optarg; 122 break; 123 default: 124 fprintf(stderr, "Usage: %s [-p port] [-c command]", argv[0]); 125 exit(1); 126 } 127 } 128 129 if (!server->listen({}, port)) { 130 perror("listen"); 131 exit(1); 132 } 133 134 HashMap<int, NonnullRefPtr<Client>> clients; 135 int next_id = 0; 136 137 server->on_ready_to_accept = [&next_id, &clients, &server, command] { 138 int id = next_id++; 139 140 auto client_socket = server->accept(); 141 if (!client_socket) { 142 perror("accept"); 143 return; 144 } 145 146 int ptm_fd = posix_openpt(O_RDWR); 147 if (ptm_fd < 0) { 148 perror("posix_openpt"); 149 client_socket->close(); 150 return; 151 } 152 if (grantpt(ptm_fd) < 0) { 153 perror("grantpt"); 154 client_socket->close(); 155 return; 156 } 157 if (unlockpt(ptm_fd) < 0) { 158 perror("unlockpt"); 159 client_socket->close(); 160 return; 161 } 162 163 run_command(ptm_fd, command); 164 165 auto client = Client::create(id, move(client_socket), ptm_fd); 166 client->on_exit = [&clients, id] { clients.remove(id); }; 167 clients.set(id, client); 168 }; 169 170 int rc = event_loop.exec(); 171 if (rc != 0) { 172 fprintf(stderr, "event loop exited badly; rc=%d", rc); 173 exit(1); 174 } 175 176 return 0; 177}