Get root before unmounting things on Linux.
[dcpomatic.git] / src / lib / cross_linux.cc
index b6241ec1a3f38763b76e19b03aad2df74fac187b..510bce8c3df4e141ed3c0e8c113c5a8e0a4d7719 100644 (file)
@@ -31,7 +31,6 @@ extern "C" {
 #include <libavformat/avio.h>
 }
 #include <boost/algorithm/string.hpp>
-#include <boost/foreach.hpp>
 #include <boost/function.hpp>
 #if BOOST_VERSION >= 106100
 #include <boost/dll/runtime_symbol_info.hpp>
@@ -57,7 +56,7 @@ using std::vector;
 using std::cerr;
 using std::cout;
 using std::runtime_error;
-using boost::shared_ptr;
+using std::shared_ptr;
 using boost::optional;
 using boost::function;
 
@@ -108,6 +107,9 @@ resources_path ()
 boost::filesystem::path
 xsd_path ()
 {
+       if (auto appdir = getenv("APPDIR")) {
+               return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp" / "xsd";
+       }
        return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "xsd";
 }
 
@@ -115,6 +117,9 @@ xsd_path ()
 boost::filesystem::path
 tags_path ()
 {
+       if (auto appdir = getenv("APPDIR")) {
+               return boost::filesystem::path(appdir) / "usr" / "share" / "libdcp" / "tags";
+       }
        return boost::filesystem::canonical(LINUX_SHARE_PREFIX) / "libdcp" / "tags";
 }
 
@@ -306,10 +311,10 @@ Drive::get ()
        vector<Drive> drives;
 
        using namespace boost::filesystem;
-       vector<pair<string, string> > mounted_devices = get_mounts("/dev/");
+       auto mounted_devices = get_mounts("/dev/");
 
-       for (directory_iterator i = directory_iterator("/sys/block"); i != directory_iterator(); ++i) {
-               string const name = i->path().filename().string();
+       for (auto i: directory_iterator("/sys/block")) {
+               string const name = i.path().filename().string();
                path device_type_file("/sys/block/" + name + "/device/type");
                optional<string> device_type;
                if (exists(device_type_file)) {
@@ -318,7 +323,7 @@ Drive::get ()
                }
                /* Device type 5 is "SCSI_TYPE_ROM" in blkdev.h; seems usually to be a CD/DVD drive */
                if (!boost::algorithm::starts_with(name, "loop") && (!device_type || *device_type != "5")) {
-                       uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(*i / "size")) * 512;
+                       uint64_t const size = dcp::raw_convert<uint64_t>(dcp::file_to_string(i / "size")) * 512;
                        if (size == 0) {
                                continue;
                        }
@@ -333,9 +338,9 @@ Drive::get ()
                                boost::trim(*model);
                        } catch (...) {}
                        vector<boost::filesystem::path> mount_points;
-                       for (vector<pair<string, string> >::const_iterator j = mounted_devices.begin(); j != mounted_devices.end(); ++j) {
-                               if (boost::algorithm::starts_with(j->first, "/dev/" + name)) {
-                                       mount_points.push_back (j->second);
+                       for (auto const& j: mounted_devices) {
+                               if (boost::algorithm::starts_with(j.first, "/dev/" + name)) {
+                                       mount_points.push_back (j.second);
                                }
                        }
                        drives.push_back(Drive("/dev/" + name, mount_points, size, vendor, model));
@@ -350,7 +355,8 @@ Drive::get ()
 bool
 Drive::unmount ()
 {
-       BOOST_FOREACH (boost::filesystem::path i, _mount_points) {
+       for (auto i: _mount_points) {
+               PrivilegeEscalator esc;
                int const r = umount(i.string().c_str());
                LOG_DISK("Tried to unmount %1 and got %2 and %3", i.string(), r, errno);
                if (r == -1) {
@@ -373,16 +379,23 @@ unprivileged ()
        }
 }
 
+
+bool PrivilegeEscalator::test = false;
+
 PrivilegeEscalator::~PrivilegeEscalator ()
 {
-       unprivileged ();
+       if (!test) {
+               unprivileged ();
+       }
 }
 
 PrivilegeEscalator::PrivilegeEscalator ()
 {
-       int const r = seteuid(0);
-       if (r < 0) {
-               throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
+       if (!test) {
+               int const r = seteuid(0);
+               if (r < 0) {
+                       throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
+               }
        }
 }
 
@@ -402,3 +415,8 @@ disk_write_finished ()
 
 }
 
+string
+dcpomatic::get_process_id ()
+{
+       return dcp::raw_convert<string>(getpid());
+}