blob: d5150d33156eb14925a7c5fe3fd04109a10bc8f6 [file] [log] [blame]
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05001#include <unistd.h>
Jayanth Othayothbcb174b2017-07-02 06:29:24 -05002#include <sys/inotify.h>
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05003
4#include <phosphor-logging/elog-errors.hpp>
5
6#include "dump_manager.hpp"
7#include "dump_internal.hpp"
8#include "xyz/openbmc_project/Common/error.hpp"
9#include "config.h"
10
11namespace phosphor
12{
13namespace dump
14{
15
16using namespace sdbusplus::xyz::openbmc_project::Common::Error;
17using namespace phosphor::logging;
18
19namespace internal
20{
21
22void Manager::create(
23 Type type,
24 std::vector<std::string> fullPaths)
25{
26 // TODO openbmc/openbmc#1795
27 // Add implementaion of internal create function.
28}
29
30} //namepsace internal
31
32uint32_t Manager::createDump()
33{
34 std::vector<std::string> paths;
35
36 return captureDump(Type::UserRequested, paths);
37}
38
39uint32_t Manager::captureDump(
40 Type type,
41 const std::vector<std::string>& fullPaths)
42{
43 pid_t pid = fork();
44
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050045 if (pid == 0)
46 {
Jayanth Othayothf9009a22017-07-12 19:40:34 -050047 fs::path dumpPath(BMC_DUMP_PATH);
48
49 dumpPath /= std::to_string(lastEntryId + 1);
50 execl("/usr/bin/ffdc", "ffdc", "-d", dumpPath.c_str(), nullptr);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050051
52 //ffdc script execution is failed.
53 auto error = errno;
54 log<level::ERR>("Error occurred during ffdc function execution",
55 entry("ERRNO=%d", error));
56 elog<InternalFailure>();
57 }
58 else if (pid > 0)
59 {
60 auto rc = sd_event_add_child(eventLoop.get(),
61 nullptr,
62 pid,
63 WEXITED | WSTOPPED,
64 callback,
65 nullptr);
66 if (0 > rc)
67 {
68 // Failed to add to event loop
69 log<level::ERR>("Error occurred during the sd_event_add_child call",
70 entry("rc=%d", rc));
71 elog<InternalFailure>();
72 }
73 }
74 else
75 {
76 auto error = errno;
77 log<level::ERR>("Error occurred during fork",
78 entry("ERRNO=%d", error));
79 elog<InternalFailure>();
80 }
81
82 return ++lastEntryId;
83}
84
85void Manager::createEntry(const fs::path& file)
86{
87 // TODO openbmc/openbmc#1795
88 // Get Dump ID and Epoch time from Dump file name.
89 // Validate the Dump file name.
90 auto id = lastEntryId;
91
92 //Get Epoch time.
93 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
94 std::chrono::system_clock::now().time_since_epoch()).count();
95
96 // Entry Object path.
97 auto objPath = fs::path(OBJ_ENTRY) / std::to_string(id);
98
99 auto size = fs::file_size(file);
100
101 entries.insert(std::make_pair(id,
102 std::make_unique<Entry>(
103 bus,
104 objPath.c_str(),
105 id,
106 ms,
107 size,
108 file,
109 *this)));
110}
111
112void Manager::erase(uint32_t entryId)
113{
114 entries.erase(entryId);
115}
116
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500117void Manager::watchCallback(const UserMap& fileInfo)
118{
119 for (const auto& i : fileInfo)
120 {
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500121 // For any new dump file create dump entry object
122 // and associated inotify watch.
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500123 if (IN_CLOSE_WRITE == i.second)
124 {
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500125 removeWatch(i.first);
126
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500127 createEntry(i.first);
128 }
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500129 // Start inotify watch on newly created directory.
130 else if ((IN_CREATE == i.second) && fs::is_directory(i.first))
131 {
132 auto watchObj = std::make_unique<Watch>(
133 eventLoop,
134 IN_NONBLOCK,
135 IN_CLOSE_WRITE,
136 EPOLLIN,
137 i.first,
138 std::bind(
139 std::mem_fn(
140 &phosphor::dump::Manager::watchCallback),
141 this, std::placeholders::_1));
142
143 childWatchMap.emplace(i.first, std::move(watchObj));
144 }
145
Jayanth Othayothbcb174b2017-07-02 06:29:24 -0500146 }
147}
148
Jayanth Othayoth764d1b22017-07-12 19:14:02 -0500149void Manager::removeWatch(const fs::path& path)
150{
151 //Delete Watch entry from map.
152 childWatchMap.erase(path);
153}
154
Jayanth Othayotha320c7c2017-06-14 07:17:21 -0500155} //namespace dump
156} //namespace phosphor