From: Mateusz Malicki Date: Wed, 7 Jan 2015 13:29:30 +0000 (+0100) Subject: Add get_zone_ids, get_active_zone_id and get_zones_status to cli X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=04f7e267d40ba248d20251dcb13223134996095f;p=platform%2Fcore%2Fsecurity%2Fvasum.git Add get_zone_ids, get_active_zone_id and get_zones_status to cli [Feature] Add get_zone_ids, get_active_zone_id and get_zones_status to cli [Cause] N/A [Solution] N/A [Verification] Build, run appropriate functions (through cli) Change-Id: If6823243d66606d28bf6b45c5b2ece8ab7c06e49 --- diff --git a/cli/command-line-interface.cpp b/cli/command-line-interface.cpp index 1222a86..06be364 100644 --- a/cli/command-line-interface.cpp +++ b/cli/command-line-interface.cpp @@ -31,7 +31,12 @@ #include #include #include +#include +#include +#include +#include #include +#include using namespace std; @@ -82,6 +87,14 @@ finish: } } +template +string stringAsInStream(const T& value) +{ + std::ostringstream stream; + stream << value; + return stream.str(); +} + ostream& operator<<(ostream& out, const VsmZoneState& state) { const char* name; @@ -113,6 +126,30 @@ ostream& operator<<(ostream& out, const VsmZone& zone) return out; } +typedef vector> Table; + +ostream& operator<<(ostream& out, const Table& table) +{ + vector sizes; + for (const auto& row : table) { + if (sizes.size() < row.size()) { + sizes.resize(row.size()); + } + for (size_t i = 0; i < row.size(); ++i) { + sizes[i] = max(sizes[i], row[i].length()); + } + } + + for (const auto& row : table) { + for (size_t i = 0; i < row.size(); ++i) { + out << left << setw(sizes[i]+2) << row[i]; + } + out << "\n"; + } + + return out; +} + } // namespace void CommandLineInterface::printUsage(std::ostream& out) const @@ -210,6 +247,58 @@ void unlock_zone(int pos, int argc, const char** argv) one_shot(bind(vsm_unlock_zone, _1, argv[pos + 1])); } +void get_zones_status(int /* pos */, int /* argc */, const char** /* argv */) +{ + using namespace std::placeholders; + + VsmArrayString ids; + VsmString activeId; + Table table; + + one_shot(bind(vsm_get_zone_ids, _1, &ids)); + one_shot(bind(vsm_get_active_zone_id, _1, &activeId)); + table.push_back({"Active", "Id", "State", "Terminal", "Root"}); + for (VsmString* id = ids; *id; ++id) { + VsmZone zone; + one_shot(bind(vsm_lookup_zone_by_id, _1, *id, &zone)); + assert(string(zone->id) == string(*id)); + table.push_back({string(zone->id) == string(activeId) ? "*" : "", + zone->id, + stringAsInStream(zone->state), + to_string(zone->terminal), + zone->rootfs_path}); + vsm_zone_free(zone); + } + vsm_string_free(activeId); + vsm_array_string_free(ids); + cout << table << endl; +} + +void get_zone_ids(int /* pos */, int /* argc */, const char** /* argv */) +{ + using namespace std::placeholders; + + VsmArrayString ids; + one_shot(bind(vsm_get_zone_ids, _1, &ids)); + string delim; + for (VsmString* id = ids; *id; ++id) { + cout << delim << *id; + delim = ", "; + } + cout << endl; + vsm_array_string_free(ids); +} + +void get_active_zone_id(int /* pos */, int /* argc */, const char** /* argv */) +{ + using namespace std::placeholders; + + VsmString id; + one_shot(bind(vsm_get_active_zone_id, _1, &id)); + cout << id << endl; + vsm_string_free(id); +} + void lookup_zone_by_id(int pos, int argc, const char** argv) { using namespace std::placeholders; diff --git a/cli/command-line-interface.hpp b/cli/command-line-interface.hpp index 6a655d8..055bb1e 100644 --- a/cli/command-line-interface.hpp +++ b/cli/command-line-interface.hpp @@ -147,6 +147,26 @@ void lock_zone(int pos, int argc, const char** argv); void unlock_zone(int pos, int argc, const char** argv); /** + * Parses command line arguments and prints list of zone with + * some useful informations (id, state, terminal, root path) + */ +void get_zones_status(int pos, int argc, const char** argv); + +/** + * Parses command line arguments and call vsm_get_zone_ids + * + * @see vsm_get_zone_ids + */ +void get_zone_ids(int pos, int argc, const char** argv); + +/** + * Parses command line arguments and call vsm_get_active_zone_id + * + * @see vsm_get_active_zone_id + */ +void get_active_zone_id(int pos, int argc, const char** argv); + +/** * Parses command line arguments and call vsm_lookup_zone_by_id * * @see vsm_lookup_zone_by_id diff --git a/cli/main.cpp b/cli/main.cpp index 1afe134..d003d10 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -92,6 +92,30 @@ std::map commands = { } }, { + "get_zones_status", { + get_zones_status, + "get_zones_status", + "Get list of zone with some useful informations (id, state, terminal, root path)", + {} + } + }, + { + "get_zone_ids", { + get_zone_ids, + "get_zone_ids", + "Get all zone ids", + {} + } + }, + { + "get_active_zone_id", { + get_active_zone_id, + "get_active_zone_id", + "Get active (foreground) zone ids", + {} + } + }, + { "lookup_zone_by_id", { lookup_zone_by_id, "lookup_zone_by_id zone_id",