#include "host-dbus-definitions.hpp"
#include "common-dbus-definitions.hpp"
#include "zone-dbus-definitions.hpp"
+#include "dynamic-config-scheme.hpp"
#include "zones-manager.hpp"
#include "zone-admin.hpp"
#include "lxc/cgroup.hpp"
return false;
}
-const std::string DB_PREFIX = "daemon";
const std::string HOST_ID = "host";
const std::string ENABLED_FILE_NAME = "enabled";
LOGD("Instantiating ZonesManager object...");
config::loadFromJsonFile(configPath, mConfig);
- config::loadFromKVStoreWithJsonFile(mConfig.dbPath, configPath, mDynamicConfig, DB_PREFIX);
+ config::loadFromKVStoreWithJsonFile(mConfig.dbPath,
+ configPath,
+ mDynamicConfig,
+ getVasumDbPrefix());
mProxyCallPolicy.reset(new ProxyCallPolicy(mConfig.proxyCallRules));
this, _1, _2, _3));
for (const auto& zoneConfig : mDynamicConfig.zoneConfigs) {
- createZone(utils::createFilePath(mConfig.zoneNewConfigPrefix, zoneConfig));
+ insertZone(utils::createFilePath(mConfig.zoneNewConfigPrefix, zoneConfig));
}
updateDefaultId();
{
auto iter = findZone(id);
if (iter == mZones.end()) {
- throw std::out_of_range("id not found");
+ throw InvalidZoneIdException("Zone id not found");
}
return get(iter);
}
void ZonesManager::saveDynamicConfig()
{
- config::saveToKVStore(mConfig.dbPath, mDynamicConfig, DB_PREFIX);
+ config::saveToKVStore(mConfig.dbPath, mDynamicConfig, getVasumDbPrefix());
}
void ZonesManager::updateDefaultId()
saveDynamicConfig();
}
-void ZonesManager::createZone(const std::string& zoneConfigPath)
+void ZonesManager::insertZone(const std::string& zoneConfigPath)
{
LOGT("Creating Zone " << zoneConfigPath);
std::unique_ptr<Zone> zone(new Zone(mWorker->createSubWorker(),
mConfig.runMountPointPrefix));
const std::string id = zone->getId();
if (id == HOST_ID) {
- throw ZoneOperationException("Cannot use reserved zone ID");
+ throw InvalidZoneIdException("Cannot use reserved zone ID");
+ }
+ if (findZone(id) != mZones.end()) {
+ throw InvalidZoneIdException("Zone already exists");
}
using namespace std::placeholders;
zone->setDbusStateChangedCallback(bind(&ZonesManager::handleDbusStateChanged,
this, id, _1));
- Lock lock(mMutex);
-
- if (findZone(id) != mZones.end()) {
- throw ZoneOperationException("Zone already exists");
- }
mZones.push_back(std::move(zone));
// after zone is created successfully, put a file informing that zones are enabled
auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("Failed to destroy zone " << zoneId << ": no such zone");
- throw ZoneOperationException("No such zone");
+ throw InvalidZoneIdException("No such zone");
}
get(iter).setDestroyOnExit();
auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("No such zone id: " << zoneId);
- throw ZoneOperationException("No such zone");
+ throw InvalidZoneIdException("No such zone");
}
return get(iter).isPaused();
auto iter = findZone(zoneId);
if (iter == mZones.end()) {
LOGE("No such zone id: " << zoneId);
- throw ZoneOperationException("No such zone");
+ throw InvalidZoneIdException("No such zone");
}
return get(iter).isRunning();
}
const std::string id = getZone(zone).declareFile(type, path, flags, mode);
result->set(g_variant_new("(s)", id.c_str()));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const config::ConfigException& ex) {
const std::string id = getZone(zone).declareMount(source, target, type, flags, data);
result->set(g_variant_new("(s)", id.c_str()));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const config::ConfigException& ex) {
const std::string id = getZone(zone).declareLink(source, target);
result->set(g_variant_new("(s)", id.c_str()));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const config::ConfigException& ex) {
out.data(),
out.size());
result->set(g_variant_new("(@as)", array));
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const VasumException& ex) {
getZone(zone).removeDeclaration(declarationId);
result->setVoid();
- } catch (const std::out_of_range&) {
+ } catch (const InvalidZoneIdException&) {
LOGE("No zone with id=" << zone);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const VasumException& ex) {
return *candidates.begin();
}
-void ZonesManager::handleCreateZoneCall(const std::string& id,
- const std::string& templateName,
- dbus::MethodResultBuilder::Pointer result)
+void ZonesManager::createZone(const std::string& id,
+ const std::string& templateName)
{
- if (id.empty()) {
+ if (id.empty()) { // TODO validate id (no spaces, slashes etc)
LOGE("Failed to add zone - invalid name.");
- result->setError(api::ERROR_INVALID_ID, "Invalid name");
- return;
+ throw InvalidZoneIdException("Invalid name");
}
LOGI("Creating zone " << id);
// check if zone does not exist
if (findZone(id) != mZones.end()) {
LOGE("Cannot create " << id << " zone - already exists!");
- result->setError(api::ERROR_INVALID_ID, "Already exists");
- return;
+ throw InvalidZoneIdException("Already exists");
}
const std::string zonePathStr = utils::createFilePath(mConfig.zonesPath, id, "/");
if (!utils::launchAsRoot(copyImageContentsWrapper)) {
LOGE("Failed to copy zone image.");
- result->setError(api::ERROR_INTERNAL, "Failed to copy zone image.");
- return;
+ throw ZoneOperationException("Failed to copy zone image.");
}
}
} catch (VasumException& e) {
LOGE("Generate config failed: " << e.what());
utils::launchAsRoot(std::bind(removeAllWrapper, zonePathStr));
- result->setError(api::ERROR_INTERNAL, "Failed to generate config");
- return;
+ throw e;
}
LOGT("Creating new zone");
try {
- createZone(newConfigPath);
+ insertZone(newConfigPath);
} catch (VasumException& e) {
LOGE("Creating new zone failed: " << e.what());
utils::launchAsRoot(std::bind(removeAllWrapper, zonePathStr));
- result->setError(api::ERROR_INTERNAL, "Failed to create zone");
- return;
+ throw e;
}
mDynamicConfig.zoneConfigs.push_back(newConfigName);
saveDynamicConfig();
updateDefaultId();
+}
- result->setVoid();
+void ZonesManager::handleCreateZoneCall(const std::string& id,
+ const std::string& templateName,
+ dbus::MethodResultBuilder::Pointer result)
+{
+ try {
+ createZone(id, templateName);
+ result->setVoid();
+ } catch (const InvalidZoneIdException& e) {
+ result->setError(api::ERROR_INVALID_ID, "Existing or invalid zone id");
+ } catch (const VasumException& e) {
+ result->setError(api::ERROR_INTERNAL, "Failed to create zone");
+ }
}
void ZonesManager::handleDestroyZoneCall(const std::string& id,
LOGI("Destroying zone " << id);
destroyZone(id);
- } catch (const ZoneOperationException& e) {
+ } catch (const InvalidZoneIdException&) {
LOGE("Failed to destroy zone - no such zone id: " << id);
result->setError(api::ERROR_INVALID_ID, "No such zone id");
} catch (const VasumException& e) {