diff options
author | Vernon Mauery <vernon.mauery@intel.com> | 2018-01-26 13:42:54 -0800 |
---|---|---|
committer | Ed Tanous <ed.tanous@intel.com> | 2018-02-05 22:31:01 +0000 |
commit | 168792a1349438ca10d9e275a41ddb412634c171 (patch) | |
tree | cf2c1b198d69c632564815eae84f78757a1040cf /src | |
parent | 41ff64d7f1615325cbf04b2249a3e9b06f42e704 (diff) | |
download | bmcweb-168792a1349438ca10d9e275a41ddb412634c171.tar.gz bmcweb-168792a1349438ca10d9e275a41ddb412634c171.zip |
Allow for systemd socket activation
If spawned via systemd's socket activation mechanism, use that socket
instead of opening a new one to listen on.
Change-Id: Ia35110902b30b08355edf2fe4041e8377582e72c
Signed-off-by: Vernon Mauery <vernon.mauery@linux.intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/webserver_main.cpp | 29 |
1 files changed, 26 insertions, 3 deletions
diff --git a/src/webserver_main.cpp b/src/webserver_main.cpp index 8060f88..834a5df 100644 --- a/src/webserver_main.cpp +++ b/src/webserver_main.cpp @@ -14,8 +14,31 @@ #include <string> #include <crow/app.h> #include <boost/asio.hpp> +#include <systemd/sd-daemon.h> #include "redfish.hpp" +constexpr int defaultPort = 18080; + +template <typename... Middlewares> +void setup_socket(crow::Crow<Middlewares...>& app) { + int listen_fd = sd_listen_fds(0); + if (1 == listen_fd) { + CROW_LOG_INFO << "attempting systemd socket activation"; + if (sd_is_socket_inet(SD_LISTEN_FDS_START, AF_UNSPEC, SOCK_STREAM, 1, 0)) { + CROW_LOG_INFO << "Starting webserver on socket handle " + << SD_LISTEN_FDS_START; + app.socket(SD_LISTEN_FDS_START); + } else { + CROW_LOG_INFO << "bad incoming socket, starting webserver on port " + << defaultPort; + app.port(defaultPort); + } + } else { + CROW_LOG_INFO << "Starting webserver on port " << defaultPort; + app.port(defaultPort); + } +} + int main(int argc, char** argv) { auto io = std::make_shared<boost::asio::io_service>(); crow::App<crow::PersistentData::Middleware, @@ -44,9 +67,9 @@ int main(int argc, char** argv) { crow::openbmc_mapper::request_routes(app); crow::logger::setLogLevel(crow::LogLevel::INFO); - int port = 18080; - std::cout << "Starting webserver on port " << port << "\n"; - app.port(port); + + CROW_LOG_INFO << "bmcweb (" << __DATE__ << ": " << __TIME__ << ')'; + setup_socket(app); // Start dbus connection crow::connections::system_bus = |