Hieu Huynh | 1463f70 | 2021-09-23 03:14:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-2021 Ampere Computing LLC |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <ipmid/api.hpp> |
| 18 | #include <ipmid/types.hpp> |
| 19 | #include <ipmid/utils.hpp> |
| 20 | #include <phosphor-logging/log.hpp> |
| 21 | #include "oemcommands.hpp" |
| 22 | #include <cstdlib> |
| 23 | |
| 24 | using namespace phosphor::logging; |
| 25 | |
| 26 | static inline auto response(uint8_t cc) |
| 27 | { |
| 28 | return std::make_tuple(cc, std::nullopt); |
| 29 | } |
| 30 | |
| 31 | static inline auto responseFailure() |
| 32 | { |
| 33 | return response(responseFail); |
| 34 | } |
| 35 | |
| 36 | /** @brief execute a command and get the output of the command |
| 37 | * @param[in] the command |
| 38 | * @returns output of the command |
| 39 | */ |
| 40 | std::string exec(const char* cmd) { |
| 41 | char buffer[128]; |
| 42 | std::string result = ""; |
| 43 | /* Pipe stream from a command */ |
| 44 | FILE* pipe = popen(cmd, "r"); |
| 45 | if (!pipe) throw std::runtime_error("popen() failed!"); |
| 46 | try { |
| 47 | /* Reads a line from the specified stream and stores it */ |
| 48 | while (fgets(buffer, sizeof buffer, pipe) != NULL) { |
| 49 | result += buffer; |
| 50 | } |
| 51 | } catch (...) { |
| 52 | pclose(pipe); |
| 53 | throw; |
| 54 | } |
| 55 | /* Close a stream that was opened by popen() */ |
| 56 | pclose(pipe); |
| 57 | return result; |
| 58 | } |
| 59 | |
| 60 | /** @brief implements sync RTC time to BMC commands |
| 61 | * @param - None |
| 62 | * @returns IPMI completion code. |
| 63 | */ |
| 64 | ipmi::RspType<> ipmiSyncRTCTimeToBMC() |
| 65 | { |
| 66 | std::string cmd; |
| 67 | std::string cmdOutput; |
Hieu Huynh | 90a4fb8 | 2022-08-02 07:21:03 +0000 | [diff] [blame^] | 68 | int ret; |
Hieu Huynh | 1463f70 | 2021-09-23 03:14:59 +0000 | [diff] [blame] | 69 | try |
| 70 | { |
| 71 | /* Check the mode of NTP in the system, set the system time in case the |
| 72 | * NTP mode is disabled. |
| 73 | */ |
| 74 | cmd = "systemctl status systemd-timesyncd.service | grep inactive"; |
| 75 | cmdOutput = exec(cmd.c_str()); |
| 76 | if (cmdOutput.empty()) |
| 77 | { |
| 78 | log<level::INFO>("Can not set system time while the mode is NTP"); |
| 79 | return responseFailure(); |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | /* Sync time from RTC to BMC using hwclock */ |
Hieu Huynh | 90a4fb8 | 2022-08-02 07:21:03 +0000 | [diff] [blame^] | 84 | ret = system("hwclock --hctosys"); |
| 85 | if (ret == -1) |
| 86 | { |
| 87 | log<level::INFO>("Can not set the system time"); |
| 88 | return responseFailure(); |
| 89 | } |
Hieu Huynh | 1463f70 | 2021-09-23 03:14:59 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | catch(const std::exception& e) |
| 93 | { |
| 94 | log<level::ERR>(e.what()); |
| 95 | return responseFailure(); |
| 96 | } |
| 97 | |
| 98 | return ipmi::responseSuccess(); |
| 99 | } |
| 100 | |
| 101 | void registerOEMFunctions() __attribute__((constructor)); |
| 102 | void registerOEMFunctions() |
| 103 | { |
| 104 | ipmi::registerHandler(ipmi::prioOemBase, ipmi::ampere::netFnAmpere, |
| 105 | ipmi::general::cmdSyncRtcTime, |
| 106 | ipmi::Privilege::User, ipmiSyncRTCTimeToBMC); |
| 107 | } |