forked from open-power/guard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathguard.cpp
More file actions
143 lines (130 loc) · 3.92 KB
/
guard.cpp
File metadata and controls
143 lines (130 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// SPDX-License-Identifier: Apache-2.0
#include "libguard/guard_interface.hpp"
#include "libguard/include/guard_record.hpp"
#include <CLI/CLI.hpp>
using namespace std;
using namespace openpower::guard;
void guardList()
{
auto records = getAll();
if (!records.size())
{
std::cout << "No Records to display" << std::endl;
return;
}
std::cout << "ID | ERROR | Type | Path " << std::endl;
for (const auto& elem : records)
{
if (elem.errType != GARD_Reconfig)
{
std::cout << std::hex << std::setw(8) << std::setfill('0')
<< elem.recordId;
std::cout << " | ";
std::cout << std::hex << std::setw(8) << std::setfill('0')
<< elem.elogId;
std::cout << " | ";
std::optional<std::string> gReasonToStr =
guardReasonToStr(elem.errType);
std::cout << *gReasonToStr;
std::cout << " | ";
std::optional<std::string> physicalPath =
getPhysicalPath(elem.targetId);
if (!physicalPath)
{
std::cout << "Unknown ";
}
else
{
std::cout << *physicalPath;
}
std::cout << std::endl;
}
}
}
void guardDelete(const std::string& physicalPath)
{
std::optional<EntityPath> entityPath = getEntityPath(physicalPath);
if (!entityPath)
{
std::cerr << "Unsupported physical path " << physicalPath << std::endl;
return;
}
clear(*entityPath);
}
void guardClear()
{
clearAll();
}
void guardCreate(const std::string& physicalPath)
{
std::optional<EntityPath> entityPath = getEntityPath(physicalPath);
if (!entityPath)
{
std::cerr << "Unsupported physical path " << physicalPath << std::endl;
return;
}
create(*entityPath);
std::cout << "Success" << std::endl;
}
static void exitWithError(const std::string& help, const char* err)
{
std::cerr << "ERROR: " << err << std::endl << help << std::endl;
exit(-1);
}
int main(int argc, char** argv)
{
try
{
CLI::App app{"GUARD Tool"};
std::optional<std::string> createGuardStr;
std::optional<std::string> deleteGuardStr;
bool listGuardRecords = false;
bool clearAll = false;
bool gversion = false;
app.set_help_flag("-h, --help",
"Use the below listed functions.\n"
"Warning: Don't try guard on non guardable units "
"(sys, perv)");
app.add_option("-c, --create", createGuardStr,
"Create GUARD record, expects physical path as input");
app.add_option("-d, --delete", deleteGuardStr,
"Delete GUARD record, expects physical path as input");
app.add_flag("-l, --list", listGuardRecords,
"Listing of GUARDed resources.");
app.add_flag("-r, --clearall", clearAll,
"Clears GUARD states for all resources");
app.add_flag("-v, --version", gversion, "Version of GUARD tool");
CLI11_PARSE(app, argc, argv);
libguard_init();
if (createGuardStr)
{
guardCreate(*createGuardStr);
}
else if (deleteGuardStr)
{
guardDelete(*deleteGuardStr);
}
else if (clearAll)
{
guardClear();
}
else if (listGuardRecords)
{
guardList();
}
else if (gversion)
{
std::cout << "GUARD Tool version is " << GUARD_VERSION << std::endl;
}
else
{
exitWithError(app.help("", CLI::AppFormatMode::All),
"Invalid option");
}
}
catch (const std::exception& ex)
{
std::cout << "Exception " << ex.what() << std::endl;
}
return 0;
}