clboss/main.cpp

61 lines
2 KiB
C++
Raw Permalink Normal View History

2020-08-24 05:13:32 +00:00
#include<Boss/Main.hpp>
#include<Boss/open_rpc_socket.hpp>
2020-08-24 02:28:40 +00:00
#include<Ev/Io.hpp>
#include<Ev/start.hpp>
#include<Net/Fd.hpp>
2020-08-24 02:28:40 +00:00
#include<iostream>
#include<memory>
2020-08-24 02:28:40 +00:00
namespace {
Ev::Io<int> io_main(int argc, char **argv) {
2020-08-24 05:13:32 +00:00
auto arg_vec = std::vector<std::string>();
for (int i = 0; i < argc; ++i) {
arg_vec.push_back(std::string(argv[i]));
}
auto main_obj = std::make_shared<Boss::Main>(
arg_vec, std::cin, std::cout, std::cerr,
Boss::open_rpc_socket
);
return main_obj->run().then([main_obj](int ec) {
/* Ensures main_obj is alive! */
return Ev::lift(ec);
2020-08-24 03:13:12 +00:00
});
2020-08-24 02:28:40 +00:00
}
}
2020-08-22 14:33:01 +00:00
int main (int argc, char **argv) {
/* For some reason, doing a direct Ev::start(io_main(argc, argv));
* results in a crash under an edge-case condition.
* This edge-case condition is if data is immediately fed to
* the stdin of clboss.
* You can tickle it (reverting to Ev::start(io_main(argc, argv))
* and recompiling first) with this:
*
* echo '{"id": 0, "method": "getmanifest", "params": {}}' | ./clboss
*
* Note that if you just invoke clboss and input the above text
* manually, it will work perfectly fine, since most humans have
* reflex times measurable in dozens or hundreds of milliseconds.
*
* This does not normally happen in the usual case since the
* lightningd usually takes a bit of time before it actually
* emits the getmanifest call, and that is sufficient for the
* bug to leave the edge-case condition.
* On the other hand, a Sufficiently Fast (TM) processor might
* re-tickle this bug someday in the future even when run on
* some lightningd.
*
* This may be a bug in the C++ library or GCC itself, not
* CLBOSS.
* Why?
* Because the crash reports an error somewhere in the guts of
* malloc, and if you run clboss through valgrind, it does not
* crash even with the direct Ev::start(io_main(argc, argv)).
* valgrind works by replacing the malloc implementation, so ---
*/
auto code = io_main(argc, argv);
return Ev::start(code);
2020-08-22 14:33:01 +00:00
}