Skip to content

Commit

Permalink
was/Stock: add MOUNT_LISTEN_STREAM support
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jul 31, 2024
1 parent 9f6f237 commit 7c4f437
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 3 deletions.
2 changes: 1 addition & 1 deletion debian/changelog
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cm4all-beng-proxy (18.0.85) unstable; urgency=low

* fcgi: fix crash bug on error during early process initialization
* fcgi: add MOUNT_LISTEN_STREAM support
* fcgi, was: add MOUNT_LISTEN_STREAM support

--

Expand Down
1 change: 1 addition & 0 deletions src/bp/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ try {
#ifdef HAVE_LIBWAS
instance.was_stock = new WasStock(instance.event_loop,
*instance.spawn_service,
instance.listen_stream_spawn_stock.get(),
child_log_socket, child_log_options,
instance.config.was_stock_limit,
instance.config.was_stock_max_idle);
Expand Down
27 changes: 26 additions & 1 deletion src/was/Launch.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
// author: Max Kellermann <[email protected]>

#include "Launch.hxx"
#include "pool/tpool.hxx"
#include "spawn/Interface.hxx"
#include "spawn/ListenStreamSpawnStock.hxx"
#include "spawn/Mount.hxx"
#include "spawn/Prepared.hxx"
#include "spawn/ChildOptions.hxx"
#include "net/SocketDescriptor.hxx"
#include "io/FdHolder.hxx"
#include "AllocatorPtr.hxx"

static auto
WasLaunch(SpawnService &spawn_service,
ListenStreamSpawnStock *listen_stream_spawn_stock,
SharedLease &listen_stream_lease,
const char *name,
const char *executable_path,
std::span<const char *const> args,
Expand All @@ -23,6 +29,22 @@ WasLaunch(SpawnService &spawn_service,
p.stdout_fd = socket.output;
p.stdin_fd = socket.input;

const TempPoolLease tpool;
if (p.ns.mount.mount_listen_stream.data() != nullptr) {
if (listen_stream_spawn_stock == nullptr)
throw std::runtime_error{"No ListenStreamSpawnStock"};

const AllocatorPtr alloc{tpool};

/* copy the mount list before editing it, which is
currently a shallow copy pointing to inside the
translation cache*/
p.ns.mount.mounts = Mount::CloneAll(alloc, p.ns.mount.mounts);

listen_stream_lease = listen_stream_spawn_stock->Apply(alloc,
p.ns.mount);
}

p.Append(executable_path);
for (auto i : args)
p.Append(i);
Expand All @@ -38,6 +60,7 @@ WasLaunch(SpawnService &spawn_service,

WasProcess
was_launch(SpawnService &spawn_service,
ListenStreamSpawnStock *listen_stream_spawn_stock,
const char *name,
const char *executable_path,
std::span<const char *const> args,
Expand All @@ -50,7 +73,9 @@ was_launch(SpawnService &spawn_service,
process.input.SetNonBlocking();
process.output.SetNonBlocking();

process.handle = WasLaunch(spawn_service, name, executable_path, args,
process.handle = WasLaunch(spawn_service, listen_stream_spawn_stock,
process.listen_stream_lease,
name, executable_path, args,
options, std::move(stderr_fd),
std::move(s.second));
return process;
Expand Down
8 changes: 8 additions & 0 deletions src/was/Launch.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,24 @@

#include "was/async/Socket.hxx"
#include "spawn/ProcessHandle.hxx"
#include "util/SharedLease.hxx"

#include <memory>
#include <span>

class SpawnService;
class ListenStreamSpawnStock;
class ChildProcessHandle;
struct ChildOptions;

struct WasProcess : WasSocket {
std::unique_ptr<ChildProcessHandle> handle;

/**
* A lease obtained from #ListenStreamSpawnStock.
*/
SharedLease listen_stream_lease;

WasProcess() = default;

explicit WasProcess(WasSocket &&_socket) noexcept
Expand All @@ -30,6 +37,7 @@ struct WasProcess : WasSocket {
*/
WasProcess
was_launch(SpawnService &spawn_service,
ListenStreamSpawnStock *listen_stream_spawn_stock,
const char *name,
const char *executable_path,
std::span<const char *const> args,
Expand Down
5 changes: 5 additions & 0 deletions src/was/Stock.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class WasChild final : public WasStockConnection, ExitListener {

std::unique_ptr<ChildProcessHandle> handle;

SharedLease listen_stream_lease;

const bool disposable;

public:
Expand All @@ -63,6 +65,7 @@ class WasChild final : public WasStockConnection, ExitListener {
const ChildErrorLogOptions &log_options) {
auto process =
was_launch(was_stock.GetSpawnService(),
was_stock.GetListenStreamSpawnStock(),
GetStockName(),
params.executable_path,
params.args,
Expand All @@ -74,6 +77,8 @@ class WasChild final : public WasStockConnection, ExitListener {
handle = std::move(process.handle);
handle->SetExitListener(*this);

listen_stream_lease = std::move(process.listen_stream_lease);

WasSocket &socket = process;
Open(std::move(socket));
}
Expand Down
8 changes: 8 additions & 0 deletions src/was/Stock.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ struct pool;
struct ChildOptions;
struct WasSocket;
class SpawnService;
class ListenStreamSpawnStock;

/**
* Launch and manage WAS child processes.
*/
class WasStock final : StockClass {
SpawnService &spawn_service;
ListenStreamSpawnStock *const listen_stream_spawn_stock;
const SocketDescriptor log_socket;
const ChildErrorLogOptions log_options;

Expand All @@ -38,10 +40,12 @@ class WasStock final : StockClass {

public:
explicit WasStock(EventLoop &event_loop, SpawnService &_spawn_service,
ListenStreamSpawnStock *_listen_stream_spawn_stock,
const SocketDescriptor _log_socket,
const ChildErrorLogOptions &_log_options,
unsigned limit, unsigned max_idle) noexcept
:spawn_service(_spawn_service),
listen_stream_spawn_stock(_listen_stream_spawn_stock),
log_socket(_log_socket), log_options(_log_options),
stock(event_loop, *this, limit, max_idle,
std::chrono::minutes(10)) {}
Expand All @@ -50,6 +54,10 @@ public:
return spawn_service;
}

auto *GetListenStreamSpawnStock() const noexcept {
return listen_stream_spawn_stock;
}

void FadeAll() noexcept {
stock.FadeAll();
}
Expand Down
1 change: 1 addition & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ if libwas.found()
executable(
'run_was',
'run_was.cxx',
'../src/spawn/ListenStreamSpawnStock.cxx',
include_directories: inc,
dependencies: [
test_instance_dep,
Expand Down
2 changes: 1 addition & 1 deletion test/run_was.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ try {
LocalSpawnService spawn_service(spawn_config, context.event_loop,
child_process_registry);

context.process = was_launch(spawn_service, "was",
context.process = was_launch(spawn_service, nullptr, "was",
argv[1], {},
child_options, {});

Expand Down

0 comments on commit 7c4f437

Please sign in to comment.