kcsbridge: Add alternative daemon

This removes the unused signal handling logic and ensures that the main
event loop only drives timeouts from the management channel, and
transitions back to reading when needed.

Change-Id: I01a70de8b301b5ba0b417922fd6a6555433e18b7
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/.gitignore b/.gitignore
index 90752d7..4849188 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
 /**/build*/
 /**/subprojects/*/
 !/**/subprojects/acpi-power-state-daemon/
+!/**/subprojects/kcsbridge/
 !/**/subprojects/metrics-ipmi-blobs/
 !/**/subprojects/ncsid/
 /**/subprojects/*.wrap
diff --git a/kcsbridge b/kcsbridge
new file mode 120000
index 0000000..bf7dcf7
--- /dev/null
+++ b/kcsbridge
@@ -0,0 +1 @@
+subprojects/kcsbridge
\ No newline at end of file
diff --git a/meson.build b/meson.build
index df87700..12ae455 100644
--- a/meson.build
+++ b/meson.build
@@ -35,5 +35,6 @@
 endif
 
 subproject('acpi-power-state-daemon')
+subproject('kcsbridge')
 subproject('ncsid', default_options: 'tests=' + tests_str)
 subproject('metrics-ipmi-blobs', default_options: 'tests=' + tests_str)
diff --git a/subprojects/kcsbridge/meson.build b/subprojects/kcsbridge/meson.build
new file mode 100644
index 0000000..3d14785
--- /dev/null
+++ b/subprojects/kcsbridge/meson.build
@@ -0,0 +1,26 @@
+# Copyright 2021 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+project(
+  'kcsbridge',
+  ['cpp', 'c'],
+  version: '0.1',
+  meson_version: '>=0.53.2',
+  default_options: [
+    'warning_level=3',
+    'werror=true',
+    'cpp_std=c++17',
+  ])
+
+subdir('src')
diff --git a/subprojects/kcsbridge/src/args.cpp b/subprojects/kcsbridge/src/args.cpp
new file mode 100644
index 0000000..c96ac4b
--- /dev/null
+++ b/subprojects/kcsbridge/src/args.cpp
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "args.hpp"
+
+#include <fmt/format.h>
+#include <getopt.h>
+
+#include <stdexcept>
+
+namespace kcsbridge
+{
+
+Args::Args(int argc, char* argv[])
+{
+    static const char opts[] = ":c:";
+    static const struct option longopts[] = {
+        {"channel", required_argument, nullptr, 'c'},
+        {nullptr, 0, nullptr, 0},
+    };
+    int c;
+    optind = 0;
+    while ((c = getopt_long(argc, argv, opts, longopts, nullptr)) > 0)
+    {
+        switch (c)
+        {
+            case 'c':
+                channel = optarg;
+                break;
+            case ':':
+                throw std::runtime_error(
+                    fmt::format("Missing argument for `{}`", argv[optind - 1]));
+                break;
+            default:
+                throw std::runtime_error(fmt::format(
+                    "Invalid command line argument `{}`", argv[optind - 1]));
+        }
+    }
+    if (optind != argc)
+    {
+        throw std::invalid_argument("Requires no additional arguments");
+    }
+    if (channel == nullptr)
+    {
+        throw std::invalid_argument("Missing KCS channel");
+    }
+}
+
+} // namespace kcsbridge
diff --git a/subprojects/kcsbridge/src/args.hpp b/subprojects/kcsbridge/src/args.hpp
new file mode 100644
index 0000000..39b28c1
--- /dev/null
+++ b/subprojects/kcsbridge/src/args.hpp
@@ -0,0 +1,30 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <cstddef>
+
+namespace kcsbridge
+{
+
+struct Args
+{
+    const char* channel = nullptr;
+
+    Args(int argc, char* argv[]);
+};
+
+} // namespace kcsbridge
diff --git a/subprojects/kcsbridge/src/dbus.hpp b/subprojects/kcsbridge/src/dbus.hpp
new file mode 100644
index 0000000..7a92d2d
--- /dev/null
+++ b/subprojects/kcsbridge/src/dbus.hpp
@@ -0,0 +1,95 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+#include <fmt/format.h>
+#include <systemd/sd-bus.h>
+
+#include <sdbusplus/message.hpp>
+#include <stdplus/handle/managed.hpp>
+#include <stdplus/util/cexec.hpp>
+
+#include <limits>
+#include <stdexcept>
+#include <type_traits>
+#include <utility>
+
+namespace kcsbridge
+{
+
+struct SdBusSlotDrop
+{
+    void operator()(sd_bus_slot* slot) noexcept
+    {
+        sd_bus_slot_unref(slot);
+    }
+};
+using ManagedSdBusSlot = stdplus::Managed<sd_bus_slot*>::HandleF<SdBusSlotDrop>;
+
+template <typename CbT>
+int busCallAsyncCb(sd_bus_message* m, void* userdata, sd_bus_error*) noexcept
+{
+    try
+    {
+        (*reinterpret_cast<CbT*>(userdata))(sdbusplus::message::message(m));
+    }
+    catch (const std::exception& e)
+    {
+        fmt::print(stderr, "Callback failed: {}\n", e.what());
+    }
+    return 1;
+}
+
+template <typename CbT>
+void busCallAsyncDest(void* userdata) noexcept
+{
+    delete reinterpret_cast<CbT*>(userdata);
+}
+
+template <typename Cb>
+auto busCallAsync(sdbusplus::message::message&& m, Cb&& cb)
+{
+    sd_bus_slot* slot;
+    using CbT = std::remove_cv_t<std::remove_reference_t<Cb>>;
+    CHECK_RET(sd_bus_call_async(nullptr, &slot, m.get(), busCallAsyncCb<CbT>,
+                                nullptr, std::numeric_limits<uint64_t>::max()),
+              "sd_bus_call_async");
+    ManagedSdBusSlot ret(std::move(slot));
+    CHECK_RET(sd_bus_slot_set_destroy_callback(*ret, busCallAsyncDest<CbT>),
+              "sd_bus_slot_set_destroy_callback");
+    sd_bus_slot_set_userdata(*ret, new CbT(std::forward<Cb>(cb)));
+    return ret;
+}
+
+template <auto func, typename Data>
+int methodRsp(sd_bus_message* mptr, void* dataptr, sd_bus_error* error) noexcept
+{
+    sdbusplus::message::message m(mptr);
+    try
+    {
+        func(m, *reinterpret_cast<Data*>(dataptr));
+    }
+    catch (const std::exception& e)
+    {
+        fmt::print(stderr, "Method response failed: {}\n", e.what());
+        sd_bus_error_set(error,
+                         "xyz.openbmc_project.Common.Error.InternalFailure",
+                         "The operation failed internally.");
+    }
+    return 1;
+}
+
+} // namespace kcsbridge
diff --git a/subprojects/kcsbridge/src/kcsbridge@.service.in b/subprojects/kcsbridge/src/kcsbridge@.service.in
new file mode 100644
index 0000000..ef62d5f
--- /dev/null
+++ b/subprojects/kcsbridge/src/kcsbridge@.service.in
@@ -0,0 +1,11 @@
+[Unit]
+Description=Google IPMI KCS Bridge
+After=phosphor-ipmi-host.service
+
+[Service]
+Restart=on-failure
+Type=notify
+ExecStart=@BIN@ -c '%I'
+
+[Install]
+WantedBy=multi-user.target
diff --git a/subprojects/kcsbridge/src/main.cpp b/subprojects/kcsbridge/src/main.cpp
new file mode 100644
index 0000000..8a7114b
--- /dev/null
+++ b/subprojects/kcsbridge/src/main.cpp
@@ -0,0 +1,206 @@
+/*
+ * Copyright 2021 Google LLC
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "args.hpp"
+#include "dbus.hpp"
+
+#include <fmt/format.h>
+#include <linux/ipmi_bmc.h>
+#include <systemd/sd-daemon.h>
+
+#include <sdbusplus/bus.hpp>
+#include <sdbusplus/exception.hpp>
+#include <sdbusplus/server/interface.hpp>
+#include <sdbusplus/vtable.hpp>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/io.hpp>
+#include <sdeventplus/source/signal.hpp>
+#include <stdplus/fd/create.hpp>
+#include <stdplus/fd/ops.hpp>
+#include <stdplus/signal.hpp>
+
+#include <array>
+#include <map>
+#include <stdexcept>
+#include <tuple>
+#include <utility>
+#include <variant>
+
+namespace kcsbridge
+{
+
+using sdbusplus::bus::bus;
+using sdbusplus::message::message;
+using sdeventplus::source::IO;
+using sdeventplus::source::Signal;
+using stdplus::fd::OpenAccess;
+using stdplus::fd::OpenFlag;
+using stdplus::fd::OpenFlags;
+
+void setAttention(message& m, stdplus::Fd& kcs)
+{
+    stdplus::fd::ioctl(kcs, IPMI_BMC_IOCTL_SET_SMS_ATN, nullptr);
+    m.new_method_return().method_return();
+}
+
+void clearAttention(message& m, stdplus::Fd& kcs)
+{
+    stdplus::fd::ioctl(kcs, IPMI_BMC_IOCTL_CLEAR_SMS_ATN, nullptr);
+    m.new_method_return().method_return();
+}
+
+void forceAbort(message& m, stdplus::Fd& kcs)
+{
+    stdplus::fd::ioctl(kcs, IPMI_BMC_IOCTL_FORCE_ABORT, nullptr);
+    m.new_method_return().method_return();
+}
+
+template <typename Data>
+constexpr sdbusplus::vtable::vtable_t dbusMethods[] = {
+    sdbusplus::vtable::start(),
+    sdbusplus::vtable::method("setAttention", "", "",
+                              methodRsp<setAttention, Data>),
+    sdbusplus::vtable::method("clearAttention", "", "",
+                              methodRsp<clearAttention, Data>),
+    sdbusplus::vtable::method("forceAbort", "", "",
+                              methodRsp<forceAbort, Data>),
+    sdbusplus::vtable::end(),
+};
+
+void write(stdplus::Fd& kcs, message&& m)
+{
+    std::array<uint8_t, 1024> buffer;
+    stdplus::span<uint8_t> out(buffer.begin(), 3);
+    try
+    {
+        if (m.is_method_error())
+        {
+            // Extra copy to workaround lack of `const sd_bus_error` constructor
+            auto error = *m.get_error();
+            throw sdbusplus::exception::SdBusError(&error, "ipmid response");
+        }
+        std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>
+            ret;
+        m.read(ret);
+        const auto& [netfn, lun, cmd, cc, data] = ret;
+        // Based on the IPMI KCS spec Figure 9-2
+        // netfn needs to be changed to odd in KCS responses
+        buffer[0] = (netfn | 1) << 2;
+        buffer[0] |= lun;
+        buffer[1] = cmd;
+        buffer[2] = cc;
+        memcpy(&buffer[3], data.data(), data.size());
+        out = stdplus::span<uint8_t>(buffer.begin(), data.size() + 3);
+    }
+    catch (const std::exception& e)
+    {
+        fmt::print(stderr, "IPMI response failure: {}\n", e.what());
+        buffer[0] |= 1 << 2;
+        buffer[2] = 0xff;
+    }
+    stdplus::fd::writeExact(kcs, out);
+}
+
+void read(stdplus::Fd& kcs, bus& bus, ManagedSdBusSlot& slot)
+{
+    std::array<uint8_t, 1024> buffer;
+    auto in = stdplus::fd::read(kcs, buffer);
+    if (in.empty())
+    {
+        return;
+    }
+    if (slot)
+    {
+        fmt::print(stderr, "Canceling outstanding request\n");
+        slot.reset();
+    }
+    if (in.size() < 2)
+    {
+        fmt::print(stderr, "Read too small, ignoring\n");
+        return;
+    }
+    auto m = bus.new_method_call("xyz.openbmc_project.Ipmi.Host",
+                                 "/xyz/openbmc_project/Ipmi",
+                                 "xyz.openbmc_project.Ipmi.Server", "execute");
+    std::map<std::string, std::variant<int>> options;
+    // Based on the IPMI KCS spec Figure 9-1
+    uint8_t netfn = in[0] >> 2, lun = in[0] & 3, cmd = in[1];
+    m.append(netfn, lun, cmd, in.subspan(2), options);
+    slot = busCallAsync(std::move(m), [&](message&& m) {
+        slot.reset();
+        write(kcs, std::move(m));
+    });
+}
+
+int execute(const char* channel)
+{
+    // Set up our DBus and event loop
+    auto event = sdeventplus::Event::get_default();
+    auto bus = sdbusplus::bus::new_default();
+    bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
+
+    // Configure basic signal handling
+    auto exit_handler = [&](Signal&, const struct signalfd_siginfo*) {
+        fmt::print(stderr, "Interrupted, Exiting\n");
+        event.exit(0);
+    };
+    stdplus::signal::block(SIGINT);
+    Signal sig_int(event, SIGINT, exit_handler);
+    stdplus::signal::block(SIGTERM);
+    Signal sig_term(event, SIGTERM, exit_handler);
+
+    // Open an FD for the KCS channel
+    stdplus::ManagedFd kcs = stdplus::fd::open(
+        fmt::format("/dev/{}", channel),
+        OpenFlags(OpenAccess::ReadWrite).set(OpenFlag::NonBlock));
+    ManagedSdBusSlot slot(std::nullopt);
+
+    // Add a reader to the bus for handling inbound IPMI
+    IO ioSource(event, kcs.get(), EPOLLIN | EPOLLET,
+                [&](IO&, int, uint32_t) { read(kcs, bus, slot); });
+
+    // Allow processes to affect the state machine
+    std::optional<sdbusplus::server::interface::interface> intf;
+    {
+        std::string dbusChannel = channel;
+        std::replace(dbusChannel.begin(), dbusChannel.end(), '-', '_');
+        auto obj = "/xyz/openbmc_project/Ipmi/Channel/" + dbusChannel;
+        auto srv = "com.google.gbmc." + dbusChannel;
+        intf.emplace(bus, obj.c_str(), "xyz.openbmc_project.Ipmi.Channel.SMS",
+                     dbusMethods<stdplus::Fd>,
+                     reinterpret_cast<stdplus::Fd*>(&kcs));
+        bus.request_name(srv.c_str());
+    }
+
+    sd_notify(0, "READY=1");
+    return event.loop();
+}
+
+} // namespace kcsbridge
+
+int main(int argc, char* argv[])
+{
+    try
+    {
+        kcsbridge::Args args(argc, argv);
+        return kcsbridge::execute(args.channel);
+    }
+    catch (const std::exception& e)
+    {
+        fmt::print(stderr, "FAILED: {}\n", e.what());
+        return 1;
+    }
+}
diff --git a/subprojects/kcsbridge/src/meson.build b/subprojects/kcsbridge/src/meson.build
new file mode 100644
index 0000000..f463d92
--- /dev/null
+++ b/subprojects/kcsbridge/src/meson.build
@@ -0,0 +1,68 @@
+# Copyright 2021 Google LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+headers = include_directories('.')
+
+fmt_dep = dependency('fmt', required: false)
+if not fmt_dep.found()
+  fmt_proj = import('cmake').subproject(
+    'fmt',
+    cmake_options: [
+      '-DCMAKE_POSITION_INDEPENDENT_CODE=ON',
+      '-DMASTER_PROJECT=OFF'
+    ],
+    required: false)
+  assert(fmt_proj.found(), 'fmtlib is required')
+  fmt_dep = fmt_proj.dependency('fmt')
+endif
+
+deps = [
+  fmt_dep,
+  dependency('stdplus', fallback: ['stdplus', 'stdplus']),
+  dependency('sdbusplus', fallback: ['sdbusplus', 'sdbusplus_dep']),
+  dependency('sdeventplus', fallback: ['sdeventplus', 'sdeventplus']),
+  dependency('libsystemd'),
+]
+
+lib = static_library(
+  'kcsbridged',
+  'args.cpp',
+  include_directories: headers,
+  implicit_include_directories: false,
+  dependencies: deps)
+
+dep = declare_dependency(
+  dependencies: deps,
+  include_directories: headers,
+  link_with: lib)
+
+libexecdir = get_option('prefix') / get_option('libexecdir')
+
+executable(
+  'kcsbridged',
+  'main.cpp',
+  implicit_include_directories: false,
+  dependencies: dep,
+  install: true,
+  install_dir: libexecdir)
+
+systemd = dependency('systemd', required: false)
+if systemd.found()
+  configure_file(
+    configuration: {'BIN': libexecdir / 'kcsbridged'},
+    input: 'kcsbridge@.service.in',
+    output: 'kcsbridge@.service',
+    install_mode: 'rw-r--r--',
+    install_dir: systemd.get_pkgconfig_variable('systemdsystemunitdir'))
+endif
diff --git a/subprojects/kcsbridge/subprojects b/subprojects/kcsbridge/subprojects
new file mode 120000
index 0000000..a96aa0e
--- /dev/null
+++ b/subprojects/kcsbridge/subprojects
@@ -0,0 +1 @@
+..
\ No newline at end of file
diff --git a/subprojects/sdeventplus.wrap b/subprojects/sdeventplus.wrap
new file mode 100644
index 0000000..7503664
--- /dev/null
+++ b/subprojects/sdeventplus.wrap
@@ -0,0 +1,3 @@
+[wrap-git]
+url = https://github.com/openbmc/sdeventplus
+revision = HEAD