scm: support --listen flag for uds
Support the --listen flag to mu scm, to start listening on a Unix domain socket.
This commit is contained in:
@ -17,8 +17,7 @@ mu_scm_dir=join_paths(datadir, 'mu', 'scm')
|
||||
mu_scm_dir_arg='-DMU_SCM_DIR="' + mu_scm_dir + '"'
|
||||
|
||||
lib_mu_scm=static_library(
|
||||
'mu-scm',
|
||||
[
|
||||
'mu-scm', [
|
||||
'mu-scm.cc',
|
||||
'mu-scm-message.cc',
|
||||
'mu-scm-mime.cc',
|
||||
@ -33,7 +32,9 @@ lib_mu_scm=static_library(
|
||||
install: false,
|
||||
cpp_args: [mu_scm_dir_arg])
|
||||
|
||||
install_data(['mu-scm.scm', 'mu-scm-shell.scm'], install_dir : mu_scm_dir)
|
||||
install_data(['mu-scm.scm',
|
||||
'mu-scm-repl.scm'],
|
||||
install_dir : mu_scm_dir)
|
||||
|
||||
# note: top-level meson.build defines a dummy replacement for this.
|
||||
mu_scm_dep = declare_dependency(
|
||||
|
||||
@ -13,6 +13,16 @@
|
||||
;; You should have received a copy of the GNU General Public License
|
||||
;; along with this program; if not, write to the Free Software Foundation,
|
||||
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
(use-modules (system repl server)
|
||||
(ice-9 threads))
|
||||
(use-modules (mu))
|
||||
|
||||
;; when a socket path is defined, listen on it (blocking)
|
||||
;; after printing UNIX-CONNECT:<socket-file>\n on stdout
|
||||
(let ((socket-path (getenv "MU_SCM_SOCKET_PATH")))
|
||||
(when socket-path
|
||||
(format #t "UNIX-CONNECT:~a\n" socket-path)
|
||||
(run-server
|
||||
(make-unix-domain-server-socket #:path socket-path))))
|
||||
|
||||
(display "Welcome to the mu shell!\n\n")
|
||||
(use-modules (mu))
|
||||
@ -216,7 +216,6 @@ init_subrs()
|
||||
#pragma GCC diagnostic pop
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Mu::Scm::init_store(const Store& store)
|
||||
{
|
||||
|
||||
@ -85,7 +85,9 @@ make_mu_scm_path(const std::string& fname) {
|
||||
|
||||
namespace {
|
||||
static std::string mu_scm_path;
|
||||
static std::string mu_scm_shell_path;
|
||||
static std::string mu_scm_repl_path;
|
||||
static std::string mu_scm_socket_path;
|
||||
constexpr auto SOCKET_PATH_ENV = "MU_SCM_SOCKET_PATH";
|
||||
}
|
||||
|
||||
static Result<void>
|
||||
@ -104,12 +106,11 @@ prepare_run(const Mu::Scm::Config& conf)
|
||||
else
|
||||
return Err(path.error());
|
||||
|
||||
if (const auto path = make_mu_scm_path("mu-scm-shell.scm"); path)
|
||||
mu_scm_shell_path = *path;
|
||||
if (const auto path = make_mu_scm_path("mu-scm-repl.scm"); path)
|
||||
mu_scm_repl_path = *path;
|
||||
else
|
||||
return Err(path.error());
|
||||
|
||||
|
||||
if (config->options.scm.script_path) {
|
||||
const auto path{config->options.scm.script_path->c_str()};
|
||||
if (const auto res = ::access(path, R_OK); res != 0) {
|
||||
@ -121,9 +122,28 @@ prepare_run(const Mu::Scm::Config& conf)
|
||||
return Ok();
|
||||
}
|
||||
|
||||
Result<void>
|
||||
Mu::Scm::run(const Mu::Scm::Config& conf) {
|
||||
// make a unique unix-socket path
|
||||
static std::string
|
||||
maybe_set_uds_path(bool set)
|
||||
{
|
||||
if (set) {
|
||||
GRand* grand{g_rand_new()};
|
||||
auto path = join_paths(g_get_user_runtime_dir(),
|
||||
mu_format("mu-scm-socket-{:08x}",
|
||||
g_rand_int(grand)));
|
||||
g_rand_free(grand);
|
||||
g_setenv(SOCKET_PATH_ENV, path.c_str(), 1);
|
||||
return path;
|
||||
} else {
|
||||
g_unsetenv(SOCKET_PATH_ENV);
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Result<void>
|
||||
Mu::Scm::run(const Mu::Scm::Config& conf)
|
||||
{
|
||||
if (const auto res = prepare_run(conf); !res)
|
||||
return Err(res.error());
|
||||
|
||||
@ -151,10 +171,13 @@ Mu::Scm::run(const Mu::Scm::Config& conf) {
|
||||
"-c", cmd.c_str()})
|
||||
args.emplace_back(arg);
|
||||
} else {
|
||||
// otherwise, drop us into an interactive shell/repl (and
|
||||
// shell spec)
|
||||
// otherwise, drop us into an interactive shell/repl
|
||||
// or start listening on a domain socket.
|
||||
mu_scm_socket_path =
|
||||
maybe_set_uds_path(config->options.scm.listen);
|
||||
args.emplace_back("--no-auto-compile");
|
||||
args.emplace_back("-l");
|
||||
args.emplace_back(mu_scm_shell_path.c_str());
|
||||
args.emplace_back(mu_scm_repl_path.c_str());
|
||||
}
|
||||
/* ahem...*/
|
||||
scm_shell(std::size(args), const_cast<char**>(args.data()));
|
||||
|
||||
Reference in New Issue
Block a user