Better error handling with Linux privilege escalator.
authorCarl Hetherington <cth@carlh.net>
Sun, 26 Jul 2020 21:08:01 +0000 (23:08 +0200)
committerCarl Hetherington <cth@carlh.net>
Sun, 26 Jul 2020 21:08:01 +0000 (23:08 +0200)
src/lib/cross_linux.cc
src/lib/exceptions.h

index 28b8a4b416d343824fddf94a28505d760d38e6d8..e17f188b9f8aacece46cfa29eb7cd884ab3d9797 100644 (file)
@@ -379,11 +379,9 @@ unprivileged ()
        uid_t ruid, euid, suid;
        if (getresuid(&ruid, &euid, &suid) == -1) {
                cerr << "getresuid() failed.\n";
-               exit (EXIT_FAILURE);
        }
        if (seteuid(ruid) == -1) {
                cerr << "seteuid() failed.\n";
-               exit (EXIT_FAILURE);
        }
 }
 
@@ -394,7 +392,10 @@ PrivilegeEscalator::~PrivilegeEscalator ()
 
 PrivilegeEscalator::PrivilegeEscalator ()
 {
-       seteuid (0);
+       int const r = seteuid(0);
+       if (r < 0) {
+               throw PrivilegeError (String::compose("seteuid() call failed with %1", errno));
+       }
 }
 
 boost::filesystem::path
index 98534bb32d410db108c2d8bd8f00fe220822074f..05cda9659f2c63a5b104d6fccbe896e0294e4457 100644 (file)
@@ -369,4 +369,14 @@ private:
        std::string _message;
        int _number;
 };
+
+
+class PrivilegeError : public std::runtime_error
+{
+public:
+       explicit PrivilegeError (std::string s)
+                       : std::runtime_error (s)
+               {}
+};
+
 #endif