namespace {
-template<typename T>
-string stringAsInStream(const T& value)
+std::string zoneStateToString(const VsmZoneState& state)
{
- std::ostringstream stream;
- stream << value;
- return stream.str();
-}
-
-ostream& operator<<(ostream& out, const VsmZoneState& state)
-{
- const char* name;
+ std::string name;
switch (state) {
case STOPPED: name = "STOPPED"; break;
case STARTING: name = "STARTING"; break;
default: name = "MAX_STATE (ERROR)";
}
- out << name;
- return out;
+ return name;
}
-ostream& operator<<(ostream& out, const VsmZone& zone)
+std::string zoneToString(const VsmZone& zone)
{
- out << "Name: " << zone->id
- << "\nTerminal: " << zone->terminal
- << "\nState: " << zone->state
- << "\nRoot: " << zone->rootfs_path;
+ std::string out = std::string("Name: ") + vsm_zone_get_id(zone)
+ + std::string("\nTerminal: ") + std::to_string(vsm_zone_get_terminal(zone))
+ + std::string("\nState: ") + zoneStateToString(vsm_zone_get_state(zone))
+ + std::string("\nRoot: ") + vsm_zone_get_rootfs(zone);
return out;
}
-ostream& operator<<(ostream& out, const VsmNetdevType& netdevType)
+std::string netdevTypeToString(const VsmNetdevType& netdevType)
{
+ std::string type;
switch (netdevType) {
- case VSMNETDEV_VETH: out << "VETH"; break;
- case VSMNETDEV_PHYS: out << "PHYS"; break;
- case VSMNETDEV_MACVLAN: out << "MACVLAN"; break;
+ case VSMNETDEV_VETH: type = "VETH"; break;
+ case VSMNETDEV_PHYS: type = "PHYS"; break;
+ case VSMNETDEV_MACVLAN: type = "MACVLAN"; break;
}
- return out;
+ return type;
}
-ostream& operator<<(ostream& out, const VsmNetdev& netdev)
+std::string netdevToString(const VsmNetdev& netdev)
{
- out << "Name: " << netdev->name
- << "\nType: " << netdev->type;
+ std::string out = std::string("Name: ") + vsm_netdev_get_name(netdev)
+ + std::string("\nType: ") + netdevTypeToString(vsm_netdev_get_type(netdev));
return out;
}
VsmZone zone;
CommandLineInterface::executeCallback(bind(vsm_lookup_zone_by_id, _1, argv[1].c_str(), &zone));
- if (stringAsInStream(zone->state) != "RUNNING") {
+ if (zoneStateToString(vsm_zone_get_state(zone)) != "RUNNING") {
vsm_zone_free(zone);
throw runtime_error("Zone is not running");
}
- std::string zonesPath = zone->rootfs_path;
+ std::string zonesPath = vsm_zone_get_rootfs(zone);
std::string zoneRootFs = argv[1] + "/rootfs";
zonesPath.erase(zonesPath.length()- zoneRootFs.length(), zoneRootFs.length());
for (VsmString* id = ids; *id; ++id) {
VsmZone zone;
CommandLineInterface::executeCallback(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});
+ assert(string(vsm_zone_get_id(zone)) == string(*id));
+ table.push_back({string(vsm_zone_get_id(zone)) == string(activeId) ? "*" : "",
+ vsm_zone_get_id(zone),
+ zoneStateToString(vsm_zone_get_state(zone)),
+ to_string(vsm_zone_get_terminal(zone)),
+ vsm_zone_get_rootfs(zone)});
vsm_zone_free(zone);
}
vsm_string_free(activeId);
VsmZone zone;
CommandLineInterface::executeCallback(bind(vsm_lookup_zone_by_id, _1, argv[1].c_str(), &zone));
- cout << zone << endl;
+ cout << zoneToString(zone) << endl;
vsm_zone_free(zone);
}
if (argv.size() <= 2) {
throw runtime_error("Not enough parameters");
}
- VsmNetdev vsmNetdev = NULL;
+ VsmNetdev netdev = NULL;
CommandLineInterface::executeCallback(bind(vsm_lookup_netdev_by_name,
_1,
argv[1].c_str(),
argv[2].c_str(),
- &vsmNetdev));
- cout << vsmNetdev << endl;
- vsm_netdev_free(vsmNetdev);
+ &netdev));
+ cout << netdevToString(netdev) << endl;
+ vsm_netdev_free(netdev);
}
}
}
-void convert(const api::ZoneInfoOut& info, VsmZone& zone)
+void convert(const api::ZoneInfoOut& info, Zone& zone)
{
- VsmZone vsmZone = reinterpret_cast<VsmZone>(malloc(sizeof(*vsmZone)));
+ Zone vsmZone = static_cast<Zone>(malloc(sizeof(*vsmZone)));
vsmZone->id = ::strdup(info.id.c_str());
vsmZone->terminal = info.vt;
vsmZone->state = getZoneState(info.state.c_str());
});
}
-VsmStatus Client::vsm_get_zone_rootpath(const char* id, VsmString* rootpath) noexcept
-{
- return coverException([&] {
- IS_SET(id);
- IS_SET(rootpath);
-
- api::ZoneInfoOut info = *mClient->callSync<api::ZoneId, api::ZoneInfoOut>(
- api::ipc::METHOD_GET_ZONE_INFO,
- std::make_shared<api::ZoneId>(api::ZoneId{ id }));
- *rootpath = ::strdup(info.rootPath.c_str());
- });
-}
-
VsmStatus Client::vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept
{
return coverException([&] {
});
}
-VsmStatus Client::vsm_lookup_zone_by_id(const char* id, VsmZone* zone) noexcept
+VsmStatus Client::vsm_lookup_zone_by_id(const char* id, Zone* zone) noexcept
{
return coverException([&] {
IS_SET(id);
VsmStatus Client::vsm_lookup_netdev_by_name(const char* id,
const char* netdevId,
- VsmNetdev* netdev) noexcept
+ Netdev* netdev) noexcept
{
using namespace boost::algorithm;
throw InvalidResponseException("Unknown netdev type: " + it->second);
}
- *netdev = reinterpret_cast<VsmNetdev>(malloc(sizeof(**netdev)));
+ *netdev = static_cast<Netdev>(malloc(sizeof(**netdev)));
(*netdev)->name = ::strdup(id);
(*netdev)->type = type;
});
typedef std::function<void (const char *zoneId, const char *dbusAddress, void *data)> VsmZoneDbusStateFunction;
/**
+ * Zone information structure
+ */
+typedef struct ZoneStructure {
+ char *id;
+ int terminal;
+ VsmZoneState state;
+ char *rootfs_path;
+} *Zone;
+
+/**
+ * Network device information structure
+ */
+typedef struct NetdevStructure {
+ char *name;
+ VsmNetdevType type;
+} *Netdev;
+
+/**
* vasum's client definition.
*
* Client uses dbus API.
VsmStatus vsm_get_active_zone_id(VsmString* id) noexcept;
/**
- * @see ::vsm_get_zone_rootpath
- */
- VsmStatus vsm_get_zone_rootpath(const char* id, VsmString* rootpath) noexcept;
-
- /**
* @see ::vsm_lookup_zone_by_pid
*/
VsmStatus vsm_lookup_zone_by_pid(int pid, VsmString* id) noexcept;
/**
* @see ::vsm_lookup_zone_by_id
*/
- VsmStatus vsm_lookup_zone_by_id(const char* id, VsmZone* zone) noexcept;
+ VsmStatus vsm_lookup_zone_by_id(const char* id, Zone* zone) noexcept;
/**
* @see ::vsm_lookup_zone_by_terminal_id
*/
VsmStatus vsm_lookup_netdev_by_name(const char* zone,
const char* netdevId,
- VsmNetdev* netdev) noexcept;
+ Netdev* netdev) noexcept;
/**
* @see ::vsm_destroy_netdev
free(string);
}
+API VsmString vsm_zone_get_id(VsmZone zone)
+{
+ Zone z = static_cast<Zone>(zone);
+ return z->id;
+}
+
+API int vsm_zone_get_terminal(VsmZone zone)
+{
+ Zone z = static_cast<Zone>(zone);
+ return z->terminal;
+}
+
+API VsmZoneState vsm_zone_get_state(VsmZone zone)
+{
+ Zone z = static_cast<Zone>(zone);
+ return z->state;
+}
+
+API VsmString vsm_zone_get_rootfs(VsmZone zone)
+{
+ Zone z = static_cast<Zone>(zone);
+ return z->rootfs_path;
+}
+
API void vsm_zone_free(VsmZone zone)
{
- free(zone->rootfs_path);
- free(zone->id);
- free(zone);
+ Zone z = static_cast<Zone>(zone);
+ free(z->rootfs_path);
+ free(z->id);
+ free(z);
+}
+
+API VsmString vsm_netdev_get_name(VsmNetdev netdev)
+{
+ Netdev n = static_cast<Netdev>(netdev);
+ return n->name;
+}
+
+API VsmNetdevType vsm_netdev_get_type(VsmNetdev netdev)
+{
+ Netdev n = static_cast<Netdev>(netdev);
+ return n->type;
}
API void vsm_netdev_free(VsmNetdev netdev)
{
- free(netdev->name);
- free(netdev);
+ Netdev n = static_cast<Netdev>(netdev);
+ free(n->name);
+ free(n);
}
API void vsm_client_free(VsmClient client)
return getClient(client).vsm_get_active_zone_id(id);
}
-API VsmStatus vsm_get_zone_rootpath(VsmClient client, const char* id, VsmString* rootpath)
-{
- return getClient(client).vsm_get_zone_rootpath(id, rootpath);
-}
-
API VsmStatus vsm_lookup_zone_by_pid(VsmClient client, int pid, VsmString* id)
{
return getClient(client).vsm_lookup_zone_by_pid(pid, id);
API VsmStatus vsm_lookup_zone_by_id(VsmClient client, const char* id, VsmZone* zone)
{
- return getClient(client).vsm_lookup_zone_by_id(id, zone);
+ Zone* z = reinterpret_cast<Zone*>(zone);
+ return getClient(client).vsm_lookup_zone_by_id(id, z);
}
API VsmStatus vsm_lookup_zone_by_terminal_id(VsmClient client, int terminal, VsmString* id)
const char* netdevId,
VsmNetdev* netdev)
{
- return getClient(client).vsm_lookup_netdev_by_name(zone, netdevId, netdev);
+ Netdev* n = reinterpret_cast<Netdev*>(netdev);
+ return getClient(client).vsm_lookup_netdev_by_name(zone, netdevId, n);
}
API VsmStatus vsm_destroy_netdev(VsmClient client, const char* zone, const char* devId)
} VsmZoneState;
/**
- * Zone information structure
- */
-typedef struct {
- char* id;
- int terminal;
- VsmZoneState state;
- char *rootfs_path;
-} VsmZoneStructure;
-
-/**
* Zone information
*/
-typedef VsmZoneStructure* VsmZone;
+typedef void* VsmZone;
/**
* Netowrk device type
} VsmNetdevType;
/**
- * Network device information structure
- */
-typedef struct {
- char* name;
- VsmNetdevType type;
-} VsmNetdevStructure;
-
-/**
* Network device information
*/
-typedef VsmNetdevStructure* VsmNetdev;
+typedef void* VsmNetdev;
/**
* File type
void vsm_string_free(VsmString string);
/**
+ * Get zone id (offline)
+ *
+ * @param zone VsmZone
+ * @return zone id
+ */
+VsmString vsm_zone_get_id(VsmZone zone);
+
+/**
+ * Get zone terminal (offline)
+ *
+ * @param zone VsmZone
+ * @return zone terminal
+ */
+int vsm_zone_get_terminal(VsmZone zone);
+
+/**
+ * Get zone state (offline)
+ *
+ * @param zone VsmZone
+ * @return zone state
+ */
+VsmZoneState vsm_zone_get_state(VsmZone zone);
+
+/**
+ * Get zone rootfs path (offline)
+ *
+ * @param zone VsmZone
+ * @return zone rootfs path
+ */
+VsmString vsm_zone_get_rootfs(VsmZone zone);
+
+/**
* Release VsmZone
*
* @param zone VsmZone
void vsm_zone_free(VsmZone zone);
/**
+ * Get netdev name (offline)
+ *
+ * @param netdev VsmNetdev
+ * @return netdev name
+ */
+VsmString vsm_netdev_get_name(VsmNetdev netdev);
+
+/**
+ * Get netdev type (offline)
+ *
+ * @param netdev VsmNetdev
+ * @return netdev type
+ */
+VsmNetdevType vsm_netdev_get_type(VsmNetdev netdev);
+
+/**
* Release VsmNetdev
*
* @param netdev VsmNetdev
VsmStatus vsm_get_active_zone_id(VsmClient client, VsmString* id);
/**
- * Get zone rootfs path.
- *
- * @param[in] client vasum-server's client
- * @param[in] id zone name
- * @param[out] rootpath zone rootfs path
- * @return status of this function call
- * @remark Use @p vsm_string_free() to free memory occupied by @p rootpath.
- */
-VsmStatus vsm_get_zone_rootpath(VsmClient client, const char* id, VsmString* rootpath);
-
-/**
* Get zone name of process with given pid.
*
* @param[in] client vasum-server's client
vsm_client_free(client);
}
-BOOST_AUTO_TEST_CASE(GetZoneRootPath)
-{
- const std::string zoneId = "zone1";
-
- VsmClient client = vsm_client_create();
- VsmStatus status = vsm_connect(client);
- BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, status);
- VsmString rootpath;
- status = vsm_get_zone_rootpath(client, zoneId.c_str(), &rootpath);
- BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, status);
-
- BOOST_CHECK_EQUAL(rootpath, "/tmp/ut-zones/" + zoneId + "/rootfs");
-
- vsm_string_free(rootpath);
- vsm_client_free(client);
-}
-
BOOST_AUTO_TEST_CASE(LookupZoneById)
{
const std::string activeZoneId = "zone1";
status = vsm_lookup_zone_by_id(client, activeZoneId.c_str(), &info);
BOOST_REQUIRE_EQUAL(VSMCLIENT_SUCCESS, status);
- BOOST_CHECK_EQUAL(info->id, activeZoneId);
- BOOST_CHECK_EQUAL(info->state, RUNNING);
- BOOST_CHECK_EQUAL(info->terminal, -1);
- BOOST_CHECK_EQUAL(info->rootfs_path, "/tmp/ut-zones/" + activeZoneId + "/rootfs");
+ BOOST_CHECK_EQUAL(vsm_zone_get_id(info), activeZoneId);
+ BOOST_CHECK_EQUAL(vsm_zone_get_state(info), RUNNING);
+ BOOST_CHECK_EQUAL(vsm_zone_get_terminal(info), -1);
+ BOOST_CHECK_EQUAL(vsm_zone_get_rootfs(info), "/tmp/ut-zones/" + activeZoneId + "/rootfs");
vsm_zone_free(info);
vsm_client_free(client);
LOGS("");
}
-static struct vsm_zone* wrap_vsm_zone(WrappedContext *w, VsmZone zone, bool create = false)
+static struct vsm_zone* wrap_vsm_zone(WrappedContext *w, Zone zone, bool create = false)
{
if (zone == NULL) {
return NULL;
if (!w->client) return VSM_ERROR_GENERIC;
VsmStatus st = w->client->vsm_destroy_zone(zone_name);
if (st == VSMCLIENT_SUCCESS) {
- auto zonebyname = [zone_name](const WrappedZone& v) {return v.zone->id == zone_name;};
+ auto zonebyname = [zone_name](const WrappedZone& v) {
+ return static_cast<Zone>(v.zone)->id == zone_name;
+ };
auto zonelist = std::remove_if(w->zones.begin(), w->zones.end(), zonebyname);
w->zones.erase(zonelist);
}
if (!w->client) return -VSM_ERROR_GENERIC;
callback(ctx->root_zone, user_data);
for (auto& z : w->zones) {
- LOGI("iterate callback zone: " << z.zone->id);
+ LOGI("iterate callback zone: " << static_cast<Zone>(z.zone)->id);
callback(&z.vz, user_data);
}
return 0;
LOGS("name=" << path);
callcheck();
WrappedContext *w = container_of(ctx, WrappedContext, hq_ctx);
- VsmZone zone;
+ Zone zone;
if (!w->client) return NULL;
//CHECK if path is same as zone_name
if (w->client->vsm_lookup_zone_by_id(path, &zone) != VSMCLIENT_SUCCESS)
LOGS("pid=" << pid);
callcheck();
WrappedContext *w = container_of(ctx, WrappedContext, hq_ctx);
- VsmZone zone;
+ Zone zone;
VsmString id;
VsmStatus st;
if (!w->client) return NULL;
auto dbus_cb = [=](const char* id, const char* dbusAddress, void* data) ->
void {
- VsmZone zone;
+ Zone zone;
//TODO what are valid state, event
vsm_zone_state_t t = VSM_ZONE_STATE_RUNNING;
UNUSED(dbusAddress);
callcheck();
WrappedZone *w = container_of(dom, WrappedZone, vz);
const char *id = dom->name;
- VsmZone zone;
+ Zone zone;
w->client->vsm_lookup_zone_by_id(id, &zone);
VsmStatus st = w->client->vsm_grant_device(id, name, flags);
return wrap_error(st, w->client);
LOGS("");
callcheck();
WrappedZone *w = container_of(zone, WrappedZone, vz);
- VsmNetdev nd;
+ Netdev nd;
VsmStatus st = w->client->vsm_lookup_netdev_by_name(zone->name, name, &nd);
if (st == VSMCLIENT_SUCCESS) {
auto devbyname = [name](const vsm_netdev& v) {return ::strcmp(v.name, name) == 0;};
LOGS("terminal=" << terminal);
callcheck();
WrappedContext *w = container_of(ctx, WrappedContext, hq_ctx);
- VsmZone zone;
+ Zone zone;
VsmString id;
if (!w->client) return NULL;
if (w->client->vsm_lookup_zone_by_terminal_id(terminal, &id) != VSMCLIENT_SUCCESS)