diff options
author | Rapkiewicz, Pawel <pawel.rapkiewicz@intel.com> | 2019-06-17 16:29:12 +0300 |
---|---|---|
committer | Rapkiewicz, Pawel <pawel.rapkiewicz@intel.com> | 2019-08-27 10:58:23 +0300 |
commit | b708fd4d83297bd77471db51de85ca6f6b0963b7 (patch) | |
tree | 99cf78be9e0c1ceecf57e663fe427baa45c3ae2c | |
parent | 197f412ce7aafe23f4aca762c4ca54a78f9523c0 (diff) | |
download | provingground-b708fd4d83297bd77471db51de85ca6f6b0963b7.tar.xz |
Initial version of Virtual-Media
This is initial version of virtual media support
this covers:
* udev monitoring
* configuration reading
* exposing appropriate interfaces on dbus
* allows mount/umount images from existing unix socket
Does not cover:
* configuration of usb gadget
Integration with bmcweb will be delivered to bmcweb
Change-Id: I358ab80fe32a7ed933007143bfa00da847a95316
Signed-off-by: Rapkiewicz, Pawel <pawel.rapkiewicz@intel.com>
Signed-off-by: Kowalski, Kamil <kamil.kowalski@intel.com>
-rw-r--r-- | virtual-media/.clang-format | 98 | ||||
-rw-r--r-- | virtual-media/CMakeLists.txt | 144 | ||||
-rw-r--r-- | virtual-media/LICENSE | 201 | ||||
-rw-r--r-- | virtual-media/cmake/Findudev.cmake | 77 | ||||
-rw-r--r-- | virtual-media/src/main.cpp | 722 | ||||
-rw-r--r-- | virtual-media/virtual-media.json | 22 | ||||
-rw-r--r-- | virtual-media/xyz.openbmc_project.VirtualMedia.service | 11 |
7 files changed, 1275 insertions, 0 deletions
diff --git a/virtual-media/.clang-format b/virtual-media/.clang-format new file mode 100644 index 0000000..ae9ad39 --- /dev/null +++ b/virtual-media/.clang-format @@ -0,0 +1,98 @@ +--- +Language: Cpp +# BasedOnStyle: LLVM +AccessModifierOffset: -2 +AlignAfterOpenBracket: Align +AlignConsecutiveAssignments: false +AlignConsecutiveDeclarations: false +AlignEscapedNewlinesLeft: false +AlignOperands: true +AlignTrailingComments: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: false +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: true +BinPackArguments: true +BinPackParameters: true +BraceWrapping: + AfterClass: true + AfterControlStatement: true + AfterEnum: true + AfterFunction: true + AfterNamespace: true + AfterObjCDeclaration: true + AfterStruct: true + AfterUnion: true + BeforeCatch: true + BeforeElse: true + IndentBraces: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: AfterColon +ColumnLimit: 80 +CommentPragmas: '^ IWYU pragma:' +ConstructorInitializerAllOnOneLineOrOnePerLine: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +PointerAlignment: Left +DisableFormat: false +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ] +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^[<"](gtest|gmock)' + Priority: 5 + - Regex: '^"config.h"' + Priority: -1 + - Regex: '^".*\.hpp"' + Priority: 1 + - Regex: '^<.*\.h>' + Priority: 2 + - Regex: '^<.*' + Priority: 3 + - Regex: '.*' + Priority: 4 +IndentCaseLabels: true +IndentWidth: 4 +IndentWrappedFunctionNames: true +KeepEmptyLinesAtTheStartOfBlocks: true +MacroBlockBegin: '' +MacroBlockEnd: '' +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBlockIndentWidth: 2 +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakString: 1000 +PenaltyExcessCharacter: 1000000 +PenaltyReturnTypeOnItsOwnLine: 60 +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeParens: ControlStatements +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: false +SpacesInContainerLiterals: true +SpacesInCStyleCastParentheses: false +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Cpp11 +TabWidth: 4 +UseTab: Never +... diff --git a/virtual-media/CMakeLists.txt b/virtual-media/CMakeLists.txt new file mode 100644 index 0000000..60af112 --- /dev/null +++ b/virtual-media/CMakeLists.txt @@ -0,0 +1,144 @@ +cmake_minimum_required(VERSION 3.5 FATAL_ERROR) + +project(VirtualMedia CXX) + +cmake_policy(SET CMP0054 NEW) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH}) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fno-rtti") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Os -flto") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0") + +option(YOCTO_DEPENDENCIES "Use YOCTO dependencies system" OFF) + +option(VM_USE_VALGRIND "Build VirtualMedia to work with valgrind" OFF) + +if(NOT ${YOCTO_DEPENDENCIES}) + include(ExternalProject) + + externalproject_add(sdbusplus-project + PREFIX + ${CMAKE_BINARY_DIR}/sdbusplus-project + GIT_REPOSITORY + https://github.com/openbmc/sdbusplus.git + GIT_TAG + c08cf5283b80a071d19506d9a462f6c69e1797f1 + SOURCE_DIR + ${CMAKE_BINARY_DIR}/sdbusplus-src + BINARY_DIR + ${CMAKE_BINARY_DIR}/sdbusplus-build + CONFIGURE_COMMAND + "" + BUILD_COMMAND + cd + ${CMAKE_BINARY_DIR}/sdbusplus-src + && + ./bootstrap.sh + && + ./configure + --enable-transaction + && + make + -j + libsdbusplus.la + INSTALL_COMMAND + "" + LOG_DOWNLOAD + ON + UPDATE_COMMAND + "") + + externalproject_add(nlohmann-json + GIT_REPOSITORY + "https://github.com/nlohmann/json.git" + GIT_TAG + aafad2be1f3cd259a1e79d2f6fcf267d1ede9ec7 + SOURCE_DIR + "${CMAKE_BINARY_DIR}/nlohmann-json-src" + BINARY_DIR + "${CMAKE_BINARY_DIR}/nlohmann-json-build" + CONFIGURE_COMMAND + "" + BUILD_COMMAND + "" + INSTALL_COMMAND + mkdir + -p + "${CMAKE_BINARY_DIR}/prefix/include/nlohmann" + && + cp + -r + "${CMAKE_BINARY_DIR}/nlohmann-json-src/include/nlohmann" + "${CMAKE_BINARY_DIR}/prefix/include" + UPDATE_COMMAND + "") + + include_directories(${CMAKE_BINARY_DIR}/prefix/include) + include_directories(${CMAKE_BINARY_DIR}/sdbusplus-src) + + link_directories(${CMAKE_BINARY_DIR}/sdbusplus-src/.libs) + +endif() + +# Include UDEV library +find_package(udev REQUIRED) +include_directories(${UDEV_INCLUDE_DIRS}) +link_directories(${UDEV_LIBRARIES}) + +# Include Boost library This allows specify exact version of BOOST to be used, +# especially important while using valgrind, to point BOOST that is compiled +# with valgrind support +if(${BOOST_VERSION}) + find_package(Boost ${BOOST_VERSION} EXACT) +else() + find_package(Boost 1.69 REQUIRED) +endif() +message("++ Using Boost version: " ${Boost_VERSION}) + +include_directories(${Boost_INCLUDE_DIRS}) +link_directories(${Boost_LIBRARY_DIRS}) + +# Boost related definitions +add_definitions(-DBOOST_COROUTINES_NO_DEPRECATION_WARNING) +add_definitions(-DBOOST_ERROR_CODE_HEADER_ONLY) +add_definitions(-DBOOST_SYSTEM_NO_DEPRECATED) +add_definitions(-DBOOST_ALL_NO_LIB) +add_definitions(-DBOOST_NO_RTTI) +add_definitions(-DBOOST_NO_TYPEID) +add_definitions(-DBOOST_ASIO_DISABLE_THREADS) + +# Define source files +set(SRC_FILES src/main.cpp) + +# Executables +add_executable(virtual-media ${SRC_FILES} ${HEADER_FILES}) +if(NOT ${YOCTO_DEPENDENCIES}) + add_dependencies(virtual-media nlohmann-json sdbusplus-project) +endif() + +# Default linkage +target_link_libraries(virtual-media systemd) +target_link_libraries(virtual-media -lsdbusplus) +target_link_libraries(virtual-media -ludev) +target_link_libraries(virtual-media -lboost_coroutine) +target_link_libraries(virtual-media -lboost_context) +install(TARGETS virtual-media DESTINATION sbin) + +# Options based compile definitions +target_compile_definitions(virtual-media + PRIVATE + $<$<BOOL:${VM_USE_VALGRIND}>: + -DBOOST_USE_VALGRIND> + $<$<BOOL:${CUSTOM_DBUS_PATH}>: + -DCUSTOM_DBUS_PATH="${CUSTOM_DBUS_PATH}">) + +if(CMAKE_INSTALL_SYSCONFDIR) + install(FILES ${PROJECT_SOURCE_DIR}/virtual-media.json DESTINATION + ${CMAKE_INSTALL_SYSCONFDIR}) +endif() +install(FILES ${PROJECT_SOURCE_DIR}/xyz.openbmc_project.VirtualMedia.service + DESTINATION /lib/systemd/system/) diff --git a/virtual-media/LICENSE b/virtual-media/LICENSE new file mode 100644 index 0000000..8dada3e --- /dev/null +++ b/virtual-media/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + 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. diff --git a/virtual-media/cmake/Findudev.cmake b/virtual-media/cmake/Findudev.cmake new file mode 100644 index 0000000..ce454d5 --- /dev/null +++ b/virtual-media/cmake/Findudev.cmake @@ -0,0 +1,77 @@ +# - try to find the udev library +# +# Cache Variables: (probably not for direct use in your scripts) +# UDEV_INCLUDE_DIR +# UDEV_SOURCE_DIR +# UDEV_LIBRARY +# +# Non-cache variables you might use in your CMakeLists.txt: +# UDEV_FOUND +# UDEV_INCLUDE_DIRS +# UDEV_LIBRARIES +# +# Requires these CMake modules: +# FindPackageHandleStandardArgs (known included with CMake >=2.6.2) +# +# Original Author: +# 2014 Kevin M. Godby <kevin@godby.org> +# +# Distributed under the Boost Software License, Version 1.0. +# (See accompanying file LICENSE_1_0.txt or copy at +# http://www.boost.org/LICENSE_1_0.txt) + +set(UDEV_ROOT_DIR + "${UDEV_ROOT_DIR}" + CACHE + PATH + "Directory to search for udev") + +find_package(PkgConfig QUIET) +if(PKG_CONFIG_FOUND) + pkg_check_modules(PC_LIBUDEV libudev) +endif() + +find_library(UDEV_LIBRARY + NAMES + udev + PATHS + ${PC_LIBUDEV_LIBRARY_DIRS} + ${PC_LIBUDEV_LIBDIR} + HINTS + "${UDEV_ROOT_DIR}" + PATH_SUFFIXES + lib + ) + +get_filename_component(_libdir "${UDEV_LIBRARY}" PATH) + +find_path(UDEV_INCLUDE_DIR + NAMES + libudev.h + PATHS + ${PC_LIBUDEV_INCLUDE_DIRS} + ${PC_LIBUDEV_INCLUDEDIR} + HINTS + "${_libdir}" + "${_libdir}/.." + "${UDEV_ROOT_DIR}" + PATH_SUFFIXES + include + ) + +include(FindPackageHandleStandardArgs) +find_package_handle_standard_args(UDEV + DEFAULT_MSG + UDEV_LIBRARY + UDEV_INCLUDE_DIR + ) + +if(UDEV_FOUND) + list(APPEND UDEV_LIBRARIES ${UDEV_LIBRARY}) + list(APPEND UDEV_INCLUDE_DIRS ${UDEV_INCLUDE_DIR}) + mark_as_advanced(UDEV_ROOT_DIR) +endif() + +mark_as_advanced(UDEV_INCLUDE_DIR + UDEV_LIBRARY) + diff --git a/virtual-media/src/main.cpp b/virtual-media/src/main.cpp new file mode 100644 index 0000000..3dfd0ad --- /dev/null +++ b/virtual-media/src/main.cpp @@ -0,0 +1,722 @@ +#include <boost/asio.hpp> +#include <boost/asio/buffer.hpp> +#include <boost/asio/posix/stream_descriptor.hpp> +#include <boost/asio/spawn.hpp> +#include <boost/container/flat_map.hpp> +#include <boost/container/flat_set.hpp> +#include <boost/process.hpp> +#include <filesystem> +#include <iostream> +#include <memory> +#include <nlohmann/json.hpp> +#include <sdbusplus/asio/connection.hpp> +#include <sdbusplus/asio/object_server.hpp> + +namespace fs = std::filesystem; + +namespace udev +{ +#include <libudev.h> +struct udev; +struct udev_monitor; +struct udev_device; + +struct udevDeleter +{ + void operator()(udev* desc) const + { + udev_unref(desc); + } +}; + +struct monitorDeleter +{ + void operator()(udev_monitor* desc) const + { + udev_monitor_unref(desc); + }; +}; + +struct deviceDeleter +{ + void operator()(udev_device* desc) const + { + udev_device_unref(desc); + }; +}; +} // namespace udev + +enum class StateChange +{ + notmonitored, + unknown, + removed, + inserted +}; + +class DeviceMonitor +{ + public: + DeviceMonitor(boost::asio::io_context& ioc) : ioc(ioc), monitorSd(ioc) + { + udev = std::unique_ptr<udev::udev, udev::udevDeleter>(udev::udev_new()); + if (!udev) + { + throw std::system_error(EFAULT, std::generic_category(), + "Unable to create uDev handler."); + } + + monitor = std::unique_ptr<udev::udev_monitor, udev::monitorDeleter>( + udev::udev_monitor_new_from_netlink(udev.get(), "kernel")); + + if (!monitor) + { + throw std::system_error(EFAULT, std::generic_category(), + "Unable to create uDev Monitor handler."); + } + int rc = udev_monitor_filter_add_match_subsystem_devtype( + monitor.get(), "block", "disk"); + + if (rc) + { + throw std::system_error(EFAULT, std::generic_category(), + "Could not apply filters."); + } + rc = udev_monitor_enable_receiving(monitor.get()); + if (rc) + { + throw std::system_error(EFAULT, std::generic_category(), + "Enable receiving failed."); + } + monitorSd.assign(udev_monitor_get_fd(monitor.get())); + } + + DeviceMonitor(const DeviceMonitor&) = delete; + DeviceMonitor(DeviceMonitor&&) = delete; + + DeviceMonitor& operator=(const DeviceMonitor&) = delete; + DeviceMonitor& operator=(DeviceMonitor&&) = delete; + + template <typename DeviceChangeStateCb> + void run(DeviceChangeStateCb callback) + { + boost::asio::spawn(ioc, [this, + callback](boost::asio::yield_context yield) { + boost::system::error_code ec; + while (1) + { + monitorSd.async_wait( + boost::asio::posix::stream_descriptor::wait_read, + yield[ec]); + + std::unique_ptr<udev::udev_device, udev::deviceDeleter> device = + std::unique_ptr<udev::udev_device, udev::deviceDeleter>( + udev::udev_monitor_receive_device(monitor.get())); + if (device) + { + const char* devAction = + udev_device_get_action(device.get()); + if (devAction == nullptr) + { + std::cerr << "Received NULL action.\n"; + continue; + } + if (strcmp(devAction, "change") == 0) + { + const char* sysname = + udev_device_get_sysname(device.get()); + if (sysname == nullptr) + { + std::cerr << "Received NULL sysname.\n"; + continue; + } + auto monitoredDevice = devices.find(sysname); + if (monitoredDevice != devices.cend()) + { + const char* size_str = + udev_device_get_sysattr_value(device.get(), + "size"); + if (size_str == nullptr) + { + std::cerr << "Received NULL size.\n"; + continue; + } + uint64_t size = 0; + try + { + size = std::stoul(size_str, 0, 0); + } + catch (const std::exception& e) + { + std::cerr + << "Could not convert size to integer.\n"; + continue; + } + if (size > 0 && monitoredDevice->second != + StateChange::inserted) + { + std::cout << sysname << " inserted.\n"; + monitoredDevice->second = StateChange::inserted; + callback(sysname, StateChange::inserted); + } + else if (size == 0 && monitoredDevice->second != + StateChange::removed) + { + std::cout << sysname << " removed.\n"; + monitoredDevice->second = StateChange::removed; + callback(sysname, StateChange::removed); + } + }; + } + } + } + }); + } + + void addDevice(const std::string& device) + { + std::cout << "Watch on /dev/" << device << '\n'; + devices.insert( + std::pair<std::string, StateChange>(device, StateChange::unknown)); + } + + StateChange getState(const std::string& device) + { + auto monitoredDevice = devices.find(device); + if (monitoredDevice != devices.cend()) + { + return monitoredDevice->second; + } + return StateChange::notmonitored; + } + + private: + boost::asio::io_context& ioc; + boost::asio::posix::stream_descriptor monitorSd; + + std::unique_ptr<udev::udev, udev::udevDeleter> udev; + std::unique_ptr<udev::udev_monitor, udev::monitorDeleter> monitor; + + boost::container::flat_map<std::string, StateChange> devices; +}; + +class Process : public std::enable_shared_from_this<Process> +{ + public: + Process(boost::asio::io_context& ioc, const std::string& name) : + ioc(ioc), pipe(ioc), name(name) + { + } + + template <typename ExitCb> + bool spawn(const std::vector<std::string>& args, ExitCb&& onExit) + { + std::error_code ec; + child = boost::process::child( + "/usr/sbin/nbd-client", + //"/sbin/nbd-client", "-N", "otherexport", "127.0.0.1", "/dev/nbd0", + //"-n", + boost::process::args(args), + boost::process::on_exit( + [name(this->name), onExit{std::move(onExit)}]( + int exit, const std::error_code& ecIn) { + std::cout << "- child " << name << "(" << exit << " " + << ecIn << ")\n"; + auto process = processList.find(name); + if (process != processList.end() && process->second) + { + std::cout << "Removing process from processList\n"; + process->second.reset(); + } + onExit(name); + }), + (boost::process::std_out & boost::process::std_err) > pipe, ec, + ioc); + + if (ec) + { + std::cerr << "Error while creating child process: " << ec << '\n'; + return false; + } + + boost::asio::spawn(ioc, [this, self = shared_from_this()]( + boost::asio::yield_context yield) { + boost::system::error_code bec; + std::string line; + boost::asio::dynamic_string_buffer buffer{line}; + std::cout << "Entering COUT Loop\n"; + while (1) + { + auto x = boost::asio::async_read_until(pipe, std::move(buffer), + '\n', yield[bec]); + std::cout << "-[" << name << "]-> " << line; + buffer.consume(x); + if (bec) + { + std::cerr << "Loop Error: " << bec << '\n'; + break; + } + } + std::cout << "Exiting from COUT Loop\n"; + }); + return true; + } + + void terminate(int signal = SIGTERM) + { + int rc = kill(child.id(), SIGTERM); + if (rc) + { + return; + } + child.wait(); + } + + static void addProcess(const std::string& name) + { + processList[name] = nullptr; + } + + template <typename ExitCb> + static bool spawn(boost::asio::io_context& ioc, const std::string& name, + const std::vector<std::string>& args, ExitCb&& onExit) + { + auto process = processList.find(name); + if (process != processList.end() && !process->second) + { + std::cout << "Spawning process for " << name << '\n'; + + auto newProcess = std::make_shared<Process>(ioc, name); + if (newProcess->spawn(args, onExit)) + { + process->second = newProcess; + return true; + } + std::cout << "Process (" << name << ") exited immediately.\n"; + return false; + } + std::cout << "No process (" << name << ") to be spawned.\n"; + return false; + } + + static bool terminate(const std::string& name, int signal = SIGTERM) + { + auto process = processList.find(name); + if (process != processList.end() && process->second) + { + std::cout << "Terminating " << name << '\n'; + process->second->terminate(signal); + return true; + } + std::cout << "No process (" << name << ") for termination.\n"; + return false; + } + + static bool isActive(const std::string& name) + { + auto process = processList.find(name); + if (process != processList.end()) + { + if (process->second) + { + return true; + } + } + return false; + } + + private: + boost::asio::io_context& ioc; + boost::process::child child; + boost::process::async_pipe pipe; + std::string name; + inline static boost::container::flat_map<std::string, + std::shared_ptr<Process>> + processList; +}; + +class Configuration +{ + public: + struct MountPoint + { + std::string nbdDevice; + std::string unixSocket; + std::string endPointId; + std::optional<int> timeout; + std::optional<int> blocksize; + + static std::vector<std::string> toArgs(const MountPoint& mp) + { + std::vector<std::string> args = { + "-N", "otherexport", "-u", + mp.unixSocket, "/dev/" + mp.nbdDevice, "-n"}; + return args; + } + }; + + bool valid = false; + boost::container::flat_map<std::string, MountPoint> mountPoints; + + Configuration(const std::string& file) + { + valid = loadConfiguration(file); + } + + private: + bool loadConfiguration(const std::string& file) noexcept + { + std::ifstream configFile(file); + if (!configFile.is_open()) + { + std::cout << "Could not open configuration file\n"; + return false; + } + try + { + auto data = nlohmann::json::parse(configFile, nullptr); + setupVariables(data); + } + catch (nlohmann::json::exception& e) + { + std::cerr << "Error parsing JSON file\n"; + return false; + } + catch (std::out_of_range& e) + { + std::cerr << "Error parsing JSON file\n"; + return false; + } + return true; + } + + bool setupVariables(const nlohmann::json& config) + { + for (const auto& item : config.items()) + { + if (item.key() == "MountPoints") + { + for (const auto& mountpoint : item.value().items()) + { + MountPoint mp; + const auto nbdDeviceIter = + mountpoint.value().find("NBDDevice"); + if (nbdDeviceIter != mountpoint.value().cend()) + { + const std::string* value = + nbdDeviceIter->get_ptr<const std::string*>(); + if (value) + { + mp.nbdDevice = *value; + } + else + { + std::cerr << "NBDDevice required, not set\n"; + continue; + } + }; + const auto unixSocketIter = + mountpoint.value().find("UnixSocket"); + if (unixSocketIter != mountpoint.value().cend()) + { + const std::string* value = + unixSocketIter->get_ptr<const std::string*>(); + if (value) + { + mp.unixSocket = *value; + } + else + { + std::cerr << "UnixSocket required, not set\n"; + continue; + } + } + const auto endPointIdIter = + mountpoint.value().find("EndpointId"); + if (endPointIdIter != mountpoint.value().cend()) + { + const std::string* value = + endPointIdIter->get_ptr<const std::string*>(); + if (value) + { + mp.endPointId = *value; + } + else + { + std::cerr << "EndpointId required, not set\n"; + continue; + } + } + const auto timeoutIter = mountpoint.value().find("Timeout"); + if (timeoutIter != mountpoint.value().cend()) + { + const uint64_t* value = + timeoutIter->get_ptr<const uint64_t*>(); + if (value) + { + mp.timeout = *value; + } + else + { + std::cout << "Timeout not set, use default\n"; + } + } + const auto blocksizeIter = + mountpoint.value().find("BlockSize"); + if (blocksizeIter != mountpoint.value().cend()) + { + const uint64_t* value = + blocksizeIter->get_ptr<const uint64_t*>(); + if (value) + { + mp.blocksize = *value; + } + else + { + std::cout << "BlockSize not set, use default\n"; + } + } + mountPoints[mountpoint.key()] = std::move(mp); + } + } + } + return true; + } +}; + +class App +{ + public: + App(boost::asio::io_context& ioc, const Configuration& config, + sd_bus* custom_bus = nullptr) : + ioc(ioc), + devMonitor(ioc), config(config) + { + if (!custom_bus) + { + bus = std::make_shared<sdbusplus::asio::connection>(ioc); + } + else + { + bus = + std::make_shared<sdbusplus::asio::connection>(ioc, custom_bus); + } + objServer = std::make_shared<sdbusplus::asio::object_server>(bus); + bus->request_name("xyz.openbmc_project.VirtualMedia"); + objManager = std::make_shared<sdbusplus::server::manager::manager>( + *bus, "/xyz/openbmc_project/VirtualMedia"); + for (const auto& entry : config.mountPoints) + { + devMonitor.addDevice(entry.second.nbdDevice); + Process::addProcess(entry.first); + + addMountPointInterface(entry.first, entry.second); + addProxyInterface(entry.first, entry.second); + addProcessInterface(entry.first); + } + devMonitor.run([this](const std::string& device, StateChange change) { + configureUsbGadget(device, change); + }); + }; + + private: + bool echoToFile(const fs::path& fname, const std::string& content) + { + std::ofstream fileWriter; + fileWriter.exceptions(std::ofstream::failbit | std::ofstream::badbit); + fileWriter.open(fname, std::ios::out | std::ios::app); + fileWriter << content << std::endl; // Make sure for new line and flush + fileWriter.close(); + return true; + } + + bool configureUsbGadget(const std::string& device, StateChange change) + { + bool success = true; + std::error_code ec; + if (change == StateChange::unknown) + { + std::cerr << "Change to unknown state is not possible\n"; + return false; + } + StateChange currentState = devMonitor.getState(device); + if (currentState == StateChange::notmonitored) + { + std::cerr << "Unregistered device\n"; + return false; + } + + const fs::path gadgetDir = + "/sys/kernel/config/usb_gadget/mass-storage-" + device; + const fs::path funcMassStorageDir = + gadgetDir / "functions/mass_storage.usb0"; + const fs::path stringsDir = gadgetDir / "strings/0x409"; + const fs::path configDir = gadgetDir / "configs/c.1"; + const fs::path massStorageDir = configDir / "mass_storage.usb0"; + const fs::path configStringsDir = configDir / "strings/0x409"; + + if (change == StateChange::inserted) + { + try + { + fs::create_directories(gadgetDir); + echoToFile(gadgetDir / "idVendor", "0x1d6b"); + echoToFile(gadgetDir / "idProduct", "0x0104"); + fs::create_directories(stringsDir); + echoToFile(stringsDir / "manufacturer", "OpenBMC"); + echoToFile(stringsDir / "product", "Virtual Media Device"); + fs::create_directories(configStringsDir); + echoToFile(configStringsDir / "configuration", "config 1"); + fs::create_directories(funcMassStorageDir); + fs::create_directories(funcMassStorageDir / "lun.0"); + fs::create_directory_symlink(funcMassStorageDir, + massStorageDir); + echoToFile(funcMassStorageDir / "lun.0/removable", "1"); + echoToFile(funcMassStorageDir / "lun.0/ro", "1"); + echoToFile(funcMassStorageDir / "lun.0/cdrom", "0"); + echoToFile(funcMassStorageDir / "lun.0/file", "/dev/" + device); + + for (const auto& port : fs::directory_iterator( + "/sys/bus/platform/devices/1e6a0000.usb-vhub")) + { + if (fs::is_directory(port) && !fs::is_symlink(port) && + !fs::exists(port.path() / "gadget/suspended")) + { + const std::string portId = port.path().filename(); + std::cout << "Use port : " << port.path().filename() + << '\n'; + echoToFile(gadgetDir / "UDC", portId); + return true; + } + } + } + catch (fs::filesystem_error& e) + { + // Got error perform cleanup + std::cerr << e.what() << '\n'; + success = false; + } + catch (std::ofstream::failure& e) + { + // Got error perform cleanup + std::cerr << e.what() << '\n'; + success = false; + } + } + // StateChange: unknown, notmonitored, inserted were handler + // earlier. We'll get here only for removed, or cleanup + + fs::remove_all(massStorageDir, ec); + if (ec) + { + std::cerr << ec.message() << '\n'; + } + fs::remove_all(funcMassStorageDir, ec); + if (ec) + { + std::cerr << ec.message() << '\n'; + } + fs::remove_all(configStringsDir, ec); + if (ec) + { + std::cerr << ec.message() << '\n'; + } + fs::remove_all(configDir, ec); + if (ec) + { + std::cerr << ec.message() << '\n'; + } + fs::remove_all(stringsDir, ec); + if (ec) + { + std::cerr << ec.message() << '\n'; + } + fs::remove_all(gadgetDir, ec); + if (ec) + { + std::cerr << ec.message() << '\n'; + } + return (success && !static_cast<bool>(ec)); + } + + void addMountPointInterface(const std::string& name, + const Configuration::MountPoint& mp) + { + auto iface = objServer->add_interface( + "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, + "xyz.openbmc_project.VirtualMedia.MountPoint"); + iface->register_property("Device", mp.nbdDevice); + iface->register_property("EndpointId", mp.endPointId); + iface->register_property("Socket", mp.unixSocket); + iface->register_property("Timeout", *mp.timeout); + iface->register_property("BlockSize", *mp.blocksize); + iface->initialize(); + }; + + void addProxyInterface(const std::string& name, + const Configuration::MountPoint& mp) + { + auto iface = objServer->add_interface( + "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, + "xyz.openbmc_project.VirtualMedia.Proxy"); + iface->register_method( + "Mount", [&, name, args(Configuration::MountPoint::toArgs(mp))]() { + return Process::spawn( + ioc, name, args, [this](const std::string& name) { + configureUsbGadget(name, StateChange::removed); + }); + }); + iface->register_method("Unmount", [this, name]() { + const auto mp = config.mountPoints.find(name); + std::string nbdDevice = mp->second.nbdDevice; + configureUsbGadget(nbdDevice, StateChange::removed); + return Process::terminate(name); + }); + iface->initialize(); + } + + void addProcessInterface(const std::string& name) + { + auto iface = objServer->add_interface( + "/xyz/openbmc_project/VirtualMedia/Proxy/" + name, + "xyz.openbmc_project.VirtualMedia.Process"); + iface->register_property( + "Active", bool(false), + [](const bool& req, bool& property) { return 0; }, + [name](const bool& property) { return Process::isActive(name); }); + iface->initialize(); + } + + boost::asio::io_context& ioc; + DeviceMonitor devMonitor; + std::shared_ptr<sdbusplus::asio::connection> bus; + std::shared_ptr<sdbusplus::asio::object_server> objServer; + std::shared_ptr<sdbusplus::server::manager::manager> objManager; + const Configuration& config; +}; + +int main() +{ + Configuration config("/etc/virtual-media.json"); + if (!config.valid) + return -1; + + boost::asio::io_context ioc; + boost::asio::signal_set signals(ioc, SIGINT, SIGTERM); + signals.async_wait( + [&ioc](const boost::system::error_code&, const int&) { ioc.stop(); }); + + sd_bus* b = nullptr; +#if defined(CUSTOM_DBUS_PATH) +#pragma message("You are using custom DBUS path set to " CUSTOM_DBUS_PATH) + sd_bus_new(&b); + sd_bus_set_bus_client(b, true); + sd_bus_set_address(b, CUSTOM_DBUS_PATH); + sd_bus_start(b); +#endif + App app(ioc, config, b); + + ioc.run(); + + return 0; +} diff --git a/virtual-media/virtual-media.json b/virtual-media/virtual-media.json new file mode 100644 index 0000000..3c61583 --- /dev/null +++ b/virtual-media/virtual-media.json @@ -0,0 +1,22 @@ +{ + "InactivityTimeout": 1800, + "MountPoints": { + "ISO0": { + "EndpointId": "/nbd/0", + "Mode": 0, + "NBDDevice": "nbd0", + "UnixSocket": "/tmp/nbd0.sock", + "Timeout": 30, + "BlockSize": 512 + }, + "USB0": { + "EndpointId": "/nbd/1", + "Mode": 0, + "NBDDevice": "nbd1", + "UnixSocket": "/tmp/nbd1.sock", + "Timeout": 30, + "BlockSize": 512 + } + } +} + diff --git a/virtual-media/xyz.openbmc_project.VirtualMedia.service b/virtual-media/xyz.openbmc_project.VirtualMedia.service new file mode 100644 index 0000000..b5c21ae --- /dev/null +++ b/virtual-media/xyz.openbmc_project.VirtualMedia.service @@ -0,0 +1,11 @@ +[Unit] +Description=Virtual Media Service +After=dbus.service + +[Service] +ExecStart=/usr/sbin/virtual-media +Restart=always +Type=simple + +[Install] +WantedBy=multi-user.target |