lxcpp: Setting up the control terminal 13/48213/3
authorJan Olszak <j.olszak@samsung.com>
Wed, 16 Sep 2015 09:22:22 +0000 (11:22 +0200)
committerLukasz Pawelczyk <l.pawelczyk@samsung.com>
Wed, 16 Sep 2015 12:57:46 +0000 (05:57 -0700)
[Feature]       Setting up the control terminal in Attach
[Cause]         N/A
[Solution]      N/A
[Verification]  Build, install, run tests

Change-Id: I6b1dced4f9a16c04e82b122679f86b90be29d3d1

libs/lxcpp/commands/attach.cpp
libs/lxcpp/commands/attach.hpp
libs/lxcpp/container-impl.cpp
libs/lxcpp/credentials.hpp

index 0ff856e..01a811b 100644 (file)
 #include "lxcpp/credentials.hpp"
 
 #include "utils/exception.hpp"
+#include "utils/fd-utils.hpp"
+#include "logger/logger.hpp"
 
 #include <unistd.h>
 #include <sys/mount.h>
+#include <sys/types.h>
+#include <fcntl.h>
 
 #include <functional>
 
@@ -67,6 +71,35 @@ void setupMountPoints()
     */
 }
 
+bool setupControlTTY(const int ttyFD)
+{
+    if (!::isatty(ttyFD)) {
+        return false;
+    }
+
+    if (::setsid() < 0) {
+        return false;
+    }
+
+    if (::ioctl(ttyFD, TIOCSCTTY, NULL) < 0) {
+        return false;
+    }
+
+    if (::dup2(ttyFD, STDIN_FILENO) < 0) {
+        return false;
+    }
+
+    if (::dup2(ttyFD, STDOUT_FILENO) < 0) {
+        return false;
+    }
+
+    if (::dup2(ttyFD, STDERR_FILENO) < 0) {
+        return false;
+    }
+
+    return true;
+}
+
 int execFunction(void* call)
 {
     try {
@@ -83,6 +116,7 @@ Attach::Attach(lxcpp::ContainerImpl& container,
                Container::AttachCall& userCall,
                const uid_t uid,
                const gid_t gid,
+               const std::string& ttyPath,
                const std::vector<gid_t>& supplementaryGids,
                const int capsToKeep,
                const std::string& workDirInContainer,
@@ -98,10 +132,18 @@ Attach::Attach(lxcpp::ContainerImpl& container,
       mEnvToKeep(envToKeep),
       mEnvToSet(envToSet)
 {
+    mTTYFD = ::open(ttyPath.c_str(), O_RDWR | O_NOCTTY);
+    if (mTTYFD < 0) {
+        const std::string msg = "open() failed: " +
+                                utils::getSystemErrorMessage();
+        LOGE(msg);
+        throw BadArgument(msg);
+    }
 }
 
 Attach::~Attach()
 {
+    utils::close(mTTYFD);
 }
 
 void Attach::execute()
@@ -113,6 +155,7 @@ void Attach::execute()
                           mUserCall,
                           mUid,
                           mGid,
+                          mTTYFD,
                           mSupplementaryGids,
                           mCapsToKeep,
                           mEnvToKeep,
@@ -127,13 +170,14 @@ void Attach::execute()
         intermChannel.setRight();
         interm(intermChannel, call);
         intermChannel.shutdown();
-        ::_exit(0);
+        ::_exit(EXIT_SUCCESS);
     }
 }
 
 int Attach::child(const Container::AttachCall& call,
                   const uid_t uid,
                   const gid_t gid,
+                  const int ttyFD,
                   const std::vector<gid_t>& supplementaryGids,
                   const int capsToKeep,
                   const std::vector<std::string>& envToKeep,
@@ -155,6 +199,11 @@ int Attach::child(const Container::AttachCall& call,
 
     lxcpp::setuid(uid);
 
+    // Set control TTY
+    if(!setupControlTTY(ttyFD)) {
+        ::_exit(EXIT_FAILURE);
+    }
+
     // Run user's code
     return call();
 }
index 36c57ba..2c1f365 100644 (file)
@@ -58,6 +58,7 @@ public:
            Container::AttachCall& userCall,
            const uid_t uid,
            const gid_t gid,
+           const std::string& ttyPath,
            const std::vector<gid_t>& supplementaryGids,
            const int capsToKeep,
            const std::string& workDirInContainer,
@@ -72,6 +73,7 @@ private:
     const Container::AttachCall& mUserCall;
     const uid_t mUid;
     const gid_t mGid;
+    int mTTYFD;
     const std::vector<gid_t>& mSupplementaryGids;
     const int mCapsToKeep;
     const std::string& mWorkDirInContainer;
@@ -82,6 +84,7 @@ private:
     static int child(const Container::AttachCall& call,
                      const uid_t uid,
                      const gid_t gid,
+                     const int ttyFD,
                      const std::vector<gid_t>& supplementaryGids,
                      const int capsToKeep,
                      const std::vector<std::string>& envToKeep,
index 73e5254..8249009 100644 (file)
@@ -152,6 +152,7 @@ void ContainerImpl::attach(Container::AttachCall& call,
                   call,
                   /*uid in container*/ 0,
                   /*gid in container*/ 0,
+                  "/dev/tty",
                   /*supplementary gids in container*/ {},
                   /*capsToKeep*/ 0,
                   cwdInContainer,
index df00ce5..ab1a490 100644 (file)
@@ -36,8 +36,6 @@ void setgid(const gid_t gid);
 
 void setuid(const uid_t uid);
 
-
-
 } // namespace lxcpp
 
 #endif // LXCPP_CREDENTIALS_HPP
\ No newline at end of file