diff --git a/src/disk_tool.h b/src/disk_tool.h index 9e7a43c4..477aed3c 100644 --- a/src/disk_tool.h +++ b/src/disk_tool.h @@ -132,7 +132,6 @@ void disk_tool_simple_offsets(json11::Json cfg, bool json_output); uint64_t sscanf_json(const char *fmt, const json11::Json & str); void fromhexstr(const std::string & from, int bytes, uint8_t *to); -std::string realpath_str(std::string path, bool nofail = true); int disable_cache(std::string dev); std::string get_parent_device(std::string dev); bool json_is_true(const json11::Json & val); diff --git a/src/disk_tool_utils.cpp b/src/disk_tool_utils.cpp index 8dfcfed0..ab94db85 100644 --- a/src/disk_tool_utils.cpp +++ b/src/disk_tool_utils.cpp @@ -42,19 +42,6 @@ void fromhexstr(const std::string & from, int bytes, uint8_t *to) } } -std::string realpath_str(std::string path, bool nofail) -{ - char *p = realpath((char*)path.c_str(), NULL); - if (!p) - { - fprintf(stderr, "Failed to resolve %s: %s\n", path.c_str(), strerror(errno)); - return nofail ? path : ""; - } - std::string rp(p); - free(p); - return rp; -} - // returns 1 = check error, 0 = write through, -1 = write back // (similar to 1 = warning, -1 = error, 0 = success in disable_cache) static int check_queue_cache(std::string dev, std::string parent_dev) diff --git a/src/nfs_proxy.cpp b/src/nfs_proxy.cpp index 07d4e903..15060840 100644 --- a/src/nfs_proxy.cpp +++ b/src/nfs_proxy.cpp @@ -1032,6 +1032,7 @@ void single_child_handler(int signal) void nfs_proxy_t::mount_fs() { + check_already_mounted(); signal(SIGCHLD, single_child_handler); auto pid = fork(); if (pid < 0) @@ -1072,6 +1073,30 @@ void nfs_proxy_t::mount_fs() } } +void nfs_proxy_t::check_already_mounted() +{ + std::string realpoint = realpath_str(mountpoint, false); + if (realpoint == "") + { + return; + } + std::string mountstr = read_file("/proc/mounts"); + if (mountstr == "") + { + return; + } + auto mounts = explode("\n", mountstr, true); + for (auto & str: mounts) + { + auto mnt = explode(" ", str, true); + if (mnt.size() >= 2 && mnt[1] == realpoint) + { + fprintf(stderr, "%s is already mounted\n", mountpoint.c_str()); + exit(1); + } + } +} + void nfs_proxy_t::check_exit() { if (active_connections || !exit_on_umount) @@ -1084,7 +1109,7 @@ void nfs_proxy_t::check_exit() return; } auto port_opt = "port="+std::to_string(listening_port); - auto mountport_opt = "port="+std::to_string(listening_port); + auto mountport_opt = "mountport="+std::to_string(listening_port); auto mounts = explode("\n", mountstr, true); for (auto & str: mounts) { diff --git a/src/nfs_proxy.h b/src/nfs_proxy.h index ae40c4d3..8dbd164e 100644 --- a/src/nfs_proxy.h +++ b/src/nfs_proxy.h @@ -66,6 +66,7 @@ public: void daemonize(); void write_pid(); void mount_fs(); + void check_already_mounted(); void check_exit(); }; diff --git a/src/str_util.cpp b/src/str_util.cpp index d96b9303..1b78f1ad 100644 --- a/src/str_util.cpp +++ b/src/str_util.cpp @@ -428,3 +428,16 @@ std::string auto_addslashes(const std::string & str) } return res+"\""; } + +std::string realpath_str(std::string path, bool nofail) +{ + char *p = realpath((char*)path.c_str(), NULL); + if (!p) + { + fprintf(stderr, "Failed to resolve %s: %s\n", path.c_str(), strerror(errno)); + return nofail ? path : ""; + } + std::string rp(p); + free(p); + return rp; +} diff --git a/src/str_util.h b/src/str_util.h index 9c1bd8ed..3fb03be1 100644 --- a/src/str_util.h +++ b/src/str_util.h @@ -25,3 +25,4 @@ size_t utf8_length(const char *s); std::vector explode(const std::string & sep, const std::string & value, bool trim); std::string scan_escaped(const std::string & cmd, size_t & pos); std::string auto_addslashes(const std::string & str); +std::string realpath_str(std::string path, bool nofail = true);