Defer daemonization to Fuse, introduce Fuse::runInBackground() / Fuse::runInForeground()

This commit is contained in:
Sebastian Messmer 2018-12-22 02:26:38 +01:00
parent 15aabee1ae
commit d8691d282b
6 changed files with 40 additions and 10 deletions

View File

@ -268,11 +268,15 @@ namespace cryfs {
std::cout << "\nMounting filesystem. To unmount, call:\n$ fusermount -u " << options.mountDir() << "\n" std::cout << "\nMounting filesystem. To unmount, call:\n$ fusermount -u " << options.mountDir() << "\n"
<< std::endl; << std::endl;
#endif #endif
fuse->run(options.mountDir(), options.fuseOptions()); if (options.foreground()) {
fuse->runInForeground(options.mountDir(), options.fuseOptions());
} else {
fuse->runInBackground(options.mountDir(), options.fuseOptions());
}
if (stoppedBecauseOfIntegrityViolation) { if (stoppedBecauseOfIntegrityViolation) {
throw CryfsException("Integrity violation detected. Unmounting.", ErrorCode::IntegrityViolation); throw CryfsException("Integrity violation detected. Unmounting.", ErrorCode::IntegrityViolation);
} }
} catch (const CryfsException &e) { } catch (const CryfsException &e) {
throw; // CryfsException is only thrown if setup goes wrong. Throw it through so that we get the correct process exit code. throw; // CryfsException is only thrown if setup goes wrong. Throw it through so that we get the correct process exit code.
} catch (const std::exception &e) { } catch (const std::exception &e) {

View File

@ -56,9 +56,6 @@ ProgramOptions Parser::parse(const vector<string> &supportedCiphers) const {
configfile = bf::absolute(vm["config"].as<string>()); configfile = bf::absolute(vm["config"].as<string>());
} }
bool foreground = vm.count("foreground"); bool foreground = vm.count("foreground");
if (foreground) {
fuseOptions.push_back(const_cast<char*>("-f"));
}
bool allowFilesystemUpgrade = vm.count("allow-filesystem-upgrade"); bool allowFilesystemUpgrade = vm.count("allow-filesystem-upgrade");
bool allowReplacedFilesystem = vm.count("allow-replaced-filesystem"); bool allowReplacedFilesystem = vm.count("allow-replaced-filesystem");
optional<double> unmountAfterIdleMinutes = 0.0; // first setting to 0 and then to none is somehow needed to silence a GCC warning from -Wmaybe-uninitialized optional<double> unmountAfterIdleMinutes = 0.0; // first setting to 0 and then to none is somehow needed to silence a GCC warning from -Wmaybe-uninitialized

View File

@ -243,7 +243,33 @@ void Fuse::_logUnknownException() {
LOG(ERR, "Unknown exception thrown"); LOG(ERR, "Unknown exception thrown");
} }
void Fuse::run(const bf::path &mountdir, const vector<string> &fuseOptions) { void Fuse::runInForeground(const bf::path &mountdir, const vector<string> &fuseOptions) {
vector<string> realFuseOptions = fuseOptions;
if (std::find(realFuseOptions.begin(), realFuseOptions.end(), "-f") == realFuseOptions.end()) {
realFuseOptions.push_back("-f");
}
_run(mountdir, realFuseOptions);
}
void Fuse::runInBackground(const bf::path &mountdir, const vector<string> &fuseOptions) {
vector<string> realFuseOptions = fuseOptions;
_removeAndWarnIfExists(&realFuseOptions, "-f");
_removeAndWarnIfExists(&realFuseOptions, "-d");
_run(mountdir, realFuseOptions);
}
void Fuse::_removeAndWarnIfExists(vector<string> *fuseOptions, const std::string &option) {
auto found = std::find(fuseOptions->begin(), fuseOptions->end(), option);
if (found != fuseOptions->end()) {
LOG(WARN, "The fuse option {} only works when running in foreground. Removing fuse option.", option);
do {
fuseOptions->erase(found);
found = std::find(fuseOptions->begin(), fuseOptions->end(), option);
} while (found != fuseOptions->end());
}
}
void Fuse::_run(const bf::path &mountdir, const vector<string> &fuseOptions) {
_mountdir = mountdir; _mountdir = mountdir;
ASSERT(_argv.size() == 0, "Filesystem already started"); ASSERT(_argv.size() == 0, "Filesystem already started");

View File

@ -24,7 +24,8 @@ public:
explicit Fuse(std::function<std::shared_ptr<Filesystem> (Fuse *fuse)> init, std::function<void()> onMounted, std::string fstype, boost::optional<std::string> fsname); explicit Fuse(std::function<std::shared_ptr<Filesystem> (Fuse *fuse)> init, std::function<void()> onMounted, std::string fstype, boost::optional<std::string> fsname);
~Fuse(); ~Fuse();
void run(const boost::filesystem::path &mountdir, const std::vector<std::string> &fuseOptions); void runInBackground(const boost::filesystem::path &mountdir, const std::vector<std::string> &fuseOptions);
void runInForeground(const boost::filesystem::path &mountdir, const std::vector<std::string> &fuseOptions);
bool running() const; bool running() const;
void stop(); void stop();
@ -63,6 +64,8 @@ private:
static void _logException(const std::exception &e); static void _logException(const std::exception &e);
static void _logUnknownException(); static void _logUnknownException();
static char *_create_c_string(const std::string &str); static char *_create_c_string(const std::string &str);
static void _removeAndWarnIfExists(std::vector<std::string> *fuseOptions, const std::string &option);
void _run(const boost::filesystem::path &mountdir, const std::vector<std::string> &fuseOptions);
static bool _has_option(const std::vector<char *> &vec, const std::string &key); static bool _has_option(const std::vector<char *> &vec, const std::string &key);
static bool _has_entry_with_prefix(const std::string &prefix, const std::vector<char *> &vec); static bool _has_entry_with_prefix(const std::string &prefix, const std::vector<char *> &vec);
std::vector<char *> _build_argv(const boost::filesystem::path &mountdir, const std::vector<std::string> &fuseOptions); std::vector<char *> _build_argv(const boost::filesystem::path &mountdir, const std::vector<std::string> &fuseOptions);

View File

@ -59,7 +59,7 @@ FuseTest::TempTestFS::TempTestFS(shared_ptr<MockFilesystem> fsimpl)
:_mountDir(), :_mountDir(),
_fuse([fsimpl] (Fuse*) {return fsimpl;}, []{}, "fusetest", boost::none), _fuse_thread(&_fuse) { _fuse([fsimpl] (Fuse*) {return fsimpl;}, []{}, "fusetest", boost::none), _fuse_thread(&_fuse) {
_fuse_thread.start(_mountDir.path(), {"-f"}); _fuse_thread.start(_mountDir.path(), {});
} }
FuseTest::TempTestFS::~TempTestFS() { FuseTest::TempTestFS::~TempTestFS() {

View File

@ -18,7 +18,7 @@ FuseThread::FuseThread(Fuse *fuse)
void FuseThread::start(const bf::path &mountDir, const vector<string> &fuseOptions) { void FuseThread::start(const bf::path &mountDir, const vector<string> &fuseOptions) {
_child = thread([this, mountDir, fuseOptions] () { _child = thread([this, mountDir, fuseOptions] () {
_fuse->run(mountDir, fuseOptions); _fuse->runInForeground(mountDir, fuseOptions);
}); });
//Wait until it is running (busy waiting is simple and doesn't hurt much here) //Wait until it is running (busy waiting is simple and doesn't hurt much here)
while(!_fuse->running()) {} while(!_fuse->running()) {}