From d6923af3c41ad416a740d45035209010f3cd116c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Tue, 6 Nov 2012 14:25:25 +0100 Subject: [PATCH] server socket filename available through $kak_socket env var --- src/main.cc | 91 ++++++++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 43 deletions(-) diff --git a/src/main.cc b/src/main.cc index 95e0d30b..090ea3f5 100644 --- a/src/main.cc +++ b/src/main.cc @@ -454,6 +454,51 @@ std::unordered_map> keymap = void run_unit_tests(); +struct Server : public Singleton +{ + Server() + { + m_filename = "/tmp/kak-" + int_to_str(getpid()); + + m_listen_sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); + sockaddr_un addr; + addr.sun_family = AF_UNIX; + strncpy(addr.sun_path, m_filename.c_str(), sizeof(addr.sun_path) - 1); + + if (bind(m_listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1) + throw runtime_error("unable to bind listen socket " + m_filename); + + if (listen(m_listen_sock, 4) == -1) + throw runtime_error("unable to listen on socket " + m_filename); + + auto accepter = [=](int socket) { + sockaddr_un client_addr; + socklen_t client_addr_len = sizeof(sockaddr_un); + int sock = accept(socket, (sockaddr*) &client_addr, &client_addr_len); + if (sock == -1) + throw runtime_error("accept failed"); + + auto& buffer = *BufferManager::instance().begin(); + RemoteUI* ui = new RemoteUI{sock}; + ClientManager::instance().create_client( + std::unique_ptr{ui}, *buffer, sock); + }; + EventManager::instance().watch(m_listen_sock, accepter); + } + + ~Server() + { + unlink(m_filename.c_str()); + close(m_listen_sock); + } + + const String& filename() const { return m_filename; } + +private: + int m_listen_sock; + String m_filename; +}; + void register_env_vars() { ShellManager& shell_manager = ShellManager::instance(); @@ -473,6 +518,9 @@ void register_env_vars() shell_manager.register_env_var("reg_.+", [](const String& name, const Context& context) { return RegisterManager::instance()[name[4]].values(context)[0]; }); + shell_manager.register_env_var("socket", + [](const String& name, const Context& context) + { return Server::instance().filename(); }); } void register_registers() @@ -513,49 +561,6 @@ void create_local_client(const String& file) std::unique_ptr{ui}, *buffer, 0); } -struct Server -{ - Server() - { - m_filename = "/tmp/kak-" + int_to_str(getpid()); - - m_listen_sock = socket(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0); - sockaddr_un addr; - addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, m_filename.c_str(), sizeof(addr.sun_path) - 1); - - if (bind(m_listen_sock, (sockaddr*) &addr, sizeof(sockaddr_un)) == -1) - throw runtime_error("unable to bind listen socket " + m_filename); - - if (listen(m_listen_sock, 4) == -1) - throw runtime_error("unable to listen on socket " + m_filename); - - auto accepter = [=](int socket) { - sockaddr_un client_addr; - socklen_t client_addr_len = sizeof(sockaddr_un); - int sock = accept(socket, (sockaddr*) &client_addr, &client_addr_len); - if (sock == -1) - throw runtime_error("accept failed"); - - auto& buffer = *BufferManager::instance().begin(); - RemoteUI* ui = new RemoteUI{sock}; - ClientManager::instance().create_client( - std::unique_ptr{ui}, *buffer, sock); - }; - EventManager::instance().watch(m_listen_sock, accepter); - } - - ~Server() - { - unlink(m_filename.c_str()); - close(m_listen_sock); - } - -private: - int m_listen_sock; - String m_filename; -}; - RemoteClient* connect_to(const String& pid) { auto filename = "/tmp/kak-" + pid;