kakoune/src/remote.cc

693 lines
17 KiB
C++
Raw Normal View History

#include "remote.hh"
#include "buffer_manager.hh"
#include "buffer_utils.hh"
2013-04-09 20:05:40 +02:00
#include "client_manager.hh"
#include "command_manager.hh"
2013-04-09 20:05:40 +02:00
#include "display_buffer.hh"
#include "event_manager.hh"
#include "file.hh"
#include "id_map.hh"
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <pwd.h>
#include <fcntl.h>
namespace Kakoune
{
enum class RemoteUIMsg
{
MenuShow,
MenuSelect,
MenuHide,
InfoShow,
InfoHide,
Draw,
DrawStatus,
Refresh,
SetOptions
};
struct socket_error{};
class Message
{
public:
Message(int sock) : m_socket(sock) {}
~Message() noexcept(false)
{
if (m_stream.size() == 0)
return;
int res = ::write(m_socket, m_stream.data(), m_stream.size());
if (res == 0)
throw peer_disconnected{};
}
void write(const char* val, size_t size)
{
m_stream.insert(m_stream.end(), val, val + size);
}
template<typename T>
void write(const T& val)
{
write((const char*)&val, sizeof(val));
}
void write(StringView str)
{
write(str.length());
write(str.data(), (int)str.length());
};
void write(const String& str)
{
write(StringView{str});
}
template<typename T>
void write(ConstArrayView<T> view)
{
write<uint32_t>(view.size());
for (auto& val : view)
write(val);
}
2015-01-14 20:16:32 +01:00
template<typename T, MemoryDomain domain>
void write(const Vector<T, domain>& vec)
{
write(ConstArrayView<T>(vec));
}
template<typename Val, MemoryDomain domain>
void write(const IdMap<Val, domain>& map)
{
write<uint32_t>(map.size());
for (auto& val : map)
{
2015-09-16 20:57:57 +02:00
write(val.key);
write(val.value);
}
}
2013-07-26 00:26:43 +02:00
void write(Color color)
{
write(color.color);
if (color.color == Color::RGB)
{
write(color.r);
write(color.g);
write(color.b);
}
}
void write(const DisplayAtom& atom)
{
2013-07-24 14:55:57 +02:00
write(atom.content());
write(atom.face);
}
void write(const DisplayLine& line)
{
write(line.atoms());
}
void write(const DisplayBuffer& display_buffer)
{
write(display_buffer.lines());
}
private:
2015-01-12 14:58:41 +01:00
Vector<char> m_stream;
int m_socket;
};
void read(int socket, char* buffer, size_t size)
{
while (size)
{
int res = ::read(socket, buffer, size);
if (res == 0)
throw peer_disconnected{};
if (res < 0)
throw socket_error{};
buffer += res;
size -= res;
}
}
template<typename T>
T read(int socket)
{
2013-07-31 00:32:51 +02:00
union U
{
T object;
2014-11-05 14:50:06 +01:00
alignas(T) char data[sizeof(T)];
2013-07-31 00:32:51 +02:00
U() {}
~U() { object.~T(); }
} u;
read(socket, u.data, sizeof(T));
return u.object;
}
template<>
String read<String>(int socket)
{
ByteCount length = read<ByteCount>(socket);
String res;
if (length > 0)
{
2016-02-05 10:27:22 +01:00
res.force_size((int)length);
read(socket, &res[0_byte], (int)length);
}
return res;
}
template<typename T>
2015-01-12 14:58:41 +01:00
Vector<T> read_vector(int socket)
{
uint32_t size = read<uint32_t>(socket);
2015-01-12 14:58:41 +01:00
Vector<T> res;
res.reserve(size);
while (size--)
res.push_back(read<T>(socket));
return res;
}
template<>
Color read<Color>(int socket)
{
Color res;
res.color = read<Color::NamedColor>(socket);
if (res.color == Color::RGB)
{
res.r = read<unsigned char>(socket);
res.g = read<unsigned char>(socket);
res.b = read<unsigned char>(socket);
}
return res;
}
template<>
DisplayAtom read<DisplayAtom>(int socket)
{
2013-07-24 14:55:57 +02:00
DisplayAtom atom(read<String>(socket));
atom.face = read<Face>(socket);
return atom;
}
template<>
DisplayLine read<DisplayLine>(int socket)
{
return DisplayLine(read_vector<DisplayAtom>(socket));
}
template<>
DisplayBuffer read<DisplayBuffer>(int socket)
{
DisplayBuffer db;
db.lines() = read_vector<DisplayLine>(socket);
return db;
}
template<typename Val, MemoryDomain domain>
IdMap<Val, domain> read_idmap(int socket)
{
uint32_t size = read<uint32_t>(socket);
IdMap<Val, domain> res;
res.reserve(size);
while (size--)
{
auto key = read<String>(socket);
auto val = read<Val>(socket);
res.append({std::move(key), std::move(val)});
}
return res;
}
class RemoteUI : public UserInterface
{
public:
RemoteUI(int socket, CharCoord dimensions);
~RemoteUI();
2015-10-05 02:25:23 +02:00
void menu_show(ConstArrayView<DisplayLine> choices,
CharCoord anchor, Face fg, Face bg,
MenuStyle style) override;
void menu_select(int selected) override;
void menu_hide() override;
void info_show(StringView title, StringView content,
CharCoord anchor, Face face,
InfoStyle style) override;
void info_hide() override;
void draw(const DisplayBuffer& display_buffer,
const Face& default_face,
const Face& padding_face) override;
void draw_status(const DisplayLine& status_line,
const DisplayLine& mode_line,
const Face& default_face) override;
void refresh(bool force) override;
bool is_key_available() override;
Key get_key() override;
CharCoord dimensions() override;
void set_input_callback(InputCallback callback) override;
void set_ui_options(const Options& options) override;
private:
FDWatcher m_socket_watcher;
CharCoord m_dimensions;
InputCallback m_input_callback;
};
RemoteUI::RemoteUI(int socket, CharCoord dimensions)
: m_socket_watcher(socket, [this](FDWatcher&, EventMode mode) {
2014-04-05 13:04:37 +02:00
if (m_input_callback)
m_input_callback(mode);
}),
m_dimensions(dimensions)
2012-11-19 19:06:32 +01:00
{
write_to_debug_buffer(format("remote client connected: {}", m_socket_watcher.fd()));
2012-11-19 19:06:32 +01:00
}
RemoteUI::~RemoteUI()
{
write_to_debug_buffer(format("remote client disconnected: {}", m_socket_watcher.fd()));
m_socket_watcher.close_fd();
2012-11-19 19:06:32 +01:00
}
2015-10-05 02:25:23 +02:00
void RemoteUI::menu_show(ConstArrayView<DisplayLine> choices,
CharCoord anchor, Face fg, Face bg,
MenuStyle style)
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::MenuShow);
msg.write(choices);
msg.write(anchor);
msg.write(fg);
msg.write(bg);
msg.write(style);
}
void RemoteUI::menu_select(int selected)
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::MenuSelect);
msg.write(selected);
}
void RemoteUI::menu_hide()
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::MenuHide);
}
void RemoteUI::info_show(StringView title, StringView content,
CharCoord anchor, Face face,
InfoStyle style)
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::InfoShow);
msg.write(title);
msg.write(content);
msg.write(anchor);
msg.write(face);
msg.write(style);
}
void RemoteUI::info_hide()
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::InfoHide);
}
void RemoteUI::draw(const DisplayBuffer& display_buffer,
const Face& default_face,
const Face& padding_face)
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::Draw);
msg.write(display_buffer);
msg.write(default_face);
msg.write(padding_face);
}
void RemoteUI::draw_status(const DisplayLine& status_line,
const DisplayLine& mode_line,
const Face& default_face)
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::DrawStatus);
msg.write(status_line);
msg.write(mode_line);
msg.write(default_face);
}
void RemoteUI::refresh(bool force)
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::Refresh);
msg.write(force);
}
void RemoteUI::set_ui_options(const Options& options)
{
Message msg(m_socket_watcher.fd());
msg.write(RemoteUIMsg::SetOptions);
msg.write(options);
}
bool RemoteUI::is_key_available()
{
timeval tv;
fd_set rfds;
int sock = m_socket_watcher.fd();
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
2013-07-28 16:40:02 +02:00
int res = select(sock+1, &rfds, nullptr, nullptr, &tv);
return res == 1;
}
Key RemoteUI::get_key()
{
try
{
Key key = read<Key>(m_socket_watcher.fd());
2015-08-23 16:18:18 +02:00
if (key.modifiers == Key::Modifiers::Resize)
m_dimensions = key.coord();
return key;
}
catch (peer_disconnected&)
{
throw client_removed{ false };
}
catch (socket_error&)
{
write_to_debug_buffer("ungraceful deconnection detected");
throw client_removed{ false };
}
}
CharCoord RemoteUI::dimensions()
{
return m_dimensions;
}
void RemoteUI::set_input_callback(InputCallback callback)
{
m_input_callback = std::move(callback);
}
2014-11-06 14:54:18 +01:00
static sockaddr_un session_addr(StringView session)
{
2014-11-06 14:54:18 +01:00
sockaddr_un addr;
addr.sun_family = AF_UNIX;
if (find(session, '/')!= session.end())
format_to(addr.sun_path, "/tmp/kakoune/{}", session);
else
format_to(addr.sun_path, "/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, session);
2014-11-06 14:54:18 +01:00
return addr;
}
2014-11-05 14:57:12 +01:00
2014-11-06 14:54:18 +01:00
static int connect_to(StringView session)
{
2014-11-05 14:57:12 +01:00
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
fcntl(sock, F_SETFD, FD_CLOEXEC);
2014-11-06 14:54:18 +01:00
sockaddr_un addr = session_addr(session);
2014-11-05 14:57:12 +01:00
if (connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) == -1)
2014-11-06 14:54:18 +01:00
throw connection_failed(addr.sun_path);
return sock;
}
bool check_session(StringView session)
{
int sock = socket(AF_UNIX, SOCK_STREAM, 0);
auto close_sock = on_scope_end([sock]{ close(sock); });
sockaddr_un addr = session_addr(session);
return connect(sock, (sockaddr*)&addr, sizeof(addr.sun_path)) != -1;
}
2014-11-06 14:54:18 +01:00
RemoteClient::RemoteClient(StringView session, std::unique_ptr<UserInterface>&& ui,
const EnvVarMap& env_vars, StringView init_command)
2015-08-23 16:18:18 +02:00
: m_ui(std::move(ui))
2014-11-06 14:54:18 +01:00
{
int sock = connect_to(session);
2014-11-05 14:57:12 +01:00
{
Message msg(sock);
msg.write(init_command.data(), (int)init_command.length());
msg.write((char)0);
msg.write(m_ui->dimensions());
2014-11-05 14:57:12 +01:00
msg.write(env_vars);
}
m_ui->set_input_callback([this](EventMode){ write_next_key(); });
2014-11-05 14:57:12 +01:00
m_socket_watcher.reset(new FDWatcher{sock, [this](FDWatcher&, EventMode){ process_available_messages(); }});
}
void RemoteClient::process_available_messages()
{
2014-11-05 14:57:12 +01:00
int socket = m_socket_watcher->fd();
timeval tv{ 0, 0 };
fd_set rfds;
do {
process_next_message();
FD_ZERO(&rfds);
FD_SET(socket, &rfds);
} while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1);
}
void RemoteClient::process_next_message()
{
2014-11-05 14:57:12 +01:00
int socket = m_socket_watcher->fd();
RemoteUIMsg msg = read<RemoteUIMsg>(socket);
switch (msg)
{
case RemoteUIMsg::MenuShow:
{
2015-10-05 02:25:23 +02:00
auto choices = read_vector<DisplayLine>(socket);
auto anchor = read<CharCoord>(socket);
auto fg = read<Face>(socket);
auto bg = read<Face>(socket);
auto style = read<MenuStyle>(socket);
m_ui->menu_show(choices, anchor, fg, bg, style);
2013-01-04 18:39:13 +01:00
break;
}
case RemoteUIMsg::MenuSelect:
m_ui->menu_select(read<int>(socket));
2013-01-04 18:39:13 +01:00
break;
case RemoteUIMsg::MenuHide:
2013-01-04 18:39:13 +01:00
m_ui->menu_hide();
break;
case RemoteUIMsg::InfoShow:
{
auto title = read<String>(socket);
auto content = read<String>(socket);
auto anchor = read<CharCoord>(socket);
auto face = read<Face>(socket);
auto style = read<InfoStyle>(socket);
m_ui->info_show(title, content, anchor, face, style);
2013-01-04 18:39:13 +01:00
break;
}
case RemoteUIMsg::InfoHide:
2013-01-04 18:39:13 +01:00
m_ui->info_hide();
break;
case RemoteUIMsg::Draw:
{
auto display_buffer = read<DisplayBuffer>(socket);
auto default_face = read<Face>(socket);
auto padding_face = read<Face>(socket);
m_ui->draw(display_buffer, default_face, padding_face);
break;
}
case RemoteUIMsg::DrawStatus:
{
auto status_line = read<DisplayLine>(socket);
auto mode_line = read<DisplayLine>(socket);
auto default_face = read<Face>(socket);
m_ui->draw_status(status_line, mode_line, default_face);
2013-01-04 18:39:13 +01:00
break;
}
case RemoteUIMsg::Refresh:
m_ui->refresh(read<bool>(socket));
break;
case RemoteUIMsg::SetOptions:
m_ui->set_ui_options(read_idmap<String, MemoryDomain::Options>(socket));
break;
}
}
void RemoteClient::write_next_key()
{
2014-11-05 14:57:12 +01:00
Message msg(m_socket_watcher->fd());
2012-12-10 18:46:57 +01:00
// do that before checking dimensions as get_key may
// handle a resize event.
msg.write(m_ui->get_key());
}
void send_command(StringView session, StringView command)
{
2014-11-06 14:54:18 +01:00
int sock = connect_to(session);
if (::write(sock, command.data(), (int)command.length()) < 0)
throw runtime_error(format("unable to write data to socket (fd: {}; errno: {})", sock, ::strerror(errno)));
close(sock);
}
// A client accepter handle a connection until it closes or a nul byte is
// recieved. Everything recieved before is considered to be a command.
//
// * When a nul byte is recieved, the socket is handed to a new Client along
// with the command.
// * When the connection is closed, the command is run in an empty context.
class Server::Accepter
{
public:
Accepter(int socket)
2014-04-05 13:04:37 +02:00
: m_socket_watcher(socket,
[this](FDWatcher&, EventMode mode) {
if (mode == EventMode::Normal)
handle_available_input();
})
2014-04-05 13:04:37 +02:00
{}
private:
void handle_available_input()
{
int socket = m_socket_watcher.fd();
timeval tv{ 0, 0 };
fd_set rfds;
do
{
char c;
int res = ::read(socket, &c, 1);
if (res <= 0)
{
if (not m_buffer.empty()) try
{
Context context{Context::EmptyContextFlag{}};
CommandManager::instance().execute(m_buffer, context);
}
catch (runtime_error& e)
{
write_to_debug_buffer(format("error running command '{}': {}",
m_buffer, e.what()));
}
catch (client_removed&) {}
2013-04-02 14:27:04 +02:00
close(socket);
Server::instance().remove_accepter(this);
return;
}
2014-04-05 13:04:37 +02:00
if (c == 0) // end of initial command stream, go to interactive ui
{
CharCoord dimensions = read<CharCoord>(socket);
EnvVarMap env_vars = read_idmap<String, MemoryDomain::EnvVars>(socket);
std::unique_ptr<UserInterface> ui{new RemoteUI{socket, dimensions}};
2014-04-05 13:04:37 +02:00
ClientManager::instance().create_client(std::move(ui),
std::move(env_vars),
2014-04-05 13:04:37 +02:00
m_buffer);
Server::instance().remove_accepter(this);
return;
}
else
m_buffer += c;
FD_ZERO(&rfds);
FD_SET(socket, &rfds);
}
2013-07-28 16:40:02 +02:00
while (select(socket+1, &rfds, nullptr, nullptr, &tv) == 1);
}
String m_buffer;
FDWatcher m_socket_watcher;
};
Server::Server(String session_name)
: m_session{std::move(session_name)}
{
int listen_sock = socket(AF_UNIX, SOCK_STREAM, 0);
fcntl(listen_sock, F_SETFD, FD_CLOEXEC);
2014-11-06 14:54:18 +01:00
sockaddr_un addr = session_addr(m_session);
make_directory(split_path(addr.sun_path).first);
if (bind(listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1)
throw runtime_error(format("unable to bind listen socket '{}'", addr.sun_path));
if (listen(listen_sock, 4) == -1)
throw runtime_error(format("unable to listen on socket '{}'", addr.sun_path));
auto accepter = [this](FDWatcher& watcher, EventMode mode) {
sockaddr_un client_addr;
socklen_t client_addr_len = sizeof(sockaddr_un);
2014-04-05 13:04:37 +02:00
int sock = accept(watcher.fd(), (sockaddr*) &client_addr,
&client_addr_len);
if (sock == -1)
throw runtime_error("accept failed");
fcntl(sock, F_SETFD, FD_CLOEXEC);
m_accepters.emplace_back(new Accepter{sock});
};
m_listener.reset(new FDWatcher{listen_sock, accepter});
}
bool Server::rename_session(const String& name)
{
String old_socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session);
String new_socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, name);
if (link(old_socket_file.c_str(), new_socket_file.c_str()) != 0)
return false;
unlink(old_socket_file.c_str());
m_session = name;
return true;
}
void Server::close_session(bool do_unlink)
{
if (do_unlink)
{
String socket_file = format("/tmp/kakoune/{}/{}", getpwuid(geteuid())->pw_name, m_session);
unlink(socket_file.c_str());
}
m_listener->close_fd();
2014-01-27 20:53:17 +01:00
m_listener.reset();
}
Server::~Server()
{
2015-08-22 12:27:48 +02:00
if (m_listener)
close_session();
}
void Server::remove_accepter(Accepter* accepter)
{
auto it = find(m_accepters, accepter);
kak_assert(it != m_accepters.end());
m_accepters.erase(it);
}
}