#include <vist/client/schema/processes.hpp>
#include <vist/client/schema/time.hpp>
+#include <vist/exception.hpp>
#include <vist/logger.hpp>
#include <vist/query-builder.hpp>
Member VirtualRow<T>::at(Member Struct::* field) const
{
if (this->data.size() == 0)
- throw std::runtime_error("Data is not exist.");
+ THROW(ErrCode::RuntimeError) << "Data is not exist.";
std::string key = metaDB.getColumnName(field);
if (key.empty())
- throw std::runtime_error("Key is not exist.");
+ THROW(ErrCode::RuntimeError) << "Column is not exist.";
/// Convert "table.column" to "column"
std::size_t pos = key.find(".");
std::string value = this->data.at(key);
if (value.empty()) {
- ERROR(VIST) << "The value of key[" << key << "] is not exist.";
+ ERROR(VIST) << "The value of column[" << key << "] is empty.";
return Member();
}
try {
return boost::lexical_cast<Member>(value);
} catch (...) {
- ERROR(VIST) << "Failed to casting [key]: " << key;
+ THROW(ErrCode::BadCast) << "Failed to casting [key]: " << key;
return Member();
}
}
#pragma once
#include <map>
-#include <stdexcept>
#include <string>
#include <vector>
KeyValuePair data;
};
-
template <typename T>
class VirtualTable final {
public:
namespace vist {
+enum class ErrCode {
+ LogicError = 0, /// Includes invalid_argument
+ RuntimeError,
+ BadCast
+};
+
template <typename ErrCode>
class Exception final : public std::exception {
public:
#include <vist/service/vist.hpp>
+#include <vist/exception.hpp>
+#include <vist/logger.hpp>
+
#include <cstdlib>
-#include <stdexcept>
using namespace vist;
int main() try {
Vist::Instance().start();
return EXIT_SUCCESS;
-} catch(const std::exception&) {
+} catch(const Exception<ErrCode>& e) {
+ ERROR(VIST) << "Failed while daemon is running." << e.what();
+ return EXIT_FAILURE;
+} catch(const std::exception& e) {
+ ERROR(VIST) << "Failed while daemon is running." << e.what();
return EXIT_FAILURE;
}
PolicyProvider::FactoryType factory = nullptr;
loader.load(PolicyProvider::getFactoryName(), factory);
if (factory == nullptr)
- std::runtime_error("Failed to load symbol. " + PolicyProvider::getFactoryName());
+ THROW(ErrCode::RuntimeError) << "Failed to load factory: "
+ << PolicyProvider::getFactoryName();
auto provider = (*factory)();
if (provider == nullptr)
- std::runtime_error("Failed to make provider. " + PolicyProvider::getFactoryName());
+ THROW(ErrCode::RuntimeError) << "Failed to make provider: "
+ << PolicyProvider::getFactoryName();
return provider;
}
// : handle(::dlopen(path.c_str(), flag), ::dlclose)
{
if (handle == nullptr)
- throw std::invalid_argument("Failed to open: " + path);
+ THROW(ErrCode::LogicError) << "Failed to open: " << path;
}
} // namespace policy
#include <vist/sdk/policy-provider.hpp>
+#include <vist/exception.hpp>
+
#include <memory>
-#include <stdexcept>
#include <string>
#include <dlfcn.h>
{
symbol = reinterpret_cast<T>(::dlsym(handle.get(), name.c_str()));
if (symbol == nullptr)
- throw std::runtime_error("Failed to load: " + name);
+ THROW(ErrCode::RuntimeError) << "Failed to load: " << name;
}
} // namespace policy
#include "policy-manager.hpp"
#include "policy-loader.hpp"
+#include <vist/exception.hpp>
#include <vist/logger.hpp>
#include <klay/filesystem.h>
INFO(VIST) << "Load policies from :" << path;
klay::File dir(path);
if (!dir.exists() || !dir.isDirectory())
- throw std::invalid_argument("Plugin directory is wrong.: " + path);
+ THROW(ErrCode::LogicError) << "Plugin directory is wrong.: " << path;
int passed = 0, failed = 0;
klay::DirectoryIterator end;
#include "policy-storage.hpp"
-#include <exception>
#include <memory>
#include <string>
#include <unordered_map>
#include "policy-storage.hpp"
+#include <vist/exception.hpp>
#include <vist/logger.hpp>
#include <vist/query-builder.hpp>
std::string path = SCRIPT_BASE + "/" + name + ".sql";
std::ifstream is(path);
if (is.fail())
- throw std::invalid_argument("Failed to open script: " + path);
+ THROW(ErrCode::LogicError) << "Failed to open script: " << path;
std::istreambuf_iterator<char> begin(is), end;
auto content = std::string(begin, end);
if (content.empty())
- throw std::runtime_error("Failed to read script: " + path);
+ THROW(ErrCode::LogicError) << "Failed to read script: " << path;
return content;
}
stmt.bind(1, pd.name);
stmt.bind(2, pd.ivalue);
if (!stmt.exec())
- throw std::runtime_error("Failed to define policy: " + pd.name);
+ THROW(ErrCode::RuntimeError) << "Failed to define policy: " << pd.name;
}
void PolicyStorage::enroll(const std::string& name)
database::Statement stmt(*database, query);
stmt.bind(1, name);
if (!stmt.exec())
- throw std::runtime_error("Failed to enroll admin: " + name);
+ THROW(ErrCode::RuntimeError) << "Failed to enroll admin: " << name;
admins.push_back(name);
/// PolicyActivated is triggered by enrolling admin.
database::Statement stmt(*database, query);
stmt.bind(1, name);
if (!stmt.exec())
- throw std::runtime_error("Failed to disenroll admin: " + name);
+ THROW(ErrCode::RuntimeError) << "Failed to disenroll admin: " << name;
}
void PolicyStorage::update(const std::string& admin,
<< ", about: " << policy << ", value: " << std::to_string(value);
if (std::find(admins.begin(), admins.end(), admin) == admins.end())
- throw std::runtime_error("Not exist admin: " + admin);
+ THROW(ErrCode::LogicError) << "Not exist admin: " << admin;
if (definitions.find(policy) == definitions.end())
- throw std::runtime_error("Not exist policy: " + policy);
+ THROW(ErrCode::LogicError) << "Not exist policy: " << policy;
int policyValue = value;
std::string query = polActivatedTable.update(&PolicyActivated::value)
stmt.bind(2, admin);
stmt.bind(3, policy);
if (!stmt.exec())
- throw runtime::Exception("Failed to update policy:" + policy);
+ THROW(ErrCode::RuntimeError) << "Failed to update policy:" << policy;
syncPolicyActivated();
}
PolicyValue PolicyStorage::strictest(const std::string& policy)
{
if (definitions.find(policy) == definitions.end())
- throw std::runtime_error("Not exist policy: " + policy);
+ THROW(ErrCode::LogicError) << "Not exist policy: " << policy;
// There is no enrolled admins.
if (activatedPolicies.size() == 0)
}
if (strictestPtr == nullptr)
- throw std::runtime_error("Not exist managed policy: " + policy);
+ THROW(ErrCode::RuntimeError) << "Not exist managed policy: " << policy;
return std::move(*strictestPtr);
}
#include <vist/ipc/server.hpp>
#include <vist/logger.hpp>
-
-#include <stdexcept>
+#include <vist/exception.hpp>
#include <osquery/registry_interface.h>
#include <osquery/sql.h>
{
osquery::SQL sql(statement, true);
if (!sql.ok())
- throw std::runtime_error("Faild to execute query: " + sql.getMessageString());
+ THROW(ErrCode::RuntimeError) << "Faild to execute query: " << sql.getMessageString();
return std::move(sql.rows());
}