blob: 3851f947a4c5f341cd2fce9fcc607afd56703348 [file] [log] [blame]
Patrick Venture3ecb3502019-05-17 11:03:51 -07001/*
2 * Copyright 2019 Google Inc.
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
Patrick Venture26a17262019-05-20 11:03:35 -070017#include "verify_systemd.hpp"
Patrick Venture3ecb3502019-05-17 11:03:51 -070018
19#include "status.hpp"
20
21#include <fstream>
22#include <memory>
23#include <sdbusplus/bus.hpp>
24#include <string>
25#include <vector>
26
Patrick Venture1d5a31c2019-05-20 11:38:22 -070027namespace ipmi_flash
Patrick Venture3ecb3502019-05-17 11:03:51 -070028{
29
Patrick Venture1d66fe62019-06-03 14:57:27 -070030std::unique_ptr<TriggerableActionInterface>
Patrick Venture29af1e32019-08-05 13:42:28 -070031 SystemdWithStatusFile::CreateVerification(sdbusplus::bus::bus&& bus,
32 const std::string& path,
33 const std::string& service,
34 const std::string& mode)
Patrick Venture3ecb3502019-05-17 11:03:51 -070035{
Patrick Venture29af1e32019-08-05 13:42:28 -070036 return std::make_unique<SystemdWithStatusFile>(std::move(bus), path,
37 service, mode);
Patrick Venture3ecb3502019-05-17 11:03:51 -070038}
39
Patrick Venture29af1e32019-08-05 13:42:28 -070040bool SystemdWithStatusFile::trigger()
Patrick Venture3ecb3502019-05-17 11:03:51 -070041{
42 static constexpr auto systemdService = "org.freedesktop.systemd1";
43 static constexpr auto systemdRoot = "/org/freedesktop/systemd1";
44 static constexpr auto systemdInterface = "org.freedesktop.systemd1.Manager";
45
46 auto method = bus.new_method_call(systemdService, systemdRoot,
47 systemdInterface, "StartUnit");
48 method.append(triggerService);
Patrick Ventureee614ec2019-08-05 12:08:44 -070049 method.append(mode);
Patrick Venture3ecb3502019-05-17 11:03:51 -070050
51 try
52 {
53 bus.call_noreply(method);
54 }
55 catch (const sdbusplus::exception::SdBusError& ex)
56 {
57 /* TODO: Once logging supports unit-tests, add a log message to test
58 * this failure.
59 */
60 return false;
61 }
62
63 return true;
64}
65
Patrick Venture29af1e32019-08-05 13:42:28 -070066void SystemdWithStatusFile::abort()
Patrick Venture3ecb3502019-05-17 11:03:51 -070067{
68 /* TODO: Implement this. */
69}
70
Patrick Venture29af1e32019-08-05 13:42:28 -070071ActionStatus SystemdWithStatusFile::status()
Patrick Venture3ecb3502019-05-17 11:03:51 -070072{
Patrick Ventureda66fd82019-06-03 11:11:24 -070073 ActionStatus result = ActionStatus::unknown;
Patrick Venture3ecb3502019-05-17 11:03:51 -070074
75 std::ifstream ifs;
76 ifs.open(checkPath);
77 if (ifs.good())
78 {
79 /*
80 * Check for the contents of the file, accepting:
81 * running, success, or failed.
82 */
83 std::string status;
84 ifs >> status;
85 if (status == "running")
86 {
Patrick Ventureda66fd82019-06-03 11:11:24 -070087 result = ActionStatus::running;
Patrick Venture3ecb3502019-05-17 11:03:51 -070088 }
89 else if (status == "success")
90 {
Patrick Ventureda66fd82019-06-03 11:11:24 -070091 result = ActionStatus::success;
Patrick Venture3ecb3502019-05-17 11:03:51 -070092 }
93 else if (status == "failed")
94 {
Patrick Ventureda66fd82019-06-03 11:11:24 -070095 result = ActionStatus::failed;
Patrick Venture3ecb3502019-05-17 11:03:51 -070096 }
97 }
98
99 return result;
100}
101
Patrick Venture29af1e32019-08-05 13:42:28 -0700102const std::string SystemdWithStatusFile::getMode() const
Patrick Ventureee614ec2019-08-05 12:08:44 -0700103{
104 return mode;
105}
106
Patrick Venture1d5a31c2019-05-20 11:38:22 -0700107} // namespace ipmi_flash