mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:37:37 +00:00
TelnetServer: Port to LibMain
This is only a very basic change, since the fallible function calls are all inside the `on_ready_to_accept` callback.
This commit is contained in:
parent
98c8eca70c
commit
143f820c68
2 changed files with 9 additions and 14 deletions
|
@ -10,4 +10,4 @@ set(SOURCES
|
|||
)
|
||||
|
||||
serenity_bin(TelnetServer)
|
||||
target_link_libraries(TelnetServer LibCore)
|
||||
target_link_libraries(TelnetServer LibCore LibMain)
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <LibCore/ArgsParser.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibCore/TCPServer.h>
|
||||
#include <LibMain/Main.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -21,7 +22,7 @@ static void run_command(int ptm_fd, String command)
|
|||
{
|
||||
pid_t pid = fork();
|
||||
if (pid == 0) {
|
||||
const char* tty_name = ptsname(ptm_fd);
|
||||
char const* tty_name = ptsname(ptm_fd);
|
||||
if (!tty_name) {
|
||||
perror("ptsname");
|
||||
exit(1);
|
||||
|
@ -65,12 +66,12 @@ static void run_command(int ptm_fd, String command)
|
|||
perror("ioctl(TIOCSCTTY)");
|
||||
exit(1);
|
||||
}
|
||||
const char* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr };
|
||||
char const* args[4] = { "/bin/Shell", nullptr, nullptr, nullptr };
|
||||
if (!command.is_empty()) {
|
||||
args[1] = "-c";
|
||||
args[2] = command.characters();
|
||||
}
|
||||
const char* envs[] = { "TERM=xterm", "PATH=/usr/local/bin:/usr/bin:/bin", nullptr };
|
||||
char const* envs[] = { "TERM=xterm", "PATH=/usr/local/bin:/usr/bin:/bin", nullptr };
|
||||
rc = execve("/bin/Shell", const_cast<char**>(args), const_cast<char**>(envs));
|
||||
if (rc < 0) {
|
||||
perror("execve");
|
||||
|
@ -80,15 +81,15 @@ static void run_command(int ptm_fd, String command)
|
|||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
||||
{
|
||||
int port = 23;
|
||||
const char* command = "";
|
||||
char const* command = "";
|
||||
|
||||
Core::ArgsParser args_parser;
|
||||
args_parser.add_option(port, "Port to listen on", nullptr, 'p', "port");
|
||||
args_parser.add_option(command, "Program to run on connection", nullptr, 'c', "command");
|
||||
args_parser.parse(argc, argv);
|
||||
args_parser.parse(arguments);
|
||||
|
||||
if ((u16)port != port) {
|
||||
warnln("Invalid port number: {}", port);
|
||||
|
@ -148,11 +149,5 @@ int main(int argc, char** argv)
|
|||
clients.set(id, client);
|
||||
};
|
||||
|
||||
int rc = event_loop.exec();
|
||||
if (rc != 0) {
|
||||
fprintf(stderr, "event loop exited badly; rc=%d", rc);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return event_loop.exec();
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue