Skip to content

Commit

Permalink
Squash HTM to a single commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Gauci authored and MisterTea committed May 17, 2018
1 parent 3e3be47 commit d83352f
Show file tree
Hide file tree
Showing 29 changed files with 2,692 additions and 35 deletions.
89 changes: 89 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}",
"${workspaceFolder}/build",
"${workspaceFolder}/src",
"${workspaceFolder}/terminal",
"${workspaceFolder}/htm",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include",
"/Library/Developer/CommandLineTools/usr/include",
"/usr/include"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"${workspaceFolder}",
"/Library/Developer/CommandLineTools/usr/include/c++/v1",
"/usr/local/include",
"/Library/Developer/CommandLineTools/usr/lib/clang/9.1.0/include",
"/Library/Developer/CommandLineTools/usr/include",
"/usr/include"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
},
"macFrameworkPath": [
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17"
},
{
"name": "Linux",
"includePath": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}",
"${workspaceFolder}/build",
"${workspaceFolder}/src",
"${workspaceFolder}/terminal",
"${workspaceFolder}/htm"
],
"defines": [],
"intelliSenseMode": "clang-x64",
"browse": {
"path": [
"/usr/include",
"/usr/local/include",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
},
{
"name": "Win32",
"includePath": [
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include",
"${workspaceFolder}",
"${workspaceFolder}/build",
"${workspaceFolder}/src",
"${workspaceFolder}/terminal",
"${workspaceFolder}/htm"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"intelliSenseMode": "msvc-x64",
"browse": {
"path": [
"C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/include/*",
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
51 changes: 51 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"files.associations": {
"sstream": "cpp",
"strstream": "cpp",
"fstream": "cpp",
"locale": "cpp",
"*.ipp": "cpp",
"__locale": "cpp",
"__string": "cpp",
"string": "cpp",
"string_view": "cpp",
"*.tcc": "cpp",
"__bit_reference": "cpp",
"algorithm": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"hash_map": "cpp",
"hash_set": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"stdexcept": "cpp",
"memory": "cpp",
"utility": "cpp",
"__hash_table": "cpp",
"__split_buffer": "cpp",
"__tree": "cpp",
"array": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"initializer_list": "cpp",
"iterator": "cpp",
"list": "cpp",
"valarray": "cpp",
"vector": "cpp",
"slist": "cpp",
"iosfwd": "cpp",
"__config": "cpp",
"__nullptr": "cpp",
"cstddef": "cpp",
"exception": "cpp",
"new": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"cctype": "cpp",
"cstdlib": "cpp",
"random": "cpp"
},
"C_Cpp.clang_format_style": "Google"
}
72 changes: 71 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ endif (CMAKE_SYSTEM_NAME MATCHES "FreeBSD")
# Add cmake script directory.
LIST(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")

# Find packages
# Required packages
find_package(Threads REQUIRED)
find_package(GFlags REQUIRED)
find_package(Protobuf REQUIRED)
find_package(Sodium REQUIRED)

# Optional packages
IF(LINUX)
find_package(SELinux)
ENDIF()
Expand Down Expand Up @@ -122,6 +124,7 @@ add_library(
TerminalCommon
STATIC

terminal/DaemonCreator.cpp
terminal/SystemUtils.cpp
terminal/RawSocketUtils.cpp
terminal/PortForwardHandler.cpp
Expand Down Expand Up @@ -173,6 +176,73 @@ target_link_libraries (
${CORE_LIBRARIES}
)

PROTOBUF_GENERATE_CPP(
HTM_SRCS
HTM_HDRS

proto/HTM.proto
)

add_library (
HtmCommon
STATIC

${HTM_SRCS}
htm/TerminalHandler.cpp
htm/MultiplexerState.cpp
htm/IpcPairClient.cpp
htm/IpcPairEndpoint.cpp
htm/IpcPairServer.cpp
htm/HtmClient.cpp
htm/HtmServer.cpp
)

add_executable (
htm

htm/HtmClientMain.cpp
)

target_link_libraries (
htm
LINK_PUBLIC

HtmCommon
TerminalCommon
EternalTCP-static
${CMAKE_THREAD_LIBS_INIT}
${PROTOBUF_LIBRARIES}
${GLOG_LIBRARIES}
${GFLAGS_LIBRARIES}
${sodium_LIBRARY_RELEASE}
${SELINUX_LIBRARIES}
${UTEMPTER_LIBRARIES}
${CORE_LIBRARIES}
)

add_executable (
htmd

htm/HtmServerMain.cpp
)

target_link_libraries (
htmd
LINK_PUBLIC

HtmCommon
TerminalCommon
EternalTCP-static
${CMAKE_THREAD_LIBS_INIT}
${PROTOBUF_LIBRARIES}
${GLOG_LIBRARIES}
${GFLAGS_LIBRARIES}
${sodium_LIBRARY_RELEASE}
${SELINUX_LIBRARIES}
${UTEMPTER_LIBRARIES}
${CORE_LIBRARIES}
)

if(BUILD_TEST) # Build unit tests.
message(STATUS "Enabling test for ${PROJECT_NAME}")
enable_testing()
Expand Down
55 changes: 55 additions & 0 deletions htm/HtmClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "HtmClient.hpp"

#include "HTM.pb.h"

#include "HtmServer.hpp"
#include "IpcPairClient.hpp"
#include "LogHandler.hpp"
#include "MultiplexerState.hpp"
#include "RawSocketUtils.hpp"

namespace et {
HtmClient::HtmClient() : IpcPairClient(HtmServer::getPipeName()) {}

void HtmClient::run() {
const int BUF_SIZE = 1024;
char buf[BUF_SIZE];
while (true) {
// Data structures needed for select() and
// non-blocking I/O.
fd_set rfd;
timeval tv;

FD_ZERO(&rfd);
FD_SET(endpointFd, &rfd);
FD_SET(STDIN_FILENO, &rfd);
tv.tv_sec = 0;
tv.tv_usec = 10000;
select(max(STDIN_FILENO, endpointFd) + 1, &rfd, NULL, NULL, &tv);

if (FD_ISSET(STDIN_FILENO, &rfd)) {
VLOG(1) << "READING FROM STDIN";
int rc = ::read(STDIN_FILENO, buf, BUF_SIZE);
if (rc < 0) {
throw std::runtime_error("Cannot read from raw socket");
}
if (rc == 0) {
throw std::runtime_error("stdin has closed abruptly.");
}
RawSocketUtils::writeAll(endpointFd, buf, rc);
}

if (FD_ISSET(endpointFd, &rfd)) {
VLOG(1) << "READING FROM ENDPOINT";
int rc = ::read(endpointFd, buf, BUF_SIZE);
if (rc < 0) {
throw std::runtime_error("Cannot read from raw socket");
}
if (rc == 0) {
throw std::runtime_error("htmd has closed abruptly.");
}
RawSocketUtils::writeAll(STDOUT_FILENO, buf, rc);
}
}
}
} // namespace et
16 changes: 16 additions & 0 deletions htm/HtmClient.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef __HTM_CLIENT_H__
#define __HTM_CLIENT_H__

#include "Headers.hpp"

#include "IpcPairClient.hpp"

namespace et {
class HtmClient : public IpcPairClient {
public:
HtmClient();
void run();
};
} // namespace et

#endif // __HTM_CLIENT_H__
72 changes: 72 additions & 0 deletions htm/HtmClientMain.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "HTM.pb.h"

#include "DaemonCreator.hpp"
#include "HtmClient.hpp"
#include "HtmServer.hpp"
#include "IpcPairClient.hpp"
#include "LogHandler.hpp"
#include "MultiplexerState.hpp"
#include "RawSocketUtils.hpp"

DEFINE_bool(x, false, "flag to kill all old sessions belonging to the user");

int main(int argc, char** argv) {
// Version string need to be set before GFLAGS parse arguments
SetVersionString(string(ET_VERSION));
GOOGLE_PROTOBUF_VERIFY_VERSION;
srand(1);

setvbuf(stdin, NULL, _IONBF, 0); // turn off buffering
setvbuf(stdout, NULL, _IONBF, 0); // turn off buffering

// Turn on raw terminal mode
termios terminal_backup;
termios terminal_local;
tcgetattr(0, &terminal_local);
memcpy(&terminal_backup, &terminal_local, sizeof(struct termios));
cfmakeraw(&terminal_local);
tcsetattr(0, TCSANOW, &terminal_local);

// Setup easylogging configurations
el::Configurations defaultConf =
et::LogHandler::SetupLogHandler(&argc, &argv);
defaultConf.setGlobally(el::ConfigurationType::ToStandardOutput, "false");
el::Loggers::setVerboseLevel(3);
// default max log file size is 20MB for etserver
string maxlogsize = "20971520";
et::LogHandler::SetupLogFile(&defaultConf, "/tmp/htm.log", maxlogsize);

// Reconfigure default logger to apply settings above
el::Loggers::reconfigureLogger("default", defaultConf);

uid_t myuid = getuid();
if (FLAGS_x) {
LOG(INFO) << "Killing previous htmd";
// Kill previous htm daemon
string command =
string("pkill -x -U ") + to_string(myuid) + string(" htmd");
system(command.c_str());
}

// Check if daemon exists
string command = string("pgrep -x -U ") + to_string(myuid) + string(" htmd");
string pgrepOutput = SystemToStr(command.c_str());

if (pgrepOutput.length() == 0) {
// Fork to create the daemon
int result = et::DaemonCreator::create();
if (result == et::DaemonCreator::CHILD) {
// This means we are the daemon
exit(system("htmd"));
}
}

// This means we are the client to the daemon
usleep(10 * 1000); // Sleep for 10ms to let the daemon come alive
et::HtmClient htmClient;
htmClient.run();

tcsetattr(0, TCSANOW, &terminal_backup);

return 0;
}
Loading

0 comments on commit d83352f

Please sign in to comment.